Mask_rcnn: AttributeError: module 'keras.engine.saving' has no attribute 'load_weights_from_hdf5_group_by_name'

Created on 21 Jun 2020  路  5Comments  路  Source: matterport/Mask_RCNN

Which weights to start with?

init_with = "coco" # imagenet, coco, or last

if init_with == "imagenet":
model.load_weights(model.get_imagenet_weights(), by_name=True)
elif init_with == "coco":
# Load weights trained on MS COCO, but skip layers that
# are different due to the different number of classes
# See README for instructions to download the COCO weights
model.load_weights(COCO_MODEL_PATH, by_name=True,
exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",
"mrcnn_bbox", "mrcnn_mask"])
elif init_with == "last":
# Load the last model you trained and continue training

model.load_weights(model.find_last(), by_name=True)

AttributeError Traceback (most recent call last)

in
10 model.load_weights(COCO_MODEL_PATH, by_name=True,
11 exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",
---> 12 "mrcnn_bbox", "mrcnn_mask"])
13 elif init_with == "last":
14 # Load the last model you trained and continue training

~/Desktop/Mask_RCNN-master/mrcnn/model.py in load_weights(self, filepath, by_name, exclude)
2141
2142 if by_name:
-> 2143 saving.load_weights_from_hdf5_group_by_name(f, layers)
2144 else:
2145 saving.load_weights_from_hdf5_group(f, layers)

AttributeError: module 'keras.engine.saving' has no attribute 'load_weights_from_hdf5_group_by_name'

Most helpful comment

@emptybottle @baronnobody
Hello. I had same problem and solved.

Load weight file by given to arguments filepath and by_name with the Keras API.

repository's code

2129: if by_name:
2130: saving.load_weights_from_hdf5_group_by_name(f, layers)
2131: else:
2132: saving.load_weights_from_hdf5_group(f, layers)

modified to

keras_model.load_weights(filepath, by_name=by_name)

Keras API document is here.

What are my options for saving models?
https://keras.io/getting_started/faq/#keras-model

All 5 comments

I had this same problem. It seems according to the poorly-documented TF source code, keras.engine.saving was mostly moved to keras.saving.

you want to replace
from keras import saving
with
from tensorflow.python.keras.saving import hdf5_format

then you will also want to replace the lines
saving.load_weights_from_hdf5_group(f, layers)
and so on with
hdf5_format.load_weights_from_hdf5_group(f, layers)

Hopefully this helps you and anyone else trying to use this repo in 2020.

@emptybottle @baronnobody
Hello. I had same problem and solved.

Load weight file by given to arguments filepath and by_name with the Keras API.

repository's code

2129: if by_name:
2130: saving.load_weights_from_hdf5_group_by_name(f, layers)
2131: else:
2132: saving.load_weights_from_hdf5_group(f, layers)

modified to

keras_model.load_weights(filepath, by_name=by_name)

Keras API document is here.

What are my options for saving models?
https://keras.io/getting_started/faq/#keras-model

@emptybottle @baronnobody
Hello. I had same problem and solved.
Load weight file by given to arguments filepath and by_name with the Keras API.
repository's code
2129: if by_name:
2130: saving.load_weights_from_hdf5_group_by_name(f, layers)
2131: else:
2132: saving.load_weights_from_hdf5_group(f, layers)
modified to
keras_model.load_weights(filepath, by_name=by_name)
Keras API document is here.
What are my options for saving models?
https://keras.io/getting_started/faq/#keras-model

Hello. I am changing my script according to your advise, while another error occurs to me.
It seems that there is a conflict between the shape of saved weight and the shape of M-RCNN built by the model.py script.
Partial error information is showing below.
ValueError: Layer #389 (named "mrcnn_bbox_fc"), weight <tf.Variable 'mrcnn_bbox_fc/kernel:0' shape=(1024, 12) dtype=float32, numpy= *** has shape (1024, 12), but the saved weight has shape (1024, 324).

I wonder if there is any way to solve this problem.

@emptybottle @baronnobody
Hello. I had same problem and solved.
Load weight file by given to arguments filepath and by_name with the Keras API.
repository's code
2129: if by_name:
2130: saving.load_weights_from_hdf5_group_by_name(f, layers)
2131: else:
2132: saving.load_weights_from_hdf5_group(f, layers)
modified to
keras_model.load_weights(filepath, by_name=by_name)
Keras API document is here.
What are my options for saving models?
https://keras.io/getting_started/faq/#keras-model

Hello. I am changing my script according to your advise, while another error occurs to me.
It seems that there is a conflict between the shape of saved weight and the shape of M-RCNN built by the model.py script.
Partial error information is showing below.
ValueError: Layer #389 (named "mrcnn_bbox_fc"), weight <tf.Variable 'mrcnn_bbox_fc/kernel:0' shape=(1024, 12) dtype=float32, numpy= *** has shape (1024, 12), but the saved weight has shape (1024, 324).

I wonder if there is any way to solve this problem.

Hi! Did you find a fix for this error? I'm getting the same one.
Using TF=2.2.0, keras=2.3.1.

I had this same problem. It seems according to the poorly-documented TF source code, keras.engine.saving was mostly moved to keras.saving.

you want to replace
from keras import saving
with
from tensorflow.python.keras.saving import hdf5_format

then you will also want to replace the lines
saving.load_weights_from_hdf5_group(f, layers)
and so on with
hdf5_format.load_weights_from_hdf5_group(f, layers)

Hopefully this helps you and anyone else trying to use this repo in 2020.

Totally worked for me! Thanks

Was this page helpful?
0 / 5 - 0 ratings