xanjay
xanjay

Reputation: 571

Failed to load keras resnet50 model offline using weight file

I wanted to train keras pretrained resnet50 model offline but I am unable to load model.

It works when I set weights='imagenet'. It automatically downloads imagenet weight file.

from keras.applications.resnet import ResNet50
base_model = ResNet50(include_top=False, weights='resnet', input_shape=(w,h,3),pooling='avg')

But when I manually downloaded the same weight file and set weights=resnet_weights_path, it throws ValueError.

(w,h) = 224,224
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
base_model = ResNet50(include_top=False, weights=resnet_weights_path, input_shape=(w,h,3),pooling='avg')

ValueError: Shapes (1, 1, 256, 512) and (512, 128, 1, 1) are incompatible.

Full Traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-7683562fa2b9> in <module>
      1 resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
      2 base_model = ResNet50(include_top=False, weights=resnet_weights_path,
----> 3                       pooling='avg')
      4 base_model.summary()

/opt/conda/lib/python3.6/site-packages/keras/applications/__init__.py in wrapper(*args, **kwargs)
     18         kwargs['models'] = models
     19         kwargs['utils'] = utils
---> 20         return base_fun(*args, **kwargs)
     21 
     22     return wrapper

/opt/conda/lib/python3.6/site-packages/keras/applications/resnet.py in ResNet50(*args, **kwargs)
     12 @keras_modules_injection
     13 def ResNet50(*args, **kwargs):
---> 14     return resnet.ResNet50(*args, **kwargs)
     15 
     16 

/opt/conda/lib/python3.6/site-packages/keras_applications/resnet_common.py in ResNet50(include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
    433                   input_tensor, input_shape,
    434                   pooling, classes,
--> 435                   **kwargs)
    436 
    437 

/opt/conda/lib/python3.6/site-packages/keras_applications/resnet_common.py in ResNet(stack_fn, preact, use_bias, model_name, include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
    411         model.load_weights(weights_path)
    412     elif weights is not None:
--> 413         model.load_weights(weights)
    414 
    415     return model

/opt/conda/lib/python3.6/site-packages/keras/engine/saving.py in load_wrapper(*args, **kwargs)
    490                 os.remove(tmp_filepath)
    491             return res
--> 492         return load_function(*args, **kwargs)
    493 
    494     return load_wrapper

/opt/conda/lib/python3.6/site-packages/keras/engine/network.py in load_weights(self, filepath, by_name, skip_mismatch, reshape)
   1228             else:
   1229                 saving.load_weights_from_hdf5_group(
-> 1230                     f, self.layers, reshape=reshape)
   1231             if hasattr(f, 'close'):
   1232                 f.close()

/opt/conda/lib/python3.6/site-packages/keras/engine/saving.py in load_weights_from_hdf5_group(f, layers, reshape)
   1235                              ' elements.')
   1236         weight_value_tuples += zip(symbolic_weights, weight_values)
-> 1237     K.batch_set_value(weight_value_tuples)
   1238 
   1239 

/opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in batch_set_value(tuples)
   2958             `value` should be a Numpy array.
   2959     """
-> 2960     tf_keras_backend.batch_set_value(tuples)
   2961 
   2962 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py in batch_set_value(tuples)
   3321     with ops.init_scope():
   3322       for x, value in tuples:
-> 3323         x.assign(np.asarray(value, dtype=dtype(x)))
   3324   else:
   3325     with get_graph().as_default():

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py in assign(self, value, use_locking, name, read_value)
    817     with _handle_graph(self.handle):
    818       value_tensor = ops.convert_to_tensor(value, dtype=self.dtype)
--> 819       self._shape.assert_is_compatible_with(value_tensor.shape)
    820       assign_op = gen_resource_variable_ops.assign_variable_op(
    821           self.handle, value_tensor, name=name)

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
   1108     """
   1109     if not self.is_compatible_with(other):
-> 1110       raise ValueError("Shapes %s and %s are incompatible" % (self, other))
   1111 
   1112   def most_specific_compatible_shape(self, other):

ValueError: Shapes (1, 1, 256, 512) and (512, 128, 1, 1) are incompatible

Upvotes: 3

Views: 10389

Answers (4)

ElvisM89
ElvisM89

Reputation: 57

For a Simple solution loading Resnet50 for offline use, You can try loading the weights automatically by setting the parameter weights ='imagenet'

from keras.applications.resnet import ResNet50

base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(w,h,3), pooling='avg')

Save the model using

base_model.save("model_name.h5")

Can then load it offline as model (architecture + weights) using

from tensorflow.keras.models import load_model
resnet = load_model('model_name.h5')

Upvotes: 1

Mike Chen
Mike Chen

Reputation: 397

Please add the argument "by_name=True" into "model.load_weights()". It is the correct solution to the issue in either the online or the offline mode. I adopts the offline mode because I have the weights in my desktop.

# Build model.
model = Model(inputs, x, name='resnet50')

# load weights
if weights == 'imagenet':
    if include_top:
        weights_path = WEIGHTS_PATH
    else:
        weights_path = WEIGHTS_PATH_NO_TOP
    # -model.load_weights(weights_path)
    model.load_weights(weights_path, by_name=True)

Upvotes: 0

xanjay
xanjay

Reputation: 571

The issue was probably due to keras version. The current keras version I'm using is 2.3.1.
Do the following to resolve issue:
1. Ran the code with option weights='imagenet'. It downloads the weight file automatically.
2. Provide the path to the downloaded weight file.

Upvotes: 7

Sohaib Anwaar
Sohaib Anwaar

Reputation: 1547

their is a shape mis-match which cannot be solved except changing the architecture according to weights because vectors shape mis-match cause problems.

Download weights from here and try again. these are the weights given by the keras it-self.

WEIGHTS_PATH = ('https://github.com/fchollet/deep-learning-models/'
                'releases/download/v0.2/'
                'resnet50_weights_tf_dim_ordering_tf_kernels.h5')
WEIGHTS_PATH_NO_TOP = ('https://github.com/fchollet/deep-learning-models/'
                       'releases/download/v0.2/'
                       'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')

Upvotes: 2

Related Questions