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()? I tried to print the input shape: train_img_array.shape[1:] but it gives an empty shape: ()
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:
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)
createModel()
. – deeiipkeras
docs the input for theconv2d
layer must be 4-dimensional of the type:(samples, channels, rows, cols)
or(samples, rows, cols, channels)
– Dav2357