{"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":"# 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 file+names:\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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2023-10-19T12:40:17.042538Z","iopub.execute_input":"2023-10-19T12:40:17.042846Z","iopub.status.idle":"2023-10-19T12:40:17.048159Z","shell.execute_reply.started":"2023-10-19T12:40:17.042823Z","shell.execute_reply":"2023-10-19T12:40:17.047397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install timm torchmetrics grad-cam &> /dev/null","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:17.049817Z","iopub.execute_input":"2023-10-19T12:40:17.050513Z","iopub.status.idle":"2023-10-19T12:40:37.519225Z","shell.execute_reply.started":"2023-10-19T12:40:17.050477Z","shell.execute_reply":"2023-10-19T12:40:37.518288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport shutil\nimport cv2\nimport glob\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport copy\n\nimport timm\n\nfrom tqdm.notebook import tqdm\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader, Dataset, WeightedRandomSampler\nfrom torchvision import transforms\nimport torch.optim as optim\n\nfrom torchmetrics.classification import BinaryF1Score, BinaryAccuracy, BinarySpecificity\n\nfrom sklearn.metrics import f1_score\nfrom sklearn.model_selection import StratifiedGroupKFold\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-10-19T12:40:37.520525Z","iopub.execute_input":"2023-10-19T12:40:37.520803Z","iopub.status.idle":"2023-10-19T12:40:53.030205Z","shell.execute_reply.started":"2023-10-19T12:40:37.52078Z","shell.execute_reply":"2023-10-19T12:40:53.029256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import *\nfrom tensorflow.keras.optimizers import *\nfrom tensorflow.keras.losses import *\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *\nfrom tensorflow.keras.callbacks import *\nfrom tensorflow.keras.preprocessing.image import *\nfrom tensorflow.keras.utils import *\nfrom sklearn.metrics import *\nfrom collections import Counter\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport seaborn as sn\n#import preprocessing\nfrom tqdm import tqdm\nfrom io import BytesIO\nfrom PIL import Image\nfrom os import listdir\nimport matplotlib.pyplot as plt\nfrom imageio import imread\nfrom skimage.transform import resize\nfrom sklearn.utils import compute_class_weight\nfrom sklearn.model_selection import *\n# import tensorflow.keras.backend as K\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom glob import glob\nfrom skimage.io import *\n%config Completer.use_jedi = False\nimport warnings\nwarnings.filterwarnings('ignore')\nprint(\"All modules have been imported\")","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:53.032296Z","iopub.execute_input":"2023-10-19T12:40:53.033155Z","iopub.status.idle":"2023-10-19T12:40:53.81135Z","shell.execute_reply.started":"2023-10-19T12:40:53.033121Z","shell.execute_reply":"2023-10-19T12:40:53.810324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:53.812354Z","iopub.execute_input":"2023-10-19T12:40:53.812646Z","iopub.status.idle":"2023-10-19T12:40:53.915587Z","shell.execute_reply.started":"2023-10-19T12:40:53.812621Z","shell.execute_reply":"2023-10-19T12:40:53.914896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ddsm = train_df[['patient_id', 'image_id', 'cancer']]","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:53.916493Z","iopub.execute_input":"2023-10-19T12:40:53.916741Z","iopub.status.idle":"2023-10-19T12:40:53.935074Z","shell.execute_reply.started":"2023-10-19T12:40:53.91672Z","shell.execute_reply":"2023-10-19T12:40:53.933991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_folder= '/kaggle/input/breat-cancer-png-train-images'\n\nddsm['img_path_list'] = ddsm.apply(lambda x: img_folder+ '/' + str(x.patient_id)+'_'+str(x.image_id)+'.png', axis = 1)\n\nddsm.tail(15)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:53.936188Z","iopub.execute_input":"2023-10-19T12:40:53.936477Z","iopub.status.idle":"2023-10-19T12:40:54.711398Z","shell.execute_reply.started":"2023-10-19T12:40:53.936447Z","shell.execute_reply":"2023-10-19T12:40:54.710515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ninput_dir = '/kaggle/input/'\ndataset_list = os.listdir(input_dir)\n\n# Find your dataset directory\nfor dataset_name in dataset_list:\n    if dataset_name != '__notebook_source__.ipynb':  # Exclude the notebook itself\n        dataset_path = os.path.join(input_dir, dataset_name)\n        if os.path.isdir(dataset_path):\n            print(\"Dataset directory:\", dataset_name)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.712448Z","iopub.execute_input":"2023-10-19T12:40:54.712687Z","iopub.status.idle":"2023-10-19T12:40:54.718747Z","shell.execute_reply.started":"2023-10-19T12:40:54.712666Z","shell.execute_reply":"2023-10-19T12:40:54.717859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ddsm.loc[0, 'img_path_list']","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.721795Z","iopub.execute_input":"2023-10-19T12:40:54.722027Z","iopub.status.idle":"2023-10-19T12:40:54.732321Z","shell.execute_reply.started":"2023-10-19T12:40:54.722007Z","shell.execute_reply":"2023-10-19T12:40:54.731466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" g = ddsm[:54705]","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.733575Z","iopub.execute_input":"2023-10-19T12:40:54.733867Z","iopub.status.idle":"2023-10-19T12:40:54.744182Z","shell.execute_reply.started":"2023-10-19T12:40:54.733843Z","shell.execute_reply":"2023-10-19T12:40:54.743351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" g.tail(15)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.745135Z","iopub.execute_input":"2023-10-19T12:40:54.745464Z","iopub.status.idle":"2023-10-19T12:40:54.761202Z","shell.execute_reply.started":"2023-10-19T12:40:54.745433Z","shell.execute_reply":"2023-10-19T12:40:54.760383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimg = Image.open(ddsm.iloc[11]['img_path_list'])\nimg=transforms.ToTensor()(img)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.762273Z","iopub.execute_input":"2023-10-19T12:40:54.762821Z","iopub.status.idle":"2023-10-19T12:40:54.829118Z","shell.execute_reply.started":"2023-10-19T12:40:54.762798Z","shell.execute_reply":"2023-10-19T12:40:54.828154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_list = ddsm['img_path_list']","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.830258Z","iopub.execute_input":"2023-10-19T12:40:54.830498Z","iopub.status.idle":"2023-10-19T12:40:54.834374Z","shell.execute_reply.started":"2023-10-19T12:40:54.830478Z","shell.execute_reply":"2023-10-19T12:40:54.833477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_list=list(images_list)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.83564Z","iopub.execute_input":"2023-10-19T12:40:54.836163Z","iopub.status.idle":"2023-10-19T12:40:54.848496Z","shell.execute_reply.started":"2023-10-19T12:40:54.83612Z","shell.execute_reply":"2023-10-19T12:40:54.847723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images_list)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.849318Z","iopub.execute_input":"2023-10-19T12:40:54.849603Z","iopub.status.idle":"2023-10-19T12:40:54.861954Z","shell.execute_reply.started":"2023-10-19T12:40:54.849582Z","shell.execute_reply":"2023-10-19T12:40:54.861181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ddsm.sort_values('cancer', inplace =True)\nddsm","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.863237Z","iopub.execute_input":"2023-10-19T12:40:54.863952Z","iopub.status.idle":"2023-10-19T12:40:54.891228Z","shell.execute_reply.started":"2023-10-19T12:40:54.863917Z","shell.execute_reply":"2023-10-19T12:40:54.890353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ddsm['cancer'].sum()","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.892239Z","iopub.execute_input":"2023-10-19T12:40:54.892572Z","iopub.status.idle":"2023-10-19T12:40:54.89921Z","shell.execute_reply.started":"2023-10-19T12:40:54.892546Z","shell.execute_reply":"2023-10-19T12:40:54.8983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h = ddsm.iloc[54706-2316:]\nh","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.900252Z","iopub.execute_input":"2023-10-19T12:40:54.900537Z","iopub.status.idle":"2023-10-19T12:40:54.915781Z","shell.execute_reply.started":"2023-10-19T12:40:54.900513Z","shell.execute_reply":"2023-10-19T12:40:54.914733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DDSMDataset(Dataset):\n    def __init__(self, df, is_test=False, transform=None):\n\n        self.images_list = df['img_path_list']\n        self.images_list = list(self.images_list)\n        self.labels_list = df['cancer']\n        self.labels_list = list(self.labels_list)\n        \n        self.is_test = is_test\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.labels_list)\n    \n    def __getitem__(self, idx):\n        if self.is_test:\n            img = Image.open(self.images_list[idx])\n            \n        else:\n            img = Image.open(self.images_list[idx])\n        img = transforms.ToTensor()(img)\n        if self.transform is not None:\n            img = self.transform(img)\n        #img = torch.permute(img, (2, 0, 1)) \n        img = transforms.Resize((512,512))(img)  \n        img = img.repeat(3,1,1)\n        if not self.is_test:\n            target = self.labels_list[idx]\n            return img, target\n        #img = img.unsqueeze(0)\n        \n        return img, target","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.916796Z","iopub.execute_input":"2023-10-19T12:40:54.917115Z","iopub.status.idle":"2023-10-19T12:40:54.928301Z","shell.execute_reply.started":"2023-10-19T12:40:54.917091Z","shell.execute_reply":"2023-10-19T12:40:54.927197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = DDSMDataset(ddsm, is_test=False, transform=None)\n#valid_dataset = DDSMDataset(images_list = x_val, labels_list = y_val, is_test=False, transform=None)\n#test_dataset = DDSMDataset(images_list = x_test, labels_list = y_test, is_test=False, transform=None)\n    \n#train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)\n#val_loader = DataLoader(valid_dataset, batch_size=16, shuffle=True)\n#test_loader = DataLoader(test_dataset, batch_size=16, shuffle=True)\nlen(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.929541Z","iopub.execute_input":"2023-10-19T12:40:54.929929Z","iopub.status.idle":"2023-10-19T12:40:54.949774Z","shell.execute_reply.started":"2023-10-19T12:40:54.929891Z","shell.execute_reply":"2023-10-19T12:40:54.948905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.unique(ddsm['cancer'])","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.951048Z","iopub.execute_input":"2023-10-19T12:40:54.95236Z","iopub.status.idle":"2023-10-19T12:40:54.963228Z","shell.execute_reply.started":"2023-10-19T12:40:54.95233Z","shell.execute_reply":"2023-10-19T12:40:54.962318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights = []\nfor i in np.unique(ddsm['cancer']):\n    class_weights.append(1/len(ddsm[ddsm['cancer']==i]))\nclass_weights","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.964504Z","iopub.execute_input":"2023-10-19T12:40:54.96488Z","iopub.status.idle":"2023-10-19T12:40:54.978228Z","shell.execute_reply.started":"2023-10-19T12:40:54.964849Z","shell.execute_reply":"2023-10-19T12:40:54.977474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[-1]","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:54.979117Z","iopub.execute_input":"2023-10-19T12:40:54.979332Z","iopub.status.idle":"2023-10-19T12:40:55.054282Z","shell.execute_reply.started":"2023-10-19T12:40:54.979313Z","shell.execute_reply":"2023-10-19T12:40:55.053508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_dataset[12][0].shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:55.059658Z","iopub.execute_input":"2023-10-19T12:40:55.059861Z","iopub.status.idle":"2023-10-19T12:40:55.073157Z","shell.execute_reply.started":"2023-10-19T12:40:55.059843Z","shell.execute_reply":"2023-10-19T12:40:55.072366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntype(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:55.074323Z","iopub.execute_input":"2023-10-19T12:40:55.074748Z","iopub.status.idle":"2023-10-19T12:40:55.079726Z","shell.execute_reply.started":"2023-10-19T12:40:55.074718Z","shell.execute_reply":"2023-10-19T12:40:55.0789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import collections\nfrom tqdm.notebook import tqdm_notebook\n\n# get labels\n# for _,y in tqdm()\ny_train=[y for _,y in tqdm(train_dataset)]\n\n# count labels\ncounter_train=collections.Counter(y_train)\nprint(counter_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:40:55.080769Z","iopub.execute_input":"2023-10-19T12:40:55.081143Z","iopub.status.idle":"2023-10-19T12:49:53.769978Z","shell.execute_reply.started":"2023-10-19T12:40:55.08112Z","shell.execute_reply":"2023-10-19T12:49:53.769196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_1=0\ncount_0=0\nfor _,y in tqdm_notebook(train_dataset):\n     if y==1:\n         count_1+=1\n     else:\n         count_0+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:49:53.771474Z","iopub.execute_input":"2023-10-19T12:49:53.771774Z","iopub.status.idle":"2023-10-19T12:53:11.639996Z","shell.execute_reply.started":"2023-10-19T12:49:53.771745Z","shell.execute_reply":"2023-10-19T12:53:11.638993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_1","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:53:11.641266Z","iopub.execute_input":"2023-10-19T12:53:11.641869Z","iopub.status.idle":"2023-10-19T12:53:11.64774Z","shell.execute_reply.started":"2023-10-19T12:53:11.641834Z","shell.execute_reply":"2023-10-19T12:53:11.646895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedShuffleSplit\n\nsss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=0)\n\nindices=list(range(len(train_dataset)))\n#y_train0=[y for _,y in train_dataset]\nfor train_index, val_index in sss.split(indices, y_train):\n    print(\"train:\", train_index, \"val:\", val_index)\n    print(len(train_index),len(val_index))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:53:11.649282Z","iopub.execute_input":"2023-10-19T12:53:11.650038Z","iopub.status.idle":"2023-10-19T12:53:11.691381Z","shell.execute_reply.started":"2023-10-19T12:53:11.649992Z","shell.execute_reply":"2023-10-19T12:53:11.690309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Subset\n\ntrain_ds=Subset(train_dataset,train_index)\nval_data=Subset(train_dataset,val_index)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:53:11.692757Z","iopub.execute_input":"2023-10-19T12:53:11.693045Z","iopub.status.idle":"2023-10-19T12:53:11.697871Z","shell.execute_reply.started":"2023-10-19T12:53:11.693019Z","shell.execute_reply":"2023-10-19T12:53:11.69668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_ds))\nprint(len(val_data))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:53:11.69896Z","iopub.execute_input":"2023-10-19T12:53:11.699208Z","iopub.status.idle":"2023-10-19T12:53:11.711869Z","shell.execute_reply.started":"2023-10-19T12:53:11.699187Z","shell.execute_reply":"2023-10-19T12:53:11.710947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" y_train_ds=[y for _,y in tqdm_notebook(train_ds)]\n\n#count labels\ncounter_train=collections.Counter(y_train_ds)\nprint(counter_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:53:11.713098Z","iopub.execute_input":"2023-10-19T12:53:11.713362Z","iopub.status.idle":"2023-10-19T12:55:49.609562Z","shell.execute_reply.started":"2023-10-19T12:53:11.713338Z","shell.execute_reply":"2023-10-19T12:55:49.608673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sss = StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=0)\n\nindices=list(range(len(val_data)))\ny_val=[y for _,y in tqdm(val_data)]\nfor val_index, test_index in sss.split(indices, y_val):\n    print(\"train:\", val_index, \"val:\", test_index)\n    print(len(val_index),len(test_index))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:55:49.611005Z","iopub.execute_input":"2023-10-19T12:55:49.611647Z","iopub.status.idle":"2023-10-19T12:56:28.199153Z","shell.execute_reply.started":"2023-10-19T12:55:49.611607Z","shell.execute_reply":"2023-10-19T12:56:28.198263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds=Subset(val_data,val_index)\ntest_ds=Subset(val_data,test_index)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:56:28.200475Z","iopub.execute_input":"2023-10-19T12:56:28.200832Z","iopub.status.idle":"2023-10-19T12:56:28.205196Z","shell.execute_reply.started":"2023-10-19T12:56:28.200799Z","shell.execute_reply":"2023-10-19T12:56:28.20442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_ds))\nprint(len(val_ds))\nprint(len(test_ds))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:56:28.206663Z","iopub.execute_input":"2023-10-19T12:56:28.207331Z","iopub.status.idle":"2023-10-19T12:56:28.219703Z","shell.execute_reply.started":"2023-10-19T12:56:28.207307Z","shell.execute_reply":"2023-10-19T12:56:28.218947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=[y for _,y in test_ds]\ny_val=[y for _,y in val_ds]\n\ncounter_test=collections.Counter(y_test)\ncounter_val=collections.Counter(y_val)\nprint(counter_test)\nprint(counter_val)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:56:28.220937Z","iopub.execute_input":"2023-10-19T12:56:28.221175Z","iopub.status.idle":"2023-10-19T12:57:06.229505Z","shell.execute_reply.started":"2023-10-19T12:56:28.221148Z","shell.execute_reply":"2023-10-19T12:57:06.228703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import utils\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\n# fix random seed\nnp.random.seed(0)\n\ndef show(img,y=None,color=True):\n    npimg = img.numpy()\n    npimg_tr=np.transpose(npimg, (1,2,0))\n    plt.imshow(npimg_tr)\n    if y is not None:\n        plt.title(\"label: \"+str(y))\n        \ngrid_size=4\nrnd_inds=np.random.randint(0,len(train_ds),grid_size)\nprint(\"image indices:\",rnd_inds)\n\nx_grid=[train_ds[i][0] for i in rnd_inds]\ny_grid=[train_ds[i][1] for i in rnd_inds]\n\nx_grid=utils.make_grid(x_grid, nrow=4, padding=2)\n#print(x_grid.shape)\n\n# call helper function\nplt.figure(figsize=(10,10))\nshow(x_grid,y_grid)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:06.230771Z","iopub.execute_input":"2023-10-19T12:57:06.231379Z","iopub.status.idle":"2023-10-19T12:57:06.911195Z","shell.execute_reply.started":"2023-10-19T12:57:06.231351Z","shell.execute_reply":"2023-10-19T12:57:06.910239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import utils\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\n# fix random seed\nnp.random.seed(0)\n\ndef show(img,y=None,color=True):\n    npimg = img.numpy()\n    npimg_tr=np.transpose(npimg, (1,2,0))\n    plt.imshow(npimg_tr)\n    if y is not None:\n        plt.title(\"label: \"+str(y))\n        \ngrid_size=4\nrnd_inds=np.random.randint(0,len(val_ds),grid_size)\nprint(\"image indices:\",rnd_inds)\n\nx_grid=[val_ds[i][0] for i in rnd_inds]\ny_grid=[val_ds[i][1] for i in rnd_inds]\n\nx_grid=utils.make_grid(x_grid, nrow=4, padding=2)\n#print(x_grid.shape)\n\n# call helper function\nplt.figure(figsize=(10,10))\nshow(x_grid,y_grid)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:06.91226Z","iopub.execute_input":"2023-10-19T12:57:06.912516Z","iopub.status.idle":"2023-10-19T12:57:07.504209Z","shell.execute_reply.started":"2023-10-19T12:57:06.912494Z","shell.execute_reply":"2023-10-19T12:57:07.503433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import utils\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\n# fix random seed\nnp.random.seed(0)\n\ndef show(img,y=None,color=True):\n    npimg = img.numpy()\n    npimg_tr=np.transpose(npimg, (1,2,0))\n    plt.imshow(npimg_tr)\n    if y is not None:\n        plt.title(\"label: \"+str(y))\n        \ngrid_size=4\nrnd_inds=np.random.randint(0,len(test_ds),grid_size)\nprint(\"image indices:\",rnd_inds)\n\nx_grid=[test_ds[i][0] for i in rnd_inds]\ny_grid=[test_ds[i][1] for i in rnd_inds]\n\nx_grid=utils.make_grid(x_grid, nrow=4, padding=2)\n#print(x_grid.shape)\n\n# call helper function\nplt.figure(figsize=(10,10))\nshow(x_grid,y_grid)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:07.50533Z","iopub.execute_input":"2023-10-19T12:57:07.505687Z","iopub.status.idle":"2023-10-19T12:57:08.177143Z","shell.execute_reply.started":"2023-10-19T12:57:07.505648Z","shell.execute_reply":"2023-10-19T12:57:08.17611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transformer = transforms.Compose([\n    transforms.RandomHorizontalFlip(p=0.5),  \n    transforms.RandomVerticalFlip(p=0.5),  \n    transforms.ToTensor(),\n    transforms.Lambda(lambda x: torch.cat([x, x, x], 0)),\n    transforms.Normalize((0.5,), (0.5,))])\n                 \n\ntest0_transformer = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Lambda(lambda x: torch.cat([x, x, x], 0)),\n    transforms.Normalize((0.5,), (0.5,)),\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:08.178317Z","iopub.execute_input":"2023-10-19T12:57:08.178627Z","iopub.status.idle":"2023-10-19T12:57:08.185121Z","shell.execute_reply.started":"2023-10-19T12:57:08.178595Z","shell.execute_reply":"2023-10-19T12:57:08.184091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds.transform=train_transformer\nval_data.transform=test0_transformer","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:08.186273Z","iopub.execute_input":"2023-10-19T12:57:08.186576Z","iopub.status.idle":"2023-10-19T12:57:08.203216Z","shell.execute_reply.started":"2023-10-19T12:57:08.186551Z","shell.execute_reply":"2023-10-19T12:57:08.202358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import utils\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\n# fix random seed\nnp.random.seed(0)\n\ndef show(img,y=None,color=True):\n    npimg = img.numpy()\n    npimg_tr=np.transpose(npimg, (1,2,0))\n    plt.imshow(npimg_tr)\n    if y is not None:\n        plt.title(\"label: \"+str(y))\n        \ngrid_size=4\nrnd_inds=np.random.randint(0,len(train_ds),grid_size)\nprint(\"image indices:\",rnd_inds)\n\nx_grid=[train_ds[i][0] for i in rnd_inds]\ny_grid=[train_ds[i][1] for i in rnd_inds]\n\nx_grid=utils.make_grid(x_grid, nrow=4, padding=2)\n#print(x_grid.shape)\n\n# call helper function\nplt.figure(figsize=(10,10))\nshow(x_grid,y_grid)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:08.204372Z","iopub.execute_input":"2023-10-19T12:57:08.204763Z","iopub.status.idle":"2023-10-19T12:57:08.835299Z","shell.execute_reply.started":"2023-10-19T12:57:08.204728Z","shell.execute_reply":"2023-10-19T12:57:08.834476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import utils\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\n# fix random seed\nnp.random.seed(0)\n\ndef show(img,y=None,color=True):\n    npimg = img.numpy()\n    npimg_tr=np.transpose(npimg, (1,2,0))\n    plt.imshow(npimg_tr)\n    if y is not None:\n        plt.title(\"label: \"+str(y))\n        \ngrid_size=4\nrnd_inds=np.random.randint(0,len(val_ds),grid_size)\nprint(\"image indices:\",rnd_inds)\n\nx_grid=[val_ds[i][0] for i in rnd_inds]\ny_grid=[val_ds[i][1] for i in rnd_inds]\n\nx_grid=utils.make_grid(x_grid, nrow=4, padding=2)\n#print(x_grid.shape)\n\n# call helper function\nplt.figure(figsize=(10,10))\nshow(x_grid,y_grid)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:08.83653Z","iopub.execute_input":"2023-10-19T12:57:08.836866Z","iopub.status.idle":"2023-10-19T12:57:09.373896Z","shell.execute_reply.started":"2023-10-19T12:57:08.836836Z","shell.execute_reply":"2023-10-19T12:57:09.373064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import utils\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\n# fix random seed\nnp.random.seed(0)\n\ndef show(img,y=None,color=True):\n    npimg = img.numpy()\n    npimg_tr=np.transpose(npimg, (1,2,0))\n    plt.imshow(npimg_tr)\n    if y is not None:\n        plt.title(\"label: \"+str(y))\n        \ngrid_size=4\nrnd_inds=np.random.randint(0,len(test_ds),grid_size)\nprint(\"image indices:\",rnd_inds)\n\nx_grid=[test_ds[i][0] for i in rnd_inds]\ny_grid=[test_ds[i][1] for i in rnd_inds]\n\nx_grid=utils.make_grid(x_grid, nrow=4, padding=2)\n#print(x_grid.shape)\n\n# call helper function\nplt.figure(figsize=(10,10))\nshow(x_grid,y_grid)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:09.375112Z","iopub.execute_input":"2023-10-19T12:57:09.375461Z","iopub.status.idle":"2023-10-19T12:57:09.903981Z","shell.execute_reply.started":"2023-10-19T12:57:09.375431Z","shell.execute_reply":"2023-10-19T12:57:09.90312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_weights = [0]*len(train_ds)\nfor idx, (data, label) in enumerate(train_ds):\n    class_weight = class_weights[label]\n    sample_weights[idx] = class_weight","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:57:09.905038Z","iopub.execute_input":"2023-10-19T12:57:09.905307Z","iopub.status.idle":"2023-10-19T12:59:43.707548Z","shell.execute_reply.started":"2023-10-19T12:57:09.905284Z","shell.execute_reply":"2023-10-19T12:59:43.70673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sampler = WeightedRandomSampler(sample_weights, num_samples=len(sample_weights), replacement=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:43.70859Z","iopub.execute_input":"2023-10-19T12:59:43.708859Z","iopub.status.idle":"2023-10-19T12:59:43.716565Z","shell.execute_reply.started":"2023-10-19T12:59:43.708837Z","shell.execute_reply":"2023-10-19T12:59:43.715857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl1 = DataLoader(train_ds, batch_size=16, shuffle=True)\nfor i, (x,y) in enumerate(train_dl1):\n    print(y.sum())\n    if i>20:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:43.717469Z","iopub.execute_input":"2023-10-19T12:59:43.718397Z","iopub.status.idle":"2023-10-19T12:59:45.539313Z","shell.execute_reply.started":"2023-10-19T12:59:43.718367Z","shell.execute_reply":"2023-10-19T12:59:45.538335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\ntrain_dl = DataLoader(train_ds, batch_size=16, sampler=sampler)\nval_dl = DataLoader(val_ds, batch_size=16, shuffle=False) ","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:45.540725Z","iopub.execute_input":"2023-10-19T12:59:45.541078Z","iopub.status.idle":"2023-10-19T12:59:45.545956Z","shell.execute_reply.started":"2023-10-19T12:59:45.541043Z","shell.execute_reply":"2023-10-19T12:59:45.545223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, (x,y) in enumerate(train_dl):\n    print(y.sum())\n    if i>20:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:45.547198Z","iopub.execute_input":"2023-10-19T12:59:45.547504Z","iopub.status.idle":"2023-10-19T12:59:47.582159Z","shell.execute_reply.started":"2023-10-19T12:59:45.547474Z","shell.execute_reply":"2023-10-19T12:59:47.581293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x, y in train_dl:\n    print(x.shape)\n    print(y.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:47.583239Z","iopub.execute_input":"2023-10-19T12:59:47.583528Z","iopub.status.idle":"2023-10-19T12:59:47.679119Z","shell.execute_reply.started":"2023-10-19T12:59:47.583503Z","shell.execute_reply":"2023-10-19T12:59:47.678175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x, y in val_dl:\n    print(x.shape)\n    print(y.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:47.680364Z","iopub.execute_input":"2023-10-19T12:59:47.680742Z","iopub.status.idle":"2023-10-19T12:59:47.770041Z","shell.execute_reply.started":"2023-10-19T12:59:47.680709Z","shell.execute_reply":"2023-10-19T12:59:47.768967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision import models\nimport torch\nfrom torch import nn\n\n# load model with pretrained weights\ndensenet121_pretrained = models.densenet121(pretrained=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:47.77131Z","iopub.execute_input":"2023-10-19T12:59:47.771703Z","iopub.status.idle":"2023-10-19T12:59:48.289308Z","shell.execute_reply.started":"2023-10-19T12:59:47.771669Z","shell.execute_reply":"2023-10-19T12:59:48.288473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"densenet121_pretrained","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:48.290335Z","iopub.execute_input":"2023-10-19T12:59:48.29119Z","iopub.status.idle":"2023-10-19T12:59:48.302047Z","shell.execute_reply.started":"2023-10-19T12:59:48.291142Z","shell.execute_reply":"2023-10-19T12:59:48.301066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# densenet121_pretrained.features.conv0","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:48.303696Z","iopub.execute_input":"2023-10-19T12:59:48.303957Z","iopub.status.idle":"2023-10-19T12:59:48.312079Z","shell.execute_reply.started":"2023-10-19T12:59:48.303935Z","shell.execute_reply":"2023-10-19T12:59:48.311135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# densenet121_pretrained.features.conv0=nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\ndensenet121_pretrained.classifier = nn.Sequential(nn.Linear(1024,2)) # ResNet50\ndensenet121_pretrained = densenet121_pretrained.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:48.313218Z","iopub.execute_input":"2023-10-19T12:59:48.31353Z","iopub.status.idle":"2023-10-19T12:59:54.480604Z","shell.execute_reply.started":"2023-10-19T12:59:48.313505Z","shell.execute_reply":"2023-10-19T12:59:54.479732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:54.481733Z","iopub.execute_input":"2023-10-19T12:59:54.48202Z","iopub.status.idle":"2023-10-19T12:59:54.487444Z","shell.execute_reply.started":"2023-10-19T12:59:54.481996Z","shell.execute_reply":"2023-10-19T12:59:54.486458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#densenet121_pretrained.load_state_dict(torch.load('./densenet121_pretrained_abhishek_expt1 (copy).pt', map_location =torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:54.488305Z","iopub.execute_input":"2023-10-19T12:59:54.488564Z","iopub.status.idle":"2023-10-19T12:59:54.498392Z","shell.execute_reply.started":"2023-10-19T12:59:54.488543Z","shell.execute_reply":"2023-10-19T12:59:54.497531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"densenet121_pretrained","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:54.499633Z","iopub.execute_input":"2023-10-19T12:59:54.499915Z","iopub.status.idle":"2023-10-19T12:59:54.515604Z","shell.execute_reply.started":"2023-10-19T12:59:54.499892Z","shell.execute_reply":"2023-10-19T12:59:54.514734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for x,y in train_dl:\n    print(x.shape)\n    break\n  \ndensenet121_pretrained(x.cuda()).shape","metadata":{"execution":{"iopub.status.busy":"2023-10-19T12:59:54.51638Z","iopub.execute_input":"2023-10-19T12:59:54.516631Z","iopub.status.idle":"2023-10-19T13:00:01.236437Z","shell.execute_reply.started":"2023-10-19T12:59:54.516611Z","shell.execute_reply":"2023-10-19T13:00:01.2356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:01.23765Z","iopub.execute_input":"2023-10-19T13:00:01.237936Z","iopub.status.idle":"2023-10-19T13:00:02.432175Z","shell.execute_reply.started":"2023-10-19T13:00:01.23791Z","shell.execute_reply":"2023-10-19T13:00:02.43127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = optim.Adam(densenet121_pretrained.parameters(), lr=1e-4)\ndef get_lr(opt):\n    for param_group in opt.param_groups:\n        return param_group['lr']\n\ncurrent_lr=get_lr(opt)\nprint('current lr={}'.format(current_lr))\n\nfrom torch.optim.lr_scheduler import CosineAnnealingLR\n\n# define learning rate scheduler\nlr_scheduler = CosineAnnealingLR(opt,T_max=2,eta_min=1e-5)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.433721Z","iopub.execute_input":"2023-10-19T13:00:02.434032Z","iopub.status.idle":"2023-10-19T13:00:02.442875Z","shell.execute_reply.started":"2023-10-19T13:00:02.434009Z","shell.execute_reply":"2023-10-19T13:00:02.442028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def metrics_batch(output, target):\n    # get output class\n    pred = output.argmax(dim=1, keepdim=True)\n    \n    # compare output class with target class\n    corrects=pred.eq(target.view_as(pred)).sum().item()\n    return corrects","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.444046Z","iopub.execute_input":"2023-10-19T13:00:02.44428Z","iopub.status.idle":"2023-10-19T13:00:02.454923Z","shell.execute_reply.started":"2023-10-19T13:00:02.444259Z","shell.execute_reply":"2023-10-19T13:00:02.454112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.455935Z","iopub.execute_input":"2023-10-19T13:00:02.456165Z","iopub.status.idle":"2023-10-19T13:00:02.468431Z","shell.execute_reply.started":"2023-10-19T13:00:02.456145Z","shell.execute_reply":"2023-10-19T13:00:02.467618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loss_batch(loss_func, output, target, opt=None):\n    \n    # get loss \n    loss = loss_func(output, target)\n    \n    # get performance metric\n    metric_b = metrics_batch(output,target)\n    \n    if opt is not None:\n        opt.zero_grad()\n        loss.backward()\n        opt.step()\n\n    return loss.item(), metric_b","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.46935Z","iopub.execute_input":"2023-10-19T13:00:02.469638Z","iopub.status.idle":"2023-10-19T13:00:02.478094Z","shell.execute_reply.started":"2023-10-19T13:00:02.469618Z","shell.execute_reply":"2023-10-19T13:00:02.477457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loss_epoch(model,loss_func,dataset_dl,opt=None):\n    running_loss=0.0\n    running_metric=0.0\n    len_data=len(dataset_dl.dataset)\n\n    for xb, yb in tqdm(dataset_dl):\n        # move batch to device\n        xb=xb.to(device)\n        yb=yb.to(device)\n        \n        # get model output\n        output=model(xb)\n        \n        # get loss per batch\n        loss_b,metric_b=loss_batch(loss_func, output, yb, opt)\n        \n        # update running loss\n        running_loss+=loss_b\n        \n        # update running metric\n        if metric_b is not None:\n            running_metric+=metric_b\n    \n    # average loss value\n    loss=running_loss/float(len_data)\n    \n    # average metric value\n    metric=running_metric/float(len_data)\n    \n    return loss, metric","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.479185Z","iopub.execute_input":"2023-10-19T13:00:02.479928Z","iopub.status.idle":"2023-10-19T13:00:02.48885Z","shell.execute_reply.started":"2023-10-19T13:00:02.479896Z","shell.execute_reply":"2023-10-19T13:00:02.488041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import copy\n\ndef train_val(model, params):\n    # extract model parameters\n    num_epochs=params[\"num_epochs\"]\n    loss_func=params[\"loss_func\"]\n    opt=params[\"optimizer\"]\n    train_dl=params[\"train_dl\"]\n    val_dl=params[\"val_dl\"]\n    lr_scheduler=params[\"lr_scheduler\"]\n    path2weights=params[\"path2weights\"]\n    \n    os.makedirs(os.path.dirname(path2weights), exist_ok=True)# Create the parent directory\n    \n    \n    # history of loss values in each epoch\n    loss_history={\n        \"train\": [],\n        \"val\": [],\n    }\n    \n    # histroy of metric values in each epoch\n    metric_history={\n        \"train\": [],\n        \"val\": [],\n    }\n    \n    # a deep copy of weights for the best performing model\n    best_model_wts = copy.deepcopy(model.state_dict())\n    \n    # initialize best loss to a large value\n    best_loss=float('inf')\n    \n    # main loop\n    for epoch in tqdm(range(num_epochs)):\n\n        # get current learning rate\n        current_lr=get_lr(opt)\n        print('Epoch {}/{}, current lr={}'.format(epoch+1, num_epochs, current_lr))\n        \n        # print('Epoch {}/{}'.format(epoch+1, num_epochs))\n        \n        # train model on training dataset\n        model.train()\n        train_loss, train_metric=loss_epoch(model,loss_func,train_dl,opt)\n\n        # collect loss and metric for training dataset\n        loss_history[\"train\"].append(train_loss)\n        metric_history[\"train\"].append(train_metric)\n        \n        # evaluate model on validation dataset    \n        model.eval()\n        with torch.no_grad():\n            val_loss, val_metric=loss_epoch(model,loss_func,val_dl)\n        \n       \n        # store best model\n        if val_loss < best_loss:\n            best_loss = val_loss\n            best_model_wts = copy.deepcopy(model.state_dict())\n            \n            # store weights into a local file\n            torch.save(model.state_dict(), path2weights)\n            print(\"Copied best model weights!\")\n        \n        # collect loss and metric for validation dataset\n        loss_history[\"val\"].append(val_loss)\n        metric_history[\"val\"].append(val_metric)\n        \n        # learning rate schedule\n        lr_scheduler.step()\n\n        print(\"train loss: %.6f, dev loss: %.6f, accuracy: %.2f\" %(train_loss,val_loss,100*val_metric))\n        print(\"-\"*10) \n\n    # load best model weights\n    model.load_state_dict(best_model_wts)\n        \n    return model, loss_history, metric_history","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.49001Z","iopub.execute_input":"2023-10-19T13:00:02.490298Z","iopub.status.idle":"2023-10-19T13:00:02.504841Z","shell.execute_reply.started":"2023-10-19T13:00:02.490274Z","shell.execute_reply":"2023-10-19T13:00:02.504135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.505803Z","iopub.execute_input":"2023-10-19T13:00:02.506016Z","iopub.status.idle":"2023-10-19T13:00:02.523395Z","shell.execute_reply.started":"2023-10-19T13:00:02.505997Z","shell.execute_reply":"2023-10-19T13:00:02.522667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch import optim\n\nloss_func = nn.CrossEntropyLoss(weight = torch.tensor([1.0,2.0]).cuda())\n# opt = optim.Adam(densenet121_pretrained.parameters(), lr=1e-4)\n\nparams_train={\n \"num_epochs\": 2,\n \"optimizer\": opt,\n \"loss_func\": loss_func,\n \"train_dl\": train_dl,\n \"val_dl\": val_dl,\n \"lr_scheduler\": lr_scheduler,\n \"path2weights\": \"/kaggle/working/models/weights/pt\"}\n\n# train and validate the model\ndensenet121_pretrained,loss_hist,metric_hist=train_val(densenet121_pretrained,params_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T13:00:02.524493Z","iopub.execute_input":"2023-10-19T13:00:02.524718Z","iopub.status.idle":"2023-10-19T14:23:01.461285Z","shell.execute_reply.started":"2023-10-19T13:00:02.524699Z","shell.execute_reply":"2023-10-19T14:23:01.460419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train-Validation Progress\nnum_epochs=params_train[\"num_epochs\"]\n\n# plot loss progress\nplt.title(\"Train-Val Loss\")\nplt.plot(range(1,num_epochs+1),loss_hist[\"train\"],label=\"train\")\nplt.plot(range(1,num_epochs+1),loss_hist[\"val\"],label=\"val\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"Training Epochs\")\nplt.legend()\nplt.show()\n\n# plot accuracy progress\nplt.title(\"Train-Val Accuracy\")\nplt.plot(range(1,num_epochs+1),metric_hist[\"train\"],label=\"train\")\nplt.plot(range(1,num_epochs+1),metric_hist[\"val\"],label=\"val\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Training Epochs\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:23:01.46839Z","iopub.execute_input":"2023-10-19T14:23:01.468671Z","iopub.status.idle":"2023-10-19T14:23:01.99332Z","shell.execute_reply.started":"2023-10-19T14:23:01.468648Z","shell.execute_reply":"2023-10-19T14:23:01.992573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"densenet121_pretrained.load_state_dict(torch.load('/kaggle/working/models/weights/pt', map_location =torch.device('cuda')))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:32:57.835321Z","iopub.execute_input":"2023-10-19T14:32:57.835794Z","iopub.status.idle":"2023-10-19T14:32:57.963789Z","shell.execute_reply.started":"2023-10-19T14:32:57.83576Z","shell.execute_reply":"2023-10-19T14:32:57.962843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"densenet121_pretrained.eval()","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:33:03.050115Z","iopub.execute_input":"2023-10-19T14:33:03.050886Z","iopub.status.idle":"2023-10-19T14:33:03.065927Z","shell.execute_reply.started":"2023-10-19T14:33:03.05085Z","shell.execute_reply":"2023-10-19T14:33:03.064553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def deploy_model(model,dataset,device,num_classes=2):\n  len_data=len(dataset)\n  y_out=torch.zeros(len_data,num_classes)\n  y_gt=np.zeros((len_data),dtype=\"uint8\")\n  with torch.no_grad():\n    for i in range(len_data):\n      x,y=dataset[i]\n      y_gt[i]=y\n      yy=model(x.unsqueeze(0).to(device))\n      y_out[i]=torch.softmax(yy,dim=1)\n  return y_out.numpy(),y_gt","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:33:23.63304Z","iopub.execute_input":"2023-10-19T14:33:23.63378Z","iopub.status.idle":"2023-10-19T14:33:23.640067Z","shell.execute_reply.started":"2023-10-19T14:33:23.633743Z","shell.execute_reply":"2023-10-19T14:33:23.638839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_out,y_gt=deploy_model(densenet121_pretrained,test_ds,device=device)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:33:38.419247Z","iopub.execute_input":"2023-10-19T14:33:38.419908Z","iopub.status.idle":"2023-10-19T14:34:02.091967Z","shell.execute_reply.started":"2023-10-19T14:33:38.419878Z","shell.execute_reply":"2023-10-19T14:34:02.090969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique, counts = np.unique(y_gt, return_counts=True)\ndict(zip(unique, counts))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:06.218705Z","iopub.execute_input":"2023-10-19T14:34:06.219029Z","iopub.status.idle":"2023-10-19T14:34:06.22553Z","shell.execute_reply.started":"2023-10-19T14:34:06.218998Z","shell.execute_reply":"2023-10-19T14:34:06.224677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.argmax(y_out,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:09.400561Z","iopub.execute_input":"2023-10-19T14:34:09.401501Z","iopub.status.idle":"2023-10-19T14:34:09.405921Z","shell.execute_reply.started":"2023-10-19T14:34:09.401466Z","shell.execute_reply":"2023-10-19T14:34:09.404747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:11.790301Z","iopub.execute_input":"2023-10-19T14:34:11.790704Z","iopub.status.idle":"2023-10-19T14:34:11.797716Z","shell.execute_reply.started":"2023-10-19T14:34:11.790674Z","shell.execute_reply":"2023-10-19T14:34:11.796645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique, counts = np.unique(y_pred, return_counts=True)\ndict(zip(unique, counts))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:14.798947Z","iopub.execute_input":"2023-10-19T14:34:14.799679Z","iopub.status.idle":"2023-10-19T14:34:14.806458Z","shell.execute_reply.started":"2023-10-19T14:34:14.799651Z","shell.execute_reply":"2023-10-19T14:34:14.805608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_ds)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:18.268064Z","iopub.execute_input":"2023-10-19T14:34:18.268353Z","iopub.status.idle":"2023-10-19T14:34:18.274071Z","shell.execute_reply.started":"2023-10-19T14:34:18.268332Z","shell.execute_reply":"2023-10-19T14:34:18.273051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nprint('Accuracy:', np.round(metrics.accuracy_score(y_gt, y_pred),5))\nprint('Precision:', np.round(metrics.precision_score(y_gt, y_pred, average='weighted'),5))\nprint('Recall:', np.round(metrics.recall_score(y_gt,y_pred, average='weighted'),5))\nprint('F1 Score:', np.round(metrics.f1_score(y_gt, y_pred, average='weighted'),5))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:21.26589Z","iopub.execute_input":"2023-10-19T14:34:21.266865Z","iopub.status.idle":"2023-10-19T14:34:21.278473Z","shell.execute_reply.started":"2023-10-19T14:34:21.266825Z","shell.execute_reply":"2023-10-19T14:34:21.277667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=['0','1']\nprint('\\t\\tClassification Report:\\n', metrics.classification_report(y_gt, y_pred,target_names=target))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:52.550774Z","iopub.execute_input":"2023-10-19T14:34:52.551533Z","iopub.status.idle":"2023-10-19T14:34:52.565284Z","shell.execute_reply.started":"2023-10-19T14:34:52.551499Z","shell.execute_reply":"2023-10-19T14:34:52.564294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.datasets import make_classification\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\ncm = confusion_matrix(y_gt, y_pred, labels=[0,1])\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=[0,1])\ndisp.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:23:03.197347Z","iopub.status.idle":"2023-10-19T14:23:03.197724Z","shell.execute_reply.started":"2023-10-19T14:23:03.197566Z","shell.execute_reply":"2023-10-19T14:23:03.197583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(123)\nn_rows=6\nn_cols=3\nindices = np.random.choice(range(len(test_ds)), n_rows*n_cols, \n                            replace=False)\nfig= plt.figure(figsize=(30,30))\nfor i, ind in enumerate(indices):\n    fig.add_subplot(n_rows, n_cols, i+1)\n    plt.imshow(test_ds[ind][0].numpy().transpose(1,2,0),cmap='gray')\n    plt.axis('off')\n    proba = y_pred[ind].max()\n    pred = y_pred[ind].argmax()\n    if pred == y_gt[ind]:\n        colour = 'green'\n    else:\n        colour = 'red'\n    plt.title(f\"Prediction: {pred}, True: {test_ds[ind][1]} ({proba:.1%})\", fontsize=20, \n              color=colour)\n    #plt.subplots_adjust(wspace=0, hspace=0)\n#plt.suptitle(f'Sample {dataset} images with prediction', fontsize=20)\nplt.tight_layout(pad=0)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:34:55.800166Z","iopub.execute_input":"2023-10-19T14:34:55.800484Z","iopub.status.idle":"2023-10-19T14:34:59.458929Z","shell.execute_reply.started":"2023-10-19T14:34:55.800457Z","shell.execute_reply":"2023-10-19T14:34:59.457863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Grad-Cam","metadata":{}},{"cell_type":"code","source":"class DenseNet(nn.Module):\n    def __init__(self, model):\n        super(DenseNet, self).__init__()\n        \n        # get the pretrained DenseNet201 network\n        self.densenet = model\n        \n        # disect the network to access its last convolutional layer\n        self.features_conv = self.densenet.features\n        \n        # add the average global pool\n        self.global_avg_pool = nn.AvgPool2d(kernel_size=16, stride=1)\n        \n        # get the classifier of the vgg19\n        self.classifier = self.densenet.classifier\n        \n        # placeholder for the gradients\n        self.gradients = None\n    \n    # hook for the gradients of the activations\n    def activations_hook(self, grad):\n        self.gradients = grad\n        \n    def forward(self, x):\n        x = self.features_conv(x)\n        print(x.shape)\n        # register the hook\n        h = x.register_hook(self.activations_hook)\n        \n        # don't forget the pooling\n        x = self.global_avg_pool(x)\n        x = x.ravel()\n        x = self.classifier(x)\n        return x\n    \n    def get_activations_gradient(self):\n        return self.gradients\n    \n    def get_activations(self, x):\n        return self.features_conv(x)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:10.237969Z","iopub.execute_input":"2023-10-19T14:35:10.238338Z","iopub.status.idle":"2023-10-19T14:35:10.245851Z","shell.execute_reply.started":"2023-10-19T14:35:10.238306Z","shell.execute_reply":"2023-10-19T14:35:10.244781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grad = DenseNet(densenet121_pretrained)\ngrad.eval()\nimg = torch.unsqueeze(test_ds[1][0].cuda(), dim=0)\npred = grad(img)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:14.639508Z","iopub.execute_input":"2023-10-19T14:35:14.63983Z","iopub.status.idle":"2023-10-19T14:35:14.676378Z","shell.execute_reply.started":"2023-10-19T14:35:14.639802Z","shell.execute_reply":"2023-10-19T14:35:14.675668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred[1].backward()\ngradients = grad.get_activations_gradient()\n\n# pool the gradients across the channels\npooled_gradients = torch.mean(gradients, dim=[0, 2, 3])\n\n# get the activations of the last convolutional layer\nactivations = grad.get_activations(img).detach()","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:18.224359Z","iopub.execute_input":"2023-10-19T14:35:18.225024Z","iopub.status.idle":"2023-10-19T14:35:18.346108Z","shell.execute_reply.started":"2023-10-19T14:35:18.224989Z","shell.execute_reply":"2023-10-19T14:35:18.345178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"activations.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:21.006046Z","iopub.execute_input":"2023-10-19T14:35:21.006914Z","iopub.status.idle":"2023-10-19T14:35:21.012204Z","shell.execute_reply.started":"2023-10-19T14:35:21.006879Z","shell.execute_reply":"2023-10-19T14:35:21.0113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1024):\n    activations[:, i, :, :] *= pooled_gradients[i]\n    \n# average the channels of the activations\nheatmap = torch.mean(activations, dim=1).squeeze()\n\n# relu on top of the heatmap\n# expression (2) in https://arxiv.org/pdf/1610.02391.pdf\nheatmap = torch.maximum(heatmap, torch.tensor([0]).cuda())\n\n# normalize the heatmap\nheatmap /= torch.max(heatmap)\n\n# draw the heatmap\nplt.matshow(heatmap.cpu().squeeze().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:23.791872Z","iopub.execute_input":"2023-10-19T14:35:23.792214Z","iopub.status.idle":"2023-10-19T14:35:24.215298Z","shell.execute_reply.started":"2023-10-19T14:35:23.792186Z","shell.execute_reply":"2023-10-19T14:35:24.214122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg = test_ds[1][0].numpy().transpose((1,2,0))\nheatmap = cv2.resize(heatmap.cpu().numpy(), (img.shape[1], img.shape[0]))\nheatmap = np.uint8(255 * heatmap)\nheatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)\nsuperimposed_img = heatmap * 0.4 + img\ncv2.imwrite('./map.jpg', superimposed_img)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:28.01707Z","iopub.execute_input":"2023-10-19T14:35:28.017783Z","iopub.status.idle":"2023-10-19T14:35:28.12126Z","shell.execute_reply.started":"2023-10-19T14:35:28.017748Z","shell.execute_reply":"2023-10-19T14:35:28.120283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = superimposed_img.astype(np.int32)\nprint(a.shape)\nplt.imshow(a)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:32.671667Z","iopub.execute_input":"2023-10-19T14:35:32.672774Z","iopub.status.idle":"2023-10-19T14:35:32.920749Z","shell.execute_reply.started":"2023-10-19T14:35:32.672722Z","shell.execute_reply":"2023-10-19T14:35:32.91984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test_ds[1][0].permute(1,2,0).numpy())\nplt.imshow(a, cmap='jet', alpha=0.5)\nprint(test_ds[1][1])","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:36.437951Z","iopub.execute_input":"2023-10-19T14:35:36.438672Z","iopub.status.idle":"2023-10-19T14:35:36.761528Z","shell.execute_reply.started":"2023-10-19T14:35:36.43864Z","shell.execute_reply":"2023-10-19T14:35:36.760707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(test_ds)):\n    if test_ds[i][1] ==1 and y_pred[i]==1:\n        print(i)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:41.093484Z","iopub.execute_input":"2023-10-19T14:35:41.094254Z","iopub.status.idle":"2023-10-19T14:35:45.161524Z","shell.execute_reply.started":"2023-10-19T14:35:41.094227Z","shell.execute_reply":"2023-10-19T14:35:45.160585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.random.seed(123)\nn_rows=6\nn_cols=3\n# indices = np.random.choice(range(len(test_ds)), n_rows*n_cols, \n                            # replace=False)\nfig= plt.figure(figsize=(30,30))\nfor i, ind in enumerate(indices):\n    fig.add_subplot(n_rows, n_cols, i+1)\n    plt.imshow(test_ds[ind][0].numpy().transpose(1,2,0),cmap='gray')\n    plt.axis('off')\n    proba = y_pred[ind].max()\n    pred = y_pred[ind].argmax()\n    if pred == y_gt[ind]:\n        colour = 'green'\n    else:\n        colour = 'red'\n    plt.title(f\"Prediction: {pred}, True: {test_ds[ind][1]} ({proba:.1%})\", fontsize=20, \n              color=colour)\n    #plt.subplots_adjust(wspace=0, hspace=0)\n#plt.suptitle(f'Sample {dataset} images with prediction', fontsize=20)\nplt.tight_layout(pad=0)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:45.163683Z","iopub.execute_input":"2023-10-19T14:35:45.164089Z","iopub.status.idle":"2023-10-19T14:35:48.861705Z","shell.execute_reply.started":"2023-10-19T14:35:45.164049Z","shell.execute_reply":"2023-10-19T14:35:48.860325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\ni = [110,186,263,410,429, 783]\n\nfigure, axis = plt.subplots(6,3)\nk=0\nfig= plt.figure(figsize=(30,30))\n\nfor j in i:\n    grad = DenseNet(densenet121_pretrained)\n    grad.eval()\n    img = torch.unsqueeze(test_ds[j][0].cuda(), dim=0)\n    pred = grad(img)\n    pred[1].backward()\n    gradients = grad.get_activations_gradient()\n\n    # pool the gradients across the channels\n    pooled_gradients = torch.mean(gradients, dim=[0, 2, 3])\n\n    # get the activations of the last convolutional layer\n    activations = grad.get_activations(img).detach()\n\n    for i in range(1024):\n        activations[:, i, :, :] *= pooled_gradients[i]\n    \n    # average the channels of the activations\n    heatmap = torch.mean(activations, dim=1).squeeze()\n\n    # relu on top of the heatmap\n    # expression (2) in https://arxiv.org/pdf/1610.02391.pdf\n    heatmap = torch.maximum(heatmap, torch.tensor([0]).cuda())\n\n    # normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # draw the heatmap\n    # plt.matshow(heatmap.cpu().squeeze().numpy())\n\n    \n\n    img = test_ds[j][0].numpy().transpose((1,2,0))\n    heatmap = cv2.resize(heatmap.cpu().numpy(), (img.shape[1], img.shape[0]))\n    heatmap = np.uint8(255 * heatmap)\n    heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)\n    superimposed_img = heatmap * 0.4 + img\n    # cv2.imwrite('./map_{0}.jpg', superimposed_img)\n    a = superimposed_img.astype(np.int32)\n    fig.add_subplot(n_rows, n_cols, k+1)\n    plt.imshow(test_ds[j][0].permute(1,2,0).numpy())\n    k+=1\n\n    fig.add_subplot(n_rows, n_cols, k+1)\n    plt.imshow(a, cmap='jet', alpha=0.5)\n    k+=1\n\n    fig.add_subplot(n_rows, n_cols, k+1)\n    plt.imshow(test_ds[j][0].permute(1,2,0).numpy())\n    plt.imshow(a, cmap='jet', alpha=0.5)\n    k+=1\n\n\n\nplt.tight_layout(pad=0)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:35:48.863374Z","iopub.execute_input":"2023-10-19T14:35:48.863977Z","iopub.status.idle":"2023-10-19T14:35:56.955674Z","shell.execute_reply.started":"2023-10-19T14:35:48.863938Z","shell.execute_reply":"2023-10-19T14:35:56.954448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n# Saving the model\ntorch.save(densenet121_pretrained.state_dict(), '/kaggle/working/models/densenet121_model.pt')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:55:08.842949Z","iopub.execute_input":"2023-10-19T14:55:08.8433Z","iopub.status.idle":"2023-10-19T14:55:08.96866Z","shell.execute_reply.started":"2023-10-19T14:55:08.84327Z","shell.execute_reply":"2023-10-19T14:55:08.96791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the entire model\ndensenet121_model = torch.load('/kaggle/working/models/densenet121_model.pt')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-19T14:49:28.546363Z","iopub.execute_input":"2023-10-19T14:49:28.546778Z","iopub.status.idle":"2023-10-19T14:49:28.645941Z","shell.execute_reply.started":"2023-10-19T14:49:28.546746Z","shell.execute_reply":"2023-10-19T14:49:28.645187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#densenet121_model","metadata":{"execution":{"iopub.status.busy":"2023-10-19T15:00:51.558667Z","iopub.execute_input":"2023-10-19T15:00:51.559011Z","iopub.status.idle":"2023-10-19T15:00:51.56334Z","shell.execute_reply.started":"2023-10-19T15:00:51.558986Z","shell.execute_reply":"2023-10-19T15:00:51.562277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\n# Save the model to a pickle file\nwith open(\"densenet121_model.pkl\", \"wb\") as f:\n    pickle.dump(densenet121_model, f)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T15:08:55.029872Z","iopub.execute_input":"2023-10-19T15:08:55.030193Z","iopub.status.idle":"2023-10-19T15:08:55.161716Z","shell.execute_reply.started":"2023-10-19T15:08:55.030164Z","shell.execute_reply":"2023-10-19T15:08:55.160749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\n# Save the model using pickle\nwith open('/kaggle/working/models/densenet121_model.pkl', 'wb') as f:\n    pickle.dump(densenet121_model, f)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T15:02:59.265051Z","iopub.execute_input":"2023-10-19T15:02:59.265466Z","iopub.status.idle":"2023-10-19T15:02:59.430962Z","shell.execute_reply.started":"2023-10-19T15:02:59.265404Z","shell.execute_reply":"2023-10-19T15:02:59.429835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\n\n# Create a download link for your pickle file\nFileLink(\"densenet121_model.pkl\")","metadata":{"execution":{"iopub.status.busy":"2023-10-19T15:09:01.608335Z","iopub.execute_input":"2023-10-19T15:09:01.608727Z","iopub.status.idle":"2023-10-19T15:09:01.615447Z","shell.execute_reply.started":"2023-10-19T15:09:01.608697Z","shell.execute_reply":"2023-10-19T15:09:01.614477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\n\n# Specify the path to the pickle file\nfile_path = 'densenet121_model.pkl'\n\n# Load the model from the pickle file\nwith open(file_path, 'rb') as f:\n    loaded_model = pickle.load(f)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-19T15:10:29.112472Z","iopub.execute_input":"2023-10-19T15:10:29.112802Z","iopub.status.idle":"2023-10-19T15:10:29.259087Z","shell.execute_reply.started":"2023-10-19T15:10:29.112777Z","shell.execute_reply":"2023-10-19T15:10:29.258277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model","metadata":{"execution":{"iopub.status.busy":"2023-10-19T15:10:40.939024Z","iopub.execute_input":"2023-10-19T15:10:40.939863Z","iopub.status.idle":"2023-10-19T15:10:45.072821Z","shell.execute_reply.started":"2023-10-19T15:10:40.93983Z","shell.execute_reply":"2023-10-19T15:10:45.07153Z"},"trusted":true},"execution_count":null,"outputs":[]}]}