{"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":"# Import stuff\nimport random\nimport numpy as np\nimport torch\nimport torch.backends.cudnn as cudnn\nimport torch.optim\nimport pandas as pd\nfrom torch.utils.data import Dataset\nfrom glob import glob\nimport random\nimport torch.distributed as dist\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import DataLoader\nfrom torchvision import transforms\nfrom PIL import Image\nfrom torchvision.transforms import ToTensor\nimport torchvision\nimport cv2\nfrom sklearn.metrics import roc_curve, auc\nimport os\nimport pytorch_lightning as pl\nimport torch.nn.functional as F\nimport re\nimport ast\nimport random\nfrom torchvision import models\nfrom pytorch_lightning.loggers import WandbLogger\nfrom scipy import signal\nimport json","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:28:51.599768Z","iopub.execute_input":"2022-07-05T20:28:51.600499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset():\n\n    def __init__(self, file_list, mode, ett_annotations, carina_annotations, crop_dim):\n        self.file_list = file_list\n        self.mode = mode\n        self.ett_annotations = ett_annotations\n        self.carina_box_height, self.carina_box_width = 10, 10\n        self.ett_box_height, self.ett_box_width = 10, 10\n        self.crop_dim = crop_dim\n        self.carina_annotated_filenames  = carina_annotations['External ID'].tolist()\n        self.ett_annotated_filenames = [i + '.jpg' for i in ett_annotations['StudyInstanceUID'].tolist()]\n        self.carina_annots = carina_annotations['Label'].tolist()\n\n    def __getitem__(self,idx):\n        image_filepath = self.file_list[idx]\n        image, img_uid, original_imsize = self.loadimage(image_filepath)\n        filename = f'{img_uid}.jpg'\n\n        if filename in self.carina_annotated_filenames and filename in self.ett_annotated_filenames:\n            identity = 'Both'\n        elif filename in self.carina_annotated_filenames:\n            identity = 'Carina'\n        else:\n            identity = 'ETT'\n\n        gt_map, ett_coord_map = self.get_GT(img_uid, original_imsize, identity)\n\n        sample = {'image': image,\n                  'gt': gt_map,\n                  'img_uid': img_uid,\n                  'ett_coord_map': ett_coord_map,\n                  'identity' : identity\n              }\n\n        return sample\n\n    def __len__(self):\n        return len(self.file_list)\n\n    def loadimage(self, image_filepath):\n        img = cv2.imread(image_filepath, 0)\n        original_imsize = img.shape\n#         img = self.crop_center(img, 1600, 1600)\n        img = self.crop_center(img, self.crop_dim, self.crop_dim)\n        image = ToTensor()(img)\n        image = 2*(image/torch.max(image))-1\n        img_uid = image_filepath.split('/')[4][:-4]\n        return image, img_uid, original_imsize\n\n    def get_GT(self, img_uid, original_imsize, identity):\n        ett_annotations = self.ett_annotations\n\n        # repeat for et tube\n        if identity == 'ETT' or identity == 'Both':\n            ids = ett_annotations['StudyInstanceUID'].tolist()\n            index_ett = ids.index(img_uid)\n            xs = (ett_annotations['data'][index_ett])\n            points = self.str2array(xs)\n            y_coords = points[:,1]\n            lowest_y = np.argmax(y_coords)\n            gt_map_et = np.zeros([original_imsize[0], original_imsize[1]], dtype='float')\n            ett_coord_map = np.zeros([original_imsize[0], original_imsize[1]], dtype='float')\n            x_ett, y_ett = points[lowest_y]\n            ett_coord_map[y_ett,x_ett] = 1\n            gt_map_et[y_ett-self.ett_box_height:y_ett+self.ett_box_height, x_ett-self.ett_box_width:x_ett+self.ett_box_width] = 1\n#             gt_map_et = self.crop_center(gt_map_et, 1600, 1600)\n#             ett_coord_map = self.crop_center(ett_coord_map, 1600, 1600)\n            gt_map_et = self.crop_center(gt_map_et, self.crop_dim,self.crop_dim)\n            ett_coord_map = self.crop_center(ett_coord_map, self.crop_dim,self.crop_dim)\n  \n        return gt_map_et, ett_coord_map\n\n    def crop_center(self,img,cropx,cropy):\n        y,x = img.shape\n        startx = x//2-(cropx//2)\n        # crop closer to top\n        return img[0:cropy,startx:startx+cropx]\n\n    def str2array(self, s):\n        # Remove space after [\n        s=re.sub('\\[ +', '[', s.strip())\n        # Replace commas and spaces\n        s=re.sub('[,\\s]+', ', ', s)\n        return np.array(ast.literal_eval(s))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:40:27.661412Z","iopub.execute_input":"2022-07-05T19:40:27.661837Z","iopub.status.idle":"2022-07-05T19:40:27.684628Z","shell.execute_reply.started":"2022-07-05T19:40:27.661804Z","shell.execute_reply":"2022-07-05T19:40:27.683296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"carina_annotations = pd.read_json('../input/nihcsvdata/carina_annot.json')\nett_annotations = pd.read_csv('../input/nihcsvdata/ETT_Annotations.csv')\ncombined_df = pd.read_csv('../input/nihcsvdata/combined_NIH_CLIP.csv')\nf = open('../input/5k-trachea-bifurcation-on-chest-xray/trachea_annotations.json')\ncarina_extras = json.load(f)\ncarina_extras_keys = carina_extras.keys()\ncarina_extras_keys = [i for i in carina_extras_keys]\ncarina_extras_names = [i[:-10] for i in carina_extras_keys]\ncarina_to_include = []\ncarina_extras_indexes_to_include = []\ncombined_df = combined_df[combined_df['Original Image Pixel Spacing x'].notna()]\ncombined_df = combined_df.reset_index()\ncombined_df = combined_df.drop(['index'], axis=1)\ncombined_df = combined_df.drop(['Unnamed: 0'], axis=1)\ndist_conversions = combined_df['StudyInstanceUID'].to_list()\nett_annotations = ett_annotations[ett_annotations['StudyInstanceUID'].isin(dist_conversions)]\nett_annotations = ett_annotations.reset_index()\nett_annotations = ett_annotations.drop(['index'], axis=1)\n\nfor i in range(0,len(carina_annotations)):\n    if carina_annotations['Label'][i]['objects'] == []:\n        stop_ind = i\n        break\n\ncarina_annotations = carina_annotations[:stop_ind]\ncarina_annotations_original = carina_annotations['External ID'].to_list()\n\nnum_samples_with_annotations = len(carina_annotations['Label'])\n\n# loop over each sample that has at least one anntation\nfor i in range(num_samples_with_annotations):\n    annotations_for_sample = carina_annotations['Label'][i]['objects']\n    \n    has_carina_annotation = False\n    has_uncertainty = False\n\n    # loop over each annotation and check (1) that a carina annotation exists, and (2) there's no uncertainty bounding box.\n    for annotation in annotations_for_sample:\n        if annotation['title'] == 'Carina':\n            has_carina_annotation = True\n        elif annotation['title'] == 'Carina - Uncertainty': ## Need to double check the name of this one\n            has_uncertainty = True\n\n    if has_carina_annotation and has_uncertainty is False:\n        carina_to_include.append(carina_annotations['External ID'][i])\n        \ndist_conversion_filenames = [i + '.jpg' for i in dist_conversions]\nreal_carina_include = [i for i in carina_to_include if i in dist_conversion_filenames]\ncarina_annotations = carina_annotations[carina_annotations['External ID'].isin(real_carina_include)]\ncarina_annotations = carina_annotations.reset_index()\ncarina_annotations = carina_annotations.drop(['index'], axis=1)\n\n# for i in range(0,len(carina_extras_names)):\n#     if carina_extras_names[i] in dist_conversions:\n#         if carina_extras_names[i] not in carina_annotations_original:\n#             carina_extras_indexes_to_include.append(i)\n            \n# for i in carina_extras_indexes_to_include:\n#     dict_format = {'objects': [{'title': 'Carina','value': 'carina','point': {'x': 0, 'y': 0}}]}\n#     name = carina_extras_keys[i]\n#     external_id = carina_extras_names[i] + '.jpg'\n#     if len(carina_extras[carina_extras_keys[i]]['regions']) > 0:\n#         x = carina_extras[carina_extras_keys[i]]['regions'][0]['shape_attributes']['cx']\n#         y = carina_extras[carina_extras_keys[i]]['regions'][0]['shape_attributes']['cy']\n#         dict_format['objects'][0]['point']['x'] = x\n#         dict_format['objects'][0]['point']['y'] = y\n#         carina_annotations.loc[len(carina_annotations.index)] = [0,0,0,dict_format,0,0,0,0,0,external_id,0,0,0,0,0,0,0,0]\n# carina_annotations = carina_annotations.reset_index()\n# carina_annotations = carina_annotations.drop(['index'], axis=1)\n\nprint(len(carina_annotations))\nprint(len(ett_annotations))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:31:43.134679Z","iopub.execute_input":"2022-07-05T19:31:43.13509Z","iopub.status.idle":"2022-07-05T19:31:43.435988Z","shell.execute_reply.started":"2022-07-05T19:31:43.135061Z","shell.execute_reply":"2022-07-05T19:31:43.434871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef get_paths(names, dir):\n        \"\"\"\n        Slice an input list into train, val, and test splits\n        \"\"\"\n        names = [dir + i + '.jpg' for i in names]\n        val_length = int(len(names) * 0.2)\n        random.seed(10)\n        random.shuffle(names)\n        #  Slicing is done as follows:\n        #  |-- validation (0.2) -- | --------- train (0.6) --------- | -- test ( 0.2 ) -- |\n        return names[val_length:-val_length], names[:val_length], names[-val_length:]\n    \ntrain_dir = '../input/ranzcr-clip-catheter-line-classification/train/'\nall_ett = ett_annotations['StudyInstanceUID'].to_list()\nall_carina = [i[:-4] for i in carina_annotations['External ID'].to_list()]\nboth_carina_ett = [value for value in all_ett if value in all_carina]\nonly_ett = [value for value in all_ett if value not in both_carina_ett]\nonly_carina = [value for value in all_carina if value not in both_carina_ett]\n\ntrain_ett, val_ett, test_ett = get_paths(only_ett, train_dir)\ntrain_carina, val_carina, test_carina = get_paths(only_carina, train_dir)\ntrain_both, val_both, test_both = get_paths(both_carina_ett, train_dir)\n\n# train_files = train_carina+train_both\n# val_files = val_carina+val_both\n# test_files = test_carina+test_both\n\ntrain_files = train_ett+train_both+test_ett+test_both\nval_files = val_ett+val_both\n\nprint(len(train_files))\nprint(len(val_files))\n# print(len(test_files))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:31:46.432752Z","iopub.execute_input":"2022-07-05T19:31:46.433156Z","iopub.status.idle":"2022-07-05T19:31:46.480122Z","shell.execute_reply.started":"2022-07-05T19:31:46.433127Z","shell.execute_reply":"2022-07-05T19:31:46.478932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# real_paths = []\n# paths = ett_annotations['StudyInstanceUID'].tolist()\n# for path in paths:\n#     if path in combined_df['StudyInstanceUID'].tolist():\n#         real_paths.append(path)\n\n# train_dir = '../input/ranzcr-clip-catheter-line-classification/train/'\n# total_files =[train_dir + i + '.jpg' for i in real_paths]\n# val_length = int(len(total_files) * 0.2)\n# random.seed(10)\n# random.shuffle(total_files)\n# train_files = total_files[val_length:]\n# val_files = total_files[:val_length]\n# test_files = []\n# test_files.extend(sorted(glob(test_dir + '/*.jpg')))\n\n# CHANGE SHUFFLE BACK\nTrain_Dataset = Dataset(train_files, \"Train\", ett_annotations, carina_annotations, 1280)\nTrain_dataloader = DataLoader(Train_Dataset, shuffle=True, num_workers=2, batch_size=1)\n\nVal_Dataset = Dataset(val_files, \"Val\", ett_annotations, carina_annotations, 1280)\nVal_dataloader = DataLoader(Val_Dataset, shuffle=False, num_workers=2, batch_size=1, pin_memory=True)\n\n# Test_Dataset = Dataset(test_set, \"Test\", ett_annotations, carina_annotations)\n# Test_dataloader = DataLoader(Test_Dataset, shuffle=False, num_workers=2, batch_size=1, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:40:32.727688Z","iopub.execute_input":"2022-07-05T19:40:32.728057Z","iopub.status.idle":"2022-07-05T19:40:32.738348Z","shell.execute_reply.started":"2022-07-05T19:40:32.728029Z","shell.execute_reply":"2022-07-05T19:40:32.737165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example cropped image from dataloader\nfor i, data in enumerate(Train_dataloader):\n    image = data['image']\n#     print(image.shape)\n    gt = data['gt']\n#     print(data['identity'])\n#     gt = gt.squeeze(0)\n#     print(gt.shape)\n    print(data['img_uid'])\n#     gt = gt.cpu().detach().numpy()[0]\n# #     g = data['ETT_coord_map']\n    \n    image = image.cpu().detach().numpy()[0][0]\n    \n# #     for g in range(0,1280):\n# #         for j in range(0,1280):\n# #             if gt[g][j] != 0:\n# #                 image[g][j] = gt[g][j]\n    plt.imshow(image, interpolation='nearest', cmap='Greys')\n    plt.imshow(gt[0], cmap='Greys', alpha=0.2)\n#     plt.imshow(gt[0], cmap='Greys', alpha=0.2)\n\n    break \n#     print(i)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:41:30.59377Z","iopub.execute_input":"2022-07-05T19:41:30.594189Z","iopub.status.idle":"2022-07-05T19:41:31.659823Z","shell.execute_reply.started":"2022-07-05T19:41:30.594156Z","shell.execute_reply":"2022-07-05T19:41:31.658492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Gaussian ting\n\n# class Dataset():\n\n#     def __init__(self, file_list, mode, ett_annotations, carina_annotations):\n#         self.file_list = file_list\n#         self.mode = mode\n#         self.ett_annotations = ett_annotations\n#         self.carina_annotations = carina_annotations\n\n#     def __getitem__(self,idx):\n#         image_filepath = self.file_list[idx]\n#         image, code, original_imsize = self.loadimage(image_filepath)\n        \n#         gt_map, ett_coords = self.get_GT(code, original_imsize)\n# #         for i in range(0,len(image)):\n# #             image[i] = self.crop_center(image[i], ett_coords)\n        \n#         sample = {'Image': image,\n#                   'GT': gt_map,\n#                   'Code': code,\n#                   'ETT_coords': ett_coords,\n#               }\n\n#         return sample\n\n#     def __len__(self):\n#         return len(self.file_list)\n\n#     def loadimage(self, image_filepath):\n#         img = cv2.imread(image_filepath, 0)\n#         original_imsize = img.shape\n#         img = self.crop_center(img, 1280, 1280)\n#         image = ToTensor()(img) \n#         image = 2*(image/torch.max(image))-1\n#         code = image_filepath.split('/')[4][:-4] \n#         return image, code, original_imsize\n    \n#     def get_GT(self, code, original_imsize):\n#         ett_annotations = self.ett_annotations\n#         ids = ett_annotations['StudyInstanceUID'].tolist()\n#         index_ett = ids.index(code) \n        \n#         xs = (ett_annotations['data'][index_ett])\n#         points = self.str2array(xs)\n#         y_coords = points[:,1]\n#         lowest_y = np.argmax(y_coords)\n# #         gt_map_et = np.zeros([original_imsize[0], original_imsize[1]], dtype='float')\n#         x_ett, y_ett = points[lowest_y]\n#         ett_coords = np.array([x_ett,y_ett])\n#         gt_map_et = self.get_gaussian_kernel(ett_coords, original_imsize)\n# #         X, Y = np.mgrid[x_ett-20:x_ett+20, y_ett-20:y_ett+20]\n# #         coords_et = np.vstack((X.ravel(), Y.ravel()))\n# #         for i in range(0,len(coords_et[0])):\n# #             gt_map_et[coords_et[1][i]][coords_et[0][i]] = 1\n        \n#         gt_map_et = self.crop_center(gt_map_et, 1280, 1280)\n\n#         return gt_map_et, ett_coords\n\n#     def crop_center(self,img,cropx,cropy):\n#         y,x = img.shape\n#         startx = x//2-(cropx//2)\n#         # crop closer to top  \n#         return img[0:cropy,startx:startx+cropx]\n    \n#     def str2array(self, s):\n#         # Remove space after [\n#         s=re.sub('\\[ +', '[', s.strip())\n#         # Replace commas and spaces\n#         s=re.sub('[,\\s]+', ', ', s)\n#         return np.array(ast.literal_eval(s))\n    \n#     def get_gaussian_kernel(self, coords, imshape):\n#         N = 201 # kernel size\n#         k1d = signal.gaussian(N, std=20).reshape(N, 1)\n#         kernel = np.outer(k1d, k1d)\n#         x, y = imshape\n#         A = np.zeros([x,y])\n#         A[coords[1],coords[0]] = 1    # random\n#         row, col = np.where(A == 1)\n#         if row[0] >= N//2 and col[0] >= N//2:\n#             A[row[0]-(N//2):row[0]+(N//2)+1, col[0]-(N//2):col[0]+(N//2)+1] = kernel\n#         return A\n ","metadata":{"execution":{"iopub.status.busy":"2022-06-19T22:58:34.642868Z","iopub.execute_input":"2022-06-19T22:58:34.643462Z","iopub.status.idle":"2022-06-19T22:58:34.661898Z","shell.execute_reply.started":"2022-06-19T22:58:34.643429Z","shell.execute_reply":"2022-06-19T22:58:34.66102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # bce\n\n# class Dataset():\n\n#     def __init__(self, file_list, mode, ett_annotations, carina_annotations):\n#         self.file_list = file_list\n#         self.mode = mode\n#         self.ett_annotations = ett_annotations\n#         self.carina_annotations = carina_annotations\n\n#     def __getitem__(self,idx):\n#         image_filepath = self.file_list[idx]\n#         image, code, original_imsize = self.loadimage(image_filepath)\n        \n#         gt_map, ett_coord_map = self.get_GT(code, original_imsize)\n# #         for i in range(0,len(image)):\n# #             image[i] = self.crop_center(image[i], ett_coords)\n        \n#         sample = {'Image': image,\n#                   'GT': gt_map,\n#                   'Code': code,\n#                   'ETT_coord_map': ett_coord_map,\n#               }\n\n#         return sample\n\n#     def __len__(self):\n#         return len(self.file_list)\n\n#     def loadimage(self, image_filepath):\n#         img = cv2.imread(image_filepath, 0)\n#         original_imsize = img.shape\n#         img = self.crop_center(img, 1280,1280)\n#         image = ToTensor()(img) \n#         image = 2*(image/torch.max(image))-1\n#         code = image_filepath.split('/')[4][:-4] \n#         return image, code, original_imsize\n    \n#     def get_GT(self, code, original_imsize):\n#         ett_annotations = self.ett_annotations\n#         ids = ett_annotations['StudyInstanceUID'].tolist()\n#         index_ett = ids.index(code) \n        \n#         xs = (ett_annotations['data'][index_ett])\n#         points = self.str2array(xs)\n#         y_coords = points[:,1]\n#         lowest_y = np.argmax(y_coords)\n#         gt_map_et = np.zeros([original_imsize[0], original_imsize[1]], dtype='float')\n#         ett_coord_map = np.zeros([original_imsize[0], original_imsize[1]], dtype='float')\n#         x_ett, y_ett = points[lowest_y]\n#         ett_coord_map[y_ett,x_ett] = 1\n#         ett_coords = np.array([x_ett,y_ett])\n#         X, Y = np.mgrid[x_ett-20:x_ett+20, y_ett-20:y_ett+20]\n#         coords_et = np.vstack((X.ravel(), Y.ravel()))\n#         for i in range(0,len(coords_et[0])):\n#             gt_map_et[coords_et[1][i]][coords_et[0][i]] = 1\n#         gt_map_et = self.crop_center(gt_map_et, 1280,1280)\n#         ett_coord_map = self.crop_center(ett_coord_map, 1280,1280)\n        \n#         return gt_map_et, ett_coord_map\n\n#     def crop_center(self,img,cropx,cropy):\n#         y,x = img.shape\n#         startx = x//2-(cropx//2)\n#         # crop closer to top  \n#         return img[0:cropy,startx:startx+cropx]\n    \n#     def str2array(self, s):\n#         # Remove space after [\n#         s=re.sub('\\[ +', '[', s.strip())\n#         # Replace commas and spaces\n#         s=re.sub('[,\\s]+', ', ', s)\n#         return np.array(ast.literal_eval(s))\n ","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:19:23.609781Z","iopub.execute_input":"2022-07-05T14:19:23.610164Z","iopub.status.idle":"2022-07-05T14:19:23.627104Z","shell.execute_reply.started":"2022-07-05T14:19:23.61013Z","shell.execute_reply":"2022-07-05T14:19:23.62633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class resconv_block(nn.Module):\n#     def __init__(self,ch_in,ch_out):\n#         super(resconv_block,self).__init__()\n#         self.conv = nn.Sequential(\n#             nn.Conv2d(ch_in, ch_out, kernel_size=3,stride=1,padding=1,bias=True),\n#             nn.BatchNorm2d(ch_out),\n#             nn.ReLU(inplace=True),\n#             nn.Conv2d(ch_out, ch_out, kernel_size=3,stride=1,padding=1,bias=True),\n#             nn.BatchNorm2d(ch_out),\n#             nn.ReLU(inplace=True)\n#         )\n#         self.Conv_1x1 = nn.Conv2d(ch_in,ch_out,kernel_size=1,stride=1,padding=0)\n\n#     def forward(self,x):\n\n#         residual =  self.Conv_1x1(x)\n#         x = self.conv(x)\n\n#         return residual+x\n\n# class up_conv(nn.Module):\n#     def __init__(self,ch_in,ch_out):\n#         super(up_conv,self).__init__()\n#         self.up = nn.Sequential(\n#             nn.Upsample(scale_factor=2),\n#             nn.Conv2d(ch_in,ch_out,kernel_size=3,stride=1,padding=1,bias=True),\n#             nn.BatchNorm2d(ch_out),\n#             nn.ReLU(inplace=True)\n#         )\n\n#     def forward(self,x):\n#         x = self.up(x)\n#         return x\n\n# class ResU_Net(nn.Module):\n#     def __init__(self,img_ch=1,output_ch=1):\n#         super(ResU_Net,self).__init__()\n\n#         self.Maxpool = nn.MaxPool2d(kernel_size=2,stride=2)\n#         self.Softmax = nn.Softmax(dim=1)\n#         self.Sigmoid = nn.Sigmoid()\n\n#         self.Conv1 = resconv_block(ch_in=img_ch,ch_out=64)\n#         self.Conv2 = resconv_block(ch_in=64,ch_out=128)\n#         self.Conv3 = resconv_block(ch_in=128,ch_out=256)\n#         self.Conv4 = resconv_block(ch_in=256,ch_out=512)\n#         self.Conv5 = resconv_block(ch_in=512,ch_out=1024)\n\n#         self.Up5 = up_conv(ch_in=1024,ch_out=512)\n#         self.Up_conv5 = resconv_block(ch_in=1024, ch_out=512)\n\n#         self.Up4 = up_conv(ch_in=512,ch_out=256)\n#         self.Up_conv4 = resconv_block(ch_in=512, ch_out=256)\n\n#         self.Up3 = up_conv(ch_in=256,ch_out=128)\n#         self.Up_conv3 = resconv_block(ch_in=256, ch_out=128)\n\n#         self.Up2 = up_conv(ch_in=128,ch_out=64)\n#         self.Up_conv2 = resconv_block(ch_in=128, ch_out=64)\n\n#         self.Conv_1x1 = nn.Conv2d(64,output_ch,kernel_size=1,stride=1,padding=0)\n\n\n#     def forward(self,x):\n#         # encoding path\n#         x1 = self.Conv1(x)\n\n#         x2 = self.Maxpool(x1)\n#         x2 = self.Conv2(x2)\n\n#         x3 = self.Maxpool(x2)\n#         x3 = self.Conv3(x3)\n\n#         x4 = self.Maxpool(x3)\n#         x4 = self.Conv4(x4)\n\n#         x5 = self.Maxpool(x4)\n#         x5 = self.Conv5(x5)\n\n#         # decoding + concat path\n#         d5 = self.Up5(x5)\n#         d5 = torch.cat((x4,d5),dim=1)\n\n#         d5 = self.Up_conv5(d5)\n\n#         d4 = self.Up4(d5)\n#         d4 = torch.cat((x3,d4),dim=1)\n#         d4 = self.Up_conv4(d4)\n\n#         d3 = self.Up3(d4)\n#         d3 = torch.cat((x2,d3),dim=1)\n#         d3 = self.Up_conv3(d3)\n\n#         d2 = self.Up2(d3)\n#         d2 = torch.cat((x1,d2),dim=1)\n#         d2 = self.Up_conv2(d2)\n\n#         d1 = self.Conv_1x1(d2)\n# #         d1 = self.Sigmoid(d1)\n\n#         return d1","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:58:27.515817Z","iopub.execute_input":"2022-07-04T17:58:27.516223Z","iopub.status.idle":"2022-07-04T17:58:27.538577Z","shell.execute_reply.started":"2022-07-04T17:58:27.51618Z","shell.execute_reply":"2022-07-04T17:58:27.537845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class _model_args():\n    def __init__(self,moco,model,freeze_backbone,ckpt_path):\n        super(_model_args, self).__init__()\n        self.moco = True\n        self.model = model\n        self.pretrained = False\n        self.fine_tuning = freeze_backbone\n        self.model_uncertainty = False\n        self.ckpt_path = ckpt_path\n\ndef _load_model(ckpt_path, device, model_args, num_out, freeze_backbone,model_name):\n\n        if model_name==\"resnet18\":\n            model = torchvision.models.resnet18(pretrained=True)\n            cl = 512\n        elif model_name==\"resnet50\":\n            model = torchvision.models.resnet50(pretrained=True)\n            cl = 2048\n\n        ckpt_dict = torch.load(ckpt_path, map_location=device)\n\n        if not model_args.moco:\n            model_fn = models.__dict__[ckpt_dict['model_name']]\n        else:\n            s = ckpt_dict['arch']\n            model_fn = models.__dict__[s]\n\n        tasks = list(range(20))\n        model = model_fn(tasks, model_args)\n\n        if not model_args.moco:\n            model.load_state_dict(ckpt_dict['model_state'])\n        else:\n            state_dict = ckpt_dict['state_dict']\n            for k in list(state_dict.keys()):\n                # retain only encoder_q up to before the embedding layer\n                if k.startswith('module.encoder_q') and not k.startswith('module.encoder_q.fc'):\n                    # remove prefix\n                    # state_dict[k[len(\"module.encoder_q.\"):]] = state_dict[k]\n                    state_dict[k[len(\"module.encoder_q.\"):]] = state_dict[k]\n                    # delete renamed or unused k\n                    del state_dict[k]\n                elif 'encoder_k' in k or 'module.queue' in k:\n                    del state_dict[k]\n                elif k.startswith('module.encoder_q.fc'):\n                    # if 'fc.0' not in k:\n                    #     state_dict['module.model.fc' + k[len(\"module.encoder_q.fc.2\"):]] = state_dict[k]\n                    # TODO: JBY these are bad\n                    del state_dict[k]\n\n            model.load_state_dict(state_dict, strict=False)\n\n        if num_out == None:\n            return model.to(device)\n\n\n        model.fc = torch.nn.Linear(cl, num_out)\n        model.fc = model.fc.to(device)\n\n        if freeze_backbone:\n            for param in list(model.parameters())[:-2]:\n                param.requires_grad = False\n\n\n        model = model.to(device)\n\n        return model\n\ndef _medaug(model=\"resnet50\",pretrained_on=\"mimic-cxr\",device=torch.device(\"cuda\"),freeze_backbone=False,num_out=None, ckpt=''):\n\n    assert isinstance(device,torch.device), \"device has to be a type torch.device, use torch.device('\"'cuda:0'\"' if torch.cuda.is_available() else '\"'cpu'\"') before calling medaug function\"\n\n    model_params = _model_args(moco=False,model=model,freeze_backbone=freeze_backbone,ckpt_path=ckpt)\n\n    model = _load_model(ckpt_path=ckpt,device = device,model_args=model_params,num_out=num_out,freeze_backbone=freeze_backbone,model_name=model)\n    model = model.to(device)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:22:16.222107Z","iopub.execute_input":"2022-07-05T19:22:16.222916Z","iopub.status.idle":"2022-07-05T19:22:16.242478Z","shell.execute_reply.started":"2022-07-05T19:22:16.222886Z","shell.execute_reply":"2022-07-05T19:22:16.240399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ConvBlock(nn.Module):\n    \"\"\"\n    Helper module that consists of a Conv -> BN -> ReLU\n    \"\"\"\n\n    def __init__(self, in_channels, out_channels, padding=1, kernel_size=3, stride=1, with_nonlinearity=True):\n        super().__init__()\n        self.conv = nn.Conv2d(in_channels, out_channels, padding=padding, kernel_size=kernel_size, stride=stride)\n        self.bn = nn.BatchNorm2d(out_channels)\n        self.relu = nn.ReLU()\n        self.with_nonlinearity = with_nonlinearity\n\n    def forward(self, x):\n        x = self.conv(x)\n        x = self.bn(x)\n        if self.with_nonlinearity:\n            x = self.relu(x)\n        return x\n\n\nclass Bridge(nn.Module):\n    \"\"\"\n    This is the middle layer of the UNet which just consists of some\n    \"\"\"\n\n    def __init__(self, in_channels, out_channels):\n        super().__init__()\n        self.bridge = nn.Sequential(\n            ConvBlock(in_channels, out_channels),\n            ConvBlock(out_channels, out_channels)\n        )\n\n    def forward(self, x):\n        return self.bridge(x)\n\n\nclass UpBlockForUNetWithResNet50(nn.Module):\n    \"\"\"\n    Up block that encapsulates one up-sampling step which consists of Upsample -> ConvBlock -> ConvBlock\n    \"\"\"\n\n    def __init__(self, in_channels, out_channels, up_conv_in_channels=None, up_conv_out_channels=None,\n                 upsampling_method=\"conv_transpose\"):\n        super().__init__()\n\n        if up_conv_in_channels == None:\n            up_conv_in_channels = in_channels\n        if up_conv_out_channels == None:\n            up_conv_out_channels = out_channels\n\n        if upsampling_method == \"conv_transpose\":\n            self.upsample = nn.ConvTranspose2d(up_conv_in_channels, up_conv_out_channels, kernel_size=2, stride=2)\n        elif upsampling_method == \"bilinear\":\n            self.upsample = nn.Sequential(\n                nn.Upsample(mode='bilinear', scale_factor=2),\n                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1)\n            )\n        self.conv_block_1 = ConvBlock(in_channels, out_channels)\n        self.conv_block_2 = ConvBlock(out_channels, out_channels)\n\n    def forward(self, up_x, down_x):\n        \"\"\"\n        :param up_x: this is the output from the previous up block\n        :param down_x: this is the output from the down block\n        :return: upsampled feature map\n        \"\"\"\n        x = self.upsample(up_x)\n        x = torch.cat([x, down_x], 1)\n        x = self.conv_block_1(x)\n        x = self.conv_block_2(x)\n        return x\n\n\nclass UNetWithResnet50Encoder(nn.Module):\n    DEPTH = 6\n\n    def __init__(self, n_classes=1, checkpoint_pth='../input/nihcsvdata/medaug_chexpert_resnet50.pth.tar'):\n        super().__init__()\n        resnet = _medaug(pretrained_on=True,ckpt=checkpoint_pth)\n        down_blocks = []\n        up_blocks = []\n        self.input_block = nn.Sequential(*list(resnet.children()))[:3]\n        self.Sigmoid = nn.Sigmoid()\n        self.input_pool = list(resnet.children())[3]\n        for bottleneck in list(resnet.children()):\n            if isinstance(bottleneck, nn.Sequential):\n                down_blocks.append(bottleneck)\n        self.down_blocks = nn.ModuleList(down_blocks)\n        self.bridge = Bridge(2048, 2048)\n        up_blocks.append(UpBlockForUNetWithResNet50(2048, 1024))\n        up_blocks.append(UpBlockForUNetWithResNet50(1024, 512))\n        up_blocks.append(UpBlockForUNetWithResNet50(512, 256))\n        up_blocks.append(UpBlockForUNetWithResNet50(in_channels=128 + 64, out_channels=128,\n                                                    up_conv_in_channels=256, up_conv_out_channels=128))\n        up_blocks.append(UpBlockForUNetWithResNet50(in_channels=64 + 3, out_channels=64,\n                                                    up_conv_in_channels=128, up_conv_out_channels=64))\n\n        self.up_blocks = nn.ModuleList(up_blocks)\n        \n        self.out = nn.Conv2d(64, n_classes, kernel_size=1, stride=1)\n\n    def forward(self, x, with_output_feature_map=False):\n        x = torch.cat((x,x,x),dim=1)\n        pre_pools = dict()\n        pre_pools[f\"layer_0\"] = x\n        x = self.input_block(x)\n        pre_pools[f\"layer_1\"] = x\n        x = self.input_pool(x)\n\n        for i, block in enumerate(self.down_blocks, 2):\n            x = block(x)\n            if i == (UNetWithResnet50Encoder.DEPTH - 1):\n                continue\n            pre_pools[f\"layer_{i}\"] = x\n\n        x = self.bridge(x)\n\n        for i, block in enumerate(self.up_blocks, 1):\n            key = f\"layer_{UNetWithResnet50Encoder.DEPTH - 1 - i}\"\n            x = block(x, pre_pools[key])\n        output_feature_map = x\n        x = self.out(x)\n        del pre_pools\n        if with_output_feature_map:\n            return self.Sigmoid(x), output_feature_map\n        else:\n            \n            return self.Sigmoid(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:22:17.606522Z","iopub.execute_input":"2022-07-05T19:22:17.607198Z","iopub.status.idle":"2022-07-05T19:22:17.636449Z","shell.execute_reply.started":"2022-07-05T19:22:17.607165Z","shell.execute_reply":"2022-07-05T19:22:17.635439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = UNetWithResnet50Encoder(checkpoint_pth = '../input/nihcsvdata/medaug_chexpert_resnet50.pth.tar')\n# model = ResU_Net()\nmodel = model.cuda()\n# criterion=nn.BCELoss()\n# weight_tensor = torch.full((1280,1280), 4095)\ncriterion = nn.BCEWithLogitsLoss(pos_weight = torch.tensor([4095]))\n# criterion=nn.MSELoss()\ncriterion = criterion.cuda()\n\n# try dice loss \n\n\noptimizer = torch.optim.Adam(model.parameters(), lr=0.0001)\nwith torch.no_grad():\n\n    g = torch.rand(1,1,768,1472)\n    g = g.cuda()\n    out = model(g)\n    print(out.shape)\n#     hello = criterion(torch.rand(1,1,1280,1280).cuda(), torch.rand(1,1,1280,1280).cuda())\n#     print(hello)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:31:03.600371Z","iopub.execute_input":"2022-07-05T19:31:03.601907Z","iopub.status.idle":"2022-07-05T19:31:07.227381Z","shell.execute_reply.started":"2022-07-05T19:31:03.60186Z","shell.execute_reply":"2022-07-05T19:31:07.226244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 0\nrandom.seed(seed)\nnp.random.seed(seed)\ntorch.manual_seed(0)\n\ndef calculate_metrics_indiv(probability_map, code, combined_df, gt):\n    probability_map = probability_map.cpu().detach().numpy()\n    gt = gt.cpu().detach().numpy()\n\n    coords = np.array(np.unravel_index(np.argmax(probability_map, axis=None), probability_map.shape))\n    gt = np.array(np.unravel_index(np.argmax(gt, axis=None), gt.shape))\n\n    combined_list = combined_df['StudyInstanceUID'].tolist()\n    ind = combined_list.index(code)\n\n    x_conversion = combined_df['Original Image Pixel Spacing x'][int(ind)]\n    y_conversion = combined_df['Original Image Pixel Spacing y'][int(ind)]\n    coords[0] = coords[0] * x_conversion\n    coords[1] = coords[1] * y_conversion\n    gt[0] = gt[0] * x_conversion\n    gt[1] = gt[1] * y_conversion\n\n    abs_error = np.linalg.norm(coords - gt)/10\n\n    return abs_error\n\n# Train/val\n\ntrain_global_losses = []\nval_global_losses = []\ntrain_global_dists = []\nval_global_dists = []\ntrain_global_1cm = []\nval_global_1cm = []\ntrain_global_2cm = []\nval_global_2cm = []\n\nfor epoch in range(0, 5):\n    \n    # Declare lists to keep track of losses and metrics within the epoch\n    train_epoch_losses = []\n    train_1cm = []\n    train_2cm = []\n    val_epoch_losses = []\n    train_epoch_dists = []\n    val_epoch_dists = []\n    val_1cm = []\n    val_2cm = []\n    \n    model.train()\n\n    count = 0\n\n    for i, batch in enumerate(Train_dataloader):\n\n        x, y, img_uid = batch['image'], batch['gt'], batch['img_uid']\n        y = y.to(torch.float)\n        x, y = x.cuda(), y.cuda()\n        logits = model(x)\n        carina_coords_map = batch['ett_coord_map']\n\n        loss = criterion(logits[:,0],y)\n        train_epoch_losses.append(loss)\n        \n        # compute metrics for every image in batch\n        for i in range(0,len(x)):\n\n            carina_abs_error = calculate_metrics_indiv(logits[i,0],img_uid[i],combined_df, carina_coords_map[i])\n            train_epoch_dists.append(carina_abs_error)\n            if carina_abs_error <= 1:\n                train_1cm.append(1)\n            else:\n                train_1cm.append(0)\n            \n            if carina_abs_error <= 2:\n                train_2cm.append(1)\n            else:\n                train_2cm.append(0)\n\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n#         break\n\n    # Transition to val mode\n    model.eval()\n\n    # Avoid computing gradients during validation to save memory\n    with torch.no_grad():\n        count = 0\n\n        for i, batch in enumerate(Val_dataloader):\n            x, y, img_uid = batch['image'], batch['gt'], batch['img_uid']\n            y = y.to(torch.float)\n            x, y = x.cuda(), y.cuda()\n            logits = model(x)\n            carina_coords_map = batch['ett_coord_map']\n\n            loss = criterion(logits[:,0],y)\n            val_epoch_losses.append(loss)\n\n            # compute metrics for every image in batch\n            for i in range(0,len(x)):\n\n                carina_abs_error = calculate_metrics_indiv(logits[i,0],img_uid[i],combined_df, carina_coords_map[i])\n                val_epoch_dists.append(carina_abs_error)\n                if carina_abs_error <= 1:\n                    val_1cm.append(1)\n                else:\n                    val_1cm.append(0)\n\n                if carina_abs_error <= 2:\n                    val_2cm.append(1)\n                else:\n                    val_2cm.append(0)\n#             break\n                    \n    train_net_loss = sum(train_epoch_losses) / len(train_epoch_losses)\n    val_net_loss = sum(val_epoch_losses) / len(val_epoch_losses)\n    train_global_losses.append(train_net_loss)\n    val_global_losses.append(val_net_loss)\n    \n    train_net_dists = sum(train_epoch_dists) / len(train_epoch_dists)\n    val_net_dists = sum(val_epoch_dists) / len(val_epoch_dists)\n    train_global_dists.append(train_net_dists)\n    val_global_dists.append(val_net_dists)\n    \n    train_net_1cm = sum(train_1cm)/len(train_1cm)\n    train_net_2cm = sum(train_2cm)/len(train_2cm)\n    train_global_1cm.append(train_net_1cm)\n    train_global_2cm.append(train_net_2cm)\n    \n    val_net_1cm = sum(val_1cm)/len(val_1cm)\n    val_net_2cm = sum(val_2cm)/len(val_2cm)\n    val_global_1cm.append(val_net_1cm)\n    val_global_2cm.append(val_net_2cm)\n    \n# \n    print('Epoch: {} | Train Loss: {} | Val Loss: {} | Train Dist: {} | Val Dist: {} | train 1cm: {} | train 2cm: {} | val 1cm: {} | val 2cm: {} |'.format(epoch, train_net_loss, val_net_loss,train_net_dists, val_net_dists, train_net_1cm, train_net_2cm, val_net_1cm, val_net_2cm))\n#     print('Epoch: {} | Train Loss: {} | Train Dist: {} |'.format(epoch, train_net_loss,train_net_dists))\n\n    checkpoint_dir = './'\n    # Save the model if it reaches a new min validation loss\n#     if val_global_losses[-1] == min(val_global_losses):\n    if val_global_dists[-1] == min(val_global_dists):\n#     if train_global_losses[-1] == min(train_global_losses):\n\n        print('saving model at the end of epoch ' + str(epoch))\n        if epoch >= 0:\n            best_epoch = epoch\n            file_name = os.path.join(checkpoint_dir, 'model_epoch_{}.pth'.format(epoch))\n            torch.save({\n                'epoch': epoch,\n                'state_dict': model.state_dict(),\n                'optim_dict': optimizer.state_dict(),\n                },\n                file_name)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T18:26:26.477984Z","iopub.execute_input":"2022-07-05T18:26:26.47836Z","iopub.status.idle":"2022-07-05T18:50:47.289763Z","shell.execute_reply.started":"2022-07-05T18:26:26.478324Z","shell.execute_reply":"2022-07-05T18:50:47.288338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 0\nrandom.seed(seed)\nnp.random.seed(seed)\ntorch.manual_seed(0)\n\n# Test\n\nbest_epoch = np.argmin(np.array(val_global_dists))\n# best_epoch = np.argmin(np.array(train_global_losses))\n\n# model = ResU_Net()\nmodel = UNetWithResnet50Encoder(checkpoint_pth = '../input/nihcsvdata/medaug_chexpert_resnet50.pth.tar')\n\nload_dir = './model_epoch_' + str(best_epoch) + '.pth'\n# load_dir = './model_epoch_' + '195' + '.pth'\n\ncheckpoint = torch.load(load_dir)\nmodel.load_state_dict(checkpoint['state_dict'])\nmodel.cuda()\noutput_map = []\ngts = []\ncodes = []\nett_coords_map = []\n\nlosses = []\ndists = []\nval_1cm = []\nval_2cm = []\n\nwith torch.no_grad():\n    count = 0\n    for i, batch in enumerate(Val_dataloader):\n        x, y, img_uid = batch['image'], batch['gt'], batch['img_uid']\n        y = y.to(torch.float)\n        x, y = x.cuda(), y.cuda()\n        logits = model(x)\n        carina_coords_map = batch['ett_coord_map']\n\n        loss = criterion(logits[:,0],y)\n        losses.append(loss)\n        output_map.append(logits[:,0])\n        gts.append(y)\n        codes.append(img_uid)\n\n        # compute metrics for every image in batch\n        for i in range(0,len(x)):\n\n            carina_abs_error = calculate_metrics_indiv(logits[i,0],img_uid[i],combined_df, carina_coords_map[i])\n            dists.append(carina_abs_error)\n            if carina_abs_error <= 1:\n                val_1cm.append(1)\n            else:\n                val_1cm.append(0)\n\n            if carina_abs_error <= 2:\n                val_2cm.append(1)\n            else:\n                val_2cm.append(0)\n\nnet_loss = sum(losses)/len(losses)\nnet_dist = sum(dists)/len(dists)\ncm1_acc = sum(val_1cm)/len(val_1cm)\ncm2_acc = sum(val_2cm)/len(val_2cm)\n\nprint('net loss: {}, net dist: {}, 1cm acc: {}, 2 cm acc: {}'.format(net_loss, net_dist, cm1_acc, cm2_acc))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T18:50:47.291225Z","iopub.status.idle":"2022-07-05T18:50:47.291925Z","shell.execute_reply.started":"2022-07-05T18:50:47.291692Z","shell.execute_reply":"2022-07-05T18:50:47.291717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dists)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T18:08:02.747686Z","iopub.execute_input":"2022-07-04T18:08:02.748076Z","iopub.status.idle":"2022-07-04T18:08:02.93577Z","shell.execute_reply.started":"2022-07-04T18:08:02.748039Z","shell.execute_reply":"2022-07-04T18:08:02.935015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_center(img,cropx,cropy):\n        y,x = img.shape\n        startx = x//2-(cropx//2)\n        # crop closer to top\n        return img[0:cropy,startx:startx+cropx]\n\ndef str2array(s):\n        # Remove space after [\n        s=re.sub('\\[ +', '[', s.strip())\n        # Replace commas and spaces\n        s=re.sub('[,\\s]+', ', ', s)\n        return np.array(ast.literal_eval(s))\n    \n    \n# print(codes[0])\ncur_img = codes[0][0] + '.jpg'\ntrain_dir = '../input/ranzcr-clip-catheter-line-classification/train/'\nimage_filepath = train_dir + cur_img\nimg = cv2.imread(image_filepath, 0)\noriginal_imsize = img.shape\nimage = ToTensor()(img)\nimage = 2*(image/torch.max(image))-1\nimage = image[0]\n\nids = ett_annotations['StudyInstanceUID'].tolist()\nindex_ett = ids.index(codes[0][0])\nxs = (ett_annotations['data'][index_ett])\npoints = str2array(xs)\ny_coords = points[:,1]\nlowest_y = np.argmax(y_coords)\nx, y = points[lowest_y]\n\nprint(x)\nprint(y)\nimage[y-20:y+20, x-20:x+20] = 1\n\nimage = crop_center(image,1280,1280)\n\nplt.imshow(image, interpolation='nearest', cmap='Greys')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T18:09:27.345084Z","iopub.execute_input":"2022-07-04T18:09:27.345895Z","iopub.status.idle":"2022-07-04T18:09:27.633711Z","shell.execute_reply.started":"2022-07-04T18:09:27.345858Z","shell.execute_reply":"2022-07-04T18:09:27.632958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cur_output = output_map[0][0].cpu().detach().numpy()\nplt.imshow(cur_output, interpolation='nearest', cmap='Greys')","metadata":{"execution":{"iopub.status.busy":"2022-07-04T18:09:31.201062Z","iopub.execute_input":"2022-07-04T18:09:31.20172Z","iopub.status.idle":"2022-07-04T18:09:31.408067Z","shell.execute_reply.started":"2022-07-04T18:09:31.201685Z","shell.execute_reply":"2022-07-04T18:09:31.407359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Test\n\n# # best_epoch = np.argmin(np.array(val_global_losses))\n# best_epoch = np.argmin(np.array(train_global_losses))\n\n# model = ResU_Net()\n\n# # load_dir = './model_epoch_' + str(best_epoch) + '.pth'\n# load_dir = './model_epoch_' + '195' + '.pth'\n\n# checkpoint = torch.load(load_dir)\n# model.load_state_dict(checkpoint['state_dict'])\n# model.cuda()\n# output_map = []\n# gts = []\n# codes = []\n# ett_coords_map = []\n\n# with torch.no_grad():\n#     count = 0\n#     for i, data in enumerate(Train_dataloader):\n#         input_img = data['Image']\n#         gt = data['GT']\n#         codes.append(data['Code'])\n#         ett_coords_map.append(data['ETT_coord_map'])        \n\n#         input_img = input_img.cuda()\n#         gt = gt.cuda()\n#         gt = gt.to(torch.float)\n#         gts.append(gt)\n        \n#         output = model(input_img)\n#         output_map.append(output[0].cpu().detach().numpy())\n        \n#         if count > 5:\n#             break\n        \n#         count += 1\n        \n","metadata":{"execution":{"iopub.status.busy":"2022-06-21T18:14:21.555303Z","iopub.execute_input":"2022-06-21T18:14:21.556141Z","iopub.status.idle":"2022-06-21T18:14:24.786278Z","shell.execute_reply.started":"2022-06-21T18:14:21.556102Z","shell.execute_reply":"2022-06-21T18:14:24.783647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Visualization (original test image with GT)\n\n# def crop_center(img,cropx,cropy):\n#     y,x = img.shape\n#     startx = x//2-(cropx//2)\n#     starty = y//3-(cropy//2)    \n#     return img[starty:starty+cropy,startx:startx+cropx]\n\n# ind = 1\n# test_path_1 = train_files[ind]\n# test_img = cv2.imread(test_path_1, 0)\n# test_img = 2*(test_img/np.max(test_img))-1\n# g = crop_center(test_img, 1280, 1280)\n# plt.imshow(g, interpolation='nearest', cmap='Greys')\n# gt = gts[ind].cpu().detach().numpy()\n# print(gt.shape)\n# plt.imshow(gt[0], cmap='Greys', alpha=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T17:52:49.53078Z","iopub.execute_input":"2022-06-21T17:52:49.53115Z","iopub.status.idle":"2022-06-21T17:52:49.96598Z","shell.execute_reply.started":"2022-06-21T17:52:49.531122Z","shell.execute_reply":"2022-06-21T17:52:49.96522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Visualization (test image output)\n\n# # map_1 = output_map[ind]\n# map_1 = output_map[ind]\n\n# # plt.imshow(g, interpolation='nearest', cmap='Greys')\n# # plt.imshow(map_1[0], cmap='Greys', alpha=0.3)\n# plt.imshow(map_1[0], cmap='Greys')","metadata":{"execution":{"iopub.status.busy":"2022-06-21T18:10:12.07585Z","iopub.execute_input":"2022-06-21T18:10:12.076332Z","iopub.status.idle":"2022-06-21T18:10:12.430318Z","shell.execute_reply.started":"2022-06-21T18:10:12.076294Z","shell.execute_reply":"2022-06-21T18:10:12.429559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def calculate_metrics_no_carina(probability_map, code, combined_df, ett_gt):\n#     et_tube = probability_map.cpu().detach().numpy()\n#     ett_gt = ett_gt.cpu().detach().numpy()\n\n#     et_tube_coords = np.array(np.unravel_index(np.argmax(et_tube, axis=None), et_tube.shape))\n#     ett_gt = np.array(np.unravel_index(np.argmax(ett_gt, axis=None), ett_gt.shape))\n\n#     combined_list = combined_df['StudyInstanceUID'].tolist()\n#     ind = combined_list.index(code)\n\n#     x_conversion = combined_df['Original Image Pixel Spacing x'][int(ind)]\n#     y_conversion = combined_df['Original Image Pixel Spacing y'][int(ind)]\n#     et_tube_coords[0] = et_tube_coords[0] * x_conversion\n#     et_tube_coords[1] = et_tube_coords[1] * y_conversion\n#     ett_gt[0] = ett_gt[0] * x_conversion\n#     ett_gt[1] = ett_gt[1] * y_conversion\n\n#     ett_abs_error = np.linalg.norm(et_tube_coords - ett_gt)/10\n\n#     if ett_abs_error <= 1:\n#         ett_correct = True\n#     else:\n#         ett_correct = False\n\n#     return ett_abs_error, ett_correct\n\n# print(calculate_metrics_no_carina(output_map[ind], codes[ind], combined_df, ett_coords_map[ind]))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T18:14:29.497371Z","iopub.execute_input":"2022-06-21T18:14:29.498051Z","iopub.status.idle":"2022-06-21T18:14:29.51724Z","shell.execute_reply.started":"2022-06-21T18:14:29.498013Z","shell.execute_reply":"2022-06-21T18:14:29.51618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-06-20T22:48:55.000419Z","iopub.execute_input":"2022-06-20T22:48:55.00093Z","iopub.status.idle":"2022-06-20T22:48:55.0134Z","shell.execute_reply.started":"2022-06-20T22:48:55.000884Z","shell.execute_reply":"2022-06-20T22:48:55.01253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# old dataloader\n\n# class Dataset():\n\n#     def __init__(self, file_list, mode, ett_annotations, carina_annotations):\n#         self.file_list = file_list\n#         self.mode = mode\n#         self.ett_annotations = ett_annotations\n#         self.carina_annotations = carina_annotations\n\n#     def __getitem__(self,idx):\n#         image_filepath = self.file_list[idx]\n#         image, code, original_imsize = self.loadimage(image_filepath)\n        \n#         gt_map, ett_coords = self.get_GT(code, original_imsize)\n# #         for i in range(0,len(image)):\n# #             image[i] = self.crop_center(image[i], ett_coords)\n        \n#         sample = {'Image': image,\n#                   'GT': gt_map,\n#                   'Code': code,\n#                   'ETT_coords': ett_coords,\n#               }\n\n#         return sample\n\n#     def __len__(self):\n#         return len(self.file_list)\n\n#     def loadimage(self, image_filepath):\n#         img = cv2.imread(image_filepath, 0)\n#         original_imsize = img.shape\n# #         img = self.crop_center(img, 1024, 1024)\n#         image = ToTensor()(img) \n#         image = 2*(image/torch.max(image))-1\n#         code = image_filepath.split('/')[4][:-4] \n#         return image, code, original_imsize\n    \n#     def get_GT(self, code, original_imsize):\n#         ett_annotations = self.ett_annotations\n#         ids = ett_annotations['StudyInstanceUID'].tolist()\n#         index_ett = ids.index(code) \n        \n#         xs = (ett_annotations['data'][index_ett])\n#         points = self.str2array(xs)\n#         y_coords = points[:,1]\n#         lowest_y = np.argmax(y_coords)\n#         gt_map_et = np.zeros([original_imsize[0], original_imsize[1]], dtype='float')\n#         x_ett, y_ett = points[lowest_y]\n#         ett_coords = np.array([x_ett,y_ett])\n#         X, Y = np.mgrid[x_ett-20:x_ett+20, y_ett-20:y_ett+20]\n#         coords_et = np.vstack((X.ravel(), Y.ravel()))\n#         for i in range(0,len(coords_et[0])):\n#             gt_map_et[coords_et[1][i]][coords_et[0][i]] = 1\n#         gt_map_et = self.crop_center(gt_map_et, ett_coords)\n\n#         return gt_map_et, ett_coords\n\n#     def crop_center(self,img,ett_coords):\n#         x,y = ett_coords[0], ett_coords[1]\n#         print(img.shape)\n#         x_im,y_im = img.shape\n#         if x - 512 >= 0:\n#             startx = x-512\n#         else:\n#             startx = 0\n#         if x + 512 < x_im:\n#             endx = x+512\n#         else:\n#             endx = x_im\n            \n#         if y - 100 >= 0:\n#             starty = y-100\n#         else:\n#             starty = 0\n#         if y + 924 < y_im:\n#             endy = y+924\n#         else:\n#             endy = y_im\n            \n#         return img[starty:endy,startx:endx]\n    \n#     def str2array(self, s):\n#         # Remove space after [\n#         s=re.sub('\\[ +', '[', s.strip())\n#         # Replace commas and spaces\n#         s=re.sub('[,\\s]+', ', ', s)\n#         return np.array(ast.literal_eval(s))\n ","metadata":{},"execution_count":null,"outputs":[]}]}