【问题标题】:fine tune a model using Keras Functional API使用 Keras 功能 API 微调模型
【发布时间】:2019-02-07 17:33:05
【问题描述】:

我正在使用 VGG16 在我的数据集上对其进行微调。

这是模型:

def finetune(self, aux_input):
        model = applications.VGG16(weights='imagenet', include_top=False)
        # return model

        drop_5 = Input(shape=(7, 7, 512))
        flatten = Flatten()(drop_5)
        # aux_input = Input(shape=(1,))
        concat = Concatenate(axis=1)([flatten, aux_input])

        fc1 = Dense(512, kernel_regularizer=regularizers.l2(self.weight_decay))(concat)
        fc1 = Activation('relu')(fc1)
        fc1 = BatchNormalization()(fc1)

        fc1_drop = Dropout(0.5)(fc1)
        fc2 = Dense(self.num_classes)(fc1_drop)
        top_model_out = Activation('softmax')(fc2)

        top_model = Model(inputs=drop_5, outputs=top_model_out)

        output = top_model(model.output)

        complete_model = Model(inputs=[model.input, aux_input], outputs=output)

        return complete_model

我有两个模型输入。在上面的函数中,我将 Concatenate 用于扁平数组和我的 aux_input。 我不确定这是否适用于 imagenet 权重。

当我运行这个时,我得到一个错误:

ValueError: Graph disconnected: cannot get value for tensor Tensor("aux_input:0", shape=(?, 1), dtype=float32) at layer “辅助输入”。以下之前的层是在没有访问的 问题:['input_2', 'flatten_1']

不知道我哪里出错了。

如果重要的话,这就是 fit 函数:

model.fit(x={'input_1': x_train, 'aux_input': y_aux_train}, y=y_train, batch_size=batch_size,
                    epochs=maxepoches, validation_data=([x_test, y_aux_test], y_test),
                    callbacks=[reduce_lr, tensorboard], verbose=2)

但是,当我调用 model.summary() 时,我在此 fit 函数之前收到错误消息。

【问题讨论】:

  • 你如何调用你的finetune函数?
  • 在课堂上。所以ft = FModel() model = ft.finetune(aux_input)

标签: python keras deep-learning vgg-net


【解决方案1】:

问题是您在top_model 中使用了aux_input,但您没有在top_model 的定义中将其指定为输入。尝试将 top_modeloutput 的定义替换为以下内容:

top_model = Model(inputs=[drop_5, aux_input], outputs=top_model_out)
output = top_model([model.output, aux_input])

【讨论】:

  • 现在,出现此错误:ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 512). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
  • 这是因为 VGG 模型没有固定的输入形状,因此您无法确定它是否与顶级模型所需的指定输入形状 (7, 7, 512) 匹配。您可以通过将 input_shape=(224, 224, 3) 作为参数添加到 applications.VGG16() 函数来解决此问题。
猜你喜欢
  • 2023-03-15
  • 2023-03-30
  • 1970-01-01
  • 2019-12-11
  • 2019-05-26
  • 2021-06-24
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多