python - 将 Keras 中 VGG19 的部分层与 TimeDistributed 层一起使用

标签 python tensorflow keras keras-layer

我想将经过训练的 VGG19 模型的前 9 层与 TimeDistributed 层结合使用。但我收到一个 InvalidArgumentError。

def build_vgg(in_shape):
    vgg = VGG19(weights="imagenet")
    vgg.outputs = [vgg.layers[9].output]
    img = keras.Input(in_shape)
    img_features = vgg(img)
    return keras.Model(img, img_features)

vggmodel = build_vgg((50,50,3))
input_layer = keras.Input(batch_shape=(10,10,50,50,3))
h2 = keras.layers.wrappers.TimeDistributed(vggmodel)(input_layer)
model = keras.Model(input_layer,h2)
model.summary()

我收到此错误:

InvalidArgumentError                      Traceback (most recent call last)
~/.conda/envs/py3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs)
   1566   try:
-> 1567     c_op = c_api.TF_FinishOperation(op_desc)
   1568   except errors.InvalidArgumentError as e:

InvalidArgumentError: Dimensions must be equal, but are 512 and 25088 for 'time_distributed_1/vgg19/fc1/MatMul' (op: 'MatMul') with input shapes: [10,512], [25088,4096].

最佳答案

首先,您的模型不应在 build_vgg 中使用额外的输入。您应该只采用您想要的张量。

其次,您应该使用兼容的输入形状。

第三,如果您要更改输入形状并加载 imagenet 权重,则不能包含 top:

def build_vgg(in_shape):
    vgg = VGG19(weights="imagenet", input_shape= in_shape, include_top = False)
    outputs = vgg.layers[9].output

    return keras.Model(vgg.input, outputs)

然后剩下的

vggmodel = build_vgg((50,50,3))
#vggmodel.summary()
input_layer = keras.Input(batch_shape=(10,10,50,50,3))
h2 = keras.layers.wrappers.TimeDistributed(vggmodel)(input_layer)
model = keras.Model(input_layer,h2)
model.summary()

model.predict(np.ones((10,10,50,50,3)))

关于python - 将 Keras 中 VGG19 的部分层与 TimeDistributed 层一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51272415/

相关文章:

python - 基本过滤器返回一个空列表

python - 在 C++ 中使用加载的 tensorflow 模型运行推理

python - 如何使用 Keras 预测函数/表?

python - 如何设置 1dCNN+LSTM 网络(Keras)的输入形状?

python - CNN 中的模型精度和损失没有改善

python - 如何将列表列表写入 CSV 文件 Python?

python - 如何在 'with' 语句中包装静态类(来自 .NET)

python - 并行 Python-C++ 程序卡住(内存?)

python - Keras - 从顺序 API 到函数式 API 的转换

python - Tensorflow - 平均恢复模型的模型权重