【发布时间】:2023-03-31 11:56:02
【问题描述】:
我目前正在尝试在 keras 中使用一些预训练的 ImageNet 网络来从图像中提取特征。为此,我将移除网络的顶层,根据每个网络要求对输入进行预处理,然后将输出保存在 hdf5 文件中。我使用了其他几个使用完全相同的方法和代码(仅切换网络)的预训练网络,它似乎工作得很好。但是,我正在努力工作的网络是“InceptionResNetV2”。我不相信我在网络上遇到任何问题,只是通过保存 - 我附上了一个稍微简化的代码版本。更改特征提取器中的模型和预处理中的模型意味着它可以完美运行 - 对于 vgg16、vgg19、resnet、inception 等 - 都很好。
db = h5py.File(hdf5_path, mode="w")
featuresDB = db.create_dataset("features", shape=features_shape, dtype="float")
images = [cv2.imread(path, 1) for path in image_paths[start:end]]
images = inception_resnet_v2.preprocess_input(images)
features = feature_extractor.extract(images)
featuresDB[start:end] = features
但是,这会产生以下错误。我试图将进入 featuresDB 的数据的 dtype 更改为 int,但这没有效果。任何建议表示赞赏!
File "h5py/utils.pyx", line 101, in h5py.utils.convert_tuple
TypeError: an integer is required
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Extract_Features.py", line 111, in <module>
extract_features(image_paths, hdf5_path=args["features_db"],
feature_extractor=feature_extractor)
File "Extract_Features.py", line 83, in extract_features
featuresDB = db.create_dataset("features", shape=features_shape, dtype="float")
File "/home/will/tensorflow/lib/python3.5/site-packages/h5py/_hl/group.py", line
106, in create_dataset
dsid = dataset.make_new_dset(self, shape, dtype, data, **kwds)
File "/home/will/tensorflow/lib/python3.5/site-packages/h5py/_hl/dataset.py", line 137, in make_new_dset
sid = h5s.create_simple(shape, maxshape)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5s.pyx", line 95, in h5py.h5s.create_simple
File "h5py/utils.pyx", line 103, in h5py.utils.convert_tuple
TypeError: Can't convert element 1 (None) to hsize_t
【问题讨论】:
-
看起来
features_shape变量有问题。错误发生在最初的create_dataset中。要么变量不是元组,要么有一些错误的值——不是整数。我认为h5py允许在形状中使用None来表示增长轴。
标签: keras deep-learning h5py