def process_cam(cam, model_id, model, img_NAC, device): if model_id == 1: # gating signals of all attention gates of att resnet18_unet were replaced by Grad-CAM cam1 = np.float32(resize_sitk_2D(cam, (24, 24))) cam2 = np.float32(resize_sitk_2D(cam, (48, 48))) cam3 = np.float32(resize_sitk_2D(cam, (96, 96))) cam4 = np.float32(resize_sitk_2D(cam, (192, 192))) cam1 = np.expand_dims(np.expand_dims(cam1, axis=0), axis=0) cam2 = np.expand_dims(np.expand_dims(cam2, axis=0), axis=0) cam3 = np.expand_dims(np.expand_dims(cam3, axis=0), axis=0) cam4 = np.expand_dims(np.expand_dims(cam4, axis=0), axis=0) cam1 = torch.from_numpy(cam1).to(device) cam2 = torch.from_numpy(cam2).to(device) cam3 = torch.from_numpy(cam3).to(device) cam4 = torch.from_numpy(cam4).to(device) pred = model(img_NAC, cam1, cam2, cam3, cam4) return pred elif model_id == 2: # gating signal of 4th attention gate of att resnet18_unet was replaced by Grad-CAM cam = np.float32(resize_sitk_2D(cam, (192, 192))) cam = np.expand_dims(np.expand_dims(cam, axis=0), axis=0) cam = torch.from_numpy(cam).to(device) pred = model(img_NAC, cam) return pred
def store_cam(tensor_img_AC, tensor_img_NAC, output_dir, time_stamp, model_name, slice_id): grads_val = grad_block[0].cpu().data.numpy().squeeze(0) fmap = fmap_block[0].cpu().data.numpy().squeeze(0) cam = gen_cam(fmap, grads_val) cam = np.float32(resize_sitk_2D(cam, (344, 344))) tensor_img_NAC = tensor_img_NAC.cpu().data.numpy().squeeze() tensor_img_NAC = np.float32(resize_sitk_2D(tensor_img_NAC, (344, 344))) tensor_img_AC = tensor_img_AC.cpu().data.numpy().squeeze() tensor_img_AC = np.float32(resize_sitk_2D(tensor_img_AC, (344, 344))) show_cam_on_image(tensor_img_AC, tensor_img_NAC, cam, output_dir, time_stamp, model_name, slice_id) print('Attention Map of', str(model_name), 'with slice_id', str(slice_id), 'is saved!')
def load_data(volume_path, slice_id): volume = sitk.ReadImage(volume_path) img = volume[:, :, slice_id] np_img = sitk.GetArrayFromImage(img) resize_img = resize_sitk_2D(np_img, (384, 384)) transform = transforms.Compose([transforms.ToTensor()]) tensor_img = transform(resize_img) tensor_img = tensor_img.unsqueeze(0) return tensor_img
def process_cam(cam, model_id, model, img_NAC, device): if model_id == 1: cam1 = np.float32(resize_sitk_2D(cam, (48, 48))) cam2 = np.float32(resize_sitk_2D(cam, (96, 96))) cam3 = np.float32(resize_sitk_2D(cam, (192, 192))) cam4 = np.float32(resize_sitk_2D(cam, (384, 384))) cam1 = np.expand_dims(np.expand_dims(cam1, axis=0), axis=0) cam2 = np.expand_dims(np.expand_dims(cam2, axis=0), axis=0) cam3 = np.expand_dims(np.expand_dims(cam3, axis=0), axis=0) cam4 = np.expand_dims(np.expand_dims(cam4, axis=0), axis=0) cam1 = torch.from_numpy(cam1).to(device) cam2 = torch.from_numpy(cam2).to(device) cam3 = torch.from_numpy(cam3).to(device) cam4 = torch.from_numpy(cam4).to(device) pred = model(img_NAC, cam1, cam2, cam3, cam4) return pred elif model_id == 2: cam = np.float32(resize_sitk_2D(cam, (384, 384))) cam = np.expand_dims(np.expand_dims(cam, axis=0), axis=0) cam = torch.from_numpy(cam).to(device) pred = model(img_NAC, cam) return pred elif model_id == 3: cam = np.float32(resize_sitk_2D(cam, (48, 48))) cam = np.expand_dims(np.expand_dims(cam, axis=0), axis=0) cam = torch.from_numpy(cam).to(device) pred = model(img_NAC, cam) return pred elif model_id == 4: cam1 = np.float32(resize_sitk_2D(cam, (48, 48))) cam2 = np.float32(resize_sitk_2D(cam, (96, 96))) cam1 = np.expand_dims(np.expand_dims(cam1, axis=0), axis=0) cam2 = np.expand_dims(np.expand_dims(cam2, axis=0), axis=0) cam1 = torch.from_numpy(cam1).to(device) cam2 = torch.from_numpy(cam2).to(device) pred = model(img_NAC, cam1, cam2) return pred elif model_id == 5: cam1 = np.float32(resize_sitk_2D(cam, (48, 48))) cam2 = np.float32(resize_sitk_2D(cam, (96, 96))) cam3 = np.float32(resize_sitk_2D(cam, (192, 192))) cam1 = np.expand_dims(np.expand_dims(cam1, axis=0), axis=0) cam2 = np.expand_dims(np.expand_dims(cam2, axis=0), axis=0) cam3 = np.expand_dims(np.expand_dims(cam3, axis=0), axis=0) cam1 = torch.from_numpy(cam1).to(device) cam2 = torch.from_numpy(cam2).to(device) cam3 = torch.from_numpy(cam3).to(device) pred = model(img_NAC, cam1, cam2, cam3) return pred