{"cells":[{"metadata":{},"cell_type":"markdown","source":"# My Kannada MNIST Kaggle competition\n### with Fastai v1"},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../input/Kannada-MNIST","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/Kannada-MNIST')\ntrain_csv = path/'train.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv(train_csv)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = data.label.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = torch.tensor(data.drop('label', axis = 1).values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X[0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Randomly Split onto Training and Validation sets"},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"rand_idx = torch.randperm(X.shape[0])\nsplit_ratio = 0.8\nsplit = int(X.shape[0] * split_ratio)\ntrain_idxs = rand_idx[:split]\ntest_idxs  = rand_idx[split:]\n\nX_train = X[train_idxs]\nX_valid = X[test_idxs]\ny_train = y[train_idxs]\ny_valid = y[test_idxs]\nX_train.shape, X_valid.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def tensor2Images(x):\n    return [Image(x[i].reshape(-1,28,28).repeat(3, 1, 1)/255.) for i in range(x.shape[0])]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class MNISTImageList(ImageList):\n    \"`ImageList` of Images stored as in `items` as tensor.\"\n\n    def open(self, fn):\n        \"No file associated to open\"\n        pass\n\n    def get(self, i):\n        res = self.items[i]\n        self.sizes[i] = sys.getsizeof(res)\n        return res","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Get LabelLists"},{"metadata":{"trusted":true},"cell_type":"code","source":"til = MNISTImageList(tensor2Images(X_train))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"til[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_ll = LabelList(MNISTImageList(tensor2Images(X_train)),CategoryList(y_train, ['0','1','2','3','4','5','6','7','8','9']))\nvalid_ll = LabelList(MNISTImageList(tensor2Images(X_valid)),CategoryList(y_valid, ['0','1','2','3','4','5','6','7','8','9']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_ll[1][0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":" valid_ll[1][1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ll = LabelLists('.',train_ll,valid_ll)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_csv  = path/'test.csv'\ndata = pd.read_csv(test_csv)\nXtest = torch.tensor(data.drop('id', axis = 1).values)\ntest_il = ItemList(tensor2Images(Xtest))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ll.add_test(test_il)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"assert len(ll.train.x)==len(ll.train.y)\nassert len(ll.valid.x)==len(ll.valid.y)","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"ll.train.x[0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### Get ImageDataBunch from LabelLists"},{"metadata":{"trusted":true},"cell_type":"code","source":"dbch = ImageDataBunch.create_from_ll(ll)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dbch.sanity_check()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dbch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dbch.show_batch(rows=4, figsize=(6,6))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"path = '/kaggle/working/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dbch,models.resnet50,metrics=accuracy, pretrained=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.freeze()","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(10,slice(2e-3,2e-2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('stage1')","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(start_lr=1e-9)\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(10,slice(2e-7,2e-6))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"scrolled":false,"trusted":true},"cell_type":"code","source":"learn.show_results(ds_type=DatasetType.Train, rows=4, figsize=(8,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(5,max_lr=1e-6)","execution_count":null,"outputs":[]},{"metadata":{"scrolled":false,"trusted":true},"cell_type":"code","source":"learn.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.export()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Inference"},{"metadata":{"trusted":true},"cell_type":"code","source":"preds,y = learn.get_preds(ds_type=DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = preds.argmax(dim=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"assert len(y)==len(test_il)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res = pd.DataFrame(y,columns=['label'],index=range(1, 5001))\nres.index.name = 'id'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res.to_csv('submission.csv')","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.7.3"}},"nbformat":4,"nbformat_minor":1}