{"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"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":39272,"databundleVersionId":4629629,"sourceType":"competition"},{"sourceId":4619402,"sourceType":"datasetVersion","datasetId":2688773},{"sourceId":5298990,"sourceType":"datasetVersion","datasetId":3080803}],"dockerImageVersionId":30446,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg","metadata":{"execution":{"iopub.status.busy":"2023-04-13T03:08:38.915197Z","iopub.execute_input":"2023-04-13T03:08:38.915563Z","iopub.status.idle":"2023-04-13T03:08:48.701128Z","shell.execute_reply.started":"2023-04-13T03:08:38.91553Z","shell.execute_reply":"2023-04-13T03:08:48.69975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport glob\nimport gdcm\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport json\nfrom PIL import Image\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed\nimport torch\nimport torch.nn as nn\nfrom torch import Tensor\nfrom typing import Type\nimport torchvision","metadata":{"execution":{"iopub.status.busy":"2023-04-13T03:08:55.778159Z","iopub.execute_input":"2023-04-13T03:08:55.778578Z","iopub.status.idle":"2023-04-13T03:08:55.786529Z","shell.execute_reply.started":"2023-04-13T03:08:55.778537Z","shell.execute_reply":"2023-04-13T03:08:55.785404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\ntrain_path = \"/kaggle/input/rsna-breast-cancer-256-pngs/\"\ntrain_df['path'] = train_path+train_df.patient_id.astype(str)+'_'+train_df.image_id.astype(str)+'.png'\n\ntrain_df['path'][0]","metadata":{"execution":{"iopub.status.busy":"2023-04-13T03:08:57.588559Z","iopub.execute_input":"2023-04-13T03:08:57.589522Z","iopub.status.idle":"2023-04-13T03:08:57.805787Z","shell.execute_reply.started":"2023-04-13T03:08:57.589477Z","shell.execute_reply":"2023-04-13T03:08:57.804785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['transform'] = False","metadata":{"execution":{"iopub.status.busy":"2023-04-13T03:08:59.087014Z","iopub.execute_input":"2023-04-13T03:08:59.087454Z","iopub.status.idle":"2023-04-13T03:08:59.094483Z","shell.execute_reply.started":"2023-04-13T03:08:59.087418Z","shell.execute_reply":"2023-04-13T03:08:59.093435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Making copies of Cancerous Images","metadata":{}},{"cell_type":"code","source":"cancerous = train_df[train_df['cancer'] == 1]","metadata":{"execution":{"iopub.status.busy":"2023-04-13T03:09:01.197552Z","iopub.execute_input":"2023-04-13T03:09:01.198281Z","iopub.status.idle":"2023-04-13T03:09:01.211279Z","shell.execute_reply.started":"2023-04-13T03:09:01.198241Z","shell.execute_reply":"2023-04-13T03:09:01.210148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def augment_dup_cancerous_imgs(num_duplicates=10, cancerous=cancerous):\n    temp_df = cancerous\n    for i, row in cancerous.iterrows():\n        for j in range(num_duplicates):\n            temp = row\n            temp['transform'] = True\n            temp_df = temp_df.append(temp, ignore_index=True)\n            \n    return temp_df           ","metadata":{"execution":{"iopub.status.busy":"2023-04-13T03:09:02.591936Z","iopub.execute_input":"2023-04-13T03:09:02.592295Z","iopub.status.idle":"2023-04-13T03:09:02.601589Z","shell.execute_reply.started":"2023-04-13T03:09:02.592263Z","shell.execute_reply":"2023-04-13T03:09:02.600567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_with_dups = augment_dup_cancerous_imgs()","metadata":{"execution":{"iopub.status.busy":"2023-04-13T03:09:04.750238Z","iopub.execute_input":"2023-04-13T03:09:04.750756Z","iopub.status.idle":"2023-04-13T03:09:55.696945Z","shell.execute_reply.started":"2023-04-13T03:09:04.750687Z","shell.execute_reply":"2023-04-13T03:09:55.695838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(cancer_with_dups[cancer_with_dups['transform'] == False]), len(cancer_with_dups[cancer_with_dups['transform'] == True]))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:26:57.020262Z","iopub.execute_input":"2023-04-12T21:26:57.021206Z","iopub.status.idle":"2023-04-12T21:26:57.032938Z","shell.execute_reply.started":"2023-04-12T21:26:57.021156Z","shell.execute_reply":"2023-04-12T21:26:57.031615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Append cancerous duplicate df to train_df","metadata":{}},{"cell_type":"code","source":"print(len(train_df))\ntrain_with_dups = train_df.append(cancer_with_dups)\nshuffled_train = train_with_dups.sample(frac=1).reset_index()\nprint(\"RAN\")\n","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:26:58.596484Z","iopub.execute_input":"2023-04-12T21:26:58.596933Z","iopub.status.idle":"2023-04-12T21:26:58.663477Z","shell.execute_reply.started":"2023-04-12T21:26:58.596892Z","shell.execute_reply":"2023-04-12T21:26:58.662026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"shuffled_train = train_with_dups.sample(frac=1)\nshuffled_train.reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:00.502973Z","iopub.execute_input":"2023-04-12T21:27:00.503494Z","iopub.status.idle":"2023-04-12T21:27:00.578412Z","shell.execute_reply.started":"2023-04-12T21:27:00.503448Z","shell.execute_reply":"2023-04-12T21:27:00.577359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"sucessful rate","metadata":{}},{"cell_type":"code","source":"# Sample labels and predicted results\nactual_labels = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]  # Actual labels\npredicted_labels = [0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0]  # Predicted results by the model\n​\n# Calculate the number of correct predictions\ncorrect_predictions = sum(1 for actual, predicted in zip(actual_labels, predicted_labels) if actual == predicted)\n​\n# Calculate success percentage\ntotal_predictions = len(actual_labels)\nsuccess_percentage = (correct_predictions / total_predictions) * 100\n​\n# Print the success percentage\nprint(f\"The success percentage of the breast cancer detection is: {success_percentage:.2f}%\")\n​","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Undersampling non-cancerous examples (only run if you mean to do this)","metadata":{}},{"cell_type":"code","source":"# taking every cancerous example we have and undersampling the non cancerous for a 50-50 split\nprint(len(shuffled_train[shuffled_train['cancer'] == True]))\nundersampled_df = pd.DataFrame()\nundersampled_df = undersampled_df.append(shuffled_train[shuffled_train['cancer'] == True].sample(13896))\nundersampled_df = undersampled_df.append(shuffled_train[shuffled_train['cancer'] == False].sample(13896))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:08.562071Z","iopub.execute_input":"2023-04-12T21:27:08.562453Z","iopub.status.idle":"2023-04-12T21:27:08.598177Z","shell.execute_reply.started":"2023-04-12T21:27:08.562419Z","shell.execute_reply":"2023-04-12T21:27:08.596965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(undersampled_df[undersampled_df['cancer'] == True]))\n\nshuffled_train = undersampled_df\nshuffled_train = shuffled_train.reset_index(drop=True)\n# print(len(shuffled_train[shuffled_train['cancer'] == False]))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:10.019106Z","iopub.execute_input":"2023-04-12T21:27:10.019803Z","iopub.status.idle":"2023-04-12T21:27:10.036024Z","shell.execute_reply.started":"2023-04-12T21:27:10.019764Z","shell.execute_reply":"2023-04-12T21:27:10.034736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(shuffled_train))\nprint(len(train_df))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:13.160668Z","iopub.execute_input":"2023-04-12T21:27:13.161625Z","iopub.status.idle":"2023-04-12T21:27:13.168547Z","shell.execute_reply.started":"2023-04-12T21:27:13.161569Z","shell.execute_reply":"2023-04-12T21:27:13.167218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PermuteDimensionsTransform():\n    def __call__(self, image):\n        image = image.permute(2,0,1)\n        return image\n\nclass PermuteDimensionsTransformBack():\n    def __call__(self, image):\n        image = image.permute(1,2,0)\n        return image\n    \n\nclass RSNADataset():\n    \n    def __init__(self, df):\n        self.df = df\n        self.transform = torchvision.transforms.Compose([\n#             PermuteDimensionsTransform(),\n#             torchvision.transforms.RandomRotation(degrees=(0,60)),\n#             PermuteDimensionsTransformBack(),\n            torchvision.transforms.RandomHorizontalFlip(p=0.5),\n            torchvision.transforms.RandomVerticalFlip(p=0.5)\n        ])\n\n    def __len__(self):\n        return len(self.df)\n    \n    def getimginfo(self, idx):\n        row = self.df.iloc[idx]\n        img_path = row['path']\n        image = cv2.imread(img_path).astype(np.float32)/255\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        image = np.array(self.transform(torch.tensor(image)))\n        target = torch.tensor(row.cancer).float()\n        \n        \n        return {\"image\": image, \"target\": target}","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:15.734843Z","iopub.execute_input":"2023-04-12T21:27:15.735213Z","iopub.status.idle":"2023-04-12T21:27:15.745537Z","shell.execute_reply.started":"2023-04-12T21:27:15.73518Z","shell.execute_reply":"2023-04-12T21:27:15.744486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add more stuff in this function like flags for different ways to show the image\n\ndef displayImage(img, rgb):\n\n    plt.imshow(img)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:19.299363Z","iopub.execute_input":"2023-04-12T21:27:19.299721Z","iopub.status.idle":"2023-04-12T21:27:19.304641Z","shell.execute_reply.started":"2023-04-12T21:27:19.29969Z","shell.execute_reply":"2023-04-12T21:27:19.303589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shuffled_train[shuffled_train['transform'] == True]","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:21.143824Z","iopub.execute_input":"2023-04-12T21:27:21.144199Z","iopub.status.idle":"2023-04-12T21:27:21.171559Z","shell.execute_reply.started":"2023-04-12T21:27:21.144168Z","shell.execute_reply":"2023-04-12T21:27:21.170347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataLoader = RSNADataset(shuffled_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:23.397699Z","iopub.execute_input":"2023-04-12T21:27:23.398115Z","iopub.status.idle":"2023-04-12T21:27:23.407194Z","shell.execute_reply.started":"2023-04-12T21:27:23.398079Z","shell.execute_reply":"2023-04-12T21:27:23.406334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_data = dataLoader.getimginfo(2)\ndisplayImage(img_data['image'], 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:24.943035Z","iopub.execute_input":"2023-04-12T21:27:24.944014Z","iopub.status.idle":"2023-04-12T21:27:25.307168Z","shell.execute_reply.started":"2023-04-12T21:27:24.943975Z","shell.execute_reply":"2023-04-12T21:27:25.306113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class BasicBlock(nn.Module):\n    def __init__(\n        self, \n        in_channels: int,\n        out_channels: int,\n        stride: int = 1,\n        expansion: int = 1,\n        downsample: nn.Module = None\n    ) -> None:\n        super(BasicBlock, self).__init__()\n        # Multiplicative factor for the subsequent conv2d layer's output channels.\n        # It is 1 for ResNet18 and ResNet34.\n        self.expansion = expansion\n        self.downsample = downsample\n        self.conv1 = nn.Conv2d(\n            in_channels, \n            out_channels, \n            kernel_size=3, \n            stride=stride, \n            padding=1,\n            bias=False\n        )\n        self.bn1 = nn.BatchNorm2d(out_channels)\n        self.relu = nn.ReLU(inplace=True)\n        self.conv2 = nn.Conv2d(\n            out_channels, \n            out_channels*self.expansion, \n            kernel_size=3, \n            padding=1,\n            bias=False\n        )\n        self.bn2 = nn.BatchNorm2d(out_channels*self.expansion)\n    def forward(self, x: Tensor) -> Tensor:\n        identity = x\n        out = self.conv1(x)\n        out = self.bn1(out)\n        out = self.relu(out)\n        out = self.conv2(out)\n        out = self.bn2(out)\n        if self.downsample is not None:\n            identity = self.downsample(x)\n        out += identity\n        out = self.relu(out)\n        return  out","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:27.571365Z","iopub.execute_input":"2023-04-12T21:27:27.571734Z","iopub.status.idle":"2023-04-12T21:27:27.582905Z","shell.execute_reply.started":"2023-04-12T21:27:27.571703Z","shell.execute_reply":"2023-04-12T21:27:27.58139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet(nn.Module):\n    def __init__(\n        self, \n        img_channels: int,\n        num_layers: int,\n        block: Type[BasicBlock],\n        num_classes: int  = 1000\n    ) -> None:\n        super(ResNet, self).__init__()\n        if num_layers == 18:\n            # The following `layers` list defines the number of `BasicBlock` \n            # to use to build the network and how many basic blocks to stack\n            # together.\n            layers = [2, 2, 2, 2]\n            self.expansion = 1\n        \n        self.in_channels = 64\n        # All ResNets (18 to 152) contain a Conv2d => BN => ReLU for the first\n        # three layers. Here, kernel size is 7.\n        self.conv1 = nn.Conv2d(\n            in_channels=img_channels,\n            out_channels=self.in_channels,\n            kernel_size=7, \n            stride=2,\n            padding=3,\n            bias=False\n        )\n        self.bn1 = nn.BatchNorm2d(self.in_channels)\n        self.relu = nn.ReLU(inplace=True)\n        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n\n        self.layer1 = self._make_layer(block, 64, layers[0])\n        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)\n        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)\n        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)\n\n        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))\n        self.fc = nn.Linear(512*self.expansion, num_classes)\n\n    def _make_layer(\n        self, \n        block: Type[BasicBlock],\n        out_channels: int,\n        blocks: int,\n        stride: int = 1\n    ) -> nn.Sequential:\n        downsample = None\n        if stride != 1:\n            \"\"\"\n            This should pass from `layer2` to `layer4` or \n            when building ResNets50 and above. Section 3.3 of the paper\n            Deep Residual Learning for Image Recognition\n            (https://arxiv.org/pdf/1512.03385v1.pdf).\n            \"\"\"\n            downsample = nn.Sequential(\n                nn.Conv2d(\n                    self.in_channels, \n                    out_channels*self.expansion,\n                    kernel_size=1,\n                    stride=stride,\n                    bias=False \n                ),\n                nn.BatchNorm2d(out_channels * self.expansion),\n            )\n        layers = []\n        layers.append(\n            block(\n                self.in_channels, out_channels, stride, self.expansion, downsample\n            )\n        )\n        self.in_channels = out_channels * self.expansion\n\n        for i in range(1, blocks):\n            layers.append(block(\n                self.in_channels,\n                out_channels,\n                expansion=self.expansion\n            ))\n        return nn.Sequential(*layers)\n\n    def forward(self, x: Tensor) -> Tensor:\n        x = self.conv1(x)\n        x = self.bn1(x)\n        x = self.relu(x)\n        x = self.maxpool(x)\n\n        x = self.layer1(x)\n        x = self.layer2(x)\n        x = self.layer3(x)\n        x = self.layer4(x)\n        # The spatial dimension of the final layer's feature \n        # map should be (7, 7) for all ResNets.\n     #   print('Dimensions of the last convolutional feature map: ', x.shape)\n\n        x = self.avgpool(x)\n        x = torch.flatten(x, 1)\n        x = self.fc(x)\n        x = self.relu(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:29.906458Z","iopub.execute_input":"2023-04-12T21:27:29.906832Z","iopub.status.idle":"2023-04-12T21:27:29.923172Z","shell.execute_reply.started":"2023-04-12T21:27:29.906799Z","shell.execute_reply":"2023-04-12T21:27:29.922002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(type(img_data['image']))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:34.498732Z","iopub.execute_input":"2023-04-12T21:27:34.499093Z","iopub.status.idle":"2023-04-12T21:27:34.504983Z","shell.execute_reply.started":"2023-04-12T21:27:34.49906Z","shell.execute_reply":"2023-04-12T21:27:34.503976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = torch.Tensor(img_data['image'])\nprint(tensor.shape)\nmodel = ResNet(img_channels=256, num_layers=18, block=BasicBlock, num_classes=1)\nresult = model(tensor.unsqueeze(0))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:35.62278Z","iopub.execute_input":"2023-04-12T21:27:35.62353Z","iopub.status.idle":"2023-04-12T21:27:35.978416Z","shell.execute_reply.started":"2023-04-12T21:27:35.623493Z","shell.execute_reply":"2023-04-12T21:27:35.977347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor","metadata":{"execution":{"iopub.status.busy":"2023-04-03T06:44:52.589834Z","iopub.execute_input":"2023-04-03T06:44:52.590335Z","iopub.status.idle":"2023-04-03T06:44:52.604886Z","shell.execute_reply.started":"2023-04-03T06:44:52.590294Z","shell.execute_reply":"2023-04-03T06:44:52.603465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = torch.Tensor(img_data['image'])","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:41.772226Z","iopub.execute_input":"2023-04-12T21:27:41.773112Z","iopub.status.idle":"2023-04-12T21:27:41.778008Z","shell.execute_reply.started":"2023-04-12T21:27:41.773075Z","shell.execute_reply":"2023-04-12T21:27:41.776664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-04-03T06:44:52.615321Z","iopub.execute_input":"2023-04-03T06:44:52.616057Z","iopub.status.idle":"2023-04-03T06:44:52.642932Z","shell.execute_reply.started":"2023-04-03T06:44:52.616015Z","shell.execute_reply":"2023-04-03T06:44:52.642043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ny = shuffled_train['cancer']\nX = shuffled_train.drop(columns = ['cancer'])\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=3)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:44.309753Z","iopub.execute_input":"2023-04-12T21:27:44.310331Z","iopub.status.idle":"2023-04-12T21:27:44.445446Z","shell.execute_reply.started":"2023-04-12T21:27:44.310235Z","shell.execute_reply":"2023-04-12T21:27:44.444174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(shuffled_train))\nprint(len(X_train), len(X_test), len(y_train), len(y_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:46.342598Z","iopub.execute_input":"2023-04-12T21:27:46.343171Z","iopub.status.idle":"2023-04-12T21:27:46.349741Z","shell.execute_reply.started":"2023-04-12T21:27:46.343127Z","shell.execute_reply":"2023-04-12T21:27:46.348555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X_test[X_test['transform'] == True]))\nprint(len(X_train[X_train['transform'] == True]))","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:47.37861Z","iopub.execute_input":"2023-04-12T21:27:47.379596Z","iopub.status.idle":"2023-04-12T21:27:47.390777Z","shell.execute_reply.started":"2023-04-12T21:27:47.379532Z","shell.execute_reply":"2023-04-12T21:27:47.38937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, row in X_train.iterrows():\n    print(idx)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:49.006928Z","iopub.execute_input":"2023-04-12T21:27:49.007613Z","iopub.status.idle":"2023-04-12T21:27:49.024708Z","shell.execute_reply.started":"2023-04-12T21:27:49.007574Z","shell.execute_reply":"2023-04-12T21:27:49.023592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\ncriterion = nn.BCELoss()\noptimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:47:48.458119Z","iopub.execute_input":"2023-04-12T21:47:48.459083Z","iopub.status.idle":"2023-04-12T21:47:48.465279Z","shell.execute_reply.started":"2023-04-12T21:47:48.459045Z","shell.execute_reply":"2023-04-12T21:47:48.464152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df['cancer']==1]\nimg_data = dataLoader.getimginfo(87)\n\nif img_data['target'] == 1:\n    print(\"Asd\")","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:27:57.54699Z","iopub.execute_input":"2023-04-12T21:27:57.547364Z","iopub.status.idle":"2023-04-12T21:27:57.579245Z","shell.execute_reply.started":"2023-04-12T21:27:57.54733Z","shell.execute_reply":"2023-04-12T21:27:57.578158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### f1 loss","metadata":{}},{"cell_type":"code","source":"def macro_f1_score(y_true, y_pred):\n    ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ResNet(img_channels=256, num_layers=18, block=BasicBlock, num_classes=1)\nmodel.to(torch.device(\"cuda\"))\n\n\nlosses = []\nfor epoch in range (2):\n    i=0\n    running_loss = 0\n    for idx, row in X_train.iterrows():\n        img_data = dataLoader.getimginfo(idx)\n        optimizer.zero_grad()\n        \n        tensor_img = torch.Tensor(img_data['image']).to(torch.device(\"cuda\"))\n        outputs = model(tensor_img.unsqueeze(0))\n        if i < 10:\n            print(outputs)\n        if outputs < 0:\n            print(outputs)\n            outputs[0] = torch.Tensor([0])\n          \n        try:\n            loss = criterion(outputs, img_data['target'].flatten().unsqueeze(1).to(torch.device(\"cuda\")))\n        except RuntimeError as e:\n            print(\"Had an error with outputs\", outputs, '\\n And target:',img_data['target'].flatten().unsqueeze(1))\n            continue\n        \n#         if img_data['target'] == 1:\n#             print(outputs)\n            \n        i+=1\n        running_loss += loss.item()\n        \n        loss.backward()\n        optimizer.step()\n        \n        if i%2000 == 1999:\n            print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')\n            losses.append(running_loss / 2000)\n            running_loss = 0.0\n\nprint('Finished Training')\n        #  print(idx)","metadata":{"execution":{"iopub.status.busy":"2023-04-12T21:47:57.0379Z","iopub.execute_input":"2023-04-12T21:47:57.038468Z","iopub.status.idle":"2023-04-12T21:51:48.568856Z","shell.execute_reply.started":"2023-04-12T21:47:57.038431Z","shell.execute_reply":"2023-04-12T21:51:48.56682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = y_test.to_frame()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T01:22:41.358537Z","iopub.execute_input":"2023-04-03T01:22:41.358892Z","iopub.status.idle":"2023-04-03T01:22:41.365695Z","shell.execute_reply.started":"2023-04-03T01:22:41.358856Z","shell.execute_reply":"2023-04-03T01:22:41.364303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(t[t['cancer'] == 1])/len(t)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T01:22:41.367447Z","iopub.execute_input":"2023-04-03T01:22:41.36783Z","iopub.status.idle":"2023-04-03T01:22:41.386318Z","shell.execute_reply.started":"2023-04-03T01:22:41.367796Z","shell.execute_reply":"2023-04-03T01:22:41.3848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_saved = model","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:41:02.332073Z","iopub.execute_input":"2023-04-03T04:41:02.332549Z","iopub.status.idle":"2023-04-03T04:41:02.337823Z","shell.execute_reply.started":"2023-04-03T04:41:02.332508Z","shell.execute_reply":"2023-04-03T04:41:02.336795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = 'resnet18_10xcancer_relu.pth'\ntorch.save(model.state_dict(), path)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:41:02.339116Z","iopub.execute_input":"2023-04-03T04:41:02.340122Z","iopub.status.idle":"2023-04-03T04:41:02.418353Z","shell.execute_reply.started":"2023-04-03T04:41:02.340083Z","shell.execute_reply":"2023-04-03T04:41:02.417291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = 'resnet18_10xcancer_loss_relu.pth'\ntorch.save(losses, path)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:41:02.420219Z","iopub.execute_input":"2023-04-03T04:41:02.420617Z","iopub.status.idle":"2023-04-03T04:41:02.425897Z","shell.execute_reply.started":"2023-04-03T04:41:02.420578Z","shell.execute_reply":"2023-04-03T04:41:02.42465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = 'resnet18_losses_noundersample.pth'\ntorch.save(losses, path)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:59:39.741387Z","iopub.execute_input":"2023-04-03T04:59:39.742066Z","iopub.status.idle":"2023-04-03T04:59:39.747242Z","shell.execute_reply.started":"2023-04-03T04:59:39.742028Z","shell.execute_reply":"2023-04-03T04:59:39.74612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = y_test.to_frame(name='iscancer')","metadata":{"execution":{"iopub.status.busy":"2023-04-03T01:22:41.504586Z","iopub.execute_input":"2023-04-03T01:22:41.505035Z","iopub.status.idle":"2023-04-03T01:22:41.51711Z","shell.execute_reply.started":"2023-04-03T01:22:41.504968Z","shell.execute_reply":"2023-04-03T01:22:41.515475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancerous_y = y_test[y_test['cancer']==1]\ncancerous_y.iloc[0].values[0]","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:18:51.454281Z","iopub.execute_input":"2023-04-01T23:18:51.454727Z","iopub.status.idle":"2023-04-01T23:18:51.464464Z","shell.execute_reply.started":"2023-04-01T23:18:51.454687Z","shell.execute_reply":"2023-04-01T23:18:51.462919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancerous_x = X_test.loc[cancerous_y.index]","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:13:30.454216Z","iopub.execute_input":"2023-04-01T23:13:30.454654Z","iopub.status.idle":"2023-04-01T23:13:30.46174Z","shell.execute_reply.started":"2023-04-01T23:13:30.454614Z","shell.execute_reply":"2023-04-01T23:13:30.460347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(cancerous_x), len(cancerous_y))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:13:49.551815Z","iopub.execute_input":"2023-04-01T23:13:49.55227Z","iopub.status.idle":"2023-04-01T23:13:49.558861Z","shell.execute_reply.started":"2023-04-01T23:13:49.552227Z","shell.execute_reply":"2023-04-01T23:13:49.557467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_sub = X_test.head(200)\ny_test_sub = y_test.head(200)\n\nfinal_X = cancerous_x.append(x_test_sub)\nfinal_y = cancerous_y.append(y_test_sub)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:27:15.309498Z","iopub.execute_input":"2023-04-01T23:27:15.309954Z","iopub.status.idle":"2023-04-01T23:27:15.321341Z","shell.execute_reply.started":"2023-04-01T23:27:15.309912Z","shell.execute_reply":"2023-04-01T23:27:15.32007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_X","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:28:15.707302Z","iopub.execute_input":"2023-04-01T23:28:15.708098Z","iopub.status.idle":"2023-04-01T23:28:15.735259Z","shell.execute_reply.started":"2023-04-01T23:28:15.70805Z","shell.execute_reply":"2023-04-01T23:28:15.734311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(final_y[final_y['cancer'] == 1]), len(final_y[final_y['cancer'] == 0]), len(final_y))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:29:05.732328Z","iopub.execute_input":"2023-04-01T23:29:05.732757Z","iopub.status.idle":"2023-04-01T23:29:05.740151Z","shell.execute_reply.started":"2023-04-01T23:29:05.732719Z","shell.execute_reply":"2023-04-01T23:29:05.739135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(y_test==1), len(y_test == 0))","metadata":{"execution":{"iopub.status.busy":"2023-04-02T04:02:53.009552Z","iopub.execute_input":"2023-04-02T04:02:53.010071Z","iopub.status.idle":"2023-04-02T04:02:53.01875Z","shell.execute_reply.started":"2023-04-02T04:02:53.010026Z","shell.execute_reply":"2023-04-02T04:02:53.017315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ni=0\ncorrect = 0\nmax_output_val = 0\npred_for_max = None\nfor idx, row in X_test.iterrows():\n    img_data = dataLoader.getimginfo(idx)\n    tensor_img = torch.Tensor(img_data['image'])\n    outputs = model(tensor_img.unsqueeze(0))\n#     print(outputs, y_test.iloc[i])\n    if outputs > max_output_val:\n        max_output_val = outputs\n        pred_for_max = y_test.iloc[i]\n    if outputs > 0.5:\n        outputs = 1\n    else:\n        outputs = 0\n    \n    if outputs == y_test.iloc[i]:\n        correct+=1\n        \n    if i%100 == 0:\n        print(i, \"Completed\")\n    i+=1\n","metadata":{"execution":{"iopub.status.busy":"2023-04-02T21:04:18.492076Z","iopub.execute_input":"2023-04-02T21:04:18.492525Z","iopub.status.idle":"2023-04-02T21:07:28.939183Z","shell.execute_reply.started":"2023-04-02T21:04:18.492489Z","shell.execute_reply":"2023-04-02T21:07:28.938121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(final_X), correct)\nprint(\"ACCURACY\", correct/len(X_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-02T21:07:45.269992Z","iopub.execute_input":"2023-04-02T21:07:45.271457Z","iopub.status.idle":"2023-04-02T21:07:45.279703Z","shell.execute_reply.started":"2023-04-02T21:07:45.271389Z","shell.execute_reply":"2023-04-02T21:07:45.278088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(max_output_val, pred_for_max)","metadata":{"execution":{"iopub.status.busy":"2023-04-02T07:14:06.145127Z","iopub.execute_input":"2023-04-02T07:14:06.146252Z","iopub.status.idle":"2023-04-02T07:14:06.156213Z","shell.execute_reply.started":"2023-04-02T07:14:06.14617Z","shell.execute_reply":"2023-04-02T07:14:06.154568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(cancerous_x), correct)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:21:07.632285Z","iopub.execute_input":"2023-04-01T23:21:07.633861Z","iopub.status.idle":"2023-04-01T23:21:07.641637Z","shell.execute_reply.started":"2023-04-01T23:21:07.633806Z","shell.execute_reply":"2023-04-01T23:21:07.64024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"ACCURACY\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=0\ncorrect = 0\nfor idx, row in X_test.iterrows():\n    img_data = dataLoader.getimginfo(idx)\n    tensor_img = torch.Tensor(img_data['image'])\n    outputs = model(tensor_img.unsqueeze(0))\n    \n    if outputs > 0.5:\n        outputs = 1\n    else:\n        outputs = 0\n        \n    if outputs == y_test.iloc[i]:\n        correct+=1\n        \n    if i%500 == 0:\n        print(i, \"Completed\")\n    i+=1\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:01:03.218906Z","iopub.execute_input":"2023-04-01T23:01:03.219993Z","iopub.status.idle":"2023-04-01T23:05:17.566784Z","shell.execute_reply.started":"2023-04-01T23:01:03.219949Z","shell.execute_reply":"2023-04-01T23:05:17.565751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:07:59.65665Z","iopub.execute_input":"2023-04-01T23:07:59.657235Z","iopub.status.idle":"2023-04-01T23:07:59.665032Z","shell.execute_reply.started":"2023-04-01T23:07:59.657164Z","shell.execute_reply":"2023-04-01T23:07:59.663765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correct","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:07:54.37327Z","iopub.execute_input":"2023-04-01T23:07:54.373725Z","iopub.status.idle":"2023-04-01T23:07:54.381892Z","shell.execute_reply.started":"2023-04-01T23:07:54.373683Z","shell.execute_reply":"2023-04-01T23:07:54.380519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(correct / len(X_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T23:08:15.964246Z","iopub.execute_input":"2023-04-01T23:08:15.964689Z","iopub.status.idle":"2023-04-01T23:08:15.970354Z","shell.execute_reply.started":"2023-04-01T23:08:15.96465Z","shell.execute_reply":"2023-04-01T23:08:15.969033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ROC CURVE","metadata":{}},{"cell_type":"code","source":"# thresholds = {\n#     0.005: {},\n#     0.050: {},\n#     0.10: { },\n#     0.002: {},\n#     0.0024: {\n\n#    }, \n# }\n# keep track of every test prediction and its actual target value\n# list of tuples\n# (prediction value, target)\ndef get_predictions():\n    test_predictions = []\n    i=0\n    for idx, row in X_test.iterrows():\n        img_data = dataLoader.getimginfo(idx)\n        tensor_img = torch.Tensor(img_data['image']).to(torch.device(\"cuda\"))\n        outputs = model(tensor_img.unsqueeze(0))\n        test_predictions.append((outputs.item(), y_test.iloc[i]))\n        i+=1\n        \n    return test_predictions\n    ","metadata":{"execution":{"iopub.status.busy":"2023-04-03T07:11:15.24195Z","iopub.execute_input":"2023-04-03T07:11:15.242282Z","iopub.status.idle":"2023-04-03T07:11:15.250086Z","shell.execute_reply.started":"2023-04-03T07:11:15.242252Z","shell.execute_reply":"2023-04-03T07:11:15.247981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = get_predictions()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T07:11:17.223386Z","iopub.execute_input":"2023-04-03T07:11:17.224108Z","iopub.status.idle":"2023-04-03T07:12:24.401101Z","shell.execute_reply.started":"2023-04-03T07:11:17.224069Z","shell.execute_reply":"2023-04-03T07:12:24.399934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:58:25.339858Z","iopub.execute_input":"2023-04-03T04:58:25.340224Z","iopub.status.idle":"2023-04-03T04:58:25.344932Z","shell.execute_reply.started":"2023-04-03T04:58:25.340193Z","shell.execute_reply":"2023-04-03T04:58:25.343447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nfrom sklearn.metrics import roc_curve, auc\n# predictions = torch.load('/kaggle/input/resnet16-pretrained/resnet18_10xcancer_loss_undersample_relu_predictions.pth')\n\nthreshold_pred = {}\ntprs = []\nfprs = []\nthresholds = np.linspace(0, 0.5, 150)\nfor threshold in thresholds:\n    tp = 0\n    fp = 0\n    tn = 0\n    fn = 0\n    for (prediction, target) in predictions:\n        if prediction >= threshold:\n            if target == 1:\n                tp += 1\n            else:\n                fp += 1\n        else:\n            if target == 0:\n                tn += 1\n            else:\n                fn += 1\n#     threshold_pred[threshold] = {\n#         \"tpr\": tp / (tp+fn),\n#         \"fpr\": fp / (fp+tn)\n#     }\n    tpr = tp / float(tp + fn)\n    fpr = fp / float(fp + tn)\n\n    # append the TPR and FPR to the corresponding lists\n    tprs.append(tpr)\n    fprs.append(fpr)\n\nplt.plot(fprs, tprs)\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC Curve for undersampled data\")\nplt.show()\n# roc(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T07:13:15.04688Z","iopub.execute_input":"2023-04-03T07:13:15.047277Z","iopub.status.idle":"2023-04-03T07:13:15.537769Z","shell.execute_reply.started":"2023-04-03T07:13:15.047243Z","shell.execute_reply":"2023-04-03T07:13:15.536542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## pretrained resnet 18","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PreTrained Resnet 16","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.transforms as T\ntransforms = torch.nn.Sequential(\n    T.RandomCrop(224),\n    T.RandomHorizontalFlip(p=0.3),\n)\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T02:06:09.07344Z","iopub.execute_input":"2023-04-03T02:06:09.073905Z","iopub.status.idle":"2023-04-03T02:06:09.082043Z","shell.execute_reply.started":"2023-04-03T02:06:09.073863Z","shell.execute_reply":"2023-04-03T02:06:09.080841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision.models as models\nimport torchvision.transforms as transforms\n\nresnet = models.resnet18(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T02:15:29.799547Z","iopub.execute_input":"2023-04-03T02:15:29.800575Z","iopub.status.idle":"2023-04-03T02:15:30.173836Z","shell.execute_reply.started":"2023-04-03T02:15:29.800502Z","shell.execute_reply":"2023-04-03T02:15:30.172809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image = np.transpose(tensor_img, (2, 0, 1))\nimg_data = dataLoader.getimginfo(2)\nimg_data['image'].shape\nim2 = np.transpose(img_data['image'], (2,0,1))\nim2.shape\n# tensor_img = torch.Tensor(img_data['image']).unsqueeze(0)\na = resnet(torch.Tensor(im2))","metadata":{"execution":{"iopub.status.busy":"2023-04-03T02:27:40.361477Z","iopub.execute_input":"2023-04-03T02:27:40.362181Z","iopub.status.idle":"2023-04-03T02:27:40.400383Z","shell.execute_reply.started":"2023-04-03T02:27:40.362144Z","shell.execute_reply":"2023-04-03T02:27:40.398991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictor = Predictor().to(device)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T02:06:15.032511Z","iopub.execute_input":"2023-04-03T02:06:15.035214Z","iopub.status.idle":"2023-04-03T02:06:17.841718Z","shell.execute_reply.started":"2023-04-03T02:06:15.035174Z","shell.execute_reply":"2023-04-03T02:06:17.840442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictor(torch.Tensor(img_data['image']).unsqueeze(0))","metadata":{"execution":{"iopub.status.busy":"2023-04-03T02:08:44.505201Z","iopub.execute_input":"2023-04-03T02:08:44.506187Z","iopub.status.idle":"2023-04-03T02:08:51.197958Z","shell.execute_reply.started":"2023-04-03T02:08:44.506146Z","shell.execute_reply":"2023-04-03T02:08:51.196602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = 'resnet18_10xcancer_loss_undersample_relu_predictions.pth'\ntorch.save(predictions, path)","metadata":{"execution":{"iopub.status.busy":"2023-04-02T21:11:28.741267Z","iopub.execute_input":"2023-04-02T21:11:28.741735Z","iopub.status.idle":"2023-04-02T21:11:28.822555Z","shell.execute_reply.started":"2023-04-02T21:11:28.741696Z","shell.execute_reply":"2023-04-02T21:11:28.820787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nfrom sklearn.metrics import roc_curve, auc\npredictions = torch.load('/kaggle/input/resnet16-pretrained/resnet18_10xcancer_loss_undersample_relu_predictions.pth')\n\nthreshold_pred = {}\ntprs = []\nfprs = []\nthresholds = np.linspace(0, 0.5, 150)\nfor threshold in thresholds:\n    tp = 0\n    fp = 0\n    tn = 0\n    fn = 0\n    for (prediction, target) in predictions:\n        if prediction >= threshold:\n            if target == 1:\n                tp += 1\n            else:\n                fp += 1\n        else:\n            if target == 0:\n                tn += 1\n            else:\n                fn += 1\n#     threshold_pred[threshold] = {\n#         \"tpr\": tp / (tp+fn),\n#         \"fpr\": fp / (fp+tn)\n#     }\n    tpr = tp / float(tp + fn)\n    fpr = fp / float(fp + tn)\n\n    # append the TPR and FPR to the corresponding lists\n    tprs.append(tpr)\n    fprs.append(fpr)\n\nplt.plot(fprs, tprs)\nplt.xlabel(\"False Positive Rate\")\nplt.ylabel(\"True Positive Rate\")\nplt.title(\"ROC Curve\")\nplt.show()\n# roc(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T01:53:56.729608Z","iopub.execute_input":"2023-04-03T01:53:56.730135Z","iopub.status.idle":"2023-04-03T01:53:57.374024Z","shell.execute_reply.started":"2023-04-03T01:53:56.730092Z","shell.execute_reply":"2023-04-03T01:53:57.37233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = torch.load('/kaggle/input/resnet16-pretrained/resnet18_10xcancer_loss_undersample_relu_predictions.pth')\n","metadata":{"execution":{"iopub.status.busy":"2023-04-03T01:42:40.253637Z","iopub.execute_input":"2023-04-03T01:42:40.255038Z","iopub.status.idle":"2023-04-03T01:42:40.290319Z","shell.execute_reply.started":"2023-04-03T01:42:40.254978Z","shell.execute_reply":"2023-04-03T01:42:40.289048Z"},"trusted":true},"execution_count":null,"outputs":[]}]}