2

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)
desertnaut
  • 57,590
  • 26
  • 140
  • 166
frankenstein
  • 125
  • 2
  • 11
  • Not sure about the format you need but you could convert the channels to a single hex value with #RRGGBB format for example – DJSchaffner Aug 06 '20 at 18:43
  • Hi, so it depends on how you what kind of features you are looking for. So, if you convert into 1 channel, you will get a grayscale version of the inputs. One generic way to do this is by taking the average across the three channels. Instead of resizing you should perform `np.mean(img, axis=2)` – Anurag Reddy Aug 06 '20 at 18:43
  • @DJSchaffner I want all image in foder has just 1 single channle, which is grayscale – frankenstein Aug 06 '20 at 18:47
  • @AnuragReddy thank you, let me try that one. – frankenstein Aug 06 '20 at 18:48
  • @AnuragReddy somehow the output still perform 3 channels dept. :( it should be : (28709, 48, 48, 1) (3589, 48, 48, 1) instead of (28709, 48, 48, 3) (3589, 48, 48, 3) – frankenstein Aug 06 '20 at 18:51
  • @frankenstein change the axis along which you want to take the mean. It would be `axis = 3` in this case. And then you can reshape it to `(img.shape,1)` – Anurag Reddy Aug 06 '20 at 19:01

1 Answers1

3

After a day of researching I found that I need to reshape 28709 images into shape (48,48,1) but current shape of image is (48,48,3) which is 3 channels dept. First I need to convert all image in grayscale by gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) then use the numpy.reshape to reshape all image into 1 channel dept.

TRAINING_DATA_PATH = os.path.join(path, 'Training')
TESTING_DATA_PATH = os.path.join(path, 'PrivateTest')

x_train = [] 
y_train = [] 
x_test = []
y_test = []

label_id = 0


num_classes = len(os.listdir(TRAINING_DATA_PATH)) 
for label in os.listdir(TRAINING_DATA_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)) 
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ADD THIS
        img = cv2.resize(gray, image_shape)
        
        x_train.append(img) 
        y = np.zeros(num_classes) 
        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))
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ADD THIS
        img = cv2.resize(gray, image_shape)
        x_test.append(img)
        
        y = np.zeros(num_classes)
        y[label_id] = 1
        y_test.append(y)
    label_id += 1
x_train = np.array(x_train) # ADD THIS
x_train = x_train.reshape(x_train.shape[0],48,48, 1) # ADD THIS

x_test = np.array(x_test) # ADD THIS
x_test = x_test.reshape(x_test.shape[0], 48, 48,1) # ADD THIS

return np.array(x_train), np.array(y_train), np.array(x_test), np.array(y_test) 

Number of images in Training set: 28709
Number of images in Test set: 3589
(28709, 48, 48, 1)
(3589, 48, 48, 1)
(28709, 7)
(3589, 7)
frankenstein
  • 125
  • 2
  • 11