ValueError:无法将大小为200的数组重塑为形状(200,320,240,1)

时间:2019-07-12 01:41:33

标签: python python-3.x numpy keras conv-neural-network

我正在尝试为卷积神经网络格式化一个numpy数组,并且在尝试使用此函数将数组重塑为所需尺寸时,我一直收到此错误。

x_train = x_train.reshape(200, 320, 240, 1)

每个图像的长度为320像素,宽度为240像素,其中有200个。当前,数组的格式如下:

[[..],[.....],[.....]]

其中每个“子”数组与从每个图像提取的数据相对应。但是,上面的函数然后将数组的长度读取为“子”数组的数量,而不是项的数量。我该如何将子数组合并为一个更大的数组,

[.....................]

是否允许reshape函数根据项数计算其长度?

我已经尝试过将单个术语复制到数组中,但这会产生相同的错误。我还阅读了其他所有包含相同错误的Stack Overflow帖子。

当前,这是我的数据处理脚本。


train_size = 600
test_size = 200

batch_size = 60
num_classes = 2
epochs = 10

img_rows, img_cols = 320, 240


TrainArray = []
for file in os.listdir("C:/Users/aeshon/Desktop/Project2020/Step2/TRAIN/birds"):
    img = imread("C:/Users/aeshon/Desktop/Project2020/Step2/TRAIN/birds/" + file)
    # print("Hi"+str(count))
    new_img = img[:, :, 0]
    TrainArray.append(new_img)
for file in os.listdir("C:/Users/aeshon/Desktop/Project2020/Step2/TRAIN/animals"):
    img = imread("C:/Users/aeshon/Desktop/Project2020/Step2/TRAIN/animals/" + file)
    # print("Hi"+str(count))
    new_img = img[:, :, 0]
    TrainArray.append(new_img)
print("Train Array Synthesis Complete")
x_train = np.asarray(TrainArray)
del TrainArray
print("Array Deleted!")
count = 0
TestArray = []
for file in os.listdir("C:/Users/aeshon/Desktop/Project2020/Step2/TEST/birds"):
    img = imread("C:/Users/aeshon/Desktop/Project2020/Step2/TEST/birds/" + file)
    # print("Hi"+str(count))
    new_img = img[:, :, 0]
    TestArray.append(new_img)
for file in os.listdir("C:/Users/aeshon/Desktop/Project2020/Step2/TEST/animals"):
    img = imread("C:/Users/aeshon/Desktop/Project2020/Step2/TEST/animals/" + file)
    # print("Hi"+str(count))
    new_img = img[:, :, 0]
    TestArray.append(new_img)
print("Test Array Synthesis Complete")
x_test = np.asarray(TestArray)
#print(TestArray)
del TestArray
print("Array Deleted!")

#sys.exit()

x_train = x_train.reshape(train_size, img_rows, img_cols, 1)
x_test = x_test.reshape(test_size, img_rows, img_cols, 1)

编辑:错误代码

Traceback (most recent call last):
  File "C:/Users/aeshon/PycharmProjects/Object_Detector/Model2.py", line 59, in <module>
    x_test = x_test.reshape(-1, img_rows, img_cols, 1)
ValueError: cannot reshape array of size 200 into shape (320,240,1)

该数组当前如下所示(已编辑长度):


[[  2,  13,   0, ...,   0,  16,   0],
 [ 11,  96, 112, ...,  80,  77,  13],
 [  0, 106, 122, ...,  93,  81,   4],

...

[168, 167, 164, ..., 183, 183, 184],
[170, 168, 165, ..., 183, 183, 184],
[171, 169, 165, ..., 183, 184, 184]]

0 个答案:

没有答案