在预训练网络中间添加辍学

时间:2020-10-13 20:23:07

标签: tensorflow keras deep-learning neural-network

我正在尝试在RestNet50网络中间添加一个Dropout层, 这是我要运行的代码:

base_model = ResNet50(weights='imagenet', include_top=False)
layers = [l for l in base_model.layers]

conv3_block4_out = base_model.get_layer('conv3_block4_out')
x = conv3_block4_out.output

add = False
for layer in layers:
    if layer.name == 'conv3_block4_out':
        add = True
        x = Dropout(0.5)(x)
        continue

    if not add:
        continue


    x = layer(x)  # This is where the error is thrown, when the layer name is 'conv4_block1_0_conv'

x = GlobalAveragePooling2D()(x)
preds = Dense(196, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=preds)

我正在做的是获取conv3_block4_out层,在其中添加Dropout,然后继续堆积剩余的食物。

我得到的错误是:

Traceback (most recent call last):
  File "/machine-learning/test-proj/make_model_year_model.py", line 70, in <module>
    x = layer(x)
  File "/machine-learning/test-proj/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 925, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "/machine-learning/test-proj/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1092, in _functional_construction_call
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
  File "/machine-learning/test-proj/venv/lib/python3.8/site-packages/tensorflow/python/keras/engine/input_spec.py", line 212, in assert_input_compatibility
    raise ValueError(
ValueError: Input 0 of layer conv4_block1_0_conv is incompatible with the layer: expected axis -1 of input shape to have value 512 but received input with shape [None, None, None, 256]

大约5次成功粘贴图层后,就会引发此错误。

据我所知,辍学图层不会影响图层的形状,那么为什么会出现该错误?

有人可以解释原因,或者建议进行更改以使其起作用吗?

0 个答案:

没有答案