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

I'm trying to pass all my images (71 for each class) from folder 'train' to model.fit. The method ReadImages get these images and resize them (because are too big 4288x2848)....

But when i run my code throws this error:

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


This is my code:

def ReadImages(Path):
LabelList = list()
ImageCV = list()
classes = ["nonPdr", "pdr"]

# Get all subdirectories
FolderList = os.listdir(Path)

# Loop over each directory
for File in FolderList:
if(os.path.isdir(os.path.join(Path, File))):
for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
# Convert the path into a file
ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))
ImageCV[index] = cv2.resize(ImageCV[index], (700, 600))

# Add a label for each image and remove the file extension
LabelList.append(classes.index(os.path.splitext(File)[0]))
else:
ImageCV.append(cv2.imread(os.path.join(Path, File) + os.path.sep + Image))
# Add a label for each image and remove the file extension
LabelList.append(classes.index(os.path.splitext(File)[0]))
return ImageCV, LabelList

model = Sequential()
model.compile(optimizer='RMSprop', loss='binary_crossentropy', metrics=['accuracy'])

data, labels = ReadImages(TRAIN_DIR)
print(data[0])
model.fit(np.array(data), np.array(labels), epochs=10, batch_size=20)

model.save('model.h5')


detail: when I print (data[0]) it return:

  [0 0 0]
[0 0 0]
...
[1 0 0]
[2 2 2]
[0 0 0]]

[[3 0 0]
[3 0 0]
[3 0 0]
...
[2 0 0]
[2 2 2]
[0 0 0]]

[[8 0 0]
[8 0 0]
[8 0 0]
...
[2 0 0]
[0 0 0]
[0 0 0]]

...

[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]

[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]

[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]]


What should I do to fix it? I appreciate any help

• By the looks of it, I am guessing you have two classes with 71 images and when you are doing np.array(data) It's treating it as a single array. Could you try data.values instead? It should most probably fix the issue. Sep 3, 2019 at 16:14
• AttributeError: 'list' object has no attribute 'values' Sep 3, 2019 at 16:49
• Oh sorry!!! Why do you have lists as the return values? I am assuming you should have a data frame. Can you check data[0] now? Sep 3, 2019 at 16:58
• the lists return all the imgs from folder... And the return of data[0] is up on question. Can you be more clear? Sep 3, 2019 at 17:05
• Apologies for not being clear! I want to check your shape of data[0] and type(data[0]). If type(data[0]) is np.array, then try doing np.stack(data). Sep 3, 2019 at 17:14

def ReadImages(Path):
LabelList = list()
ImageCV = list()
classes = ["nonPdr", "pdr"]

# Get all subdirectories
FolderList = [f for f in os.listdir(Path) if not f.startswith('.')]
print(FolderList)

# Loop over each directory
for File in FolderList:
for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
# Convert the path into a file
ImageCV.append(cv2.resize(cv2.imread(os.path.join(Path, File) + os.path.sep + Image), (600,700)))
LabelList.append(classes.index(os.path.splitext(File)[0]))

return ImageCV, LabelList


A bit of change to your code after discussion in the chat room.