{"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":"<div align=\"center\">\n<img src=\"https://user-images.githubusercontent.com/48846576/121096632-33afc800-c7b8-11eb-970a-0af7433de15a.png\" alt=\"fastai\" width=\"300\" height=\"200\"/> \n<p>In this notebook we will use fastai library to classify the <a href=\"http://https://www.kaggle.com/c/siim-covid19-detection\">SIIM-FISABIO-RSNA COVID-19 Detection</a> images into the following categories</p>\n<div align=\"left\">    \n<ul>\n    <li>Negative </li>\n    <li>Typical </li>\n    <li>Atypical </li>\n    <li>Indeterminate</li>\n    </ul>\n    </div>    \n</div>\n\nI'm using resized images from my other notebook https://www.kaggle.com/rajsengo/image-resize-siim-covid-19-detection using which I learnt the image resize techniques.\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport gc\n\nfrom colorama import Fore, Back, Style\n\ny_ = Fore.YELLOW\nr_ = Fore.RED\ng_ = Fore.GREEN\nb_ = Fore.BLUE\nm_ = Fore.MAGENTA\nc_ = Fore.CYAN\nres = Style.RESET_ALL\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nPATH = '/kaggle/input/siim-covid19-detection/'\nsubmission = pd.read_csv('/kaggle/input/siim-covid19-detection/sample_submission.csv', index_col=None)\nimage_df = pd.read_csv('/kaggle/input/siim-covid19-detection/train_image_level.csv', index_col=None)\nstudy_df = pd.read_csv('/kaggle/input/siim-covid19-detection/train_study_level.csv', index_col=None)\npd.set_option('display.max_columns', None)  \npd.set_option('display.max_colwidth', None)\nprint(f\"{y_}Train image level csv shape : {image_df.shape}{res}\\n{g_}Train study level csv shape : {study_df.shape}{res}\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-09T22:45:27.595660Z","iopub.execute_input":"2021-07-09T22:45:27.596090Z","iopub.status.idle":"2021-07-09T22:45:28.380560Z","shell.execute_reply.started":"2021-07-09T22:45:27.595988Z","shell.execute_reply":"2021-07-09T22:45:28.379691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nall_files = []\nfor dirname, _, filenames in os.walk('/kaggle/input/image-resize-siim-covid-19-detection/'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2021-07-09T22:45:30.526215Z","iopub.execute_input":"2021-07-09T22:45:30.526548Z","iopub.status.idle":"2021-07-09T22:45:30.540312Z","shell.execute_reply.started":"2021-07-09T22:45:30.526517Z","shell.execute_reply":"2021-07-09T22:45:30.539515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train\n!cd train\n!tar xzf /kaggle/input/image-resize-siim-covid-19-detection/train.tar.gz","metadata":{"execution":{"iopub.status.busy":"2021-07-09T22:45:31.012051Z","iopub.execute_input":"2021-07-09T22:45:31.012409Z","iopub.status.idle":"2021-07-09T22:45:40.841314Z","shell.execute_reply.started":"2021-07-09T22:45:31.012378Z","shell.execute_reply":"2021-07-09T22:45:40.840162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2021-07-09T22:45:40.843201Z","iopub.execute_input":"2021-07-09T22:45:40.843586Z","iopub.status.idle":"2021-07-09T22:45:41.472200Z","shell.execute_reply.started":"2021-07-09T22:45:40.843544Z","shell.execute_reply":"2021-07-09T22:45:41.471202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Dataset","metadata":{}},{"cell_type":"code","source":"from fastai.vision.data import ImageDataLoaders\nfrom fastai.vision import models\nfrom fastai.vision.all import cnn_learner\nfrom fastai.metrics import RocAuc, accuracy, error_rate\nfrom fastai.interpret import ClassificationInterpretation\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2021-07-09T22:45:41.474431Z","iopub.execute_input":"2021-07-09T22:45:41.474819Z","iopub.status.idle":"2021-07-09T22:45:43.279266Z","shell.execute_reply.started":"2021-07-09T22:45:41.474775Z","shell.execute_reply":"2021-07-09T22:45:43.278462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trn_path = Path('/kaggle/working/')\ndl = ImageDataLoaders.from_folder(trn_path, train='train', bs=8, valid_pct=.1, seed=42)\ndl.train.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T22:45:43.280680Z","iopub.execute_input":"2021-07-09T22:45:43.281013Z","iopub.status.idle":"2021-07-09T22:45:48.541931Z","shell.execute_reply.started":"2021-07-09T22:45:43.280978Z","shell.execute_reply":"2021-07-09T22:45:48.541082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dl.valid.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T22:45:48.543062Z","iopub.execute_input":"2021-07-09T22:45:48.543456Z","iopub.status.idle":"2021-07-09T22:45:49.307284Z","shell.execute_reply.started":"2021-07-09T22:45:48.543423Z","shell.execute_reply":"2021-07-09T22:45:49.306262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer Learning\n\nTransfer learning is popular approach in deep learning where a model trained on a large dataset is reused for another classification task. We typically load a pre-trained model as a starting point instead of creating a model from scratch and retrain the model to adjust the weights for the specific dataset that we have.\n\nThe fastai library includes several pretrained models from torchvision, like resnet18, resnet34, resnet50, resnet101, resnet152, alexnet, etc. More details [here](https://fastai1.fast.ai/vision.models.html)\n\n## ResNets\n\nResidual networks are deep convolutional neural networks introduced by Microsoft paper [\"Deep Residual Learning for Image Recognition\"](https://arxiv.org/pdf/1512.03385.pdf). These CNNs use shortcut connections to skip one or more layers. ResNet addresses vanishing gradient problem which is a problem with gradient-based learning methods and backpropaga- tion where the gradient becomes very small after certain epochs, effectively preventing the weights from adjusting its value. ResNet50 has 50 layers and each resnet block is three layers deep.ResNet models are trained on ImageNet dataset\n\nHere are the steps\n\n- Load pre-trained resnet50\n- Use 1 cycle policy and find learning rates. Detailed explanation is [here](https://iconof.com/1cycle-learning-rate-policy/) \n- Retrain with best learning rate\n- Validate and Interpret results","metadata":{}},{"cell_type":"code","source":"learn = cnn_learner(dl, models.resnet50, metrics=error_rate)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:26:53.044592Z","iopub.execute_input":"2021-07-09T23:26:53.044929Z","iopub.status.idle":"2021-07-09T23:26:53.799535Z","shell.execute_reply.started":"2021-07-09T23:26:53.044899Z","shell.execute_reply":"2021-07-09T23:26:53.798696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(2)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:26:56.214786Z","iopub.execute_input":"2021-07-09T23:26:56.215138Z","iopub.status.idle":"2021-07-09T23:32:01.334409Z","shell.execute_reply.started":"2021-07-09T23:26:56.215107Z","shell.execute_reply":"2021-07-09T23:32:01.333463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlr_min, lr_steep = learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T00:03:30.775496Z","iopub.execute_input":"2021-07-10T00:03:30.775818Z","iopub.status.idle":"2021-07-10T00:03:30.795587Z","shell.execute_reply.started":"2021-07-10T00:03:30.775788Z","shell.execute_reply":"2021-07-10T00:03:30.793976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_min","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:55:32.057986Z","iopub.execute_input":"2021-07-09T23:55:32.058337Z","iopub.status.idle":"2021-07-09T23:55:32.064902Z","shell.execute_reply.started":"2021-07-09T23:55:32.058301Z","shell.execute_reply":"2021-07-09T23:55:32.064094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(20, lr_min)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:57:40.244560Z","iopub.execute_input":"2021-07-09T23:57:40.244904Z","iopub.status.idle":"2021-07-09T23:57:43.368162Z","shell.execute_reply.started":"2021-07-09T23:57:40.244868Z","shell.execute_reply":"2021-07-09T23:57:43.364726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:42:23.243865Z","iopub.status.idle":"2021-07-09T23:42:23.244431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:42:23.245699Z","iopub.status.idle":"2021-07-09T23:42:23.246435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(12, figsize=(15,11))","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:42:23.247649Z","iopub.status.idle":"2021-07-09T23:42:23.248351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(8,8), cmap='viridis_r', dpi=75)","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:42:23.249511Z","iopub.status.idle":"2021-07-09T23:42:23.250213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.most_confused()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:42:23.251328Z","iopub.status.idle":"2021-07-09T23:42:23.252018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.print_classification_report()","metadata":{"execution":{"iopub.status.busy":"2021-07-09T23:42:23.253260Z","iopub.status.idle":"2021-07-09T23:42:23.253849Z"},"trusted":true},"execution_count":null,"outputs":[]}]}