Change the input size in Keras

前提是你 提交于 2020-11-30 16:58:06

问题


I have trained a fully convolutional neural network with Keras. I have used the Functional API and have defined the input layer as Input(shape=(128,128,3)), corresponding to the size of the images in my training set.

However, I want to use the trained model on images of variable sizes (which should be ok because the network is fully convolutional). To do this, I need to change my input layer to Input(shape=(None,None,3)). The obvious way to solve the problem would have been to train my model directly with an input shape of (None,None,3) but I use a custom loss function where I need to specify the size of my training images.

I have tried to define a new input layer and assign it to my model like this :

from keras.engine import InputLayer

input_layer = InputLayer(input_shape=(None, None, 3), name="input")
model.layers[0] = input_layer

This actually changes the size of the input layers accordingly but the following layers still expect (128,128,filters) inputs.

Is there a way to change all of the inputs values at once ?


回答1:


Create a new model, exactly the same, except for new input shape; and tranfer weights:

newModel.set_weights(oldModel.get_weights())

If anything goes wrong, then it might not be fully convolutional (ex: contains a Flatten layer).



来源:https://stackoverflow.com/questions/50063237/change-the-input-size-in-keras

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