1

More of a novice question though, I am using VGG16 as feature extractor. I have used 3 channel images as input. Now I want to use 1 channel images as input. As VGG-16 is trained on 3 channel images, can I use 1 channel images as input?

1 Answers1

0

Okay I get to an answer.

First of all, VGG16 requires 3 channel input and then minimum size of an image should be 32×32.

So, if you have 1 channel input convert hem to 3 channel as below:

#let' convert images to 3 channels
X_train=np.dstack([X_train] * 3)
X_val =np.dstack([X_val ]*3)
test_images =np.dstack([test_images]*3)

X_train = X_train.reshape(-1, 28,28,3)
X_val= X_val.reshape (-1,28,28,3)
test_images= test_images.reshape (-1,28,28,3)


X_train.shape, X_val.shape, test_images.shape

Then convert the image shape more than 32×32. I did for 48×48:

# Resize the images 48*48 as required by VGG16

from keras.preprocessing.image import img_to_array, array_to_img

X_train = np.asarray([img_to_array(array_to_img(im, scale=False).resize((48,48))) for im in X_train])
X_val = np.asarray([img_to_array(array_to_img(im, scale=False).resize((48,48))) for im in X_val])
test_images = np.asarray([img_to_array(array_to_img(im, scale=False).resize((48,48))) for im in test_images])
#train_x = preprocess_input(x)
X_train.shape, X_val.shape, test_images.shape