{"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":"code","source":"# Ignore  the warnings\nimport warnings\nwarnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\n# data visualisation and manipulation\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import style\nimport seaborn as sns\nimport timm\n\n#configure\n# sets matplotlib to inline and displays graphs below the corressponding cell.\n%matplotlib inline  \nstyle.use('fivethirtyeight')\nsns.set(style='whitegrid', color_codes=True)\n\nfrom sklearn.metrics import confusion_matrix\n\n# specifically for manipulating zipped images and getting numpy arrays of pixel values of images.\nimport cv2                  \nimport numpy as np  \nfrom tqdm import tqdm, tqdm_notebook\nimport os, random\nfrom random import shuffle  \nfrom zipfile import ZipFile\nfrom PIL import Image\nfrom sklearn.utils import shuffle\nimport torch\n\n!ls ../input/*","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-12T07:11:38.656463Z","iopub.execute_input":"2023-03-12T07:11:38.657204Z","iopub.status.idle":"2023-03-12T07:11:44.214788Z","shell.execute_reply.started":"2023-03-12T07:11:38.657174Z","shell.execute_reply":"2023-03-12T07:11:44.213544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fastai\nfrom fastai import *\nfrom fastai.data import *\nfrom fastai.vision import *\nfrom fastai.vision.all import *\n\nfrom fastai.vision.learner import *\n\nfastai.__version__","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:44.217637Z","iopub.execute_input":"2023-03-12T07:11:44.218033Z","iopub.status.idle":"2023-03-12T07:11:44.467277Z","shell.execute_reply.started":"2023-03-12T07:11:44.217994Z","shell.execute_reply":"2023-03-12T07:11:44.466302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    \nseed_everything(42)","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:44.470350Z","iopub.execute_input":"2023-03-12T07:11:44.470745Z","iopub.status.idle":"2023-03-12T07:11:44.480731Z","shell.execute_reply.started":"2023-03-12T07:11:44.470716Z","shell.execute_reply":"2023-03-12T07:11:44.479716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nPATH = Path('../input/aptos2019-blindness-detection')\n\ndf_train = pd.read_csv(PATH/'train.csv')\ndf_test = pd.read_csv(PATH/'test.csv')\n\n# if is_interactive():\n#     df_train = df_train.sample(800)\n\n_ = df_train.hist()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:44.484389Z","iopub.execute_input":"2023-03-12T07:11:44.484899Z","iopub.status.idle":"2023-03-12T07:11:44.853591Z","shell.execute_reply.started":"2023-03-12T07:11:44.484871Z","shell.execute_reply":"2023-03-12T07:11:44.852565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aptos19_stats = ([0.42, 0.22, 0.075], [0.27, 0.15, 0.081])\ndf_train['id_code'] = df_train['id_code'].apply(lambda x: x + '.png')\ndls = ImageDataLoaders.from_df(df_train, \n                               path=PATH, folder='train_images',\n                               valid_pct=0.1, seed=42,\n                               item_tfms=Resize(224, method='pad', pad_mode='zeros'),\n                               batch_tfms=[*aug_transforms(flip_vert=True, max_warp=0.1, max_zoom=1.15, max_rotate=45.), Normalize.from_stats(*aptos19_stats)],\n                               bs=32, \n                               num_workers=os.cpu_count())\n","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:44.855188Z","iopub.execute_input":"2023-03-12T07:11:44.855857Z","iopub.status.idle":"2023-03-12T07:11:51.651299Z","shell.execute_reply.started":"2023-03-12T07:11:44.855817Z","shell.execute_reply":"2023-03-12T07:11:51.650254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:51.653929Z","iopub.execute_input":"2023-03-12T07:11:51.654222Z","iopub.status.idle":"2023-03-12T07:11:58.703129Z","shell.execute_reply.started":"2023-03-12T07:11:51.654194Z","shell.execute_reply":"2023-03-12T07:11:58.702169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class FocalLoss(nn.Module):\n    def __init__(self, gamma=3., reduction='mean'):\n        super().__init__()\n        self.gamma = gamma\n        self.reduction = reduction\n\n    def forward(self, inputs, targets):\n        CE_loss = nn.CrossEntropyLoss(reduction='none')(inputs, targets)\n        pt = torch.exp(-CE_loss)\n        F_loss = ((1 - pt)**self.gamma) * CE_loss\n        if self.reduction == 'sum':\n            return F_loss.sum()\n        elif self.reduction == 'mean':\n            return F_loss.mean()","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:58.704286Z","iopub.execute_input":"2023-03-12T07:11:58.706831Z","iopub.status.idle":"2023-03-12T07:11:58.714329Z","shell.execute_reply.started":"2023-03-12T07:11:58.706785Z","shell.execute_reply":"2023-03-12T07:11:58.713429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run_models(model_type,out_cnt,loss,metric,train_time=10):\n    learn = cnn_learner(dls,\n                      model_type,\n                      pretrained=True,\n                      loss_func=loss, # focalloss(),\n                      metrics=metric, # [accuracy,RocAuc()],\n                      n_out=out_cnt\n                      )\n#     learn.model[-1][-1] = nn.Linear(in_features=learn.model[-1][-1].in_features, out_features=out_cnt, bias=True)\n    print(\"total epoch \",train_time)\n    learn.fine_tune(train_time)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:58.716229Z","iopub.execute_input":"2023-03-12T07:11:58.717257Z","iopub.status.idle":"2023-03-12T07:11:58.725991Z","shell.execute_reply.started":"2023-03-12T07:11:58.717210Z","shell.execute_reply":"2023-03-12T07:11:58.724821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pcpvt_learn=run_models('twins_pcpvt_base',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-11T21:19:44.388517Z","iopub.execute_input":"2023-03-11T21:19:44.389825Z","iopub.status.idle":"2023-03-11T22:51:41.112830Z","shell.execute_reply.started":"2023-03-11T21:19:44.389796Z","shell.execute_reply":"2023-03-11T22:51:41.111475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_models('efficientnet_b0',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-11T22:51:41.114806Z","iopub.execute_input":"2023-03-11T22:51:41.115547Z","iopub.status.idle":"2023-03-12T00:15:04.897587Z","shell.execute_reply.started":"2023-03-11T22:51:41.115504Z","shell.execute_reply":"2023-03-12T00:15:04.896465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_models('darknet53',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-12T00:15:04.899720Z","iopub.execute_input":"2023-03-12T00:15:04.900131Z","iopub.status.idle":"2023-03-12T01:31:29.298502Z","shell.execute_reply.started":"2023-03-12T00:15:04.900067Z","shell.execute_reply":"2023-03-12T01:31:29.297140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_models('inception_v3',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-12T01:31:29.300680Z","iopub.execute_input":"2023-03-12T01:31:29.301113Z","iopub.status.idle":"2023-03-12T02:45:05.305731Z","shell.execute_reply.started":"2023-03-12T01:31:29.301054Z","shell.execute_reply":"2023-03-12T02:45:05.304650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_models('inception_v4',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-12T02:45:05.307673Z","iopub.execute_input":"2023-03-12T02:45:05.307971Z","iopub.status.idle":"2023-03-12T04:09:32.801183Z","shell.execute_reply.started":"2023-03-12T02:45:05.307942Z","shell.execute_reply":"2023-03-12T04:09:32.800055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_models('mobilenetv2_050',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-12T04:09:32.805500Z","iopub.execute_input":"2023-03-12T04:09:32.805814Z","iopub.status.idle":"2023-03-12T05:31:38.659458Z","shell.execute_reply.started":"2023-03-12T04:09:32.805783Z","shell.execute_reply":"2023-03-12T05:31:38.658184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_models('densenet121',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-12T07:11:58.729436Z","iopub.execute_input":"2023-03-12T07:11:58.730222Z","iopub.status.idle":"2023-03-12T08:35:47.636865Z","shell.execute_reply.started":"2023-03-12T07:11:58.730183Z","shell.execute_reply":"2023-03-12T08:35:47.635739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_models('resnet50',5,FocalLoss(),[accuracy,RocAuc()])","metadata":{"execution":{"iopub.status.busy":"2023-03-12T08:35:47.639125Z","iopub.execute_input":"2023-03-12T08:35:47.639537Z","iopub.status.idle":"2023-03-12T09:48:25.598804Z","shell.execute_reply.started":"2023-03-12T08:35:47.639476Z","shell.execute_reply":"2023-03-12T09:48:25.597410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}