0

我在将图像转换为数组时遇到了一个问题,在重塑数组时,这显示给我

ValueError: cannot reshape array of size 43200 into shape (3,120,160)

这是我的代码:

img_size = 224
i = 0
for _file in train_files:
    img = load_img(data_dir + "/" + _file , target_size=(224,224))  # this is a PIL imageflowers[0], target_size=(224,224))
    img.thumbnail((image_width, image_height))
    # Convert to Numpy Array 
    x = img_to_array(img)  
    x = x.reshape((3, 120, 160))
    # Normalize
    x = (x - 255.0) / 255.0
    dataset[i] = x
    i += 1
    if i % 250 == 0:
        print("%d images to array" % i)
print("All images to array!")

我该如何解决这个问题?

4

1 回答 1

0

问题是您的数组有 43200 个元素,而您想要的形状 (3, 120, 160) 需要 57600 个元素。维度的乘积必须等于数组的大小,numpy 才能对其进行整形。

也许如果你将它重塑为 (3, 120, 120) 它会起作用,但我想你需要检查原始图像的尺寸以获得你想要的结果。

于 2021-01-16T22:30:19.547 回答