def generate_samples(self): total_imgs = len(self.list_imgs) print('Total ' + self.mode + ' data to generate samples:', total_imgs) print('Mode: ' + self.mode + ' 2d sub grids samples to generate: ', self.samples, ' Input images: ', total_imgs) for j in range(total_imgs): input_path = self.list_imgs[j] label_path = self.list_labels[j] img_numpy = img_loader.load_2d_image(input_path, type="RGB") label_numpy = img_loader.load_2d_image(label_path, type='LA') for i in range(self.per_image_sample): h_crop, w_crop = self.generate_patch(img_numpy) img_cropped = img_numpy[h_crop:(h_crop + self.crop_dim[0]), w_crop:(w_crop + self.crop_dim[1]), :] label_cropped = label_numpy[h_crop:(h_crop + self.crop_dim[0]), w_crop:(w_crop + self.crop_dim[1])] img_tensor = torch.from_numpy(img_cropped).float() label_tensor = torch.from_numpy(label_cropped) img_tensor = img_tensor.permute(2, 0, 1) img_tensor = self.norm_img(img_tensor) if self.save: filename = self.sub_vol_path + 'id_' + str( j) + '_s_' + str(i) + '_' f_1 = filename + 'input.npy' f_2 = filename + 'label.npy' np.save(f_1, img_tensor) np.save(f_2, label_tensor) self.sample_list.append(tuple((f_1, f_2))) else: self.sample_list.append(tuple((img_tensor, label_tensor)))
def load_image(self, img_path, resize_dim): if not os.path.exists(img_path): print("IMAGE DOES NOT EXIST {}".format(img_path)) image = img_loader.load_2d_image(img_path, resize_dim) t = transforms.ToTensor() norm = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[1, 1, 1]) return norm(t(image))
def check_path_in_list(path, list): """ Checks a path if exist in the other list """ key = path.split('/')[-1] for full_path in list: path_id = full_path.split('/')[-1] if path_id == key: image_numpy = img_loader.load_2d_image(full_path, type="LA") return image_numpy return None
def preprocess_labels(maps_tuple): """ Majority labeling vote to produce ground truth labels """ label_list = [] m1, m2, m3, m4, m5, m6 = maps_tuple for j in range(len(m5)): path = m5[j] # as a reference annotation key = path.split('/')[-1] image_list = [] # voter 5 image_list.append(img_loader.load_2d_image(path, type="LA")) # voter 1 image = check_path_in_list(path, m1) if image is not None: image_list.append(image) # voter 2 image = check_path_in_list(path, m2) if image is not None: image_list.append(image) # voter 3 image = check_path_in_list(path, m3) if image is not None: image_list.append(image) # voter 4 image = check_path_in_list(path, m4) if image is not None: image_list.append(image) # voter 6 image = check_path_in_list(path, m6) if image is not None: image_list.append(image) stacked_labels = np.stack(image_list, axis=0) label = vote(stacked_labels)