首页 > 解决方案 > 奇怪的“数组索引过多”错误问题

问题描述

我已经为 keras 编写了一个自定义生成器,其中有这个功能:它运行了一到两次迭代,但停止在这一行上说太多的数组索引

label = label[y:(y + dy), x:(x + dx)]

请注意,所有图像和形状都具有相同的尺寸!

def random_crop(image, edge, label, random_crop_size=(800, 1600)):
    # Note: image_data_format is 'channel_last'

    assert image.shape[2] == 3
    height, width = image.shape[0], image.shape[1]
    dy, dx = random_crop_size

    x = np.random.randint(0, width - dx + 1)
    y = np.random.randint(0, height - dy + 1)

    image = image[y:(y + dy), x:(x + dx), :]
    print(label.shape)
    print(y,y+dy)
    print(x,x+dx)
    label = label[y:(y + dy), x:(x + dx)]
    if edge is not None:
        edge = edge[y:(y + dy), x:(x + dx)]
        imagePlusEdge = np.zeros((random_crop_size[0], random_crop_size[1], 4))
        imagePlusEdge[:, :, :3] = image
        imagePlusEdge[:, :, -1] = edge[:, :]
        return imagePlusEdge, label
    return image, label

标签: pythontensorflowkerasdeep-learning

解决方案


如果您label是 2D(即标签的形状不允许您通过第三个指数)或尺寸/形状小于 2D ,您将收到该错误image


推荐阅读