{"metadata":{"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"},{"sourceId":1236170,"sourceType":"datasetVersion","datasetId":708434}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"papermill":{"default_parameters":{},"duration":1645.104921,"end_time":"2024-01-25T17:27:54.658015","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-01-25T17:00:29.553094","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Libraries we'll use","metadata":{"papermill":{"duration":0.013179,"end_time":"2024-01-25T17:00:32.860771","exception":false,"start_time":"2024-01-25T17:00:32.847592","status":"completed"},"tags":[]}},{"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# for 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":{"execution":{"iopub.status.busy":"2024-10-28T13:43:34.009521Z","iopub.execute_input":"2024-10-28T13:43:34.009909Z","iopub.status.idle":"2024-10-28T13:43:34.015503Z","shell.execute_reply.started":"2024-10-28T13:43:34.009849Z","shell.execute_reply":"2024-10-28T13:43:34.014570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom albumentations import Compose, Flip, CropAndPad, Transpose\nimport os","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":2.627443,"end_time":"2024-01-25T17:00:35.502193","exception":false,"start_time":"2024-01-25T17:00:32.874750","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:34.017344Z","iopub.execute_input":"2024-10-28T13:43:34.017729Z","iopub.status.idle":"2024-10-28T13:43:35.932642Z","shell.execute_reply.started":"2024-10-28T13:43:34.017693Z","shell.execute_reply":"2024-10-28T13:43:35.931654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/melanoma224/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:43:35.933913Z","iopub.execute_input":"2024-10-28T13:43:35.934419Z","iopub.status.idle":"2024-10-28T13:43:36.045751Z","shell.execute_reply.started":"2024-10-28T13:43:35.934382Z","shell.execute_reply":"2024-10-28T13:43:36.044798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')\n# train.head()","metadata":{"papermill":{"duration":0.130928,"end_time":"2024-01-25T17:00:35.646793","exception":false,"start_time":"2024-01-25T17:00:35.515865","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:36.047935Z","iopub.execute_input":"2024-10-28T13:43:36.048279Z","iopub.status.idle":"2024-10-28T13:43:36.052630Z","shell.execute_reply.started":"2024-10-28T13:43:36.048242Z","shell.execute_reply":"2024-10-28T13:43:36.051597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Umbalanced dataset","metadata":{"papermill":{"duration":0.013875,"end_time":"2024-01-25T17:00:35.674146","exception":false,"start_time":"2024-01-25T17:00:35.660271","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(train['target'].value_counts())\ntrain['target'].hist()\nplt.show()","metadata":{"papermill":{"duration":0.287447,"end_time":"2024-01-25T17:00:35.974740","exception":false,"start_time":"2024-01-25T17:00:35.687293","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:36.053961Z","iopub.execute_input":"2024-10-28T13:43:36.054287Z","iopub.status.idle":"2024-10-28T13:43:36.364927Z","shell.execute_reply.started":"2024-10-28T13:43:36.054244Z","shell.execute_reply":"2024-10-28T13:43:36.363875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"mode:{train['age_approx'].mode()}\")\ntrain['age_approx'].hist()\nplt.show()","metadata":{"papermill":{"duration":0.234796,"end_time":"2024-01-25T17:00:36.223061","exception":false,"start_time":"2024-01-25T17:00:35.988265","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:36.366078Z","iopub.execute_input":"2024-10-28T13:43:36.366380Z","iopub.status.idle":"2024-10-28T13:43:36.542756Z","shell.execute_reply.started":"2024-10-28T13:43:36.366348Z","shell.execute_reply":"2024-10-28T13:43:36.541762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['age_approx'].value_counts()","metadata":{"papermill":{"duration":0.025113,"end_time":"2024-01-25T17:00:36.262400","exception":false,"start_time":"2024-01-25T17:00:36.237287","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:36.544305Z","iopub.execute_input":"2024-10-28T13:43:36.544972Z","iopub.status.idle":"2024-10-28T13:43:36.555774Z","shell.execute_reply.started":"2024-10-28T13:43:36.544923Z","shell.execute_reply":"2024-10-28T13:43:36.554743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['age_approx'].hist(bins=len(train['age_approx'].unique()))\nplt.show()","metadata":{"papermill":{"duration":0.245126,"end_time":"2024-01-25T17:00:36.521509","exception":false,"start_time":"2024-01-25T17:00:36.276383","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:36.557519Z","iopub.execute_input":"2024-10-28T13:43:36.558008Z","iopub.status.idle":"2024-10-28T13:43:36.745081Z","shell.execute_reply.started":"2024-10-28T13:43:36.557913Z","shell.execute_reply":"2024-10-28T13:43:36.744062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = os.listdir('/kaggle/input/melanoma224/jpeg224/train')\nimgs[:5]","metadata":{"papermill":{"duration":0.546377,"end_time":"2024-01-25T17:00:37.083091","exception":false,"start_time":"2024-01-25T17:00:36.536714","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:36.749116Z","iopub.execute_input":"2024-10-28T13:43:36.749841Z","iopub.status.idle":"2024-10-28T13:43:37.038462Z","shell.execute_reply.started":"2024-10-28T13:43:36.749806Z","shell.execute_reply":"2024-10-28T13:43:37.037513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# imgs = os.listdir('/kaggle/input/siim-isic-melanoma-classification/jpeg/train')\n# imgs[:5]","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:43:37.039510Z","iopub.execute_input":"2024-10-28T13:43:37.039799Z","iopub.status.idle":"2024-10-28T13:43:37.044834Z","shell.execute_reply.started":"2024-10-28T13:43:37.039768Z","shell.execute_reply":"2024-10-28T13:43:37.043912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One hot encoding for metadata","metadata":{"papermill":{"duration":0.014299,"end_time":"2024-01-25T17:00:37.112032","exception":false,"start_time":"2024-01-25T17:00:37.097733","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train.info()","metadata":{"papermill":{"duration":0.050694,"end_time":"2024-01-25T17:00:37.228383","exception":false,"start_time":"2024-01-25T17:00:37.177689","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.046059Z","iopub.execute_input":"2024-10-28T13:43:37.046426Z","iopub.status.idle":"2024-10-28T13:43:37.086324Z","shell.execute_reply.started":"2024-10-28T13:43:37.046374Z","shell.execute_reply":"2024-10-28T13:43:37.085380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isna().sum()","metadata":{"papermill":{"duration":0.049188,"end_time":"2024-01-25T17:00:37.292097","exception":false,"start_time":"2024-01-25T17:00:37.242909","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.087927Z","iopub.execute_input":"2024-10-28T13:43:37.088598Z","iopub.status.idle":"2024-10-28T13:43:37.116646Z","shell.execute_reply.started":"2024-10-28T13:43:37.088547Z","shell.execute_reply":"2024-10-28T13:43:37.115575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder\n\ncols = [\"sex\", \"age_approx\", \"anatom_site_general_challenge\"]\nselected_cols = train[cols]\noh_encoder = OneHotEncoder()\noh_encoded = oh_encoder.fit_transform(selected_cols)\ncategories = oh_encoder.categories_","metadata":{"papermill":{"duration":0.053791,"end_time":"2024-01-25T17:00:37.366126","exception":false,"start_time":"2024-01-25T17:00:37.312335","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.117826Z","iopub.execute_input":"2024-10-28T13:43:37.118178Z","iopub.status.idle":"2024-10-28T13:43:37.607805Z","shell.execute_reply.started":"2024-10-28T13:43:37.118142Z","shell.execute_reply":"2024-10-28T13:43:37.606951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oh_encoder.transform([['female', 10.0, 'torso']]).toarray()","metadata":{"papermill":{"duration":0.029113,"end_time":"2024-01-25T17:00:37.409913","exception":false,"start_time":"2024-01-25T17:00:37.380800","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.609088Z","iopub.execute_input":"2024-10-28T13:43:37.609617Z","iopub.status.idle":"2024-10-28T13:43:37.623076Z","shell.execute_reply.started":"2024-10-28T13:43:37.609578Z","shell.execute_reply":"2024-10-28T13:43:37.621907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_columns = oh_encoder.get_feature_names_out(cols)","metadata":{"papermill":{"duration":0.021593,"end_time":"2024-01-25T17:00:37.446340","exception":false,"start_time":"2024-01-25T17:00:37.424747","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.624612Z","iopub.execute_input":"2024-10-28T13:43:37.625062Z","iopub.status.idle":"2024-10-28T13:43:37.629520Z","shell.execute_reply.started":"2024-10-28T13:43:37.625014Z","shell.execute_reply":"2024-10-28T13:43:37.628534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_columns","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:43:37.631026Z","iopub.execute_input":"2024-10-28T13:43:37.631319Z","iopub.status.idle":"2024-10-28T13:43:37.641169Z","shell.execute_reply.started":"2024-10-28T13:43:37.631287Z","shell.execute_reply":"2024-10-28T13:43:37.640194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean = pd.DataFrame(oh_encoded.toarray(), columns=new_columns)","metadata":{"papermill":{"duration":0.03602,"end_time":"2024-01-25T17:00:37.499137","exception":false,"start_time":"2024-01-25T17:00:37.463117","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.642292Z","iopub.execute_input":"2024-10-28T13:43:37.642611Z","iopub.status.idle":"2024-10-28T13:43:37.661523Z","shell.execute_reply.started":"2024-10-28T13:43:37.642566Z","shell.execute_reply":"2024-10-28T13:43:37.660734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_clean.isna().sum()","metadata":{"papermill":{"duration":0.030977,"end_time":"2024-01-25T17:00:37.547754","exception":false,"start_time":"2024-01-25T17:00:37.516777","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.662588Z","iopub.execute_input":"2024-10-28T13:43:37.662887Z","iopub.status.idle":"2024-10-28T13:43:37.672373Z","shell.execute_reply.started":"2024-10-28T13:43:37.662856Z","shell.execute_reply":"2024-10-28T13:43:37.671429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train set and validation set","metadata":{"papermill":{"duration":0.017319,"end_time":"2024-01-25T17:00:37.582616","exception":false,"start_time":"2024-01-25T17:00:37.565297","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n_, val = train_test_split(train, test_size=0.33, shuffle=True, random_state=42, stratify=train['target'])\n\nval.shape","metadata":{"papermill":{"duration":0.054333,"end_time":"2024-01-25T17:00:37.654560","exception":false,"start_time":"2024-01-25T17:00:37.600227","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.673647Z","iopub.execute_input":"2024-10-28T13:43:37.674009Z","iopub.status.idle":"2024-10-28T13:43:37.789329Z","shell.execute_reply.started":"2024-10-28T13:43:37.673972Z","shell.execute_reply":"2024-10-28T13:43:37.788378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'].hist()\nplt.show()","metadata":{"papermill":{"duration":0.255884,"end_time":"2024-01-25T17:00:37.928477","exception":false,"start_time":"2024-01-25T17:00:37.672593","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.790653Z","iopub.execute_input":"2024-10-28T13:43:37.791011Z","iopub.status.idle":"2024-10-28T13:43:37.988160Z","shell.execute_reply.started":"2024-10-28T13:43:37.790975Z","shell.execute_reply":"2024-10-28T13:43:37.987205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val['target'].hist()\nplt.show()","metadata":{"papermill":{"duration":0.24867,"end_time":"2024-01-25T17:00:38.193078","exception":false,"start_time":"2024-01-25T17:00:37.944408","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:37.989335Z","iopub.execute_input":"2024-10-28T13:43:37.989615Z","iopub.status.idle":"2024-10-28T13:43:38.188886Z","shell.execute_reply.started":"2024-10-28T13:43:37.989585Z","shell.execute_reply":"2024-10-28T13:43:38.187907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PyTorch baseline","metadata":{"papermill":{"duration":0.015415,"end_time":"2024-01-25T17:00:38.224366","exception":false,"start_time":"2024-01-25T17:00:38.208951","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom skimage import io\nfrom fastprogress import master_bar, progress_bar\n\ntorch.__version__","metadata":{"papermill":{"duration":3.796423,"end_time":"2024-01-25T17:00:42.036327","exception":false,"start_time":"2024-01-25T17:00:38.239904","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:38.190084Z","iopub.execute_input":"2024-10-28T13:43:38.190403Z","iopub.status.idle":"2024-10-28T13:43:43.346856Z","shell.execute_reply.started":"2024-10-28T13:43:38.190371Z","shell.execute_reply":"2024-10-28T13:43:43.345950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val.head()","metadata":{"papermill":{"duration":0.03097,"end_time":"2024-01-25T17:00:42.083467","exception":false,"start_time":"2024-01-25T17:00:42.052497","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:43.347947Z","iopub.execute_input":"2024-10-28T13:43:43.348424Z","iopub.status.idle":"2024-10-28T13:43:43.364109Z","shell.execute_reply.started":"2024-10-28T13:43:43.348388Z","shell.execute_reply":"2024-10-28T13:43:43.363062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"papermill":{"duration":0.030221,"end_time":"2024-01-25T17:00:42.129627","exception":false,"start_time":"2024-01-25T17:00:42.099406","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:43.370701Z","iopub.execute_input":"2024-10-28T13:43:43.371201Z","iopub.status.idle":"2024-10-28T13:43:43.393270Z","shell.execute_reply.started":"2024-10-28T13:43:43.371132Z","shell.execute_reply":"2024-10-28T13:43:43.392273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['path'] = [f'/kaggle/input/melanoma224/jpeg224/train/{img}.jpg' for img in train['image_name']]\nval['path'] = [f'/kaggle/input/melanoma224/jpeg224/train/{img}.jpg' for img in val['image_name']]","metadata":{"papermill":{"duration":0.039448,"end_time":"2024-01-25T17:00:42.185268","exception":false,"start_time":"2024-01-25T17:00:42.145820","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:43.394637Z","iopub.execute_input":"2024-10-28T13:43:43.395021Z","iopub.status.idle":"2024-10-28T13:43:43.422449Z","shell.execute_reply.started":"2024-10-28T13:43:43.394968Z","shell.execute_reply":"2024-10-28T13:43:43.421563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train['path'] = [f'/kaggle/input/siim-isic-melanoma-classification/jpeg/train/{img}.jpg' for img in train['image_name']]\n# val['path'] = [f'/kaggle/input/siim-isic-melanoma-classification/jpeg/train/{img}.jpg' for img in val['image_name']]","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:43:43.423564Z","iopub.execute_input":"2024-10-28T13:43:43.423922Z","iopub.status.idle":"2024-10-28T13:43:43.432491Z","shell.execute_reply.started":"2024-10-28T13:43:43.423866Z","shell.execute_reply":"2024-10-28T13:43:43.431655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from albumentations import Resize\n","metadata":{"execution":{"iopub.status.busy":"2024-10-28T13:43:43.433542Z","iopub.execute_input":"2024-10-28T13:43:43.433884Z","iopub.status.idle":"2024-10-28T13:43:43.442607Z","shell.execute_reply.started":"2024-10-28T13:43:43.433851Z","shell.execute_reply":"2024-10-28T13:43:43.441789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pytorch dataset","metadata":{"papermill":{"duration":0.015773,"end_time":"2024-01-25T17:00:42.217050","exception":false,"start_time":"2024-01-25T17:00:42.201277","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class Dataset(torch.utils.data.Dataset):\n    def __init__(self, images, meta, labels=None, train=True, cache=False, trans=None):\n        self.cache = cache\n        self.images = [self.load_img(img) for img in images] if cache else images\n        meta = oh_encoder.transform(meta).toarray()\n        self.meta = torch.tensor(meta).float()\n        self.train = train\n        self.trans = trans\n        if train: self.labels = [torch.tensor([label]).float() for label in labels]\n    \n    def __len__(self):\n        return len(self.images)\n    \n    def load_img(self, img):\n        return io.imread(img)\n    \n    def __getitem__(self, ix):    \n        img = self.images[ix] if self.cache else self.load_img(self.images[ix])\n        if self.trans:\n            img = self.trans(image=img)['image']\n        if self.train:\n            return torch.from_numpy(img), self.meta[ix], self.labels[ix]\n        return torch.from_numpy(img), self.meta[ix]\n\ntrans = Compose([\n    Flip(p=0.6), Transpose(), CropAndPad(p=0.2, percent=-0.1),\n])","metadata":{"papermill":{"duration":0.028009,"end_time":"2024-01-25T17:00:42.261238","exception":false,"start_time":"2024-01-25T17:00:42.233229","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:43.444102Z","iopub.execute_input":"2024-10-28T13:43:43.444482Z","iopub.status.idle":"2024-10-28T13:43:43.457457Z","shell.execute_reply.started":"2024-10-28T13:43:43.444439Z","shell.execute_reply":"2024-10-28T13:43:43.456446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = {\n    'train': Dataset(train['path'], train[cols], train['target'], cache=True, trans=trans),\n    'val': Dataset(val['path'], val[cols], val['target'], cache=True, trans=trans),\n}","metadata":{"papermill":{"duration":286.053897,"end_time":"2024-01-25T17:05:28.331126","exception":false,"start_time":"2024-01-25T17:00:42.277229","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:43:43.458817Z","iopub.execute_input":"2024-10-28T13:43:43.459243Z","iopub.status.idle":"2024-10-28T13:47:01.599969Z","shell.execute_reply.started":"2024-10-28T13:43:43.459197Z","shell.execute_reply":"2024-10-28T13:47:01.598911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\nr, c = 3, 5\nfig = plt.figure(figsize=(2*c, 2*r))\nfor _r in range(r):\n    for _c in range(c):\n        plt.subplot(r, c, _r*c + _c + 1)\n        ix = random.randint(0, len(dataset['train']) - 1)\n        img, meta, label = dataset['train'][ix]\n        plt.imshow(img)\n        plt.title(label)\n        plt.axis('off')\nplt.show()","metadata":{"papermill":{"duration":1.180054,"end_time":"2024-01-25T17:05:29.560086","exception":false,"start_time":"2024-01-25T17:05:28.380032","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:01.601318Z","iopub.execute_input":"2024-10-28T13:47:01.601622Z","iopub.status.idle":"2024-10-28T13:47:02.901965Z","shell.execute_reply.started":"2024-10-28T13:47:01.601590Z","shell.execute_reply":"2024-10-28T13:47:02.900883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sample of the images","metadata":{"papermill":{"duration":0.015871,"end_time":"2024-01-25T17:05:28.363950","exception":false,"start_time":"2024-01-25T17:05:28.348079","status":"completed"},"tags":[]}},{"cell_type":"code","source":"meta","metadata":{"papermill":{"duration":0.038769,"end_time":"2024-01-25T17:05:29.620006","exception":false,"start_time":"2024-01-25T17:05:29.581237","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:02.903152Z","iopub.execute_input":"2024-10-28T13:47:02.903465Z","iopub.status.idle":"2024-10-28T13:47:02.922329Z","shell.execute_reply.started":"2024-10-28T13:47:02.903430Z","shell.execute_reply":"2024-10-28T13:47:02.921308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building the model\n\npara trabajar con cnns en pytorch, debemos tener los canales como (batch_size, canales, alto, ancho).\n\npor eso hicimos la transformacion de (b, h, w, c) -> (b, c, h, w)\n","metadata":{"papermill":{"duration":0.020575,"end_time":"2024-01-25T17:05:29.661361","exception":false,"start_time":"2024-01-25T17:05:29.640786","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class NN(torch.nn.Module):\n    def __init__(self, inputs=29):\n        super().__init__()\n        resnet = torchvision.models.resnet101(weights=True)\n        for param in resnet.parameters():\n            param.requires_grad = False # no aplicar los gradientes\n        self.encoder = torch.nn.Sequential(*list(resnet.children())[:-1]) # conecta capas secuencialmente\n        \n        self.meta_encoder = torch.nn.Sequential(\n            torch.nn.Linear(inputs, 100),\n            torch.nn.ReLU(inplace=True),\n            torch.nn.Linear(100, 3),\n            torch.nn.ReLU(inplace=True))\n        \n        self.head = torch.nn.Linear(2048+3, 1)\n        \n    def forward(self, x, y):\n        B, H, W, C = x.shape\n        x = (x / 255.).float().view(B, C, H, W)\n        x = self.encoder(x)\n        x = x.view(B, -1) # reshape\n        y = self.meta_encoder(y)\n        z = torch.cat([x, y], -1)\n        z = self.head(z)\n        \n        return z","metadata":{"papermill":{"duration":0.032018,"end_time":"2024-01-25T17:05:29.714310","exception":false,"start_time":"2024-01-25T17:05:29.682292","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:02.923538Z","iopub.execute_input":"2024-10-28T13:47:02.923827Z","iopub.status.idle":"2024-10-28T13:47:02.933946Z","shell.execute_reply.started":"2024-10-28T13:47:02.923798Z","shell.execute_reply":"2024-10-28T13:47:02.933122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Instantiate the model","metadata":{"papermill":{"duration":0.020564,"end_time":"2024-01-25T17:05:29.755794","exception":false,"start_time":"2024-01-25T17:05:29.735230","status":"completed"},"tags":[]}},{"cell_type":"code","source":"BATCH_SIZE = 64\n\nmodel = NN()\n\noutput = model(torch.randn(BATCH_SIZE, 224, 224, 3), torch.randn(BATCH_SIZE, 29))\noutput.shape","metadata":{"papermill":{"duration":13.988571,"end_time":"2024-01-25T17:05:43.765097","exception":false,"start_time":"2024-01-25T17:05:29.776526","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:02.935047Z","iopub.execute_input":"2024-10-28T13:47:02.935334Z","iopub.status.idle":"2024-10-28T13:47:18.147107Z","shell.execute_reply.started":"2024-10-28T13:47:02.935304Z","shell.execute_reply":"2024-10-28T13:47:18.146167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pytorch dataloader","metadata":{"papermill":{"duration":0.022293,"end_time":"2024-01-25T17:05:43.810825","exception":false,"start_time":"2024-01-25T17:05:43.788532","status":"completed"},"tags":[]}},{"cell_type":"code","source":"dataloader = {\n    'train': torch.utils.data.DataLoader(dataset['train'], batch_size=BATCH_SIZE, shuffle=True),\n    'val': torch.utils.data.DataLoader(dataset['val'], batch_size=BATCH_SIZE),\n}","metadata":{"papermill":{"duration":0.030174,"end_time":"2024-01-25T17:05:43.863650","exception":false,"start_time":"2024-01-25T17:05:43.833476","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:18.148424Z","iopub.execute_input":"2024-10-28T13:47:18.149149Z","iopub.status.idle":"2024-10-28T13:47:18.154565Z","shell.execute_reply.started":"2024-10-28T13:47:18.149091Z","shell.execute_reply":"2024-10-28T13:47:18.153584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs, meta, labels = next(iter(dataloader['train']))\n\nimgs.shape, meta.shape, labels.shape","metadata":{"papermill":{"duration":0.071516,"end_time":"2024-01-25T17:05:43.957715","exception":false,"start_time":"2024-01-25T17:05:43.886199","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:18.156116Z","iopub.execute_input":"2024-10-28T13:47:18.156494Z","iopub.status.idle":"2024-10-28T13:47:18.700375Z","shell.execute_reply.started":"2024-10-28T13:47:18.156452Z","shell.execute_reply":"2024-10-28T13:47:18.699307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Move the model to GPU","metadata":{"papermill":{"duration":0.022582,"end_time":"2024-01-25T17:05:44.003195","exception":false,"start_time":"2024-01-25T17:05:43.980613","status":"completed"},"tags":[]}},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\ndevice","metadata":{"papermill":{"duration":0.088581,"end_time":"2024-01-25T17:05:44.114360","exception":false,"start_time":"2024-01-25T17:05:44.025779","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:18.701687Z","iopub.execute_input":"2024-10-28T13:47:18.702087Z","iopub.status.idle":"2024-10-28T13:47:18.732520Z","shell.execute_reply.started":"2024-10-28T13:47:18.702052Z","shell.execute_reply":"2024-10-28T13:47:18.731365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## AUC error metric","metadata":{"papermill":{"duration":0.022493,"end_time":"2024-01-25T17:05:44.160345","exception":false,"start_time":"2024-01-25T17:05:44.137852","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\ndef AUC(outputs, labels):\n    outputs = torch.sigmoid(outputs)\n    outputs = outputs.detach().cpu().numpy()\n    labels = labels.detach().cpu().numpy()\n    auc = roc_auc_score(labels, outputs)\n    return auc","metadata":{"papermill":{"duration":0.030196,"end_time":"2024-01-25T17:05:44.213403","exception":false,"start_time":"2024-01-25T17:05:44.183207","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:18.733853Z","iopub.execute_input":"2024-10-28T13:47:18.734634Z","iopub.status.idle":"2024-10-28T13:47:18.743130Z","shell.execute_reply.started":"2024-10-28T13:47:18.734593Z","shell.execute_reply":"2024-10-28T13:47:18.742278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the model","metadata":{"papermill":{"duration":0.022622,"end_time":"2024-01-25T17:05:44.258744","exception":false,"start_time":"2024-01-25T17:05:44.236122","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model = NN()\noptimizer = torch.optim.Adam(model.parameters(), lr=0.01)\ncriterion = torch.nn.BCEWithLogitsLoss() # this loss apply sigmoid itself, and it's better mathematically\n\nepochs = 6\nvalidation_steps = 15\n\nmb = master_bar(range(1, epochs+1))\nbest_auc = 0\n\nmodel.to(device)\n\nfor epoch in mb:\n    train_loss = []\n    model.train() # training mode\n    for batch in progress_bar(dataloader['train'], parent=mb):\n        imgs, meta, labels = batch\n        imgs, meta, labels = imgs.to(device), meta.to(device), labels.to(device)\n        outputs = model(imgs, meta)\n        optimizer.zero_grad()\n        loss = criterion(outputs, labels)\n        #torch.cuda.empty_cache()\n        loss.backward()\n        optimizer.step() # apply the gradient\n        train_loss.append(loss.item())\n        mb.child.comment = f'loss: {np.mean(train_loss):.5f}'\n        \n    val_loss = []\n    model.eval() # evaluation mode\n    validation_step = 0\n    val_outputs = torch.tensor([])\n    val_targets = torch.tensor([])\n    with torch.no_grad():\n        for batch in progress_bar(dataloader['val'], parent=mb):\n            imgs, meta, labels = batch\n            imgs, meta, labels = imgs.to(device), meta.to(device), labels.to(device)\n            outputs = model(imgs, meta)\n            loss = criterion(outputs, labels)\n            val_loss.append(loss.item())\n            mb.child.comment = f'val_loss: {np.mean(val_loss):.5f}'\n            val_outputs = torch.cat([val_outputs, outputs.cpu()])\n            val_targets = torch.cat([val_targets, labels.cpu()])\n            validation_step += 1\n            if validation_step > validation_steps:\n                break\n                \n    auc = AUC(val_outputs, val_targets)\n    if auc > best_auc:\n        best_auc = auc\n        torch.save(model, 'model.pth') # saving the model\n        \n    mb.write(f'epoch: {epoch} | train_loss: {np.mean(train_loss):.5f} | epoch: {epoch} | val_loss: {np.mean(val_loss):.5f} | auc_loss: {auc:.5f}')","metadata":{"papermill":{"duration":1233.933706,"end_time":"2024-01-25T17:26:18.215180","exception":false,"start_time":"2024-01-25T17:05:44.281474","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:47:18.744760Z","iopub.execute_input":"2024-10-28T13:47:18.745559Z","iopub.status.idle":"2024-10-28T13:57:53.970147Z","shell.execute_reply.started":"2024-10-28T13:47:18.745476Z","shell.execute_reply":"2024-10-28T13:57:53.969030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's see the performance with the testset","metadata":{"papermill":{"duration":0.023026,"end_time":"2024-01-25T17:26:18.262040","exception":false,"start_time":"2024-01-25T17:26:18.239014","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/melanoma224/test.csv')\ntest['path'] = [f'/kaggle/input/melanoma224/jpeg224/test/{img}.jpg' for img in test['image_name']]\n\ntest.head()","metadata":{"papermill":{"duration":0.071376,"end_time":"2024-01-25T17:26:18.358040","exception":false,"start_time":"2024-01-25T17:26:18.286664","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:57:53.971575Z","iopub.execute_input":"2024-10-28T13:57:53.972049Z","iopub.status.idle":"2024-10-28T13:57:54.025462Z","shell.execute_reply.started":"2024-10-28T13:57:53.972008Z","shell.execute_reply":"2024-10-28T13:57:54.024385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = Dataset(test['path'], test[cols], train=False)\ntest_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=200)","metadata":{"papermill":{"duration":0.042586,"end_time":"2024-01-25T17:26:18.425406","exception":false,"start_time":"2024-01-25T17:26:18.382820","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:57:54.026761Z","iopub.execute_input":"2024-10-28T13:57:54.027171Z","iopub.status.idle":"2024-10-28T13:57:54.046301Z","shell.execute_reply.started":"2024-10-28T13:57:54.027126Z","shell.execute_reply":"2024-10-28T13:57:54.045261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs, meta = next(iter(test_dataloader))\nimgs.shape, meta.shape","metadata":{"papermill":{"duration":1.601715,"end_time":"2024-01-25T17:26:20.050832","exception":false,"start_time":"2024-01-25T17:26:18.449117","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:57:54.047666Z","iopub.execute_input":"2024-10-28T13:57:54.048095Z","iopub.status.idle":"2024-10-28T13:57:55.317212Z","shell.execute_reply.started":"2024-10-28T13:57:54.048047Z","shell.execute_reply":"2024-10-28T13:57:55.316193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the model and testing","metadata":{"papermill":{"duration":0.023344,"end_time":"2024-01-25T17:26:20.098786","exception":false,"start_time":"2024-01-25T17:26:20.075442","status":"completed"},"tags":[]}},{"cell_type":"code","source":"model_save = torch.load('/kaggle/working/model.pth')\nmodel_save.to(device)\nmodel_save.eval()\npreds = torch.tensor([]).to(device)\n\n# we dont need to calculate grads during testing\nwith torch.no_grad():\n    for imgs, meta in progress_bar(test_dataloader):\n        imgs, meta = imgs.to(device), meta.to(device)\n        outputs = model(imgs, meta)\n        outputs = torch.sigmoid(outputs)\n        preds = torch.cat([preds, outputs.view(-1)])","metadata":{"papermill":{"duration":91.147201,"end_time":"2024-01-25T17:27:51.269611","exception":false,"start_time":"2024-01-25T17:26:20.122410","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:57:55.318562Z","iopub.execute_input":"2024-10-28T13:57:55.318929Z","iopub.status.idle":"2024-10-28T13:58:59.737640Z","shell.execute_reply.started":"2024-10-28T13:57:55.318877Z","shell.execute_reply":"2024-10-28T13:58:59.736470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"papermill":{"duration":0.462939,"end_time":"2024-01-25T17:27:51.757088","exception":false,"start_time":"2024-01-25T17:27:51.294149","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:58:59.739130Z","iopub.execute_input":"2024-10-28T13:58:59.739574Z","iopub.status.idle":"2024-10-28T13:59:00.237554Z","shell.execute_reply.started":"2024-10-28T13:58:59.739525Z","shell.execute_reply":"2024-10-28T13:59:00.236485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert the preds to dataframe","metadata":{"papermill":{"duration":0.023735,"end_time":"2024-01-25T17:27:51.805180","exception":false,"start_time":"2024-01-25T17:27:51.781445","status":"completed"},"tags":[]}},{"cell_type":"code","source":"submission = pd.DataFrame({'image_name': test['image_name'].values, 'target': preds.cpu().numpy()})\nsubmission","metadata":{"papermill":{"duration":0.040029,"end_time":"2024-01-25T17:27:51.870181","exception":false,"start_time":"2024-01-25T17:27:51.830152","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:59:00.238776Z","iopub.execute_input":"2024-10-28T13:59:00.239120Z","iopub.status.idle":"2024-10-28T13:59:00.252742Z","shell.execute_reply.started":"2024-10-28T13:59:00.239085Z","shell.execute_reply":"2024-10-28T13:59:00.251813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Export to CSV","metadata":{"papermill":{"duration":0.023934,"end_time":"2024-01-25T17:27:51.918432","exception":false,"start_time":"2024-01-25T17:27:51.894498","status":"completed"},"tags":[]}},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"papermill":{"duration":0.070019,"end_time":"2024-01-25T17:27:52.012806","exception":false,"start_time":"2024-01-25T17:27:51.942787","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-10-28T13:59:00.254044Z","iopub.execute_input":"2024-10-28T13:59:00.254423Z","iopub.status.idle":"2024-10-28T13:59:00.292511Z","shell.execute_reply.started":"2024-10-28T13:59:00.254372Z","shell.execute_reply":"2024-10-28T13:59:00.291582Z"},"trusted":true},"execution_count":null,"outputs":[]}]}