Reading H5 Groups and Attributes

조회 수: 41 (최근 30일)
Karl Haebler
Karl Haebler 2019년 10월 22일
댓글: Jacek Dmochowski 2022년 10월 13일
Someone has ostensibly provided me with an h5 file (generated in python) that contains the weights of a neural network. These I need to open in matlab.
However, when I used the h5info and h5disp functions in MATLAB, the datasets part of the h5 file info is empty. However, there seems to be dataset information within the group category of the file, as you can see below.
How should I properly be accessing the weights with this h5 file?
Using data = hdf5read(filename,datasetname) seems to not work for no matter what I provide as the datasetname. I cannot provide the .h5 file since it is an unsupported file type with this forum.
Thank you very much
hinfo =
Filename: '/home/sem19h2/Documents/Karl/Semester_Thesis/Scripts/New_matlab_model/matlab_karl_v2/_weights.h5'
Name: '/'
Groups: [3x1 struct]
Datasets: []
Datatypes: []
Links: []
Attributes: [3x1 struct]
HDF5 _weights.h5
Group '/'
Attributes:
'layer_names': 'lstm_55 ', 'dense_29', 'lstm_56 '
'backend': 'tensorflow'
'keras_version': '2.2.4'
Group '/dense_29'
Attributes:
'weight_names': 'dense_29/kernel:0', 'dense_29/bias:0 '
Group '/dense_29/dense_29'
Dataset 'bias:0'
Size: 70
MaxSize: 70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'kernel:0'
Size: 70x70
MaxSize: 70x70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Group '/lstm_55'
Attributes:
'weight_names': 'lstm_55/kernel:0 ', 'lstm_55/recurrent_kernel:0', 'lstm_55/bias:0 '
Group '/lstm_55/lstm_55'
Dataset 'bias:0'
Size: 280
MaxSize: 280
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'kernel:0'
Size: 280x10
MaxSize: 280x10
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'recurrent_kernel:0'
Size: 280x70
MaxSize: 280x70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Group '/lstm_56'
Attributes:
'weight_names': 'lstm_56/kernel:0 ', 'lstm_56/recurrent_kernel:0', 'lstm_56/bias:0 '
Group '/lstm_56/lstm_56'
Dataset 'bias:0'
Size: 16
MaxSize: 16
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'kernel:0'
Size: 16x70
MaxSize: 16x70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'recurrent_kernel:0'
Size: 16x4
MaxSize: 16x4
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
  댓글 수: 5
Said Murtaza Jallali
Said Murtaza Jallali 2021년 2월 4일
hey i have the same problem, have you solved it yet???
Jacek Dmochowski
Jacek Dmochowski 2022년 10월 13일
You need to provide the name of the group(s) to the second argument of h5read().
For example, for grabbing the weights of the "dense_29" layer, you could do:
dense_29_weights = h5read(h5_filename,'/dense_29/dense_29/kernel:0');
Hope this helps.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sivylla Paraskevopoulou
Sivylla Paraskevopoulou 2021년 2월 12일
You might find useful the following two examples on how to import weights from a separate file.
1. Import Keras Network and Weights

카테고리

Help CenterFile Exchange에서 AI for Wireless에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by