本文介绍了使用具有不同输入形状和类模型的预训练模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CNN 处理分类问题,其中输入图像大小为 64X64 ,并且我想使用诸如 VGG16 之类的预训练模型, COCO 或其他任何内容.但是问题是预训练模型的输入图像大小是 224X224 .我该如何解决这个问题.输入图像大小是否有任何数据扩充方法.

I am working on a classification problem using CNN where my input image size is 64X64 and I want to use pretrained model such as VGG16,COCO or any other. But the problem is input image size of pretrained model is 224X224. How do I sort this issue. Is there any data augmentation way for input image size.

如果我将输入图像的尺寸调整为 224X224 ,则图像出现模糊的可能性非常高,这可能会影响训练.如果我错了,请纠正我.

If I resize my input image to 224X224 then there is very high chance of image will get blurred and that may impact the training. Please correct me if I am wrong.

另一个问题与预训练模型有关.如果我使用的是转移学习,那么通常情况下我必须如何冻结预训练模型中的图层.考虑到我的分类与预训练的模型类非常不同.但是我想我们可以冻结它的前几层,以获取图像的边缘,曲线等,这在所有图像中都很常见.

Another question is related to pretrained model. If I am using transfer learning then generally how layers I have to freeze from pretrained model. Considering my classification is very different from pretrained model classes. But I guess first few layers we can freeze it to get the edges, curve etc.. of the images which is very common in all the images.

推荐答案

我假设您使用 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:

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

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'])

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

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.

这篇关于使用具有不同输入形状和类模型的预训练模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:42
查看更多