繁体   English   中英

形状与 Keras 功能 Model 和 VGG16 model 不兼容

[英]Shapes are incompatible with Keras Functional Model and VGG16 model

I'm a little lost right now trying to merge my own model layers using Keras Functional API and layers from a VGG16 model into one new model. 我需要在 block3_pool 之后使用我的自定义图层添加新图层。 将使用 8 个类(Fashion Mnist)。 所有图像都从 28x28 放大到 32x32。 这就是我已经走了多远:

# VGG16 Model
vgg_model = VGG16(include_top=False, weights='imagenet', input_shape=(32, 32, 3), classes=8)
vgg_model.save_weights('vgg.model')
# vgg_model.summary()

for layer in model.layers:
   layer.trainable = False

inputs = Input(shape=(4, 4,))
x = tf.keras.layers.Dense(64, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(8, activation=tf.nn.softmax)(x)

new_model = tf.keras.Model(inputs=inputs, outputs=outputs, name='new_model')
new_model.load_weights('vgg.model')

block3_pool = vgg_model.get_layer('block3_pool').output

# Now combine the two models
full_output = new_model(block3_pool)
full_model = Model(inputs=vgg_model.input, outputs=full_output)
full_model.summary()

尝试运行此代码时,我在 Google Colab 中收到以下错误:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
   1132     """
   1133     if not self.is_compatible_with(other):
-> 1134       raise ValueError("Shapes %s and %s are incompatible" % (self, other))
   1135 
   1136   def most_specific_compatible_shape(self, other):

ValueError: Shapes (4, 64) and (3, 3, 3, 64) are incompatible

我似乎不完全理解为什么会这样以及我必须做些什么才能让它发挥作用。

你给了2个不同的形状。 input_shape中的VGG16参数和Input中的shape参数必须具有相同的形状

inputs = Input(shape=(32, 32, 3))

在您的代码中替换它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM