Keras change model input size in test

1,873 views
Skip to first unread message

qi yang

unread,
Apr 1, 2017, 12:12:45 AM4/1/17
to Keras-users

Does anyone have any idea of an easy way to modify input image size of a saved model in Keras? 

For example the training input image size is 32x32, but in test I would like to input the full image 180x180. 

The model has been saved and at test loaded as the following: 

json_file = open('autoencoder64a.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("autoencoder64a.h5")

Many thanks, 

Tina

Loser

unread,
Apr 1, 2017, 12:36:27 AM4/1/17
to Keras-users
Add a lamda layer and resize it that way?

jin...@eng.ucsd.edu

unread,
Apr 9, 2017, 6:03:05 PM4/9/17
to Keras-users
Same question here... Anyone has any advice?
I want to load a pre-trained model for input m*m and then use all its weights on a new model with larger input n*n.
I tried to load the weights layer by layer then set these weights to the new model. 
But it seems that only the method 1 can lead to correct result and 2 will lead to a random loss which seems like it has not loaded the correct weights.
1- use model = load_model(path)
2- for layer in layers: weight[i] = model.get_layer('layer_name') and then set_weights to the new model

aklibi...@gmail.com

unread,
Jul 10, 2017, 1:57:38 PM7/10/17
to Keras-users
Hi,

One way to do this would be to extract the JSON configuration serialization of the model, modify the input shape in its layer definition, and then re-instantiate it from this JSON.

The JSON configuration can be stored using the .to_json() method or it is accessible as an attribute on the hdf5 file that gets created when you call the .save() function.

So the idea would be to first serialize your model with input shape nxn. Then modify the batch_input_shape key/values in the configuration and save it. Then re-instantiate your model using model_from_json or load_model.

I can take a minute to make an example of this if you think it would be useful.

Matias Valdenegro

unread,
Jul 11, 2017, 3:52:48 AM7/11/17
to Keras-users
No, this won't work, you cannot change the input size as the weights have shapes that specifically encode one input shape, the one used for training. The only network that can be transformed to accept variable sized input is a Fully Convolutional Network.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/efe0aed7-13fb-4459-8ef8-3265e9aac825%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

aklibi...@gmail.com

unread,
Jul 18, 2017, 2:52:18 PM7/18/17
to Keras-users
You're right. I assumed we were discussing fully-convolutional nets since I couldn't think of another reason why one would try changing the input size. I was working specifically with the UNet model -- should have specified. The method does work nicely with UNet.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages