我试图将预处理层添加到预先训练好的网络中。这是我正在处理的代码:
orig_model = applications.vgg16.VGG16(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=1000)
orig_model.load_weights(weights_path)
preproc_layer = Lambda(preprocess, input_shape=(3,224,224), output_shape=(3,224,224))
model = Sequential()
model.add(preproc_layer)
all_layers = orig_model.layers
for l in all_layers:
config = l.get_config()
copy = layers.deserialize({'class_name':l.__class__.__name__, 'config': config})
weights = l.get_weights()
copy.set_weights(weights)
model.add(copy)
预处理的地方是:
preprocess(x):
x = x[::-1, ...]
x = K.bias_add(x, vgg_mean, data_format='channels_first')
适用于第一个InputLayer
,但在第copy.set_weights(weights)
个图层Conv2D
时会出现错误:
You called `set_weights(weights)` on layer "block1_conv1" with a weight list of length 2, but the layer was expecting 0 weights.
我在Google上发现了类似的内容:https://github.com/keras-team/keras/issues/4812。在这里,他们建议为图层设置trainable = True
,但这在我的情况下不起作用。
你有什么建议吗? Keras版本是2.1.5,Tensorflow 1.6.0