我想将训练有素的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].
答案 0 :(得分:0)
首先,您的模型不应在PLATFORM="ANDROID" #"platform of the app. ANDROID or IOS"
中使用其他输入。您只应采用所需的张量。
第二,您应该使用兼容的输入形状。
第三,如果要更改输入形状并加载imagenet权重,则不能包含top:
build_vgg
然后休息
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)