ValueError: Not a location (invalid object ID)

我今天在用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

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值