{"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":"<a href=\"https://www.kaggle.com/code/sonujha090/dogs-vs-cats-pytorch?scriptVersionId=114239526\" target=\"_blank\"><img align=\"left\" alt=\"Kaggle\" title=\"Open in Kaggle\" src=\"https://kaggle.com/static/images/open-in-kaggle.svg\"></a>","metadata":{}},{"cell_type":"code","source":"pip install timm -qqq","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:42:31.290155Z","iopub.execute_input":"2022-12-21T04:42:31.290718Z","iopub.status.idle":"2022-12-21T04:42:51.323057Z","shell.execute_reply.started":"2022-12-21T04:42:31.290605Z","shell.execute_reply":"2022-12-21T04:42:51.321628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nimport timm","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:42:51.325346Z","iopub.execute_input":"2022-12-21T04:42:51.327544Z","iopub.status.idle":"2022-12-21T04:42:56.569915Z","shell.execute_reply.started":"2022-12-21T04:42:51.327494Z","shell.execute_reply":"2022-12-21T04:42:56.568376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists('data'):\n    os.mkdir('data')","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:42:56.575314Z","iopub.execute_input":"2022-12-21T04:42:56.575970Z","iopub.status.idle":"2022-12-21T04:42:56.586474Z","shell.execute_reply.started":"2022-12-21T04:42:56.575918Z","shell.execute_reply":"2022-12-21T04:42:56.585300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n!unzip -q /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip -d data/","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:42:56.590473Z","iopub.execute_input":"2022-12-21T04:42:56.590992Z","iopub.status.idle":"2022-12-21T04:43:05.230397Z","shell.execute_reply.started":"2022-12-21T04:42:56.590950Z","shell.execute_reply":"2022-12-21T04:43:05.228992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n!unzip -q /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip -d data/","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:05.232593Z","iopub.execute_input":"2022-12-21T04:43:05.233044Z","iopub.status.idle":"2022-12-21T04:43:20.060850Z","shell.execute_reply.started":"2022-12-21T04:43:05.232993Z","shell.execute_reply":"2022-12-21T04:43:20.059378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"data\")\nPath.BASE_PATH = path\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:20.065424Z","iopub.execute_input":"2022-12-21T04:43:20.067103Z","iopub.status.idle":"2022-12-21T04:43:20.089750Z","shell.execute_reply.started":"2022-12-21T04:43:20.066922Z","shell.execute_reply":"2022-12-21T04:43:20.088569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(path/'train').ls()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:20.094571Z","iopub.execute_input":"2022-12-21T04:43:20.095331Z","iopub.status.idle":"2022-12-21T04:43:20.444400Z","shell.execute_reply.started":"2022-12-21T04:43:20.095276Z","shell.execute_reply":"2022-12-21T04:43:20.443253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_img = (path/'train').ls()[0]\none_img.name.split('.')[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:20.448760Z","iopub.execute_input":"2022-12-21T04:43:20.451554Z","iopub.status.idle":"2022-12-21T04:43:20.550678Z","shell.execute_reply.started":"2022-12-21T04:43:20.451511Z","shell.execute_reply":"2022-12-21T04:43:20.549681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def custom_splitter(train_name, valid_name, train_pct, valid_pct):\n    def fn(name_list):\n        train_idx, valid_idx = RandomSplitter(valid_pct=0.1, seed=42)(name_list)\n        np.random.shuffle(train_idx)\n        train_len = int(len(train_idx) * train_pct)\n        valid_len = int(len(valid_idx)* valid_pct)\n        return train_idx[0:train_len], valid_idx[0:valid_len]\n    return fn","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:20.555350Z","iopub.execute_input":"2022-12-21T04:43:20.558340Z","iopub.status.idle":"2022-12-21T04:43:20.567759Z","shell.execute_reply.started":"2022-12-21T04:43:20.558296Z","shell.execute_reply":"2022-12-21T04:43:20.566588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_dir = path/'train'\nlabel_fn = lambda x: x.name.split('.')[0]\nimages = get_image_files(train_img_dir)\n\ndblock = DataBlock(\n    blocks=(ImageBlock, CategoryBlock),\n    get_items=get_image_files,\n    splitter=custom_splitter(train_name='training', valid_name='validation', train_pct=1, valid_pct=1),\n    get_y=label_fn,\n    item_tfms=Resize(450),\n#     batch_tfms=aug_transforms(size=350, min_scale=0.75)\n)\n\n# taking a subset of the data for faster training\ndls = dblock.dataloaders(train_img_dir, bs=8)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:20.573504Z","iopub.execute_input":"2022-12-21T04:43:20.574369Z","iopub.status.idle":"2022-12-21T04:43:30.134635Z","shell.execute_reply.started":"2022-12-21T04:43:20.574325Z","shell.execute_reply":"2022-12-21T04:43:30.133429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training dataset: {len(dls.train_ds)} Validation dataset: {len(dls.valid_ds)}\")","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:30.140047Z","iopub.execute_input":"2022-12-21T04:43:30.144112Z","iopub.status.idle":"2022-12-21T04:43:30.155997Z","shell.execute_reply.started":"2022-12-21T04:43:30.144058Z","shell.execute_reply":"2022-12-21T04:43:30.154831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xb, yb = dls.one_batch()\nxb.shape, yb.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:30.159738Z","iopub.execute_input":"2022-12-21T04:43:30.163788Z","iopub.status.idle":"2022-12-21T04:43:30.409109Z","shell.execute_reply.started":"2022-12-21T04:43:30.163738Z","shell.execute_reply":"2022-12-21T04:43:30.407858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:30.414153Z","iopub.execute_input":"2022-12-21T04:43:30.417393Z","iopub.status.idle":"2022-12-21T04:43:31.874981Z","shell.execute_reply.started":"2022-12-21T04:43:30.417344Z","shell.execute_reply":"2022-12-21T04:43:31.869818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Testing Different Architecture","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(4)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T04:43:31.876653Z","iopub.execute_input":"2022-12-21T04:43:31.877304Z","iopub.status.idle":"2022-12-21T05:24:08.904011Z","shell.execute_reply.started":"2022-12-21T04:43:31.877258Z","shell.execute_reply":"2022-12-21T05:24:08.902785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# learn = vision_learner(dls, \"efficientnet_b0\", metrics=error_rate)\n# learn.fine_tune(2)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:24:08.906341Z","iopub.execute_input":"2022-12-21T05:24:08.906794Z","iopub.status.idle":"2022-12-21T05:24:08.913919Z","shell.execute_reply.started":"2022-12-21T05:24:08.906744Z","shell.execute_reply":"2022-12-21T05:24:08.912585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Interpretation","metadata":{}},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:24:08.915571Z","iopub.execute_input":"2022-12-21T05:24:08.916423Z","iopub.status.idle":"2022-12-21T05:25:04.175513Z","shell.execute_reply.started":"2022-12-21T05:24:08.916380Z","shell.execute_reply":"2022-12-21T05:25:04.174108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(10)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:25:04.177606Z","iopub.execute_input":"2022-12-21T05:25:04.178035Z","iopub.status.idle":"2022-12-21T05:25:06.143728Z","shell.execute_reply.started":"2022-12-21T05:25:04.177989Z","shell.execute_reply":"2022-12-21T05:25:06.141600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict\nimg = PILImage.create('data/test/1.jpg')\nimg.to_thumb(450,450)\nlearn.predict(img)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:25:16.003246Z","iopub.execute_input":"2022-12-21T05:25:16.003671Z","iopub.status.idle":"2022-12-21T05:25:16.086908Z","shell.execute_reply.started":"2022-12-21T05:25:16.003634Z","shell.execute_reply":"2022-12-21T05:25:16.085904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction on test dataset","metadata":{}},{"cell_type":"code","source":"# creating test dataloader\ntest_dl = learn.dls.test_dl(get_image_files(path/'test'))","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:25:17.494865Z","iopub.execute_input":"2022-12-21T05:25:17.495299Z","iopub.status.idle":"2022-12-21T05:25:17.595393Z","shell.execute_reply.started":"2022-12-21T05:25:17.495262Z","shell.execute_reply":"2022-12-21T05:25:17.594338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npreds, _ = learn.get_preds(dl=test_dl)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:29:17.457420Z","iopub.execute_input":"2022-12-21T05:29:17.457984Z","iopub.status.idle":"2022-12-21T05:32:10.161255Z","shell.execute_reply.started":"2022-12-21T05:29:17.457928Z","shell.execute_reply":"2022-12-21T05:32:10.159786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class 1 is the dog class and class 0 is the cat class\npreds = preds[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:32:10.167308Z","iopub.execute_input":"2022-12-21T05:32:10.170635Z","iopub.status.idle":"2022-12-21T05:32:10.179441Z","shell.execute_reply.started":"2022-12-21T05:32:10.170578Z","shell.execute_reply":"2022-12-21T05:32:10.178024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(preds)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:32:10.185341Z","iopub.execute_input":"2022-12-21T05:32:10.188871Z","iopub.status.idle":"2022-12-21T05:32:11.536277Z","shell.execute_reply.started":"2022-12-21T05:32:10.188825Z","shell.execute_reply":"2022-12-21T05:32:11.535004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating the submission dataframe\nsubmission_df = pd.DataFrame({'id': [int(x.stem) for x in test_dl.items], 'label': np.clip(preds, 0.005, 0.995)})\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:33:04.456528Z","iopub.execute_input":"2022-12-21T05:33:04.457061Z","iopub.status.idle":"2022-12-21T05:33:04.509337Z","shell.execute_reply.started":"2022-12-21T05:33:04.457011Z","shell.execute_reply":"2022-12-21T05:33:04.508136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving the submission dataframe\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-21T05:33:10.636287Z","iopub.execute_input":"2022-12-21T05:33:10.636782Z","iopub.status.idle":"2022-12-21T05:33:10.806511Z","shell.execute_reply.started":"2022-12-21T05:33:10.636728Z","shell.execute_reply":"2022-12-21T05:33:10.796193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}