如何为现有模型添加更多层(例如可教机器应用模型)?
How to add more layers to existing model (eg. teachable machine application model)?
我正在尝试通过在输出层之前添加更多层来使用可教学机器应用程序 https://teachablemachine.withgoogle.com/ 中的 google 模型。
当我重新训练模型时,总是 return 这个错误:
ValueError:层 dense_25 的输入 0 与层不兼容:输入形状的预期轴 -1 的值为 5 但收到的输入形状为 [20, 512]
这是我的方法:
重新训练模型时 return 错误:
如果我在不添加新层的情况下重新训练模型,它工作正常。
谁能告诉我问题出在哪里?
更新的答案
如果要在 pre-trained 模型的两层之间添加层,它不像使用 add 方法添加层那样简单。如果这样做将导致 un-expected 行为
错误分析:
如果你像下面那样编译模型(像你指定的那样):
model.layers[-1].add(Dense(512, activation ="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(32))
model.add(Dense(5))
模型摘要输出:
Model: "sequential_12"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
sequential_9 (Sequential) (None, 1280) 410208
_________________________________________________________________
sequential_11 (Sequential) (None, 512) 131672
_________________________________________________________________
dense_12 (Dense) (None, 128) 768
_________________________________________________________________
dense_13 (Dense) (None, 32) 4128
_________________________________________________________________
dense_14 (Dense) (None, 5) 165
=================================================================
Total params: 546,941
Trainable params: 532,861
Non-trainable params: 14,080
_________________________________________________________________
这里的一切看起来都不错,但仔细观察:
for l in model.layers:
print("layer : ", l.name, ", expects input of shape : ",l.input_shape)
输出:
layer : sequential_9 , expects input of shape : (None, 224, 224, 3)
layer : sequential_11 , expects input of shape : (None, 1280)
layer : dense_12 , expects input of shape : (None, 5) <-- **PROBLEM**
layer : dense_13 , expects input of shape : (None, 128)
layer : dense_14 , expects input of shape : (None, 32)
问题 这里是 dense_12 期望输入形状 (None, 5) 但它应该期望输入形状 (None, 512) 因为我们已经将 Dense(512) 添加到 sequential_11,可能的原因是像上面指定的那样添加层可能不会更新一些属性,例如 sequential_11 的输出形状,所以在前向传递期间有作为 miss-match 在 sequential_11 的输出和层 dense_12 的输入之间(在你的情况下 dense_25)
可能的解决方法是:
对于您的问题“在 sequential_9 和 sequential_11 之间添加图层”,您可以在 sequential_9 和 sequential_11 之间添加任意数量的图层,但是始终确保最后添加的层的输出形状应与 sequential_11 预期的输入形状相匹配。在本例中为 1280。
代码:
sequential_1 = model.layers[0] # re-using pre-trained model
sequential_2 = model.layers[1]
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
inp_sequential_1 = Input(sequential_1.layers[0].input_shape[1:])
out_sequential_1 = sequential_1(inp_sequential_1)
#adding layers in between sequential_9 and sequential_11
out_intermediate = Dense(512, activation="relu")(out_sequential_1)
out_intermediate = Dense(128, activation ="relu")(out_intermediate)
out_intermediate = Dense(32, activation ="relu")(out_intermediate)
# always make sure to include a layer with output shape matching input shape of sequential 11, in this case 1280
out_intermediate = Dense(1280, activation ="relu")(out_intermediate)
output = sequential_2(out_intermediate) # output of intermediate layers are given to sequential_11
final_model = Model(inputs=inp_sequential_1, outputs=output)
模型摘要的输出:
Model: "functional_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
sequential_9 (Sequential) (None, 1280) 410208
_________________________________________________________________
dense_15 (Dense) (None, 512) 655872
_________________________________________________________________
dense_16 (Dense) (None, 128) 65664
_________________________________________________________________
dense_17 (Dense) (None, 32) 4128
_________________________________________________________________
dense_18 (Dense) (None, 1280) 42240
_________________________________________________________________
sequential_11 (Sequential) (None, 5) 128600
=================================================================
Total params: 1,306,712
Trainable params: 1,292,632
Non-trainable params: 14,080
我正在尝试通过在输出层之前添加更多层来使用可教学机器应用程序 https://teachablemachine.withgoogle.com/ 中的 google 模型。 当我重新训练模型时,总是 return 这个错误:
ValueError:层 dense_25 的输入 0 与层不兼容:输入形状的预期轴 -1 的值为 5 但收到的输入形状为 [20, 512]
这是我的方法:
重新训练模型时 return 错误:
如果我在不添加新层的情况下重新训练模型,它工作正常。 谁能告诉我问题出在哪里?
更新的答案
如果要在 pre-trained 模型的两层之间添加层,它不像使用 add 方法添加层那样简单。如果这样做将导致 un-expected 行为
错误分析:
如果你像下面那样编译模型(像你指定的那样):
model.layers[-1].add(Dense(512, activation ="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(32))
model.add(Dense(5))
模型摘要输出:
Model: "sequential_12"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
sequential_9 (Sequential) (None, 1280) 410208
_________________________________________________________________
sequential_11 (Sequential) (None, 512) 131672
_________________________________________________________________
dense_12 (Dense) (None, 128) 768
_________________________________________________________________
dense_13 (Dense) (None, 32) 4128
_________________________________________________________________
dense_14 (Dense) (None, 5) 165
=================================================================
Total params: 546,941
Trainable params: 532,861
Non-trainable params: 14,080
_________________________________________________________________
这里的一切看起来都不错,但仔细观察:
for l in model.layers:
print("layer : ", l.name, ", expects input of shape : ",l.input_shape)
输出:
layer : sequential_9 , expects input of shape : (None, 224, 224, 3)
layer : sequential_11 , expects input of shape : (None, 1280)
layer : dense_12 , expects input of shape : (None, 5) <-- **PROBLEM**
layer : dense_13 , expects input of shape : (None, 128)
layer : dense_14 , expects input of shape : (None, 32)
问题 这里是 dense_12 期望输入形状 (None, 5) 但它应该期望输入形状 (None, 512) 因为我们已经将 Dense(512) 添加到 sequential_11,可能的原因是像上面指定的那样添加层可能不会更新一些属性,例如 sequential_11 的输出形状,所以在前向传递期间有作为 miss-match 在 sequential_11 的输出和层 dense_12 的输入之间(在你的情况下 dense_25)
可能的解决方法是:
对于您的问题“在 sequential_9 和 sequential_11 之间添加图层”,您可以在 sequential_9 和 sequential_11 之间添加任意数量的图层,但是始终确保最后添加的层的输出形状应与 sequential_11 预期的输入形状相匹配。在本例中为 1280。
代码:
sequential_1 = model.layers[0] # re-using pre-trained model
sequential_2 = model.layers[1]
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model
inp_sequential_1 = Input(sequential_1.layers[0].input_shape[1:])
out_sequential_1 = sequential_1(inp_sequential_1)
#adding layers in between sequential_9 and sequential_11
out_intermediate = Dense(512, activation="relu")(out_sequential_1)
out_intermediate = Dense(128, activation ="relu")(out_intermediate)
out_intermediate = Dense(32, activation ="relu")(out_intermediate)
# always make sure to include a layer with output shape matching input shape of sequential 11, in this case 1280
out_intermediate = Dense(1280, activation ="relu")(out_intermediate)
output = sequential_2(out_intermediate) # output of intermediate layers are given to sequential_11
final_model = Model(inputs=inp_sequential_1, outputs=output)
模型摘要的输出:
Model: "functional_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
sequential_9 (Sequential) (None, 1280) 410208
_________________________________________________________________
dense_15 (Dense) (None, 512) 655872
_________________________________________________________________
dense_16 (Dense) (None, 128) 65664
_________________________________________________________________
dense_17 (Dense) (None, 32) 4128
_________________________________________________________________
dense_18 (Dense) (None, 1280) 42240
_________________________________________________________________
sequential_11 (Sequential) (None, 5) 128600
=================================================================
Total params: 1,306,712
Trainable params: 1,292,632
Non-trainable params: 14,080