def __init__(self, root, arg): self.dataset_dir = os.path.join(root, cfg.dataset_path) self.dataset_dir = os.path.join(self.dataset_dir, 'Train') self.hd5_name = ( f"{cfg.hdf5_name}_d{arg.d}_path{arg.patch_size[0]}" f"_stride{arg.stride_size[0]}_heatmap{arg.heatmap_size[0]}" f"_version{arg.version}.hdf5") self.hd5_path = os.path.join(self.dataset_dir, self.hd5_name) self.transform = utils.transform(arg.version)
def test_model(image_path, data, model, arg): device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') img_transform = utils.transform(arg.version) model.train(False) _, _, _, stain, _ = steinseperation.stainsep(image_path) H_Channel = stain[0] cropped, coords = utils.patch_extraction(data, H_Channel, arg.patch_size, arg.stride_size, arg.version) H, W, _ = data.shape cell = np.zeros((H, W)) count = np.zeros((H, W)) [H_prime, W_prime] = arg.heatmap_size for img, coord in zip(cropped, coords): img = img_transform(np.uint8(img)) img = img.float() img = img.to(device) img = img.unsqueeze(0) point, h = model(img) heat_map = utils.heat_map_tensor(point.view(-1, 2), h.view(-1, 1), device, arg.d, arg.heatmap_size) heatmap = heat_map.cpu().detach().numpy().reshape((H_prime, W_prime)) start_H, end_H, start_W, end_W = utils.find_out_coords(coord, arg.patch_size, arg.heatmap_size) cell[start_H:end_H, start_W:end_W] += heatmap idx = np.argwhere(heatmap != 0) count[idx[:,0]+start_H, idx[:,1]+start_W] += 1 count[count==0] = 1 cell = np.divide(cell, count) return cell
def __init__(self, root, arg, file): self.imgs = [] self.coords_list = [] self.version = arg.version self.patch_size = arg.patch_size self.stride_size = arg.stride_size self.transform = utils.transform(arg.version) self.file = file self.dataset_dir = os.path.join(root, cfg.dataset_path) self.dataset_dir = os.path.join(self.dataset_dir, 'Test') self.Image_path = os.path.join(self.dataset_dir, cfg.image_path) self.Stain_path = os.path.join(self.dataset_dir, cfg.stain_path) self.process()
def print_values(img, model, arg): device = torch.device( 'cuda') if torch.cuda.is_available() else torch.device('cpu') img_transform = utils.transform(arg.version) img = img_transform(np.uint8(img)) img = img.float() img = img.to(device) img = img.unsqueeze(0) model.train(False) point, h = model(img) print('points :', point.cpu().detach().numpy().reshape((-1, 2))) print('H :', h.cpu().detach().numpy().reshape((-1, 1))) print(20 * '*') return utils.heat_map_tensor(point.view(-1, 2), h.view(-1, 1), device, arg.d, arg.heatmap_size)
def __init__(self, root, patch_size, stride_size, d, out_size, version): self.out_size = out_size self.imgs = [] self.heat_maps = [] self.epsilons = 0 self.transform = utils.transform(version) Image_path = root + cfg.dataset_path + cfg.image_path Center_path = root + cfg.dataset_path + cfg.center_path # MacOS thing :) utils.delete_file(Image_path, '.DS_Store') utils.delete_file(Center_path, '.DS_Store') # load all image files, sorting them to total_num = len(list(os.listdir(Image_path))) for idx, (img_name, center_file) in enumerate( zip(list(sorted(os.listdir(Image_path))), list(sorted(os.listdir(Center_path))))): # Check if the img and the centers are for same file assert img_name[: -4] == center_file[: -4], "The Image {} and Center name {} are not same!".format( img_name, center_file) img_path = os.path.join(Image_path, img_name) center_path = os.path.join(Center_path, center_file) # Load Image img = cv2.imread(img_path) # Obtaining H-Channel # Vahadane color normalization --> my implementation # of Matlab's version _, _, _, stain, _ = steinseperation.stainsep(img, 2, 0.02) H_Channel = stain[0] cropped, coords = utils.patch_extraction(img, H_Channel, patch_size, stride_size, version) # Reading Centers center_txt = open(center_path, "r") center = utils.read_center_txt(center_txt) # Extracting patches' centers patch_centers = utils.center_extraction(center, coords, patch_size, self.out_size) # Finding epsilon and heatmaps h_map, epsilon = utils.heat_map(patch_centers, coords, d, patch_size, self.out_size) self.imgs.extend(cropped) self.heat_maps.extend(h_map) self.epsilons += epsilon print(idx + 1, 'from', total_num, 'Images are Loaded!', sep=' ', end='\r', flush=True)