TensorFlow version : TF 2.0.0 stable + Keras 2.4.2(A )and 1.13.1 +Keras 2.3.1 (B)
Are you willing to contribute it (Yes/No): Yes
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.
Tensorflow 1.13.1+ Keras 2.3.1 works fine
# 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()
# 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())
System information
TF 2.0.0 stable + Keras 2.4.2
(A )and1.13.1 +Keras 2.3.1
(B)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.
ouput is 130x130x3
output stays 224x224x3
System info