Closed Abhijit-2592 closed 7 years ago
You might need to set the input_shape parameter when assigning InceptionV3 with the proper input shape. This is my usual setting:
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
base_model = InceptionV3(input_shape=input_shape, weights='imagenet', include_top=False)
Not sure whenever you have set K.set_image_data_format('channels_last')
and
K.set_image_dim_ordering('tf')
could let the model initialization works properly.
Hope it helps
@sakares thanks a ton! its working!!!!!
I will close this issue now
@sakares solution works indeed. But since I fine-tuned some models without specifying input_shape
before and didn't want to retrain them all just for this I figured out a way to fix it afterwards:
Assume newModel()
returns a new model where the input_shape is set as sakares explained. The structure of newModel() should be identical to the pertrained one, except for the input_shape
.
import sys
from keras.models import Model,load_model
modelArg = sys.argv[1] #read filename from command line
oldmodel=load_model(modelArg)
model = newModel()
#Some sanity checking with assertions.
assert len(oldmodel.layers) == len(model.layers)
for idx,layer in enumerate(oldmodel.layers):
assert type(oldmodel.layers[idx]) == type(model.layers[idx])
model.layers[idx].set_weights(layer.get_weights())
#Sometimes keras will complain if the model isn't compiled already so compile at will
model.compile(optimizer=SGD(lr=0.001, momentum=0.9), loss='categorical_crossentropy')
#save it again or use it with keras-vis
It takes a bit of time to transfer all the weights but orders of magnitude less than retraining the whole network.
hey @raghakot, @sakares, I am trying to use visualize cam on my network (a fine-tuned inceptionv3) here is my code
when i run this i am getting a weird value error:
** ValueError Traceback (most recent call last)