I'm working on fer2013 dataset, as you can see in the code the output got 48, 48, 3) which is 3 channels dept, that can't be used in Conv2D layer. I want it's just 1 channel dept - ( 48, 48, 1). so is there anyway to convert the x_train and x_test to 1 channel depth (grayscale channel)?
the output should be :(28709, 48, 48, 1) (3589, 48, 48, 1) instead of (28709, 48, 48, 3) (3589, 48, 48, 3)
Link to the dataset : https://drive.google.com/file/d/1HH8gqzFOBavHxHbKbwjozu2QzlPlo6RW/view?usp=sharing
Thank you.!
import os
import cv2
def prepare_Data(path="Facial_expression/fer2013",
image_shape = (48, 48)):
TRAINING_DATA_PATH = os.path.join(path, 'Training')
TESTING_DATA_PATH = os.path.join(path, 'PrivateTest')
x_train = [] # is the training data set
y_train = [] # is the set of labels to all the data in x_train
x_test = []
y_test = []
label_id = 0
num_classes = len(os.listdir(TRAINING_DATA_PATH)) # get number of classes 7
for label in os.listdir(TRAINING_DATA_PATH): # get label in training path
# Read training data
for img_file in os.listdir(os.path.join(TRAINING_DATA_PATH, label)):
img = cv2.imread(os.path.join(TRAINING_DATA_PATH, label, img_file)) # read all image in training path
img = cv2.resize(img, image_shape) # resize all image with size = 48 x 48
x_train.append(img) # append all training image in x_train
y = np.zeros(num_classes) # create one hot vector with dimension of 7
# print(y)
y[label_id] = 1
y_train.append(y)
# Read testing data
for img_file in os.listdir(os.path.join(TESTING_DATA_PATH, label)):
img = cv2.imread(os.path.join(TESTING_DATA_PATH, label, img_file))
img = cv2.resize(img, image_shape)
x_test.append(img)
y = np.zeros(num_classes)
y[label_id] = 1
y_test.append(y)
label_id += 1
return np.array(x_train), np.array(y_train), np.array(x_test), np.array(y_test)
x_train, y_train,x_test, y_test = prepare_Data()
print("Number of images in Training set:", len(x_train))
print("Number of images in Test set:", len(x_test))
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
'Output'
Number of images in Training set: 28709
Number of images in Test set: 3589
(28709, 48, 48, 3)
(3589, 48, 48, 3)
(28709, 7)
(3589, 7)