Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;顺序模型中的第一层必须获得“inputShape”或“batchInputShape”参数;使用TensorFlow.js加载Keras模型时_Python_Tensorflow_Machine Learning_Keras_Tensorflow.js - Fatal编程技术网

Python &引用;顺序模型中的第一层必须获得“inputShape”或“batchInputShape”参数;使用TensorFlow.js加载Keras模型时

Python &引用;顺序模型中的第一层必须获得“inputShape”或“batchInputShape”参数;使用TensorFlow.js加载Keras模型时,python,tensorflow,machine-learning,keras,tensorflow.js,Python,Tensorflow,Machine Learning,Keras,Tensorflow.js,我使用(版本2.2.4)培训了以下模型: #导入。。。 模型=顺序() 添加(Conv2D(filters=64,kernel\u size=5,data\u format=“channels\u last”,activation=“relu”)) 添加(BatchNormalization()) model.add(MaxPooling2D(data\u format=“channels\u last”)) 添加(Conv2D(filters=32,kernel\u size=3,data\u

我使用(版本2.2.4)培训了以下模型:

#导入。。。
模型=顺序()
添加(Conv2D(filters=64,kernel\u size=5,data\u format=“channels\u last”,activation=“relu”))
添加(BatchNormalization())
model.add(MaxPooling2D(data\u format=“channels\u last”))
添加(Conv2D(filters=32,kernel\u size=3,data\u format=“channels\u last”,activation=“relu”))
添加(BatchNormalization())
model.add(MaxPooling2D(data\u format=“channels\u last”))
model.add(展平(data\u format=“channels\u last”))
model.add(密集(单位=256,activation=“relu”))
添加(密集(单位=128,激活=“relu”))
添加(密集(单位=32,激活=“relu”))
添加(密集(单位=8,激活=“softmax”))
#训练。。。
模型保存(“模型h5”)
输入是形状的28 x 28灰度图像
(28,28,1)

我使用转换了模型,现在我想使用(版本1.1.0)将其加载到我的网站中:

tf.loadLayersModel('./model/model.json')
这会产生以下错误:

The first layer in a Sequential model must get an `inputShape` or `batchInputShape` argument.
    at new e (errors.ts:48)
    at e.add (models.ts:440)
    at e.fromConfig (models.ts:1020)
    at vp (generic_utils.ts:277)
    at nd (serialization.ts:31)
    at models.ts:299
    at common.ts:14
    at Object.next (common.ts:14)
    at o (common.ts:14)

如何在不必重新训练模型的情况下修复此错误?

尝试将您的神经网络调整为此格式:

input_img = Input(batch_shape=(None, 28,28,1))
layer1=Conv2D(filters=64, kernel_size=5, data_format="channels_last", activation="relu")(input_img)
layer2=BatchNormalization()(layer1)
.
.
.
final_layer=Dense(units=8, activation="softmax")(previous_layer)
。。。等等最后:

model = Model(inputs = input_img, outputs = final_layer)

尝试将您的神经网络调整为以下格式:

input_img = Input(batch_shape=(None, 28,28,1))
layer1=Conv2D(filters=64, kernel_size=5, data_format="channels_last", activation="relu")(input_img)
layer2=BatchNormalization()(layer1)
.
.
.
final_layer=Dense(units=8, activation="softmax")(previous_layer)
。。。等等最后:

model = Model(inputs = input_img, outputs = final_layer)

必须在keras模型的Conv2D层中指定输入形状

# imports ...
model = Sequential()
model.add(Conv2D(input_shape=(28, 28, 1), filters=64, kernel_size=5, data_format="channels_last", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(data_format="channels_last"))
model.add(Conv2D(filters=32, kernel_size=3, data_format="channels_last", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(data_format="channels_last"))
model.add(Flatten(data_format="channels_last"))
model.add(Dense(units=256, activation="relu"))
model.add(Dense(units=128, activation="relu"))
model.add(Dense(units=32, activation="relu"))
model.add(Dense(units=8, activation="softmax"))
# training ...
model.save("model.h5")

必须在keras模型的Conv2D层中指定输入形状

# imports ...
model = Sequential()
model.add(Conv2D(input_shape=(28, 28, 1), filters=64, kernel_size=5, data_format="channels_last", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(data_format="channels_last"))
model.add(Conv2D(filters=32, kernel_size=3, data_format="channels_last", activation="relu"))
model.add(BatchNormalization())
model.add(MaxPooling2D(data_format="channels_last"))
model.add(Flatten(data_format="channels_last"))
model.add(Dense(units=256, activation="relu"))
model.add(Dense(units=128, activation="relu"))
model.add(Dense(units=32, activation="relu"))
model.add(Dense(units=8, activation="softmax"))
# training ...
model.save("model.h5")

最好的方法是改变你的keras模型并进行再培训

无论如何,如果无法重新训练网络,可以手动编辑
model.json
文件

您需要在
model.json
文件中找到输入层并添加

  "config": {
    ...
    "batch_input_shape": [
      null,
      28,
      28,
      1
    ]
    ...
  }

最好的方法是改变你的keras模型并进行再培训

无论如何,如果无法重新训练网络,可以手动编辑
model.json
文件

您需要在
model.json
文件中找到输入层并添加

  "config": {
    ...
    "batch_input_shape": [
      null,
      28,
      28,
      1
    ]
    ...
  }

如果不定义输入,我认为不可能训练您的模型shape@Primusa训练成功了,正如您在上图中所看到的,正确的输入形状得到了正确的派生。请尝试运行您的模型定义代码,并在某个虚拟机上实际编译/训练它data@Primusa我做到了,而且成功了。损失减少,并创建了model.h5文件。如果它不起作用,我就无法转换模型。也许可以尝试使用
model保存权重。保存权重
,然后在定义了输入形状的架构上执行
model.load\u权重
,然后保存模型并转换我认为如果不定义输入,就不可能训练模型shape@Primusa训练成功了,正如您在上图中所看到的,正确的输入形状得到了正确的派生。请尝试运行您的模型定义代码,并在某个虚拟机上实际编译/训练它data@Primusa我做到了,它成功了。损失减少,并创建了model.h5文件。如果它不起作用,我将无法转换模型。也许可以尝试使用
model保存权重。保存权重
,然后执行
model。在定义了输入形状的架构上加载权重,然后保存该模型并转换。你能详细说明一下吗,我不太清楚?我必须再训练一次模特吗?这不能在JavaScript代码中解决吗?您必须调整python代码,然后将模型转换为TensorFlow.js您能详细说明一下吗,我不太清楚?我必须再训练一次模特吗?这不能在JavaScript代码中解决吗?您必须调整python代码,然后将模型转换为TensorFlow.jsI。我知道我首先应该这样做。但问题是我能否在事后解决这个问题,因为我不想再次训练这个模型。但现在一切都结束了,因为我不得不因为过度装配而改变模型的架构。所以我不再寻找解决方案了。我知道我本来应该这样做的。但问题是我能否在事后解决这个问题,因为我不想再次训练这个模型。但现在一切都结束了,因为我不得不因为过度装配而改变模型的架构。所以我不再寻找解决方案了。