Home>

ValueError: Error when checking input: expected conv2d_4_input to have 4 dimensions, but got array with shape (0, 1)
I am in trouble with the error.

For sites i am referring to
input_shape = X_train.shape [1:]
Then you get an error
input_shape = (64, 64, 3)
Changed to

Is the error message shape (0, 1) meaning that there is no data because the usage of input_shape is wrong

Applicable source code
# coding: utf-8
import keras
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape
import numpy as np
from sklearn.model_selection import train_test_split
from PIL import Image
import glob
folder = ["cat", "lion", "dog", "woman"]
image_size = 50
X = []
Y = []
for index, name in enumerate (folder):
    dir = "./" + name
    files = glob.glob (dir + "/*.jpg")
    for i, file in enumerate (files):
        image = Image.open (file)
        image = image.convert ("RGB")
        image = image.resize ((image_size, image_size))
        data = np.asarray (image)
        X.append (data)
        Y.append (index)
X = np.array (X)
Y = np.array (Y)
X = X.astype ('float32')
X = X/255.0
# Convert correct label format
Y = np_utils.to_categorical (Y, 4)
# Learning data and test data
X_train, X_test, y_train, y_test = train_test_split (X, Y, test_size = 0.20)
# Build CNN
model = Sequential ()
model.add (Conv2D (32, (3, 3), padding = 'same', input_shape = (64, 64, 3)))
model.add (Activation ('relu'))
model.add (Conv2D (32, (3, 3)))
model.add (Activation ('relu'))
model.add (MaxPooling2D (pool_size = (2, 2)))
model.add (Dropout (0.25))
model.add (Conv2D (64, (3, 3), padding = 'same'))
model.add (Activation ('relu'))
model.add (Conv2D (64, (3, 3)))
model.add (Activation ('relu'))
model.add (MaxPooling2D (pool_size = (2, 2)))
model.add (Dropout (0.25))
model.add (Flatten ())
model.add (Dense (512))
model.add (Activation ('relu'))
model.add (Dropout (0.5))
model.add (Dense (4))
model.add (Activation ('softmax'))
model.summary ()
model.compile (loss = 'categorical_crossentropy',
              optimizer = 'SGD',
              metrics = ['accuracy'])
history = model.fit (X_train, y_test, epochs = 20)