NOTE: This Use Case is not purposed for resource constrained devices.
In [1]:
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
Unzipping data¶
In [2]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/SiddharthGan/Kannada-MNIST.zip
!unzip -qo Kannada-MNIST.zip
# zip folder is not needed anymore
!rm Kannada-MNIST.zip
Data Preprocess¶
In [3]:
train_set = pd.read_csv('train.csv')
test_set = pd.read_csv("test.csv")
dig_set = pd.read_csv("Dig-MNIST.csv")
In [4]:
train_df=pd.read_csv("train.csv")
test_df=pd.read_csv("test.csv")
x_train=train_df.drop(["label"], axis=1).values.astype('float32')
y_train=train_df["label"].values.astype("int32")
x_test=test_df.drop(["id"],axis=1).values.astype('float32')
In [5]:
test_df.columns
Out[5]:
In [6]:
x_train=x_train.reshape(x_train.shape[0], 28, 28)/255.0
x_test=x_test.reshape(x_test.shape[0], 28, 28)/255.0
Train-Test Split¶
In [7]:
from sklearn.model_selection import train_test_split
x_train,x_val,y_train,y_val=train_test_split(x_train, y_train, test_size=0.2)
In [8]:
print(x_train.shape)
print(y_train.shape)
print(x_val.shape)
print(y_val.shape)
print(x_test.shape)
In [9]:
x_train=x_train.reshape(x_train.shape[0], 28,28,1)
x_test=x_test.reshape(x_test.shape[0],28,28,1)
x_val=x_val.reshape(x_val.shape[0],28,28,1)
Model Architecture¶
In [10]:
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Input, Dense, Activation, ReLU, Flatten, Dropout, BatchNormalization
from tensorflow.keras.models import Model
X=Input(shape=[28,28,1])
x=Conv2D(16, (3,3), strides=1, padding="same", name="conv1")(X)
x=BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer="uniform", name="batch1")(x)
x=Activation('relu',name='relu1')(x)
x=Dropout(0.1)(x)
x=Conv2D(32, (3,3), strides=1, padding="same", name="conv2")(x)
x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch2")(x)
x=Activation('relu',name='relu2')(x)
x=Dropout(0.15)(x)
x=MaxPooling2D(pool_size=2, strides=2, padding="same", name="max2")(x)
x=Conv2D(64, (5,5), strides=1, padding ="same", name="conv3")(x)
x=BatchNormalization(momentum=0.17, epsilon=1e-5, gamma_initializer="uniform", name="batch3")(x)
x=Activation('relu', name="relu3")(x)
x=MaxPooling2D(pool_size=2, strides=2, padding="same", name="max3")(x)
x=Conv2D(64, (5,5), strides=1, padding="same", name="conv4")(x)
x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch4")(x)
x=Activation('relu', name="relu4")(x)
x=Dropout(0.17)(x)
x=Conv2D(32, (3,3), strides=1, padding="same", name="conv5")(x)
x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch5")(x)
x=Activation('relu', name='relu5')(x)
#x=MaxPooling2D(pool_size=2, strides=2, padding="same", name="max5")(x)
x=Dropout(0.2)(x)
x=Conv2D(16, (3,3), strides=1, padding="same", name="conv6")(x)
x=BatchNormalization(momentum=0.15, epsilon=1e-5, gamma_initializer="uniform", name="batch6" )(x)
x=Activation('relu', name="relu6")(x)
x=Dropout(0.05)(x)
#x=MaxPooling2D(pool_size=2, strides=2, padding="same", name="max6")(x)
x=Flatten()(x)
x=Dense(50, name="Dense1")(x)
x=Activation('relu', name='relu7')(x)
x=Dropout(0.05)(x)
x=Dense(25, name="Dense2")(x)
x=Activation('relu', name='relu8')(x)
x=Dropout(0.03)(x)
x=Dense(10, name="Dense3")(x)
x=Activation('softmax')(x)
model=Model(inputs=X, outputs=x)
print(model.summary())
Training the model¶
In [11]:
from tensorflow.keras.preprocessing. image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.optimizers import Adam
checkpoint=ModelCheckpoint('best.h5', monitor='val_acc', verbose=1, save_best_only=True, mode="max")
epochs=20
lr=0.002
optimizer=Adam(lr=lr, decay=lr/(epochs*1.5))
model.compile(optimizer=optimizer, loss="sparse_categorical_crossentropy",metrics=["accuracy"])
datagenerator=ImageDataGenerator( rotation_range=9, zoom_range=0.25, width_shift_range=0.25, height_shift_range=0.25)
datagenerator.fit(x_train)
batch_size=32
history=model.fit_generator(datagenerator.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_val, y_val), verbose=2,steps_per_epoch=x_train.shape[0]//batch_size, callbacks=[checkpoint])
Accuracy Loss Graphs¶
In [12]:
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
Out[12]:
In [13]:
plt.plot(history.history['loss'], label='loss')
plt.plot(history.history['val_loss'], label = 'val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(loc='lower right')
Out[13]:
Result and Predictions¶
In [14]:
results=model.predict(x_test)
In [15]:
results=np.argmax(results, axis=1)
In [16]:
results
Out[16]:
In [17]:
model.save('model.h5')
Deep CC¶
In [18]:
!deepCC model.h5