{"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":"%%capture\n!pip install -qU python-gdcm pydicom pylibjpeg\n!pip install -U pylibjpeg-libjpeg -v\n!pip install pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg\n!pip install pydicom","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:06.959602Z","iopub.execute_input":"2023-02-11T13:18:06.960126Z","iopub.status.idle":"2023-02-11T13:18:56.752202Z","shell.execute_reply.started":"2023-02-11T13:18:06.960075Z","shell.execute_reply":"2023-02-11T13:18:56.750612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport torch \nfrom torch import nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom torch.optim import Adam\nimport pylibjpeg\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport glob\nfrom tqdm import tqdm,trange,tqdm_notebook\nfrom PIL import Image\nimport torchvision\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:56.755528Z","iopub.execute_input":"2023-02-11T13:18:56.756386Z","iopub.status.idle":"2023-02-11T13:18:56.765856Z","shell.execute_reply.started":"2023-02-11T13:18:56.756326Z","shell.execute_reply":"2023-02-11T13:18:56.764432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\nprint('The model will be running on', device, 'device')","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:56.767669Z","iopub.execute_input":"2023-02-11T13:18:56.770205Z","iopub.status.idle":"2023-02-11T13:18:56.803168Z","shell.execute_reply.started":"2023-02-11T13:18:56.770169Z","shell.execute_reply":"2023-02-11T13:18:56.801648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\",dtype={'image_id':str})\ntest_meta = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")\nsample_submission_meta = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:56.806989Z","iopub.execute_input":"2023-02-11T13:18:56.807774Z","iopub.status.idle":"2023-02-11T13:18:56.923308Z","shell.execute_reply.started":"2023-02-11T13:18:56.807722Z","shell.execute_reply":"2023-02-11T13:18:56.921958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:56.9263Z","iopub.execute_input":"2023-02-11T13:18:56.92713Z","iopub.status.idle":"2023-02-11T13:18:56.946064Z","shell.execute_reply.started":"2023-02-11T13:18:56.927082Z","shell.execute_reply":"2023-02-11T13:18:56.944525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = glob.glob('/kaggle/input/rsnadata/Dataset/*/*.png')\nlen(train_image)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:56.94871Z","iopub.execute_input":"2023-02-11T13:18:56.949274Z","iopub.status.idle":"2023-02-11T13:18:57.152458Z","shell.execute_reply.started":"2023-02-11T13:18:56.94922Z","shell.execute_reply":"2023-02-11T13:18:57.151318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split Cancer, Nocancer Dataframe\ncancer_list = train_meta[train_meta['cancer']==1]['image_id'].tolist()\nlen(cancer_list)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:57.154053Z","iopub.execute_input":"2023-02-11T13:18:57.154916Z","iopub.status.idle":"2023-02-11T13:18:57.170309Z","shell.execute_reply.started":"2023-02-11T13:18:57.154876Z","shell.execute_reply":"2023-02-11T13:18:57.169084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_path_cancer = []\nlist_path_nocancer = []\nfor uid in tqdm(train_image):\n    image_id = uid.split('/')[-1].split('.')[0]\n    if image_id in cancer_list:\n        list_path_cancer.append(uid)\n    else: list_path_nocancer.append(uid)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:57.172853Z","iopub.execute_input":"2023-02-11T13:18:57.173818Z","iopub.status.idle":"2023-02-11T13:18:58.71078Z","shell.execute_reply.started":"2023-02-11T13:18:57.173767Z","shell.execute_reply":"2023-02-11T13:18:58.709608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(list_path_cancer), len(list_path_nocancer)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.712402Z","iopub.execute_input":"2023-02-11T13:18:58.713635Z","iopub.status.idle":"2023-02-11T13:18:58.72246Z","shell.execute_reply.started":"2023-02-11T13:18:58.713586Z","shell.execute_reply":"2023-02-11T13:18:58.721211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_path_cancer[0],list_path_nocancer[0]","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.730126Z","iopub.execute_input":"2023-02-11T13:18:58.730509Z","iopub.status.idle":"2023-02-11T13:18:58.741275Z","shell.execute_reply.started":"2023-02-11T13:18:58.730476Z","shell.execute_reply":"2023-02-11T13:18:58.740104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_df, nocancer = train_meta[train_meta[\"cancer\"] == 1], train_meta[train_meta[\"cancer\"] == 0][:1000]\n#concatenate the two dataframes\ndata_split = pd.concat([cancer_df, nocancer], axis=0).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.743139Z","iopub.execute_input":"2023-02-11T13:18:58.743948Z","iopub.status.idle":"2023-02-11T13:18:58.769041Z","shell.execute_reply.started":"2023-02-11T13:18:58.743903Z","shell.execute_reply":"2023-02-11T13:18:58.767756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = data_split.sample(frac=0.7, random_state=42).reset_index(drop=True)\nval_df = data_split.sample(frac=0.2, random_state=42).reset_index(drop=True)\ntest_df = data_split.sample(frac=0.1, random_state=42).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.771149Z","iopub.execute_input":"2023-02-11T13:18:58.771963Z","iopub.status.idle":"2023-02-11T13:18:58.7842Z","shell.execute_reply.started":"2023-02-11T13:18:58.771913Z","shell.execute_reply":"2023-02-11T13:18:58.783073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape, val_df.shape, test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.786231Z","iopub.execute_input":"2023-02-11T13:18:58.787118Z","iopub.status.idle":"2023-02-11T13:18:58.795004Z","shell.execute_reply.started":"2023-02-11T13:18:58.78708Z","shell.execute_reply":"2023-02-11T13:18:58.793566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.iloc[1]","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.799574Z","iopub.execute_input":"2023-02-11T13:18:58.799908Z","iopub.status.idle":"2023-02-11T13:18:58.809348Z","shell.execute_reply.started":"2023-02-11T13:18:58.799878Z","shell.execute_reply":"2023-02-11T13:18:58.807916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# root_dir = '/kaggle/input/rsnadata/Dataset'\n# image_paths,labels = [], []\n# row = train_df.iloc[1]\n# for label in ['cancer', 'nocancer']:\n#     label_dir = os.path.join(root_dir, label)\n#     for file in os.listdir(label_dir):\n#         if (file.split('.')[0] == str(row['image_id'])):\n#             image_paths.append(os.path.join(label_dir, file))\n# print(image_paths)\n# #         labels.append(label)\n# #         if labels.count('nocancer') > 1157:\n# #             break","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.811594Z","iopub.execute_input":"2023-02-11T13:18:58.812406Z","iopub.status.idle":"2023-02-11T13:18:58.81837Z","shell.execute_reply.started":"2023-02-11T13:18:58.812362Z","shell.execute_reply":"2023-02-11T13:18:58.817094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the root directory for the dataset\n\ndef get_transform(is_train):\n    if is_train:\n        return torchvision.transforms.Compose([\n            transforms.Grayscale(num_output_channels=3),\n            transforms.Resize((224, 224)),\n            transforms.RandomHorizontalFlip(),\n            transforms.RandomRotation(10),\n            transforms.ToTensor(),\n            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n        ])\n    else:\n        return transforms.Compose([\n            transforms.Grayscale(num_output_channels=3),\n            transforms.Resize((224, 224)),\n            transforms.ToTensor(),\n            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n        ])","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.820535Z","iopub.execute_input":"2023-02-11T13:18:58.821514Z","iopub.status.idle":"2023-02-11T13:18:58.831424Z","shell.execute_reply.started":"2023-02-11T13:18:58.821468Z","shell.execute_reply":"2023-02-11T13:18:58.830135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BreastCancerDataset(torch.utils.data.Dataset):\n    def __init__(self,dataframe,is_train):\n        self.dataframe = dataframe\n        self.is_train = is_train\n        self.tfms = get_transform(self.is_train)\n\n    def __len__(self):\n        return len(self.dataframe)\n\n    def __getitem__(self, idx):\n        row = self.dataframe.iloc[idx]\n        label = row[\"cancer\"]\n#         root_dir = ''\n        if label == 1:\n            root_dir = '/kaggle/input/rsnadata/Dataset/cancer/'\n            path = root_dir + str(row['image_id'])+'.png'\n        else:\n            root_dir = '/kaggle/input/rsnadata/Dataset/nocancer/'\n            path = root_dir + str(row['image_id'])+'.png'\n        image = Image.open(path)\n        image = self.tfms(image)\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.834367Z","iopub.execute_input":"2023-02-11T13:18:58.835423Z","iopub.status.idle":"2023-02-11T13:18:58.846353Z","shell.execute_reply.started":"2023-02-11T13:18:58.835376Z","shell.execute_reply":"2023-02-11T13:18:58.84528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class BreastCancerDataset(torch.utils.data.Dataset):\n#     def __init__(self, root_dir, is_train):\n#         self.root_dir = root_dir\n#         self.is_train = is_train\n#         self.image_paths = []\n#         self.labels = []\n#         self.tfms = get_transform(self.is_train)\n\n#         # Load the image paths and labels\n#         for label in ['cancer', 'nocancer']:\n#             label_dir = os.path.join(root_dir, label)\n#             for file in os.listdir(label_dir):\n#                 self.image_paths.append(os.path.join(label_dir, file))\n#                 self.labels.append(label)\n#                 if self.labels.count('nocancer') > 1157:\n#                     break\n#     def __len__(self):\n#         return len(self.image_paths)\n\n#     def __getitem__(self, idx):\n#         image = Image.open(self.image_paths[idx])\n#         if self.labels[idx] == 'cancer':\n#             label = 1\n#         else:\n#             label = 0\n#         image = self.tfms(image)\n#         return image, label","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.848555Z","iopub.execute_input":"2023-02-11T13:18:58.849485Z","iopub.status.idle":"2023-02-11T13:18:58.857677Z","shell.execute_reply.started":"2023-02-11T13:18:58.849433Z","shell.execute_reply":"2023-02-11T13:18:58.856771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class BreastCancerDataset(torch.utils.data.Dataset):\n#     def __init__(self, root_dir, is_train):\n#         self.root_dir = root_dir\n#         self.is_train = is_train\n#         self.image_paths = []\n#         self.labels = []\n#         self.tfms = get_transform(self.is_train)\n\n#         # Load the image paths and labels\n#         for label in ['cancer', 'nocancer']:\n#             label_dir = os.path.join(root_dir, label)\n#             for file in os.listdir(label_dir):\n#                 self.image_paths.append(os.path.join(label_dir, file))\n#                 self.labels.append(label)\n#                 if self.labels.count('nocancer') > 1157:\n#                     break\n#     def __len__(self):\n#         return len(self.image_paths)\n\n#     def __getitem__(self, idx):\n#         image = Image.open(self.image_paths[idx])\n#         if self.labels[idx] == 'cancer':\n#             label = 1\n#         else:\n#             label = 0\n#         image = self.tfms(image)\n#         return image, label","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.859605Z","iopub.execute_input":"2023-02-11T13:18:58.860479Z","iopub.status.idle":"2023-02-11T13:18:58.871078Z","shell.execute_reply.started":"2023-02-11T13:18:58.860431Z","shell.execute_reply":"2023-02-11T13:18:58.870161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = BreastCancerDataset(dataframe =train_df, is_train=True)\nval_dataset = BreastCancerDataset(dataframe =val_df, is_train=False)\ntest_dataset = BreastCancerDataset(dataframe =test_df, is_train=False)\n\ntrain_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True)\nval_dataloader = DataLoader(val_dataset, batch_size=64, shuffle=False)\ntest_dataloader = DataLoader(test_dataset, batch_size=64, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.872933Z","iopub.execute_input":"2023-02-11T13:18:58.873927Z","iopub.status.idle":"2023-02-11T13:18:58.883592Z","shell.execute_reply.started":"2023-02-11T13:18:58.873881Z","shell.execute_reply":"2023-02-11T13:18:58.882414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # # Create the dataset\n# # dataset = BreastCancerDataset(root_dir=root_dir, is_train=True)\n# # # Split the dataset into train, val, and test sets\n# # num_samples = len(dataset)\n# # train_size = int(0.7 * num_samples)\n# # val_size = int(0.2 * num_samples)\n# # test_size = num_samples - train_size - val_size\n\n\n# train_dataset = BreastCancerDataset(root_dir=root_dir, is_train=True)\n# val_dataset = BreastCancerDataset(root_dir=root_dir, is_train=False)\n# test_dataset = BreastCancerDataset(root_dir=root_dir, is_train=False)\n\n# #Create tran, val and test dataloader\n# train_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True)\n# val_dataloader = DataLoader(val_dataset, batch_size=64, shuffle=False)\n# test_dataloader = DataLoader(test_dataset, batch_size=64, shuffle=False)\n\n# # train_dataset, val_dataset, test_dataset = torch.utils.data.random_split(dataset, [train_size, val_size, test_size])\n\n# # #Create tran, val and test dataloader\n","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.887313Z","iopub.execute_input":"2023-02-11T13:18:58.887672Z","iopub.status.idle":"2023-02-11T13:18:58.897182Z","shell.execute_reply.started":"2023-02-11T13:18:58.887644Z","shell.execute_reply":"2023-02-11T13:18:58.896087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Create tran, val and test dataloader\n# train_dataloader = DataLoader(train_dataset(is_train=True), batch_size=64, \n#                             shuffle=True)\n# val_dataloader = DataLoader(val_dataset(is_train=False), batch_size=64, \n#                             shuffle=False)\n# test_dataloader = DataLoader(test_dataset(is_train=False), batch_size=64, \n#                              shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.89905Z","iopub.execute_input":"2023-02-11T13:18:58.899406Z","iopub.status.idle":"2023-02-11T13:18:58.907849Z","shell.execute_reply.started":"2023-02-11T13:18:58.899372Z","shell.execute_reply":"2023-02-11T13:18:58.906607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:18:58.909898Z","iopub.execute_input":"2023-02-11T13:18:58.910456Z","iopub.status.idle":"2023-02-11T13:19:10.946797Z","shell.execute_reply.started":"2023-02-11T13:18:58.910362Z","shell.execute_reply":"2023-02-11T13:19:10.945325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet_pytorch import EfficientNet\n# model = EfficientNet.from_pretrained('efficientnet-b0')\n\nclass MyEfficientNet(nn.Module):\n\n    def __init__(self):\n        super().__init__()\n\n        # EfficientNet\n        self.network = EfficientNet.from_pretrained(\"efficientnet-b0\")\n        \n        # Replace last layer\n        self.network._fc = nn.Sequential(nn.Linear(self.network._fc.in_features, 512), \n                                         nn.ReLU(),  \n                                         nn.Dropout(0.25),\n                                         nn.Linear(512, 128), \n                                         nn.ReLU(),  \n                                         nn.Dropout(0.50), \n                                         nn.Linear(128,2))\n    \n    def forward(self, x):\n        out = self.network(x)\n        return out\n\nmodel = MyEfficientNet()","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:19:10.949406Z","iopub.execute_input":"2023-02-11T13:19:10.949922Z","iopub.status.idle":"2023-02-11T13:19:12.616369Z","shell.execute_reply.started":"2023-02-11T13:19:10.949867Z","shell.execute_reply":"2023-02-11T13:19:12.615087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import torch.optim as optim\n# model = torchvision.models.efficientnet_b0(pretrained=True)\n\n# # Freeze the model weights\n# for param in model.parameters():\n#     param.requires_grad = False\n# # Replace the fully connected layer with one suited to your classification task\n# num_classes = 2\n# model._fc = torch.nn.Linear(model.fc.in_features, num_classes)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:19:12.618153Z","iopub.execute_input":"2023-02-11T13:19:12.618642Z","iopub.status.idle":"2023-02-11T13:19:12.625104Z","shell.execute_reply.started":"2023-02-11T13:19:12.618592Z","shell.execute_reply":"2023-02-11T13:19:12.623754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:19:12.627088Z","iopub.execute_input":"2023-02-11T13:19:12.627563Z","iopub.status.idle":"2023-02-11T13:19:12.646225Z","shell.execute_reply.started":"2023-02-11T13:19:12.62752Z","shell.execute_reply":"2023-02-11T13:19:12.644702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the loss function\ncriterion = nn.CrossEntropyLoss()\n# Define the optimizer\noptimizer = Adam(model.parameters(),lr=1e-3)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:19:12.648254Z","iopub.execute_input":"2023-02-11T13:19:12.649955Z","iopub.status.idle":"2023-02-11T13:19:12.659008Z","shell.execute_reply.started":"2023-02-11T13:19:12.649889Z","shell.execute_reply":"2023-02-11T13:19:12.657733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = '/kaggle/working/EfficientNet.pt'\nlist_acc_train, list_acc_val, list_loss_train, list_loss_val= [],[],[],[]\nnum_epochs = 50\nbest_f1 = 0.0\nprint('Begin Training')\nfor epoch in tqdm((range(1, num_epochs+1))):\n    model.train()  # set model to training mode\n    train_loss = 0.0\n    train_acc = 0.0\n    total = 0\n    for inputs, labels in train_dataloader:\n        optimizer.zero_grad()\n        model.to(device)\n        inputs, labels = inputs.to(device), labels.to(device)\n        outputs = model(inputs)\n#         forward pass\n        loss = criterion(outputs, labels)  # compute loss\n        loss.backward()  # backpropagate to compute gradients\n        optimizer.step()  # update model weights\n        train_loss += loss.item()\n        predicted = torch.max(outputs, 1)[1].to(device)\n        total += labels.size(0)\n        train_acc += (predicted == labels).sum().item()\n\n    train_acc /= total\n    train_loss /= len(train_dataloader)\n    list_loss_train.append(train_loss)\n    list_acc_train.append(train_acc)\n    \n    model.eval()  # set model to evaluation mode\n    \n    with torch.no_grad():\n        val_loss = 0.0\n        val_acc = 0.0\n        f1_scr = 0.0\n        total = 0.0\n        label_list, label_pred_list = [],[]\n        for inputs, labels in val_dataloader:\n            inputs, labels = inputs.to(device), labels.to(device)\n            outputs = model(inputs)\n            loss = criterion(outputs, labels) # compute loss\n            val_loss += loss.item()\n            predicted = torch.max(outputs, 1)[1].to(device)\n            total += labels.size(0)\n            val_acc += (predicted == labels).sum().item()\n#             label_prob_list.append(outputs) #auc score\n            label_list.append(labels)\n            label_pred_list.append(predicted)\n            # update validation F1 score\n        val_acc =  val_acc/total\n        label_list, label_pred_list = torch.cat(label_list, 0), torch.cat(label_pred_list, 0)\n        f1_scr = f1_score(label_list.cpu(), label_pred_list.cpu(), average = 'macro')\n        val_loss /= len(val_dataloader)\n        list_loss_val.append(val_loss)\n        list_acc_val.append(val_acc)\n    print(f'Epoch {epoch+1:2d}/{num_epochs}: train_loss = {train_loss:.3f}, train_acc = {train_acc:.3f}, val_loss = {val_loss:.3f}, val_acc = {val_acc:.3f}')\n    if f1_scr > best_f1:  # if the current validation F1 score is better than the best F1 score so far\n        best_f1 = f1_scr  # update the best F1 scor\n        state = {'epoch': epoch+1, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}\n        torch.save(state, checkpoint_path)  # save the model checkpoint\n\nhistory = {'loss_train':list_loss_train,'acc_train':list_acc_train,\n           'loss_val':list_loss_val, 'acc_val':list_acc_val}\ndf_history = pd.DataFrame(history)\ndf_history.to_csv('/kaggle/working/history.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:19:12.668382Z","iopub.execute_input":"2023-02-11T13:19:12.668716Z","iopub.status.idle":"2023-02-11T13:36:49.990207Z","shell.execute_reply.started":"2023-02-11T13:19:12.668686Z","shell.execute_reply":"2023-02-11T13:36:49.988848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    y_true = []\n    y_pred = []\n    model.to('cpu')\n    for inputs, labels in test_dataloader:\n        outputs = model(inputs)\n        _, predicted = torch.max(outputs.data, 1)\n        y_true += labels.tolist()\n        y_pred += predicted.tolist()\n\n# Calculate evaluation metrics\naccuracy = accuracy_score(y_true, y_pred)\nprecision = precision_score(y_true, y_pred, average='weighted')\nrecall = recall_score(y_true, y_pred, average='weighted')\nf1 = f1_score(y_true, y_pred, average='weighted')\n\n# Print the results\nprint(f'Accuracy: {accuracy:.2f}')\nprint(f'Precision: {precision:.2f}')\nprint(f'Recall: {recall:.2f}')\nprint(f'F1 score: {f1:.2f}')","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:36:49.992491Z","iopub.execute_input":"2023-02-11T13:36:49.993436Z","iopub.status.idle":"2023-02-11T13:37:28.1899Z","shell.execute_reply.started":"2023-02-11T13:36:49.993385Z","shell.execute_reply":"2023-02-11T13:37:28.188544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    y_true = []\n    y_pred = []\n    model.to('cpu')\n    for inputs, labels in test_dataloader:\n        outputs = model(inputs)\n        _, predicted = torch.max(outputs.data, 1)\n        y_true += labels.tolist()\n        y_pred += predicted.tolist()\n\n\ntarget_names = ['cancer', 'nocancer']\nprint(classification_report(y_true, y_pred, target_names=target_names))","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:37:28.191905Z","iopub.execute_input":"2023-02-11T13:37:28.192745Z","iopub.status.idle":"2023-02-11T13:38:05.767496Z","shell.execute_reply.started":"2023-02-11T13:37:28.192692Z","shell.execute_reply":"2023-02-11T13:38:05.766033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nconfusion_matrix(y_true, y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.769505Z","iopub.execute_input":"2023-02-11T13:38:05.773409Z","iopub.status.idle":"2023-02-11T13:38:05.785196Z","shell.execute_reply.started":"2023-02-11T13:38:05.773364Z","shell.execute_reply":"2023-02-11T13:38:05.783532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n# def training(model,num_epochs):\n#     checkpoint_path = '/kaggle/working/resnet18.pt'\n#     list_acc_train, list_acc_val,list_loss_train,list_loss_val,label_prob_list = [],[],[],[],[]\n    \n#     for epoch in tqdm((range(1, num_epochs+1))):\n#         model.train()  # set model to training mode\n#         train_loss = 0.0\n#         train_acc = 0.0\n#         for batch_idx, data in enumerate(train_dataloader):\n#             optimizer.zero_grad()\n#             model = model.to(device)\n#             # zero out the gradients from the previous step\n#             inputs, labels = data[0].to(device), data[1].to(device)\n#             outputs = model(inputs)  # forward pass\n#             loss = criterion(outputs, labels)  # compute loss\n#             loss.backward()  # backpropagate to compute gradients\n#             optimizer.step()  # update model weights\n#             train_loss += loss.item()  # update training loss\n#             label_hat = torch.max(outputs,1)[1].to(device) \n#             train_acc += label_hat.float().sum().item()\n#     #         train_f1 += f1_score(label_hat.cpu(), labels.cpu(), average='micro')\n\n#         train_loss /= len(train_dataloader)  # average training loss for the epoch\n#         train_acc /= len(train_dataloader)# average training accuracy for the epoch\n#         list_loss_train.append(train_loss)\n#         list_acc_train.append(train_acc)\n        \n#         model.eval()  # set model to evaluation mode\n#         val_loss = 0.0\n#         val_acc = 0.0\n        \n#         label_prob_list, label_hat, label_list, label_pred_list = [],[],[],[]\n#         with torch.no_grad():  # disable gradient computation for evaluation\n#             for i, data in enumerate(val_dataloader):\n#                 inputs, labels = data[0].to(device), data[1].to(device)\n#                 outputs = model(inputs)  # forward pass\n#                 loss = criterion(outputs, labels)  # compute loss\n#                 #f1-score\n#                 label_prob_list.append(outputs) #auc score\n#                 label_hat = torch.max(outputs,1)[1].to(device) \n#                 label_list.append(labels)\n#                 label_pred_list.append(label_hat)\n\n#                 val_acc += label_hat.float().sum().item()  # update validation accuracy\n#                 val_loss += loss.item()  # update validation loss\n\n#         label_list, label_pred_list = torch.cat(label_list, 0), torch.cat(label_pred_list, 0)\n#         val_precision = precision_score(label_list.cpu(), label_pred_list.cpu())\n#         val_recall = recall_score(label_list.cpu(), label_pred_list.cpu())\n        \n#         val_loss /= len(val_dataloader)  # average validation loss for the epoch\n#         val_acc /= len(val_dataloader)  # average validation accuracy for the epoch\n        \n#         list_loss_val.append(val_loss)\n#         list_acc_val.append(val_acc)\n        \n#         print(f'Epoch {epoch+1:2d}/{num_epochs}: train_loss = {train_loss:.3f}, train_acc = {train_acc:.3f}, val_loss = {val_loss:.3f}, val_acc = {val_acc:.3f}')\n#         if val_f1 > best_f1:  # if the current validation F1 score is better than the best F1 score so far\n#             best_f1 = val_f1  # update the best F1 scor\n#             state = {'epoch': epoch+1, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}\n#             torch.save(state, checkpoint_path)  # save the model checkpoint\n            \n#     history = {'loss_train':list_loss_train,'acc_train':list_acc_train,\n#                'loss_val':list_loss_val, 'acc_val':list_acc_val}\n#     df_history = pd.DataFrame(history)\n#     df_history.to_csv('/kaggle/working/history.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.787646Z","iopub.execute_input":"2023-02-11T13:38:05.78817Z","iopub.status.idle":"2023-02-11T13:38:05.800842Z","shell.execute_reply.started":"2023-02-11T13:38:05.788121Z","shell.execute_reply":"2023-02-11T13:38:05.798112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# num_epochs = 100\n# training(model,num_epochs)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.802901Z","iopub.execute_input":"2023-02-11T13:38:05.803493Z","iopub.status.idle":"2023-02-11T13:38:05.813653Z","shell.execute_reply.started":"2023-02-11T13:38:05.803447Z","shell.execute_reply":"2023-02-11T13:38:05.812523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checkpoint_path = \"/kaggle/working/model_resnet.pt\"\n# for epoch in tqdm((range(1, num_epochs+1))):\n#     model.train()  # set model to training mode\n#     train_loss = 0.0\n#     train_acc = 0.0\n#     train_f1 = 0.0\n#     for batch_idx, data in enumerate(train_dataloader):\n#         optimizer.zero_grad()\n#         model = model.to(device)\n#         # zero out the gradients from the previous step\n#         inputs, labels = data[0].to(device), data[1].to(device)\n#         outputs = model(inputs)  # forward pass\n        \n#         loss = criterion(outputs, labels)  # compute loss\n#         loss.backward()  # backpropagate to compute gradients\n#         optimizer.step()  # update model weights\n        \n#         train_loss += loss.item()  # update training loss\n#         label_hat = torch.max(outputs,1)[1].to(device) \n#         train_acc += label_hat.float().mean().item()\n#         train_f1 += f1_score(label_hat.cpu(), labels.cpu(), average='micro')\n        \n# #         if batch_idx % 10 == 0:\n# #                 print('Train Epoch: {} [{}/{} ({:.0f}%)]\\tLoss: {:.6f}'.format(\n# #                     epoch, batch_idx * len(inputs), len(train_dataloader.dataset),\n# #                     100. * batch_idx / len(train_dataloader), loss.item()))\n# #                 saveModel()\n#         # compute F1 score\n#     train_loss /= len(train_dataloader)  # average training loss for the epoch\n#     train_acc /= len(train_dataloader)  # average training accuracy for the epoch\n#     list_loss_train.append(train_loss)\n#     list_acc_train.append(train_acc)\n#     train_f1 /= len(train_dataloader)  # average training F1 score for the epoch\n    \n    \n#     model.eval()  # set model to evaluation mode\n#     val_loss = 0.0\n#     val_acc = 0.0\n#     val_f1 = 0.0\n#     label_prob_list, label_hat, label_list, label_pred_list = [],[],[],[]\n#     with torch.no_grad():  # disable gradient computation for evaluation\n#         for i, data in enumerate(val_dataloader):\n#             inputs, labels = data[0].to(device), data[1].to(device)\n#             outputs = model(inputs)  # forward pass\n#             loss = criterion(outputs, labels)  # compute loss\n#             #f1-score\n#             label_prob_list.append(outputs) #auc score\n#             label_hat = torch.max(outputs,1)[1].to(device) \n#             label_list.append(labels)\n#             label_pred_list.append(label_hat)\n            \n#             val_acc += label_hat.float().mean().item()  # update validation accuracy\n#             val_loss += loss.item()  # update validation loss\n            \n#     label_list, label_pred_list = torch.cat(label_list, 0), torch.cat(label_pred_list, 0)\n#     val_f1 = pre\n#     val_f1 = f1_score(label_list.cpu(), label_pred_list.cpu(), average = 'macro')\n    \n#     val_loss /= len(val_dataloader)  # average validation loss for the epoch\n#     val_acc /= len(val_dataloader)  # average validation accuracy for the epoch\n#     list_loss_val.append(val_loss)\n#     list_acc_val.append(val_acc)\n\n    \n\n#     print(f'Epoch {epoch+1:2d}/{num_epochs}: train_loss = {train_loss:.3f}, train_acc = {train_acc:.3f}, train_f1 = {train_f1:.3f}, val_loss = {val_loss:.3f}, val_acc = {val_acc:.3f}, val_f1 = {val_f1:.3f}')\n#     if val_f1 > best_f1:  # if the current validation F1 score is better than the best F1 score so far\n#         best_f1 = val_f1  # update the best F1 scor\n#         state = {'epoch': epoch+1, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}\n#         torch.save(state, checkpoint_path)  # save the model checkpoint ","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.81568Z","iopub.execute_input":"2023-02-11T13:38:05.816153Z","iopub.status.idle":"2023-02-11T13:38:05.826963Z","shell.execute_reply.started":"2023-02-11T13:38:05.816111Z","shell.execute_reply":"2023-02-11T13:38:05.825796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class BreastCancerDataset(Dataset):\n#     def __init__(self, dataframe):\n#         self.dataframe = dataframe\n#     def __len__(self):\n#         return len(self.dataframe)\n    \n#     def __getitem__(self, idx):\n#         row = self.dataframe.iloc[idx]\n#         input_path = '/kaggle/input/rsna-breast-cancer-detection/train_images/'\n#         label = row[\"cancer\"]\n#         path = input_path + str(row['patient_id'])+'/'+str(row['image_id'])+'.dcm'\n#         #Read file Dicom\n#         dicom = pydicom.dcmread(path)\n#         img = dicom.pixel_array\n#         img = (img - img.min()) / (img.max() - img.min())\n#         if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n#             img = 1 - img\n#         img = (img * 255).astype(np.uint8)\n#         X = img\n#         X = X[5:-5, 5:-5]\n#         output= cv2.connectedComponentsWithStats((X > 30).astype(np.uint8)[:, :], 8, cv2.CV_32S)\n#         stats = output[2]\n#         idx = stats[1:, 4].argmax() + 1\n#         x1, y1, w, h = stats[idx][:4]\n#         x2 = x1 + w\n#         y2 = y1 + h\n#         # cutting out the breast data\n#         X_fit = X[y1: y2, x1: x2]\n#         Image = cv2.cvtColor(X_fit, cv2.COLOR_GRAY2BGR)\n#         Image = cv2.resize(Image,(224,224))\n#         transform = transforms.Compose([transforms.ToTensor()])\n#         Image = transform(Image)\n#         sample = {\"image\": Image, \"label\": label}\n#         return sample","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.828522Z","iopub.execute_input":"2023-02-11T13:38:05.828906Z","iopub.status.idle":"2023-02-11T13:38:05.842855Z","shell.execute_reply.started":"2023-02-11T13:38:05.828864Z","shell.execute_reply":"2023-02-11T13:38:05.841489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class BreastCancerDataset(Dataset):\n#     def __init__(self, dataframe):\n#         self.dataframe = dataframe\n#     def __len__(self):\n#         return len(self.dataframe)\n    \n#     def __getitem__(self, idx):\n#         row = self.dataframe.iloc[idx]\n#         input_path = '/kaggle/input/rsna-breast-cancer-detection/train_images/'\n#         label = row[\"cancer\"]\n#         path = input_path + str(row['patient_id'])+'/'+str(row['image_id'])+'.dcm'\n#         #Read file Dicom\n#         dicom = pydicom.dcmread(path)\n#         img = dicom.pixel_array\n#         img = (img - img.min()) / (img.max() - img.min())\n#         if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n#             img = 1 - img\n#         img = (img * 255).astype(np.uint8)\n#         X = img\n#         X = X[5:-5, 5:-5]\n#         output= cv2.connectedComponentsWithStats((X > 30).astype(np.uint8)[:, :], 8, cv2.CV_32S)\n#         stats = output[2]\n#         idx = stats[1:, 4].argmax() + 1\n#         x1, y1, w, h = stats[idx][:4]\n#         x2 = x1 + w\n#         y2 = y1 + h\n#         # cutting out the breast data\n#         X_fit = X[y1: y2, x1: x2]\n#         Image = cv2.cvtColor(X_fit, cv2.COLOR_GRAY2BGR)\n#         Image = cv2.resize(Image,(224,224))\n#         transform = transforms.Compose([transforms.ToTensor()])\n#         Image = transform(Image)\n#         sample = {\"image\": Image, \"label\": label}\n#         return sample","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.844412Z","iopub.execute_input":"2023-02-11T13:38:05.844818Z","iopub.status.idle":"2023-02-11T13:38:05.856566Z","shell.execute_reply.started":"2023-02-11T13:38:05.844784Z","shell.execute_reply":"2023-02-11T13:38:05.855372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #create train and validation dataset\n# train_loader = DataLoader(BreastCancerDataset(train_df),batch_size=16, shuffle=True)\n# val_loader = DataLoader(BreastCancerDataset(val_df),batch_size=16, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.858167Z","iopub.execute_input":"2023-02-11T13:38:05.859586Z","iopub.status.idle":"2023-02-11T13:38:05.869548Z","shell.execute_reply.started":"2023-02-11T13:38:05.859533Z","shell.execute_reply":"2023-02-11T13:38:05.868088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classes = [\"cancer\", \"no cancer\"]","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.87173Z","iopub.execute_input":"2023-02-11T13:38:05.872537Z","iopub.status.idle":"2023-02-11T13:38:05.880961Z","shell.execute_reply.started":"2023-02-11T13:38:05.872491Z","shell.execute_reply":"2023-02-11T13:38:05.879731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def saveModel():\n#     path = \"/kaggle/working/model.pt\"\n#     torch.save(model.state_dict(), path)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.883303Z","iopub.execute_input":"2023-02-11T13:38:05.883824Z","iopub.status.idle":"2023-02-11T13:38:05.891917Z","shell.execute_reply.started":"2023-02-11T13:38:05.883781Z","shell.execute_reply":"2023-02-11T13:38:05.890722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = monai.networks.nets.EfficientNetBN(\"efficientnet-b0\",pretrained=True, num_classes=2).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.894158Z","iopub.execute_input":"2023-02-11T13:38:05.894858Z","iopub.status.idle":"2023-02-11T13:38:05.90216Z","shell.execute_reply.started":"2023-02-11T13:38:05.894815Z","shell.execute_reply":"2023-02-11T13:38:05.900898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# criterion = nn.CrossEntropyLoss()\n# optimizer = Adam(model.parameters(), lr=1e-3)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.904333Z","iopub.execute_input":"2023-02-11T13:38:05.904864Z","iopub.status.idle":"2023-02-11T13:38:05.911577Z","shell.execute_reply.started":"2023-02-11T13:38:05.90482Z","shell.execute_reply":"2023-02-11T13:38:05.910281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.metrics import f1_score\n# from fastprogress import master_bar\n# best_f1 = 0.0  # best F1 score achieved so far\n# num_epochs=2\n# for epoch in master_bar(range(0,num_epochs)):\n#     model.train()  # set model to training mode\n#     train_loss = 0.0\n#     train_acc = 0.0\n#     train_f1 = 0.0\n#     for i, data in enumerate(train_loader):\n#         optimizer.zero_grad()  # zero out the gradients from the previous step\n#         inputs, labels = data[\"image\"].to(device), data[\"label\"].to(device)\n#         outputs = model(inputs)  # forward pass\n#         loss = criterion(outputs, labels)  # compute loss\n#         loss.backward()  # backpropagate to compute gradients\n#         optimizer.step()  # update model weights\n#         train_loss += loss.item()  # update training loss\n#         label_hat = torch.max(outputs,1)[1].to(device) \n#         train_acc += label_hat.float().mean().item()\n#         #f1-score\n# #         label_prob_list.append(label_hat) #auc score\n# #         label_hat = torch.max(label_hat,1)[1].to(device) \n# #         label_list.append(label)\n# #         label_pred_list.append(label_hat)\n\n        \n# #         label_list, label_pred_list = torch.cat(label_list, 0), torch.cat(label_pred_list, 0)\n# #         f1_scr = f1_score(label_list.cpu(), label_pred_list.cpu(), average = 'macro')\n# #         train_acc += (outputs.argmax(dim=1) == labels).float().mean().item()  # update training accuracy\n#         # update training F1 score\n#         train_f1 += f1_score(label_hat.cpu(), labels.cpu(), average='micro')  # compute F1 score\n#     train_loss /= len(train_loader)  # average training loss for the epoch\n#     train_acc /= len(train_loader)  # average training accuracy for the epoch\n#     train_f1 /= len(train_loader)  # average training F1 score for the epoch","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.913691Z","iopub.execute_input":"2023-02-11T13:38:05.914983Z","iopub.status.idle":"2023-02-11T13:38:05.92309Z","shell.execute_reply.started":"2023-02-11T13:38:05.914875Z","shell.execute_reply":"2023-02-11T13:38:05.922279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.metrics import f1_score\n# from fastprogress import master_bar\n# best_f1 = 0.0  # best F1 score achieved so far\n# list_acc_train, list_acc_val, list_loss_train,list_loss_val, list_f1_score = [],[],[],[],[]\n# num_epochs = 50\n# for epoch in master_bar(range(0,num_epochs)):\n#     model.train()  # set model to training mode\n#     train_loss = 0.0\n#     train_acc = 0.0\n#     train_f1 = 0.0\n#     for i, data in enumerate(train_loader):\n#         optimizer.zero_grad()  # zero out the gradients from the previous step\n#         inputs, labels = data[\"image\"].to(device), data[\"label\"].to(device)\n#         outputs = model(inputs)  # forward pass\n#         loss = criterion(outputs, labels)  # compute loss\n#         loss.backward()  # backpropagate to compute gradients\n#         optimizer.step()  # update model weights\n#         train_loss += loss.item()  # update training loss\n#         label_hat = torch.max(outputs,1)[1].to(device) \n#         train_acc += label_hat.float().mean().item()\n#         # update training F1 score\n# #         train_f1 += f1_score(label_hat.cpu(), labels.cpu(), average='micro')  # compute F1 score\n#     train_loss /= len(train_loader)  # average training loss for the epoch\n#     train_acc /= len(train_loader)  # average training accuracy for the epoch\n#     list_loss_train.append(train_loss)\n#     list_acc_train.append(train_acc)\n# #     train_f1 /= len(train_loader)  # average training F1 score for the epoch\n\n#     model.eval()  # set model to evaluation mode\n#     val_loss = 0.0\n#     val_acc = 0.0\n#     val_f1 = 0.0\n    \n#     with torch.no_grad():  # disable gradient computation for evaluation\n#         for i, data in enumerate(val_loader):\n#             inputs, labels = data[\"image\"].to(device), data[\"label\"].to(device)\n#             outputs = model(inputs)  # forward pass\n#             loss = criterion(outputs, labels)  # compute loss\n#             val_loss += loss.item()  # update validation loss\n#             #f1-score\n#             label_prob_list.append(outputs) #auc score\n#             label_hat = torch.max(outputs,1)[1].to(device) \n#             label_list.append(labels)\n#             label_pred_list.append(label_hat)\n#             val_acc += label_hat.float().mean().item()  # update validation accuracy\n#             # update validation F1 score\n#             val_f1 += f1_score(label_hat.cpu(), labels.cpu(), average='micro')  # compute F1 score\n            \n# #     label_list, label_pred_list = torch.cat(label_list, 0), torch.cat(label_pred_list, 0)\n# #     f1_scr = f1_score(label_list.cpu(), label_pred_list.cpu(), average = 'macro')\n    \n#     val_loss /= len(val_loader)  # average validation loss for the epoch\n#     val_acc /= len(val_loader)  # average validation accuracy for the epoch\n#     val_f1 /= len(val_loader)  # average validation F1 score for the epoch\n#     list_loss_val.append(val_loss)\n#     list_acc_val.append(val_acc)\n#     list_f1_score.append(val_f1)\n    \n\n#     print(f'Epoch {epoch+1:2d}/{num_epochs}: train_loss = {train_loss:.3f}, train_acc = {train_acc:.3f}, train_f1 = {train_f1:.3f}, val_loss = {val_loss:.3f}, val_acc = {val_acc:.3f}, val_f1 = {val_f1:.3f}')\n#     if val_f1 > best_f1:  # if the current validation F1 score is better than the best F1 score so far\n#         best_f1 = val_f1  # update the best F1 scor\n#         state = {'epoch': epoch+1, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}\n#         torch.save(state, checkpoint_path)  # save the model checkpoint ","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.925125Z","iopub.execute_input":"2023-02-11T13:38:05.92606Z","iopub.status.idle":"2023-02-11T13:38:05.938165Z","shell.execute_reply.started":"2023-02-11T13:38:05.925935Z","shell.execute_reply":"2023-02-11T13:38:05.936896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n# results = {'acc_train':list_acc_train,'loss_train':list_loss_train,'val_acc':list_acc_val,\n#           'val_loss':list_loss_val,'f1_score_val':list_f1_score}\n# data_log = pd.DataFrame(results)\n# data_log.to_csv('/kaggle/working/datalog.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.941825Z","iopub.execute_input":"2023-02-11T13:38:05.942313Z","iopub.status.idle":"2023-02-11T13:38:05.951597Z","shell.execute_reply.started":"2023-02-11T13:38:05.942279Z","shell.execute_reply":"2023-02-11T13:38:05.950419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def train_loop_classification_model(model, criterion, optimizer, train_loader, val_loader, num_epochs=10, checkpoint_path='checkpoint.pth'):\n#     best_f1 = 0.0  # best F1 score achieved so far\n#     for epoch in range(num_epochs):\n#         model.train()  # set model to training mode\n#         train_loss = 0.0\n#         train_acc = 0.0\n#         train_f1 = 0.0\n#         for i, data in enumerate(train_loader):\n#             optimizer.zero_grad()  # zero out the gradients from the previous step\n#             inputs, labels = data[\"image\"].to(device), data[\"label\"].to(device)\n#             outputs = model(inputs)  # forward pass\n#             loss = criterion(outputs, labels)  # compute loss\n#             loss.backward()  # backpropagate to compute gradients\n#             optimizer.step()  # update model weights\n#             train_loss += loss.item()  # update training loss\n#             train_acc += (outputs.argmax(dim=1) == labels).float().mean().item()  # update training accuracy\n#             # update training F1 score\n#             train_f1 += f1_score(outputs.argmax(dim=1), labels, average='micro')  # compute F1 score\n#         train_loss /= len(train_loader)  # average training loss for the epoch\n#         train_acc /= len(train_loader)  # average training accuracy for the epoch\n#         train_f1 /= len(train_loader)  # average training F1 score for the epoch\n\n#         model.eval()  # set model to evaluation mode\n#         val_loss = 0.0\n#         val_acc = 0.0\n#         val_f1 = 0.0\n#         with torch.no_grad():  # disable gradient computation for evaluation\n#             for i, data in enumerate(val_loader):\n#                 inputs, labels = data[\"image\"].to(device), data[\"label\"].to(device)\n#                 outputs = model(inputs)  # forward pass\n#                 loss = criterion(outputs, labels)  # compute loss\n#                 val_loss += loss.item()  # update validation loss\n#                 val_acc += (outputs.argmax(dim=1) == labels).float().mean().item()  # update validation accuracy\n#                 # update validation F1 score\n#                 val_f1 += f1_score(outputs.argmax(dim=1), labels, average='micro')  # compute F1 score\n#         val_loss /= len(val_loader)  # average validation loss for the epoch\n#         val_acc /= len(val_loader)  # average validation accuracy for the epoch\n#         val_f1 /= len(val_loader)  # average validation F1 score for the epoch\n\n#         print(f'Epoch {epoch+1:2d}/{num_epochs}: train_loss = {train_loss:.3f}, train_acc = {train_acc:.3f}, train_f1 = {train_f1:.3f}, val_loss = {val_loss:.3f}, val_acc = {val_acc:.3f}, val_f1 = {val_f1:.3f}')\n#         if val_f1 > best_f1:  # if the current validation F1 score is better than the best F1 score so far\n#             best_f1 = val_f1  # update the best F1 scor\n#             state = {'epoch': epoch+1, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict()}\n#             torch.save(state, checkpoint_path)  # save the model checkpoint ","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.954157Z","iopub.execute_input":"2023-02-11T13:38:05.95472Z","iopub.status.idle":"2023-02-11T13:38:05.963614Z","shell.execute_reply.started":"2023-02-11T13:38:05.954672Z","shell.execute_reply":"2023-02-11T13:38:05.962575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # epochs = 10\n# train_loop_classification_model(model, loss_function, optimizer, trainset, valset, num_epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-02-11T13:38:05.965295Z","iopub.execute_input":"2023-02-11T13:38:05.966471Z","iopub.status.idle":"2023-02-11T13:38:05.977087Z","shell.execute_reply.started":"2023-02-11T13:38:05.966426Z","shell.execute_reply":"2023-02-11T13:38:05.975936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}