gpt4 book ai didi

python - 使用 TensorFlow.js 加载 Keras 模型时,"The first layer in a Sequential model must get an ` inputShape ` or ` batchInputShape ` argument."

转载 作者:行者123 更新时间:2023-11-30 09:16:07 24 4
gpt4 key购买 nike

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

# imports ...
model = Sequential()
model.add(Conv2D(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")

输入是形状为(28, 28, 1)的 28 x 28 灰度图像。

Model Overview

我用 tensorflowjs_converter 转换了模型现在我想使用 TensorFlow.js 将其加载到我的网站中(版本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)

关于python - 使用 TensorFlow.js 加载 Keras 模型时,"The first layer in a Sequential model must get an ` inputShape ` or ` batchInputShape ` argument.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55798841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com