{"cells":[{"metadata":{},"cell_type":"markdown","source":"In this kernel, I will be exploring some of the techniques presented in the Fast.ai's deep learning course. [Link](https://github.com/fastai/course-v3/blob/master/nbs/dl1/lesson7-resnet-mnist.ipynb)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\nfrom fastai.vision import *\nfrom fastai.tabular import *\nimport re\n\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":{"trusted":true},"cell_type":"code","source":"path = '/kaggle/input/Kannada-MNIST/'\ntrain = pd.read_csv(path+'train.csv')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(path+'test.csv')\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Thanks to Hans Lee for sharing this custom method to load image data as pixels from a csv : https://www.kaggle.com/hanslee01/digit-recognizer-with-cnn-fastai"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"class NumpyImageList(ImageList):\n    def open(self, fn):\n        img = fn.reshape(28,28,1)\n        return Image(pil2tensor(img, dtype=np.float32))\n    \n    @classmethod\n    def from_csv(cls, path:PathOrStr, csv:str, **kwargs)->'ItemList': \n        df = pd.read_csv(Path(path)/csv, header='infer')\n        res = super().from_df(df, path=path, cols=0, **kwargs)\n\n        if 'label' in df.columns:\n            df = df.drop('label', axis=1)\n        if 'id' in df.columns:\n            df = df.drop('id', axis=1)\n        df = np.array(df)/255.\n        res.items = (df-df.mean())/df.std()\n\n        return res\n    \ndefaults.cmap='binary'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = NumpyImageList.from_csv(path, 'test.csv')\ntest","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = NumpyImageList.from_csv(path, 'train.csv')\ntrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=False)\ndata = (NumpyImageList.from_csv(path, 'train.csv')\n        .split_by_rand_pct(.1)\n        .label_from_df(cols='label')\n        .add_test(test, label=0)\n        .transform(tfms)\n        .databunch(bs=128, num_workers=0)\n        .normalize(imagenet_stats))\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(rows=5, figsize=(10,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xb,yb = data.one_batch()\nxb.shape,yb.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def conv(ni,nf): return nn.Conv2d(ni, nf, kernel_size=3, stride=2, padding=1)\n\nmodel = nn.Sequential(\n    conv(3, 8), # 14   ## Why not 1 ??\n    nn.BatchNorm2d(8),\n    nn.ReLU(),\n    conv(8, 16), # 7\n    nn.BatchNorm2d(16),\n    nn.ReLU(),\n    conv(16, 32), # 4\n    nn.BatchNorm2d(32),\n    nn.ReLU(),\n    conv(32, 16), # 2\n    nn.BatchNorm2d(16),\n    nn.ReLU(),\n    conv(16, 10), # 1\n    nn.BatchNorm2d(10),\n    Flatten()     # remove (1,1) grid\n)\n\nif torch.cuda.is_available():\n    model = model.cuda()\nlearn = Learner(data, model, metrics=accuracy, model_dir='/kaggle/working/models')\nlearn.summary()","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":"learn.fit_one_cycle(3, max_lr=slice(1e+0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions, *_ = learn.get_preds(DatasetType.Test)\nlabels = np.argmax(predictions, 1)\n# output to a file\nsubmission_df = pd.DataFrame({'id': list(range(0,len(labels))), 'label': labels})\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Check format of sample submission\nsample_submission = pd.read_csv(path+'sample_submission.csv')\nsample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Our submission files matches the required format, so we can submit it\nsubmission_df.to_csv('submission.csv', index=False)","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":1}