[英]ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1)
I am trying to build an image classifier but im running in to the error as mentioned in the title of this post. 我正在尝试构建一个图像分类器,但我正在运行这个帖子的标题中提到的错误。 Below is the code im working on.
以下是我正在进行的代码。 How do i convert my numpy array that is of shape (8020,) to the shape as required by the function fit()?
如何将形状为(8020,)的numpy数组转换为函数fit()所需的形状? I tried to print the input shape: train_img_array.shape[1:] but it gives an empty shape: ()
我试图打印输入形状: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)
Edit: ----------------------------------------------------------- As Nassim requested, adding a few more details to this post: 编辑:------------------------------------------------ -----------正如Nassim所要求的,在这篇文章中添加了一些细节:
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)
So after debuging by using the result : 因此在使用结果进行调试后:
> 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)
we see that what is returned when you do : 我们看到当你这样做时返回的内容:
train_img_array =img_train['data'].values
is actually one numpy array of shape (8020, ) where all the elements are other numpy arrays containin images. 实际上是一个numpy形状的数组(8020,),其中所有元素都是包含图像的其他numpy数组。 Basically two numpy arrays nested.
基本上嵌套了两个numpy数组。
So what you want is to kind of flatten that nested structure of nested arrays into one single array object. 所以你想要的是将嵌套数组的嵌套结构扁平化为一个单独的数组对象。 The way I would do it, it might be a bit hacky but should work, is the following :
我会这样做,它可能有点hacky但应该工作,如下:
train_img_array =img_train['data'].values
train_img_array = np.array(list(train_img_array))
So basically transform the structure of numpy array of numpy arrays into a list of numpy arrays. 所以基本上将numpy数组numpy数组的结构转换为numpy数组列表。 Then when you build a numpy array out of the list of numpy arrays, you get (magic) a numpy array with one more dimension.
然后当你从numpy数组列表中构建一个numpy数组时,你得到(魔术)一个多维数组的numpy数组。
The shape after this operation should be (8020, 700, 584, 4) 此操作后的形状应为(8020,700,584,4)
Now, I see one more potential issue you might encounter with this is the format of your image. 现在,我看到您可能遇到的另一个潜在问题是图像的格式。 The channel dimension of your images is the last one (4 channels here).
图像的通道尺寸是最后一个(此处为4个通道)。 You should then, in your convolutional layers specify :
然后,您应该在卷积层中指定:
conv2D(... , data_format="channels_last", )
Also, your input shape for the first layer is (700, 584, 4), not (256, 256, 3) 另外,第一层的输入形状是(700,584,4),而不是(256,256,3)
hope it works :-) 希望它有效:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.