【问题标题】:cannot reshape array of size 23715 into shape (224,224,3)无法将大小为 23715 的数组重塑为形状 (224,224,3)
【发布时间】:2019-12-20 21:55:23
【问题描述】:

我正在做一个皮肤癌检测算法。问题是我有 2635 个 224 x 224 像素的 RGB 图像。数据增强后,我得到 23715 张图像(train_data)。

我的问题是我得到了这个错误 --> ValueError: cannot reshape array of size 23715 into shape (224,224,3)

这是我的代码:

X = np.array([i[0] for i in train_data]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
Y = [i[1] for i in train_data]

test_x = np.array([i[0] for i in test_data]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
test_y = [i[1] for i in test_data]

model.fit({'input': X}, {'targets': Y}, n_epoch=10, shuffle=True, validation_set=({'input': test_x}, {'targets': test_y}),  
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

我应该怎么做才能解决这个问题?在互联网上我发现了类似的错误,但我仍然无法修复它。

【问题讨论】:

  • 你能展示train_data的结构吗?
  • training_data.append([np.array(img), np.array(label)])
  • 每个img的形状是什么?
  • 每张图片的形状为--> (224, 224, 3)
  • 请编辑您的问题并在底部添加输出

标签: python numpy


【解决方案1】:

您的问题是train_data 中的图像大小不同。从您的测试输出:

>>> [i[0].shape for i in train_data[:5]]
[(160, 160, 3), (224, 224, 3), (224, 224, 3), (160, 160, 3), (224, 224, 3)]

这里至少有两种不同的尺寸(160x160 RGB 和 224x224 RGB)。

效果是 NumPy 在创建 X 时不知道如何对齐数据,因为输入元素没有统一的大小。如果您打印X,您会看到如下内容:

>>> X
array([array([[[...
    ...
    ...]]])], dtype=object)

在这种情况下,NumPy 会采用这种方式。

您将要追踪为什么您的图片在您的情况下具有不同的尺寸。

【讨论】:

    猜你喜欢
    • 2022-06-12
    • 2020-10-15
    • 2019-08-23
    • 2020-02-18
    • 2020-04-14
    • 2020-10-05
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多