mobilenet_v2_keras icon indicating copy to clipboard operation
mobilenet_v2_keras copied to clipboard

ValueError: Shapes (3, 3, 3, 32) and (48, 3, 3, 3) are incompatible

Open HRKpython opened this issue 5 years ago • 1 comments

My tensorflow version is 1.14.0 I have downloaded the "mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.4_224_no_top.h5" and I am trying to load the model:

local_weights_file = '../mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.4_224_no_top.h5'
input_tensor = Input(shape=(224,224, 3))
pre_trained_model = MobileNetV2(
    input_tensor = input_tensor, include_top=False, weights=None, layers = layers)

I get below error: ValueError: Shapes (3, 3, 3, 32) and (48, 3, 3, 3) are incompatible

What is the issue? Thanks,

HRKpython avatar Oct 31 '19 15:10 HRKpython

This error arises because of the alpa argument in MobileNetV2

keras.applications.mobilenet_v2.MobileNetV2(input_shape=None, alpha=1.0,
 include_top=True, weights='imagenet',
 input_tensor=None, pooling=None, classes=1000)

the default value for alpha is 1.0 but the value of alpha for the weights file you downloaded is 1.4 the name of weights file consists of: mobilenet_v2_weights_tf_dim_ordering_tf_kernels_ alpha _ rows _no_top .h5 so the alpha for your file is 1.4 not 1.0

you can deduce that form model_name variable form keras mobilenet source code

 # Load weights.
    if weights == 'imagenet':
        if include_top:
            model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' +
                          str(alpha) + '_' + str(rows) + '.h5')
            weight_path = BASE_WEIGHT_PATH + model_name
            weights_path = keras_utils.get_file(
                model_name, weight_path, cache_subdir='models')
        else:
            model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' +
                          str(alpha) + '_' + str(rows) + '_no_top' + '.h5')
            weight_path = BASE_WEIGHT_PATH + model_name
            weights_path = keras_utils.get_file(
                model_name, weight_path, cache_subdir='models')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

alpha: controls the width of the network. This is known as the width multiplier in the MobileNetV2 paper.

    If alpha < 1.0, proportionally decreases the number of filters in each layer.
    If alpha > 1.0, proportionally increases the number of filters in each layer.
    If alpha = 1, default number of filters from the paper are used at each layer.

as mentioned in (https://keras.io/applications/#mobilenetv2)

Mostafa3zazi avatar Feb 27 '20 16:02 Mostafa3zazi