Я работаю над набором данных fer2013, как вы можете видеть в коде, на выходе получилось 48, 48, 3), что составляет 3 канала, которые нельзя использовать в слое Conv2D. Я хочу, чтобы это был только отдел 1 канала - (48, 48, 1). так есть ли способ преобразовать x_train и x_test в глубину 1 канала (канал в градациях серого)?
вывод должен быть: (28709, 48, 48, 1) (3589, 48, 48, 1) вместо (28709, 48, 48, 3) (3589, 48, 48, 3)
Ссылка на набор данных: https://drive.google.com/file/d/1HH8gqzFOBavHxHbKbwjozu2QzlPlo6RW/view?usp=sharing
Спасибо.!
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)