Reputation: 51
I have an .h5 file which contains only the weights of a model that has been trained using Mask-RCNN and the Keras framework to perform object detection. I would like to load this model with Tensorflow + Keras in Python and run object detection on custom images.
I am working with Python 3.8.5, and I have tried to load this model using the keras.models.load_model()
function, but when trying to do so I receive the error:
ValueError: No model found in config file
I understand this means the .h5 file was saved with only the model weights, and that to make use of those weights I must load them into a model with the same architecture.
I have used the h5py python module to view the groups inside the .h5 file in an attempt to understand the architecture, but I see only keys such as the below: (There are many, many more like this but those are a general overview of the keys I see)
"activation_1", "add_32", "bn2a_branch1", "res5c_out" etc.
These groups sometimes have dataset members, which when accessed, provide information similar to the following:
<HDF5 group "/bn4v_branch2a/bn4v_branch2a" (4 members)\>
<HDF5 dataset "beta:0": shape (256,), type "<f4"\>
<HDF5 dataset "gamma:0": shape (256,), type "<f4"\>
<HDF5 dataset "moving_mean:0": shape (256,), type "<f4"\>
<HDF5 dataset "moving_variance:0": shape (256,), type "<f4"\>
I can assume some things, like "activation_1" probably correspond to activation layers, but the rest of the layers don't seem related to things like Dense, LeakyReLU, BatchNormalization layers, etc. that I'm used to seeing/using when building a model.
What do I need to do here? How can I determine the architecture of a Keras + Mask-RCNN model from an .h5 file with only model weights?
Thank you, any help is greatly appreciated!
Upvotes: 4
Views: 7440
Reputation: 21
There is a website called, netron and you can upload your .h5 file and get a good view of your model.
Upvotes: 0
Reputation: 21
You can find it in 'model_config' of the attributes of the root group. Simply check:
f = h5py.File(your_model_name, 'r')
f.attrs.get('model_config')
There you'll find all the layer classes with name, shape, activation function, etc.
Upvotes: 2
Reputation: 813
You might be able to glean some information from the output you are getting. E.g.
<HDF5 group "/bn4v_branch2a/bn4v_branch2a" (4 members)\>
<HDF5 dataset "beta:0": shape (256,), type "<f4"\>
<HDF5 dataset "gamma:0": shape (256,), type "<f4"\>
<HDF5 dataset "moving_mean:0": shape (256,), type "<f4"\>
<HDF5 dataset "moving_variance:0": shape (256,), type "<f4"\>
is a batch normalisation layer. I know this from writing manual conversion code to convert from pytorch to tensorflow.
Maybe you could build a test network of your own, extract the weights and read the structure to see what the structures look like with layers you know, and then compare with your unknown model structure?
Upvotes: 0
Reputation: 29
Try loading the model into another variable then call the model.summary() function
model_2 = load_model('old_model.h5')
model_2.summary()
This should show the layers of the model like so : Summary of model
Upvotes: 1