I am going to use Keras pretrained Inception V3 model. After preprocessing the image shape is 224 x 224 x 3. But the input to the Keras Inception V3 model is (?, 3 , ?, ?), that is after batch size comes the channel. So I did array reshape. But this makes the whole thing super slow and eats up memory I am not sure why.
Note: When the image shape was 224, 224, 3 it works fine on a simple CNN. But 3, 224, 224 fed to the simple CNN made things super slow and memory overflow.
This is my code:
def get_image_preprocessed(image_name):
im = Image.open(image_name)
im = np.asarray(im)
im = im/float(255)
im = im.reshape(3,224,224) #this changes 224,224,3 to 3,224,224
return im
This is the input tensor shape
tf.Tensor 'input_1:0' shape=(?, 3, ?, ?) dtype=float32
More Information:
Model-
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3,224, 224), padding='same', activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))
Generator Function-
def generator(save_dir_path, encoding_list, batch_size, image_size):
# Create empty arrays to contain batch of features and labels#
batch_features = np.zeros((batch_size, 3, image_size, image_size))
batch_labels = np.zeros((batch_size,len(encoding_list)))
image_list= [file for file in os.listdir(save_dir_path) if (file.endswith('.jpeg') or file.endswith('.-'))]
while True:
for i in range(batch_size):
# choose random index in features
image_name= random.choice(image_list)
batch_features[i] = get_image_preprocessed(save_dir_path, image_name)
batch_labels[i] = np.asarray(get_encoding(encoding_list, image_name.split('_')[0]))
yield batch_features, batch_labels