tensorflow - 如何在tensorflow或keras的h5模型中查找特定层的步幅/填充信息
问题描述
我一直致力于从 h5 文件中提取卷积层信息,其中包括神经网络模型。我已经能够在 h5 文件中提取有关卷积层数的信息,但我看不到获取有关步幅大小或填充信息的方法。我一直在使用 h5py 来读取 h5 模型。
这是我用来在 h5 中查找卷积层数和权重矩阵的代码
f = h5py.File(weight_file_path)
layers_counter=0
if len(f.attrs.items()):
print("{} contains: ".format(weight_file_path))
print("Root attributes:")
for layer, g in f.items():
print(" {}".format(layer))
print(" Attributes:")
for key, value in g.attrs.items():
print(" {}: {}".format(key, value))
print(" Dataset:")
for p_name in g.keys():
param = g[p_name]
matrix=param.value #It will be weights matrix
matrix_size=a.shape #It is matrix size
if len(matrix_size)>3:
layers_counter=layers_counter+1
执行后,layers_counter
将有多个卷积层。
解决方案
模型配置作为根数据集的属性存储为 HDF5 文件中的 JSON,您可以使用以下代码获取它:
import h5py
import json
model_h5 = h5py.File(filename, 'r')
model_config = model_h5["/"].attrs["model_config"]
config_dict = json.loads(model_config)
然后,您可以索引config_dict
以获取所需的配置参数,例如,config_dict["config"]["layers"][1]["config"]["strides"]
用于第一个卷积层。
推荐阅读
- java - 将初始化代码添加到 Spring Boot 应用程序的正确方法是什么?
- javascript - 通过 js/react 中的对象数组映射
- java - 如何在 Spring 中编写查询以将记录插入 Mysql 数据库中的 2 个表中
- android - 如何正确计算android设备对角线?
- javascript - 如果它们相同,我可以将字符串与属性名称相关联并相应地应用它们吗
- python - 调用模块时使用可变变量的智能方法
- java - 在查询时有条件地忽略实体中的 a 字段
- python - Python:创建具有多选值的列表。错误:找到一个开放的集合属性。在 OData 中,不支持开放集合属性
- c++ - 如何使用递归计算从0到给定数字的奇数?
- jmeter - 从TestNG测试调用时,Jmeter包括控制器外部脚本执行被跳过