{"cells":[{"metadata":{},"cell_type":"markdown","source":"# **Import libraries**"},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.compat.v1 import ConfigProto\nfrom tensorflow.compat.v1 import InteractiveSession\n\nconfig = ConfigProto()\nconfig.gpu_options.per_process_gpu_memory_fraction = 0.8\nconfig.gpu_options.allow_growth = True\nsession = InteractiveSession(config=config)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow \nfrom sklearn.model_selection import StratifiedKFold\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.applications.vgg19 import preprocess_input\nfrom tensorflow.keras.applications import VGG19 \nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import models, layers,Sequential,regularizers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau,EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.layers import Input, Flatten, Dense, Conv2D, MaxPooling2D, AveragePooling2D, Dropout,BatchNormalization \nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.metrics import Recall, Precision\nfrom tensorflow.keras.optimizers import Adam","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **Preparing dataset**"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_path=\"../input/Kannada-MNIST/train.csv\"\ntest_path=\"../input/Kannada-MNIST/test.csv\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df=pd.read_csv(train_path)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This is a balanced dataset. All the numbers have 6000 images each"},{"metadata":{"trusted":true},"cell_type":"code","source":"image= train_df.iloc[1,1:].values.reshape(28,28)\nplt.imshow(image,cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_k_folds(kfolds,train_df):\n    train_df.loc[:,'kfold']=-1\n    train_df.sample(frac=1).reset_index(drop=True)\n    sf=StratifiedKFold(n_splits=kfolds)\n    y=train_df.label.values\n    for fold,(t_,v_) in enumerate(sf.split(train_df,y)):\n        train_df.loc[v_,'kfold']=fold\n    return train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_train_valid_set(fold,df):\n    train_set=df[df.kfold!=fold].reset_index(drop=True)\n    valid_set=df[df.kfold==fold].reset_index(drop=True)\n    X_train=train_set.drop(['label','kfold'],axis=1)\n    y_train=train_set.label\n    X_valid=valid_set.drop(['label','kfold'],axis=1)\n    y_valid=valid_set.label\n    X_train=X_train.values.reshape(-1,28,28,1)\n    X_valid=X_valid.values.reshape(-1,28,28,1)\n    print('The shape of train set now is',X_train.shape)\n    print('The shape of valid set now is',X_valid.shape)\n    y_train=to_categorical(y_train,num_classes=10)\n    y_valid=to_categorical(y_valid,num_classes=10)\n    return X_train,y_train,X_valid,y_valid\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_train_val_generator(X_train,X_valid,y_train,y_valid):\n    train_gen= ImageDataGenerator(rescale=1./255,\n                                  rotation_range=20,\n                                  zoom_range=0.15,\n                                  width_shift_range=0.2,\n                                  height_shift_range=0.2,\n                                  shear_range=0.15,\n                                  horizontal_flip=True,\n                                  fill_mode=\"nearest\").flow(X_train,y_train,batch_size=64)\n    valid_gen= ImageDataGenerator(rescale=1./255).flow(X_valid,y_valid,batch_size=64)\n    return train_gen,valid_gen\n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **Making Model**"},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_model():\n    model=Sequential()\n    model.add(Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = (28,28,1)))\n    model.add(Conv2D(filters = 128, kernel_size = (5,5),padding = 'Same',activation ='relu'))\n    model.add(BatchNormalization(momentum=.15))\n    model.add(MaxPooling2D(pool_size=(2,2)))\n    model.add(Dropout(0.5))\n\n\n    model.add(Conv2D(filters = 256, kernel_size = (3,3), activation ='relu'))\n    model.add(Conv2D(filters = 256, kernel_size = (3,3), activation ='relu'))\n    model.add(BatchNormalization(momentum=0.15))\n    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.5))\n\n    model.add(AveragePooling2D())\n    model.add(Flatten())\n    model.add(Dense(256, activation = \"relu\"))\n    model.add(Dropout(0.4))\n    model.add(Dense(64, activation = \"relu\"))\n    model.add(Dropout(0.4))\n    model.add(Dense(10, activation = \"softmax\"))\n    \n    model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy',tensorflow.keras.metrics.AUC(name='auc')])\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Epochs=100\nBatch_size=64\nmy_callbacks = [EarlyStopping(patience=3,monitor='val_loss', mode='min',restore_best_weights=True,verbose=True),\n                ReduceLROnPlateau(monitor='val_loss', factor=0.1,patience=2, min_lr=0.00001, mode='auto',verbose=1)]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# **Training the model**"},{"metadata":{"trusted":true},"cell_type":"code","source":"def run_train(kfold):\n    train=create_k_folds(kfold,train_df)\n    for i in range(kfold):\n        X_train,y_train,X_valid,y_valid=create_train_valid_set(i,train)\n        train_gen,val_gen=create_train_val_generator(X_train,X_valid,y_train,y_valid)\n        model=make_model()\n        print('#'*25)\n        print(f'{i}th fold training')\n        print('#'*25)\n        history=model.fit_generator(train_gen,\n                              epochs =20, validation_data=val_gen,\n                              verbose = 1, steps_per_epoch=X_train.shape[0] // Batch_size\n                              ,callbacks=my_callbacks)\n        model.save(f'model_{i}.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"run_train(5)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Model with k fold = 3 works the best**"},{"metadata":{"trusted":true},"cell_type":"code","source":"model=tensorflow.keras.models.load_model('model_3.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df=pd.read_csv(test_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test=test_df.drop('id',axis=1)\ntest=test/255\ntest=test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred=model.predict(test)     \ny_pred=np.argmax(y_pred,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub=pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsub['label']=y_pred\nsub.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}