【问题标题】:Error while using TimeDistributed with VGG19 Keras model将 TimeDistributed 与 VGG19 Keras 模型一起使用时出错
【发布时间】:2019-11-09 08:04:05
【问题描述】:

在 Keras 中使用带有 TimeDistributed 的预训练 VGG19 时,出现以下错误:

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)

【问题讨论】:

  • 这里的问题是:在 Keras 的 Wrapper 类中,在计算输出形状时,此行返回 (child_output_shape[0], timesteps) + child_output_shape[1:],其中 ipdb> child_output_shape[1 :] [(None, 128, 128, 128), (None, 128, 128, 256)] ipdb> (child_output_shape[0], timesteps) ((None, 256, 256, 64), 4)

标签: python keras vgg-net


【解决方案1】:

我用这种方式重写它,它工作正常

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2019-11-15
    • 2017-07-12
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    相关资源
    最近更新 更多