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

# + [markdown] papermill={"duration": 0.008329, "end_time": "2020-12-20T11:03:32.742200", "exception": false, "start_time": "2020-12-20T11:03:32.733871", "status": "completed"} tags=[]
# # Modified version of LeNet using image augmentation and lot more layers.

# + _cell_guid="b1076dfc-b9ad-4769-8c92-a6c4dae69d19" _uuid="8f2839f25d086af736a60e9eeb907d3b93b6e0e5" papermill={"duration": 0.970365, "end_time": "2020-12-20T11:03:33.719908", "exception": false, "start_time": "2020-12-20T11:03:32.749543", "status": "completed"} tags=[]
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import cv2
from sklearn.model_selection import train_test_split

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))


# + papermill={"duration": 0.016231, "end_time": "2020-12-20T11:03:33.744726", "exception": false, "start_time": "2020-12-20T11:03:33.728495", "status": "completed"} tags=[]
def rotate_image(image, angle = 90, scale = 1.0):
    h, w = image.shape
    M = cv2.getRotationMatrix2D((w/2, h/2), angle, scale)
    return cv2.warpAffine(image, M, (w, h))


# + papermill={"duration": 19.71788, "end_time": "2020-12-20T11:03:53.470596", "exception": false, "start_time": "2020-12-20T11:03:33.752716", "status": "completed"} tags=[]
data = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', delimiter = ',', skiprows = 1)

# + [markdown] papermill={"duration": 0.0076, "end_time": "2020-12-20T11:03:53.486682", "exception": false, "start_time": "2020-12-20T11:03:53.479082", "status": "completed"} tags=[]
# **Using rotate_image to generate more training examples**

# + papermill={"duration": 5.526096, "end_time": "2020-12-20T11:03:59.020519", "exception": false, "start_time": "2020-12-20T11:03:53.494423", "status": "completed"} tags=[]
y = data[:, 0]
x = data[:, 1:].reshape(-1, 28, 28)

x_train, x_cv, y_train, y_cv = train_test_split(x, y, test_size = 0.1)
x_train_temp = x_train.copy()

for angle in np.arange(-10, 15, 5):
    x_train = np.concatenate((x_train, np.array([rotate_image(image, angle, scale = 1) for image in x_train_temp])))
    
x_train = x_train.reshape(-1, 28, 28, 1)
x_cv = x_cv.reshape(-1, 28, 28, 1)
    
y_train_temp = y_train.copy()
for i in range(x_train.shape[0]//y_train_temp.shape[0] - 1):
    y_train = np.concatenate((y_train, y_train_temp))    

# + papermill={"duration": 0.018169, "end_time": "2020-12-20T11:03:59.047695", "exception": false, "start_time": "2020-12-20T11:03:59.029526", "status": "completed"} tags=[]
print(f'initial train shape: {x_train_temp.shape}')
print(f'new train shape: {x_train.shape}')
print(f'Got {x_train.shape[0] // x_train_temp.shape[0]} times more images')

# + papermill={"duration": 5.441866, "end_time": "2020-12-20T11:04:04.498443", "exception": false, "start_time": "2020-12-20T11:03:59.056577", "status": "completed"} tags=[]
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, Dropout, BatchNormalization
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

# + papermill={"duration": 4592.042832, "end_time": "2020-12-20T12:20:36.550757", "exception": false, "start_time": "2020-12-20T11:04:04.507925", "status": "completed"} tags=[]
YaNet = Sequential(name = 'YaNet')
YaNet.add(Conv2D(filters = 32, 
                 kernel_size = (5, 5),
                 kernel_initializer = 'he_uniform',
                 padding = 'same', 
                 activation = 'relu', 
                 input_shape = (28, 28, 1)))
YaNet.add(BatchNormalization())
YaNet.add(Conv2D(filters = 32, 
                 kernel_size = (5, 5),
                 kernel_initializer = 'he_uniform',
                 padding = 'same', 
                 activation = 'relu'))
YaNet.add(MaxPooling2D())
YaNet.add(BatchNormalization())
YaNet.add(Conv2D(filters = 64, 
                 kernel_size = (3, 3),
                 kernel_initializer = 'he_uniform',
                 padding = 'same', 
                 activation = 'relu'))
YaNet.add(MaxPooling2D())
YaNet.add(BatchNormalization())
YaNet.add(Conv2D(filters = 128, 
                 kernel_size = (3, 3), 
                 kernel_initializer = 'he_uniform',
                 padding = 'same', 
                 activation = 'relu'))
YaNet.add(BatchNormalization())
YaNet.add(Conv2D(filters = 128, 
                 kernel_size = (3, 3), 
                 kernel_initializer = 'he_uniform',
                 padding = 'same', 
                 activation = 'relu'))
YaNet.add(MaxPooling2D())
YaNet.add(BatchNormalization())
YaNet.add(Flatten())
YaNet.add(Dense(units = 512, 
                activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
YaNet.add(Dense(units = 256, 
                activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
YaNet.add(Dense(units = 128, 
                activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
YaNet.add(Dense(units = 64, 
                activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
YaNet.add(Dense(units = 10, 
                activation = 'softmax'))

YaNet.compile(optimizer = 'adam',
              loss = 'sparse_categorical_crossentropy',
              metrics = ['accuracy'])

YaNet.fit(x_train, y_train, 
          epochs = 100,
          validation_data = (x_cv, y_cv),
          use_multiprocessing = True)

# + papermill={"duration": 25.515641, "end_time": "2020-12-20T12:21:27.143916", "exception": false, "start_time": "2020-12-20T12:21:01.628275", "status": "completed"} tags=[]
YaNet.summary()

# + papermill={"duration": 39.081402, "end_time": "2020-12-20T12:22:31.499333", "exception": false, "start_time": "2020-12-20T12:21:52.417931", "status": "completed"} tags=[]
x_test = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', skiprows = 1, delimiter = ',')
x_test = x_test.reshape(-1, 28, 28, 1)

# + [markdown] papermill={"duration": 25.336792, "end_time": "2020-12-20T12:23:21.729427", "exception": false, "start_time": "2020-12-20T12:22:56.392635", "status": "completed"} tags=[]
# **Making prediction**

# + papermill={"duration": 26.680198, "end_time": "2020-12-20T12:24:13.670678", "exception": false, "start_time": "2020-12-20T12:23:46.990480", "status": "completed"} tags=[]
final_prediction = np.argmax(YaNet.predict(x_test), axis = 1)
output = pd.DataFrame({'ImageId': np.arange(1, x_test.shape[0]+1), 'Label': final_prediction})
output.to_csv('my_submission.csv', index = False)

StarcoderdataJupyterScriptsDedupFiltered