0

I trained a CNN model with Input_shape(256,256,4) in Keras. But I want to change the input size to be large as (512,512,4) in order to reduce the test time when testing a large image.

I manually change the input shape in .json file, and use

model = model_from_json(json file)

, and then use

model=load_weights(weights file)

I got the error:

ValueError: Error when checking : expected input_9 to have shape (None, 512, 512, 4) but got array with shape (1, 256, 256, 4)

I used two different types of weights file, one saved by Modelcheckpoint(), one saved by model.save_weights(). But the error also raised.

Somebody could help me ? Thanks in advance.

spider
  • 909
  • 1
  • 11
  • 19

1 Answers1

4

By the error message you've got, nothing went wrong with changing the dimensions.

But you defined a static shape (512,512,4). This is the size your model is expecting, although it's a fully convolutional model and supports different sizes.

Based on this link, you should be able to define variable dimensions in such models by using None. So, instead of (512,512,4), you should use input_shape=(None,None,4).

This way, the model will not expect static sizes. But only models that are fully convolutional can accept the exact same weights for two different sizes. (Dense layers cannot do that, for instance)

For training/testing, though, you should separate your batches by size, because numpy arrays don't support variable shapes. You can use this answer to achieve that.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • The model is a FCN model, which has only Conv layers and BatchNorm layers, no fully-connected layers. So, the weights should not be related to the input size, like caffe does. – spider Sep 11 '17 at 05:38
  • Ok, I've learned more about it and updated my answer. I thought you couldn't define a shape using `None`. I knew convolutional models would be able to be redefined for different shapes, but I didn't knew a single model could handle many shapes. – Daniel Möller Sep 11 '17 at 13:19