In Keras, how do imagenet-pretrained models handled input size different from original 224x224 input size?

Viewed 3965

Since pre-trained models are trained on ImageNet images with a particular width and height, how does Keras model (e.g. ResNet50) allow for a different input_size?

I also would love to know where the magic number '197' comes from:

input_shape: optional shape tuple, only to be specified if include_top is False (otherwise the input shape has to be (224, 224, 3) (with 'channels_last' data format) or (3, 224, 224) (with 'channels_first' data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 197. E.g. (200, 200, 3) would be one valid value.

Source

3 Answers

to start you have to focus on the include_top part when you specify it as false, you chose to not include the fully connected layers and have more freedom on how to work with the model given you the possibility to retrain (or just readjust the weights) of the model.

and when you look at a deep learning model at this size like the resnet50, you will notice that nearer the layer is to the input of the model more abstract it is, at each block of the layer it will capture feature like contours, and the increase of the size of the image wouldn't affect much the result from this part of the network.

the most important layer will sit at the end (or what keras refers as top here) of the model where you should concentrate the training, that is why is possible to change the input size when using include_top = false, so you can readjust the FC layers to the proper size.

about the magic number, I'm not really sure but I would guess that is because the model is a big one and each time it does a pooling it reduces the image size and the 197 would be the limit to not arrive with a size 0 at the end.

if you look at the page you sent at the Fine-tune InceptionV3 on a new set of classes you will see a good example of it where it retrains just the last 2 blocks from the inception model but leave the top ones untouched.

Keras lets you change the input size when "include_top" is False, since that means that you plan to retrain your network on a different task (with different inputs; transfer learning).

If not, you have to pass the original accepted input of the corresponding network. The 197 is the lower limit that is accepted for the specific network to still be compatible to the following layers. This is different for each model.

Related