python - 奇怪的“数组索引过多”错误问题
问题描述
我已经为 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
解决方案
如果您label
是 2D(即标签的形状不允许您通过第三个指数)或尺寸/形状小于 2D ,您将收到该错误image
。
推荐阅读
- javascript - 来自 Json 的过滤器数组
- java - 如何使用 Try Catch Block 捕获特定的自定义异常
- python - 从文件名以“df”开头的目录中删除所有文件
- c# - 捕获的变量 IndexOutOfBounds
- ruby-on-rails - Rails 5.2.4:key 必须是 32 字节
- java - Gson 返回 null
- oracle - 为什么我有一个错误“精确提取返回的行数超过了请求的行数”?
- javascript - 尝试使用用 JQuery 编写的日历创建一个简单的时间选择器
- java - 通过 Spring Boot Rest API 查询 mongo db 集合,以分页格式返回结果
- r - R仍然是单线程的吗?