Change input tensor shape for VGG16 application

大城市里の小女人 提交于 2019-12-08 06:08:50

问题


I want to feed images with the shape (160,320,3) to

 VGG16(input_tensor=input_tensor, include_top=False)   

How can I include a layer that reshapes the images to the shape expected by the VGG16 model, which is (224,224,3) ?


回答1:


VGG16 model in itself is just a set of weights of the fixed sequence of layers and fixed convolution kernel sizes etc. That doesn't mean that those convolution kernels cannot be applied to images of other sizes.

For example in your case:

from keras.models import Model
from keras.layers import Dense,Flatten
from keras.applications import vgg16
from keras import backend as K

model = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(160,320,3))
model.summary(line_length=150)

flatten = Flatten()
new_layer2 = Dense(10, activation='softmax', name='my_dense_2')

inp2 = model.input
out2 = new_layer2(flatten(model.output))

model2 = Model(inp2, out2)
model2.summary(line_length=150)

According to here the minimum image size can be 48x48x3 anything above than that is fine.

Now its true the original weights were learnt on 224,224,3 shaped images but the filters weights act as very good starting point for new tasks with new set of images. You do need to re-train the network but the network would converge very quickly. This is the basis of transfer learning.




回答2:


There are two things that you need to do:

  1. Explicitly declare the input shape to have variable sized inputs by defining None for the image width and height.
  2. Donot use flatten() as it relies on the fixed input shape. Instead use GlobalMaxPooling that will not only do the adaptive pooling, but also flatten the input tensor for the FC to work on.

I hope this will help you achieve what you what.




回答3:


You can use resize() function of Opencv library.

 import cv2
    width = int(224)
    height = int(224)
    dim = (width, height)
    '''images contains original dimension image array'''
    resized_images=[]
    for i in range(0,images.shape[0]):
           resized = cv2.resize(images[i], dim, interpolation = cv2.INTER_AREA)
           resized_images.append(resized)


来源:https://stackoverflow.com/questions/41903051/change-input-tensor-shape-for-vgg16-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!