首页 > 解决方案 > 写入 H5 文件时“无法广播 (37,) -> (37, 2)”

问题描述

我想在我的 H5 文件(称为“注释”)的每个单元格中插入一个坐标(x, y)Nonewhere (x, y) = (0.0, 0.0)

这是H5文件的可视化:

这是我创建和填充 H5 文件的代码:


# Create a NaN matrix
none_matrix = np.zeros((num_images, len(skeleton_names_list), 2))
for i in range(num_images):
    for j in range(len(skeleton_names_list)):
        none_matrix[i][j] = None

# Create the H5 file with all NaN entries
hf.create_dataset('annotations', shape=(num_images, len(skeleton_names_list), 2), dtype=np.float64, data=none_matrix)

joints_coordinates = []
num_ann = 0
j_file = open('dog.json')
j_data = json.load(j_file)
for xy in j_data:
    joints_coordinates.append(xy['joints'])
    image_name = xy['segmentation_path']
    image_index = idx_dict.get(image_name)
    for i in range(len(skeleton_names_list)):
        if joints_coordinates[num_ann][i][0] + joints_coordinates[num_ann][i][1] == 0:
            joints_coordinates[num_ann][i] = None
    # Update the image_index-row of the H5 file with the array "joints_coordinates[num_ann]"
    hf["annotations"][image_index] = joints_coordinates[num_ann]
    num_ann += 1

joints_coordinates形式为:

[[[x, y], ...], ...]

为什么我会收到此错误?为什么在我的 H5 文件中有 0.0 值而不是 NaN?

    Traceback (most recent call last):
      File "C:/Users/Alberto Ursino/Desktop/IntellIj Local Files/Write on H5/write.py", line 120, in <module>
        hf["annotations"][image_index] = joints_coordinates[num_ann]
      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 "C:\Users\Alberto Ursino\anaconda3\envs\TensorFlow 1x\lib\site-packages\h5py\_hl\dataset.py", line 707, in __setitem__
        for fspace in selection.broadcast(mshape):
      File "C:\Users\Alberto Ursino\anaconda3\envs\TensorFlow 1x\lib\site-packages\h5py\_hl\selections.py", line 299, in broadcast
        raise TypeError("Can't broadcast %s -> %s" % (target_shape, self.mshape))
    TypeError: Can't broadcast (37,) -> (37, 2)

标签: pythonh5py

解决方案


np.nan而不是在None这里:

for i in range(len(skeleton_names_list)):
        if joints_coordinates[num_ann][i][0] + joints_coordinates[num_ann][i][1] == 0:
            joints_coordinates[num_ann][i] = None

作品!


推荐阅读