# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/satoru2001/Handwritten_DigitRecognition/blob/master/HandDigit.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="H3aDy8hcKb_K" colab_type="text"
# # Installing Kaggle and Downloading Dataset

# + id="C7S4TUu1Bj57" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 215} outputId="07b5803b-c273-447c-b110-1cbe107b6102"
# !pip install kaggle

# + id="EKOFR2H9Ate2" colab_type="code" colab={}
# !mkdir -p ~/.kaggle
# !cp kaggle.json ~/.kaggle/
# !chmod 600 ~/.kaggle/kaggle.json

# + id="6gGjEb24AySV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 197} outputId="10d098b5-15cf-41d1-e589-c46af7774527"
# !kaggle competitions download -c digit-recognizer

# + [markdown] id="277GkY3ZKjWH" colab_type="text"
# # Unzipping Train and Test to '/tmp'

# + id="mTDjjBk5BYvp" colab_type="code" colab={}
import os
import zipfile
ref = '/content/train.csv.zip'
zip_ref = zipfile.ZipFile(ref,'r')
zip_ref.extractall('/tmp')
zip_ref.close()

# + id="lu5fpH6gKPwc" colab_type="code" colab={}
ref = '/content/test.csv.zip'
zip_ref = zipfile.ZipFile(ref,'r')
zip_ref.extractall('/tmp')
zip_ref.close()

# + [markdown] id="ElWoFkZpKqy_" colab_type="text"
# # Loading Train Data

# + id="Bfj_9aDDKUFm" colab_type="code" colab={}
import pandas as pd

# + id="bwEOubaQLWlV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 408} outputId="a46bd42c-f531-4c58-ec48-a2764a9089b3"
df = pd.read_csv('/tmp/train.csv',index_col=None)
df.head(10)

# + [markdown] id="5vaHtUHAKxP1" colab_type="text"
# # Splitting to Labels,Images and reshaping Images to (28,28,1)

# + id="mecM6lJOq9V9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="aba21906-0c7d-4726-9fe8-60e009d6c712"
labels = df['label'].to_numpy()
labels[:10]

# + id="fTt50imfMRJ2" colab_type="code" colab={}
image_df = df.drop(axis=1,columns = 'label')
images = image_df.to_numpy()

# + id="KDl7zq5vMUZT" colab_type="code" colab={}
images = images.reshape(42000,28,28)

# + id="YxTlxeulTuaz" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="bdb2e1ae-6499-45c2-c90c-0dad1a770b26"
images.shape

# + id="1XIb1ZySse-m" colab_type="code" colab={}
import numpy as np
images = np.expand_dims(images,axis=-1)
images = images/255

# + id="vkMxTf0p0KPX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="9da353ff-588d-41cc-c108-dbee370f6fbb"
images[1].shape

# + [markdown] id="FKrgNOBGLGTQ" colab_type="text"
# # Test Train Split and Model

# + id="moCpCCMLTCGi" colab_type="code" colab={}
import tensorflow
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Dense,BatchNormalization,Dropout,Lambda,Flatten
from tensorflow.keras.models import Sequential,Model
from tensorflow.keras.regularizers import l1
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from sklearn.model_selection import train_test_split
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.backend import clear_session
clear_session() 
opt = Adam(lr=0.0005)

# + id="ICLuQ5JHtKKR" colab_type="code" colab={}
x_train,x_test,y_train,y_test = train_test_split(images,labels,test_size=0.1)

# + id="fM-suNrcvPZQ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 809} outputId="46da2b20-b95e-46df-af66-6130e9f07539"
CNN = Sequential()

#Convolution layer 1
CNN.add(Conv2D(8,(3,3),activation='relu',input_shape=(28,28,1),kernel_regularizer='l2'))
CNN.add(MaxPooling2D())
CNN.add(BatchNormalization())
CNN.add(Dropout(0.2))

#Convolution layer 2
CNN.add(Conv2D(32,(3,3),activation='relu',kernel_regularizer='l2'))
CNN.add(MaxPooling2D())
CNN.add(BatchNormalization())
CNN.add(Dropout(0.2))

#Convolution layer 3
CNN.add(Conv2D(64,(3,3),activation='relu',kernel_regularizer='l2'))
CNN.add(MaxPooling2D())
CNN.add(BatchNormalization())
CNN.add(Dropout(0.2))

# Flatten Layer
CNN.add(Flatten())

# Dense Layer 1
CNN.add(Dense(1024,activation='relu',kernel_regularizer='l2'))
CNN.add(Dropout(0.15))

#Dense Layer 2
CNN.add(Dense(128,activation='relu',kernel_regularizer='l2'))
CNN.add(Dropout(0.15))

#Output
CNN.add(Dense(10,activation='softmax',kernel_regularizer='l2'))

CNN.compile(loss='sparse_categorical_crossentropy',optimizer=opt,metrics=['accuracy'],)

CNN.summary()


# + [markdown] id="TY6CuYUoLUUh" colab_type="text"
# # Call Backs for
# - To stop training when an accuracy of 99% Reached
# - To decrease Learning rate if previous 2 epochs val_loss is not changed
# - To store maximum val_accuracy weights

# + id="cDK03kquzLaS" colab_type="code" colab={}
class myCallback(tensorflow.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('accuracy')>0.99999):
      print("\nReached 99% accuracy so cancelling training!")
      self.model.stop_training = True

stop_callback = myCallback()

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.001,
                              patience=3, min_lr=0.000001, mode='auto')

checkpoint = ModelCheckpoint("model_weights.h5", monitor='val_accuracy',
                             save_weights_only=True, mode='max', verbose=1)

callbacks = [checkpoint, reduce_lr ,stop_callback]

# + [markdown] id="shyj7EbyLtyH" colab_type="text"
# # Training

# + id="1yflXRqAyfJ_" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="b2d45d85-47d0-44da-896c-e99c810b3075"
history = CNN.fit(x=x_train,y=y_train,batch_size=32,epochs=30,validation_data=(x_test,y_test),shuffle=True,callbacks=callbacks)

# + [markdown] id="pzbFt8PZLwF4" colab_type="text"
# # Plotting Training,Testing Accuracies

# + id="FDkJDfgdz3DC" colab_type="code" colab={}
import matplotlib.pyplot as plt

# + id="JcXKszHACenP" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 295} outputId="af2c6087-5beb-47dc-92d7-ade9822f1070"
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# + id="6aBcfQTKEnRn" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 295} outputId="113f4881-63b1-4627-b337-2409aad641a5"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# + [markdown] id="GqY3l9dZL3a0" colab_type="text"
# # Loading Test data,Resize and predicting

# + id="tMUmv8S1D7np" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="d0a8393b-388c-43d2-be77-49c577250585"
test_df = pd.read_csv('/tmp/test.csv')
test_images = test_df.to_numpy()
test_images = test_images.reshape(28000,28,28)
test_images = np.expand_dims(test_images,axis=-1)
test_images = test_images/255
test_images.shape

# + id="Jjmad8XgD9wS" colab_type="code" colab={}
predict_model = Model(CNN.input,CNN.output)
predict_model.load_weights('model_weights.h5')

# + id="P131HoYyF1Qf" colab_type="code" colab={}
predictions = predict_model.predict(test_images)
prediction_classes = np.argmax(predictions,axis=1)

# + id="o84n4o0SGI21" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="8ea28b39-16b9-4a28-934f-feff7a8f3149"
prediction_classes[:10]

# + id="lLWtQ80qGMOu" colab_type="code" colab={}
columns = ['ImageId','Label']

# + id="_82Ee722Gqjc" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="0bdfe455-50d9-414b-ff2c-c702419041f8"
ImageId = np.arange(1,28001,1)
ImageId.shape

# + id="BrB3vJHdISMa" colab_type="code" colab={}
list_of_tuples = list(zip(ImageId.tolist(),prediction_classes.tolist()))  

# + id="G3B157ILI-IB" colab_type="code" colab={}
test_df = pd.DataFrame(list_of_tuples, columns=columns)  

# + id="OpC_6Q94JQwT" colab_type="code" colab={}
test_df.to_csv('test.csv',index=False)

# + id="mI63tWJBJacH" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered