{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!nvidia-smi","execution_count":null,"outputs":[]},{"metadata":{"id":"2ozCaAmmlXJF","colab_type":"text"},"cell_type":"markdown","source":"# Fast.ai 必要的安裝\nFAST.AI參考設置資料\nhttps://towardsdatascience.com/fastai-image-classification-32d626da20"},{"metadata":{"trusted":true},"cell_type":"code","source":"#accelerate pandas\n# !pip install modin[ray]\n# !pip install pandas","execution_count":null,"outputs":[]},{"metadata":{"id":"9B1KjhsUvR50","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"id":"x37gfyctuald","colab_type":"text"},"cell_type":"markdown","source":"# Input Image\nbecause there is no corresponding function in fastai, so we inheritance \"ImageList\" and alter it to read .csv."},{"metadata":{"id":"saYpEfiGufr3","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":119},"outputId":"27c65c87-b711-48cb-d562-3482ab01981f","executionInfo":{"status":"ok","timestamp":1573066752020,"user_tz":-480,"elapsed":2388,"user":{"displayName":"郭峻廷","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBPg2eSNHXkZonz-xkBDV_J35WaCZVsZR2_h2OxSA=s64","userId":"14475103851274282086"}},"trusted":true},"cell_type":"code","source":"path = '../input/Kannada-MNIST//'\npath\n! ls {path} -l","execution_count":null,"outputs":[]},{"metadata":{"id":"8hpTkTEcu-1M","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":253},"outputId":"dd3fa1b0-2bcd-46f8-cf10-c9f5308c6cdd","executionInfo":{"status":"ok","timestamp":1573066759993,"user_tz":-480,"elapsed":5500,"user":{"displayName":"郭峻廷","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBPg2eSNHXkZonz-xkBDV_J35WaCZVsZR2_h2OxSA=s64","userId":"14475103851274282086"}},"trusted":true},"cell_type":"code","source":"# df1 = pd.read_csv(path+'train.csv')\n# df2 = pd.read_csv(path+'Dig-MNIST.csv')\n# df = pd.concat([df1,df2],axis=0)\n# df.to_csv('train.csv',index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# df.iloc[:,1:] = df.iloc[:,1:].replace(range(128,254),'255')\n# df.iloc[:,1:] = df.iloc[:,1:].replace(range(1,127),'0')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# df.to_csv('train.csv',index = False)","execution_count":null,"outputs":[]},{"metadata":{"id":"vM8fCo698wQj","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"class CustomImageItemList(ImageList):\n    def open(self, fn):\n        img = fn.reshape(28, 28)\n        img = np.stack((img,)*3, axis=-1) # convert to 3 channels\n        return Image(pil2tensor(img, dtype=np.float32))\n\n    @classmethod\n    def from_csv_custom(cls, path:PathOrStr, csv_name:str, imgIdx:int=1, header:str='infer', **kwargs) -> 'ItemList':\n        df = pd.read_csv(Path(path)/csv_name, header=header)\n        res = super().from_df(df, path=path, cols=0, **kwargs)\n        # convert pixels to an ndarray\n        res.items = df.iloc[:,imgIdx:].apply(lambda x: x.values / 783.0, axis=1).values\n        return res","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Databunch(Fast.AI)\nget_transforms() 則是fastai的內建方法，提供了適用於大多數計算機視覺任務的預設資料增強方案：\n\n以0.5的概率隨機水平翻轉\n\n以0.75的概率在-10與10度之間旋轉\n\n以0.75的概率在1與1.1倍之間隨機放大\n\n以0.75的概率隨機改變亮度和對比度\n\n以0.75的概率進行隨機對稱扭曲\n\n\n大多數卷積神經網路的輸入層形狀都是28、32、64、96、224、384、512之類的數字"},{"metadata":{"trusted":true},"cell_type":"code","source":"#best 0.986\n# test = CustomImageItemList.from_csv_custom(path=path, csv_name='test.csv', imgIdx=1)\n# data = (CustomImageItemList.from_csv_custom(path=path, csv_name='train.csv')\n#                        .split_by_rand_pct(0.2)\n#                        .label_from_df(cols='label')\n#                        .add_test(test, label=0)\n#                        .transform(get_transforms(do_flip = False, max_rotate = 0.), size=49)\n#                        .databunch(bs=256, num_workers=16)\n#                        .normalize(mnist_stats))\n# data","execution_count":null,"outputs":[]},{"metadata":{"id":"5ziJEuP19HNa","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"test = CustomImageItemList.from_csv_custom(path=path, csv_name='test.csv', imgIdx=1)\n# DigMNIST = CustomImageItemList.from_csv_custom(path=path, csv_name='Dig-MNIST.csv')\ndata = (CustomImageItemList.from_csv_custom(path=path, csv_name='train.csv')\n                       .split_by_rand_pct(0.2)\n#                       .split_by_idx(list(range(60000,70240)))\n                       .label_from_df(cols='label')\n                       .add_test(test, label=0)\n#                        .transform(get_transforms(do_flip = False))\n                       .transform(get_transforms(do_flip = False, max_rotate = 0.,max_zoom = 1.), size=49)#, p_affine = 0.), size=49)\n                       .databunch(bs=256, num_workers=16)\n                       .normalize(mnist_stats))\ndata","execution_count":null,"outputs":[]},{"metadata":{"id":"jaiVj1oQ5fA-","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":657},"outputId":"f0d70cd3-96c8-4396-9eae-85f3c73f63c3","executionInfo":{"status":"ok","timestamp":1573066839405,"user_tz":-480,"elapsed":1443,"user":{"displayName":"郭峻廷","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBPg2eSNHXkZonz-xkBDV_J35WaCZVsZR2_h2OxSA=s64","userId":"14475103851274282086"}},"trusted":true},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(12,9))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model\nresnet 50"},{"metadata":{"id":"VDs0b_BA6871","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"arch = models.resnet50\n# arch = models.resnet152\n# arch = models.resnet18\narch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir -p /tmp/.cache/torch/checkpoints\n!cp ../input/resnet50/resnet50-19c8e357.pth /tmp/.cache/torch/checkpoints/resnet50-19c8e357.pth\n# !cp ../input/resnet152/resnet152-b121ed2d.pth /tmp/.cache/torch/checkpoints/resnet152-b121ed2d.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !cd ../input/radam-pytorch/RAdam/\n# !ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !cp ../input/radam-pytorch/RAdam .\n# import radam\n# optar = partial(radam)","execution_count":null,"outputs":[]},{"metadata":{"id":"N-yFKQrgxnbB","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":85},"outputId":"5e297c68-4f2b-4288-c3d5-764c3ba8560d","executionInfo":{"status":"ok","timestamp":1573066855076,"user_tz":-480,"elapsed":3721,"user":{"displayName":"郭峻廷","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBPg2eSNHXkZonz-xkBDV_J35WaCZVsZR2_h2OxSA=s64","userId":"14475103851274282086"}},"trusted":true},"cell_type":"code","source":"#learn = cnn_learner(data, arch,pretrained = False, loss_func = nn.CrossEntropyLoss(), metrics=[error_rate,accuracy], model_dir='../kaggle/working').to_fp16()\n\n# learn = cnn_learner(data, arch,pretrained = False, opt_func = optar, loss_func = nn.CrossEntropyLoss(), metrics=[error_rate,accuracy], model_dir='../kaggle/working').to_fp16()\n#learn = cnn_learner(data, models.densenet161, metrics=[error_rate, accuracy], model_dir=\"/tmp/model/\", pretrained=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#FOR TESTING\nlearn = cnn_learner(data, arch,pretrained = False, loss_func = nn.MultiMarginLoss(), metrics=[error_rate,accuracy], model_dir='../kaggle/working')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Learning\nOne-Cycle-Policy 大概有三个步骤：\n\n我们逐渐将学习率从 lr_max / div_factor 提高到 lr_max，同时我们逐渐减少从 mom_max 到 mom_min 的动量(momentum)。\n\n反向再做一次：我们逐渐将学习率从 lr_max 降低到 lr_max / div_factor，同时我们逐渐增加从 mom_min 到 mom_max 的动量。\n\n我们进一步将学习率从 lr_max / div_factor 降低到 lr_max /（div_factor x 100），我们保持动力稳定在 mom_max。\n\n作者：lai-bluejay\n链接：https://hacpai.com/article/1552929502787\n来源：黑客派\n协议：CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/"},{"metadata":{"trusted":true},"cell_type":"code","source":"# lr = 1e-02","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn.lr_find()\n# learn.recorder.plot(suggestion=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 0.9986\nlearn.fit_one_cycle(20)\nlearn.save('stage-1-50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# #TEST\n# learn.fit_one_cycle(3,1e-02)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn.unfreeze()\n# learn.lr_find()\n# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# lr = slice(1e-04, 1e-03)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learn.fit_one_cycle(6,lr)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plot"},{"metadata":{"trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\n\nlosses,idxs = interp.top_losses()\n\nlen(data.valid_ds)==len(losses)==len(idxs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_top_losses(20, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(12,12), dpi=60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.most_confused(min_val=2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submission(with fast.ai predict())\nwith learn.data.test_ds in datablock fast.ai"},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df = pd.read_csv(path+'sample_submission.csv')\ntmp_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,5000):\n    img = learn.data.test_ds[i][0]\n    tmp_df.loc[i]=[i,int(learn.predict(img)[1])]\ntmp_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tmp_df.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"KannadaMNIST.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}