I am training a model for Optical Character Recognition of Gujarati Language. The input image is a character image. I have taken 37 classes. Total training images are 22200 (600 per class) and testing images are 5920 (160 per class). My input images are 32x32 in size.
Below is my code:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Convolution2D, MaxPooling2D
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import ReLU
from tensorflow.keras.callbacks import TensorBoard
import pickle
import time
from datetime import datetime
from packaging import version
import tensorflow as tf
from tensorflow import keras
import numpy as np
from tensorflow.keras import layers
model = tf.keras.applications.DenseNet121(include_top=False, weights='imagenet', pooling='max', input_shape=(32,32,3))
model.trainable = True
print("Number of layers in the base model: ", len(model.layers))
fine_tune_at = 400
for layer in model.layers[:fine_tune_at]:
layer.trainable = False
base_outputs = model.layers[-1].output
final_outputs = layers.Dense(37)(base_outputs)
new_model = keras.Model(inputs=model.input, outputs=final_outputs)
new_model.summary()
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=False)
test_datagen = ImageDataGenerator(horizontal_flip = False)
training_set = train_datagen.flow_from_directory('C:/Users/shweta/Desktop/characters/train',
target_size = (32, 32),
batch_size = 64,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('C:/Users/shweta/Desktop/characters/test',
target_size = (32, 32),
batch_size = 64,
class_mode = 'categorical')
new_model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
new_model.fit_generator(training_set,
epochs = 25,
validation_data = test_set, shuffle=True)
new_model.save('alphanumeric.mod')
Below is my output:
Thanks in advance!!
Rescale the input data. You can do this by setting rescale factor to 1. / 255 in the ImageDataGenerator.