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.add(Conv2D(64, kernel_size=(3,3), padding="same",activation="relu", input_shape=(605,700,3)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, kernel_size=(4,4), padding="same",activation="relu"))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
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
np.array(data)
It's treating it as a single array. Could you trydata.values
instead? It should most probably fix the issue. $\endgroup$data[0]
now? $\endgroup$data[0]
andtype(data[0])
. Iftype(data[0])
is np.array, then try doingnp.stack(data)
. $\endgroup$