Home > Software design >  How to extract the size of the input image from an H5 file
How to extract the size of the input image from an H5 file

Time:07-28

I created and ran a CNN model where the size of the input images are (64x64x3).

I would like to know if it is possible to extract this information from the H5 files (weight or model) I got using model.save_weights("****.h5") and model.save("****.h5").

CodePudding user response:

You can use h5py library to read .h5 files For installing - pip install h5py

import h5py
f = h5py.File('model.h5', 'r')
one_data = f['key']
print(one_data.shape)

CodePudding user response:

If you want to load the model and then extract the input shape. Try:

import tensorflow as tf

model = tf.keras.Sequential([tf.keras.layers.Conv2D(32, 3, input_shape=(64, 64, 3))])
model.save('model.h5')

model = tf.keras.models.load_model('model.h5')

# assuming your first layer providers the input shape
model.get_config()['layers'][0]['config']['batch_input_shape']
(None, 64, 64, 3)

Using the h5py library will unfortunately only provide the model weights:

import h5py
f = h5py.File('model.h5', 'r')
f['model_weights']

But you can access the model config over the attrs properties:

import h5py
import json
with h5py.File('model.h5', 'r') as h5f:
    data = json.loads(h5f.attrs['model_config'])
    print(data['config']['layers'][0]['config']['batch_input_shape'])
[None, 64, 64, 3]
  • Related