{"cells":[{"metadata":{},"cell_type":"markdown","source":"I forked from this notebook https://www.kaggle.com/faizu07/kannada-mnist-with-fastai and tried to improved it by tuning parameters and adding augmentation to reduce overfitting."},{"metadata":{},"cell_type":"markdown","source":"Later I added ensembling with other models to check how it influences accuracy of predictions."},{"metadata":{},"cell_type":"markdown","source":"# Libraries import"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nimport imageio","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/Kannada-MNIST')\ntrain = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest  = pd.read_csv('../input/Kannada-MNIST/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Preprocessing"},{"metadata":{"trusted":true},"cell_type":"code","source":"def to_img_shape(data_X, data_y=[]):\n    data_X = np.array(data_X).reshape(-1,28,28)\n    data_X = np.stack((data_X,)*3, axis=-1)\n    data_y = np.array(data_y)\n    return data_X,data_y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_X, data_y = train.loc[:,'pixel0':'pixel783'], train['label']\n\nfrom sklearn.model_selection import train_test_split\n\ntrain_X, val_X, train_y, val_y = train_test_split(data_X, data_y, test_size=0.1,random_state=42,stratify=data_y)\n\ntest_X = test.loc[:,'pixel0':'pixel783']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_X,train_y = to_img_shape(train_X, train_y)\nval_X,val_y = to_img_shape(val_X,val_y)\ntest_X, _ = to_img_shape(test_X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def save_imgs(path:Path, data, labels):\n    path.mkdir(parents=True,exist_ok=True)\n    for label in np.unique(labels):\n        (path/str(label)).mkdir(parents=True,exist_ok=True)\n    for i in range(len(data)):\n        if(len(labels)!=0):\n            imageio.imsave( str( path/str(labels[i])/(str(i)+'.jpg') ), data[i] )\n        else:\n            imageio.imsave( str( path/(str(i)+'.jpg') ), data[i] )\n\nsave_imgs(Path('/data/train'),train_X,train_y)\nsave_imgs(Path('/data/valid'),val_X,val_y)\nsave_imgs(Path('/data/test'),test_X, [])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Augmentations and data"},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=False, max_rotate=10, max_zoom=1.1, max_warp=0.0, max_lighting=0.1)\n\ndata = (ImageList.from_folder('/data/') \n        .split_by_folder()          \n        .label_from_folder()        \n        .add_test_folder()          \n        .transform(tfms, size=64)   \n        .databunch())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(3,figsize=(6,6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(3,figsize=(6,6), ds=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training Resnet 50 model"},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir -p /tmp/.cache/torch/checkpoints\n!cp /kaggle/input/pretrained-pytorch/resnet50-19c8e357.pth /tmp/.cache/torch/checkpoints/resnet50-19c8e357.pth\n\nlearn = cnn_learner(data, models.resnet50, metrics=accuracy, model_dir = Path('../kaggle/working'),path = Path(\".\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-2\nlearn.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-4\nlearn.fit_one_cycle(4,lr)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training ResNet 101"},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp /kaggle/input/pretrained-pytorch/resnet101-5d3b4d8f.pth /tmp/.cache/torch/checkpoints/resnet101-5d3b4d8f.pth\n\nlearn101 = cnn_learner(data, models.resnet101, metrics=accuracy, model_dir = Path('../kaggle/working'),path = Path(\".\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn101.lr_find()\nlearn101.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-2\nlearn101.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn101.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn101.lr_find()\nlearn101.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-4\nlearn101.fit_one_cycle(4,lr)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training ResNet 152"},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp /kaggle/input/pretrained-pytorch/resnet152-b121ed2d.pth /tmp/.cache/torch/checkpoints/resnet152-b121ed2d.pth\n\nlearn152 = cnn_learner(data, models.resnet152, metrics=accuracy, model_dir = Path('../kaggle/working'),path = Path(\".\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn152.lr_find()\nlearn152.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-2\nlearn152.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn152.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn152.lr_find()\nlearn152.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-4\nlearn152.fit_one_cycle(4,lr)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Ensembling & Predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"preds50, _ = learn.get_preds(DatasetType.Test)\npreds101, _ = learn101.get_preds(DatasetType.Test)\npreds152, _ = learn152.get_preds(DatasetType.Test)\n\npreds = 0.34*preds50 + 0.33*preds101 + 0.33*preds152\n\ny = torch.argmax(preds, dim=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num = len(learn.data.test_ds)\nindexes = {}\n\nfor i in range(num):\n    filename = str(learn.data.test_ds.items[i]).split('/')[-1]\n    filename = filename[:-4] # get rid of .jpg\n    indexes[(int)(filename)] = i","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({ 'id': range(0, num),'label': [y[indexes[x]].item() for x in range(0, num)] })\nsubmission.to_csv(path_or_buf =\"submission.csv\", index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":1}