且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用具有不同输入形状和类模型的预训练模型

更新时间:2023-12-01 23:24:34

但是问题是预训练模型的输入图像大小为224X224.

But the problem is input image size of pretrained model is 224X224.

我假设您使用 Keras / Tensorflow (其他DL框架相同).根据 Keras应用程序中的文档:

I assume you work with Keras/Tensorflow (It's the same for other DL frameworks). According to the docs in the Keras Application:

input_shape :可选的形状元组,仅在include_top时指定为False(否则输入形状必须为(224,224,3)('channels_last'数据格式)或(3,224,224)(带有'channels_first'数据格式).它应该有3个输入通道,宽度和高度应不小于48.(200,200,3)将是一个

input_shape: optional shape tuple, only to be specified if include_top is False (otherwise the input shape has to be (224, 224, 3) (with 'channels_last' data format) or (3, 224, 224) (with 'channels_first' data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 48. E.g. (200, 200, 3) would be one

因此,有两种方法可以解决您的问题:

So there are two options to solve your issue:

  1. 通过将输入图像的大小调整为 244 * 244 库并使用 VGG 分类器[ include_top = True ].

在VGG模型之上训练自己的分类器.如上述文档在 Keras 中所述,如果您的图像不同于244 * 244,则应训练自己的分类器 [include_top = False] .您可以使用以下方法轻松完成这些事情:

Train your own classifier on top of the VGG models. As mentioned in the above documentation in Keras if your image is different than 244*244, you should train your own classifier [include_top=False]. You can do such things easily with:

 inp = keras.layers.Input(shape=(64, 64, 3), name='image_input')

 vgg_model = VGG19(weights='imagenet', include_top=False)
 vgg_model.trainable = False

 x = keras.layers.Flatten(name='flatten')(vgg_model)
 x = keras.layers.Dense(512, activation='relu', name='fc1')(x)
 x = keras.layers.Dense(512, activation='relu', name='fc2')(x)
 x = keras.layers.Dense(10, activation='softmax', name='predictions')(x)
 new_model = keras.models.Model(inputs=inp, outputs=x)
 new_model.compile(optimizer='adam', loss='categorical_crossentropy', 
                   metrics=['accuracy'])

如果我正在使用转移学习,那么通常我必须如何分层从预先训练的模型中冻结

If I am using transfer learning then generally how layers I have to freeze from pretrained model

这实际上取决于您的新任务,您拥有多少培训示例,您的预训练模型是什么以及许多其他事情.如果我是您,那么我首先会丢弃预训练的模型分类器.然后,如果不起作用,请删除其他一些卷积层,并逐步进行操作,直到获得良好的性能为止.

It is really depend on what your new task, how many training example you have, whats your pretrained model, and lots of other things. If I were you, I first throw away the pretrained model classifier. Then, If not worked, remove some other Convolution layer and do it step by step until I get good performance.