[英]How to find the number of layers from the saved h5 file of pre-trained model?
I have VGG-16 weights saved in h5 format.我有以 h5 格式保存的 VGG-16 权重。 I want to see the number of layers in the network.
我想查看网络中的层数。 How can I do that?
我怎样才能做到这一点?
I tried using:我尝试使用:
file = h5py.File(vgg16.h5)
after that, I checked for file.attrs
but after this point I don't know which command to use to find the number of layers in the network.在那之后,我检查了
file.attrs
但在这一点之后我不知道使用哪个命令来查找网络中的层数。
If you type this code in spyder provided, you have installed Keras library.如果您在提供的 spyder 中键入此代码,则您已经安装了 Keras 库。 You will get all the layers in h5.file
您将获得 h5.file 中的所有图层
from keras.models import load_model
classifier=load_model('my_model.h5')
classifier.summary()
In case you have only weights and not the model structure, you can use keys() method to get all the layers name in that weight file.如果您只有权重而不是模型结构,您可以使用 keys() 方法获取该权重文件中的所有层名称。
For example: I have one weight file: saved-weight.h5
例如:我有一个体重文件:
saved-weight.h5
If I want to know what are the layers present in this weight file, you can do the following:如果我想知道这个权重文件中存在哪些层,您可以执行以下操作:
import h5py
file = h5py.File('saved-weight.h5')
layer_names = file.keys()
# output
layer_names = <KeysViewHDF5 ['add', 'bn_3', 'bn_5', 'bn_7',
'concatenate', 'conv_1', 'conv_2', 'conv_3',
'dropout', 'fc_8', 'fc_9',
'gru_10', 'gru_10_back', 'gru_11', 'gru_11_back',
'input_1', 'input_3', 'input_4', 'labels',
'lambda', 'lambda_1', 'lambda_2', 'lambda_3',
'maxpool_3', 'maxpool_5', 'model', 'permute', 'reshape']>
These are the layers present in the saved-weights file这些是存在于保存权重文件中的层
Without your data or code, it is hard to provide more details.没有您的数据或代码,就很难提供更多详细信息。 To demonstrate h5py methods to access h5 data, here is a simple example that creates a h5 file with 1 group with 3 datasets.
为了演示访问 h5 数据的 h5py 方法,这里有一个简单的示例,该示例创建一个包含 1 个组和 3 个数据集的 h5 文件。 After the group and datasets are created, there is a loop to print the dataset name, shape and dtype.
创建组和数据集后,有一个循环打印数据集名称、形状和数据类型。
import h5py, numpy as np
h5f=h5py.File('SO_54511719.h5','w')
ds_data = np.random.random(100).reshape(10,10)
group1 = h5f.create_group('group1')
group1.create_dataset('ds_1', data=ds_data)
group1.create_dataset('ds_2', data=ds_data)
group1.create_dataset('ds_3', data=ds_data)
print ('number of datasets in group:', len(group1))
for (dsname, dsvalue) in group1.items() :
print ('for',dsname,':')
print ('shape:',dsvalue.shape)
print ('dtype:',dsvalue.dtype)
h5f.close()
Output looks like this:输出如下所示:
number of datasets in group: 3
for ds_1 :
shape: (10, 10)
dtype: float64
for ds_2 :
shape: (10, 10)
dtype: float64
for ds_3 :
shape: (10, 10)
dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.