{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# gives the best result\n\n## preds=np.clip(preds,0.0125,0.9875)\n","metadata":{}},{"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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"papermill":{"duration":0.034112,"end_time":"2022-07-07T06:49:35.592396","exception":false,"start_time":"2022-07-07T06:49:35.558284","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:09:17.746481Z","iopub.status.idle":"2022-07-11T04:09:17.747359Z","shell.execute_reply.started":"2022-07-11T04:09:17.747046Z","shell.execute_reply":"2022-07-11T04:09:17.747076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{"id":"WgwJeBhbG6WH","papermill":{"duration":0.0138,"end_time":"2022-07-07T06:49:52.670357","exception":false,"start_time":"2022-07-07T06:49:52.656557","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!unzip '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip' >&NULL\n!unzip '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip' >&NULL\n!rm NULL","metadata":{"id":"Ck2CrHFR3NBn","papermill":{"duration":17.033724,"end_time":"2022-07-07T06:49:52.641203","exception":false,"start_time":"2022-07-07T06:49:35.607479","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:09:17.757926Z","iopub.execute_input":"2022-07-11T04:09:17.758631Z","iopub.status.idle":"2022-07-11T04:09:37.911829Z","shell.execute_reply.started":"2022-07-11T04:09:17.758588Z","shell.execute_reply":"2022-07-11T04:09:37.910148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.listdir('.')","metadata":{"id":"MCPx-jno3O0D","outputId":"b1d83d6d-4488-48d9-e67e-a0daa30cca33","papermill":{"duration":0.026476,"end_time":"2022-07-07T06:49:52.710953","exception":false,"start_time":"2022-07-07T06:49:52.684477","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:09:37.916400Z","iopub.execute_input":"2022-07-11T04:09:37.917239Z","iopub.status.idle":"2022-07-11T04:09:37.929677Z","shell.execute_reply.started":"2022-07-11T04:09:37.917180Z","shell.execute_reply":"2022-07-11T04:09:37.928352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir=\"train\"\ntest_dir=\"test\"","metadata":{"id":"liPSMVZ33TRH","papermill":{"duration":0.022189,"end_time":"2022-07-07T06:49:52.747556","exception":false,"start_time":"2022-07-07T06:49:52.725367","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:09:37.931113Z","iopub.execute_input":"2022-07-11T04:09:37.932946Z","iopub.status.idle":"2022-07-11T04:09:37.955620Z","shell.execute_reply.started":"2022-07-11T04:09:37.932900Z","shell.execute_reply":"2022-07-11T04:09:37.954189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\nimport random\n\ntrain_list = glob(os.path.join(train_dir,\"*\"))\ntest_list = glob(os.path.join(test_dir,\"*\"))\ntest_list=sorted(test_list)\nprint( \"Training Images:\",len(train_list) )\nprint( \"Testing Images:\",len(test_list) )","metadata":{"id":"upkIAqFZ3WKe","outputId":"9cc0134c-d464-4a3f-8cae-de25d6298953","papermill":{"duration":0.165528,"end_time":"2022-07-07T06:49:52.927268","exception":false,"start_time":"2022-07-07T06:49:52.76174","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:09:37.961647Z","iopub.execute_input":"2022-07-11T04:09:37.964359Z","iopub.status.idle":"2022-07-11T04:09:38.108618Z","shell.execute_reply.started":"2022-07-11T04:09:37.964330Z","shell.execute_reply":"2022-07-11T04:09:38.107198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pickle as pkl\n\n# with open('../input/training-list-of-cat-dog-images/new_train_list.pkl', 'rb') as f:\n#     new_train_list=pkl.load(f)\n# len(new_train_list)","metadata":{"papermill":{"duration":0.021086,"end_time":"2022-07-07T06:49:52.962682","exception":false,"start_time":"2022-07-07T06:49:52.941596","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:09:38.110266Z","iopub.execute_input":"2022-07-11T04:09:38.111007Z","iopub.status.idle":"2022-07-11T04:09:38.117159Z","shell.execute_reply.started":"2022-07-11T04:09:38.110960Z","shell.execute_reply":"2022-07-11T04:09:38.115708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bad_imgs_list=[]\n# for a in train_list:\n#     if a not in new_train_list:\n#         bad_imgs_list.append(a)\n# bad_imgs_list","metadata":{"papermill":{"duration":0.021529,"end_time":"2022-07-07T06:49:52.998929","exception":false,"start_time":"2022-07-07T06:49:52.9774","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:09:38.118676Z","iopub.execute_input":"2022-07-11T04:09:38.119767Z","iopub.status.idle":"2022-07-11T04:09:38.130886Z","shell.execute_reply.started":"2022-07-11T04:09:38.119662Z","shell.execute_reply":"2022-07-11T04:09:38.129455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_imgs_list=['train/cat.11565.jpg',\n 'train/cat.7564.jpg',\n 'train/dog.5584.jpg',\n 'train/cat.8456.jpg',\n 'train/cat.8047.jpg',\n 'train/dog.8736.jpg',\n 'train/cat.10029.jpg',\n 'train/dog.12223.jpg',\n 'train/dog.10797.jpg',\n 'train/cat.9171.jpg',\n 'train/cat.5351.jpg',\n 'train/dog.10747.jpg',\n 'train/dog.2614.jpg',\n 'train/dog.9188.jpg',\n 'train/dog.11299.jpg',\n 'train/dog.4334.jpg',\n 'train/dog.11248.jpg',\n 'train/dog.7727.jpg',\n 'train/cat.7487.jpg',\n 'train/cat.9770.jpg',\n 'train/cat.7111.jpg',\n 'train/cat.4085.jpg',\n 'train/cat.5355.jpg',\n 'train/cat.7920.jpg',\n 'train/cat.2433.jpg',\n 'train/cat.11222.jpg',\n 'train/cat.5418.jpg',\n 'train/cat.2663.jpg',\n 'train/cat.4688.jpg',\n 'train/cat.8504.jpg',\n 'train/dog.6475.jpg',\n 'train/dog.7.jpg',\n 'train/dog.2130.jpg',\n 'train/dog.9517.jpg',\n 'train/dog.10733.jpg',\n 'train/cat.10700.jpg',\n 'train/cat.3822.jpg',\n 'train/cat.7968.jpg',\n 'train/cat.1450.jpg',\n 'train/cat.9250.jpg',\n 'train/dog.2877.jpg',\n 'train/dog.10225.jpg',\n 'train/cat.10181.jpg',\n 'train/cat.10863.jpg',\n 'train/cat.2939.jpg',\n 'train/cat.2520.jpg',\n 'train/dog.10297.jpg',\n 'train/dog.4180.jpg',\n 'train/dog.5336.jpg',\n 'train/dog.7798.jpg',\n 'train/dog.10801.jpg',\n 'train/dog.4507.jpg',\n 'train/dog.8671.jpg',\n 'train/dog.10237.jpg',\n 'train/dog.12376.jpg',\n 'train/dog.1773.jpg',\n 'train/dog.3147.jpg',\n 'train/dog.6957.jpg',\n 'train/dog.3503.jpg',\n 'train/dog.7076.jpg',\n 'train/cat.10712.jpg',\n 'train/cat.11184.jpg',\n 'train/cat.8289.jpg']\nlen(bad_imgs_list)","metadata":{"papermill":{"duration":0.02741,"end_time":"2022-07-07T06:49:53.041123","exception":false,"start_time":"2022-07-07T06:49:53.013713","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:10:01.622921Z","iopub.execute_input":"2022-07-11T04:10:01.623319Z","iopub.status.idle":"2022-07-11T04:10:01.638424Z","shell.execute_reply.started":"2022-07-11T04:10:01.623286Z","shell.execute_reply":"2022-07-11T04:10:01.637144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for a in bad_imgs_list:\n    del train_list[train_list.index(a)]","metadata":{"papermill":{"duration":0.044402,"end_time":"2022-07-07T06:49:53.099616","exception":false,"start_time":"2022-07-07T06:49:53.055214","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:10:03.597195Z","iopub.execute_input":"2022-07-11T04:10:03.597581Z","iopub.status.idle":"2022-07-11T04:10:03.621023Z","shell.execute_reply.started":"2022-07-11T04:10:03.597550Z","shell.execute_reply":"2022-07-11T04:10:03.619284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print( \"Training Images:\",len(train_list) )","metadata":{"papermill":{"duration":0.02251,"end_time":"2022-07-07T06:49:53.13611","exception":false,"start_time":"2022-07-07T06:49:53.1136","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:10:04.617200Z","iopub.execute_input":"2022-07-11T04:10:04.617556Z","iopub.status.idle":"2022-07-11T04:10:04.626616Z","shell.execute_reply.started":"2022-07-11T04:10:04.617526Z","shell.execute_reply":"2022-07-11T04:10:04.625084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_list[:4],test_list[:4]","metadata":{"id":"3OtK2gey3X_z","outputId":"442bd9c3-abcd-412a-b989-334ad15b2bff","papermill":{"duration":0.022825,"end_time":"2022-07-07T06:49:53.172997","exception":false,"start_time":"2022-07-07T06:49:53.150172","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T04:10:04.988129Z","iopub.execute_input":"2022-07-11T04:10:04.989640Z","iopub.status.idle":"2022-07-11T04:10:04.999901Z","shell.execute_reply.started":"2022-07-11T04:10:04.989592Z","shell.execute_reply":"2022-07-11T04:10:04.998027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nnp.random.seed(54)","metadata":{"id":"k80ouUd93bGa","papermill":{"duration":3.336248,"end_time":"2022-07-07T06:49:56.523579","exception":false,"start_time":"2022-07-07T06:49:53.187331","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:09:18.115509Z","iopub.execute_input":"2022-07-11T05:09:18.119184Z","iopub.status.idle":"2022-07-11T05:09:18.136534Z","shell.execute_reply.started":"2022-07-11T05:09:18.119128Z","shell.execute_reply":"2022-07-11T05:09:18.132654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndblock=DataBlock(blocks=(ImageBlock,CategoryBlock),\n                 get_y=lambda x:x.rsplit('/',1)[1].split('.',1)[0] ,\n                 item_tfms=Resize(224),\n                 batch_tfms=[Normalize.from_stats(*imagenet_stats),*aug_transforms(mult=1,max_rotate=50,flip_vert=True)],  # underfit still good results\n#                  batch_tfms=[Normalize.from_stats(*imagenet_stats),*aug_transforms(mult=1.8)],\n#                  batch_tfms=[Normalize.from_stats(*imagenet_stats)],\n                 splitter  = RandomSplitter(valid_pct=0.1),\n)\n\n# ds=dblock.datasets(train_list)\n# print(ds[0])\n# del ds\ndls=dblock.dataloaders(train_list,bs=64)\ndls.train.show_batch(max_n=6,nrows=1,unique=True)","metadata":{"id":"9fvt5bEp3kr4","outputId":"7fe0a416-f12a-44f6-ebd9-9e1e1163a14f","papermill":{"duration":10.196182,"end_time":"2022-07-07T06:50:06.73446","exception":false,"start_time":"2022-07-07T06:49:56.538278","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:09:20.579460Z","iopub.execute_input":"2022-07-11T05:09:20.580080Z","iopub.status.idle":"2022-07-11T05:09:25.916168Z","shell.execute_reply.started":"2022-07-11T05:09:20.580028Z","shell.execute_reply":"2022-07-11T05:09:25.914655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.train.show_batch(max_n=8,nrows=2,unique=True)","metadata":{"id":"xb4XBsSZU27e","outputId":"3a6de401-a207-461e-acaa-04b8f7b51078","papermill":{"duration":2.31491,"end_time":"2022-07-07T06:50:09.067984","exception":false,"start_time":"2022-07-07T06:50:06.753074","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:09:25.919050Z","iopub.execute_input":"2022-07-11T05:09:25.926021Z","iopub.status.idle":"2022-07-11T05:09:27.343856Z","shell.execute_reply.started":"2022-07-11T05:09:25.925971Z","shell.execute_reply":"2022-07-11T05:09:27.342423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.train.show_batch(max_n=8,nrows=2)","metadata":{"id":"pfJs9A0vk524","outputId":"f518fd4e-69bf-445a-d6dd-9880d534001a","papermill":{"duration":1.022061,"end_time":"2022-07-07T06:50:10.112971","exception":false,"start_time":"2022-07-07T06:50:09.09091","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:09:27.346772Z","iopub.execute_input":"2022-07-11T05:09:27.347693Z","iopub.status.idle":"2022-07-11T05:09:29.128881Z","shell.execute_reply.started":"2022-07-11T05:09:27.347646Z","shell.execute_reply":"2022-07-11T05:09:29.125348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet50","metadata":{"id":"npbHoFuCHNiF","papermill":{"duration":0.026525,"end_time":"2022-07-07T06:50:11.277695","exception":false,"start_time":"2022-07-07T06:50:11.25117","status":"completed"},"tags":[]}},{"cell_type":"code","source":"learn=cnn_learner(dls,models.resnet50,metrics=error_rate)","metadata":{"id":"dwTe4is1HP-j","outputId":"39c37218-cf7a-4c6a-cc20-b99390d7c101","papermill":{"duration":6.343446,"end_time":"2022-07-07T06:50:17.648076","exception":false,"start_time":"2022-07-07T06:50:11.30463","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:09:33.694977Z","iopub.execute_input":"2022-07-11T05:09:33.695721Z","iopub.status.idle":"2022-07-11T05:09:34.865490Z","shell.execute_reply.started":"2022-07-11T05:09:33.695636Z","shell.execute_reply":"2022-07-11T05:09:34.864063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(8,cbs=[SaveModelCallback(fname='best_model'),ShowGraphCallback()])\nprint(\"Pretraining Done.\")","metadata":{"id":"W-dWUxm6IYPh","outputId":"5fb52293-26af-4270-b82c-b9ebcdcdeee0","papermill":{"duration":1270.336142,"end_time":"2022-07-07T07:11:28.011702","exception":false,"start_time":"2022-07-07T06:50:17.67556","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:09:37.609785Z","iopub.execute_input":"2022-07-11T05:09:37.610382Z","iopub.status.idle":"2022-07-11T05:43:03.590578Z","shell.execute_reply.started":"2022-07-11T05:09:37.610334Z","shell.execute_reply":"2022-07-11T05:43:03.589021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save(\"stage-1-50\")","metadata":{"id":"0t2FdRqbIeeL","outputId":"daa103bf-650b-4994-9fee-2e4e77dc965f","papermill":{"duration":0.265073,"end_time":"2022-07-07T07:11:28.305066","exception":false,"start_time":"2022-07-07T07:11:28.039993","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:43:03.597125Z","iopub.execute_input":"2022-07-11T05:43:03.600972Z","iopub.status.idle":"2022-07-11T05:43:04.250549Z","shell.execute_reply.started":"2022-07-11T05:43:03.600910Z","shell.execute_reply":"2022-07-11T05:43:04.249034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\n# learn.lr_find()","metadata":{"id":"uHbBxgdY0Hoi","papermill":{"duration":24.223914,"end_time":"2022-07-07T07:11:52.681244","exception":false,"start_time":"2022-07-07T07:11:28.45733","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:49:04.575188Z","iopub.execute_input":"2022-07-11T05:49:04.576645Z","iopub.status.idle":"2022-07-11T05:49:04.587830Z","shell.execute_reply.started":"2022-07-11T05:49:04.576594Z","shell.execute_reply":"2022-07-11T05:49:04.586082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# learn=cnn_learner(dls,models.resnet50,metrics=error_rate)\n# learn.load(\"stage-1-50\")","metadata":{"papermill":{"duration":0.03487,"end_time":"2022-07-07T07:11:28.367686","exception":false,"start_time":"2022-07-07T07:11:28.332816","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:03:54.615065Z","iopub.execute_input":"2022-07-11T05:03:54.616602Z","iopub.status.idle":"2022-07-11T05:03:56.135206Z","shell.execute_reply.started":"2022-07-11T05:03:54.616553Z","shell.execute_reply":"2022-07-11T05:03:56.133874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Fine Tuning** \n\nreduce max lr with oldlr/10 and find lower bound of lr using learn.find_lr()","metadata":{"id":"V2a9J6gtlr4g","papermill":{"duration":0.027557,"end_time":"2022-07-07T07:11:52.801268","exception":false,"start_time":"2022-07-07T07:11:52.773711","status":"completed"},"tags":[]}},{"cell_type":"code","source":"learn.fit_one_cycle(8,lr_max=slice(1e-7,3e-4),cbs=SaveModelCallback(fname='best_model'))","metadata":{"id":"8lYhVZvYlr4h","outputId":"ba3a2c36-432f-4882-96ff-93cb1de35e6e","papermill":{"duration":674.965889,"end_time":"2022-07-07T07:23:07.794774","exception":false,"start_time":"2022-07-07T07:11:52.828885","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T05:49:07.704104Z","iopub.execute_input":"2022-07-11T05:49:07.704742Z","iopub.status.idle":"2022-07-11T06:23:40.657594Z","shell.execute_reply.started":"2022-07-11T05:49:07.704668Z","shell.execute_reply":"2022-07-11T06:23:40.656016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# learn.save(\"stage-2-50\")","metadata":{"id":"By0qkh-on1ns","papermill":{"duration":0.505868,"end_time":"2022-07-07T07:23:08.330853","exception":false,"start_time":"2022-07-07T07:23:07.824985","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-09T06:05:53.748998Z","iopub.execute_input":"2022-07-09T06:05:53.749621Z","iopub.status.idle":"2022-07-09T06:05:53.760125Z","shell.execute_reply.started":"2022-07-09T06:05:53.749573Z","shell.execute_reply":"2022-07-09T06:05:53.758789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Interpretation","metadata":{"id":"Pk9_ABzdRlBl","papermill":{"duration":0.029253,"end_time":"2022-07-07T07:23:08.390429","exception":false,"start_time":"2022-07-07T07:23:08.361176","status":"completed"},"tags":[]}},{"cell_type":"code","source":"interp=ClassificationInterpretation.from_learner(learn)","metadata":{"id":"dZCkm-6lHRO4","outputId":"41f5631e-6767-4f08-cb1e-7d1b3ad84397","papermill":{"duration":5.60045,"end_time":"2022-07-07T07:23:14.019938","exception":false,"start_time":"2022-07-07T07:23:08.419488","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:23:40.664870Z","iopub.execute_input":"2022-07-11T06:23:40.667893Z","iopub.status.idle":"2022-07-11T06:24:03.340179Z","shell.execute_reply.started":"2022-07-11T06:23:40.667843Z","shell.execute_reply":"2022-07-11T06:24:03.338723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(5,5))","metadata":{"id":"9Au7pW-wHqbt","outputId":"e8cb0777-1d85-49d5-f293-737b5c066d3a","papermill":{"duration":5.641627,"end_time":"2022-07-07T07:23:19.692545","exception":false,"start_time":"2022-07-07T07:23:14.050918","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:03.346952Z","iopub.execute_input":"2022-07-11T06:24:03.350852Z","iopub.status.idle":"2022-07-11T06:24:26.901269Z","shell.execute_reply.started":"2022-07-11T06:24:03.350798Z","shell.execute_reply":"2022-07-11T06:24:26.899751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(15,figsize=(15,11))","metadata":{"id":"kOWpu-0tHYih","outputId":"80076263-11d8-4dd2-d6f2-cedebb6a1d77","papermill":{"duration":1.285681,"end_time":"2022-07-07T07:23:21.038619","exception":false,"start_time":"2022-07-07T07:23:19.752938","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:26.909077Z","iopub.execute_input":"2022-07-11T06:24:26.912870Z","iopub.status.idle":"2022-07-11T06:24:29.207359Z","shell.execute_reply.started":"2022-07-11T06:24:26.912816Z","shell.execute_reply":"2022-07-11T06:24:29.205770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As we can see some bad examples lets clean the data**","metadata":{"papermill":{"duration":0.060508,"end_time":"2022-07-07T07:23:21.161486","exception":false,"start_time":"2022-07-07T07:23:21.100978","status":"completed"},"tags":[]}},{"cell_type":"code","source":"len(os.listdir('train'))","metadata":{"papermill":{"duration":0.097303,"end_time":"2022-07-07T07:23:21.334786","exception":false,"start_time":"2022-07-07T07:23:21.237483","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:29.209816Z","iopub.execute_input":"2022-07-11T06:24:29.210631Z","iopub.status.idle":"2022-07-11T06:24:29.256239Z","shell.execute_reply.started":"2022-07-11T06:24:29.210580Z","shell.execute_reply":"2022-07-11T06:24:29.254852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from fastai.vision.widgets import ImageClassifierCleaner\n# cleaner = ImageClassifierCleaner(learn)\n# cleaner","metadata":{"papermill":{"duration":140.519217,"end_time":"2022-07-07T07:25:41.904521","exception":false,"start_time":"2022-07-07T07:23:21.385304","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:29.258060Z","iopub.execute_input":"2022-07-11T06:24:29.258751Z","iopub.status.idle":"2022-07-11T06:24:29.265635Z","shell.execute_reply.started":"2022-07-11T06:24:29.258680Z","shell.execute_reply":"2022-07-11T06:24:29.262785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for idx in cleaner.delete():\n#     if cleaner.fns[idx] not in bad_imgs_list:\n#         bad_imgs_list.append(cleaner.fns[idx])\n#         print(cleaner.fns[idx],\"deleted.\")","metadata":{"papermill":{"duration":0.140491,"end_time":"2022-07-07T07:25:42.124877","exception":false,"start_time":"2022-07-07T07:25:41.984386","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:29.267405Z","iopub.execute_input":"2022-07-11T06:24:29.268161Z","iopub.status.idle":"2022-07-11T06:24:29.281565Z","shell.execute_reply.started":"2022-07-11T06:24:29.268118Z","shell.execute_reply":"2022-07-11T06:24:29.280199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bad_imgs_list","metadata":{"papermill":{"duration":0.052811,"end_time":"2022-07-07T07:25:42.218848","exception":false,"start_time":"2022-07-07T07:25:42.166037","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:29.283316Z","iopub.execute_input":"2022-07-11T06:24:29.283786Z","iopub.status.idle":"2022-07-11T06:24:29.294321Z","shell.execute_reply.started":"2022-07-11T06:24:29.283741Z","shell.execute_reply":"2022-07-11T06:24:29.292783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.041252,"end_time":"2022-07-07T07:25:42.301104","exception":false,"start_time":"2022-07-07T07:25:42.259852","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# get outputs","metadata":{"id":"ClKD6TJ0HJZa","papermill":{"duration":0.040877,"end_time":"2022-07-07T07:25:42.383665","exception":false,"start_time":"2022-07-07T07:25:42.342788","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test_dl = dls.test_dl(test_list)","metadata":{"id":"2ypoiW348WB-","papermill":{"duration":0.055498,"end_time":"2022-07-07T07:25:42.482064","exception":false,"start_time":"2022-07-07T07:25:42.426566","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:29.295580Z","iopub.execute_input":"2022-07-11T06:24:29.296190Z","iopub.status.idle":"2022-07-11T06:24:29.316805Z","shell.execute_reply.started":"2022-07-11T06:24:29.296130Z","shell.execute_reply":"2022-07-11T06:24:29.315422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dl.show_batch()","metadata":{"id":"St7kt-5o8oV3","outputId":"7c42a181-5e7c-4928-cf80-8337507a1bd4","papermill":{"duration":1.099952,"end_time":"2022-07-07T07:25:43.623332","exception":false,"start_time":"2022-07-07T07:25:42.52338","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:29.324108Z","iopub.execute_input":"2022-07-11T06:24:29.325164Z","iopub.status.idle":"2022-07-11T06:24:30.998322Z","shell.execute_reply.started":"2022-07-11T06:24:29.325113Z","shell.execute_reply":"2022-07-11T06:24:30.996971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=learn.get_preds(dl=test_dl)","metadata":{"id":"J-sNklQi8q1R","outputId":"2924cc71-fe05-436e-c508-13812c71dc79","papermill":{"duration":61.8302,"end_time":"2022-07-07T07:26:45.503758","exception":false,"start_time":"2022-07-07T07:25:43.673558","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:24:30.999687Z","iopub.execute_input":"2022-07-11T06:24:31.000118Z","iopub.status.idle":"2022-07-11T06:26:14.435938Z","shell.execute_reply.started":"2022-07-11T06:24:31.000080Z","shell.execute_reply":"2022-07-11T06:26:14.434365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids=list(map(lambda x: x.rsplit(\"/\",1)[1].rsplit(\".\",1)[0],test_list))","metadata":{"id":"LHGYlt5s89Td","papermill":{"duration":0.06505,"end_time":"2022-07-07T07:26:45.618049","exception":false,"start_time":"2022-07-07T07:26:45.552999","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.441762Z","iopub.execute_input":"2022-07-11T06:26:14.443117Z","iopub.status.idle":"2022-07-11T06:26:14.473012Z","shell.execute_reply.started":"2022-07-11T06:26:14.443072Z","shell.execute_reply":"2022-07-11T06:26:14.471656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dls.vocab)\npreds[0]","metadata":{"id":"WWhp0pd39XiG","outputId":"eb258c3f-380a-427a-d9b1-50ee013a6e64","papermill":{"duration":0.063892,"end_time":"2022-07-07T07:26:45.730625","exception":false,"start_time":"2022-07-07T07:26:45.666733","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.474645Z","iopub.execute_input":"2022-07-11T06:26:14.475405Z","iopub.status.idle":"2022-07-11T06:26:14.494101Z","shell.execute_reply.started":"2022-07-11T06:26:14.475361Z","shell.execute_reply":"2022-07-11T06:26:14.492779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[0][:,1]  # we are taking the dog probability","metadata":{"id":"wIgR3djY9az_","outputId":"b748a696-3a2e-426a-baaf-74b75f2e0028","papermill":{"duration":0.059238,"end_time":"2022-07-07T07:26:45.837741","exception":false,"start_time":"2022-07-07T07:26:45.778503","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.495946Z","iopub.execute_input":"2022-07-11T06:26:14.497282Z","iopub.status.idle":"2022-07-11T06:26:14.514543Z","shell.execute_reply.started":"2022-07-11T06:26:14.497224Z","shell.execute_reply":"2022-07-11T06:26:14.513025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\npreds=np.array(preds[0][:,1])","metadata":{"id":"h5cLFBAU9tPI","papermill":{"duration":0.056186,"end_time":"2022-07-07T07:26:45.941702","exception":false,"start_time":"2022-07-07T07:26:45.885516","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.516631Z","iopub.execute_input":"2022-07-11T06:26:14.518953Z","iopub.status.idle":"2022-07-11T06:26:14.535392Z","shell.execute_reply.started":"2022-07-11T06:26:14.518909Z","shell.execute_reply":"2022-07-11T06:26:14.533619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preds=np.clip(preds,0.05,0.95)\npreds","metadata":{"id":"k7nOSpQB-qD-","outputId":"108e409a-e334-480d-e588-77b5f18045ad","papermill":{"duration":0.059127,"end_time":"2022-07-07T07:26:46.048527","exception":false,"start_time":"2022-07-07T07:26:45.9894","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.539286Z","iopub.execute_input":"2022-07-11T06:26:14.543224Z","iopub.status.idle":"2022-07-11T06:26:14.556916Z","shell.execute_reply.started":"2022-07-11T06:26:14.543153Z","shell.execute_reply":"2022-07-11T06:26:14.555534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nif os.path.exists('submission.csv'): os.remove('submission.csv')\ndf=pd.DataFrame({\"id\":test_ids,\"label\":preds})\ndf.to_csv(\"submission.csv\",mode='a',index=False,header=True)","metadata":{"id":"Pq2htZlI-v03","papermill":{"duration":0.095183,"end_time":"2022-07-07T07:26:46.191358","exception":false,"start_time":"2022-07-07T07:26:46.096175","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.559099Z","iopub.execute_input":"2022-07-11T06:26:14.562766Z","iopub.status.idle":"2022-07-11T06:26:14.637399Z","shell.execute_reply.started":"2022-07-11T06:26:14.562702Z","shell.execute_reply":"2022-07-11T06:26:14.636005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.exists('clipping_0.05_submission.csv'): os.remove('clipping_0.05_submission.csv')\ndf=pd.DataFrame({\"id\":test_ids,\"label\":np.clip(preds,0.05,0.95)})\ndf.to_csv(\"clipping_0.05_submission.csv\",mode='a',index=False,header=True)","metadata":{"id":"pPrS0GsA_Qc1","outputId":"74ea5084-e8e2-4b91-f03e-4f1a6005dd20","papermill":{"duration":0.075876,"end_time":"2022-07-07T07:26:46.31538","exception":false,"start_time":"2022-07-07T07:26:46.239504","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.643043Z","iopub.execute_input":"2022-07-11T06:26:14.646531Z","iopub.status.idle":"2022-07-11T06:26:14.702095Z","shell.execute_reply.started":"2022-07-11T06:26:14.646485Z","shell.execute_reply":"2022-07-11T06:26:14.700744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### gives the best result\nif os.path.exists('clipping_0.0125_submission.csv'): os.remove('clipping_0.0125_submission.csv')\ndf=pd.DataFrame({\"id\":test_ids,\"label\":np.clip(preds,0.0125,0.9875)})\ndf.to_csv(\"clipping_0.0125_submission.csv\",mode='a',index=False,header=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:26:14.707518Z","iopub.execute_input":"2022-07-11T06:26:14.710956Z","iopub.status.idle":"2022-07-11T06:26:14.773381Z","shell.execute_reply.started":"2022-07-11T06:26:14.710888Z","shell.execute_reply":"2022-07-11T06:26:14.771917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"submission.csv\")","metadata":{"id":"x-m7GWiE_YK7","papermill":{"duration":0.048443,"end_time":"2022-07-07T07:26:46.412282","exception":false,"start_time":"2022-07-07T07:26:46.363839","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.779679Z","iopub.execute_input":"2022-07-11T06:26:14.783217Z","iopub.status.idle":"2022-07-11T06:26:14.822742Z","shell.execute_reply.started":"2022-07-11T06:26:14.783166Z","shell.execute_reply":"2022-07-11T06:26:14.821241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.read_csv(\"clipping_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T06:26:14.828439Z","iopub.execute_input":"2022-07-11T06:26:14.832122Z","iopub.status.idle":"2022-07-11T06:26:14.840455Z","shell.execute_reply.started":"2022-07-11T06:26:14.832064Z","shell.execute_reply":"2022-07-11T06:26:14.838777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_dl = dls.test_dl(bad_imgs_list)","metadata":{"papermill":{"duration":0.060531,"end_time":"2022-07-07T07:26:46.521695","exception":false,"start_time":"2022-07-07T07:26:46.461164","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.846327Z","iopub.execute_input":"2022-07-11T06:26:14.849123Z","iopub.status.idle":"2022-07-11T06:26:14.870234Z","shell.execute_reply.started":"2022-07-11T06:26:14.849073Z","shell.execute_reply":"2022-07-11T06:26:14.868382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_dl.show_batch(max_n=len(bad_imgs_list),ncols=5)","metadata":{"papermill":{"duration":3.223819,"end_time":"2022-07-07T07:26:49.793777","exception":false,"start_time":"2022-07-07T07:26:46.569958","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-11T06:26:14.876447Z","iopub.execute_input":"2022-07-11T06:26:14.880308Z","iopub.status.idle":"2022-07-11T06:26:22.720989Z","shell.execute_reply.started":"2022-07-11T06:26:14.880255Z","shell.execute_reply":"2022-07-11T06:26:22.719755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.133543,"end_time":"2022-07-07T07:26:50.070602","exception":false,"start_time":"2022-07-07T07:26:49.937059","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}