text_plain_output_1.png
from sklearn.model_selection import train_test_split

from tensorflow import keras

from tensorflow.keras.optimizers import RMSprop

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import numpy as np

import pandas as pd

import tensorflow as tf

import tensorflow as tf

mnist = keras.datasets.mnist
(train_images_mnist, train_labels_mnist), (test_images_mnist, test_labels_mnist) = mnist.load_data()
train_images_mnist = np.reshape(train_images_mnist, (train_images_mnist.shape[0], 28, 28, 1))
test_images_mnist = np.reshape(test_images_mnist, (test_images_mnist.shape[0], 28, 28, 1))

az_path = '/kaggle/input/az-handwritten-alphabets-in-csv-format/A_Z Handwritten Data.csv'
AZ_data = pd.read_csv(az_path, header=None)
AZ_labels = AZ_data.values[:, 0]
AZ_images = AZ_data.values[:, 1:]
AZ_images = np.reshape(AZ_images, (AZ_images.shape[0], 28, 28, 1))

from sklearn.model_selection import train_test_split
test_size = float(len(test_labels_mnist)) / len(train_labels_mnist)
train_images_AZ, test_images_AZ, train_labels_AZ, test_labels_AZ = train_test_split(AZ_images, AZ_labels, test_size=test_size)
train_labels_mnist = train_labels_mnist + max(AZ_labels) + 1
test_labels_mnist = test_labels_mnist + max(AZ_labels) + 1
train_images = np.concatenate((train_images_AZ, train_images_mnist), axis=0)
train_labels = np.concatenate((train_labels_AZ, train_labels_mnist))
test_images = np.concatenate((test_images_AZ, test_images_mnist), axis=0)
test_labels = np.concatenate((test_labels_AZ, test_labels_mnist))

import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop
model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(32, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(512, activation='relu'), tf.keras.layers.Dense(len(np.unique(train_labels)), activation='softmax')])
model.compile(optimizer=RMSprop(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.summary()

from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1.0 / 255, rotation_range=15, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.2, horizontal_flip=False, fill_mode='nearest')
test_datagen = ImageDataGenerator(rescale=1.0 / 255)

train_generator = train_datagen.flow(train_images, train_labels, batch_size=50, shuffle=True)
validation_generator = test_datagen.flow(test_images, test_labels, batch_size=50, shuffle=True)

history = model.fit(train_generator, steps_per_epoch=750, epochs=100, validation_data=validation_generator, validation_steps=75, verbose=2)
model.save('/kaggle/working/model_v2')