【发布时间】:2020-08-06 18:40:38
【问题描述】:
我正在处理 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)
【问题讨论】:
-
不确定您需要的格式,但您可以使用 #RRGGBB 格式将通道转换为单个十六进制值
-
您好,这取决于您在寻找什么样的功能。因此,如果您转换为 1 个通道,您将获得输入的灰度版本。一种通用的方法是取三个渠道的平均值。而不是调整大小,你应该执行
np.mean(img, axis=2) -
@DJSchaffner 我希望 foder 中的所有图像只有 1 个单一通道,即灰度
-
@AnuragReddy 谢谢你,让我试试那个。
-
@AnuragReddy 不知何故,输出仍然执行 3 个通道部门。 :( 应该是 : (28709, 48, 48, 1) (3589, 48, 48, 1) 而不是 (28709, 48, 48, 3) (3589, 48, 48, 3)
标签: python image-scaling