Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the input size in Keras

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 ?

like image 599
Nathan Hubens Avatar asked Apr 27 '18 13:04

Nathan Hubens


People also ask

How do I change the input size in Resnet?

Use PIL or similar libraries to resize the images to 224 x 224, then feed them to the pre-trained model should be OK. You can just use torchvision. transforms. Resize ( size , interpolation=2 ) to get the desired dimensions.

What is input size in neural network?

In Keras, the input dimension needs to be given excluding the batch-size (number of samples). In this neural network, the input shape is given as (32, ). 32 refers to the number of features in each input sample. Instead of not mentioning the batch-size, even a placeholder can be given.

How does Keras define input shape?

The input shape In Keras, the input layer itself is not a layer, but a tensor. It's the starting tensor you send to the first hidden layer. This tensor must have the same shape as your training data. Example: if you have 30 images of 50x50 pixels in RGB (3 channels), the shape of your input data is (30,50,50,3) .

What is the input shape for VGG16?

The default input size for this model is 224x224. Note: each Keras Application expects a specific kind of input preprocessing. For VGG16, call tf. keras.


1 Answers

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).

like image 92
Daniel Möller Avatar answered Oct 16 '22 00:10

Daniel Möller