{"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":"from __future__ import division\nimport argparse\nimport torch\nimport torch.nn.functional as F\nimport torchvision.datasets as dset\nimport torchvision.transforms as T\nimport torchvision.models as models\nimport pandas as pd\nimport os\nimport pydicom\nimport numpy as np\nimport h5py\nimport time\nfrom tqdm import tqdm as tqdm\nimport cv2\nimport pickle\nfrom shutil import copyfile\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport random\n\nfrom torch.utils.data.sampler import SubsetRandomSampler\n","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":5.384072,"end_time":"2023-08-26T18:07:46.650721","exception":false,"start_time":"2023-08-26T18:07:41.266649","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:45:49.489943Z","iopub.execute_input":"2023-08-28T09:45:49.490268Z","iopub.status.idle":"2023-08-28T09:45:54.709248Z","shell.execute_reply.started":"2023-08-28T09:45:49.49024Z","shell.execute_reply":"2023-08-28T09:45:54.707992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport random\nimport torch\n\nclass DataProcessor:\n    def __init__(self, csv_file, split, samples_per_split, transform):\n        self.pedataframe = self.load_dataframe(csv_file)\n        self.pos_df = self.filter_positive_samples(self.pedataframe)\n        self.split_df = self.get_split_dataframe(self.pedataframe, split, samples_per_split)\n        self.neg_df = self.filter_negative_samples(self.split_df)\n        self.transform = transform\n\n    def load_dataframe(self, csv_file):\n        return pd.read_csv(csv_file)\n\n    def filter_positive_samples(self, dataframe):\n        return dataframe[dataframe.pe_present_on_image == 1]\n\n    def get_split_dataframe(self, dataframe, split, samples_per_split):\n        start_idx = split * samples_per_split\n        end_idx = (split + 1) * samples_per_split\n        return dataframe.iloc[start_idx:end_idx]\n\n    def filter_negative_samples(self, dataframe):\n        return dataframe[dataframe.pe_present_on_image == 0]\n    \n    \n    \nclass ImageProcessor:\n    def __init__(self, data_processor):\n        self.data_processor = data_processor\n\n    def crop(self, crop_size, img, randomize=False):\n        if randomize:\n            row = random.randint(0, img.shape[-2] - crop_size)\n            col = random.randint(0, img.shape[-1] - crop_size)\n        else:\n            row = (img.shape[-2] - crop_size) // 2\n            col = (img.shape[-1] - crop_size) // 2\n        \n        img = img[row : row + crop_size, col : col + crop_size]\n        return img\n    \n    def flip(self, img, randomize=False):\n        if randomize:\n            if random.randint(0, 1) == 0:\n                img = np.copy(np.flipud(img))\n        return img\n    \n    \n","metadata":{"papermill":{"duration":0.07994,"end_time":"2023-08-26T18:07:46.794703","exception":false,"start_time":"2023-08-26T18:07:46.714763","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:46:02.889317Z","iopub.execute_input":"2023-08-28T09:46:02.889694Z","iopub.status.idle":"2023-08-28T09:46:02.905175Z","shell.execute_reply.started":"2023-08-28T09:46:02.889649Z","shell.execute_reply":"2023-08-28T09:46:02.904137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport numpy as np\nfrom skimage.transform import resize\n\ndef resize_dicom_image_1(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n        \n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        return None\n\n\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/0003b3d648eb/d2b2960c2bbf/5aca0cdd0dce.dcm'  # Replace with the actual path to your DICOM image\n\nnew_size = (256, 256)\nresized_image = resize_dicom_image_1(dicom_filepath, new_size)\n\nif resized_image is not None:\n    print(\"Resized image shape:\", resized_image.shape)\n    ","metadata":{"papermill":{"duration":0.396672,"end_time":"2023-08-26T18:07:47.253702","exception":false,"start_time":"2023-08-26T18:07:46.85703","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:46:53.949787Z","iopub.execute_input":"2023-08-28T09:46:53.950244Z","iopub.status.idle":"2023-08-28T09:46:53.998852Z","shell.execute_reply.started":"2023-08-28T09:46:53.950207Z","shell.execute_reply":"2023-08-28T09:46:53.997951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if resized_image is not None:\n    print(\"Resized image shape:\", resized_image.shape)\n\n    # Plot the resized DICOM image\n    plt.imshow(resized_image, cmap='gray')\n    plt.title('Resized DICOM Image')\n    plt.axis('off')  # Turn off axis labels\n    plt.show()","metadata":{"papermill":{"duration":0.354482,"end_time":"2023-08-26T18:07:47.671057","exception":false,"start_time":"2023-08-26T18:07:47.316575","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:46:34.389378Z","iopub.execute_input":"2023-08-27T07:46:34.390167Z","iopub.status.idle":"2023-08-27T07:46:34.630017Z","shell.execute_reply.started":"2023-08-27T07:46:34.390131Z","shell.execute_reply":"2023-08-27T07:46:34.629075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"  \ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n        \n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n","metadata":{"papermill":{"duration":0.073566,"end_time":"2023-08-26T18:07:47.808025","exception":false,"start_time":"2023-08-26T18:07:47.734459","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:46:59.598802Z","iopub.execute_input":"2023-08-28T09:46:59.599498Z","iopub.status.idle":"2023-08-28T09:46:59.605853Z","shell.execute_reply.started":"2023-08-28T09:46:59.599463Z","shell.execute_reply":"2023-08-28T09:46:59.604903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n# Check if GPU is available\nif torch.cuda.is_available():\n    device = torch.device(\"cuda\")\n    print(\"CUDA is available. Using GPU.\")\nelse:\n    device = torch.device(\"cpu\")\n    print(\"CUDA is not available. Using CPU.\")\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T09:47:04.730992Z","iopub.execute_input":"2023-08-28T09:47:04.731352Z","iopub.status.idle":"2023-08-28T09:47:04.760833Z","shell.execute_reply.started":"2023-08-28T09:47:04.731323Z","shell.execute_reply":"2023-08-28T09:47:04.759901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#path_to_dcm = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/00511e94edec/297f170f1197/'\npath_to_nii = '/kaggle/working/'\nnew_nifti_name = 'New.nii.gz'\n\nresult = convert_and_rename_dicom_to_nifti(path_to_dcm, path_to_nii, new_nifti_name)\n\n\nimport shutil\n\ndef delete_folder(folder_path):\n    try:\n        shutil.rmtree(folder_path)\n        print(f\"Folder '{folder_path}' has been deleted.\")\n    except OSError as e:\n        print(f\"Error deleting '{folder_path}': {e}\")\n\n\n\n\nfrom monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\n\ndef plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True):\n    if plot:\n        plt.figure(figsize=(20, 7))\n\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        center_y = original_image.shape[0] // 2\n        center_x = original_image.shape[1] // 2\n\n        ellipse_radius_x = 175\n        ellipse_radius_y = 370\n        ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(ellipse)\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]\n\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n        ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n        lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")\n\n        plt.tight_layout()\n        plt.show()\n        \n        return lung_mask\n\n\"\"\"# Example usage inside your loop\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0]\n        original_label = loader(test_files[idx]['label'])[0]\n\n        slice_idx = 157\n        plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True)\"\"\"\n\n        \n        \n\n\ndef apply_mask(image, mask):\n    masked_image = image * mask\n    return masked_image\n\n\n\n\n\ndef resize_dicom_image_lung(dicom_filepath, new_size, slice_idx):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n        \n        result = convert_and_rename_dicom_to_nifti(dicom_filepath, path_to_nii, new_nifti_name)\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n        \n        with torch.no_grad():\n            test_inputs = torch.tensor(resized_img).unsqueeze(0).unsqueeze(0).to(device)\n            test_data = [{\"image\": test_inputs}]\n            test_data[0][\"pred\"] = sliding_window_inference(test_data[0][\"image\"], (64, 64, 64), 1, model, overlap=0.75)\n            test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n            test_output = from_engine([\"pred\"])(test_data)\n\n            original_image = torch.tensor(resized_img)\n            original_label = loader(test_files[0]['label'])[0]  # Note: Index 0 used here\n\n            lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]\n            resized_lung_mask = resize(lung_mask, new_size, anti_aliasing=True)\n            masked_resized_img = apply_mask(resized_img, resized_lung_mask)\n\n            x = plot_results(masked_resized_img, original_label.detach().cpu().numpy()[:,:,slice_idx], test_output, 0, slice_idx, plot=False)\n            \n            folder_path = '/kaggle/working/New.nii.gz'\n            delete_folder(folder_path)\n            \n           \n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        \n    return plot_results(masked_resized_img, original_label.detach().cpu().numpy()[:,:,slice_idx], test_output, 0, slice_idx, plot=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:34:22.581054Z","iopub.execute_input":"2023-08-27T09:34:22.581455Z","iopub.status.idle":"2023-08-27T09:34:42.162704Z","shell.execute_reply.started":"2023-08-27T09:34:22.581423Z","shell.execute_reply":"2023-08-27T09:34:42.161676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n# Check if GPU is available\nif torch.cuda.is_available():\n    device = torch.device(\"cuda\")\n    print(\"CUDA is available. Using GPU.\")\nelse:\n    device = torch.device(\"cpu\")\n    print(\"CUDA is not available. Using CPU.\")\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:29:37.105872Z","iopub.execute_input":"2023-08-27T13:29:37.106256Z","iopub.status.idle":"2023-08-27T13:29:37.113206Z","shell.execute_reply.started":"2023-08-27T13:29:37.106226Z","shell.execute_reply":"2023-08-27T13:29:37.112022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Good\n\n\n\n\n#path_to_dcm = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/00511e94edec/297f170f1197/'\npath_to_nii = '/kaggle/working/'\nnew_nifti_name = 'New.nii.gz'\n\n#result = convert_and_rename_dicom_to_nifti(path_to_dcm, path_to_nii, new_nifti_name)\n\n\nimport shutil\n\nimport os\n\ndef delete_file(file_path):\n    try:\n        os.remove(file_path)\n        print(f\"File '{file_path}' has been deleted.\")\n    except OSError as e:\n        print(f\"Error deleting '{file_path}': {e}\")\n\n        \n\n\n\n\n\nfrom monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\n\ndef plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True):\n    if plot:\n        plt.figure(figsize=(20, 7))\n\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        center_y = original_image.shape[0] // 2\n        center_x = original_image.shape[1] // 2\n\n        ellipse_radius_x = 175\n        ellipse_radius_y = 370\n        ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(ellipse)\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]\n\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n        ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n        lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")\n\n        plt.tight_layout()\n        plt.show()\n        \n        return lung_mask\n\n# Example usage inside your loop\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0]\n        original_label = loader(test_files[idx]['label'])[0]\n\n        slice_idx = 157\n        plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True)\n\n        \n        \n\n\ndef apply_mask(image, mask):\n    masked_image = image * mask\n    return masked_image\n\n\n\n\ndef resize_dicom_image_lung(dicom_filepath, new_size, slice_idx, data_dir_im, data_dir_lb):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n        \n        result = convert_and_rename_dicom_to_nifti(dicom_filepath, path_to_nii, new_nifti_name)\n        \n        \n        #data_dir_im = '/kaggle/working/'\n        #data_dir_lb = '/kaggle/working/'\n        #images = sorted(\n            #glob.glob(os.path.join(data_dir_im, \"*.nii.gz\")))\n        #labels = sorted(\n            #glob.glob(os.path.join(data_dir_lb, \"*.nii.gz\")))\n\n        data_dicts = [\n            {\"image\": image_name, \"label\": label_name}\n            for image_name, label_name in zip(images, labels)\n        ]\n\n        train_files, val_files, test_files = data_dicts[:1], data_dicts[:1], data_dicts[:1]\n        \n        test_ds = Dataset(data=test_files, transform=val_transforms)\n        test_loader = DataLoader(test_ds, batch_size=1)\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n        \n        \n  \n        \n        \n        with torch.no_grad():\n            for idx, test_data in enumerate(test_loader):\n                test_inputs = test_data[\"image\"].to(device)\n                test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n                test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n                test_output = from_engine([\"pred\"])(test_data)\n\n                original_image = loader(test_files[idx]['image'])[0] # Load the original image\n                original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n                lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]\n                resized_lung_mask = resize(lung_mask, new_size, anti_aliasing=True)\n                masked_resized_img = apply_mask(resized_img, resized_lung_mask)\n\n            x = plot_results(masked_resized_img, original_label.detach().cpu().numpy()[:,:,slice_idx], test_output, 0, slice_idx, plot=False)\n            \n            file_path = '/kaggle/working/New.nii.gz'\n            delete_file(file_path)\n    \n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        # Add the missing 'except' block here to handle exceptions\n    \n    # Default value\n    x = resize_dicom_image(dicom_filepath, new_size)\n\n    return x\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:04:29.86183Z","iopub.execute_input":"2023-08-27T10:04:29.862387Z","iopub.status.idle":"2023-08-27T10:05:18.822334Z","shell.execute_reply.started":"2023-08-27T10:04:29.862334Z","shell.execute_reply":"2023-08-27T10:05:18.821268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n#path_to_dcm = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/00511e94edec/297f170f1197/'\npath_to_nii = '/kaggle/working/'\nnew_nifti_name = 'New.nii.gz'\n\n#result = convert_and_rename_dicom_to_nifti(path_to_dcm, path_to_nii, new_nifti_name)\n\n\nimport shutil\n\nimport os\n\ndef delete_file(file_path):\n    try:\n        os.remove(file_path)\n        print(f\"File '{file_path}' has been deleted.\")\n    except OSError as e:\n        print(f\"Error deleting '{file_path}': {e}\")\n\n        \n\n\n\n\n\nfrom monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\n\ndef plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True):\n    if plot:\n        plt.figure(figsize=(20, 7))\n\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        center_y = original_image.shape[0] // 2\n        center_x = original_image.shape[1] // 2\n\n        ellipse_radius_x = 175\n        ellipse_radius_y = 370\n        ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(ellipse)\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]\n\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n        ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n        lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")\n\n        plt.tight_layout()\n        plt.show()\n        \n        return lung_mask\n\n# Example usage inside your loop\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0]\n        original_label = loader(test_files[idx]['label'])[0]\n\n        slice_idx = 157\n        plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True)\n\n        \n        \n\n\ndef apply_mask(image, mask):\n    masked_image = image * mask\n    return masked_image\n\n\n\n\ndef resize_dicom_image_lung(dicom_filepath, new_size, slice_idx, data_dir_im, data_dir_lb):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n        \n        result = convert_and_rename_dicom_to_nifti(dicom_filepath, path_to_nii, new_nifti_name)\n        \n        \n        #data_dir_im = '/kaggle/working/'\n        #data_dir_lb = '/kaggle/working/'\n        #images = sorted(\n            #glob.glob(os.path.join(data_dir_im, \"*.nii.gz\")))\n        #labels = sorted(\n            #glob.glob(os.path.join(data_dir_lb, \"*.nii.gz\")))\n\n        data_dicts = [\n            {\"image\": image_name, \"label\": label_name}\n            for image_name, label_name in zip(images, labels)\n        ]\n\n        train_files, val_files, test_files = data_dicts[:1], data_dicts[:1], data_dicts[:1]\n        \n        test_ds = Dataset(data=test_files, transform=val_transforms)\n        test_loader = DataLoader(test_ds, batch_size=1)\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n        \n        \n  \n        \n        \n        with torch.no_grad():\n            for idx, test_data in enumerate(test_loader):\n                test_inputs = test_data[\"image\"].to(device)\n                test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n                test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n                test_output = from_engine([\"pred\"])(test_data)\n\n                original_image = loader(test_files[idx]['image'])[0] # Load the original image\n                original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n                lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]\n                resized_lung_mask = resize(lung_mask, new_size, anti_aliasing=True)\n                masked_resized_img = apply_mask(resized_img, resized_lung_mask)\n\n            x = plot_results(masked_resized_img, original_label.detach().cpu().numpy()[:,:,slice_idx], test_output, 0, slice_idx, plot=False)\n            \n            file_path = '/kaggle/working/New.nii.gz'\n            delete_file(file_path)\n            \n    \n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n    return plot_results(masked_resized_img, original_label.detach().cpu().numpy()[:,:,slice_idx], test_output, 0, slice_idx, plot=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T09:41:22.139553Z","iopub.execute_input":"2023-08-27T09:41:22.139924Z","iopub.status.idle":"2023-08-27T09:42:11.146773Z","shell.execute_reply.started":"2023-08-27T09:41:22.139893Z","shell.execute_reply":"2023-08-27T09:42:11.145749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ndef find_file_position_in_directory(directory_path, file_name):\n    try:\n        file_list = sorted(os.listdir(directory_path))\n        file_position = file_list.index(file_name)\n        return file_position\n    except ValueError:\n        print(f\"File '{file_name}' not found in directory '{directory_path}'.\")\n        return None\n\n# Example usage\ndirectory_path = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/0003b3d648eb/d2b2960c2bbf/'\nfile_name = '0cbe2fc5870a.dcm'\nposition = find_file_position_in_directory(directory_path, file_name)\n#position","metadata":{"execution":{"iopub.status.busy":"2023-08-28T02:56:10.056227Z","iopub.execute_input":"2023-08-28T02:56:10.056651Z","iopub.status.idle":"2023-08-28T02:56:10.21062Z","shell.execute_reply.started":"2023-08-28T02:56:10.056613Z","shell.execute_reply":"2023-08-28T02:56:10.20968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 24_08_17h\n\n#KagglePEDataset(torch.utils.data.Dataset) Newname\n\n\n\n\n\n\n\n\nclass PulmoDataset(torch.utils.data.Dataset):\n    \"\"\"Kaggle PE dataset.\"\"\"\n\n    def __init__(self, csv_file, stage, transform=None, split=0):\n        self.data_processor = DataProcessor(csv_file, split,sp_pr_splt, transform)\n        self.image_processor = ImageProcessor(self.data_processor)\n        self.transform = transform\n        self.split_df = self.data_processor.split_df\n        self.stage = stage\n        self.pos_df = self.data_processor.pos_df\n        self.neg_df = self.data_processor.neg_df\n        \n        #self.pos_df = self.pedataframe[self.pedataframe.pe_present_on_image == 1]\n        #self.split_df = self.pedataframe[split*sp_pr_splt : (split+1)*sp_pr_splt]\n        #self.neg_df = self.split_df[self.split_df.pe_present_on_image == 0]\n\n    def __len__(self):\n        return len(self.data_processor.split_df)\n\n    def get_class_weights(self):\n        subset = self.data_processor.split_df\n        pos = subset[subset.pe_present_on_image == 1]\n        neg = subset[subset.pe_present_on_image == 0]\n        \n        return 1.0 / torch.tensor([len(neg), len(pos)], dtype=torch.float)\n\n    def get_targets(self):\n        return torch.tensor(self.data_processor.split_df.pe_present_on_image, dtype=torch.long)\n\n    def __getitem__(self, idx):\n        idx = np.clip(idx, 0, len(self.data_processor.split_df) - 1)\n        pe_present_on_image = int(self.data_processor.split_df.pe_present_on_image.iloc[idx])\n        data_identifier = self.data_processor.split_df.StudyInstanceUID.iloc[idx] + '/' + self.data_processor.split_df.SeriesInstanceUID.iloc[idx] + '/' + self.data_processor.split_df.SOPInstanceUID.iloc[idx]\n        \n        \n        #data_identifier = self.data_processor.split_df.StudyInstanceUID.iloc[idx] + '\\' + self.data_processor.split_df.SOPInstanceUID.iloc[idx]\n        img_path = os.path.join('/kaggle/input/rsna-str-pulmonary-embolism-detection/train', data_identifier + '.dcm')\n        \n        \n        #img_path = os.path.join(r'e:\\kaggle\\input\\rsna-str-pulmonary-embolism-detection\\train', data_identifier + '.dcm')\n\n        #img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)\n        #img = cv2.resize(img, (256, 256), interpolation=cv2.INTER_AREA)\n        \n        new_size = (256, 256)\n        img = resize_dicom_image(img_path,new_size )\n        \n        #print(\"Loaded image shape:\", img.shape) \n\n        if self.stage == 'train':\n            img = self.image_processor.crop(224, img, randomize=True)\n            img = self.image_processor.flip(img, randomize=True)\n        else:\n            img = self.image_processor.crop(224, img)\n\n        img = torch.tensor(img, dtype=torch.float).unsqueeze(0)\n\n        return self.transform(img), torch.tensor(pe_present_on_image, dtype=torch.float), idx\n\n","metadata":{"papermill":{"duration":0.080297,"end_time":"2023-08-26T18:07:47.951829","exception":false,"start_time":"2023-08-26T18:07:47.871532","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:47:32.753221Z","iopub.execute_input":"2023-08-28T09:47:32.753572Z","iopub.status.idle":"2023-08-28T09:47:32.769Z","shell.execute_reply.started":"2023-08-28T09:47:32.753542Z","shell.execute_reply":"2023-08-28T09:47:32.766276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.063566,"end_time":"2023-08-26T18:07:48.079677","exception":false,"start_time":"2023-08-26T18:07:48.016111","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n\n\ndef get_weighted_sampler(pe_dataset, stage='train'):\n    assert len(pe_dataset.pos_df) == valid_samples + train_samples\n    assert len(pe_dataset.split_df) == samples_per_split\n    assert len(pe_dataset.neg_df) > train_samples\n    \n    if stage == 'train':\n        pos_indices = np.array(pe_dataset.pos_df[:train_samples].index)\n    else:\n        pos_indices = np.array(pe_dataset.pos_df[train_samples:].index)\n        \n    neg_indices = np.array(pe_dataset.neg_df.index)\n    np.random.shuffle(neg_indices)\n    \n    if stage == 'train':\n        indices = np.concatenate((pos_indices, neg_indices[:train_samples]))\n    else:\n        indices = np.concatenate((pos_indices, neg_indices[:valid_samples]))\n        \n    np.random.shuffle(indices)\n    sampler = torch.utils.data.SubsetRandomSampler(indices)\n    \n    if stage == 'train':\n        assert len(sampler) == train_samples*2\n    else:\n        assert len(sampler) == valid_samples*2\n    \n    return sampler\n\"\"\"\n\n\nfrom torch.utils.data.sampler import SubsetRandomSampler\n\n\nvd_sp = 26540\nsp_pr_splt = 100000\ntr_sp = 70000\n\n\n\ndef assert_dataset_lengths(pulmo_dataset, vd_sp, tr_sp, sp_pr_splt):\n    assert len(pulmo_dataset.pos_df) == vd_sp + tr_sp\n    assert len(pulmo_dataset.split_df) == sp_pr_splt\n    assert len(pulmo_dataset.neg_df) > tr_sp\n\n    # New_name = get_weighted_sampler = get_splt\ndef get_splt(pulmo_dataset, stage='train'):\n    #assert_dataset_lengths(pulmo_dataset, vd_sp, tr_sp, sp_pr_splt)\n    \n    if stage == 'train':\n        pos_indices = np.array(pulmo_dataset.pos_df[:tr_sp].index)\n        neg_indices = np.array(pulmo_dataset.neg_df[tr_sp:].index)\n    else:\n        pos_indices = np.array(pulmo_dataset.pos_df[tr_sp:].index)\n        neg_indices = np.array(pulmo_dataset.neg_df[:vd_sp].index)\n    \n    indices = np.concatenate((pos_indices, neg_indices))\n    np.random.shuffle(indices)\n    \n    sampler = SubsetRandomSampler(indices)\n    \n    #assert len(sampler) == (tr_sp + vd_sp) * 2\n    \n    return sampler\n\n\n\n\n\n\"\"\"def get_splt(p_dataset, stage='train'):\n\n    assert len(p_dataset.pos_df) == vd_sp + tr_sp\n    assert len(p_dataset.split_df) == sp_pr_splt\n    assert len(p_dataset.neg_df) > tr_sp\n    \n    if stage == 'train':\n        pos_indices = np.array(p_dataset.pos_df[:tr_sp].index)\n    else:\n        pos_indices = np.array(p_dataset.pos_df[tr_sp:].index)\n        \n    neg_indices = np.array(p_dataset.neg_df.index)\n    np.random.shuffle(neg_indices)\n    \n    if stage == 'train':\n        indices = np.concatenate((pos_indices, neg_indices[:tr_sp]))\n    else:\n        indices = np.concatenate((pos_indices, neg_indices[:vd_sp]))\n        \n    np.random.shuffle(indices)\n    sampler = torch.utils.data.SubsetRandomSampler(indices)\n    \n    if stage == 'train':\n        assert len(sampler) == tr_sp*2\n    else:\n        assert len(sampler) == vd_sp*2\n    \n    return sampler\n\n\n\n\"\"\"","metadata":{"papermill":{"duration":0.079318,"end_time":"2023-08-26T18:07:48.222395","exception":false,"start_time":"2023-08-26T18:07:48.143077","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:48:02.169541Z","iopub.execute_input":"2023-08-28T09:48:02.170594Z","iopub.status.idle":"2023-08-28T09:48:02.206029Z","shell.execute_reply.started":"2023-08-28T09:48:02.170547Z","shell.execute_reply":"2023-08-28T09:48:02.203868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"#V2\n\nfrom torch.utils.data.sampler import SubsetRandomSampler\n\n\n\"\"\"\n\ntotal_data_length = len(pulmo_dataset.pos_df) + len(pulmo_dataset.neg_df)\n\n# Define the portions as fractions of the total data length\nvd_portion = 0.3  # 30% of the data\nsp_portion = 1  # 100% sample per split\ntr_portion = 0.7  # 70% of the data  training\n\nvd_sp = int(total_data_length * vd_portion)\nsp_pr_splt = int(total_data_length * sp_portion)\ntr_sp = int(total_data_length * tr_portion)\"\"\"\n\n\ndef assert_dataset_lengths(pulmo_dataset, vd_sp, tr_sp, sp_pr_splt):\n    print(\"pos_df length:\", len(pulmo_dataset.pos_df))\n    print(\"vd_sp + tr_sp:\", vd_sp + tr_sp)\n    #assert len(pulmo_dataset.pos_df) == vd_sp + tr_sp\n    #assert len(pulmo_dataset.split_df) == sp_pr_splt\n    #assert len(pulmo_dataset.neg_df) > tr_sp\n\n    \n\nfrom torch.utils.data.sampler import SubsetRandomSampler\n\n\nvd_sp = 26540\nsp_pr_splt = 100000\ntr_sp = 70000\n\n\ndef get_splt(pulmo_dataset, stage='train'):\n    assert_dataset_lengths(pulmo_dataset, vd_sp, tr_sp, sp_pr_splt)\n    \n    if stage == 'train':\n        pos_indices = np.array(pulmo_dataset.pos_df[:tr_sp].index)\n        neg_indices = np.array(pulmo_dataset.neg_df[:tr_sp].index)\n    else:\n        pos_indices = np.array(pulmo_dataset.pos_df[tr_sp:].index)\n        neg_indices = np.array(pulmo_dataset.neg_df[:vd_sp].index)\n    \n    indices = np.concatenate((pos_indices, neg_indices))\n    np.random.shuffle(indices)\n    \n    sampler = SubsetRandomSampler(indices)\n    \n    print(\"tr_sp:\", tr_sp)\n    print(\"vd_sp:\", vd_sp)\n    print(\"len(sampler):\", len(sampler))\n    \n    #assert len(sampler) == (tr_sp + vd_sp) * 2\n    \n    return sampler\n\"\"\"","metadata":{"papermill":{"duration":0.075365,"end_time":"2023-08-26T18:07:48.360853","exception":false,"start_time":"2023-08-26T18:07:48.285488","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T13:35:26.580696Z","iopub.execute_input":"2023-08-27T13:35:26.581128Z","iopub.status.idle":"2023-08-27T13:35:26.592961Z","shell.execute_reply.started":"2023-08-27T13:35:26.581098Z","shell.execute_reply":"2023-08-27T13:35:26.591922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"#V3\n\ndef assert_dataset_lengths(pos_df, split_df, neg_df, vd_sp, tr_sp, sp_pr_splt):\n    print(\"pos_df length:\", len(pos_df))\n    print(\"vd_sp + tr_sp:\", vd_sp + tr_sp)\n    #assert len(pos_df) == vd_sp + tr_sp\n    #assert len(split_df) == sp_pr_splt\n    #assert len(neg_df) > tr_sp\n\ndef get_splt(pulmo_dataset, stage='train'):\n    assert_dataset_lengths(\n        pulmo_dataset.pos_df, pulmo_dataset.split_df, pulmo_dataset.neg_df,\n        vd_sp, tr_sp, sp_pr_splt\n    )\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T18:07:48.499203Z","iopub.status.busy":"2023-08-26T18:07:48.498193Z","iopub.status.idle":"2023-08-26T18:07:48.505508Z","shell.execute_reply":"2023-08-26T18:07:48.50452Z"},"papermill":{"duration":0.083853,"end_time":"2023-08-26T18:07:48.507574","exception":false,"start_time":"2023-08-26T18:07:48.423721","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\"\"\"def get_loaders(epoch):\n    \n    # Each epoch is 100,000 samples.\n    # Rotate between 6 training sets and 2 validation sets.\n    # (600,000 training samples and 200,000 validation samples)\n    train_set_indices = [0, 1, 2, 3, 4, 5]\n    valid_set_indices = [6, 7]\n\n    # training dataset\n    train_split = train_set_indices[epoch % len(train_set_indices)]\n    print('Train split: ', train_split)\n    dataset_train = KagglePEDataset(csv_file=train_csv, \n                                    stage='train', \n                                    transform=transform, \n                                    split=train_split)\n    \n    # Weighted sampler (to balance the dataset between positive and negative)\n    train_sampler = get_weighted_sampler(dataset_train, stage='train')\n\n    # training loader\n    train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=16, \n        sampler=train_sampler, num_workers=1)\n    \n    # validation dataset\n    valid_split = valid_set_indices[epoch % len(valid_set_indices)]\n    print('Valid split: ', valid_split)\n    dataset_valid = KagglePEDataset(csv_file=train_csv, \n                                    stage='valid',\n                                    transform=transform, \n                                    split=valid_split)\n    \n    # validation loader\n    valid_sampler = get_weighted_sampler(dataset_valid, stage='valid')\n    valid_loader = torch.utils.data.DataLoader(dataset_valid, batch_size=16, \n        sampler=valid_sampler, num_workers=1)\n    \n    return train_loader, valid_loader\n    \n    \n\"\"\"\n\n\nbatch_size = 16\n\ndef loaders(epoch, batch_size=batch_size):\n    # Define the training and validation sets\n    tr_idc = [0, 1, 2, 3, 4, 5]\n    vd_idc = [6, 7]\n    \n    # Determine the current training and validation set indices\n    train_split = tr_idc[epoch % len(tr_idc)]\n    valid_split = vd_idc[epoch % len(vd_idc)]\n    \n    # Create the training dataset and loader\n    dataset_train = PulmoDataset(csv_file=train_csv, stage='train', transform=transform, split=train_split)\n    train_sampler = get_splt(dataset_train, stage='train')\n    train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=batch_size, sampler=train_sampler, num_workers=2)\n    \n    # Create the validation dataset and loader\n    dataset_valid = PulmoDataset(csv_file=train_csv, stage='valid', transform=transform, split=valid_split)\n    valid_sampler = get_splt(dataset_valid, stage='valid')\n    valid_loader = torch.utils.data.DataLoader(dataset_valid, batch_size=batch_size, sampler=valid_sampler, num_workers=2)\n    \n    return train_loader, valid_loader\n\n\n\n","metadata":{"papermill":{"duration":0.07835,"end_time":"2023-08-26T18:07:48.649407","exception":false,"start_time":"2023-08-26T18:07:48.571057","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:48:23.237791Z","iopub.execute_input":"2023-08-28T09:48:23.238264Z","iopub.status.idle":"2023-08-28T09:48:23.248147Z","shell.execute_reply.started":"2023-08-28T09:48:23.238219Z","shell.execute_reply":"2023-08-28T09:48:23.246972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install monai","metadata":{"papermill":{"duration":13.997805,"end_time":"2023-08-26T18:08:02.711103","exception":false,"start_time":"2023-08-26T18:07:48.713298","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:49:09.704648Z","iopub.execute_input":"2023-08-28T09:49:09.705041Z","iopub.status.idle":"2023-08-28T09:49:23.390874Z","shell.execute_reply.started":"2023-08-28T09:49:09.705009Z","shell.execute_reply":"2023-08-28T09:49:23.3897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lung","metadata":{}},{"cell_type":"code","source":"# Lung\n\n\nimport torch\nfrom torch.utils.data import DataLoader\n\nfrom monai.networks.nets import UNet\nfrom monai.networks.layers import Norm\nfrom monai.inferers import sliding_window_inference\n\n\n\nfrom monai.transforms import Compose, AsDiscrete\nfrom monai.data import Dataset, DataLoader\nfrom monai.inferers import sliding_window_inference\nfrom monai.losses import DiceLoss\nfrom monai.metrics import DiceMetric\nfrom monai.networks.nets import UNet\nfrom monai.networks.layers import Norm\nfrom monai.utils import set_determinism\nimport torch\nimport matplotlib.pyplot as plt\n\nset_determinism(seed=0)\n\ndevice = torch.device(\"cuda:0\")\n\nimport torch\nfrom monai.networks.nets import UNet\nfrom monai.networks.layers import Norm\n\ndef loaders_lung(epoch, batch_size=16):\n    # Your existing code for creating data loaders here\n    \n    # Load your trained U-Net model\n    model = UNet(\n        spatial_dims=3,\n        in_channels=1,\n        out_channels=2,\n        channels=(16, 32, 64, 128, 256, 512),\n        strides=(2, 2, 2, 2, 2),\n        num_res_units=2,\n        norm=Norm.BATCH,\n    ).to(device)\n    model.load_state_dict(torch.load('/kaggle/input/best-metric-model-08-06-12h-lung/best_metric_model_08_06_11h.pth'))\n    model.eval()\n\n    # Iterate through the training loader\n    for data in train_loader:\n        inputs, _ = data  # No need for targets, as you're performing inference\n\n        # Perform lung segmentation using the trained U-Net model\n        with torch.no_grad():\n            inputs = inputs.to(device)  # Move inputs to the correct device\n            lung_masks = model(inputs)  # Run inference with the model\n\n        # lung_masks now contains the predicted segmentation masks\n\n        # You can further process or visualize the lung_masks if needed\n\n    # Iterate through the validation loader\n    for data in valid_loader:\n        inputs, _ = data  # No need for targets, as you're performing inference\n\n        # Perform lung segmentation using the trained U-Net model\n        with torch.no_grad():\n            inputs = inputs.to(device)  # Move inputs to the correct device\n            lung_masks = model(inputs)  # Run inference with the model\n\n        # lung_masks now contains the predicted segmentation masks\n\n        # You can further process or visualize the lung_masks if needed\n\n    return train_loader, valid_loader\n","metadata":{"papermill":{"duration":12.767834,"end_time":"2023-08-26T18:08:15.554024","exception":false,"start_time":"2023-08-26T18:08:02.78619","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T09:09:24.798989Z","iopub.execute_input":"2023-08-27T09:09:24.799405Z","iopub.status.idle":"2023-08-27T09:09:24.814235Z","shell.execute_reply.started":"2023-08-27T09:09:24.79937Z","shell.execute_reply":"2023-08-27T09:09:24.81322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2 With lungs\n\n\n\nimport torch\nimport pydicom\nfrom monai.networks.nets import UNet\nfrom monai.networks.layers import Norm\n\ndef loaders(epoch, batch_size=batch_size):\n    # Define the training and validation sets\n    tr_idc = [0, 1, 2, 3, 4, 5]\n    vd_idc = [6, 7]\n    \n    # Determine the current training and validation set indices\n    train_split = tr_idc[epoch % len(tr_idc)]\n    valid_split = vd_idc[epoch % len(vd_idc)]\n    \n    # Create the training dataset and loader\n    dataset_train = PulmoDataset(csv_file=train_csv, stage='train', transform=transform, split=train_split)\n    train_sampler = get_splt(dataset_train, stage='train')\n    train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=batch_size, sampler=train_sampler, num_workers=2)\n    \n    # Create the validation dataset and loader\n    dataset_valid = PulmoDataset(csv_file=train_csv, stage='valid', transform=transform, split=valid_split)\n    valid_sampler = get_splt(dataset_valid, stage='valid')\n    valid_loader = torch.utils.data.DataLoader(dataset_valid, batch_size=batch_size, sampler=valid_sampler, num_workers=2)\n\n    # Load your trained U-Net model\n    model = UNet(\n        spatial_dims=3,\n        in_channels=1,\n        out_channels=2,\n        channels=(16, 32, 64, 128, 256, 512),\n        strides=(2, 2, 2, 2, 2),\n        num_res_units=2,\n        norm=Norm.BATCH,\n    ).to(device)\n    model.load_state_dict(torch.load('/kaggle/input/best-metric-model-08-06-12h-lung/best_metric_model_08_06_11h.pth'))\n    model.eval()\n\n    # Iterate through the training loader\n    for data in train_loader:\n        inputs, _ = data  # No need for targets, as you're performing inference\n\n        # Perform lung segmentation using the trained U-Net model\n        with torch.no_grad():\n            inputs = inputs.to(device)  # Move inputs to the correct device\n            lung_masks = model(inputs)  # Run inference with the model\n\n        # lung_masks now contains the predicted segmentation masks\n\n        # You can further process or visualize the lung_masks if needed\n\n    # Iterate through the validation loader\n    for data in valid_loader:\n        inputs, _ = data  # No need for targets, as you're performing inference\n\n        # Perform lung segmentation using the trained U-Net model\n        with torch.no_grad():\n            inputs = inputs.to(device)  # Move inputs to the correct device\n            lung_masks = model(inputs)  # Run inference with the model\n\n        # lung_masks now contains the predicted segmentation masks\n\n        # You can further process or visualize the lung_masks if needed\n\n    return train_loader, valid_loader\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T18:08:15.696092Z","iopub.status.busy":"2023-08-26T18:08:15.695025Z","iopub.status.idle":"2023-08-26T18:08:15.709073Z","shell.execute_reply":"2023-08-26T18:08:15.708034Z"},"papermill":{"duration":0.085374,"end_time":"2023-08-26T18:08:15.711228","exception":false,"start_time":"2023-08-26T18:08:15.625854","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '/kaggle/input/rsna-str-pulmonary-embolism-detection/'\ntrain_csv = data_dir + 'train.csv'\ntrain_dir = data_dir + 'train/'\ntrain_df = pd.read_csv(train_csv)","metadata":{"papermill":{"duration":4.017893,"end_time":"2023-08-26T18:08:19.795753","exception":false,"start_time":"2023-08-26T18:08:15.77786","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:49:47.331686Z","iopub.execute_input":"2023-08-28T09:49:47.3321Z","iopub.status.idle":"2023-08-28T09:49:51.421134Z","shell.execute_reply.started":"2023-08-28T09:49:47.332065Z","shell.execute_reply":"2023-08-28T09:49:51.419972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntransform = T.Normalize(mean=[0.0], std=[500.0])\npulmo_dataset = PulmoDataset(csv_file=train_csv, stage='valid', transform=transform, split=0)\n\n","metadata":{"papermill":{"duration":2.840018,"end_time":"2023-08-26T18:08:22.701706","exception":false,"start_time":"2023-08-26T18:08:19.861688","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:49:57.066812Z","iopub.execute_input":"2023-08-28T09:49:57.067212Z","iopub.status.idle":"2023-08-28T09:49:59.803112Z","shell.execute_reply.started":"2023-08-28T09:49:57.067181Z","shell.execute_reply":"2023-08-28T09:49:59.80211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pour visualiser la grad_cam\n#pulmo_dataset = PulmoDataset(csv_file=train_csv[train_csv['pe_present_on_image'] == 1], stage='valid', transform=transform, split=0)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T15:39:03.473177Z","iopub.execute_input":"2023-08-27T15:39:03.474266Z","iopub.status.idle":"2023-08-27T15:39:04.455149Z","shell.execute_reply.started":"2023-08-27T15:39:03.47423Z","shell.execute_reply":"2023-08-27T15:39:04.453626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image, target, _ = pulmo_dataset[103]\nimage = image.squeeze(0)\nfig, ax = plt.subplots(1,2,figsize=(10,5))\nax[0].imshow(image)\nsns.distplot(image.flatten(), ax=ax[1]);\n","metadata":{"papermill":{"duration":1.064324,"end_time":"2023-08-26T18:08:23.831259","exception":false,"start_time":"2023-08-26T18:08:22.766935","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T15:40:45.526477Z","iopub.execute_input":"2023-08-27T15:40:45.527015Z","iopub.status.idle":"2023-08-27T15:40:46.506129Z","shell.execute_reply.started":"2023-08-27T15:40:45.526982Z","shell.execute_reply":"2023-08-27T15:40:46.505143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image, target, _ = pulmo_dataset[107]\nimage = image.squeeze(0)\nfig, ax = plt.subplots(1,2,figsize=(10,5))\nax[0].imshow(image)\nsns.distplot(image.flatten(), ax=ax[1]);\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T15:41:08.788821Z","iopub.execute_input":"2023-08-27T15:41:08.789222Z","iopub.status.idle":"2023-08-27T15:41:09.747198Z","shell.execute_reply.started":"2023-08-27T15:41:08.789189Z","shell.execute_reply":"2023-08-27T15:41:09.746236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pulmo_dataset.pos_df)","metadata":{"papermill":{"duration":0.078722,"end_time":"2023-08-26T18:08:24.119823","exception":false,"start_time":"2023-08-26T18:08:24.041101","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:50:12.358615Z","iopub.execute_input":"2023-08-28T09:50:12.359001Z","iopub.status.idle":"2023-08-28T09:50:12.365902Z","shell.execute_reply.started":"2023-08-28T09:50:12.358968Z","shell.execute_reply":"2023-08-28T09:50:12.364913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/c1a5538bba20.dcm","metadata":{"execution":{"iopub.execute_input":"2023-08-26T18:08:24.256603Z","iopub.status.busy":"2023-08-26T18:08:24.256257Z","iopub.status.idle":"2023-08-26T18:08:24.261715Z","shell.execute_reply":"2023-08-26T18:08:24.260861Z"},"papermill":{"duration":0.076306,"end_time":"2023-08-26T18:08:24.264023","exception":false,"start_time":"2023-08-26T18:08:24.187717","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pulmo_dataset.pos_df","metadata":{"papermill":{"duration":0.097804,"end_time":"2023-08-26T18:08:24.433677","exception":false,"start_time":"2023-08-26T18:08:24.335873","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T02:59:17.075228Z","iopub.execute_input":"2023-08-28T02:59:17.075603Z","iopub.status.idle":"2023-08-28T02:59:17.101733Z","shell.execute_reply.started":"2023-08-28T02:59:17.075567Z","shell.execute_reply":"2023-08-28T02:59:17.100704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-08-27T15:34:08.149318Z","iopub.execute_input":"2023-08-27T15:34:08.149959Z","iopub.status.idle":"2023-08-27T15:34:08.213638Z","shell.execute_reply.started":"2023-08-27T15:34:08.149922Z","shell.execute_reply":"2023-08-27T15:34:08.212363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pulmo_dataset","metadata":{"execution":{"iopub.status.busy":"2023-08-28T09:50:24.014816Z","iopub.execute_input":"2023-08-28T09:50:24.015524Z","iopub.status.idle":"2023-08-28T09:50:24.022104Z","shell.execute_reply.started":"2023-08-28T09:50:24.01549Z","shell.execute_reply":"2023-08-28T09:50:24.020988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Lung Seg \npulmo_dataset.pos_df\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:38:56.442066Z","iopub.execute_input":"2023-08-27T13:38:56.442482Z","iopub.status.idle":"2023-08-27T13:38:56.46284Z","shell.execute_reply.started":"2023-08-27T13:38:56.44245Z","shell.execute_reply":"2023-08-27T13:38:56.461789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pulmo_dataset.neg_df)","metadata":{"papermill":{"duration":0.079335,"end_time":"2023-08-26T18:08:24.582628","exception":false,"start_time":"2023-08-26T18:08:24.503293","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T02:59:26.00243Z","iopub.execute_input":"2023-08-28T02:59:26.002828Z","iopub.status.idle":"2023-08-28T02:59:26.009443Z","shell.execute_reply.started":"2023-08-28T02:59:26.002795Z","shell.execute_reply":"2023-08-28T02:59:26.008279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pulmo_dataset.pos_df)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T15:44:12.666496Z","iopub.execute_input":"2023-08-27T15:44:12.666939Z","iopub.status.idle":"2023-08-27T15:44:12.675089Z","shell.execute_reply.started":"2023-08-27T15:44:12.666905Z","shell.execute_reply":"2023-08-27T15:44:12.674141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_splt(pulmo_dataset, stage='train')\nget_splt(pulmo_dataset, stage='valid')","metadata":{"papermill":{"duration":0.087073,"end_time":"2023-08-26T18:08:24.740373","exception":false,"start_time":"2023-08-26T18:08:24.6533","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:50:35.279953Z","iopub.execute_input":"2023-08-28T09:50:35.280413Z","iopub.status.idle":"2023-08-28T09:50:35.295827Z","shell.execute_reply.started":"2023-08-28T09:50:35.280381Z","shell.execute_reply":"2023-08-28T09:50:35.294601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#With lung\nget_splt(pulmo_dataset, stage='train')\nget_splt(pulmo_dataset, stage='valid')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:05:42.348434Z","iopub.execute_input":"2023-08-27T10:05:42.34888Z","iopub.status.idle":"2023-08-27T10:05:42.370388Z","shell.execute_reply.started":"2023-08-27T10:05:42.348849Z","shell.execute_reply":"2023-08-27T10:05:42.369327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader, valid_loader = loaders(660)","metadata":{"papermill":{"duration":15.866027,"end_time":"2023-08-26T18:08:40.674499","exception":true,"start_time":"2023-08-26T18:08:24.808472","status":"failed"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:50:46.87028Z","iopub.execute_input":"2023-08-28T09:50:46.870649Z","iopub.status.idle":"2023-08-28T09:50:52.247303Z","shell.execute_reply.started":"2023-08-28T09:50:46.870617Z","shell.execute_reply":"2023-08-28T09:50:52.24632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Lung \ntrain_loader, valid_loader = loaders(23)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:05:47.467259Z","iopub.execute_input":"2023-08-27T10:05:47.467687Z","iopub.status.idle":"2023-08-27T10:05:54.377975Z","shell.execute_reply.started":"2023-08-27T10:05:47.467654Z","shell.execute_reply":"2023-08-27T10:05:54.376909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"# Lung\n\nimport torch\nimport pydicom\nfrom monai.networks.nets import UNet\nfrom monai.networks.layers import Norm\n\ndef loaders_lung(epoch, batch_size=batch_size):\n    # Define the training and validation sets\n    tr_idc = [0, 1, 2, 3, 4, 5]\n    vd_idc = [6, 7]\n    \n    # Determine the current training and validation set indices\n    train_split = tr_idc[epoch % len(tr_idc)]\n    valid_split = vd_idc[epoch % len(vd_idc)]\n    \n    # Create the training dataset and loader\n    dataset_train = PulmoDataset(csv_file=train_csv, stage='train', transform=transform, split=train_split)\n    train_sampler = get_splt(dataset_train, stage='train')\n    train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=batch_size, sampler=train_sampler, num_workers=2)\n    \n    # Create the validation dataset and loader\n    dataset_valid = PulmoDataset(csv_file=train_csv, stage='valid', transform=transform, split=valid_split)\n    valid_sampler = get_splt(dataset_valid, stage='valid')\n    valid_loader = torch.utils.data.DataLoader(dataset_valid, batch_size=batch_size, sampler=valid_sampler, num_workers=2)\n\n    # Load your trained U-Net model\n    # Load your trained U-Net model\n    # Load your trained U-Net model\n    model = UNet(\n        spatial_dims=3,\n        in_channels=1,\n        out_channels=2,\n        channels=(16, 32, 64, 128, 256, 512),\n        strides=(2, 2, 2, 2, 2),\n        num_res_units=2,\n        norm=Norm.BATCH,\n    ).to(device)\n    model.load_state_dict(torch.load('/kaggle/input/best-metric-model-08-06-12h-lung/best_metric_model_08_06_11h.pth'))\n    model.eval()\n\n    # Iterate through the training loader\n    for data in train_loader:\n        dicom_file_paths = data  # Unpack the data\n\n        dicom_images = dicom_file_paths\n\n        # Load and preprocess the DICOM images\n        # dicom_images = [pydicom.dcmread(path).pixel_array for path in dicom_file_paths]\n        # dicom_images = torch.tensor(dicom_images, dtype=torch.float32).unsqueeze(1)  # Add channel dimension\n\n        with torch.no_grad():\n            dicom_images = dicom_images.to(device)  # Move to the correct device\n            lung_masks = model(dicom_images)  # Run inference with the model\n\n        # lung_masks now contains the predicted segmentation masks\n\n        # You can further process or visualize the lung_masks if needed\n\n    # Iterate through the validation loader\n    for data in valid_loader:\n        dicom_file_paths = data  # Unpack the data\n\n        dicom_images = dicom_file_paths\n\n        # Load and preprocess the DICOM images\n        # dicom_images = [pydicom.dcmread(path).pixel_array for path in dicom_file_paths]\n        # dicom_images = torch.tensor(dicom_images, dtype=torch.float32).unsqueeze(1)  # Add channel dimension\n\n        with torch.no_grad():\n            dicom_images = dicom_images.to(device)  # Move to the correct device\n            lung_masks = model(dicom_images)  # Run inference with the model\n\n        # lung_masks now contains the predicted segmentation masks\n\n        # You can further process or visualize the lung_masks if needed\n\n    return train_loader, valid_loader\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T12:26:26.067591Z","iopub.status.busy":"2023-08-26T12:26:26.067181Z","iopub.status.idle":"2023-08-26T12:26:26.080844Z","shell.execute_reply":"2023-08-26T12:26:26.079848Z","shell.execute_reply.started":"2023-08-26T12:26:26.06756Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_iter = iter(train_loader)\n#data_iter = iter(valid_loader)\ndata, target, indices = next(data_iter)\nprint(target)\nprint(indices)\nprint(data.shape)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:51:22.952504Z","iopub.execute_input":"2023-08-28T09:51:22.953695Z","iopub.status.idle":"2023-08-28T09:51:24.394865Z","shell.execute_reply.started":"2023-08-28T09:51:22.953652Z","shell.execute_reply":"2023-08-28T09:51:24.393312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Lung segmentation\n\n\"\"\"data_iter = iter(train_loader)\n#data_iter = iter(valid_loader)\ndata, target, indices = next(data_iter)\nprint(target)\nprint(indices)\nprint(data.shape)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:40:32.849675Z","iopub.execute_input":"2023-08-27T13:40:32.850126Z","iopub.status.idle":"2023-08-27T13:40:32.859877Z","shell.execute_reply.started":"2023-08-27T13:40:32.850083Z","shell.execute_reply":"2023-08-27T13:40:32.858526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ntrain_df.head(1170)\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T16:37:10.277025Z","iopub.execute_input":"2023-08-27T16:37:10.277487Z","iopub.status.idle":"2023-08-27T16:37:10.301579Z","shell.execute_reply.started":"2023-08-27T16:37:10.277452Z","shell.execute_reply":"2023-08-27T16:37:10.300427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nindices = indices.tolist()\nfor index in indices:\n    print(train_df.pe_present_on_image[index])\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:51:39.233439Z","iopub.execute_input":"2023-08-28T09:51:39.233933Z","iopub.status.idle":"2023-08-28T09:51:39.242205Z","shell.execute_reply.started":"2023-08-28T09:51:39.23389Z","shell.execute_reply":"2023-08-28T09:51:39.241063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pydicom\nimport numpy as np\n\ndef get_dcm(dataframe, index):\n    img_name = os.path.join(train_dir,\n                            dataframe.loc[index, 'StudyInstanceUID'],\n                            dataframe.loc[index, 'SeriesInstanceUID'],\n                            dataframe.loc[index, 'SOPInstanceUID'] + '.dcm')\n    dicom_image = pydicom.dcmread(img_name) \n\n    try:\n        image = dicom_image.pixel_array\n    except Exception as e:\n        print('Error parsing', img_name)\n        print(e)\n        return None\n\n    image[image <= -1000] = 0\n\n    intercept = dicom_image.RescaleIntercept\n    slope = dicom_image.RescaleSlope\n\n    if slope != 1:\n        image = slope * image.astype(np.float64)\n\n    image = image.astype(np.int16)\n    image += np.int16(intercept)\n    return image","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:51:48.990076Z","iopub.execute_input":"2023-08-28T09:51:48.990453Z","iopub.status.idle":"2023-08-28T09:51:48.999268Z","shell.execute_reply.started":"2023-08-28T09:51:48.990426Z","shell.execute_reply":"2023-08-28T09:51:48.998152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  With lung \n\nimport os\nimport pydicom\nimport numpy as np\n\ndef get_dcm(dataframe, index):\n    img_name = os.path.join(train_dir,\n                            dataframe.loc[index, 'StudyInstanceUID'],\n                            dataframe.loc[index, 'SeriesInstanceUID'],\n                            dataframe.loc[index, 'SOPInstanceUID'] + '.dcm')\n    dicom_image = pydicom.dcmread(img_name) \n\n    try:\n        image = dicom_image.pixel_array\n    except Exception as e:\n        print('Error parsing', img_name)\n        print(e)\n        return None\n\n    image[image <= -1000] = 0\n\n    intercept = dicom_image.RescaleIntercept\n    slope = dicom_image.RescaleSlope\n\n    if slope != 1:\n        image = slope * image.astype(np.float64)\n\n    image = image.astype(np.int16)\n    image += np.int16(intercept)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:11:41.554147Z","iopub.execute_input":"2023-08-27T10:11:41.554736Z","iopub.status.idle":"2023-08-27T10:11:41.574055Z","shell.execute_reply.started":"2023-08-27T10:11:41.554695Z","shell.execute_reply":"2023-08-27T10:11:41.573138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:51:59.130693Z","iopub.execute_input":"2023-08-28T09:51:59.131081Z","iopub.status.idle":"2023-08-28T09:51:59.137695Z","shell.execute_reply.started":"2023-08-28T09:51:59.131049Z","shell.execute_reply":"2023-08-28T09:51:59.1367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With lung \ndata.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:11:59.35581Z","iopub.execute_input":"2023-08-27T10:11:59.356425Z","iopub.status.idle":"2023-08-27T10:11:59.373125Z","shell.execute_reply.started":"2023-08-27T10:11:59.356382Z","shell.execute_reply":"2023-08-27T10:11:59.372174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = get_dcm(train_df, 700)\n\nimage.shape\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:52:23.042973Z","iopub.execute_input":"2023-08-28T09:52:23.043427Z","iopub.status.idle":"2023-08-28T09:52:23.102497Z","shell.execute_reply.started":"2023-08-28T09:52:23.043389Z","shell.execute_reply":"2023-08-28T09:52:23.10157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With lung \nimage = get_dcm(train_df, 700)\n\nimage.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T10:12:14.341117Z","iopub.execute_input":"2023-08-27T10:12:14.341905Z","iopub.status.idle":"2023-08-27T10:12:14.462041Z","shell.execute_reply.started":"2023-08-27T10:12:14.341851Z","shell.execute_reply":"2023-08-27T10:12:14.460433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(indices)):\n    index = indices[i]\n    image = get_dcm(train_df, index)  \n    data_i = data[i, 0, :, :]\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(image)\n    ax[1].imshow(data_i)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T03:01:53.746517Z","iopub.execute_input":"2023-08-28T03:01:53.746924Z","iopub.status.idle":"2023-08-28T03:02:02.973384Z","shell.execute_reply.started":"2023-08-28T03:01:53.746892Z","shell.execute_reply":"2023-08-28T03:02:02.972445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With lung\n\nfor i in range(len(indices)):\n    index = indices[i]\n    image = get_dcm(train_df, index)  \n    data_i = data[i, 0, :, :]\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(image)\n    ax[1].imshow(data_i)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T13:46:54.502716Z","iopub.execute_input":"2023-08-27T13:46:54.503196Z","iopub.status.idle":"2023-08-27T13:47:07.687571Z","shell.execute_reply.started":"2023-08-27T13:46:54.503158Z","shell.execute_reply":"2023-08-27T13:47:07.686532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef criterion(logits, target):\n    return torch.nn.BCEWithLogitsLoss()(logits.view(-1), target.view(-1)) # loss","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:52:41.407268Z","iopub.execute_input":"2023-08-28T09:52:41.407641Z","iopub.status.idle":"2023-08-28T09:52:41.41323Z","shell.execute_reply.started":"2023-08-28T09:52:41.407611Z","shell.execute_reply":"2023-08-28T09:52:41.412176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install efficientnet_pytorch","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:52:46.486857Z","iopub.execute_input":"2023-08-28T09:52:46.487248Z","iopub.status.idle":"2023-08-28T09:53:00.479675Z","shell.execute_reply.started":"2023-08-28T09:52:46.487219Z","shell.execute_reply":"2023-08-28T09:53:00.478391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nbce = torch.nn.BCEWithLogitsLoss()\ndef criterion(logits, target):\n    loss = bce(logits.view(-1), target.view(-1))\n    return loss\n\ndef train_epoch(model, loader, optimizer):\n\n    model.train()\n    train_loss = []\n    bar = tqdm(loader)\n    for (data, target, _) in bar:\n        data, target = data.to(device), target.to(device)\n        \n        # zero gradients\n        optimizer.zero_grad()\n\n        #forward pass\n        logits = model(data.float())\n        \n        # calculate loss\n        loss = criterion(logits, target)\n\n        # backpropagate the loss (backward pass)\n        loss.backward()\n \n        # update parameters based on accumulated gradients\n        optimizer.step()\n\n        loss_np = loss.detach().cpu().numpy()\n\n        train_loss.append(loss_np)\n        average_loss = sum(train_loss) / len(train_loss)\n        bar.set_description('loss: %.5f, mean: %.5f' % (loss_np, average_loss))\n    return float(average_loss)\n\ndef valid_epoch(model, loader):\n    model.eval()\n    val_loss = []\n    pos_logits = []\n    neg_logits = []\n\n    with torch.no_grad():\n        for (data, target, _) in tqdm(loader):\n            data, target = data.to(device), target.to(device)\n            logits = model(data)\n\n            loss = criterion(logits, target)\n\n            loss_np = loss.detach().cpu().numpy()\n\n            val_loss.append(loss_np)\n            \n            for i in range(logits.shape[-1]):\n                b = target[i].detach().cpu().numpy()\n                if b == 1:\n                    pos_logits.append(logits[i].detach().cpu().numpy())\n                else:\n                    neg_logits.append(logits[i].detach().cpu().numpy())\n                    \n    val_loss = sum(val_loss) / len(val_loss)\n    \n    neg_logits_tensor = torch.FloatTensor(neg_logits).cuda()\n    pos_logits_tensor = torch.FloatTensor(pos_logits).cuda()\n    \n    neg_loss = criterion(neg_logits_tensor, torch.zeros(neg_logits_tensor.shape).float().cuda())\n    pos_loss = criterion(pos_logits_tensor, torch.ones(pos_logits_tensor.shape).float().cuda())\n    \n    neg_loss = neg_loss.detach().cpu().numpy()\n    pos_loss = pos_loss.detach().cpu().numpy()\n    \n    neg_mean = float(torch.sigmoid(neg_logits_tensor).mean().detach().cpu().numpy())\n    pos_mean = float(torch.sigmoid(pos_logits_tensor).mean().detach().cpu().numpy())\n    \n    return float(val_loss), float(pos_loss), float(neg_loss), pos_mean, neg_mean\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-25T13:47:08.927685Z","iopub.status.busy":"2023-08-25T13:47:08.92697Z","iopub.status.idle":"2023-08-25T13:47:08.943751Z","shell.execute_reply":"2023-08-25T13:47:08.942549Z","shell.execute_reply.started":"2023-08-25T13:47:08.927643Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# V2\n\n\nbce = torch.nn.BCEWithLogitsLoss()\n\ndef criterion(logits, target):\n    loss = bce(logits.view(-1), target.view(-1))\n    return loss\n\ndef calculate_accuracy(logits, targets, threshold=0.5):\n    probabilities = torch.sigmoid(logits)\n    predictions = (probabilities > threshold).float()\n    correct_predictions = (predictions == targets).float()\n    accuracy = correct_predictions.mean()\n    return accuracy\n\ndef train_epoch(model, loader, optimizer):\n    model.train()\n    train_loss = []\n    train_accuracy = []\n    \n    bar = tqdm(loader)\n    for (data, target, _) in bar:\n        data, target = data.to(device), target.to(device)\n        \n        # zero gradients\n        optimizer.zero_grad()\n\n        # forward pass\n        logits = model(data.float())\n        \n        # calculate loss\n        loss = criterion(logits, target)\n        \n        # calculate accuracy\n        accuracy = calculate_accuracy(logits, target)\n\n        # backpropagate the loss (backward pass)\n        loss.backward()\n \n        # update parameters based on accumulated gradients\n        optimizer.step()\n\n        loss_np = loss.detach().cpu().numpy()\n        accuracy_np = accuracy.detach().cpu().numpy()\n\n        train_loss.append(loss_np)\n        train_accuracy.append(accuracy_np)\n        \n        average_loss = sum(train_loss) / len(train_loss)\n        average_accuracy = sum(train_accuracy) / len(train_accuracy)\n        \n        bar.set_description('loss: %.5f, acc: %.5f' % (loss_np, average_accuracy))\n        \n    return float(average_loss), float(average_accuracy)\n\ndef valid_epoch(model, loader):\n    model.eval()\n    val_loss = []\n    pos_logits = []\n    neg_logits = []\n    val_accuracy = []\n\n    with torch.no_grad():\n        for (data, target, _) in tqdm(loader):\n            data, target = data.to(device), target.to(device)\n            logits = model(data)\n\n            loss = criterion(logits, target)\n            \n            accuracy = calculate_accuracy(logits, target)\n\n            loss_np = loss.detach().cpu().numpy()\n            accuracy_np = accuracy.detach().cpu().numpy()\n\n            val_loss.append(loss_np)\n            val_accuracy.append(accuracy_np)\n            \n            for i in range(logits.shape[-1]):\n                b = target[i].detach().cpu().numpy()\n                if b == 1:\n                    pos_logits.append(logits[i].detach().cpu().numpy())\n                else:\n                    neg_logits.append(logits[i].detach().cpu().numpy())\n                    \n    val_loss = sum(val_loss) / len(val_loss)\n    val_accuracy = sum(val_accuracy) / len(val_accuracy)\n    \n    neg_logits_tensor = torch.FloatTensor(neg_logits).cuda()\n    pos_logits_tensor = torch.FloatTensor(pos_logits).cuda()\n    \n    neg_loss = criterion(neg_logits_tensor, torch.zeros(neg_logits_tensor.shape).float().cuda())\n    pos_loss = criterion(pos_logits_tensor, torch.ones(pos_logits_tensor.shape).float().cuda())\n    \n    neg_loss = neg_loss.detach().cpu().numpy()\n    pos_loss = pos_loss.detach().cpu().numpy()\n    \n    neg_mean = float(torch.sigmoid(neg_logits_tensor).mean().detach().cpu().numpy())\n    pos_mean = float(torch.sigmoid(pos_logits_tensor).mean().detach().cpu().numpy())\n    \n    return float(val_loss), float(pos_loss), float(neg_loss), val_accuracy, pos_mean, neg_mean\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T04:27:04.786047Z","iopub.execute_input":"2023-08-28T04:27:04.786442Z","iopub.status.idle":"2023-08-28T04:27:04.807411Z","shell.execute_reply.started":"2023-08-28T04:27:04.786411Z","shell.execute_reply":"2023-08-28T04:27:04.806355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# V3\n\ndef valid_epoch(model, loader):\n    model.eval()\n    val_loss = []\n    pos_logits = []\n    neg_logits = []\n    val_accuracy = []\n\n    with torch.no_grad():\n        for (data, target, _) in tqdm(loader):\n            data, target = data.to(device), target.to(device)\n            logits = model(data)\n\n            loss = criterion(logits, target)\n            \n            accuracy = calculate_accuracy(logits, target)\n\n            loss_np = loss.detach().cpu().numpy()\n            accuracy_np = accuracy.detach().cpu().numpy()\n\n            val_loss.append(loss_np)\n            val_accuracy.append(accuracy_np)\n            \n            for i in range(logits.shape[0]):\n                if target[i].item() == 1:  # Check target value for each sample\n                    pos_logits.append(logits[i].detach().cpu().numpy())\n                else:\n                    neg_logits.append(logits[i].detach().cpu().numpy())\n                    \n    val_loss = sum(val_loss) / len(val_loss)\n    val_accuracy = sum(val_accuracy) / len(val_accuracy) if len(val_accuracy) > 0 else 0.0\n    \n    if len(neg_logits) > 0:\n        neg_logits_tensor = torch.FloatTensor(neg_logits).cuda()\n        neg_loss = criterion(neg_logits_tensor, torch.zeros(neg_logits_tensor.shape).float().cuda())\n        neg_loss = neg_loss.detach().cpu().numpy()\n        neg_mean = float(torch.sigmoid(neg_logits_tensor).mean().detach().cpu().numpy())\n    else:\n        neg_loss = 0.0\n        neg_mean = 0.0\n    \n    if len(pos_logits) > 0:\n        pos_logits_tensor = torch.FloatTensor(pos_logits).cuda()\n        pos_loss = criterion(pos_logits_tensor, torch.ones(pos_logits_tensor.shape).float().cuda())\n        pos_loss = pos_loss.detach().cpu().numpy()\n        pos_mean = float(torch.sigmoid(pos_logits_tensor).mean().detach().cpu().numpy())\n    else:\n        pos_loss = 0.0\n        pos_mean = 0.0\n    \n    return float(val_loss), float(pos_loss), float(neg_loss), val_accuracy, pos_mean, neg_mean\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:46:22.428728Z","iopub.execute_input":"2023-08-28T10:46:22.429137Z","iopub.status.idle":"2023-08-28T10:46:22.444835Z","shell.execute_reply.started":"2023-08-28T10:46:22.429104Z","shell.execute_reply":"2023-08-28T10:46:22.443622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet_pytorch import EfficientNet\n\ncnn = EfficientNet.from_pretrained('efficientnet-b0',num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:46:28.443449Z","iopub.execute_input":"2023-08-28T10:46:28.443828Z","iopub.status.idle":"2023-08-28T10:46:28.56843Z","shell.execute_reply.started":"2023-08-28T10:46:28.443798Z","shell.execute_reply":"2023-08-28T10:46:28.567445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T10:46:36.796997Z","iopub.execute_input":"2023-08-28T10:46:36.797366Z","iopub.status.idle":"2023-08-28T10:46:36.803573Z","shell.execute_reply.started":"2023-08-28T10:46:36.797337Z","shell.execute_reply":"2023-08-28T10:46:36.802621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:46:40.182469Z","iopub.execute_input":"2023-08-28T10:46:40.184266Z","iopub.status.idle":"2023-08-28T10:46:40.195441Z","shell.execute_reply.started":"2023-08-28T10:46:40.184219Z","shell.execute_reply":"2023-08-28T10:46:40.194338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_optimizer(lr, model):\n    return torch.optim.SGD(model.parameters(), lr=lr, momentum=0.9, weight_decay=0.0001)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:53:45.780306Z","iopub.execute_input":"2023-08-28T09:53:45.780692Z","iopub.status.idle":"2023-08-28T09:53:45.785869Z","shell.execute_reply.started":"2023-08-28T09:53:45.78066Z","shell.execute_reply":"2023-08-28T09:53:45.784916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nn_epochs = 10\n\ndevice = 'cuda'\n\n\ngamma = 10\nschedule = [3, 6, 8]\n\n\nmodel = cnn.to(device)\noptimizer = get_optimizer(0.1, model) # 0.1 learning rate de depart\n\nmaster_train_loss = []\nmaster_valid_loss = []\nepoch = 1\n\n\ntransform = T.Normalize(mean=[0], std=[500])\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T10:47:30.82235Z","iopub.execute_input":"2023-08-28T10:47:30.822727Z","iopub.status.idle":"2023-08-28T10:47:30.838526Z","shell.execute_reply.started":"2023-08-28T10:47:30.822695Z","shell.execute_reply":"2023-08-28T10:47:30.83738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nprint(optimizer.param_groups[0][\"lr\"])\nprint(optimizer)\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T10:47:36.000011Z","iopub.execute_input":"2023-08-28T10:47:36.000384Z","iopub.status.idle":"2023-08-28T10:47:36.00612Z","shell.execute_reply.started":"2023-08-28T10:47:36.000354Z","shell.execute_reply":"2023-08-28T10:47:36.005198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import os\nimport time\nimport pickle\nimport numpy as np\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\n# Assuming you have defined n_epochs, schedule, get_optimizer, get_loaders, train_epoch, valid_epoch, and master_train_loss, master_valid_loss\n\n# Initialize variables\nbest_valid_loss = float('inf')\nepoch = 1\n\nwhile epoch <= n_epochs:\n    print(time.ctime(), 'Epoch:', epoch)\n    \n    # Update learning rate \n    if epoch in schedule:\n        new_lr = optimizer.param_groups[0][\"lr\"] / gamma\n        optimizer = get_optimizer(new_lr, model)\n    \n    train_loader, valid_loader = loaders(epoch)\n\n    # Train\n    train_loss = train_epoch(model, train_loader, optimizer)\n    optimizer.zero_grad()\n    \n    # Validate\n    valid_loss, pos_loss, neg_loss, pos_mean, neg_mean = valid_epoch(model, valid_loader)\n    \n    content = time.ctime() + ' ' + f'Epoch {epoch}, lr: {optimizer.param_groups[0][\"lr\"]:.7f}, train loss: {np.mean(train_loss):.5f}, valid loss: {(valid_loss):.5f}'\n    print(content)\n    content = f'pos loss: {(pos_loss):.5f}, neg loss: {(neg_loss):.5f}, pos mean: {(pos_mean):.5f}, neg mean {(neg_mean):.5f}'\n    print(content)\n    master_train_loss.append(train_loss)\n    master_valid_loss.append(valid_loss)\n    \n    # Save loss data and model weights\n    with open('train_loss.pkl', 'wb') as f:\n        pickle.dump(master_train_loss, f)\n    with open('valid_loss.pkl', 'wb') as f:\n        pickle.dump(master_valid_loss, f)\n    \n    # Save best model\n    if valid_loss < best_valid_loss:\n        torch.save(model.state_dict(), '/kaggle/working/best_model.pth')\n        best_valid_loss = valid_loss\n        \n    # Plot metrics\n    plt.figure(figsize=(10, 5))\n    plt.plot(master_train_loss, label='Train Loss')\n    plt.plot(master_valid_loss, label='Valid Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.title('Train and Valid Loss')\n    plt.legend()\n    plt.savefig('/kaggle/working/loss_plot.png')\n    plt.close()\n    \n    epoch += 1\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-24T23:12:46.412192Z","iopub.status.busy":"2023-08-24T23:12:46.411817Z","iopub.status.idle":"2023-08-25T04:03:35.873851Z","shell.execute_reply":"2023-08-25T04:03:35.872693Z","shell.execute_reply.started":"2023-08-24T23:12:46.41216Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"markdown","source":"# N","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"import os\nimport time\nimport pickle\nimport numpy as np\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\n\n\n\ntransform = T.Normalize(mean=[0], std=[500])\nmaster_train_loss = []\nmaster_valid_loss = []\n\nn_epochs =  40\nepoch = 1\n\n\n\ndevice = 'cuda'\n\n\ngamma = 10\nschedule = [8, 16, 24, 32]\n\nmodel = cnn.to(device)\noptimizer = get_optimizer(0.1, model)\n\n\n\n\n\n\n\n\n# Initialize variables\nbest_valid_loss = float('inf')\nepoch = 1\n\n# Lists to store accuracies\nmaster_train_accuracy = []\nmaster_valid_accuracy = []\n\nwhile epoch <= n_epochs:\n    print(time.ctime(), 'Epoch:', epoch)\n    \n    # Update learning rate \n    if epoch in schedule:\n        new_lr = optimizer.param_groups[0][\"lr\"] / gamma\n        optimizer = get_optimizer(new_lr, model)\n    \n    train_loader, valid_loader = loaders(epoch)\n\n    # Train\n    train_loss = train_epoch(model, train_loader, optimizer)\n    optimizer.zero_grad()\n    \n    # Validate\n    valid_loss, pos_loss, neg_loss, pos_mean, neg_mean = valid_epoch(model, valid_loader)\n    \n    # Calculate accuracy\n    pos_accuracy = 1 - pos_loss  # As pos_loss is a measure of error for positive samples\n    neg_accuracy = 1 - neg_loss  # As neg_loss is a measure of error for negative samples\n    valid_accuracy = (pos_accuracy + neg_accuracy) / 2\n    \n    # Print and store accuracy\n    content = time.ctime() + f' Epoch {epoch}, lr: {optimizer.param_groups[0][\"lr\"]:.7f}, train loss: {np.mean(train_loss):.5f}, valid loss: {valid_loss:.5f}'\n    print(content)\n    content = f'pos loss: {pos_loss:.5f}, neg loss: {neg_loss:.5f}, pos mean: {pos_mean:.5f}, neg mean {neg_mean:.5f}'\n    print(content)\n    accuracy_content = f'pos accuracy: {pos_accuracy:.5f}, neg accuracy: {neg_accuracy:.5f}, valid accuracy: {valid_accuracy:.5f}'\n    print(accuracy_content)\n    \n    master_train_loss.append(train_loss)\n    master_valid_loss.append(valid_loss)\n    master_train_accuracy.append([pos_accuracy, neg_accuracy])\n    master_valid_accuracy.append(valid_accuracy)\n    \n    # Save loss and accuracy data\n    with open('train_loss.pkl', 'wb') as f:\n        pickle.dump(master_train_loss, f)\n    with open('valid_loss.pkl', 'wb') as f:\n        pickle.dump(master_valid_loss, f)\n    with open('train_accuracy.pkl', 'wb') as f:\n        pickle.dump(master_train_accuracy, f)\n    with open('valid_accuracy.pkl', 'wb') as f:\n        pickle.dump(master_valid_accuracy, f)\n    \n    # Save best model\n    if valid_loss < best_valid_loss:\n        torch.save(model.state_dict(), '/kaggle/working/best_model.pth')\n        best_valid_loss = valid_loss\n        \n    # Plot metrics\n    plt.figure(figsize=(10, 5))\n    plt.subplot(1, 2, 1)\n    plt.plot(master_train_loss, label='Train Loss')\n    plt.plot(master_valid_loss, label='Valid Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.title('Train and Valid Loss')\n    plt.legend()\n    plt.ylim(0, 1)  # Set y-axis limits\n    \n    plt.subplot(1, 2, 2)\n    plt.plot(master_train_accuracy, label='Train Accuracy')\n    plt.plot(master_valid_accuracy, label='Valid Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.title('Train and Valid Accuracy')\n    plt.legend()\n    plt.ylim(0, 1)  # Set y-axis limits\n    \n    plt.tight_layout()\n    plt.savefig('/kaggle/working/metrics_plot.png')\n    plt.close()\n    \n    epoch += 1","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T04:39:59.346776Z","iopub.execute_input":"2023-08-28T04:39:59.34717Z","iopub.status.idle":"2023-08-28T05:05:05.951365Z","shell.execute_reply.started":"2023-08-28T04:39:59.347136Z","shell.execute_reply":"2023-08-28T05:05:05.949902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# With lung\n\n# estimated temps > 200h\n\n\"\"\"# With lung\n\nimport os\nimport time\nimport pickle\nimport numpy as np\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\n\n\n\ntransform = T.Normalize(mean=[0], std=[500])\nmaster_train_loss = []\nmaster_valid_loss = []\n\nn_epochs =  30\nepoch = 1\n\n\n\ndevice = 'cuda'\n\n\ngamma = 10\nschedule = [8, 16, 24]\n\nmodel = cnn.to(device)\noptimizer = get_optimizer(0.1, model)\n\n\n\n\n\n\n\n\n# Initialize variables\nbest_valid_loss = float('inf')\nepoch = 1\n\n# Lists to store accuracies\nmaster_train_accuracy = []\nmaster_valid_accuracy = []\n\nwhile epoch <= n_epochs:\n    print(time.ctime(), 'Epoch:', epoch)\n    \n    # Update learning rate \n    if epoch in schedule:\n        new_lr = optimizer.param_groups[0][\"lr\"] / gamma\n        optimizer = get_optimizer(new_lr, model)\n    \n    train_loader, valid_loader = loaders(epoch)\n\n    # Train\n    train_loss = train_epoch(model, train_loader, optimizer)\n    optimizer.zero_grad()\n    \n    # Validate\n    valid_loss, pos_loss, neg_loss, pos_mean, neg_mean = valid_epoch(model, valid_loader)\n    \n    # Calculate accuracy\n    pos_accuracy = 1 - pos_loss  # As pos_loss is a measure of error for positive samples\n    neg_accuracy = 1 - neg_loss  # As neg_loss is a measure of error for negative samples\n    valid_accuracy = (pos_accuracy + neg_accuracy) / 2\n    \n    # Print and store accuracy\n    content = time.ctime() + f' Epoch {epoch}, lr: {optimizer.param_groups[0][\"lr\"]:.7f}, train loss: {np.mean(train_loss):.5f}, valid loss: {valid_loss:.5f}'\n    print(content)\n    content = f'pos loss: {pos_loss:.5f}, neg loss: {neg_loss:.5f}, pos mean: {pos_mean:.5f}, neg mean {neg_mean:.5f}'\n    print(content)\n    accuracy_content = f'pos accuracy: {pos_accuracy:.5f}, neg accuracy: {neg_accuracy:.5f}, valid accuracy: {valid_accuracy:.5f}'\n    print(accuracy_content)\n    \n    master_train_loss.append(train_loss)\n    master_valid_loss.append(valid_loss)\n    master_train_accuracy.append([pos_accuracy, neg_accuracy])\n    master_valid_accuracy.append(valid_accuracy)\n    \n    # Save loss and accuracy data\n    with open('train_loss.pkl', 'wb') as f:\n        pickle.dump(master_train_loss, f)\n    with open('valid_loss.pkl', 'wb') as f:\n        pickle.dump(master_valid_loss, f)\n    with open('train_accuracy.pkl', 'wb') as f:\n        pickle.dump(master_train_accuracy, f)\n    with open('valid_accuracy.pkl', 'wb') as f:\n        pickle.dump(master_valid_accuracy, f)\n    \n    # Save best model\n    if valid_loss < best_valid_loss:\n        torch.save(model.state_dict(), '/kaggle/working/best_model.pth')\n        best_valid_loss = valid_loss\n        \n    # Plot metrics\n    plt.figure(figsize=(10, 5))\n    plt.subplot(1, 2, 1)\n    plt.plot(master_train_loss, label='Train Loss')\n    plt.plot(master_valid_loss, label='Valid Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.title('Train and Valid Loss')\n    plt.legend()\n    plt.ylim(0, 1)  # Set y-axis limits\n    \n    plt.subplot(1, 2, 2)\n    plt.plot(master_train_accuracy, label='Train Accuracy')\n    plt.plot(master_valid_accuracy, label='Valid Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.title('Train and Valid Accuracy')\n    plt.legend()\n    plt.ylim(0, 1)  # Set y-axis limits\n    \n    plt.tight_layout()\n    plt.savefig('/kaggle/working/metrics_plot.png')\n    plt.close()\n    \n    epoch += 1\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-27T13:55:52.838717Z","iopub.execute_input":"2023-08-27T13:55:52.839137Z","iopub.status.idle":"2023-08-27T13:55:52.852736Z","shell.execute_reply.started":"2023-08-27T13:55:52.839103Z","shell.execute_reply":"2023-08-27T13:55:52.851135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Grad-Cam","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"markdown","source":"# New grad","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"!pip install visualisation","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T09:54:39.841633Z","iopub.execute_input":"2023-08-28T09:54:39.842029Z","iopub.status.idle":"2023-08-28T09:54:41.873713Z","shell.execute_reply.started":"2023-08-28T09:54:39.841998Z","shell.execute_reply":"2023-08-28T09:54:41.872534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Define the path to the saved model\nmodel_path = '/kaggle/input/model-efficientb0/model-efficientb.pth'\n\n# Load the model\nloaded_model = torch.load(model_path, map_location=torch.device('cpu'))\n\n# Print the model architecture\nprint(loaded_model)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T03:16:17.795426Z","iopub.execute_input":"2023-08-28T03:16:17.795844Z","iopub.status.idle":"2023-08-28T03:16:17.802699Z","shell.execute_reply.started":"2023-08-28T03:16:17.795811Z","shell.execute_reply":"2023-08-28T03:16:17.801776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n# Load checkpoint\ncheckpoint = torch.load('/kaggle/input/model-efficientb0/model-efficientb.pth')\n\n# Create mapping between checkpoint keys and custom model keys\nmapping = {\n    'checkpoint_layer_name': 'custom_model_layer_name',\n    # ... repeat for other layers\n}\n\n# Load parameters into custom model\nfor ckpt_key, custom_key in mapping.items():\n    if ckpt_key in checkpoint:\n        if custom_key in custom_model.state_dict():\n            custom_model.state_dict()[custom_key].copy_(checkpoint[ckpt_key])\n        else:\n            print(f\"Warning: {custom_key} not found in custom model.\")\n    else:\n        print(f\"Warning: {ckpt_key} not found in checkpoint.\")\n\n# Now, your custom model is loaded with parameters from the checkpoint\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:09:09.306431Z","iopub.execute_input":"2023-08-28T10:09:09.307127Z","iopub.status.idle":"2023-08-28T10:09:09.365186Z","shell.execute_reply.started":"2023-08-28T10:09:09.307092Z","shell.execute_reply":"2023-08-28T10:09:09.364256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(checkpoint.keys())\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T03:41:43.374457Z","iopub.execute_input":"2023-08-28T03:41:43.374885Z","iopub.status.idle":"2023-08-28T03:41:43.380772Z","shell.execute_reply.started":"2023-08-28T03:41:43.374852Z","shell.execute_reply":"2023-08-28T03:41:43.379589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom collections import OrderedDict\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:09:27.575783Z","iopub.execute_input":"2023-08-28T10:09:27.576637Z","iopub.status.idle":"2023-08-28T10:09:27.581855Z","shell.execute_reply.started":"2023-08-28T10:09:27.576587Z","shell.execute_reply":"2023-08-28T10:09:27.580424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = '/kaggle/input/model-efficientb0/model-efficientb.pth'\ncheckpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:09:32.552242Z","iopub.execute_input":"2023-08-28T10:09:32.552686Z","iopub.status.idle":"2023-08-28T10:09:32.589908Z","shell.execute_reply.started":"2023-08-28T10:09:32.552646Z","shell.execute_reply":"2023-08-28T10:09:32.588882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:09:38.752355Z","iopub.execute_input":"2023-08-28T10:09:38.752717Z","iopub.status.idle":"2023-08-28T10:09:38.861473Z","shell.execute_reply.started":"2023-08-28T10:09:38.752686Z","shell.execute_reply":"2023-08-28T10:09:38.860469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_state_dict = OrderedDict()\nfor key, value in checkpoint.items():\n    name = key.replace(\"module.\", \"\")  # Remove 'module.' prefix\n    new_state_dict[name] = value\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:09:44.25136Z","iopub.execute_input":"2023-08-28T10:09:44.25174Z","iopub.status.idle":"2023-08-28T10:09:44.259402Z","shell.execute_reply.started":"2023-08-28T10:09:44.251711Z","shell.execute_reply":"2023-08-28T10:09:44.258337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.load_state_dict(new_state_dict, strict=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:10:27.588311Z","iopub.execute_input":"2023-08-28T10:10:27.588677Z","iopub.status.idle":"2023-08-28T10:10:27.605266Z","shell.execute_reply.started":"2023-08-28T10:10:27.588647Z","shell.execute_reply":"2023-08-28T10:10:27.604165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom collections import OrderedDict\nfrom efficientnet_pytorch import EfficientNet\n\n# Create an instance of the EfficientNet model\nmodel = EfficientNet.from_name('efficientnet-b0', num_classes=1)  # Replace num_classes with the actual number of classes\n\n# Specify the path to the checkpoint file\ncheckpoint_path = '/kaggle/input/model-efficientb0/model-efficientb.pth'\ncheckpoint = torch.load(checkpoint_path, map_location=torch.device('cpu'))\n\n# Create a new OrderedDict without the 'module.' prefix\nnew_state_dict = OrderedDict()\nfor key, value in checkpoint.items():\n    name = key.replace(\"module.\", \"\")  # Remove 'module.' prefix\n    new_state_dict[name] = value\n\n# Load the modified state dictionary into your model\nmodel.load_state_dict(new_state_dict, strict=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:13:28.154963Z","iopub.execute_input":"2023-08-28T10:13:28.155653Z","iopub.status.idle":"2023-08-28T10:13:28.24156Z","shell.execute_reply.started":"2023-08-28T10:13:28.155617Z","shell.execute_reply":"2023-08-28T10:13:28.240654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom collections import OrderedDict\nfrom efficientnet_pytorch import EfficientNet\n\n# Load the original model with modified _conv_stem\noriginal_cnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\noriginal_cnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n\n# Load the weights of the original model\noriginal_checkpoint_path = '/kaggle/input/model-efficientb0/model-efficientb.pth'\noriginal_checkpoint = torch.load(original_checkpoint_path, map_location=torch.device('cpu'))\n\n# Create a new instance of EfficientNet-B0\nnew_cnn = EfficientNet.from_name('efficientnet-b0', num_classes=1).cuda()\n\n# Create a new state dictionary without 'module.' prefix and modify weights\nnew_state_dict = OrderedDict()\nfor key, value in original_checkpoint.items():\n    name = key.replace(\"module.\", \"\")  # Remove 'module.' prefix\n    new_state_dict[name] = value\n\n# Load the modified state dictionary into the new model\nnew_cnn.load_state_dict(new_state_dict, strict=False)\n\n# Save the modified model to a new checkpoint file\nnew_checkpoint_path = '/kaggle/working/model-efficient_b0.pth'\ntorch.save(new_cnn.state_dict(), new_checkpoint_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:18:30.87388Z","iopub.execute_input":"2023-08-28T10:18:30.874189Z","iopub.status.idle":"2023-08-28T10:18:31.141356Z","shell.execute_reply.started":"2023-08-28T10:18:30.874164Z","shell.execute_reply":"2023-08-28T10:18:31.140211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Create your modified model with the same architecture as the original EfficientNet-B0\ncnn = EfficientNet.from_name('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n\n# Load the state dictionary from the checkpoint\ncheckpoint_path = '/kaggle/working/model-efficient_b0.pth'\ncheckpoint = torch.load(checkpoint_path)\n\n# Transfer compatible weights from the checkpoint's _conv_stem to the modified model's _conv_stem\nmodified_stem_weight = cnn._conv_stem.weight\npretrained_stem_weight = checkpoint['_conv_stem.weight']\nmodified_stem_weight[:, :1, :, :] = pretrained_stem_weight[:, :1, :7, :7]  # Resize and transfer weights\n\n# Load the rest of the checkpoint into the modified model\ncnn.load_state_dict(checkpoint)\n\n# Set the model to evaluation mode\ncnn.eval()\n\n# Sample input tensor (adjust the shape and values as needed)\nsample_input = torch.randn(1, 1, 224, 224).cuda()  # Change the second dimension to 1 for grayscale input\n\n# Forward pass\nwith torch.no_grad():\n    output = cnn(sample_input)\n\nprint(\"Model output:\", output)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:28:16.724192Z","iopub.execute_input":"2023-08-28T10:28:16.724641Z","iopub.status.idle":"2023-08-28T10:28:17.293324Z","shell.execute_reply.started":"2023-08-28T10:28:16.724605Z","shell.execute_reply":"2023-08-28T10:28:17.290816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the modified model\ntorch.save(cnn.state_dict(), '/kaggle/working/PE_efficientnet_B0.pth')\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T03:53:39.805693Z","iopub.execute_input":"2023-08-28T03:53:39.806755Z","iopub.status.idle":"2023-08-28T03:53:39.870741Z","shell.execute_reply.started":"2023-08-28T03:53:39.80669Z","shell.execute_reply":"2023-08-28T03:53:39.869758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Load the pretrained model\npretrained_model = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\n\n# Create the modified model\nmodified_cnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\nmodified_cnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n\n# Transfer weights from pretrained model to modified model\npretrained_stem_weight = pretrained_model._conv_stem.weight\nmodified_stem_weight = modified_cnn._conv_stem.weight\n\n# Copy the pretrained weights to the modified model's stem layer\nmodified_stem_weight[:, :1, :, :] = pretrained_stem_weight[:, :1, :7, :7]  # Resize and copy the weights\n\n# Save the modified model's checkpoint\nmodified_checkpoint_path = '/kaggle/working/model-efficientnet_b0_modified.pth'\ntorch.save(modified_cnn.state_dict(), modified_checkpoint_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:41:32.425711Z","iopub.execute_input":"2023-08-28T10:41:32.426097Z","iopub.status.idle":"2023-08-28T10:41:32.672724Z","shell.execute_reply.started":"2023-08-28T10:41:32.426067Z","shell.execute_reply":"2023-08-28T10:41:32.671155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Load the pretrained model\npretrained_model = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\n\n# Create a new modified model\nmodified_model = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\n\n# Modify the stem layer to have a kernel size of (3, 3) for grayscale input\nmodified_model._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False).cuda()\n\n# Transfer weights from the pretrained stem to the modified stem\npretrained_stem_weight = pretrained_model._conv_stem.weight\nmodified_stem_weight = torch.clone(pretrained_stem_weight)  # Create a new tensor\nmodified_stem_weight[:, :1, :, :] = pretrained_stem_weight[:, :1, :, :]\n\n# Set the modified stem weights to the model\nmodified_model._conv_stem.weight = torch.nn.Parameter(modified_stem_weight)\n\n# Sample input tensor (adjust the shape and values as needed)\nsample_input = torch.randn(1, 1, 224, 224).cuda()  # Change the second dimension to 1 for grayscale input\n\n# Forward pass\nwith torch.no_grad():\n    output = modified_model(sample_input)\n\nprint(\"Model output:\", output)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:45:18.481959Z","iopub.execute_input":"2023-08-28T10:45:18.483173Z","iopub.status.idle":"2023-08-28T10:45:19.197688Z","shell.execute_reply.started":"2023-08-28T10:45:18.483085Z","shell.execute_reply":"2023-08-28T10:45:19.194901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\ncnn = EfficientNet.from_pretrained('efficientnet-b0',num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n# Define the path to your model checkpoint\nmodel_path = '/kaggle/working/PE_efficientnet_B0.pth'\n\n# Load the model checkpoint onto the CPU\ncheckpoint = torch.load(model_path, map_location=torch.device('cpu'))\n\n# Load the model's state_dict from the checkpoint\ncnn.load_state_dict(checkpoint)\n\n# Set the model in evaluation mode for inference\ncnn.eval()\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T03:54:18.765974Z","iopub.execute_input":"2023-08-28T03:54:18.766347Z","iopub.status.idle":"2023-08-28T03:54:18.971811Z","shell.execute_reply.started":"2023-08-28T03:54:18.766316Z","shell.execute_reply":"2023-08-28T03:54:18.970624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\ncnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)  # Match the shape\n\n# Define the path to your model checkpoint\nmodel_path = '/kaggle/working/PE_efficientnet_B0.pth'\n\n# Load the model checkpoint onto the CPU\ncheckpoint = torch.load(model_path, map_location=torch.device('cpu'))\n\n# Adjust the _conv_stem weight shape in the checkpoint to match the modified model\ncheckpoint['_conv_stem.weight'] = checkpoint['_conv_stem.weight'][:, :1, :, :]\n\n# Load the model's state_dict from the checkpoint\ncnn.load_state_dict(checkpoint)\n\n# Set the model in evaluation mode for inference\ncnn.eval()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T03:59:30.366312Z","iopub.execute_input":"2023-08-28T03:59:30.366729Z","iopub.status.idle":"2023-08-28T03:59:30.524838Z","shell.execute_reply.started":"2023-08-28T03:59:30.366677Z","shell.execute_reply":"2023-08-28T03:59:30.523761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the path for saving the new model checkpoint\nnew_model_path = '/kaggle/working/Pulmo_efficientnet_B0.pth'\n\n# Save the modified model's state_dict as a new checkpoint\ntorch.save(cnn.state_dict(), new_model_path)\n\nprint(f\"New model checkpoint saved at: {new_model_path}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T03:57:44.119943Z","iopub.execute_input":"2023-08-28T03:57:44.120341Z","iopub.status.idle":"2023-08-28T03:57:44.183004Z","shell.execute_reply.started":"2023-08-28T03:57:44.120309Z","shell.execute_reply":"2023-08-28T03:57:44.18202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Create a new instance of your model architecture\ncnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n\n# Define the path to the newly saved model checkpoint\nnew_model_path = '/kaggle/working/Pulmo_efficientnet_B0.pth'\n\n# Load the model checkpoint onto the CPU\ncheckpoint = torch.load(new_model_path, map_location=torch.device('cpu'))\n\n# Load the model's state_dict from the checkpoint\ncnn.load_state_dict(checkpoint)\n\n# Set the model in evaluation mode for inference\ncnn.eval()\n\nprint(\"New model checkpoint loaded successfully!\")\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T03:58:24.65648Z","iopub.execute_input":"2023-08-28T03:58:24.656865Z","iopub.status.idle":"2023-08-28T03:58:24.858552Z","shell.execute_reply.started":"2023-08-28T03:58:24.656833Z","shell.execute_reply":"2023-08-28T03:58:24.857066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer = cnn._fc\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T03:59:40.458136Z","iopub.execute_input":"2023-08-28T03:59:40.45928Z","iopub.status.idle":"2023-08-28T03:59:40.464151Z","shell.execute_reply.started":"2023-08-28T03:59:40.459234Z","shell.execute_reply":"2023-08-28T03:59:40.46315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T03:59:45.616232Z","iopub.execute_input":"2023-08-28T03:59:45.616608Z","iopub.status.idle":"2023-08-28T03:59:45.622686Z","shell.execute_reply.started":"2023-08-28T03:59:45.616569Z","shell.execute_reply":"2023-08-28T03:59:45.621663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Register hooks to the layers of your choice\nhook_handles = []  # To store the hook handles for later removal\nhook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\nhook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n# Load les checkpoint du model\nmodel_path = '/kaggle/working/PE_efficientnet_B0.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))  \ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  #  GPU\n\n\n# Now, you can perform your forward and backward passes to trigger the hooks\ninput_tensor = torch.randn(16, 1, 224, 224).cuda()  # Assuming input size is (batch_size, channels, height, width)\noutput = cnn(input_tensor)\noutput.sum().backward()\n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-28T04:00:35.085631Z","iopub.execute_input":"2023-08-28T04:00:35.086148Z","iopub.status.idle":"2023-08-28T04:00:35.446457Z","shell.execute_reply.started":"2023-08-28T04:00:35.086107Z","shell.execute_reply":"2023-08-28T04:00:35.443818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Define the path to save the modified model checkpoint\nnew_model_path = '/kaggle/working/PE_efficientnet_B0.pth'\n\n# Save the modified model's state_dict\ntorch.save(cnn.state_dict(), new_model_path)\n\n# Load the modified model\nnew_cnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\nnew_cnn._conv_stem.in_channels = 1\nnew_cnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the state_dict of the modified model\nnew_checkpoint = torch.load(new_model_path)\nnew_cnn.load_state_dict(new_checkpoint)\n\n\ncnn = new_cnn\n# Set the model in evaluation mode for inference\ncnn.eval()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T04:03:07.408118Z","iopub.execute_input":"2023-08-28T04:03:07.408663Z","iopub.status.idle":"2023-08-28T04:03:07.760808Z","shell.execute_reply.started":"2023-08-28T04:03:07.408624Z","shell.execute_reply":"2023-08-28T04:03:07.759868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Register hooks to the layers of your choice\nhook_handles = []  # To store the hook handles for later removal\nhook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\nhook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n\n\n\n\ncnn.to(torch.device('cuda'))  #  GPU\n\n\n# Now, you can perform your forward and backward passes to trigger the hooks\ninput_tensor = torch.randn(16, 1, 224, 224).cuda()  # Assuming input size is (batch_size, channels, height, width)\noutput = cnn(input_tensor)\noutput.sum().backward()\n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])","metadata":{"execution":{"iopub.status.busy":"2023-08-28T04:05:14.834512Z","iopub.execute_input":"2023-08-28T04:05:14.835308Z","iopub.status.idle":"2023-08-28T04:05:14.919548Z","shell.execute_reply.started":"2023-08-28T04:05:14.835236Z","shell.execute_reply":"2023-08-28T04:05:14.918619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T15:51:14.83912Z","iopub.execute_input":"2023-08-27T15:51:14.840407Z","iopub.status.idle":"2023-08-27T15:51:14.848771Z","shell.execute_reply.started":"2023-08-27T15:51:14.84036Z","shell.execute_reply":"2023-08-27T15:51:14.846843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# N\n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])","metadata":{"execution":{"iopub.status.busy":"2023-08-28T04:05:29.898668Z","iopub.execute_input":"2023-08-28T04:05:29.899088Z","iopub.status.idle":"2023-08-28T04:05:29.905783Z","shell.execute_reply.started":"2023-08-28T04:05:29.899057Z","shell.execute_reply":"2023-08-28T04:05:29.904607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T15:51:20.93149Z","iopub.execute_input":"2023-08-27T15:51:20.931883Z","iopub.status.idle":"2023-08-27T15:51:20.93734Z","shell.execute_reply.started":"2023-08-27T15:51:20.931853Z","shell.execute_reply":"2023-08-27T15:51:20.936299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#N\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])","metadata":{"execution":{"iopub.status.busy":"2023-08-28T04:06:04.57786Z","iopub.execute_input":"2023-08-28T04:06:04.578235Z","iopub.status.idle":"2023-08-28T04:06:04.583499Z","shell.execute_reply.started":"2023-08-28T04:06:04.578205Z","shell.execute_reply":"2023-08-28T04:06:04.582529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn.functional as F\nimport matplotlib.pyplot as plt\n\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Draw the heatmap\n    plt.imshow(heatmap_np, cmap='viridis', interpolation='nearest')\n    plt.axis('off')  # Turn off axis numbers and ticks\n    plt.show()\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T14:16:19.890186Z","iopub.execute_input":"2023-08-27T14:16:19.891469Z","iopub.status.idle":"2023-08-27T14:16:23.511638Z","shell.execute_reply.started":"2023-08-27T14:16:19.891419Z","shell.execute_reply":"2023-08-27T14:16:23.510339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/c1a5538bba20.dcm","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# N\n\nimport torch\nimport torch.nn.functional as F\nimport matplotlib.pyplot as plt\n\n# Loop through the batch dimension\nfor batch_idx in range(gradients[0].size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(gradients[0].size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Draw the heatmap\n    plt.imshow(heatmap_np, cmap='viridis', interpolation='nearest')\n    plt.axis('off')  # Turn off axis numbers and ticks\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T04:08:25.873856Z","iopub.execute_input":"2023-08-28T04:08:25.874573Z","iopub.status.idle":"2023-08-28T04:08:28.586414Z","shell.execute_reply.started":"2023-08-28T04:08:25.874538Z","shell.execute_reply":"2023-08-28T04:08:28.585477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_heatmap_overlay(image, heatmap):\n    heatmap = heatmap.detach().cpu().numpy()\n    heatmap = (heatmap - np.min(heatmap)) / (np.max(heatmap) - np.min(heatmap))\n    \n    cmap = plt.get_cmap('jet')\n    heatmap_colored = cmap(heatmap)\n    heatmap_colored = (255 * heatmap_colored).astype(np.uint8)\n    \n    overlay = PIL.Image.fromarray(heatmap_colored)\n    overlay = overlay.resize(image.size, resample=PIL.Image.BICUBIC)  # Resize to match image size\n    \n    # Convert both images to RGBA mode\n    image = image.convert(\"RGBA\")\n    overlay = overlay.convert(\"RGBA\")\n    \n    # Blend the images\n    blended_image = PIL.Image.alpha_composite(image, overlay)\n    \n    return blended_image\n\n# Loop through the images in your batch\nfor i in range(len(indices)):\n    index = indices[i]\n    data_i = data[i, 0, :, :]\n    heatmap_i = heatmap[i]  # Assuming heatmap is a tensor of heatmaps\n    \n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n    \n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap_i)\n    \n    # Plot the original image and the heatmap overlay side by side\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n    \n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n    \n    # Show the heatmap overlay\n    ax[1].imshow(heatmap_overlay, interpolation='nearest', alpha=0.5)\n    \n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T10:53:16.930701Z","iopub.execute_input":"2023-08-28T10:53:16.931162Z","iopub.status.idle":"2023-08-28T10:53:19.720814Z","shell.execute_reply.started":"2023-08-28T10:53:16.931119Z","shell.execute_reply":"2023-08-28T10:53:19.719337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Register hooks to the layers of your choice\nhook_handles = []  # To store the hook handles for later removal\nhook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\nhook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n# Load les checkpoint du model\nmodel_path = '/kaggle/input/best-model-6/best_model_6.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))  \ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  #  GPU\n\n\n# Now, you can perform your forward and backward passes to trigger the hooks\ninput_tensor = torch.randn(16, 1, 224, 224).cuda()  # Assuming input size is (batch_size, channels, height, width)\noutput = cnn(input_tensor)\noutput.sum().backward()\n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Draw red bounding box on the overlay image\n    draw = ImageDraw.Draw(heatmap_overlay_with_bbox)\n    draw.rectangle([bbox_x0, bbox_y0, bbox_x1, bbox_y1], outline='red', width=2)\n\n    # Plot the original image and the heatmap overlay with bounding box\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n\n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n\n    # Show the heatmap overlay with bounding box\n    ax[1].imshow(heatmap_overlay_with_bbox, interpolation='nearest', alpha=0.5)\n\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:18:26.58771Z","iopub.execute_input":"2023-08-28T11:18:26.588123Z","iopub.status.idle":"2023-08-28T11:18:26.603075Z","shell.execute_reply.started":"2023-08-28T11:18:26.58809Z","shell.execute_reply":"2023-08-28T11:18:26.602019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"# With prediction \n\n\n# Good\n\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Register hooks to the layers of your choice\nhook_handles = []  # To store the hook handles for later removal\nhook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\nhook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n# Load les checkpoint du model\nmodel_path = '/kaggle/input/best-model-6/best_model_6.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))  \ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  #  GPU\n\n\n# Now, you can perform your forward and backward passes to trigger the hooks\ninput_tensor = torch.randn(16, 1, 224, 224).cuda()  # Assuming input size is (batch_size, channels, height, width)\noutput = cnn(input_tensor)\noutput.sum().backward()\n\n\n\nwith torch.no_grad():\n    output = cnn(input_tensor)\npredictions = torch.sigmoid(output)  # Assuming sigmoid activation for binary classification\n\n\n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Draw red bounding box on the overlay image\n    draw = ImageDraw.Draw(heatmap_overlay_with_bbox)\n    draw.rectangle([bbox_x0, bbox_y0, bbox_x1, bbox_y1], outline='red', width=2)\n\n    # Plot the original image and the heatmap overlay with bounding box\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n\n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n\n    # Show the heatmap overlay with bounding box\n    ax[1].imshow(heatmap_overlay_with_bbox, interpolation='nearest', alpha=0.5)\n    \n    # Display the prediction\n    prediction = predictions[batch_idx].item()\n    ax[1].text(10, 30, f'Prediction: {prediction:.2f}', fontsize=12, color='white', backgroundcolor='red')\n\n   # plt.show()\n\n    \n\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:19:38.644168Z","iopub.execute_input":"2023-08-28T11:19:38.644535Z","iopub.status.idle":"2023-08-28T11:19:38.657193Z","shell.execute_reply.started":"2023-08-28T11:19:38.644504Z","shell.execute_reply":"2023-08-28T11:19:38.656108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Draw red bounding box on the overlay image\n    draw = ImageDraw.Draw(heatmap_overlay_with_bbox)\n    draw.rectangle([bbox_x0, bbox_y0, bbox_x1, bbox_y1], outline='red', width=2)\n\n    # Plot the original image and the heatmap overlay with bounding box\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n\n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n\n    # Show the heatmap overlay with bounding box\n    ax[1].imshow(heatmap_overlay_with_bbox, interpolation='nearest', alpha=0.5)\n    \n    # Display the prediction\n    prediction = predictions[batch_idx].item()\n    ax[1].text(10, 30, f'Prediction: {prediction:.2f}', fontsize=12, color='white', backgroundcolor='red')\n\n   # plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Prediction\n# With PE\n\n\"\"\"   6897fa9de148 \t2bfbb7fd2e8b \t1f108fe8cae4\n\n6897fa9de148 \t2bfbb7fd2e8b \tc1a5538bba20\n\n6897fa9de148 \t2bfbb7fd2e8b \tfd9db7e0cffe\n\n6897fa9de148 \t2bfbb7fd2e8b \t2a6b4c0ef0e5\n\n\n6897fa9de148 \t2bfbb7fd2e8b \t21c2645ae16d\n\n4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01   \"\"\"\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\nimport torch\nimport torch.nn.functional as F\nfrom efficientnet_pytorch import EfficientNet\nimport pydicom\nfrom skimage.transform import resize\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the model checkpoint\nmodel_path = '/kaggle/input/best-mode-15l/best_mode_15l.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))\ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"# With prediction\n\n# 6897fa9de148 \t2bfbb7fd2e8b \t1f108fe8cae4\n\nimport pydicom\nfrom skimage.transform import resize\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n\n\n# Load and preprocess DICOM images for prediction\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/1f108fe8cae4.dcm'\n\ndicom_image = load_dicom_image(dicom_filepath)\nnew_size = (224, 224)  # Size expected by the model\n\n# Call your resize function to load and preprocess the DICOM image\nresized_image = resize_dicom_image(dicom_filepath, new_size)\nif resized_image is None:\n    print(\"Error loading or resizing the DICOM image.\")\nelse:\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n\n# Convert the preprocessed image to a PyTorch tensor\ninput_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:24:31.929107Z","iopub.execute_input":"2023-08-28T11:24:31.929493Z","iopub.status.idle":"2023-08-28T11:24:31.945421Z","shell.execute_reply.started":"2023-08-28T11:24:31.929464Z","shell.execute_reply":"2023-08-28T11:24:31.943233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"# With prediction \n\n\n# Good\n\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Register hooks to the layers of your choice\nhook_handles = []  # To store the hook handles for later removal\nhook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\nhook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n# Load les checkpoint du model\nmodel_path = '/kaggle/input/best-model-6/best_model_6.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))  \ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  #  GPU\n\n\n# Now, you can perform your forward and backward passes to trigger the hooks\ninput_tensor = torch.randn(16, 1, 224, 224).cuda()  # Assuming input size is (batch_size, channels, height, width)\noutput = cnn(input_tensor)\noutput.sum().backward()\n\n\n\nwith torch.no_grad():\n    output = cnn(input_tensor)\npredictions = torch.sigmoid(output)  # Assuming sigmoid activation for binary classification\n\n\n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Draw red bounding box on the overlay image\n    draw = ImageDraw.Draw(heatmap_overlay_with_bbox)\n    draw.rectangle([bbox_x0, bbox_y0, bbox_x1, bbox_y1], outline='red', width=2)\n\n    # Plot the original image and the heatmap overlay with bounding box\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n\n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n\n    # Show the heatmap overlay with bounding box\n    ax[1].imshow(heatmap_overlay_with_bbox, interpolation='nearest', alpha=0.5)\n    \n    # Display the prediction\n    prediction = predictions[batch_idx].item()\n    ax[1].text(10, 30, f'Prediction: {prediction:.2f}', fontsize=12, color='white', backgroundcolor='red')\n\n   # plt.show()\n\n    \n\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:20:57.96458Z","iopub.execute_input":"2023-08-28T11:20:57.96505Z","iopub.status.idle":"2023-08-28T11:20:57.981326Z","shell.execute_reply.started":"2023-08-28T11:20:57.965011Z","shell.execute_reply":"2023-08-28T11:20:57.980305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\nimport torch\nimport torch.nn.functional as F\nfrom efficientnet_pytorch import EfficientNet\nimport pydicom\nfrom skimage.transform import resize\nimport sys\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n\noriginal_stdout = sys.stdout\n\n\nsys.stdout = open('null', 'w')\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the model checkpoint\nmodel_path = '/kaggle/input/best-mode-15l/best_mode_15l.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))\ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))\n\n# Restore the original sys.stdout\nsys.stdout.close()\nsys.stdout = original_stdout\n\n\n\n# Define hooks\nhook_handles = []  # To store the hook handles for later removal\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n# Resize DICOM image\ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n\n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n\n# Load and preprocess DICOM images for prediction\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/2a6b4c0ef0e5.dcm'\ndicom_image = load_dicom_image(dicom_filepath)\nnew_size = (256, 256)  # Size expected by the model\n\n# Call your resize function to load and preprocess the DICOM image\nresized_image = resize_dicom_image(dicom_filepath, new_size)\nif resized_image is None:\n    print(\"Error loading or resizing the DICOM image.\")\nelse:\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Remove hooks before performing forward pass\n    for handle in hook_handles:\n        handle.remove()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n\n    # Re-register hooks after the forward pass if needed\n    hook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\n    hook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n        \n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the weighted activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Resize the heatmap to match the dimensions of the original DICOM image\n    heatmap_resized = resize(heatmap_np, dicom_image.shape, anti_aliasing=True)\n\n    # Plot the DICOM image, heatmap overlay, and bounding box\n    fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n    ax.imshow(PIL.Image.fromarray(dicom_image), cmap='gray')  # Use dicom_image here\n\n    # Show the resized heatmap overlay with bounding box\n    ax.imshow(heatmap_resized, cmap='hot', alpha=0.5)\n\n    # Display the prediction\n    #prediction_text = f'Prediction: {prediction:.2f}'\n    #ax.text(10, 30, prediction_text, fontsize=12, color='white', backgroundcolor='red')\n\n    plt.axis('off')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:28:36.17799Z","iopub.execute_input":"2023-08-28T11:28:36.178377Z","iopub.status.idle":"2023-08-28T11:28:41.879826Z","shell.execute_reply.started":"2023-08-28T11:28:36.178346Z","shell.execute_reply":"2023-08-28T11:28:41.878884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Prediction\n# With PE\n\n\"\"\"   6897fa9de148 \t2bfbb7fd2e8b \t1f108fe8cae4\n\n6897fa9de148 \t2bfbb7fd2e8b \tc1a5538bba20\n\n6897fa9de148 \t2bfbb7fd2e8b \tfd9db7e0cffe\n\n6897fa9de148 \t2bfbb7fd2e8b \t2a6b4c0ef0e5\n\n\n6897fa9de148 \t2bfbb7fd2e8b \t21c2645ae16d\n\n4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01   \"\"\"\n\n\"\"\"import numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\nimport torch\nimport torch.nn.functional as F\nfrom efficientnet_pytorch import EfficientNet\nimport pydicom\nfrom skimage.transform import resize\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the model checkpoint\nmodel_path = '/kaggle/input/best-mode-15l/best_mode_15l.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))\ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  # Move the model to the GPU\n\n# Define hooks\nhook_handles = []  # To store the hook handles for later removal\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n# Resize DICOM image\ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n\n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n\n# Load and preprocess DICOM images for prediction\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/2a6b4c0ef0e5.dcm'\ndicom_image = load_dicom_image(dicom_filepath)\nnew_size = (256, 256)  # Size expected by the model\n\n# Call your resize function to load and preprocess the DICOM image\nresized_image = resize_dicom_image(dicom_filepath, new_size)\nif resized_image is None:\n    print(\"Error loading or resizing the DICOM image.\")\nelse:\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Remove hooks before performing forward pass\n    for handle in hook_handles:\n        handle.remove()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n\n    # Re-register hooks after the forward pass if needed\n    hook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\n    hook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n        \n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the weighted activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Resize the heatmap to match the dimensions of the original DICOM image\n    heatmap_resized = resize(heatmap_np, dicom_image.shape, anti_aliasing=True)\n\n    # Plot the DICOM image, heatmap overlay, and bounding box\n    fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n    ax.imshow(PIL.Image.fromarray(dicom_image), cmap='gray')  # Use dicom_image here\n\n    # Show the resized heatmap overlay with bounding box\n    ax.imshow(heatmap_resized, cmap='hot', alpha=0.5)\n\n    # Display the prediction\n    #prediction_text = f'Prediction: {prediction:.2f}'\n    #ax.text(10, 30, prediction_text, fontsize=12, color='white', backgroundcolor='red')\n\n    plt.axis('off')\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:32:10.428319Z","iopub.execute_input":"2023-08-28T11:32:10.428729Z","iopub.status.idle":"2023-08-28T11:32:10.442614Z","shell.execute_reply.started":"2023-08-28T11:32:10.428696Z","shell.execute_reply":"2023-08-28T11:32:10.441674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Prediction\n# With PE\n\n\"\"\"   6897fa9de148 \t2bfbb7fd2e8b \t1f108fe8cae4\n\n6897fa9de148 \t2bfbb7fd2e8b \tc1a5538bba20\n\n6897fa9de148 \t2bfbb7fd2e8b \tfd9db7e0cffe\n\n6897fa9de148 \t2bfbb7fd2e8b \t2a6b4c0ef0e5\n\n\n6897fa9de148 \t2bfbb7fd2e8b \t21c2645ae16d\n\n4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01   \"\"\"\n\n\"\"\"import numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\nimport torch\nimport torch.nn.functional as F\nfrom efficientnet_pytorch import EfficientNet\nimport pydicom\nfrom skimage.transform import resize\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Define the path to save the modified model checkpoint\nnew_model_path = '/kaggle/working/PE_efficientnet_B0.pth'\n\n# Save the modified model's state_dict\ntorch.save(cnn.state_dict(), new_model_path)\n\n# Load the modified model\nnew_cnn = EfficientNet.from_pretrained('efficientnet-b0', num_classes=1).cuda()\nnew_cnn._conv_stem.in_channels = 1\nnew_cnn._conv_stem = torch.nn.Conv2d(1, 32, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the state_dict of the modified model\nnew_checkpoint = torch.load(new_model_path)\nnew_cnn.load_state_dict(new_checkpoint)\n\n\ncnn = new_cnn\n\n# Define hooks\nhook_handles = []  # To store the hook handles for later removal\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n# Resize DICOM image\ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n\n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n\n# Load and preprocess DICOM images for prediction\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/2a6b4c0ef0e5.dcm'\ndicom_image = load_dicom_image(dicom_filepath)\nnew_size = (256, 256)  # Size expected by the model\n\n# Call your resize function to load and preprocess the DICOM image\nresized_image = resize_dicom_image(dicom_filepath, new_size)\nif resized_image is None:\n    print(\"Error loading or resizing the DICOM image.\")\nelse:\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Remove hooks before performing forward pass\n    for handle in hook_handles:\n        handle.remove()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n\n    # Re-register hooks after the forward pass if needed\n    hook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\n    hook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n        \n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n# Resize gradients to match activations spatial dimensions\nresized_gradients = F.interpolate(gradients[0], size=activations.size()[2:], mode='bilinear', align_corners=False)\n\n# Define hooks\nhook_handles = []\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output[0]\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n\n# Register hooks\nhook_handles.append(cnn._blocks[-1].register_full_backward_hook(backward_hook))\nhook_handles.append(cnn._blocks[-1].register_forward_hook(forward_hook))\n\n# Convert the preprocessed image to a PyTorch tensor\ninput_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n# Perform forward pass to get predictions and trigger hooks\nwith torch.no_grad():\n    output = cnn(input_tensor)\n    prediction = torch.sigmoid(output).item()\n\n# Calculate gradients\noutput.backward()\n\n# Remove hooks after computation\nfor handle in hook_handles:\n    handle.remove()\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Resize gradients to match activations spatial dimensions\nresized_gradients = F.interpolate(gradients, size=activations.size()[2:], mode='bilinear', align_corners=False)\n\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels in activations by corresponding gradients\n    weighted_activations = activations[batch_idx] * resized_gradients[batch_idx]\n\n    # Sum along the channel dimension to get the class activation map\n    class_activation_map = torch.sum(weighted_activations, dim=0)\n\n    # ReLU on top of the class activation map\n    class_activation_map = F.relu(class_activation_map)\n\n    # Normalize the class activation map\n    class_activation_map /= torch.max(class_activation_map)\n\n    # Convert to numpy array\n    cam_np = class_activation_map.cpu().detach().numpy()\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), cam_np)\n\n    # Find the index of the maximum value in the class activation map\n    max_index = np.unravel_index(np.argmax(cam_np), cam_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Resize the class activation map to match the dimensions of the original DICOM image\n    cam_resized = resize(cam_np, dicom_image.shape, anti_aliasing=True)\n\n    # Plot the DICOM image, heatmap overlay, and bounding box\n    fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n    ax.imshow(PIL.Image.fromarray(dicom_image), cmap='gray')  # Use dicom_image here\n\n    # Show the resized heatmap overlay with bounding box\n    ax.imshow(cam_resized, cmap='hot', alpha=0.5)\n\n    # Display the prediction\n    prediction_text = f'Prediction: {prediction:.2f}'\n    ax.text(10, 30, prediction_text, fontsize=12, color='white', backgroundcolor='red')\n\n    plt.axis('off')\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:34:36.081792Z","iopub.execute_input":"2023-08-28T11:34:36.082179Z","iopub.status.idle":"2023-08-28T11:34:36.099909Z","shell.execute_reply.started":"2023-08-28T11:34:36.082149Z","shell.execute_reply":"2023-08-28T11:34:36.098908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Gradients shape:\", gradients[0].shape)\nprint(\"Activations shape:\", activations.shape)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-28T04:19:11.300851Z","iopub.execute_input":"2023-08-28T04:19:11.301245Z","iopub.status.idle":"2023-08-28T04:19:11.310857Z","shell.execute_reply.started":"2023-08-28T04:19:11.301214Z","shell.execute_reply":"2023-08-28T04:19:11.309618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the model checkpoint\nmodel_path = '/kaggle/input/best-model-6/best_model_6.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))\ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  # Move the model to the GPU\n\n# Define hooks\nhook_handles = []  # To store the hook handles for later removal\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n# Resize DICOM image\ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n\n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n\n# Define heatmap-related functions\n# ... (your heatmap-related functions)\n\n# Create a DataProcessor instance\ncsv_file = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train.csv'\nsplit = 0  # Adjust as needed\nsamples_per_split = 1  # Adjust as needed\ntransform = None  # Adjust as needed\ndata_processor = DataProcessor(csv_file, split, samples_per_split, transform)\n\n# Loop through the samples in the split dataframe\nfor idx, row in data_processor.split_df.iterrows():\n    # Load and preprocess DICOM image\n    dicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/2a6b4c0ef0e5.dcm'\n    dicom_image = load_dicom_image(dicom_filepath)\n    new_size = (256, 256)  # Size expected by the model\n\n    # Call your resize function to load and preprocess the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, new_size)\n    if resized_image is None:\n        print(\"Error loading or resizing the DICOM image.\")\n        continue\n\n    # Apply image transformations\n    image_processor = ImageProcessor(data_processor)\n    preprocessed_image = resized_image  # Replace with your transformations\n\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(preprocessed_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Remove hooks before performing forward pass\n    for handle in hook_handles:\n        handle.remove()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n        \n    # Re-register hooks after the forward pass if needed\n    hook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\n    hook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n        \n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the weighted activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Resize the heatmap to match the dimensions of the original DICOM image\n    heatmap_resized = resize(heatmap_np, dicom_image.shape, anti_aliasing=True)\n\n    # Plot the DICOM image, heatmap overlay, and bounding box\n    fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n    ax.imshow(PIL.Image.fromarray(dicom_image), cmap='gray')  # Use dicom_image here\n\n    # Show the resized heatmap overlay with bounding box\n    ax.imshow(heatmap_resized, cmap='hot', alpha=0.5)\n\n    # Display the prediction\n    prediction_text = f'Prediction: {prediction:.2f}'\n    ax.text(10, 30, prediction_text, fontsize=12, color='white', backgroundcolor='red')\n\n    plt.axis('off')\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-28T11:35:11.375805Z","iopub.execute_input":"2023-08-28T11:35:11.376222Z","iopub.status.idle":"2023-08-28T11:35:11.389205Z","shell.execute_reply.started":"2023-08-28T11:35:11.376189Z","shell.execute_reply":"2023-08-28T11:35:11.388255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\npulmo_dataset.pos_df\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T17:53:34.423443Z","iopub.execute_input":"2023-08-27T17:53:34.423891Z","iopub.status.idle":"2023-08-27T17:53:34.448239Z","shell.execute_reply.started":"2023-08-27T17:53:34.423858Z","shell.execute_reply":"2023-08-27T17:53:34.447299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Prediction\n# With PE\n\n# V2\n\n\"\"\"   6897fa9de148 \t2bfbb7fd2e8b \t1f108fe8cae4\n\n6897fa9de148 \t2bfbb7fd2e8b \tc1a5538bba20\n\n6897fa9de148 \t2bfbb7fd2e8b \tfd9db7e0cffe\n\n6897fa9de148 \t2bfbb7fd2e8b \t2a6b4c0ef0e5\n\n\n6897fa9de148 \t2bfbb7fd2e8b \t21c2645ae16d\n\n4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01   \"\"\"\n\n\n\n\"\"\"   4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01 \t   \"\"\"\n\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\nimport torch\nimport torch.nn.functional as F\nfrom efficientnet_pytorch import EfficientNet\nimport pydicom\nfrom skimage.transform import resize\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the model checkpoint\nmodel_path = '/kaggle/input/best-model-6/best_model_6.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))\ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  # Move the model to the GPU\n\n# Define hooks\nhook_handles = []  # To store the hook handles for later removal\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n# Resize DICOM image\ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n\n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n    \n\n\n# Load and preprocess DICOM images for prediction\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/4833c9b6a5d0/57e3e3c5f910/40f943bb1e01.dcm'\ndicom_image = load_dicom_image(dicom_filepath)\nnew_size = (256, 256)  # Size expected by the model\n\n# Call your resize function to load and preprocess the DICOM image\nresized_image = resize_dicom_image(dicom_filepath, new_size)\nif resized_image is None:\n    print(\"Error loading or resizing the DICOM image.\")\nelse:\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Remove hooks before performing forward pass\n    for handle in hook_handles:\n        handle.remove()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n\n    # Re-register hooks after the forward pass if needed\n    hook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\n    hook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n        \n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the weighted activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Resize the heatmap to match the dimensions of the original DICOM image\n    heatmap_resized = resize(heatmap_np, dicom_image.shape, anti_aliasing=True)\n\n    # Plot the DICOM image, heatmap overlay, and bounding box\n    fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n    ax.imshow(PIL.Image.fromarray(dicom_image), cmap='gray')  # Use dicom_image here\n\n    # Show the resized heatmap overlay with bounding box\n    ax.imshow(heatmap_resized, cmap='hot', alpha=0.5)\n\n    # Display the prediction\n    prediction_text = f'Prediction: {prediction:.2f}'\n    ax.text(10, 30, prediction_text, fontsize=12, color='white', backgroundcolor='red')\n\n    plt.axis('off')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T17:54:24.495404Z","iopub.execute_input":"2023-08-27T17:54:24.495788Z","iopub.status.idle":"2023-08-27T17:54:25.347269Z","shell.execute_reply.started":"2023-08-27T17:54:24.495757Z","shell.execute_reply":"2023-08-27T17:54:25.3463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Prediction\n# With PE\n\n# V2\n\n\"\"\"   6897fa9de148 \t2bfbb7fd2e8b \t1f108fe8cae4\n\n6897fa9de148 \t2bfbb7fd2e8b \tc1a5538bba20\n\n6897fa9de148 \t2bfbb7fd2e8b \tfd9db7e0cffe\n\n6897fa9de148 \t2bfbb7fd2e8b \t2a6b4c0ef0e5\n\n\n6897fa9de148 \t2bfbb7fd2e8b \t21c2645ae16d\n\n4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01   \"\"\"\n\n\n\n\"\"\"   4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01 \t   \"\"\"\n\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\nimport torch\nimport torch.nn.functional as F\nfrom efficientnet_pytorch import EfficientNet\nimport pydicom\nfrom skimage.transform import resize\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the model checkpoint\nmodel_path = '/kaggle/input/best-mode-15-pe/best_mode_15l.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))\ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  # Move the model to the GPU\n\n# Define hooks\nhook_handles = []  # To store the hook handles for later removal\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n# Resize DICOM image\ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n\n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n    \n\n\n# Load and preprocess DICOM images for prediction\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/4833c9b6a5d0/57e3e3c5f910/40f943bb1e01.dcm'\ndicom_image = load_dicom_image(dicom_filepath)\nnew_size = (256, 256)  # Size expected by the model\n\n# Call your resize function to load and preprocess the DICOM image\nresized_image = resize_dicom_image(dicom_filepath, new_size)\nif resized_image is None:\n    print(\"Error loading or resizing the DICOM image.\")\nelse:\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Remove hooks before performing forward pass\n    for handle in hook_handles:\n        handle.remove()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n\n    # Re-register hooks after the forward pass if needed\n    hook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\n    hook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n        \n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the weighted activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Resize the heatmap to match the dimensions of the original DICOM image\n    heatmap_resized = resize(heatmap_np, dicom_image.shape, anti_aliasing=True)\n\n    # Plot the DICOM image, heatmap overlay, and bounding box\n    fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n    ax.imshow(PIL.Image.fromarray(dicom_image), cmap='gray')  # Use dicom_image here\n\n    # Show the resized heatmap overlay with bounding box\n    ax.imshow(heatmap_resized, cmap='hot', alpha=0.5)\n\n    # Display the prediction\n    prediction_text = f'Prediction: {prediction:.2f}'\n    ax.text(10, 30, prediction_text, fontsize=12, color='white', backgroundcolor='red')\n\n    plt.axis('off')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T18:20:31.650534Z","iopub.execute_input":"2023-08-27T18:20:31.650971Z","iopub.status.idle":"2023-08-27T18:20:32.454768Z","shell.execute_reply.started":"2023-08-27T18:20:31.650939Z","shell.execute_reply":"2023-08-27T18:20:32.453745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With Prediction\n# With PE\n\n# V2\n\n\"\"\"   6897fa9de148 \t2bfbb7fd2e8b \t1f108fe8cae4\n\n6897fa9de148 \t2bfbb7fd2e8b \tc1a5538bba20\n\n6897fa9de148 \t2bfbb7fd2e8b \tfd9db7e0cffe\n\n6897fa9de148 \t2bfbb7fd2e8b \t2a6b4c0ef0e5\n\n\n6897fa9de148 \t2bfbb7fd2e8b \t21c2645ae16d\n\n4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01   \"\"\"\n\n\n\n\"\"\"   4833c9b6a5d0 \t57e3e3c5f910 \t40f943bb1e01 \t   \"\"\"\n\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\nimport torch\nimport torch.nn.functional as F\nfrom efficientnet_pytorch import EfficientNet\nimport pydicom\nfrom skimage.transform import resize\n\n\nimport torch\nfrom efficientnet_pytorch import EfficientNet\n\n# Instantiate and modify the EfficientNet model\ncnn = EfficientNet.from_pretrained('efficientnet-b4', num_classes=1).cuda()\ncnn._conv_stem.in_channels = 1\ncnn._conv_stem = torch.nn.Conv2d(1, 48, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False).cuda()\n\n# Load the model checkpoint\nmodel_path = '/kaggle/input/best-mode-15-pe/best_mode_15l.pth'\ncheckpoint = torch.load(model_path, map_location=torch.device('cuda'))\ncnn.load_state_dict(checkpoint)\ncnn.to(torch.device('cuda'))  # Move the model to the GPU\n\n# Define hooks\nhook_handles = []  # To store the hook handles for later removal\n\ndef backward_hook(module, grad_input, grad_output):\n    global gradients\n    gradients = grad_output\n    # Print the shape of gradients\n    print(f'Gradients size: {gradients[0].size()}')\n\ndef forward_hook(module, input, output):\n    global activations\n    activations = output\n    # Print the shape of activations\n    print(f'Activations size: {activations.size()}')\n\n# Load DICOM image using pydicom\ndef load_dicom_image(filepath):\n    ds = pydicom.dcmread(filepath)\n    image = ds.pixel_array\n    return image\n\n# Resize DICOM image\ndef resize_dicom_image(dicom_filepath, new_size):\n    try:\n        ds = pydicom.dcmread(dicom_filepath)\n        data = ds.pixel_array\n\n        if data is None:\n            raise Exception(\"Invalid or empty image data\")\n\n        resized_img = resize(data, new_size, anti_aliasing=True)\n\n        return resized_img\n\n    except Exception as e:\n        print(\"Error:\", str(e))\n        print(\"Image path:\", dicom_filepath)\n        return None\n    \n\n\n# Load and preprocess DICOM images for prediction\ndicom_filepath = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/4833c9b6a5d0/57e3e3c5f910/40f943bb1e01.dcm'\ndicom_image = load_dicom_image(dicom_filepath)\nnew_size = (256, 256)  # Size expected by the model\n\n# Call your resize function to load and preprocess the DICOM image\nresized_image = resize_dicom_image(dicom_filepath, new_size)\nif resized_image is None:\n    print(\"Error loading or resizing the DICOM image.\")\nelse:\n    # Convert the preprocessed image to a PyTorch tensor\n    input_tensor = torch.tensor(resized_image, dtype=torch.float32).unsqueeze(0).unsqueeze(0).cuda()\n\n    # Remove hooks before performing forward pass\n    for handle in hook_handles:\n        handle.remove()\n\n    # Perform forward pass to get predictions\n    with torch.no_grad():\n        output = cnn(input_tensor)\n        prediction = torch.sigmoid(output).item()\n\n    # Re-register hooks after the forward pass if needed\n    hook_handles.append(cnn._conv_head.register_forward_hook(forward_hook))\n    hook_handles.append(cnn._conv_head.register_backward_hook(backward_hook))\n\n        \n\n\n# Don't forget to remove hooks after you're done\nfor handle in hook_handles:\n    handle.remove()\n    \n# Register backward and forward hooks on the last layer of your EfficientNet-based model\nbackward_hook = cnn._blocks[-1].register_full_backward_hook(backward_hook, prepend=False)\nforward_hook = cnn._blocks[-1].register_forward_hook(forward_hook, prepend=False)\npooled_gradients = torch.mean(gradients[0], dim=[0, 2, 3])\n\n\n\n\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # Weight the channels by corresponding gradients\n    for i in range(activations.size()[1]):\n        activations[batch_idx, i, :, :] *= gradients[0][batch_idx, i, :, :]\n\n    # Average the channels of the weighted activations\n    heatmap = torch.mean(activations[batch_idx], dim=0)\n\n    # ReLU on top of the heatmap\n    heatmap = F.relu(heatmap)\n\n    # Normalize the heatmap\n    heatmap /= torch.max(heatmap)\n\n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Resize the heatmap to match the dimensions of the original DICOM image\n    heatmap_resized = resize(heatmap_np, dicom_image.shape, anti_aliasing=True)\n\n    # Plot the DICOM image, heatmap overlay, and bounding box\n    fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n    ax.imshow(PIL.Image.fromarray(dicom_image), cmap='gray')  # Use dicom_image here\n\n    # Show the resized heatmap overlay with bounding box\n    ax.imshow(heatmap_resized, cmap='hot', alpha=0.5)\n\n    # Display the prediction\n    prediction_text = f'Prediction: {prediction:.2f}'\n    ax.text(10, 30, prediction_text, fontsize=12, color='white', backgroundcolor='red')\n\n    plt.axis('off')\n    plt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With the folder check \n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # ... (your heatmap calculation code here)\n    \n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    y, x = max_index  # Swap the indices here\n    bbox_x0 = x - 10\n    bbox_y0 = y - 10\n    bbox_x1 = x + 10\n    bbox_y1 = y + 10\n\n    # Draw red bounding box on the overlay image\n    draw = ImageDraw.Draw(heatmap_overlay_with_bbox)\n    draw.rectangle([bbox_x0, bbox_y0, bbox_x1, bbox_y1], outline='red', width=2)\n\n    # Plot the original image and the heatmap overlay with bounding box\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n\n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n\n    # Show the heatmap overlay with bounding box\n    ax[1].imshow(heatmap_overlay_with_bbox, interpolation='nearest', alpha=0.5)\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T14:44:37.079813Z","iopub.execute_input":"2023-08-27T14:44:37.080205Z","iopub.status.idle":"2023-08-27T14:44:42.775386Z","shell.execute_reply.started":"2023-08-27T14:44:37.080173Z","shell.execute_reply":"2023-08-27T14:44:42.77426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport PIL.Image\nfrom PIL import ImageDraw\n\n# Assuming you have 'activations' and 'gradients' tensors\n# Loop through the batch dimension\nfor batch_idx in range(activations.size()[0]):\n    # ... (your heatmap calculation code here)\n    \n    # Convert to numpy array\n    heatmap_np = heatmap.cpu().detach().numpy()\n\n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n\n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap)\n\n    # Find the index of the maximum value in the heatmap\n    max_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n    # Create a copy of the heatmap overlay image to draw the bounding box on\n    heatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n    # Define bounding box coordinates based on the max_index\n    x, y = max_index\n    bbox_x0 = y - 10  # Swap x and y here\n    bbox_y0 = x - 10  # Swap x and y here\n    bbox_x1 = y + 10  # Swap x and y here\n    bbox_y1 = x + 10  # Swap x and y here\n\n    # Draw red bounding box on the overlay image\n    draw = ImageDraw.Draw(heatmap_overlay_with_bbox)\n    draw.rectangle([bbox_x0, bbox_y0, bbox_x1, bbox_y1], outline='red', width=2)\n\n    # Plot the original image and the heatmap overlay with bounding box\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n\n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n\n    # Show the heatmap overlay with bounding box\n    ax[1].imshow(heatmap_overlay_with_bbox, interpolation='nearest', alpha=0.5)\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T14:46:43.003637Z","iopub.execute_input":"2023-08-27T14:46:43.007149Z","iopub.status.idle":"2023-08-27T14:46:49.193243Z","shell.execute_reply.started":"2023-08-27T14:46:43.007101Z","shell.execute_reply":"2023-08-27T14:46:49.192306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the index of the maximum value in the heatmap\nmax_index = np.unravel_index(np.argmax(heatmap_np), heatmap_np.shape)\n\n# Create a copy of the heatmap overlay image to draw the bounding box on\nheatmap_overlay_with_bbox = heatmap_overlay.copy()\n\n# Define bounding box coordinates based on the max_index\nrow, col = max_index\nbbox_x0 = col - 10\nbbox_y0 = row - 10\nbbox_x1 = col + 10\nbbox_y1 = row + 10\n\n# Draw red bounding box on the overlay image\ndraw = ImageDraw.Draw(heatmap_overlay_with_bbox)\ndraw.rectangle([bbox_x0, bbox_y0, bbox_x1, bbox_y1], outline='red', width=2)\n\n# Plot the original image and the heatmap overlay with bounding box\nfig, ax = plt.subplots(1, 2, figsize=(10, 5))\nax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\nax[0].set_title('Resized DICOM Image')\nax[0].axis('off')\n\nax[1].imshow(data_i, cmap='gray')\nax[1].set_title('Original Data Image')\nax[1].axis('off')\n\n# Show the heatmap overlay with bounding box\nax[1].imshow(heatmap_overlay_with_bbox, interpolation='nearest', alpha=0.5)\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T14:52:45.821603Z","iopub.execute_input":"2023-08-27T14:52:45.822017Z","iopub.status.idle":"2023-08-27T14:52:46.161501Z","shell.execute_reply.started":"2023-08-27T14:52:45.821983Z","shell.execute_reply":"2023-08-27T14:52:46.160371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"# Loop through the images in your batch\nfor i in range(len(indices)):\n    index = indices[i]\n    data_i = data[i, 0, :, :]\n    heatmap_i = heatmap[i]  # Assuming heatmap is a tensor of heatmaps\n    \n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n    \n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap_i)\n    \n    # Plot the original image and the heatmap overlay side by side\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n    \n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n    \n    # Show the heatmap overlay\n    ax[1].imshow(heatmap_overlay, interpolation='nearest', alpha=0.5)\n    \n    plt.show()\n\"\"\"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T14:20:31.722473Z","iopub.execute_input":"2023-08-27T14:20:31.723002Z","iopub.status.idle":"2023-08-27T14:20:31.735929Z","shell.execute_reply.started":"2023-08-27T14:20:31.722962Z","shell.execute_reply":"2023-08-27T14:20:31.734857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loop through the images in your batch\nfor i in range(len(indices)):\n    index = indices[i]\n    data_i = data[i, 0, :, :]\n    heatmap_i = heatmap[i].detach().cpu().numpy()  # Detach and convert to NumPy array\n    \n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n    \n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap_i)\n    \n    # Find the coordinates of the maximum value in the heatmap\n    max_coords = np.unravel_index(np.argmax(heatmap_i), heatmap_i.shape)\n    \n    # Plot the original image and the heatmap overlay side by side\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n    \n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n    \n    # Show the heatmap overlay\n    heatmap_plot = ax[1].imshow(heatmap_overlay, interpolation='nearest', alpha=0.5)\n    \n    # Draw a circle around the highest prediction\n    circle = Circle((max_coords[1], max_coords[0]), radius=10, color='red', fill=False)\n    ax[1].add_patch(circle)\n    \n    plt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T10:14:16.072354Z","iopub.status.busy":"2023-08-26T10:14:16.071989Z","iopub.status.idle":"2023-08-26T10:14:16.163758Z","shell.execute_reply":"2023-08-26T10:14:16.162315Z","shell.execute_reply.started":"2023-08-26T10:14:16.072323Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loop through the images in your batch\nfor i in range(len(indices)):\n    index = indices[i]\n    data_i = data[i, 0, :, :]\n    heatmap_i = heatmap[i]  # Assuming heatmap is a tensor of heatmaps\n    \n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n    \n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap_i)\n    \n    # Find the coordinates of the maximum value in the heatmap using PyTorch functions\n    max_indices = torch.nonzero(heatmap_i == heatmap_i.max()).squeeze()\n    \n    # Convert max_indices to a NumPy array\n    max_coords = max_indices.cpu().numpy()\n    \n    # Plot the original image and the heatmap overlay side by side\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n    \n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n    \n    # Show the heatmap overlay\n    heatmap_plot = ax[1].imshow(heatmap_overlay, interpolation='nearest', alpha=0.5)\n    \n    # Draw a circle around the highest prediction\n    circle = Circle((max_coords[1], max_coords[0]), radius=10, color='red', fill=False)\n    ax[1].add_patch(circle)\n    \n    plt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T10:17:54.948813Z","iopub.status.busy":"2023-08-26T10:17:54.948437Z","iopub.status.idle":"2023-08-26T10:17:55.338613Z","shell.execute_reply":"2023-08-26T10:17:55.337277Z","shell.execute_reply.started":"2023-08-26T10:17:54.948781Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loop through the images in your batch\nfor i in range(len(indices)):\n    index = indices[i]\n    data_i = data[i, 0, :, :]\n    heatmap_i = heatmap[i]  # Assuming heatmap is a tensor of heatmaps\n    \n    # Resize the DICOM image\n    resized_image = resize_dicom_image(dicom_filepath, (256, 256))\n    if resized_image is None:\n        continue\n    \n    # Generate heatmap overlay\n    heatmap_overlay = generate_heatmap_overlay(PIL.Image.fromarray(resized_image), heatmap_i)\n    \n    # Find the coordinates of the maximum value in the heatmap\n    max_index = torch.argmax(heatmap_i)\n    max_coord_y = max_index // heatmap_i.shape[1]  # Row index\n    max_coord_x = max_index % heatmap_i.shape[1]   # Column index\n    \n    # Plot the original image and the heatmap overlay side by side\n    fig, ax = plt.subplots(1, 2, figsize=(10, 5))\n    ax[0].imshow(PIL.Image.fromarray(resized_image), cmap='gray')\n    ax[0].set_title('Resized DICOM Image')\n    ax[0].axis('off')\n    \n    ax[1].imshow(data_i, cmap='gray')\n    ax[1].set_title('Original Data Image')\n    ax[1].axis('off')\n    \n    # Show the heatmap overlay\n    heatmap_plot = ax[1].imshow(heatmap_overlay, interpolation='nearest', extent=ax[1].get_extent())\n    \n    # Draw a circle around the highest prediction\n    circle = Circle((max_coord_x, max_coord_y), radius=10, color='red', fill=False)\n    ax[1].add_patch(circle)\n    \n    plt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T10:22:59.025521Z","iopub.status.busy":"2023-08-26T10:22:59.025042Z","iopub.status.idle":"2023-08-26T10:22:59.122551Z","shell.execute_reply":"2023-08-26T10:22:59.120237Z","shell.execute_reply.started":"2023-08-26T10:22:59.025483Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom torchvision import transforms\nfrom torchvision.transforms import ToTensor\nfrom PIL import Image\n\ndicom_file_path = \"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/c1a5538bba20.dcm\"\n\nnew_size =(256,256)\n\nim = resize_dicom_image(dicom_filepath, new_size)\n\nimage_size = 256\ntransform = transforms.Compose([\n    #transforms.Resize(image_size, antialias=True),\n    #transforms.CenterCrop(im),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n])\n\n# Load the DICOM file using pydicom\ndicom_file_path = \"/kaggle/input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/c1a5538bba20.dcm\"\ndicom = pydicom.dcmread(dicom_file_path)\n\n# Convert the DICOM to a PIL Image\nimage_array = dicom.pixel_array  # Assuming pixel data is in the pixel_array attribute\nimage = Image.fromarray(image_array)\n\n# Apply the transformations\nimg_tensor = transform(image)  # Tensor that represents the image\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T07:34:04.295183Z","iopub.status.busy":"2023-08-26T07:34:04.294474Z","iopub.status.idle":"2023-08-26T07:34:04.551785Z","shell.execute_reply":"2023-08-26T07:34:04.550309Z","shell.execute_reply.started":"2023-08-26T07:34:04.29515Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from torch.utils.data import DataLoader\n\nbatch_size = 64  # You can choose any batch size you want\ntrain_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\nvalid_loader = DataLoader(valid_dataset, batch_size=batch_size)\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-25T11:39:34.923515Z","iopub.status.busy":"2023-08-25T11:39:34.922829Z","iopub.status.idle":"2023-08-25T11:39:34.983025Z","shell.execute_reply":"2023-08-25T11:39:34.981476Z","shell.execute_reply.started":"2023-08-25T11:39:34.923478Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import os\nimport time\nimport pickle\nimport numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\n# Assuming you have defined n_epochs, schedule, get_optimizer, get_loaders, train_epoch, valid_epoch, and master_train_loss, master_valid_loss\n\n# Initialize variables\nbest_valid_loss = float('inf')\nepoch = 1\n\ndef normalize_loss(loss):\n    min_loss = np.min(loss)\n    max_loss = np.max(loss)\n    normalized_loss = (loss - min_loss) / (max_loss - min_loss)\n    return normalized_loss\n\nwhile epoch <= n_epochs:\n    print(time.ctime(), 'Epoch:', epoch)\n    \n    # Update learning rate \n    if epoch in schedule:\n        new_lr = optimizer.param_groups[0][\"lr\"] / gamma\n        optimizer = get_optimizer(new_lr, model)\n    \n    train_loader, valid_loader = loaders(epoch)\n\n    # Train\n    train_loss = train_epoch(model, train_loader, optimizer)\n    optimizer.zero_grad()\n    \n    # Validate\n    valid_loss, pos_loss, neg_loss, pos_mean, neg_mean = valid_epoch(model, valid_loader)\n    \n    normalized_train_loss = normalize_loss(np.mean(train_loss))\n    normalized_valid_loss = normalize_loss(valid_loss)\n    \n    content = time.ctime() + f' Epoch {epoch}, lr: {optimizer.param_groups[0][\"lr\"]:.7f}, train loss: {normalized_train_loss:.5f}, valid loss: {normalized_valid_loss:.5f}'\n    print(content)\n    content = f'pos loss: {(pos_loss):.5f}, neg loss: {(neg_loss):.5f}, pos mean: {(pos_mean):.5f}, neg mean {(neg_mean):.5f}'\n    print(content)\n    master_train_loss.append(normalized_train_loss)\n    master_valid_loss.append(normalized_valid_loss)\n    \n    # Save loss data and model weights\n    with open('train_loss.pkl', 'wb') as f:\n        pickle.dump(master_train_loss, f)\n    with open('valid_loss.pkl', 'wb') as f:\n        pickle.dump(master_valid_loss, f)\n    \n    # Save best model\n    if normalized_valid_loss < best_valid_loss:\n        torch.save(model.state_dict(), '/kaggle/working/best_model_1.pth')\n        best_valid_loss = normalized_valid_loss\n        \n    # Plot metrics\n    plt.figure(figsize=(10, 5))\n    plt.plot(master_train_loss, label='Train Loss')\n    plt.plot(master_valid_loss, label='Valid Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Normalized Loss (0-1)')\n    plt.title('Train and Valid Loss')\n    plt.legend()\n    plt.savefig('/kaggle/working/loss_plot_1.png')\n    plt.close()\n    \n    epoch += 1\n\"\"\"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import os\nimport time\nimport pickle\nimport numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\n# Assuming you have defined n_epochs, schedule, get_optimizer, get_loaders, train_epoch, valid_epoch, master_train_loss, master_valid_loss\n\n# Initialize variables\nbest_valid_loss = float('inf')\nepoch = 1\n\ndef normalize_loss(loss):\n    min_loss = np.min(loss)\n    max_loss = np.max(loss)\n    normalized_loss = (loss - min_loss) / (max_loss - min_loss)\n    return normalized_loss\n\n# Create lists to store loss values for each epoch\nepoch_train_loss = []\nepoch_valid_loss = []\n\nwhile epoch <= n_epochs:\n    print(time.ctime(), 'Epoch:', epoch)\n    \n    # Update learning rate \n    if epoch in schedule:\n        new_lr = optimizer.param_groups[0][\"lr\"] / gamma\n        optimizer = get_optimizer(new_lr, model)\n    \n    train_loader, valid_loader = loaders(epoch)\n\n    # Train\n    train_loss = train_epoch(model, train_loader, optimizer)\n    optimizer.zero_grad()\n    \n    # Validate\n    valid_loss, pos_loss, neg_loss, pos_mean, neg_mean = valid_epoch(model, valid_loader)\n    \n    normalized_train_loss = normalize_loss(np.mean(train_loss))\n    normalized_valid_loss = normalize_loss(valid_loss)\n    \n    content = time.ctime() + f' Epoch {epoch}, lr: {optimizer.param_groups[0][\"lr\"]:.7f}, train loss: {normalized_train_loss:.5f}, valid loss: {normalized_valid_loss:.5f}'\n    print(content)\n    content = f'pos loss: {(pos_loss):.5f}, neg loss: {(neg_loss):.5f}, pos mean: {(pos_mean):.5f}, neg mean {(neg_mean):.5f}'\n    print(content)\n    epoch_train_loss.append(normalized_train_loss)\n    epoch_valid_loss.append(normalized_valid_loss)\n    \n    # Save loss data and model weights\n    with open('train_loss.pkl', 'wb') as f:\n        pickle.dump(epoch_train_loss, f)\n    with open('valid_loss.pkl', 'wb') as f:\n        pickle.dump(epoch_valid_loss, f)\n    \n    # Save best model\n    if normalized_valid_loss < best_valid_loss:\n        torch.save(model.state_dict(), '/kaggle/working/best_model_1.pth')\n        best_valid_loss = normalized_valid_loss\n        \n    # Plot metrics after each epoch\n    plt.figure(figsize=(10, 5))\n    plt.plot(epoch_train_loss, label='Train Loss')\n    plt.plot(epoch_valid_loss, label='Valid Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Normalized Loss (0-1)')\n    plt.title('Train and Valid Loss')\n    plt.legend()\n    plt.savefig(f'/kaggle/working/loss_plot_epoch_{epoch}.png')\n    plt.close()\n    \n    epoch += 1\n\n# Plot metrics for the entire training\nplt.figure(figsize=(10, 5))\nplt.plot(epoch_train_loss, label='Train Loss')\nplt.plot(epoch_valid_loss, label='Valid Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Normalized Loss (0-1)')\nplt.title('Train and Valid Loss Throughout Training')\nplt.legend()\nplt.savefig('/kaggle/working/loss_plot_whole_training.png')\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-25T04:55:38.921209Z","iopub.status.busy":"2023-08-25T04:55:38.919625Z","iopub.status.idle":"2023-08-25T05:05:55.219822Z","shell.execute_reply":"2023-08-25T05:05:55.218142Z","shell.execute_reply.started":"2023-08-25T04:55:38.921166Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Batch_size = 16\n\ninit_lr = 0.1\nn_epochs = 6\n#n_epochs = 1\ndevice = 'cuda'\n\n# reduce LR by gamma three times every 10 epochs\n# each epoch is 100,000 samples\ngamma = 10\n\nschedule = [10, 20, 30]\n\n#model = resnext101.to(device)\nmodel = cnn.to(device)\noptimizer = get_optimizer(init_lr, model)\n\nmaster_train_loss = []\nmaster_valid_loss = []\nepoch = 1\n#best_valid_loss = 10\n\nglobal_mean = 0.0\nglobal_std = 500.0\ntransform = T.Normalize(mean=[global_mean], std=[global_std])\n","metadata":{"execution":{"iopub.execute_input":"2023-08-25T05:09:59.209032Z","iopub.status.busy":"2023-08-25T05:09:59.20823Z","iopub.status.idle":"2023-08-25T05:09:59.232501Z","shell.execute_reply":"2023-08-25T05:09:59.231566Z","shell.execute_reply.started":"2023-08-25T05:09:59.208993Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_epoch(model, loader, optimizer):\n\n    model.train()\n    train_loss = []\n    bar = tqdm(loader)\n    for (data, target, _) in bar:\n        data, target = data.to(device), target.to(device)\n        \n        # zero gradients\n        optimizer.zero_grad()\n\n        #forward pass\n        logits = model(data.float())\n        \n        # calculate loss\n        loss = criterion(logits, target)\n\n        # backpropagate the loss (backward pass)\n        loss.backward()\n \n        # update parameters based on accumulated gradients\n        optimizer.step()\n\n        loss_np = loss.detach().cpu().numpy()\n\n        train_loss.append(loss_np)\n        average_loss = sum(train_loss) / len(train_loss)\n        bar.set_description('loss: %.5f, mean: %.5f' % (loss_np, average_loss))\n    return float(average_loss)\n\nimport torch\nfrom tqdm import tqdm\nfrom sklearn.metrics import precision_score, f1_score, accuracy_score\n\ndef valid_epoch(model, loader):\n    model.eval()\n    val_loss = []\n    pos_logits = []\n    neg_logits = []\n\n    # Compute predictions and true labels for all instances\n    all_predictions = []\n    all_labels = []\n    \n    with torch.no_grad():\n        for (data, target, _) in tqdm(loader):\n            data, target = data.to(device), target.to(device)\n            logits = model(data)\n\n            predictions = torch.argmax(logits, dim=1).cpu().numpy()\n            all_predictions.extend(predictions)\n            all_labels.extend(target.cpu().numpy())\n    \n    precision = precision_score(all_labels, all_predictions, average='weighted')\n    f1 = f1_score(all_labels, all_predictions, average='weighted')\n    accuracy = accuracy_score(all_labels, all_predictions)\n    \n    # Calculate the values for val_loss, pos_loss, neg_loss based on your requirements\n    # For example:\n    val_loss = compute_val_loss(all_labels, all_predictions)\n    pos_loss = compute_pos_loss(all_labels, all_predictions)\n    neg_loss = compute_neg_loss(all_labels, all_predictions)\n    \n    return float(val_loss), float(pos_loss), float(neg_loss), pos_mean, neg_mean, precision, f1, accuracy\n","metadata":{"execution":{"iopub.execute_input":"2023-08-25T11:40:28.347583Z","iopub.status.busy":"2023-08-25T11:40:28.347207Z","iopub.status.idle":"2023-08-25T11:40:28.362419Z","shell.execute_reply":"2023-08-25T11:40:28.361196Z","shell.execute_reply.started":"2023-08-25T11:40:28.347554Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import os\nimport time\nimport pickle\nimport numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom sklearn.metrics import precision_score, f1_score, accuracy_score, roc_auc_score\n\n\n# Assuming you have defined n_epochs, schedule, get_optimizer, get_loaders, train_epoch, valid_epoch, master_train_loss, master_valid_loss\n\n# Initialize variables\nbest_valid_loss = float('inf')\nepoch = 1\n\ndef normalize_loss(loss):\n    min_loss = np.min(loss)\n    max_loss = np.max(loss)\n    normalized_loss = (loss - min_loss) / (max_loss - min_loss)\n    return normalized_loss\n\n# Create lists to store loss values for each epoch\nepoch_train_loss = []\nepoch_valid_loss = []\nepoch_precision = []\nepoch_f1 = []\nepoch_accuracy = []\nepoch_roc_auc = []\n\nwhile epoch <= n_epochs:\n    print(time.ctime(), 'Epoch:', epoch)\n    \n    # Update learning rate \n    if epoch in schedule:\n        new_lr = optimizer.param_groups[0][\"lr\"] / gamma\n        optimizer = get_optimizer(new_lr, model)\n    \n    train_loader, valid_loader = loaders(epoch)\n\n    # Train\n    train_loss = train_epoch(model, train_loader, optimizer)\n    optimizer.zero_grad()\n    \n    # Validate\n    valid_loss, pos_loss, neg_loss, pos_mean, neg_mean, precision, f1, accuracy, roc_auc = valid_epoch(model, valid_loader)\n    \n    normalized_train_loss = normalize_loss(np.mean(train_loss))\n    normalized_valid_loss = normalize_loss(valid_loss)\n    \n    content = time.ctime() + f' Epoch {epoch}, lr: {optimizer.param_groups[0][\"lr\"]:.7f}, train loss: {normalized_train_loss:.5f}, valid loss: {normalized_valid_loss:.5f}'\n    print(content)\n    content = f'pos loss: {(pos_loss):.5f}, neg loss: {(neg_loss):.5f}, pos mean: {(pos_mean):.5f}, neg mean {(neg_mean):.5f}'\n    print(content)\n    epoch_train_loss.append(normalized_train_loss)\n    epoch_valid_loss.append(normalized_valid_loss)\n    epoch_precision.append(precision)\n    epoch_f1.append(f1)\n    epoch_accuracy.append(accuracy)\n    epoch_roc_auc.append(roc_auc)\n    \n    # Save loss data and model weights\n    with open('train_loss.pkl', 'wb') as f:\n        pickle.dump(epoch_train_loss, f)\n    with open('valid_loss.pkl', 'wb') as f:\n        pickle.dump(epoch_valid_loss, f)\n    \n    # Save best model\n    if normalized_valid_loss < best_valid_loss:\n        torch.save(model.state_dict(), '/kaggle/working/best_model_1.pth')\n        best_valid_loss = normalized_valid_loss\n        \n    # Plot metrics after each epoch\n    plt.figure(figsize=(10, 5))\n    plt.plot(epoch_train_loss, label='Train Loss')\n    plt.plot(epoch_valid_loss, label='Valid Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Normalized Loss (0-1)')\n    plt.title('Train and Valid Loss')\n    plt.legend()\n    plt.savefig(f'/kaggle/working/loss_plot_epoch_{epoch}.png')\n    plt.close()\n    \n    epoch += 1\n\n# Plot metrics for the entire training\nplt.figure(figsize=(10, 5))\nplt.plot(epoch_train_loss, label='Train Loss')\nplt.plot(epoch_valid_loss, label='Valid Loss')\nplt.plot(epoch_precision, label='Precision')\nplt.plot(epoch_f1, label='F1 Score')\nplt.plot(epoch_accuracy, label='Accuracy')\nplt.plot(epoch_roc_auc, label='ROC-AUC')\nplt.xlabel('Epoch')\nplt.ylabel('Normalized / Percentage Value')\nplt.title('Metrics Throughout Training')\nplt.legend()\nplt.savefig('/kaggle/working/metrics_plot_whole_training.png')\nplt.show()\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-25T05:57:36.568798Z","iopub.status.busy":"2023-08-25T05:57:36.568363Z","iopub.status.idle":"2023-08-25T06:50:14.528916Z","shell.execute_reply":"2023-08-25T06:50:14.527568Z","shell.execute_reply.started":"2023-08-25T05:57:36.568763Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport time\nimport pickle\nimport numpy as np\nimport torch\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom sklearn.metrics import precision_score, f1_score, accuracy_score\n\nBatch_size = 16\n\ninit_lr = 0.1\nn_epochs = 6\n#n_epochs = 1\ndevice = 'cuda'\n\n# reduce LR by gamma three times every 10 epochs\n# each epoch is 100,000 samples\ngamma = 10\n\nschedule = [10, 20, 30]\n\n#model = resnext101.to(device)\nmodel = cnn.to(device)\noptimizer = get_optimizer(init_lr, model)\n\nmaster_train_loss = []\nmaster_valid_loss = []\nepoch = 1\n#best_valid_loss = 10\n\nglobal_mean = 0.0\nglobal_std = 500.0\ntransform = T.Normalize(mean=[global_mean], std=[global_std])\n\n# Initialize variables\nbest_valid_loss = float('inf')\nepoch = 1\n\ndef normalize_loss(loss):\n    min_loss = np.min(loss)\n    max_loss = np.max(loss)\n    normalized_loss = (loss - min_loss) / (max_loss - min_loss)\n    return normalized_loss\n\n# Create lists to store loss values for each epoch\nepoch_train_loss = []\nepoch_valid_loss = []\nepoch_precision = []\nepoch_f1 = []\nepoch_accuracy = []\n\nwhile epoch <= n_epochs:\n    print(time.ctime(), 'Epoch:', epoch)\n    \n    # Update learning rate \n    if epoch in schedule:\n        new_lr = optimizer.param_groups[0][\"lr\"] / gamma\n        optimizer = get_optimizer(new_lr, model)\n    \n    train_loader, valid_loader = loaders(epoch)\n\n    # Train\n    train_loss = train_epoch(model, train_loader, optimizer)\n    optimizer.zero_grad()\n    \n    # Validate\n    valid_loss, pos_loss, neg_loss, pos_mean, neg_mean, precision, f1, accuracy = valid_epoch(model, valid_loader)\n    \n    normalized_train_loss = normalize_loss(np.mean(train_loss))\n    normalized_valid_loss = normalize_loss(valid_loss)\n    \n    content = time.ctime() + f' Epoch {epoch}, lr: {optimizer.param_groups[0][\"lr\"]:.7f}, train loss: {normalized_train_loss:.5f}, valid loss: {normalized_valid_loss:.5f}'\n    print(content)\n    content = f'pos loss: {(pos_loss):.5f}, neg loss: {(neg_loss):.5f}, pos mean: {(pos_mean):.5f}, neg mean {(neg_mean):.5f}'\n    print(content)\n    epoch_train_loss.append(normalized_train_loss)\n    epoch_valid_loss.append(normalized_valid_loss)\n    epoch_precision.append(precision)\n    epoch_f1.append(f1)\n    epoch_accuracy.append(accuracy)\n    \n    # Save loss data and model weights\n    with open('train_loss.pkl', 'wb') as f:\n        pickle.dump(epoch_train_loss, f)\n    with open('valid_loss.pkl', 'wb') as f:\n        pickle.dump(epoch_valid_loss, f)\n    \n    # Save best model\n    if normalized_valid_loss < best_valid_loss:\n        torch.save(model.state_dict(), '/kaggle/working/best_model_1.pth')\n        best_valid_loss = normalized_valid_loss\n        \n    # Plot metrics after each epoch\n    plt.figure(figsize=(10, 5))\n    plt.plot(epoch_train_loss, label='Train Loss')\n    plt.plot(epoch_valid_loss, label='Valid Loss')\n    plt.plot(epoch_precision, label='Precision')\n    plt.plot(epoch_f1, label='F1 Score')\n    plt.plot(epoch_accuracy, label='Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Normalized / Percentage Value')\n    plt.title('Metrics Throughout Training')\n    plt.legend()\n    plt.savefig(f'/kaggle/working/metrics_plot_epoch_{epoch}.png')\n    plt.close()\n    \n    epoch += 1\n\n# Plot metrics for the entire training\nplt.figure(figsize=(10, 5))\nplt.plot(epoch_train_loss, label='Train Loss')\nplt.plot(epoch_valid_loss, label='Valid Loss')\nplt.plot(epoch_precision, label='Precision')\nplt.plot(epoch_f1, label='F1 Score')\nplt.plot(epoch_accuracy, label='Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Normalized / Percentage Value')\nplt.title('Metrics Throughout Training')\nplt.legend()\nplt.savefig('/kaggle/working/metrics_plot_whole_training.png')\nplt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-25T11:40:37.3191Z","iopub.status.busy":"2023-08-25T11:40:37.318698Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# With Lung segmentation","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]}},{"cell_type":"code","source":"! pip install dicom2nifti","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:49:33.194606Z","iopub.execute_input":"2023-08-27T07:49:33.194959Z","iopub.status.idle":"2023-08-27T07:49:47.128616Z","shell.execute_reply.started":"2023-08-27T07:49:33.194931Z","shell.execute_reply":"2023-08-27T07:49:47.127449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import dicom2nifti\n\npath_to_dcm = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/00511e94edec/297f170f1197/'\npath_to_nii = '/kaggle/working/'\n\ndicom2nifti.convert_directory(path_to_dcm,path_to_nii)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:50:03.967387Z","iopub.execute_input":"2023-08-27T07:50:03.967768Z","iopub.status.idle":"2023-08-27T07:50:14.74203Z","shell.execute_reply.started":"2023-08-27T07:50:03.967735Z","shell.execute_reply":"2023-08-27T07:50:14.740997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install nibabel matplotlib\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:50:21.480881Z","iopub.execute_input":"2023-08-27T07:50:21.481244Z","iopub.status.idle":"2023-08-27T07:50:32.870185Z","shell.execute_reply.started":"2023-08-27T07:50:21.481214Z","shell.execute_reply":"2023-08-27T07:50:32.869046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ndef rename_kaggle_file(old_name, new_name):\n    \"\"\"\n    Rename a file on Kaggle.\n\n    Args:\n        old_name (str): Current name of the file.\n        new_name (str): New name to assign to the file.\n\n    Returns:\n        str: A message indicating success or failure.\n    \"\"\"\n    try:\n        os.rename(old_name, new_name)\n        return f\"File '{old_name}' has been renamed to '{new_name}' successfully.\"\n    except Exception as e:\n        return f\"An error occurred: {str(e)}\"\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:51:03.80061Z","iopub.execute_input":"2023-08-27T07:51:03.800957Z","iopub.status.idle":"2023-08-27T07:51:03.807197Z","shell.execute_reply.started":"2023-08-27T07:51:03.800929Z","shell.execute_reply":"2023-08-27T07:51:03.806293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nold_file_name = '/kaggle/working/2.nii.gz'\nnew_file_name = '/kaggle/working/second.nii.gz'\nresult = rename_kaggle_file(old_file_name, new_file_name)\nprint(result)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:51:27.421975Z","iopub.execute_input":"2023-08-27T07:51:27.422351Z","iopub.status.idle":"2023-08-27T07:51:27.42871Z","shell.execute_reply.started":"2023-08-27T07:51:27.422316Z","shell.execute_reply":"2023-08-27T07:51:27.427499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\nimport matplotlib.pyplot as plt\n\ndef plot_nii_file(nii_file_path):\n    try:\n        # Load NIfTI file\n        nii_img = nib.load(nii_file_path)\n        \n        # Get the NIfTI data array\n        nii_data = nii_img.get_fdata()\n        \n        # Plot the NIfTI data\n        plt.figure(figsize=(8, 8))\n        plt.imshow(nii_data[:, :, nii_data.shape[2] // 2], cmap='gray', origin='lower')\n        plt.title('NIfTI Image')\n        plt.colorbar()\n        plt.show()\n        \n    except Exception as e:\n        print(\"An error occurred:\", e)\n\n# Provide the path to your NIfTI file here\nnii_file_path = \"/kaggle/working/2.nii.gz\"\n\nnii_file_path = \"/kaggle/working/second.nii.gz\"\n\n#nii_file_path = \"/kaggle/working/out/second/second_seg.nii.gz\"\n\n\nplot_nii_file(nii_file_path)\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:51:30.06951Z","iopub.execute_input":"2023-08-27T07:51:30.069894Z","iopub.status.idle":"2023-08-27T07:51:31.697106Z","shell.execute_reply.started":"2023-08-27T07:51:30.069861Z","shell.execute_reply":"2023-08-27T07:51:31.696189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T17:22:55.169223Z","iopub.status.busy":"2023-08-26T17:22:55.168838Z","iopub.status.idle":"2023-08-26T17:22:55.175038Z","shell.execute_reply":"2023-08-26T17:22:55.174017Z","shell.execute_reply.started":"2023-08-26T17:22:55.169192Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install monai","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:52:22.267019Z","iopub.execute_input":"2023-08-27T07:52:22.267514Z","iopub.status.idle":"2023-08-27T07:52:34.82536Z","shell.execute_reply.started":"2023-08-27T07:52:22.267462Z","shell.execute_reply":"2023-08-27T07:52:34.823995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfrom monai.transforms import (\n    EnsureChannelFirstd,\n    LoadImage,\n    LoadImaged,\n    Orientationd,\n    Rand3DElasticd,\n    RandAffined,\n    Spacingd,\n)\nfrom monai.config import print_config\nfrom monai.apps import download_and_extract\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tempfile\nimport shutil\nimport os\nimport glob\n\nprint_config()\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:52:34.82788Z","iopub.execute_input":"2023-08-27T07:52:34.828259Z","iopub.status.idle":"2023-08-27T07:52:47.527677Z","shell.execute_reply.started":"2023-08-27T07:52:34.828219Z","shell.execute_reply":"2023-08-27T07:52:47.526902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n\n\nfrom __future__ import print_function\n\nimport os\nimport glob\nimport numpy as np\nfrom skimage.transform import resize\nfrom skimage.io import imsave\n\nfrom skimage.io import imread\nimport pandas as pd\n\n\nimport numpy as np\nnp.random.seed(256)\n\nimport tensorflow as tf\ntf.random.set_seed(256)\n\n#from tensorflow.keras.models import Model\nimport nibabel as nib\n\n\n\nimport os\nimport shutil\nimport tempfile\nimport time\n\n\nimport os\nimport re\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nfrom tifffile import imsave\nfrom PIL import Image\nfrom tqdm import tqdm\nfrom glob import glob\nimport tensorflow as tf\nimport tensorflow.keras\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\nimport os\nimport glob\nimport nibabel as nib\nimport pandas as pd\nimport numpy as np\nimport numpy as np\nfrom monai.transforms import SaveImage\n\n!python -c \"import monai\" || pip install -q \"monai-weekly[nibabel, tqdm]\"\n!python -c \"import matplotlib\" || pip install -q matplotlib\n%matplotlib inline\n\n\n\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:52:50.841319Z","iopub.execute_input":"2023-08-27T07:52:50.842164Z","iopub.status.idle":"2023-08-27T07:53:31.647244Z","shell.execute_reply.started":"2023-08-27T07:52:50.842123Z","shell.execute_reply":"2023-08-27T07:53:31.646041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfrom pathlib import Path # pathlib for easy path handling\nimport pydicom # pydicom to handle dicom files\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport dicom2nifti # to convert DICOM files to the NIftI format\nimport nibabel as nib # nibabel to handle nifti files\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:53:56.921693Z","iopub.execute_input":"2023-08-27T07:53:56.922154Z","iopub.status.idle":"2023-08-27T07:53:56.930744Z","shell.execute_reply.started":"2023-08-27T07:53:56.922105Z","shell.execute_reply":"2023-08-27T07:53:56.929567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_im = '/kaggle/working/'\n#data_dir_lb = '/kaggle/working/*/'\nimages = sorted(\n    glob.glob(os.path.join(data_dir_im, \"*.nii.gz\")))\n\nprint('Nbr. of images:', len(images))\n\n\"\"\"\nlabels = sorted(\n    glob.glob(os.path.join(data_dir_lb, \"*.nii.gz\")))\n\nprint('Nbr. of images:', len(images), ' labels:', len(labels))\n\n\"\"\"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:53:59.617232Z","iopub.execute_input":"2023-08-27T07:53:59.61764Z","iopub.status.idle":"2023-08-27T07:53:59.628501Z","shell.execute_reply.started":"2023-08-27T07:53:59.617609Z","shell.execute_reply":"2023-08-27T07:53:59.627515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:54:02.081498Z","iopub.execute_input":"2023-08-27T07:54:02.081874Z","iopub.status.idle":"2023-08-27T07:54:02.088573Z","shell.execute_reply.started":"2023-08-27T07:54:02.081847Z","shell.execute_reply":"2023-08-27T07:54:02.087571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = nib.load(images[0]).get_fdata()\n#lbl = nib.load(labels[3]).get_fdata()\nprint('img shape:', img.shape)\nprint('img intensity min.:', np.min(img), ' max.:', np.max(img))\n\nslice_idx = 157\nplt.figure(figsize=(8,5))\nplt.subplot(1,2,1)\nplt.imshow(img[:,:,slice_idx], cmap='gray', vmin=-1000, vmax=1000)\n\n\n\nplt.tight_layout()\nplt.show()","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:54:03.723087Z","iopub.execute_input":"2023-08-27T07:54:03.723502Z","iopub.status.idle":"2023-08-27T07:54:05.26805Z","shell.execute_reply.started":"2023-08-27T07:54:03.723472Z","shell.execute_reply":"2023-08-27T07:54:05.267203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from monai.transforms import (\n    AsDiscrete,\n    AsDiscreted,\n    EnsureChannelFirstd,\n    Compose,\n    CropForegroundd,\n    LoadImaged,\n    Orientationd,\n    RandAffined,\n    RandFlipd,\n    RandScaleIntensityd,\n    RandShiftIntensityd,\n    RandSpatialCropd,\n    SaveImaged,\n    ScaleIntensityRanged,\n    Spacingd,\n    Invertd,\n)\nfrom monai.data import CacheDataset, DataLoader, Dataset, decollate_batch\n\ntrain_transforms = Compose(\n    [\n        LoadImaged(keys=[\"image\", \"label\"]),\n        EnsureChannelFirstd(keys=[\"image\", \"label\"]),\n        ScaleIntensityRanged(\n            keys=[\"image\"], a_min=-1000, a_max=1000,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        \n        # Change labels from 255 to 1\n        ScaleIntensityRanged(\n            keys=[\"label\"], a_min=0, a_max=255,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n        Orientationd(keys=[\"image\", \"label\"], axcodes=\"PLS\"),\n        Spacingd(keys=[\"image\", \"label\"], pixdim=(1.5, 1.5, 2.0), mode=(\"bilinear\", \"nearest\")),\n        \n        # Randomly crop a patch from both image and label files\n        RandSpatialCropd(\n                keys=[\"image\", \"label\"],\n                roi_size=[96, 96, 96],\n                random_size=False,\n        ),\n     \n        # Data augmentation transforms\n        RandAffined(\n            keys=['image', 'label'],\n            mode=('bilinear', 'nearest'),\n            prob=1.0, spatial_size=(96, 96, 96),\n            rotate_range=(0, 0, np.pi/15),\n            scale_range=(0.1, 0.1, 0.1)),\n        RandFlipd(keys=[\"image\", \"label\"], prob=0.25, spatial_axis=0),\n        RandFlipd(keys=[\"image\", \"label\"], prob=0.25, spatial_axis=1),\n        RandFlipd(keys=[\"image\", \"label\"], prob=0.25, spatial_axis=2),\n        RandScaleIntensityd(keys=\"image\", factors=0.1, prob=1.0),\n        RandShiftIntensityd(keys=\"image\", offsets=0.1, prob=1.0),\n        ])\n\nval_transforms = Compose(\n    [\n        LoadImaged(keys=[\"image\", \"label\"]),\n        EnsureChannelFirstd(keys=[\"image\", \"label\"]),\n        ScaleIntensityRanged(\n            keys=[\"image\"], a_min=-1000, a_max=1000,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        ScaleIntensityRanged(\n            keys=[\"label\"], a_min=0, a_max=255,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n        Orientationd(keys=[\"image\", \"label\"], axcodes=\"PLS\"),\n        Spacingd(keys=[\"image\", \"label\"], pixdim=(1.5, 1.5, 2.0), mode=(\"bilinear\", \"nearest\")),\n    ])\n     \"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T13:52:34.374106Z","iopub.status.busy":"2023-08-26T13:52:34.373669Z","iopub.status.idle":"2023-08-26T13:52:34.412227Z","shell.execute_reply":"2023-08-26T13:52:34.411255Z","shell.execute_reply.started":"2023-08-26T13:52:34.374077Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from monai.transforms import (\n    AsDiscrete,\n    AsDiscreted,\n    EnsureChannelFirstd,\n    Compose,\n    CropForegroundd,\n    LoadImaged,\n    Orientationd,\n    RandAffined,\n    RandFlipd,\n    RandScaleIntensityd,\n    RandShiftIntensityd,\n    RandSpatialCropd,\n    SaveImaged,\n    ScaleIntensityRanged,\n    Spacingd,\n    Invertd,\n)\nfrom monai.data import CacheDataset, DataLoader, Dataset, decollate_batch\n\ntrain_transforms = Compose(\n    [\n        LoadImaged(keys=[\"image\", \"label\"]),\n        EnsureChannelFirstd(keys=[\"image\", \"label\"]),\n        ScaleIntensityRanged(\n            keys=[\"image\"], a_min=-1000, a_max=1000,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        \n        # Change labels from 255 to 1\n        ScaleIntensityRanged(\n            keys=[\"label\"], a_min=0, a_max=255,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n        Orientationd(keys=[\"image\", \"label\"], axcodes=\"PLS\"),\n        Spacingd(keys=[\"image\", \"label\"], pixdim=(1.5, 1.5, 2.0), mode=(\"bilinear\", \"nearest\")),\n        \n        # Randomly crop a patch from both image and label files\n        RandSpatialCropd(\n                keys=[\"image\", \"label\"],\n                roi_size=[96, 96, 96],\n                random_size=False,\n        ),\n     \n        # Data augmentation transforms\n        RandAffined(\n            keys=['image', 'label'],\n            mode=('bilinear', 'nearest'),\n            prob=1.0, spatial_size=(96, 96, 96),\n            rotate_range=(0, 0, np.pi/15),\n            scale_range=(0.1, 0.1, 0.1)),\n        RandFlipd(keys=[\"image\", \"label\"], prob=0.25, spatial_axis=0),\n        RandFlipd(keys=[\"image\", \"label\"], prob=0.25, spatial_axis=1),\n        RandFlipd(keys=[\"image\", \"label\"], prob=0.25, spatial_axis=2),\n        RandScaleIntensityd(keys=\"image\", factors=0.1, prob=1.0),\n        RandShiftIntensityd(keys=\"image\", offsets=0.1, prob=1.0),\n        ])\n\nval_transforms = Compose(\n    [\n        LoadImaged(keys=[\"image\", \"label\"]),\n        EnsureChannelFirstd(keys=[\"image\", \"label\"]),\n        ScaleIntensityRanged(\n            keys=[\"image\"], a_min=-1000, a_max=1000,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        ScaleIntensityRanged(\n            keys=[\"label\"], a_min=0, a_max=255,\n            b_min=0.0, b_max=1.0, clip=True,\n        ),\n        CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n        Orientationd(keys=[\"image\", \"label\"], axcodes=\"PLS\"),\n        Spacingd(keys=[\"image\", \"label\"], pixdim=(1.5, 1.5, 2.0), mode=(\"bilinear\", \"nearest\")),\n    ])\n     ","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:54:10.396784Z","iopub.execute_input":"2023-08-27T07:54:10.397138Z","iopub.status.idle":"2023-08-27T07:54:10.48385Z","shell.execute_reply.started":"2023-08-27T07:54:10.397108Z","shell.execute_reply":"2023-08-27T07:54:10.477876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_im = '/kaggle/working/'\ndata_dir_lb = '/kaggle/working/'\nimages = sorted(\n    glob.glob(os.path.join(data_dir_im, \"*.nii.gz\")))\nlabels = sorted(\n    glob.glob(os.path.join(data_dir_lb, \"*.nii.gz\")))\n\nprint('Nbr. of images:', len(images), ' labels:', len(labels))\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:54:15.061252Z","iopub.execute_input":"2023-08-27T07:54:15.061677Z","iopub.status.idle":"2023-08-27T07:54:15.069049Z","shell.execute_reply.started":"2023-08-27T07:54:15.061646Z","shell.execute_reply":"2023-08-27T07:54:15.067911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_im = '/kaggle/working/'\ndata_dir_lb = '/kaggle/working/'\nimages = sorted(\n    glob.glob(os.path.join(data_dir_im, \"*.nii.gz\")))\nlabels = sorted(\n    glob.glob(os.path.join(data_dir_lb, \"*.nii.gz\")))\n\ndata_dicts = [\n    {\"image\": image_name, \"label\": label_name}\n    for image_name, label_name in zip(images, labels)\n]\n\ntrain_files, val_files, test_files = data_dicts[:1], data_dicts[:1], data_dicts[:1]\n\nprint('train files:', len(train_files), ' val files:', len(val_files), ' test files:', len(test_files))\n\n     \n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:54:17.66104Z","iopub.execute_input":"2023-08-27T07:54:17.661428Z","iopub.status.idle":"2023-08-27T07:54:17.668679Z","shell.execute_reply.started":"2023-08-27T07:54:17.661398Z","shell.execute_reply":"2023-08-27T07:54:17.667667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n\ndata_dicts = [\n    {\"image\": image_name}\n    for image_name in zip(images)\n]\n\ntest_files = data_dicts\n\nprint(' test files:', len(test_files))\n\n     \n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T13:52:38.047344Z","iopub.status.busy":"2023-08-26T13:52:38.046306Z","iopub.status.idle":"2023-08-26T13:52:38.054019Z","shell.execute_reply":"2023-08-26T13:52:38.052924Z","shell.execute_reply.started":"2023-08-26T13:52:38.047311Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import DataLoader\nfrom monai.metrics import DiceMetric\nfrom monai.networks.nets import UNet\nfrom monai.networks.layers import Norm\nfrom monai.inferers import sliding_window_inference\n\n\n\nfrom monai.transforms import Compose, AsDiscrete\nfrom monai.data import Dataset, DataLoader\nfrom monai.inferers import sliding_window_inference\nfrom monai.losses import DiceLoss\nfrom monai.metrics import DiceMetric\nfrom monai.networks.nets import UNet\nfrom monai.networks.layers import Norm\nfrom monai.utils import set_determinism\nimport torch\nimport matplotlib.pyplot as plt\n\nset_determinism(seed=0)\n\ndevice = torch.device(\"cuda:0\")\n\nmodel = UNet(\n    spatial_dims=3,\n    in_channels=1,\n    out_channels=2,\n    channels=(16, 32, 64, 128, 256),\n    strides=(2, 2, 2, 2),\n    num_res_units=2,\n    norm=Norm.BATCH,\n).to(device)\n\nloss_function = DiceLoss(to_onehot_y=True, softmax=True)\noptimizer = torch.optim.Adam(model.parameters(), 1e-3)\ndice_metric = DiceMetric(include_background=False, reduction=\"mean\")","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:54:21.442079Z","iopub.execute_input":"2023-08-27T07:54:21.442589Z","iopub.status.idle":"2023-08-27T07:54:27.692711Z","shell.execute_reply.started":"2023-08-27T07:54:21.442544Z","shell.execute_reply":"2023-08-27T07:54:27.691467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_im = '/kaggle/working/'\ndata_dir_lb = '/kaggle/working/'\nimages = sorted(\n    glob.glob(os.path.join(data_dir_im, \"*.nii.gz\")))\nlabels = sorted(\n    glob.glob(os.path.join(data_dir_lb, \"*.nii.gz\")))\n\ndata_dicts = [\n    {\"image\": image_name, \"label\": label_name}\n    for image_name, label_name in zip(images, labels)\n]\n\ntrain_files, val_files, test_files = data_dicts[:1], data_dicts[:1], data_dicts[:1]\n\n\ntest_ds = Dataset(data=test_files, transform=val_transforms)\ntest_loader = DataLoader(test_ds, batch_size=1)\n\npost_transforms = Compose([\n    Invertd(\n        keys=\"pred\",\n        transform=val_transforms,\n        orig_keys=\"image\",\n        meta_keys=\"pred_meta_dict\",\n        orig_meta_keys=\"image_meta_dict\",\n        meta_key_postfix=\"meta_dict\",\n        nearest_interp=False,\n        to_tensor=True,\n    ),\n    AsDiscreted(keys=\"pred\", argmax=True, to_onehot=2),\n    SaveImaged(keys=\"pred\", meta_keys=\"pred_meta_dict\", output_dir=\"./out\", output_postfix=\"seg\", resample=False),\n])","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:54:27.696411Z","iopub.execute_input":"2023-08-27T07:54:27.697242Z","iopub.status.idle":"2023-08-27T07:54:27.70742Z","shell.execute_reply.started":"2023-08-27T07:54:27.697214Z","shell.execute_reply":"2023-08-27T07:54:27.706406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loader","metadata":{"execution":{"iopub.execute_input":"2023-08-26T17:24:58.714127Z","iopub.status.busy":"2023-08-26T17:24:58.713541Z","iopub.status.idle":"2023-08-26T17:24:58.720809Z","shell.execute_reply":"2023-08-26T17:24:58.719739Z","shell.execute_reply.started":"2023-08-26T17:24:58.714093Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T14:40:33.317269Z","iopub.status.busy":"2023-08-26T14:40:33.316864Z","iopub.status.idle":"2023-08-26T14:40:33.328323Z","shell.execute_reply":"2023-08-26T14:40:33.327298Z","shell.execute_reply.started":"2023-08-26T14:40:33.317231Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 57\n\n        plt.figure(figsize=(20,7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(test_output[0].detach().cpu().numpy()[1,:,:,slice_idx], cmap=\"gray\") # use index 1 to get the lung class\n        plt.tight_layout()\n        plt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T14:30:02.499081Z","iopub.status.busy":"2023-08-26T14:30:02.498693Z","iopub.status.idle":"2023-08-26T14:30:46.174566Z","shell.execute_reply":"2023-08-26T14:30:46.173655Z","shell.execute_reply.started":"2023-08-26T14:30:02.49905Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 57\n\n        plt.figure(figsize=(20,7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(test_output[0].detach().cpu().numpy()[1,:,:,slice_idx], cmap=\"gray\") # use index 1 to get the lung class\n        plt.tight_layout()\n        plt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T15:22:43.666581Z","iopub.status.busy":"2023-08-26T15:22:43.666135Z","iopub.status.idle":"2023-08-26T15:23:31.379503Z","shell.execute_reply":"2023-08-26T15:23:31.378458Z","shell.execute_reply.started":"2023-08-26T15:22:43.666545Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\nimport numpy as np\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 57\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Define a margin size (in pixels) for the border to keep\n        margin = 10\n\n        # Create a mask to remove the border regions\n        border_removed_mask = lung_mask.copy()\n        border_removed_mask[:margin, :] = 0\n        border_removed_mask[-margin:, :] = 0\n        border_removed_mask[:, :margin] = 0\n        border_removed_mask[:, -margin:] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (border removed)')\n        plt.axis('off')\n        plt.imshow(border_removed_mask, cmap=\"gray\")  # Display the border-removed mask\n\n        plt.tight_layout()\n        plt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T15:31:11.131887Z","iopub.status.busy":"2023-08-26T15:31:11.131446Z","iopub.status.idle":"2023-08-26T15:31:57.518658Z","shell.execute_reply":"2023-08-26T15:31:57.517588Z","shell.execute_reply.started":"2023-08-26T15:31:11.131856Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from matplotlib.patches import Circle\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 157\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Define the center of the circle and the radius\n        center_x = lung_mask.shape[0] // 2\n        center_y = lung_mask.shape[1] // 2\n        circle_radius = 1000  # You can adjust this radius\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n        # Create a circular mask\n        circular_mask = ((x - center_x)**2 + (y - center_y)**2) <= circle_radius**2\n\n        # Apply the circular mask to the border_removed_mask\n        border_removed_mask = lung_mask.copy()\n        border_removed_mask[~circular_mask] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (border removed)')\n        plt.axis('off')\n        plt.imshow(border_removed_mask, cmap=\"gray\")  # Display the border-removed mask\n\n        plt.tight_layout()\n        plt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T16:09:33.66423Z","iopub.status.busy":"2023-08-26T16:09:33.663844Z","iopub.status.idle":"2023-08-26T16:10:21.623331Z","shell.execute_reply":"2023-08-26T16:10:21.622464Z","shell.execute_reply.started":"2023-08-26T16:09:33.6642Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"with torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 157\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Define the center of the circle and the radius\n        center_x = lung_mask.shape[0] // 4  # Using the left half\n        center_y = lung_mask.shape[1] // 2\n        circle_radius = 190  # You can adjust this radius\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n        # Create a circular mask that only covers the left half\n        circular_mask = (((x - center_x) / circle_radius)**2 + ((y - center_y) / circle_radius)**2) <= 1\n\n        # Apply the circular mask to the border_removed_mask\n        border_removed_mask = lung_mask.copy()\n        border_removed_mask[~circular_mask & (x >= center_x)] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (border removed)')\n        plt.axis('off')\n        plt.imshow(border_removed_mask, cmap=\"gray\")  # Display the border-removed mask\n\n        plt.tight_layout()\n        plt.show()\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T16:13:13.140348Z","iopub.status.busy":"2023-08-26T16:13:13.139925Z","iopub.status.idle":"2023-08-26T16:14:00.594523Z","shell.execute_reply":"2023-08-26T16:14:00.59354Z","shell.execute_reply.started":"2023-08-26T16:13:13.140317Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from matplotlib.patches import Circle\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 157\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Define the center of the circle and the radius\n        center_x = lung_mask.shape[0] // 2\n        center_y = lung_mask.shape[1] // 2\n        circle_radius = 197  # You can adjust this radius\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n        # Create a circular mask\n        circular_mask = ((x - center_x)**2 + (y - center_y)**2) <= circle_radius**2\n\n        # Apply the circular mask to the predicted lung mask\n        lung_mask[~circular_mask] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (circle region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")  # Display the circular region of the mask\n\n        plt.tight_layout()\n        plt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T16:22:10.264104Z","iopub.status.busy":"2023-08-26T16:22:10.263702Z","iopub.status.idle":"2023-08-26T16:22:56.87478Z","shell.execute_reply":"2023-08-26T16:22:56.873866Z","shell.execute_reply.started":"2023-08-26T16:22:10.264075Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from matplotlib.patches import Circle\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 57\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        # Draw the circle on the original image\n        circle = Circle((center_y, center_x), radius=circle_radius, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(circle)\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n        # Create a circular mask\n        circular_mask = ((x - center_x)**2 + (y - center_y)**2) <= circle_radius**2\n\n        # Apply the circular mask to the predicted lung mask\n        lung_mask[~circular_mask] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (circle region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")  # Display the circular region of the mask\n\n        plt.tight_layout()\n        plt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T16:26:39.399985Z","iopub.status.busy":"2023-08-26T16:26:39.399551Z","iopub.status.idle":"2023-08-26T16:27:26.320646Z","shell.execute_reply":"2023-08-26T16:27:26.319609Z","shell.execute_reply.started":"2023-08-26T16:26:39.399956Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n# Good\n\nfrom matplotlib.patches import Ellipse\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 57\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        # Draw the ellipse on the original image\n        ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(ellipse)\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n        # Create an ellipse mask\n        ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n\n        # Apply the ellipse mask to the predicted lung mask\n        lung_mask[~ellipse_mask] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")  # Display the ellipse region of the mask\n\n        plt.tight_layout()\n        plt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T16:32:12.319355Z","iopub.status.busy":"2023-08-26T16:32:12.318914Z","iopub.status.idle":"2023-08-26T16:32:59.978504Z","shell.execute_reply":"2023-08-26T16:32:59.977541Z","shell.execute_reply.started":"2023-08-26T16:32:12.319324Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n\n# Get the predicted lung mask\nlung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n# Create a meshgrid of coordinates\nx, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n\nellipse_radius_x = 175\nellipse_radius_y = 370\n\n# Draw the ellipse on the original image\nellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\nplt.figure(figsize=(20, 7))\nplt.subplot(1, 3, 1)\nplt.axis('off')\nplt.title(f'test image {idx+1}, slice {slice_idx}')\nplt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\nplt.gca().add_patch(ellipse)\n\nplt.subplot(1, 3, 2)\nplt.title(f'reference label {idx+1}, slice {slice_idx}')\nplt.axis('off')\nplt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n# Get the predicted lung mask\nlung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\nplt.subplot(1, 3, 3)\nplt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\nplt.axis('off')\nplt.imshow(lung_mask, cmap=\"gray\")  # Display the lung mask\nplt.tight_layout()\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T16:51:17.811272Z","iopub.status.busy":"2023-08-26T16:51:17.81087Z","iopub.status.idle":"2023-08-26T16:51:19.304205Z","shell.execute_reply":"2023-08-26T16:51:19.303281Z","shell.execute_reply.started":"2023-08-26T16:51:17.811238Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\nimport numpy as np\n\nfrom matplotlib.patches import Ellipse\n\n\n\nfrom monai.transforms import LoadImage\nfrom monai.handlers.utils import from_engine\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 157\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        # Calculate the center coordinates\n        center_y = original_image.shape[0] // 2\n        center_x = original_image.shape[1] // 2\n\n        \n        # Draw the ellipse on the original image\n        ellipse_radius_x = 175\n        ellipse_radius_y = 370\n        ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(ellipse)\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n        # Create an ellipse mask with user-defined characteristics\n        ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n\n        # Set values outside the ellipse mask to 0 in the lung mask\n        lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")  # Display the lung mask with left outside part removed\n\n        plt.tight_layout()\n        plt.show()\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2023-08-27T07:55:40.257575Z","iopub.execute_input":"2023-08-27T07:55:40.25812Z","iopub.status.idle":"2023-08-27T07:56:33.378159Z","shell.execute_reply.started":"2023-08-27T07:55:40.258072Z","shell.execute_reply":"2023-08-27T07:56:33.377335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import dicom2nifti\nimport os\nimport shutil\n\ndef convert_and_rename_dicom_to_nifti(source_dir, target_dir, new_nifti_name):\n    # Convert DICOM files to NIfTI format\n    dicom2nifti.convert_directory(source_dir, target_dir)\n    \n    # Get the path to the generated NIfTI file\n    nifti_file_path = os.path.join(target_dir, '2.nii.gz')\n    \n    if os.path.exists(nifti_file_path):\n        # Rename the NIfTI file\n        new_nifti_file_path = os.path.join(target_dir, new_nifti_name)\n        shutil.move(nifti_file_path, new_nifti_file_path)\n        \n        return new_nifti_file_path\n    else:\n        return None\n\n# Example usage\npath_to_dcm = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/00511e94edec/297f170f1197/'\npath_to_nii = '/kaggle/working/'\nnew_nifti_name = 'New.nii.gz'\n\nresult = convert_and_rename_dicom_to_nifti(path_to_dcm, path_to_nii, new_nifti_name)\n\nif result:\n    print(f\"Conversion and renaming successful. New NIfTI file: {result}\")\nelse:\n    print(\"Conversion and renaming failed.\")\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T08:07:57.906847Z","iopub.execute_input":"2023-08-27T08:07:57.907261Z","iopub.status.idle":"2023-08-27T08:08:06.989692Z","shell.execute_reply.started":"2023-08-27T08:07:57.907216Z","shell.execute_reply":"2023-08-27T08:08:06.988523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\npath_to_dcm = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/00511e94edec/297f170f1197/'\npath_to_nii = '/kaggle/working/'\nnew_nifti_name = 'New.nii.gz'\n\nresult = convert_and_rename_dicom_to_nifti(path_to_dcm, path_to_nii, new_nifti_name)\n\n\n\ndef plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True):\n    if plot:\n        plt.figure(figsize=(20, 7))\n\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        center_y = original_image.shape[0] // 2\n        center_x = original_image.shape[1] // 2\n\n        ellipse_radius_x = 175\n        ellipse_radius_y = 370\n        ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(ellipse)\n\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]\n\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n        ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n        lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")\n\n        plt.tight_layout()\n        plt.show()\n\n# Example usage inside your loop\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0]\n        original_label = loader(test_files[idx]['label'])[0]\n\n        slice_idx = 157\n        plot_results(original_image, original_label, test_output, idx, slice_idx, plot=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-27T08:05:21.638905Z","iopub.execute_input":"2023-08-27T08:05:21.639305Z","iopub.status.idle":"2023-08-27T08:06:10.147915Z","shell.execute_reply.started":"2023-08-27T08:05:21.639262Z","shell.execute_reply":"2023-08-27T08:06:10.146941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\npath_to_dcm = '/kaggle/input/rsna-str-pulmonary-embolism-detection/train/00511e94edec/297f170f1197/'\npath_to_nii = '/kaggle/working/'\n\ndicom2nifti.convert_directory(path_to_dcm,path_to_nii)\n\nold_file_name = '/kaggle/working/2.nii.gz'\nnew_file_name = '/kaggle/working/second.nii.gz'\nresult = rename_kaggle_file(old_file_name, new_file_name)\nprint(result)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.patches import Ellipse\nfrom skimage.morphology import binary_dilation, binary_erosion, disk\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 157\n\n        plt.figure(figsize=(20, 7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        \n        # Calculate the center coordinates\n        center_y = original_image.shape[0] // 2\n        center_x = original_image.shape[1] // 2\n        \n        # Draw the ellipse on the original image\n        ellipse_radius_x = 175\n        ellipse_radius_y = 370\n        ellipse = Ellipse((center_x, center_y), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n        plt.gca().add_patch(ellipse)\n\n        # Get the predicted lung mask\n        lung_mask = test_output[0].detach().cpu().numpy()[1,:,:,slice_idx]  # Assuming lung mask is at index 1\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(lung_mask.shape[1]), np.arange(lung_mask.shape[0]))\n\n        # Create an ellipse mask with user-defined characteristics\n        ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n\n        # Set values outside the ellipse mask to 0 in the lung mask\n        lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n        # Apply binary dilation and erosion to remove small remaining parts\n        lung_mask = binary_erosion(binary_dilation(lung_mask, disk(9)), disk(12))\n\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n        plt.axis('off')\n        plt.imshow(lung_mask, cmap=\"gray\")  # Display the lung mask with small parts removed\n\n        plt.tight_layout()\n        plt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T18:01:41.858514Z","iopub.status.busy":"2023-08-26T18:01:41.85809Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_nifti_from_mask(mask, output_filename, original_nifti_header):\n    \"\"\"\n    Save a region of interest based on a binary mask as a NIfTI file.\n    \n    Args:\n        mask (ndarray): Binary mask where the region of interest is marked as 1.\n        output_filename (str): Filename for the output NIfTI file.\n        original_nifti_header (nibabel.nifti1.Nifti1Header): Header information of the original NIfTI image.\n    \"\"\"\n    new_nifti = nib.Nifti1Image(mask.astype(np.uint8), None, header=original_nifti_header)\n    nib.save(new_nifti, output_filename)\n\n# ... (rest of your code)\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\noutput_dir = \"/kaggle/working/\"\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(original_image.shape[2]), np.arange(original_image.shape[1]))\n\n        lung_mask_volume = []\n\n        for slice_idx in range(original_image.shape[0]):\n            plt.figure(figsize=(20, 7))\n            plt.subplot(1, 3, 1)\n            plt.axis('off')\n            plt.title(f'test image {idx+1}, slice {slice_idx}')\n            plt.imshow(original_image[slice_idx, :, :], cmap=\"gray\")\n            \n            # Draw the ellipse on the original image\n            ellipse_radius_x = 175\n            ellipse_radius_y = 370\n            ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n            plt.gca().add_patch(ellipse)\n\n            plt.subplot(1, 3, 2)\n            plt.title(f'reference label {idx+1}, slice {slice_idx}')\n            plt.axis('off')\n            plt.imshow(original_label[slice_idx, :, :], cmap=\"gray\")\n\n            # Get the predicted lung mask\n            lung_mask = test_output[0].detach().cpu().numpy()[1, slice_idx, :, :]  # Assuming lung mask is at index 1\n\n            # Create an ellipse mask with user-defined characteristics\n            ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n\n            # Set values outside the ellipse mask to 0 in the lung mask\n            lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n            plt.subplot(1, 3, 3)\n            plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n            plt.axis('off')\n            plt.imshow(lung_mask, cmap=\"gray\")  # Display the lung mask with left outside part removed\n\n            lung_mask_volume.append(lung_mask)\n\n            plt.tight_layout()\n            plt.show()\n\n        # Save the lung mask volume as a NIfTI file\n        lung_mask_volume = np.array(lung_mask_volume)\n        output_filename = os.path.join(output_dir, f\"lung_mask_{idx}.nii.gz\")\n        save_nifti_from_mask(lung_mask_volume, output_filename, original_image.nifti_header)  # Provide the nifti_header from the original image\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T17:15:56.108884Z","iopub.status.busy":"2023-08-26T17:15:56.108444Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\noutput_dir = \"/kaggle/working/\"\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(original_image.shape[2]), np.arange(original_image.shape[1]))\n\n        lung_mask_volume = []\n\n        for slice_idx in range(original_image.shape[0]):\n            plt.figure(figsize=(20, 7))\n            plt.subplot(1, 3, 1)\n            plt.axis('off')\n            plt.title(f'test image {idx+1}, slice {slice_idx}')\n            plt.imshow(original_image[slice_idx, :, :], cmap=\"gray\")\n            \n            # Draw the ellipse on the original image\n            ellipse_radius_x = 175\n            ellipse_radius_y = 370\n            ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n            plt.gca().add_patch(ellipse)\n\n            plt.subplot(1, 3, 2)\n            plt.title(f'reference label {idx+1}, slice {slice_idx}')\n            plt.axis('off')\n            plt.imshow(original_label[slice_idx, :, :], cmap=\"gray\")\n\n            # Get the predicted lung mask\n            lung_mask = test_output[0].detach().cpu().numpy()[1, slice_idx, :, :]  # Assuming lung mask is at index 1\n\n            # Create an ellipse mask with user-defined characteristics\n            ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n\n            # Set values outside the ellipse mask to 0 in the lung mask\n            lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n            plt.subplot(1, 3, 3)\n            plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n            plt.axis('off')\n            plt.imshow(lung_mask, cmap=\"gray\")  # Display the lung mask with left outside part removed\n\n            lung_mask_volume.append(lung_mask)\n\n            plt.tight_layout()\n            plt.show()\n\n        # Save the lung mask volume as a NIfTI file\n        lung_mask_volume = np.array(lung_mask_volume)\n        output_filename = os.path.join(output_dir, f\"lung_mask_{idx}.nii.gz\")\n        save_nifti_from_mask(lung_mask_volume, output_filename, original_image.nifti_header)  # Provide the nifti_header from the original image\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T17:17:58.210292Z","iopub.status.busy":"2023-08-26T17:17:58.209875Z","iopub.status.idle":"2023-08-26T17:17:58.665789Z","shell.execute_reply":"2023-08-26T17:17:58.664325Z","shell.execute_reply.started":"2023-08-26T17:17:58.210255Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.patches import Ellipse\nimport numpy as np\nimport nibabel as nib\n\n# ... (rest of your code)\n\ndef save_nifti_from_mask(mask, output_filename, original_nifti_header):\n    \"\"\"\n    Save a region of interest based on a binary mask as a NIfTI file.\n    \n    Args:\n        mask (ndarray): Binary mask where the region of interest is marked as 1.\n        output_filename (str): Filename for the output NIfTI file.\n        original_nifti_header (nibabel.nifti1.Nifti1Header): Header information of the original NIfTI image.\n    \"\"\"\n    new_nifti = nib.Nifti1Image(mask.astype(np.uint8), None, header=original_nifti_header)\n    nib.save(new_nifti, output_filename)\n\n# ... (rest of your code)\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        # Draw the ellipse on the original image\n        ellipse_radius_x = 175\n        ellipse_radius_y = 370\n        ellipse = Ellipse((center_y, center_x), width=ellipse_radius_x*2, height=ellipse_radius_y*2, fill=False, color='red', linewidth=2)\n\n        # Create a meshgrid of coordinates\n        x, y = np.meshgrid(np.arange(original_image.shape[2]), np.arange(original_image.shape[1]))\n\n        lung_mask_volume = []\n\n        for slice_idx in range(original_image.shape[0]):\n            plt.figure(figsize=(20, 7))\n            plt.subplot(1, 3, 1)\n            plt.axis('off')\n            plt.title(f'test image {idx+1}, slice {slice_idx}')\n            plt.imshow(original_image[slice_idx, :, :], cmap=\"gray\")\n            plt.gca().add_patch(ellipse)\n\n            plt.subplot(1, 3, 2)\n            plt.title(f'reference label {idx+1}, slice {slice_idx}')\n            plt.axis('off')\n            plt.imshow(original_label[slice_idx, :, :], cmap=\"gray\")\n\n            # Get the predicted lung mask\n            lung_mask = test_output[0].detach().cpu().numpy()[1, slice_idx, :, :]  # Assuming lung mask is at index 1\n\n            # Create an ellipse mask with user-defined characteristics\n            ellipse_mask = (((x - center_x) / ellipse_radius_x)**2 + ((y - center_y) / ellipse_radius_y)**2) <= 1\n\n            # Set values outside the ellipse mask to 0 in the lung mask\n            lung_mask[~ellipse_mask & (x < center_x)] = 0\n\n            plt.subplot(1, 3, 3)\n            plt.title(f'predicted label {idx+1}, slice {slice_idx} (ellipse region)')\n            plt.axis('off')\n            plt.imshow(lung_mask, cmap=\"gray\")  # Display the lung mask with left outside part removed\n\n            lung_mask_volume.append(lung_mask)\n\n            plt.tight_layout()\n            plt.show()\n\n        # Save the lung mask volume as a NIfTI file\n        lung_mask_volume = np.array(lung_mask_volume)\n        output_filename = f\"lung_mask_{idx}.nii.gz\"\n        save_nifti_from_mask(lung_mask_volume, output_filename, original_nifti_header)  # Provide the original_nifti_header\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\ndef delete_folder(folder_path):\n    try:\n        shutil.rmtree(folder_path)\n        print(f\"{folder_path} has been successfully deleted.\")\n    except OSError as e:\n        print(f\"Error deleting {folder_path}: {e}\")\n# Example usage\nfile_to_delete = '/kaggle/working/inference_results'  # Replace this with your actual file path\ndelete_folder(file_to_delete)\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T15:25:26.477614Z","iopub.status.busy":"2023-08-26T15:25:26.477195Z","iopub.status.idle":"2023-08-26T15:25:26.48565Z","shell.execute_reply":"2023-08-26T15:25:26.484438Z","shell.execute_reply.started":"2023-08-26T15:25:26.477582Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import numpy as np\nimport matplotlib.pyplot as plt\n#from skimage.segmentation import clear_border, label\nfrom skimage import io, color\n\ndef convert_to_binary(image, threshold):\n    binary_mask = image < threshold\n    return binary_mask\n\n# Comment out the following line that imports the module causing the QhullError\n# from skimage.segmentation import clear_border, label\n\ndef remove_border_blobs(binary_mask):\n    cleared_mask = clear_border(binary_mask)\n    return cleared_mask\n\n# Rest of your code...\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T15:17:19.678012Z","iopub.status.busy":"2023-08-26T15:17:19.676567Z","iopub.status.idle":"2023-08-26T15:17:19.688591Z","shell.execute_reply":"2023-08-26T15:17:19.687539Z","shell.execute_reply.started":"2023-08-26T15:17:19.677967Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import numpy as np\nimport matplotlib.pyplot as plt\nfrom skimage import io, color\nimport cv2\n\ndef convert_to_binary(image, threshold):\n    binary_mask = image < threshold\n    return binary_mask\n\ndef remove_border_blobs(binary_mask):\n    contours, _ = cv2.findContours(binary_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    cleared_mask = np.zeros_like(binary_mask)\n    for contour in contours:\n        area = cv2.contourArea(contour)\n        if area > 100:  # Set a minimum area threshold to keep\n            cv2.drawContours(cleared_mask, [contour], -1, 1, thickness=cv2.FILLED)\n    return cleared_mask\n\n# Rest of your code...\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T15:18:04.636386Z","iopub.status.busy":"2023-08-26T15:18:04.635973Z","iopub.status.idle":"2023-08-26T15:18:04.645443Z","shell.execute_reply":"2023-08-26T15:18:04.644472Z","shell.execute_reply.started":"2023-08-26T15:18:04.636355Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scipy","metadata":{"execution":{"iopub.execute_input":"2023-08-26T15:15:26.994627Z","iopub.status.busy":"2023-08-26T15:15:26.994109Z","iopub.status.idle":"2023-08-26T15:15:38.504695Z","shell.execute_reply":"2023-08-26T15:15:38.50338Z","shell.execute_reply.started":"2023-08-26T15:15:26.994587Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import numpy as np\nimport matplotlib.pyplot as plt\nimport nibabel as nib  # Library for working with NIfTI files\nimport cv2\n\ndef compute_convex_hull(binary_mask):\n    # Convert the binary_mask to 8-bit unsigned integer format\n    binary_mask_uint8 = binary_mask.astype(np.uint8) * 255\n\n    contours, _ = cv2.findContours(binary_mask_uint8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n    if len(contours) == 0:\n        return binary_mask\n    largest_contour = max(contours, key=cv2.contourArea)\n    hull = cv2.convexHull(largest_contour)\n    convex_hull_mask = np.zeros_like(binary_mask)\n    cv2.drawContours(convex_hull_mask, [hull], -1, 1, -1)\n    return convex_hull_mask\n\n\n\nthreshold = -400\n\n# Load the NIfTI file\nnifti_filename = '/kaggle/working/second.nii.gz'\nnifti_image = nib.load(nifti_filename)\nbinary_mask = nifti_image.get_fdata() > threshold  # Set your threshold here\n\n# Compute the convex hull mask\nconvex_hull_mask = compute_convex_hull(binary_mask)\n\n# Example usage\nplt.figure(figsize=(10, 5))\nplt.subplot(1, 2, 1)\nplt.imshow(binary_mask[..., 0], cmap='gray')\nplt.title('Original Binary Mask')\nplt.subplot(1, 2, 2)\nplt.imshow(convex_hull_mask[..., 0], cmap='gray')\nplt.title('Convex Hull Mask')\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.execute_input":"2023-08-26T15:20:36.776958Z","iopub.status.busy":"2023-08-26T15:20:36.77657Z","iopub.status.idle":"2023-08-26T15:20:36.78716Z","shell.execute_reply":"2023-08-26T15:20:36.786078Z","shell.execute_reply.started":"2023-08-26T15:20:36.776929Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport nibabel as nib\nfrom skimage.segmentation import clear_border\n\ndef convert_to_binary(image_data, threshold):\n    binary_mask = image_data < threshold\n    return binary_mask\n\ndef remove_border_blobs(binary_mask):\n    cleared_mask = clear_border(binary_mask)\n    return cleared_mask\n\ndef convert_mask_to_nifti(binary_mask, original_nifti_path):\n    original_nifti = nib.load(original_nifti_path)\n    nifti_image = nib.Nifti1Image(binary_mask.astype(np.uint8), original_nifti.affine, header=original_nifti.header)\n    processed_nifti_path = original_nifti_path.replace('.nii', '_processed.nii')\n    nib.save(nifti_image, processed_nifti_path)\n    return processed_nifti_path\n\n# Set the path to your original NIfTI image and the threshold value\noriginal_nifti_path = 'original_image.nii'\nthreshold = -400  # Adjust threshold as needed\n\n# Load the original NIfTI image\noriginal_nifti = nib.load(original_nifti_path)\noriginal_image_data = original_nifti.get_fdata()\n\n# Apply the convert_to_binary and remove_border_blobs functions\nbinary_mask = convert_to_binary(original_image_data, threshold)\ncleared_mask = remove_border_blobs(binary_mask)\n\n# Convert the processed mask back to NIfTI format and get the processed NIfTI path\nprocessed_nifti_path = convert_mask_to_nifti(cleared_mask, original_nifti_path)\n\nprint(\"Processed NIfTI image saved at:\", processed_nifti_path)\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from monai.transforms import LoadImage, keep_centered_components\nfrom monai.handlers.utils import from_engine\n\nloader = LoadImage()\n\nmodel.load_state_dict(torch.load('/kaggle/input/best-metric-model-14-06-10h-2/best_metric_model_14_06_10h_2.pth'))\nmodel.eval()\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import ndimage\n\ndef keep_elliptical_region(mask, elliptical_axes):\n    \"\"\"\n    Keep only the mask components within the specified elliptical region.\n    \n    Args:\n        mask (ndarray): Binary mask.\n        elliptical_axes (tuple): Semi-major and semi-minor axes of the elliptical region.\n\n    Returns:\n        ndarray: Mask with components outside the elliptical region set to 0.\n    \"\"\"\n    labels, num_label = ndimage.label(mask)\n    label_areas = np.bincount(labels.ravel())\n    largest_label = np.argmax(label_areas[1:]) + 1\n\n    mask_center = ndimage.measurements.center_of_mass(labels == largest_label)\n    mask_center = np.array(mask_center)\n    \n    elliptical_mask = np.zeros_like(mask, dtype=bool)\n    y, x = np.ogrid[-mask_center[0]:mask.shape[0]-mask_center[0], -mask_center[1]:mask.shape[1]-mask_center[1]]\n    mask_indices = (x * x) / (elliptical_axes[0] ** 2) + (y * y) / (elliptical_axes[1] ** 2) <= 1\n    elliptical_mask[mask_center[0]:mask_center[0]+mask_indices.shape[0], mask_center[1]:mask_center[1]+mask_indices.shape[1]] = mask_indices\n    \n    new_mask = np.logical_and(labels == largest_label, elliptical_mask)\n    return new_mask\n\n\nwith torch.no_grad():\n    for idx, test_data in enumerate(test_loader):\n        test_inputs = test_data[\"image\"].to(device)\n        test_data[\"pred\"] = sliding_window_inference(test_inputs, (64, 64, 64), 1, model, overlap=0.75)\n        test_data = [post_transforms(i) for i in decollate_batch(test_data)]\n        \n        # Get the predicted lung mask\n        pred_lung_mask = test_data[0][\"pred\"].argmax(0) == 1  # Assuming index 1 is the lung class\n        \n        # Apply keep_elliptical_region to the predicted lung mask\n        pred_mask_elliptical = keep_elliptical_region(pred_lung_mask, elliptical_axes=(150, 100))\n        test_data[0][\"pred\"][1] = pred_mask_elliptical  # Update the lung mask in the prediction\n        \n        test_output = from_engine([\"pred\"])(test_data)\n\n        original_image = loader(test_files[idx]['image'])[0] # Load the original image\n        original_label = loader(test_files[idx]['label'])[0] # Load the original label\n\n        slice_idx = 57\n\n        plt.figure(figsize=(20,7))\n        plt.subplot(1, 3, 1)\n        plt.axis('off')\n        plt.title(f'test image {idx+1}, slice {slice_idx}')\n        plt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        plt.subplot(1, 3, 2)\n        plt.title(f'reference label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\n        plt.subplot(1, 3, 3)\n        plt.title(f'predicted label {idx+1}, slice {slice_idx}')\n        plt.axis('off')\n        plt.imshow(test_output[0].detach().cpu().numpy()[1,:,:,slice_idx], cmap=\"gray\") # use index 1 to get the lung class\n        plt.tight_layout()\n        plt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T14:52:40.033612Z","iopub.status.busy":"2023-08-26T14:52:40.033146Z","iopub.status.idle":"2023-08-26T14:52:40.494822Z","shell.execute_reply":"2023-08-26T14:52:40.49347Z","shell.execute_reply.started":"2023-08-26T14:52:40.033579Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nslice_idx = 197\n\nplt.figure(figsize=(20,7))\nplt.subplot(1, 3, 1)\nplt.axis('off')\nplt.title(f'test image {idx+1}, slice {slice_idx}')\nplt.imshow(original_image.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\nplt.subplot(1, 3, 2)\nplt.title(f'reference label {idx+1}, slice {slice_idx}')\nplt.axis('off')\nplt.imshow(original_label.detach().cpu().numpy()[:,:,slice_idx], cmap=\"gray\")\nplt.subplot(1, 3, 3)\nplt.title(f'predicted label {idx+1}, slice {slice_idx}')\nplt.axis('off')\nplt.imshow(test_output[0].detach().cpu().numpy()[1,:,:,slice_idx], cmap=\"gray\") # use index 1 to get the lung class\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T14:00:07.931637Z","iopub.status.busy":"2023-08-26T14:00:07.931219Z","iopub.status.idle":"2023-08-26T14:00:09.02504Z","shell.execute_reply":"2023-08-26T14:00:09.020987Z","shell.execute_reply.started":"2023-08-26T14:00:07.931605Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\nimport matplotlib.pyplot as plt\n\ndef plot_nii_file(nii_file_path):\n    try:\n        # Load NIfTI file\n        nii_img = nib.load(nii_file_path)\n        \n        # Get the NIfTI data array\n        nii_data = nii_img.get_fdata()\n        \n        # Plot the NIfTI data\n        plt.figure(figsize=(8, 8))\n        plt.imshow(nii_data[:, :, nii_data.shape[2] // 2], cmap='gray', origin='lower')\n        plt.title('NIfTI Image')\n        plt.colorbar()\n        plt.show()\n        \n    except Exception as e:\n        print(\"An error occurred:\", e)\n\n# Provide the path to your NIfTI file here\nnii_file_path = \"/kaggle/working/2.nii.gz\"\n\nnii_file_path = \"/kaggle/working/second.nii.gz\"\n\nnii_file_path = \"/kaggle/working/second.nii.gz\"\nplot_nii_file(nii_file_path)\n\n","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = Dataset(data=test_files, transform=val_transforms)\ntest_loader = DataLoader(test_ds, batch_size=1)\n\npost_transforms = Compose([\n    Invertd(\n        keys=\"pred\",\n        transform=val_transforms,\n        orig_keys=\"image\",\n        meta_keys=\"pred_meta_dict\",\n        orig_meta_keys=\"image_meta_dict\",\n        meta_key_postfix=\"meta_dict\",\n        nearest_interp=False,\n        to_tensor=True,\n    ),\n    AsDiscreted(keys=\"pred\", argmax=True, to_onehot=2),\n    SaveImaged(keys=\"pred\", meta_keys=\"pred_meta_dict\", output_dir=\"./out\", output_postfix=\"seg\", resample=False),\n])","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport os\nimport glob\nimport nibabel as nib\nimport numpy as np\nfrom monai.networks.nets import UNet\nfrom monai.inferers import sliding_window_inference\nimport matplotlib.pyplot as plt\nfrom nilearn.plotting import plot_anat\n\n# Define the path to your trained model\nmodel_path = '/kaggle/input/best-metric-model-08-06-12h-lung/best_metric_model_08_06_11h.pth'\n\n# Load the model\ndevice = torch.device(\"cuda:0\")\nmodel = UNet(\n    spatial_dims=3,\n    in_channels=1,\n    out_channels=2,\n    channels=(16, 32, 64, 128, 256,512),\n    strides=(2, 2, 2, 2,2),\n    num_res_units=2,\n    norm=Norm.BATCH,\n).to(device)\nmodel.load_state_dict(torch.load(model_path))\nmodel.eval()\n\n# Define the path to the data\n# Define the path to the data\ndata_dir = '/kaggle/working/'\noutput_dir = '/kaggle/working/inference_results'\n\nos.makedirs(output_dir, exist_ok=True)\n\n# Get a list of input data files\ninput_files = glob.glob(os.path.join(data_dir, \"*.nii*\"))\n\n# Perform inference and visualization on each input file\nfor input_file in input_files:\n    img = nib.load(input_file)\n    input_data = np.array(img.dataobj)\n    input_data = np.expand_dims(input_data, axis=0)  # Add batch dimension\n    input_data = np.expand_dims(input_data, axis=1)  # Add channel dimension\n    input_data = torch.tensor(input_data, dtype=torch.float32).to(device)\n    \n    def predictor(x):\n        return model(x)\n    \n    with torch.no_grad():\n        roi_size = (96, 96, 96)  # Size of the sliding window\n        sw_batch_size = 4  # Number of patches processed simultaneously\n        inferer = sliding_window_inference(predictor, input_data, roi_size, sw_batch_size, device=device)\n        output = inferer\n    \n    # Convert output tensor to numpy array\n    output_data = output.cpu().numpy()[0, 1]  # Extract the segmentation channel\n    \n    # Plot input image, ground truth (if available), and model output\n    fig, axes = plt.subplots(1, 3, figsize=(15, 5))\n    plot_anat(input_data.squeeze(), title=\"Input Image\", axes=axes[0])\n    axes[0].set_axis_off()\n    axes[1].imshow(img.dataobj[..., img.shape[2] // 2], cmap=\"gray\")\n    axes[1].set_title(\"Ground Truth\")\n    axes[1].axis(\"off\")\n    axes[2].imshow(output_data[..., output_data.shape[2] // 2], cmap=\"gray\")\n    axes[2].set_title(\"Model Output\")\n    axes[2].axis(\"off\")\n    plt.show()\n    \n    # Save the output as a NIfTI file\n    output_img = nib.Nifti1Image(output_data, img.affine)\n    output_filename = os.path.join(output_dir, os.path.basename(input_file))\n    nib.save(output_img, output_filename)\n","metadata":{"execution":{"iopub.execute_input":"2023-08-26T13:25:08.37497Z","iopub.status.busy":"2023-08-26T13:25:08.374582Z","iopub.status.idle":"2023-08-26T13:25:10.366524Z","shell.execute_reply":"2023-08-26T13:25:10.365149Z","shell.execute_reply.started":"2023-08-26T13:25:08.374939Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bce = torch.nn.BCEWithLogitsLoss()\ndef criterion(logits, target):\n    loss = bce(logits.view(-1), target.view(-1))\n    return loss","metadata":{"execution":{"iopub.status.busy":"2023-08-24T23:07:46.231343Z","iopub.status.idle":"2023-08-24T23:07:46.231824Z","shell.execute_reply":"2023-08-24T23:07:46.231586Z","shell.execute_reply.started":"2023-08-24T23:07:46.231562Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ncnn\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-24T23:07:46.236093Z","iopub.status.idle":"2023-08-24T23:07:46.236556Z","shell.execute_reply":"2023-08-24T23:07:46.236343Z","shell.execute_reply.started":"2023-08-24T23:07:46.236321Z"},"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[]},"execution_count":null,"outputs":[]}]}