ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的形状为数组(8020,1)

时间:2018-05-30 10:12:26

标签: python numpy keras convolutional-neural-network

我正在尝试构建一个图像分类器,但我正在运行这个帖子标题中提到的错误。以下是我正在进行的代码。如何将形状为(8020,)的numpy数组转换为函数fit()所需的形状?我试图打印输入形状:train_img_array.shape [1:]但是它给出了一个空的形状:()

import numpy as np
img_train.shape
img_valid.shape
img_train.head(5)
img_valid.head(5)

(8020, 4)
(2006, 4)
         ID  index  class                                               data
8030  11596  11596      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
2152  11149  11149      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
550   10015  10015      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
1740   9035   9035      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
9549   8218   8218      1  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
         ID  index  class                                               data
3312   5481   5481      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
9079  10002  10002      0  [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ...
6129  11358  11358      0  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
1147   2613   2613      1  [[[255, 255, 255, 0], [255, 255, 255, 0], [255...
7105   5442   5442      1  [[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ...

img_train.dtypes

ID        int64
index     int64
class     int64
data     object
dtype: object

train_img_array = np.array([])
train_id_array = np.array([])
train_lab_array = np.array([])
train_id_array = img_train['ID'].values
train_lab_array = img_train['class'].values
train_img_array =img_train['data'].values

train_img_array.shape
train_lab_array.shape
train_id_array.shape

(8020,)
(8020,)
(8020,)

# Importing the Keras libraries and other packages


#matplotlib inline
from __future__ import print_function

import keras
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
Using Theano backend.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.

classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape = (256, 256, 3)))
classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.25))

classifier.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
classifier.add(Conv2D(64, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))  
classifier.add(Dropout(0.25))

classifier.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
classifier.add(Conv2D(64, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.25))

classifier.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
classifier.add(Conv2D(64, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
classifier.add(Dropout(0.25))

classifier.add(Flatten())
classifier.add(Dense(units = 256, activation = 'relu'))
classifier.add(Dropout(0.25))
classifier.add(Dense(units = 1, activation = 'sigmoid'))    classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
classifier.summary()

batch_size = 32
epochs = 15


history = classifier.fit(train_img_array, train_lab_array, batch_size=batch_size, epochs=epochs, verbose=1, 
                   validation_data=(valid_img_array, valid_lab_array))
classifier.evaluate(valid_img_array, valid_lab_array)


ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1)

编辑:--------------------------------------------- -------------- 正如纳西姆所要求的,在这篇文章中添加了更多细节:

print(train_img_array) 

[ array([[[255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        ..., 
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0]],

       [[255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        ..., 
        ..., 
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0]]], dtype=uint8)
 array([[[255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        ..., 
        ..., 
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]]], dtype=uint8)]


print(list(train_img_array)) 

[array([[[255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        ..., 
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0]],

       [[255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        ..., 
        ..., 
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0]]], dtype=uint8), array([[[255, 255, 255,   0],
        [255, 255, 255,   0],
        [255, 255, 255,   0],
        ..., 


print(np.array(list(train_img_array)))
throws the error:

ValueError: could not broadcast input array from shape (700,584,4) into shape (700,584)

1 个答案:

答案 0 :(得分:3)

所以在使用结果进行调试后:

> print(type(train_img_array[0]))
<type 'numpy.ndarray'>

> print(train_img_array[0].shape)
(700, 584, 4)

> print(rain_img_array[0])
array([[[255, 255, 255, 0], [255, 255, 255, 0], [255, 255, 255, 0], ..., ..., [255, 255, 255, 0], [255, 255, 255, 0], [255, 255, 255, 0]]], dtype=uint8)

我们会看到您执行的操作时返回的内容:

train_img_array =img_train['data'].values

实际上是一个numpy形状的数组(8020,),其中所有元素都是包含图像的其他numpy数组。基本上是嵌套了两个numpy数组。

所以你想要的是将嵌套数组的嵌套结构扁平化为一个单独的数组对象。我会这样做,它可能有点hacky但应该工作,如下:

train_img_array =img_train['data'].values
train_img_array = np.array(list(train_img_array))

所以基本上将numpy数组numpy数组的结构转换为numpy数组列表。然后当你从numpy数组列表中构建一个numpy数组时,你得到(魔术)一个多维数组的numpy数组。

此操作后的形状应为(8020,700,584,4)

现在,我发现您可能遇到的另一个潜在问题是图像的格式。图像的通道尺寸是最后一个(此处为4个通道)。然后,您应该在卷积层中指定:

conv2D(... , data_format="channels_last", )

此外,第一层的输入形状为(700,584,4),而不是(256,256,3)

希望它有效: - )