{"cells":[{"metadata":{"_uuid":"f23c5d96-d4bf-4ff6-a7a0-ddb24dc5a518","_cell_guid":"4a29b8f8-e3e1-4c29-bebd-1b9e721bc6c8","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f2eca8fa-6929-48f7-8b5d-f1002429ee6d","_cell_guid":"d432f332-f4c9-4b19-b00c-98627bda85a2","trusted":true},"cell_type":"code","source":"#%reload_ext autoreload\n#%autoreload 2\n# %matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d9e2e56d-26e9-4926-a30e-1146402a2aa8","_cell_guid":"494a5d57-e679-4e28-9187-4fb1bc3e2fb2","trusted":true},"cell_type":"code","source":"from fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2b0e1a45-755a-4ac3-83f0-32023194431e","_cell_guid":"3aba77d3-cf79-4030-a63f-554ff0bd9b10","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')\ntrain_other = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dde3b7d1-5c16-4c50-85f1-4fc23245b698","_cell_guid":"5eda12d0-2cea-4d91-b90c-1e9fd8c70613","trusted":true},"cell_type":"code","source":"data,labels = (train.iloc[:,1:],train.iloc[:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_other,labels_other = (train_other.iloc[:,1:],train_other.iloc[:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_train,labels_train = (pd.concat([data, data_other]),pd.concat([labels, labels_other]))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8b8d17e4-28d6-47cc-abeb-f29ffefd8f81","_cell_guid":"f0605e5c-4273-433c-98ee-46c9a809ca57","trusted":true},"cell_type":"markdown","source":"**Create Train Valid Data Split****"},{"metadata":{"_uuid":"1789dfd6-2b1a-43d1-85ec-f37ee8f9b270","_cell_guid":"032513fa-0309-46f4-8248-0a6a3f78a01f","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ndata_train, data_valid, labels_train, labels_valid = train_test_split(data_train, labels_train, test_size=0.25, random_state=42,stratify=labels_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_train","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4d3fa41f-f2e3-4823-b62a-c83d4ea79054","_cell_guid":"7e2edcf2-3270-490e-a41e-f485c7ad69c4","trusted":true},"cell_type":"markdown","source":"***Save Images in Folder***"},{"metadata":{"_uuid":"7dfd2fa5-9f9b-43b9-9b29-ce6c83f8fbab","_cell_guid":"06b3f03b-5054-400b-a276-1f3a82bff2a4","trusted":true},"cell_type":"code","source":"#import pdb\nimport imageio\ndef save_img_to_folder(path:Path,data,labels):\n    path.mkdir(parents=True,exist_ok=True)\n    \n    for i in range(len(data)):\n        test = path\n        #pdb.set_trace()\n        temp_path = test/(str(labels[i]))\n        if os.path.isdir(temp_path):\n            imageio.imwrite(str(temp_path/(str(i)+'.jpg')), data[i])\n            #plt.imsave(str( temp_path/(str(i)+'.jpg') ), data[i], cmap='Greys')\n        else:\n            temp_path.mkdir(parents=True,exist_ok=True)\n            #plt.imsave(str( temp_path/(str(i)+'.jpg') ), data[i], cmap='Greys')\n            imageio.imwrite(str(temp_path/(str(i)+'.jpg')), data[i])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"092e88fd-6140-4e26-b378-33d6083f2450","_cell_guid":"94e7aa51-bb5f-4acd-a51d-dc1867c77588","trusted":true},"cell_type":"code","source":"data_arr = np.array(data_valid).reshape(-1,28,28)\nlabels_arr = np.array(labels_valid)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"61ffa810-3238-475d-a2f9-6bdd91bfc7e2","_cell_guid":"749059ad-5de4-44e0-a7ea-e45319ce8eb9","trusted":true},"cell_type":"code","source":"save_img_to_folder(Path('valid'),data_arr,labels_arr)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"eb2b1d4a-918c-414b-9b8c-aca5f3d7d694","_cell_guid":"cd7bab41-22e2-4f66-8f66-d87d5b554d9e","trusted":true},"cell_type":"code","source":"data_arr1 = np.array(data_train).reshape(-1,28,28)\nlabels_arr1 = np.array(labels_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b3ebf4b6-7a6a-4bde-b0b9-9befb9f1e348","_cell_guid":"e40f9be4-e45a-4932-9ac6-bcecd6a5f168","trusted":true},"cell_type":"code","source":"save_img_to_folder(Path('train'),data_arr1,labels_arr1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c91ad160-092d-483f-8ce7-0b31709fa9b2","_cell_guid":"834c88e3-d2b3-4449-9281-d953d1bae766","trusted":true},"cell_type":"code","source":"path = Path('/kaggle/working')\ntrain_path = path/'train'\nvalid_path = path/'valid'\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3f8e2d67-80f0-49fb-9c1e-7bae76e3d8db","_cell_guid":"2721bf95-df95-4537-94eb-4404d7ef40a8","trusted":true},"cell_type":"code","source":"# test_data,test_labels = (test.iloc[:,1:],test.iloc[:,0])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4211fa14-3345-4e69-ada5-aae9bfed58bb","_cell_guid":"eb2ee710-8e12-4e36-bd76-fb4a8b476108","trusted":true},"cell_type":"code","source":"# data_arr2 = np.array(test_data).reshape(-1,28,28)\n# labels_arr2 = np.array(test_labels)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8313719-6349-43ad-a325-aaa0327b144d","_cell_guid":"cdc9bfd2-b900-4785-b251-7da685293339","trusted":true},"cell_type":"code","source":"# save_img_to_folder(Path('test'),data_arr2,labels_arr2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"df1f5131-9d33-4f72-b120-808547a20d37","_cell_guid":"9f4f3e27-534b-4926-91f9-b48982e630c8","trusted":true},"cell_type":"markdown","source":"> **Model Train**"},{"metadata":{"_uuid":"8f67f9d8-e44f-461d-af87-2c5a644a1b36","_cell_guid":"f0fd9893-635d-43f4-bf05-f0dbf5c56c49","trusted":true},"cell_type":"code","source":"from fastai.metrics import error_rate\nfrom fastai.vision import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(42)\ntfms = get_transforms(do_flip=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"src = (ImageList.from_folder(path)\n        .split_by_folder(train='train', valid='valid')\n        .label_from_folder())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = (src.transform(tfms,size=64)\n        .databunch(bs=64)\n        .normalize(mnist_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# data = (ImageList.from_folder(path)\n#         .split_by_folder(train='train', valid='valid')\n#         .label_from_folder() \n#         .transform(tfms, size=64)\n#         .databunch(bs=64)\n#         .normalize(mnist_stats)\n#        )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.train_ds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.classes","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c2c86f9e-8290-4ac2-a954-89451bc92fbe","_cell_guid":"9c5b376d-d418-4191-a1ae-7c3bd3656c7f","trusted":true},"cell_type":"code","source":"# data = ImageDataBunch.from_folder(path, train=\"train\", valid=\"valid\",test=\"test\",\n#         ds_tfms=get_transforms(do_flip=False), size=64,bs=32).normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b124eb4-970f-435f-a768-7b881f30e6f1","_cell_guid":"b1cdf819-68d1-4522-8569-dac048bcdf89","trusted":true},"cell_type":"code","source":"data.show_batch(rows=4,figsize=(7,8))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b379317d-f92d-4205-86fc-89ab690fdaae","_cell_guid":"416f4b30-5f43-4070-bd65-e5cffadeb6fb","trusted":true},"cell_type":"code","source":"!mkdir -p /tmp/.cache/torch/checkpoints","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls /kaggle/input/resnet152","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0c82b7d8-083c-4dab-9b0f-e537bf95b0ce","_cell_guid":"ae36d311-eda0-4229-919e-50907fd57dbe","trusted":true},"cell_type":"code","source":"!cp /kaggle/input/resnet152/resnet152.pth /tmp/.cache/torch/checkpoints/resnet152-b121ed2d.pth","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1e4a3872-a715-43c4-a161-05fce0427db4","_cell_guid":"3ce3b40d-c5de-40bc-a5a8-d51e9de30eef","trusted":true},"cell_type":"code","source":"!ls /tmp/.cache/torch/checkpoints/","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"34d07df0-faae-4fd8-a2cc-af718b499462","_cell_guid":"8546c7e5-043c-455a-88d4-b207c4f780aa","trusted":true},"cell_type":"code","source":"learner = cnn_learner(data,models.resnet152, metrics=[error_rate, accuracy])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c46e7af9-4cf2-45fb-af07-100f208770cc","_cell_guid":"52df7903-22d1-4190-b7e9-06ea2e07fe09","trusted":true},"cell_type":"code","source":"learner.fit_one_cycle(12)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e5f06df2-f17e-4f69-9df8-ced1afff06ef","_cell_guid":"af0a16fb-1efd-41ec-a531-2c60210220ac","trusted":true},"cell_type":"code","source":"learner.save('kannada-stage1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learner.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ccf7216c-73b4-4b9d-8697-c0aaa2bef400","_cell_guid":"3e594b4e-b9fc-4846-b86e-911cd7efbabd","trusted":true},"cell_type":"code","source":" interp = ClassificationInterpretation.from_learner(learner)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9ea46781-eb6b-4443-a2fc-cbfa41deeade","_cell_guid":"a251a909-68a6-4a35-a83f-0e547e72c58b","trusted":true},"cell_type":"code","source":"losses,idx = interp.top_losses()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"52f2c175-9e7b-4b49-9a26-a0c1a759d0fe","_cell_guid":"f0f14aa7-037a-4d41-9be7-7365324eae0d","trusted":true},"cell_type":"code","source":"len(data.valid_ds)==len(losses)==len(idx)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d4ae2987-3fc3-4257-bdb2-4ad4c0b6e388","_cell_guid":"cdbfb36a-bde0-4d61-a62b-f5e1fcdcb2e3","trusted":true},"cell_type":"code","source":"interp.plot_top_losses(9, figsize=(15,11))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3a7c12aa-adab-4be9-b0a1-a764311e3e2f","_cell_guid":"92d5a589-3506-4312-8ac4-3f824f8b9112","trusted":true},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(12,12), dpi=60)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c51034d3-c299-40e4-8c4e-4fcc3df82ece","_cell_guid":"5aa33acc-ac2c-4f46-9cea-8d22937e5e35","trusted":true},"cell_type":"code","source":"learner.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8034be44-ba51-41ca-83ea-bf713e20c03f","_cell_guid":"7133ac52-1444-425b-9fa6-dc748d671122","trusted":true},"cell_type":"code","source":"learner.recorder.plot(suggestion=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4402d911-a3cf-4a91-a7fc-4fca84513795","_cell_guid":"53acfd14-2880-447c-8d7b-529c511b2829","trusted":true},"cell_type":"code","source":"learner.unfreeze()\nlearner.fit_one_cycle(10, max_lr=slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2acc1303-4ea7-4755-a752-3549bf41ed63","_cell_guid":"9333069f-bfc0-439e-8665-5ac98620aac1","trusted":true},"cell_type":"code","source":"learner.save('kannada-stage2')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Tranfer Learning**"},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.fit_one_cycle(8,1e-5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.save('kannada-stage3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# data = (src.transform(tfms, size=64)\n#         .databunch().normalize(mnist_stats))\n\n# learner.data = data\n# data.train_ds[0][0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.freeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.lr_find()\n# learner.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.fit_one_cycle(3, slice(1e-1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.save('kannada-stage3')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.fit_one_cycle(4, max_lr=slice(1e-2, 1e-1/2))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.recorder.plot_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# learner.save('kannada-stage4')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learner.load('kannada-stage2')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1a48a5df-e659-4231-ab30-b6db38e8f8fa","_cell_guid":"a5784af2-03e8-497c-87c0-364bd1fde0b6","trusted":true},"cell_type":"code","source":"img = learner.data.valid_ds[0][0]","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"05f9964a-adc0-40e2-a3ec-e82c203a59e8","_cell_guid":"7bba3c34-d6a5-4fed-85a2-d266c220c687","trusted":true},"cell_type":"code","source":"learner.predict(img)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"ab3bb017-8464-46f5-82e4-278806d7d370","_cell_guid":"72b94504-f9ff-4f3f-a4ff-9bffbf62173d","trusted":true},"cell_type":"code","source":"learner.data.test_ds","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"20460025-0e6a-42ed-92fd-4bc0779afc2d","_cell_guid":"cbfe46cf-095c-42b1-a0da-d9d050582a8c","trusted":true},"cell_type":"markdown","source":"***Predictions on Test Dataset***"},{"metadata":{"_uuid":"200d9f98-e2ed-4992-a0bc-46e7a2bec576","_cell_guid":"ac8e98f6-f6d0-4f0d-8b88-4e0ddf40e302","trusted":true},"cell_type":"code","source":"#sub_df = pd.DataFrame(columns=['id','label'])","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"68525cc5-927d-4030-89d9-2ee7e714ce26","_cell_guid":"91d3b483-0d43-4abc-82ee-d11e4eeebefb","trusted":true},"cell_type":"code","source":"#len(learner.data.test_ds.x)","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"77a372a4-38e2-4bc0-bd0e-b690d5db8352","_cell_guid":"97dd35d6-f9c3-4bf4-897e-1b987f4cda23","trusted":true},"cell_type":"code","source":"#learner.predict(learner.data.test_ds.x[6])[1]","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"6930ba01-43c5-4686-bff3-3db7456b08f0","_cell_guid":"dbd0c427-f618-4dd6-bfb2-49843ff54cd1","trusted":true},"cell_type":"code","source":"# for i in range(len(learner.data.test_ds.x)):\n    \n#     sub_df.loc[i]=[i+1,int(learner.predict(learner.data.test_ds.x[i])[1])]","execution_count":0,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_csv = pd.read_csv('../input/Kannada-MNIST/test.csv')\ntest_csv.drop('id',axis = 'columns',inplace = True)\nsub_df = pd.DataFrame(columns=['id','label'])\ntest_data = np.array(test_csv)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_img(data):\n    t1 = data.reshape(28,28)/255\n    t1 = np.stack([t1]*3,axis=0)\n    img = Image(FloatTensor(t1))\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastprogress import progress_bar","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mb=progress_bar(range(test_data.shape[0]))\nfor i in mb:\n    timg=test_data[i]\n    img = get_img(timg)\n    sub_df.loc[i]=[i+1,int(learner.predict(img)[1])]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def decr(ido):\n    return ido-1\n\nsub_df['id'] = sub_df['id'].map(decr)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d94c9b21-0e5f-4837-a506-91a9fce28f1f","_cell_guid":"0114a17d-b3cd-49be-aa92-ce70f2806b54","trusted":true},"cell_type":"code","source":"sub_df.head()","execution_count":0,"outputs":[]},{"metadata":{"_uuid":"ba32ee58-39c9-47f7-9e61-3db7e55c043d","_cell_guid":"f971baa1-e509-49a5-a30b-f3ed2ca428a5","trusted":true},"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)","execution_count":0,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -r valid train","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}