在VGG19 Keras模型中使用TimeDistributed时出错

时间:2019-06-27 10:04:46

标签: python keras vgg-net

在Keras中将经过训练的VGG19与TimeDistributed一起使用时,出现以下错误:

TypeError: can only concatenate tuple (not "list") to tuple

这是在Windows,Keras,python3.6中

def build_vgg(self):
    img = Input(shape=(self.n_frames, self.img_rows, self.img_cols, 3))

    # Get the vgg network from Keras applications
    vgg = VGG19(weights="imagenet", include_top=False,  input_shape=(self.img_rows, self.img_cols, 3))

    # Output the first three pooling layers
    outputs = [vgg.layers[i].output for i in self.vgg_layers]

    vgg.outputs = outputs 
    # Create model and compile,         

    vggmodel = Model(inputs=vgg.inputs, outputs=outputs)
    vggmodel.trainable = False
    h2 = layers.wrappers.TimeDistributed(vggmodel)(img)
    model = Model(inputs=img,outputs=h2)
    model.compile(loss='mse', optimizer='adam')

    return model

我希望将加载经过训练的VGG19模型,并且TimeDistributed包装器将使用经过训练的模型并将其在视频中使用。

在代码上执行此行时显示的错误:

h2 = layers.wrappers.TimeDistributed(vggmodel)(img)

1 个答案:

答案 0 :(得分:0)

我用这种方式重写它,并且效果很好

def build_vgg(self):

    video  = Input(shape=(self.n_frames, self.img_rows, self.img_cols, 3))

    # Get the vgg network from Keras applications
    vgg = VGG19(weights="imagenet", include_top=False,  input_shape=(self.img_rows, self.img_cols, 3))

    # Output the first three pooling layers
    vgg.outputs = [vgg.layers[i].output for i in self.vgg_layers]

    # Create model and compile, 


    vggmodel = Model(inputs=vgg.inputs, outputs=vgg.outputs)
    #vggmodel.trainable = False
    h2 = []
    for out in vggmodel.output:
        h2.append(layers.wrappers.TimeDistributed(Model(vggmodel.input,out))(video))

    model = Model(inputs=video, outputs=h2)
    model.trainable = False
    model.compile(loss='mse', optimizer='adam')

    return model