keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 910 forks source link

Unnecessary ValueError in MobileNetV2 #92

Closed see-- closed 5 years ago

see-- commented 5 years ago

MobileNetV2 has 1 / 32 output stride if include_top=False. It should work with input shapes that are a multiple of 32.

from keras.applications.mobilenet_v2 import MobileNetV2
from keras.applications.resnet50 import ResNet50

input_shape = (224, 448, 3)

# this works
resnet = ResNet50(input_shape=input_shape, weights='imagenet', include_top=False)

# raises ValueError
try:
  mobilenet = MobileNetV2(input_shape=input_shape, weights='imagenet', include_top=False)
except ValueError as e:
  print(e)
taehoonlee commented 5 years ago

Thank you for the report, @see--. I will revise and let you know.

ezavarygin commented 5 years ago

This commit does not seem to fully solve the problem. The model now tries to load the weights for the specified size (here) which in some cases do not exist. This code illustrates the issue:

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

leading to this error message:

Exception: URL fetch failure on https://github.com/fchollet/deep-learning-models/releases/download/v0.6/mobilenet_1_0_200_tf_no_top.h5: 404 -- Not Found

It seems to me that this part of code should be replaced with something along the lines of the following:

if rows != cols or rows not in [128, 160, 192, 224]:
    rows = 224
    if rows is None:
        warnings.warn('MobileNet shape is undefined.'
                      ' Weights for input shape '
                      '(224, 224) will be loaded.')
    else:
        warnings.warn('Specified input shape (%s, %s) is not '
                      'one of (128, 128), (160, 160), (192, 192),'
                      ' or (224, 224). Weights for input shape '
                      '(224, 224) will be loaded.' % (input_shape[row_axis], input_shape[col_axis]))

I am happy to create a pull request (never done it before though) if someone could confirm that my logic above is correct.

see-- commented 5 years ago

I think your logic is correct: If we don't have weights for this specific shape, use the default ones (224, 224). Please move rows = 224 below the if-else block. BTW input_shape=(200, 200, 3) should error anyway, (256, 256, 3) is a better example.

taehoonlee commented 5 years ago

Thank you for the report, @ezavarygin. PRs are always welcome. In my opinion, the two warnings you described can be combined. For example, "If rows is different from cols or not in [128, 160, 192, 224], weights for input shape (224, 224) will be loaded as the default". How about this one? @see--

ezavarygin commented 5 years ago

@see-- What is wrong with the size of 200? Keras manual clearly says this size is valid. I tried to load the model using the code above and no error was triggered.

@taehoonlee Agreed. I created a PR. I made this change to both versions of MobileNets.

see-- commented 5 years ago

@ezavarygin You are right, 200 is valid. I thought it has to be multiple of 32.