我今天在用python写入hdf5的时候,出现了下面的错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-4a984659912f> in <module>()
5 number_of_features = len(image_feature_files)
6 for image_arg, image_file in tqdm(enumerate(image_feature_files)):
----> 7 file_id = dataset_file.create_group(image_file)
8 image_data = file_id.create_dataset('image_features',
9 (IMG_FEATS,), dtype='float32')
~/anaconda3/lib/python3.6/site-packages/h5py/_hl/group.py in create_group(self, name)
48 with phil:
49 name, lcpl = self._e(name, lcpl=True)
---> 50 gid = h5g.create(self.id, name, lcpl=lcpl)
51 return Group(gid)
52
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/_objects.pyx in h5py._objects.with_phil.wrapper()
h5py/h5g.pyx in h5py.h5g.create()
ValueError: Not a location (invalid object ID)
然后百度没有搜到答案,我这里弥补一下空缺,错误虽小,但是出现了总会让人头疼。
我的代码为:
print('Writing image features to h5...')
IMG_FEATS = 2048
cnn_extractor='inception'
dataset_file = h5py.File(cnn_extractor +'_image_name_to_features.h5')
number_of_features = len(image_feature_files)
for image_arg, image_file in tqdm(enumerate(image_feature_files)):
file_id = dataset_file.create_group(image_file)
image_data = file_id.create_dataset('image_features',
(IMG_FEATS,), dtype='float32')
image_data[:] = extracted_features[image_arg,:]
dataset_file.close()
后面发现是因为我在写入h5的时候,迭代的时候close了,所以改的方式就是把close代码放到循环外了哈
print('Writing image features to h5...')
IMG_FEATS = 2048
cnn_extractor='inception'
dataset_file = h5py.File(cnn_extractor +'_image_name_to_features.h5')
number_of_features = len(image_feature_files)
for image_arg, image_file in tqdm(enumerate(image_feature_files)):
file_id = dataset_file.create_group(image_file)
image_data = file_id.create_dataset('image_features',
(IMG_FEATS,), dtype='float32')
image_data[:] = extracted_features[image_arg,:]
dataset_file.close()
参考文献
[1].“ValueError: Not a location id (Invalid object id)” while creating HDF5 datasets. https://stackoverflow.com/questions/49151057/valueerror-not-a-location-id-invalid-object-id-while-creating-hdf5-datasets?rq=1