{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# #import os\n# #for dirname, _, filenames in os.walk('/kaggle/input'):\n# #    for filename in filenames:\n#      #   print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-10T06:48:21.127308Z","iopub.execute_input":"2023-02-10T06:48:21.12774Z","iopub.status.idle":"2023-02-10T06:48:21.14534Z","shell.execute_reply.started":"2023-02-10T06:48:21.127625Z","shell.execute_reply":"2023-02-10T06:48:21.14453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    import pylibjpeg\nexcept:\n    !pip install -q /kaggle/input/rsna-2022-whl/{pydicom-2.3.0-py3-none-any.whl,pylibjpeg-1.4.0-py3-none-any.whl,python_gdcm-3.0.15-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl}\n    !pip install -q /kaggle/input/rsna-bcd-whl-ds/python_gdcm-3.0.20-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n    # !pip install -q /kaggle/input/rsna-bcd-whl-ds/pylibjpeg-1.4.0-py3-none-any.whl\n    !pip install -q /kaggle/input/rsna-bcd-whl-ds/dicomsdl-0.109.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:48:21.147681Z","iopub.execute_input":"2023-02-10T06:48:21.148759Z","iopub.status.idle":"2023-02-10T06:49:57.407715Z","shell.execute_reply.started":"2023-02-10T06:48:21.148723Z","shell.execute_reply":"2023-02-10T06:49:57.406582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport torch\nimport torchvision\nimport torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:49:57.410187Z","iopub.execute_input":"2023-02-10T06:49:57.410595Z","iopub.status.idle":"2023-02-10T06:49:59.274814Z","shell.execute_reply.started":"2023-02-10T06:49:57.410552Z","shell.execute_reply":"2023-02-10T06:49:59.273827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageClassificationBase(nn.Module):\n    def training_step(self, batch):\n        images, labels = batch \n        out = self(images)\n        k  = nn.Sigmoid()\n        out = k(out)\n        out = torch.squeeze(out,1)\n        loss = F.binary_cross_entropy(out, labels) # Calculate loss\n        return loss\n    \n    def validation_step(self, batch):\n        images, labels = batch \n        out = self(images)\n        k  = nn.Sigmoid()\n        out = k(out)\n        out = torch.squeeze(out,1)\n        #print(out)\n        loss = F.binary_cross_entropy(out, labels)\n        acc = accuracy(out, labels)           # Calculate accuracy\n        return {'val_loss': loss.detach(), 'val_acc': acc}\n        \n    def validation_epoch_end(self, outputs):\n        batch_losses = [x['val_loss'] for x in outputs]\n        epoch_loss = torch.stack(batch_losses).mean()   # Combine losses\n        batch_accs = [x['val_acc'] for x in outputs]\n        epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies\n        return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}\n    \n    def epoch_end(self, epoch, result):\n         print(\"Epoch [{}], train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}\".format(\n            epoch, result['train_loss'], result['val_loss'], result['val_acc']))\n        \ndef accuracy(outputs, labels):\n#     _, preds = torch.max(outputs, dim=1)\n#     print(outputs)\n#     print(labels)\n    for i in range(len(outputs)):\n        if (outputs[i]>0.5):\n            outputs[i] = 1\n        else:\n            outputs[i] = 0\n    return torch.tensor(torch.sum(outputs == labels).item() / len(outputs))","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:49:59.276239Z","iopub.execute_input":"2023-02-10T06:49:59.278201Z","iopub.status.idle":"2023-02-10T06:49:59.290077Z","shell.execute_reply.started":"2023-02-10T06:49:59.278161Z","shell.execute_reply":"2023-02-10T06:49:59.289106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def conv_block(in_channels, out_channels, pool=False):\n    layers = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), \n              nn.BatchNorm2d(out_channels), \n              nn.ReLU(inplace=True)]\n    if pool: layers.append(nn.MaxPool2d(2))\n    return nn.Sequential(*layers)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:49:59.29334Z","iopub.execute_input":"2023-02-10T06:49:59.293749Z","iopub.status.idle":"2023-02-10T06:49:59.307359Z","shell.execute_reply.started":"2023-02-10T06:49:59.293711Z","shell.execute_reply":"2023-02-10T06:49:59.306403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet9(ImageClassificationBase):\n    def __init__(self, in_channels, num_classes):\n        super().__init__()\n        \n        self.conv1 = conv_block(in_channels, 64)\n        self.conv2 = conv_block(64, 128, pool=True)\n        self.res1 = nn.Sequential(conv_block(128, 128), conv_block(128, 128))\n        \n        self.conv3 = conv_block(128, 256, pool=True)\n        self.conv4 = conv_block(256, 512, pool=True)\n        self.res2 = nn.Sequential(conv_block(512, 512), conv_block(512, 512))\n        \n        self.classifier = nn.Sequential(nn.AdaptiveMaxPool2d(1), \n                                        nn.Flatten(), \n                                        nn.Dropout(0.2),\n                                        nn.Linear(512, num_classes))\n        \n    def forward(self, xb):\n        out = self.conv1(xb)\n        out = self.conv2(out)\n        out = self.res1(out) + out\n        out = self.conv3(out)\n        out = self.conv4(out)\n        out = self.res2(out) + out\n        out = self.classifier(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:49:59.308809Z","iopub.execute_input":"2023-02-10T06:49:59.309284Z","iopub.status.idle":"2023-02-10T06:49:59.320843Z","shell.execute_reply.started":"2023-02-10T06:49:59.309246Z","shell.execute_reply":"2023-02-10T06:49:59.319839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_default_device():\n    \"\"\"Pick GPU if available, else CPU\"\"\"\n    if torch.cuda.is_available():\n        return torch.device('cuda')\n    else:\n        return torch.device('cpu')\n    \ndef to_device(data, device):\n    \"\"\"Move tensor(s) to chosen device\"\"\"\n    if isinstance(data, (list,tuple)):\n        return [to_device(x, device) for x in data]\n    return data.to(device, non_blocking=True)\n\nclass DeviceDataLoader():\n    \"\"\"Wrap a dataloader to move data to a device\"\"\"\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        \"\"\"Yield a batch of data after moving it to device\"\"\"\n        for b in self.dl: \n            yield to_device(b, self.device)\n\n    def __len__(self):\n        \"\"\"Number of batches\"\"\"\n        return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:49:59.322353Z","iopub.execute_input":"2023-02-10T06:49:59.322769Z","iopub.status.idle":"2023-02-10T06:49:59.334888Z","shell.execute_reply.started":"2023-02-10T06:49:59.322733Z","shell.execute_reply":"2023-02-10T06:49:59.334001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = get_default_device()\nmodel = to_device(ResNet9(3, 1), device) ## 1 ---no of input channels rgb\nmodel","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:49:59.336112Z","iopub.execute_input":"2023-02-10T06:49:59.336536Z","iopub.status.idle":"2023-02-10T06:50:02.397339Z","shell.execute_reply.started":"2023-02-10T06:49:59.336501Z","shell.execute_reply":"2023-02-10T06:50:02.396263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"/kaggle/input/resnet9/check1.pth\", map_location=device))\n#model = torch.load(\"check.pth\")\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:02.398793Z","iopub.execute_input":"2023-02-10T06:50:02.399497Z","iopub.status.idle":"2023-02-10T06:50:02.708273Z","shell.execute_reply.started":"2023-02-10T06:50:02.399459Z","shell.execute_reply":"2023-02-10T06:50:02.706731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:02.712336Z","iopub.execute_input":"2023-02-10T06:50:02.712986Z","iopub.status.idle":"2023-02-10T06:50:02.736558Z","shell.execute_reply.started":"2023-02-10T06:50:02.71295Z","shell.execute_reply":"2023-02-10T06:50:02.735709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PNG_TEST_IMAGES_PATH = f'/kaggle/working/test'\nDCM_TEST_IMAGES_PATH = f'/kaggle/input/rsna-breast-cancer-detection/test_images'","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:02.740503Z","iopub.execute_input":"2023-02-10T06:50:02.743025Z","iopub.status.idle":"2023-02-10T06:50:02.749071Z","shell.execute_reply.started":"2023-02-10T06:50:02.742988Z","shell.execute_reply":"2023-02-10T06:50:02.748025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor\nimport re\nimport pydicom\nfrom tqdm import tqdm\nimport glob\nimport cv2\nimport os\n\ndef fit_image(fname, size=1024):\n    # 1. Read, resize\n    \n    \n    patient = fname.split('/')[-2]\n    image = fname.split('/')[-1][:-4]\n    dicom = pydicom.dcmread(fname)\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 = cv2.resize(img, (size, size))\n    \n    # 2. Crop\n    X = img\n    # Some images have narrow exterior \"frames\" that complicate selection of the main data. Cutting off the frame\n    X = X[5:-5, 5:-5]\n    \n    \n    # regions of non-empty pixels\n    output= cv2.connectedComponentsWithStats((X > 0.05).astype(np.uint8)[:, :], 8, cv2.CV_32S)\n    stats = output[2]\n    \n    # finding max area which always corresponds to the breast data. \n    idx = stats[1:, 4].argmax() + 1\n    x1, y1, w, h = stats[idx][:4]\n    x2 = x1 + w\n    y2 = y1 + h\n    \n    # cutting out the breast data\n    X_fit = X[y1: y2, x1: x2]    \n    patient_id, im_id = os.path.basename(os.path.dirname(fname)), os.path.basename(fname)[:-4]\n    os.makedirs(f'{PNG_TEST_IMAGES_PATH}/{patient_id}', exist_ok=True)\n    cv2.imwrite(f'{PNG_TEST_IMAGES_PATH}/{patient_id}/{im_id}.png', (X_fit[:, :] * 255).astype(np.uint8))\n\ndef fit_all_images(all_images):\n    with ThreadPoolExecutor(2) as p:\n        for i in tqdm(p.map(fit_image, all_images), total=len(all_images)):\n            pass\n\nall_images = glob.glob('/kaggle/input/rsna-breast-cancer-detection/train_images/*/*') \n# all_images = glob.glob('/kaggle/input/rsna-breast-cancer-detection/train_images/10006/*')\nfit_all_images(all_images)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:02.753812Z","iopub.execute_input":"2023-02-10T06:50:02.756704Z","iopub.status.idle":"2023-02-10T06:50:05.76326Z","shell.execute_reply.started":"2023-02-10T06:50:02.756636Z","shell.execute_reply":"2023-02-10T06:50:05.762181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# x = Image.open(\"/kaggle/working/test/10008/1591370361.png\")\n# np.array(x).shape","metadata":{"execution":{"iopub.status.busy":"2023-02-10T05:58:54.132552Z","iopub.execute_input":"2023-02-10T05:58:54.133029Z","iopub.status.idle":"2023-02-10T05:58:54.146712Z","shell.execute_reply.started":"2023-02-10T05:58:54.132991Z","shell.execute_reply":"2023-02-10T05:58:54.145625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img_path ='/kaggle/working/test/'","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:27.009465Z","iopub.execute_input":"2023-02-10T06:50:27.009945Z","iopub.status.idle":"2023-02-10T06:50:27.015241Z","shell.execute_reply.started":"2023-02-10T06:50:27.009901Z","shell.execute_reply":"2023-02-10T06:50:27.014116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.listdir(\"/kaggle/working/test/10008/\")","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:41:28.627766Z","iopub.execute_input":"2023-02-10T06:41:28.628216Z","iopub.status.idle":"2023-02-10T06:41:28.636339Z","shell.execute_reply.started":"2023-02-10T06:41:28.628164Z","shell.execute_reply":"2023-02-10T06:41:28.635293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:29.857866Z","iopub.execute_input":"2023-02-10T06:50:29.858242Z","iopub.status.idle":"2023-02-10T06:50:29.877787Z","shell.execute_reply.started":"2023-02-10T06:50:29.858213Z","shell.execute_reply":"2023-02-10T06:50:29.876635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path = test_img_path\nall_paths = []\nfor k in tqdm(range(len(test_df))):\n    row = test_df.iloc[k, :]\n    all_paths.append(base_path  +str(row.patient_id)+\"/\"+str(row.image_id) +\".png\")\n    \ntest_df[\"path\"] = all_paths","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:39.538484Z","iopub.execute_input":"2023-02-10T06:50:39.53888Z","iopub.status.idle":"2023-02-10T06:50:39.555525Z","shell.execute_reply.started":"2023-02-10T06:50:39.538848Z","shell.execute_reply":"2023-02-10T06:50:39.55451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:42.289119Z","iopub.execute_input":"2023-02-10T06:50:42.289491Z","iopub.status.idle":"2023-02-10T06:50:42.302483Z","shell.execute_reply.started":"2023-02-10T06:50:42.289461Z","shell.execute_reply":"2023-02-10T06:50:42.301455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_test = test_df[\"path\"] ","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:53.28603Z","iopub.execute_input":"2023-02-10T06:50:53.2864Z","iopub.status.idle":"2023-02-10T06:50:53.291319Z","shell.execute_reply.started":"2023-02-10T06:50:53.286368Z","shell.execute_reply":"2023-02-10T06:50:53.290263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader, Dataset","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:55.452419Z","iopub.execute_input":"2023-02-10T06:50:55.452818Z","iopub.status.idle":"2023-02-10T06:50:55.457991Z","shell.execute_reply.started":"2023-02-10T06:50:55.45278Z","shell.execute_reply":"2023-02-10T06:50:55.456845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RSNATestDataset(Dataset):\n    \n    def __init__(self, df, img_path, transforms=None):\n        self.df = df\n        self.img_path = img_path\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        file = self.img_path[idx]\n        #print(file)\n        file = Image.open(file).convert('RGB')\n        X = self.transforms(file)\n        #Y = self.df.loc[idx,'cancer']\n        return X.float()","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:55.605457Z","iopub.execute_input":"2023-02-10T06:50:55.606577Z","iopub.status.idle":"2023-02-10T06:50:55.614296Z","shell.execute_reply.started":"2023-02-10T06:50:55.606535Z","shell.execute_reply":"2023-02-10T06:50:55.613352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nfrom PIL import Image\n\ndef get_transforms(aug=False):\n\n    def transforms(img):\n#         img = img.convert('RGB')#.resize((512, 512))\n        if aug:\n            tfm = [\n                torchvision.transforms.RandomHorizontalFlip(0.5),\n                torchvision.transforms.RandomRotation(degrees=(-5, 5)), \n                torchvision.transforms.RandomResizedCrop((1024, 512), scale=(0.8, 1), ratio=(0.45, 0.55)) \n            ]\n        else:\n            tfm = [\n                torchvision.transforms.RandomHorizontalFlip(0.5),\n                torchvision.transforms.Resize((256, 256))\n            ]\n        img = torchvision.transforms.Compose(tfm + [            \n            torchvision.transforms.ToTensor(),\n            torchvision.transforms.Normalize(mean=0.2179, std=0.0529),\n            \n        ])(img)\n        return img\n\n    return lambda img: transforms(img)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:55.744725Z","iopub.execute_input":"2023-02-10T06:50:55.745081Z","iopub.status.idle":"2023-02-10T06:50:55.753001Z","shell.execute_reply.started":"2023-02-10T06:50:55.745052Z","shell.execute_reply":"2023-02-10T06:50:55.751861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = RSNATestDataset(test_df,path_test,get_transforms(False))","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:55.928272Z","iopub.execute_input":"2023-02-10T06:50:55.928643Z","iopub.status.idle":"2023-02-10T06:50:55.933777Z","shell.execute_reply.started":"2023-02-10T06:50:55.928611Z","shell.execute_reply":"2023-02-10T06:50:55.932776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:57.266358Z","iopub.execute_input":"2023-02-10T06:50:57.266749Z","iopub.status.idle":"2023-02-10T06:50:57.300398Z","shell.execute_reply.started":"2023-02-10T06:50:57.266715Z","shell.execute_reply":"2023-02-10T06:50:57.299295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_default_device():\n#     \"\"\"Pick GPU if available, else CPU\"\"\"\n#     if torch.cuda.is_available():\n#         return torch.device('cuda')\n#     else:\n#         return torch.device('cpu')\n    \n# def to_device(data, device):\n#     \"\"\"Move tensor(s) to chosen device\"\"\"\n#     if isinstance(data, (list,tuple)):\n#         return [to_device(x, device) for x in data]\n#     return data.to(device, non_blocking=True)\n\n# class DeviceDataLoader():\n#     \"\"\"Wrap a dataloader to move data to a device\"\"\"\n#     def __init__(self, dl, device):\n#         self.dl = dl\n#         self.device = device\n        \n#     def __iter__(self):\n#         \"\"\"Yield a batch of data after moving it to device\"\"\"\n#         for b in self.dl: \n#             yield to_device(b, self.device)\n\n#     def __len__(self):\n#         \"\"\"Number of batches\"\"\"\n#         return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:50:58.320823Z","iopub.execute_input":"2023-02-10T06:50:58.321182Z","iopub.status.idle":"2023-02-10T06:50:58.328668Z","shell.execute_reply.started":"2023-02-10T06:50:58.321153Z","shell.execute_reply":"2023-02-10T06:50:58.327696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import torch\n# device = get_default_device()","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:00.988938Z","iopub.execute_input":"2023-02-10T06:51:00.989307Z","iopub.status.idle":"2023-02-10T06:51:00.99381Z","shell.execute_reply.started":"2023-02-10T06:51:00.989276Z","shell.execute_reply":"2023-02-10T06:51:00.992684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_default_device():\n#     \"\"\"Pick GPU if available, else CPU\"\"\"\n#     if torch.cuda.is_available():\n#         return torch.device('cuda')\n#     else:\n#         return torch.device('cpu')\n    \n# def to_device(data, device):\n#     \"\"\"Move tensor(s) to chosen device\"\"\"\n#     if isinstance(data, (list,tuple)):\n#         return [to_device(x, device) for x in data]\n#     return data.to(device, non_blocking=True)\n\n# class DeviceDataLoader():\n#     \"\"\"Wrap a dataloader to move data to a device\"\"\"\n#     def __init__(self, dl, device):\n#         self.dl = dl\n#         self.device = device\n        \n#     def __iter__(self):\n#         \"\"\"Yield a batch of data after moving it to device\"\"\"\n#         for b in self.dl: \n#             yield to_device(b, self.device)\n\n#     def __len__(self):\n#         \"\"\"Number of batches\"\"\"\n#         return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:01.392528Z","iopub.execute_input":"2023-02-10T06:51:01.393214Z","iopub.status.idle":"2023-02-10T06:51:01.398126Z","shell.execute_reply.started":"2023-02-10T06:51:01.393178Z","shell.execute_reply":"2023-02-10T06:51:01.396896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# device = get_default_device()\n","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:01.892825Z","iopub.execute_input":"2023-02-10T06:51:01.893931Z","iopub.status.idle":"2023-02-10T06:51:01.899106Z","shell.execute_reply.started":"2023-02-10T06:51:01.893883Z","shell.execute_reply":"2023-02-10T06:51:01.897868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader = DataLoader(test_ds, batch_size=16,shuffle=False,num_workers=2,pin_memory=True)\ntest_dl_final = DeviceDataLoader(test_loader,device)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:02.275296Z","iopub.execute_input":"2023-02-10T06:51:02.275916Z","iopub.status.idle":"2023-02-10T06:51:02.281499Z","shell.execute_reply.started":"2023-02-10T06:51:02.275879Z","shell.execute_reply":"2023-02-10T06:51:02.280509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = next(iter(test_dl_final))\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:05.225812Z","iopub.execute_input":"2023-02-10T06:51:05.226257Z","iopub.status.idle":"2023-02-10T06:51:05.472177Z","shell.execute_reply.started":"2023-02-10T06:51:05.22622Z","shell.execute_reply":"2023-02-10T06:51:05.471002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_pred = []\nfor image in test_dl_final:\n    \n    pred = model(image).cpu().detach().numpy()\n    # print(pred.cpu().detach().numpy())\n    for i in range(len(pred)):\n        cnn_pred.append(pred[i])","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:17.538096Z","iopub.execute_input":"2023-02-10T06:51:17.538539Z","iopub.status.idle":"2023-02-10T06:51:23.624877Z","shell.execute_reply.started":"2023-02-10T06:51:17.5385Z","shell.execute_reply":"2023-02-10T06:51:23.623562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_pred","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:27.196868Z","iopub.execute_input":"2023-02-10T06:51:27.197263Z","iopub.status.idle":"2023-02-10T06:51:27.206502Z","shell.execute_reply.started":"2023-02-10T06:51:27.197222Z","shell.execute_reply":"2023-02-10T06:51:27.205225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_pred[0]","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:38.488292Z","iopub.execute_input":"2023-02-10T06:51:38.488708Z","iopub.status.idle":"2023-02-10T06:51:38.496233Z","shell.execute_reply.started":"2023-02-10T06:51:38.488662Z","shell.execute_reply":"2023-02-10T06:51:38.495268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans = []\nfor i in range(len(cnn_pred)):\n    x = torch.from_numpy(cnn_pred[i])\n    m = nn.Sigmoid()\n    z = m(x)\n    ans.append(z)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:39.626456Z","iopub.execute_input":"2023-02-10T06:51:39.62716Z","iopub.status.idle":"2023-02-10T06:51:39.6342Z","shell.execute_reply.started":"2023-02-10T06:51:39.627115Z","shell.execute_reply":"2023-02-10T06:51:39.633285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:45.577061Z","iopub.execute_input":"2023-02-10T06:51:45.57744Z","iopub.status.idle":"2023-02-10T06:51:45.586912Z","shell.execute_reply.started":"2023-02-10T06:51:45.577408Z","shell.execute_reply":"2023-02-10T06:51:45.585853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = test_df\ndf_test['cancer'] = ans\ndf_sub = df_test.groupby('prediction_id')[['cancer']].mean()\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:48.495347Z","iopub.execute_input":"2023-02-10T06:51:48.495768Z","iopub.status.idle":"2023-02-10T06:51:48.522975Z","shell.execute_reply.started":"2023-02-10T06:51:48.495734Z","shell.execute_reply":"2023-02-10T06:51:48.521842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"THRES = .3\ndf_sub['cancer'] = (df_sub.cancer > THRES).astype(float)\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:52.339013Z","iopub.execute_input":"2023-02-10T06:51:52.339386Z","iopub.status.idle":"2023-02-10T06:51:52.349955Z","shell.execute_reply.started":"2023-02-10T06:51:52.339353Z","shell.execute_reply":"2023-02-10T06:51:52.348723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('submission.csv', index=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-10T06:51:55.194004Z","iopub.execute_input":"2023-02-10T06:51:55.194431Z","iopub.status.idle":"2023-02-10T06:51:55.203637Z","shell.execute_reply.started":"2023-02-10T06:51:55.194393Z","shell.execute_reply":"2023-02-10T06:51:55.202454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}