keras: MobileNet initialization fails for small input_shape

Initializing MobileNet for small dimensions of input_shape fails with unclear error messages Running

from keras.applications.mobilenet import MobileNet
model = MobileNet(weights='imagenet', include_top=False, input_shape=(64, 64, 3))

produces misleading error

ValueError: When setting`include_top=True`, `input_shape` should be (224, 224, 3).

That seems to signify that I have specified include_top=True Also, from reading the code i thought that it should work for shapes larger than 32x32 but it seems that the lowest viable shape is 128x128.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Just as an idea how to override this size exception: use input_tensor instead of input_shape: input_tensor = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) And set as a param for MobileNet as input_tensor = input_tensor

It will work with a notification that default weights a loaded. Although not sure the behavior is expected.

MobileNets have many different input size configurations, and so I had to bypass some of the default behaviour of the input shape checks.

You are using an input shape of 64x64 but trying to load weights, where no weights for such an input size exist. The minimum is 128x128.

I agree that the error message should be more informative, but the docs clearly states the valid sizes for weights = ImageNet are 128, 160, 192 and 224.

I don’t fully understand, why mobile net can only use the imagenet weights for the 4 specified input shape even “include_top” has been set to false? Does convolution have anything to do with the input shape?

Tried to use NASNetLarge today with weights="imagenet", include_top=False and input_shape=(225, 225, 3), and I get the same error with misleading message.

ValueError: When setting `include_top=True` and loading `imagenet` weights, `input_shape` should be (331, 331, 3).

Right now, the NASNet function calls _obtain_input_shapewith require_flatten=True, so no matter the include_topvalue you use, this error will always happens if your input_shapeis not the default one. Either the code needs to be updated to accept custom shape with imagenet weights, or the error message and documentation (which make no mention on shape requirement depending on weights and not only on include_top) need to be updated.

add weights=None