Hello,
I was trying to apply transfer learning on the MNIST dataset from keras.datasets. The mnist dataset is grayscale with only single channel (28,28,1). But, as large networks like VGG or inception need input images in size (224,224,3), I was trying to resize the whole training data to that particular size.
- To convert the images from grayscale to RGB I used cv2.COLOR_GRAY2RGB. I was successful in transforming the whole dataset.
code snippet for reference:
def fixColor(image):
return(cv2.cvtColor(image, cv2.COLOR_GRAY2RGB))
new_train = []
for i in range(len(input_train)):
new_train.append(fixColor(input_train[i]))
Now, the next function takes input in numpy array format, so I had to convert this list(new_train) to numpy. This was successful with:
from numpy import array
a = array( new_train )
- Now, to change the height and width from (28,28) to (224,224) I decided to use image.smart_resize (from tensorflow.keras.preprocessing.image). It worked well when I trying to experiment with small examples. But when I tried transforming the whole training dataset, the kernal crashes, as the RAM being used shoots up. I tried both on local system and google colab to no avail.
code snippet:
new_train2 = [] # a new list as the append operation is more efficient in list compared to numpy array
for i in range(len(a)):
new_train2.append(image.smart_resize(a[i],(224,224)))
I tried many different hacks but nothing worked completely. Could anyone kindly help me with this issue?
Entire code :
import tensorflow as tf
from tensorflow import keras
from keras.datasets import mnist
import numpy as np
import cv2
from tensorflow.keras.preprocessing import image
(input_train, target_train), (input_test, target_test) = mnist.load_data()
def fixColor(image):
return(cv2.cvtColor(image, cv2.COLOR_GRAY2RGB))
new_train = []
for i in range(len(input_train)):
new_train.append(fixColor(input_train[i]))
from numpy import array
a = array( new_train )
new_train2 = []
for i in range(len(a)):
new_train2.append(image.smart_resize(a[i],(224,224)))
Thank you.