keras-team / keras-applications

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

MobileNet transfer learning feature reshape not work #152

Open shartoo opened 4 years ago

shartoo commented 4 years ago

System information

Describe the feature and the current behavior/state.

I want to do a transfer learning on MobileNet and change the input size ,i copied the code colab according to Changing input size of pre-trained models in Keras .Everythin goes fine on my machine B(TF 1.13.1 + Keras 2.3.1) ,i could got exact the same output of author namely the input size got changed,while on my another machine A(TF 2..0.0+ Keras 2.4.2) i have to do some change and the result of input shape stays 224x224x3 but not as excepted 130x130x3.

# work on tensorflow 1.13.1 
import keras
import numpy as np
keras.backend.clear_session()

def change_model(model, new_input_shape=(None, 40, 40, 3)):
    # replace input shape of first layer
    model._layers[0].batch_input_shape = new_input_shape

    # rebuild model architecture by exporting and importing via json
    new_model = keras.models.model_from_json(model.to_json())

    # copy weights from old model to new one
    for layer in new_model.layers:
        try:
            layer.set_weights(model.get_layer(name=layer.name).get_weights())
            print("Loaded layer {}".format(layer.name))
        except:
            print("Could not transfer weights for layer {}".format(layer.name))
    return new_model

from keras.applications.mobilenet import MobileNet
from keras.preprocessing import image
from keras.applications.mobilenet import preprocess_input,decode_predictions
import numpy as np
model = MobileNet(weights='imagenet',include_top=True,input_shape=(224, 224,3))
new_model = change_model(model,new_input_shape=(None, 130, 130, 3))
new_model.summary()

ouput is 130x130x3

Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 130, 130, 3)       0
_________________________________________________________________
conv1_pad (ZeroPadding2D)    (None, 131, 131, 3)       0
_________________________________________________________________
conv1 (Conv2D)               (None, 65, 65, 32)        864
_________________________________________________________________
conv1_bn (BatchNormalization (None, 65, 65, 32)        128
...
# work on Tensorflow 2.0.0 
import keras
import tensorflow as tf
from keras_applications.mobilenet import MobileNet

def change_model(model, new_input_shape=(None,40, 40, 3)):
    # replace input shape of first layer
    model._layers[0].batch_input_shape = new_input_shape
    # rebuild model architecture by exporting and importing via json
    new_model = tf.keras.models.model_from_json(model.to_json())
    # copy weights from old model to new one
    for layer in new_model.layers:
        try:
            layer.set_weights(model.get_layer(name=layer.name).get_weights())
            print("Loaded layer {}".format(layer.name))
        except:
            print("Could not transfer weights for layer {}".format(layer.name))
    return new_model

model = MobileNet(include_top=True,weights="imagenet",input_shape=(224,224,3),backend = tf.keras.backend, layers = tf.keras.layers, models = tf.keras.models, utils = tf.keras.utils)
new_model = change_model(model,new_input_shape=(None,130,130,3))
print(new_model.summary())

output stays 224x224x3

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 224, 224, 3)]     0         
_________________________________________________________________
conv1_pad (ZeroPadding2D)    (None, 225, 225, 3)       0         
_________________________________________________________________
conv1 (Conv2D)               (None, 112, 112, 32)      864       
_________________________________________________________________
conv1_bn (BatchNormalization (None, 112, 112, 32)      128       
_________________________________________________________________
conv1_relu (ReLU)            (None, 112, 112, 32)      0         
_________________________________________________________________
....

System info