def __getitem__(self, index): seed = random.random() scale = random.choice(self.scale) new_image_size = (int(self.image_size[0] * scale), int(self.image_size[1] * scale)) # Load image and randomly resize and crop img = Image.open(self.image_list[index]) if self.mode == 'train': img = transforms.Resize(new_image_size, Image.BILINEAR)(img) img = RandomCrop(self.image_size, seed, pad_if_needed=True)(img) img = np.array(img) # Load label and randomly resize and crop like the image label = Image.open(self.label_list[index]) if self.mode == 'train': label = transforms.Resize(new_image_size, Image.NEAREST)(label) label = RandomCrop(self.image_size, seed, pad_if_needed=True)(label) label = np.array(label) # Augmentations if self.mode == 'train': if random.random() < 0.5: img, label = augmentation(img, label) if self.mode == 'train': if random.random() < 0.5: img = augmentation_pixel(img) # Convert image to tensor in CHW format img = Image.fromarray(img) img = self.to_tensor(img).float() # Convert label to tensor in CHW format if self.loss == 'dice': label = label[:, :, 0] label_with_camvid_indices = self.func(label) label = onehot_initialization_v2(label_with_camvid_indices) label = np.transpose(label, [2, 0, 1]).astype(np.float32) label = torch.from_numpy(label) return img, label elif self.loss == 'crossentropy': label = one_hot_it_v11(label, self.label_info).astype(np.uint8) label = torch.from_numpy(label).long() return img, label
def __getitem__(self, index): #open image and label img = Image.open(self.images[index]) label = Image.open(self.labels[index]).convert("RGB") #resize image and label, then crop them scale = random.choice(self.scale) scale = (int(self.shape[0] * scale), int(self.shape[1] * scale)) seed = random.random() img = transforms.Resize(scale, Image.BILINEAR)(img) img = RandomCrop(self.shape, seed, pad_if_needed=True)(img) img = np.array(img) label = transforms.Resize(scale, Image.NEAREST)(label) label = RandomCrop(self.shape, seed, pad_if_needed=True)(label) label = np.array(label) #translete to CamVid color palette label = self.__toCamVid(label) #apply augmentation img, label = augmentation(img, label) if random.randint(0, 1) == 1: img = augmentation_pixel(img) img = Image.fromarray(img) img = self.to_tensor(img).float() #computing losses if self.loss == 'dice': # label -> [num_classes, H, W] label = one_hot_it_v11_dice(label, self.label_info).astype(np.uint8) label = np.transpose(label, [2, 0, 1]).astype(np.float32) label = torch.from_numpy(label) return img, label elif self.loss == 'crossentropy': label = one_hot_it_v11(label, self.label_info).astype(np.uint8) label = torch.from_numpy(label).long() return img, label
def __getitem__(self, index): # load image and crop seed = random.random() img = Image.open(self.image_list[index]) # random crop image # ===================================== # w,h = img.size # th, tw = self.scale # i = random.randint(0, h - th) # j = random.randint(0, w - tw) # img = F.crop(img, i, j, th, tw) # ===================================== scale = random.choice(self.scale) scale = (int(self.image_size[0] * scale), int(self.image_size[1] * scale)) # randomly resize image and random crop # ===================================== if self.mode == 'train' or self.mode == 'adversarial_train': img = transforms.Resize(scale, Image.BILINEAR)(img) img = RandomCrop(self.image_size, seed, pad_if_needed=True)(img) # ===================================== img = np.array(img) if self.mode != 'adversarial_train': label = Image.open(self.label_list[index]) else: imarray = np.zeros(shape=(2, 2, 4)) * 255 label = Image.fromarray(imarray.astype('uint8')).convert('RGBA') # crop the corresponding label # ===================================== # label = F.crop(label, i, j, th, tw) # ===================================== # randomly resize label and random crop # ===================================== if self.mode == 'train' or self.mode == 'adversarial_train': label = transforms.Resize(scale, Image.NEAREST)(label) label = RandomCrop(self.image_size, seed, pad_if_needed=True)(label) label = np.array(label) # augment image and label if self.mode == 'train' or self.mode == 'adversarial_train': if random.random() < 0.5: img, label = augmentation(img, label) # augment pixel image if self.mode == 'train' or self.mode == 'adversarial_train': # set a probability of 0.5 if random.random() < 0.5: img = augmentation_pixel(img) # image -> [C, H, W] img = Image.fromarray(img) img = self.to_tensor(img).float() if self.loss == 'dice': # label -> [num_classes, H, W] if self.mode != 'adversarial_train': label = one_hot_it_v11_dice(label, self.label_info).astype(np.uint8) label = np.transpose(label, [2, 0, 1]).astype(np.float32) label = torch.from_numpy(label) return img, label elif self.loss == 'crossentropy': label = one_hot_it_v11(label, self.label_info).astype(np.uint8) # label = label.astype(np.float32) label = torch.from_numpy(label).long() return img, label
def __getitem__(self, index): # load image and crop seed = random.random() img = Image.open(self.image_list[index]) # random crop image # ===================================== # w,h = img.size # th, tw = self.scale # i = random.randint(0, h - th) # j = random.randint(0, w - tw) # img = F.crop(img, i, j, th, tw) # ===================================== scale = random.choice(self.scale) scale = (int(self.image_size[0] * scale), int(self.image_size[1] * scale)) # randomly resize image and random crop # ===================================== if self.mode == 'train': img = transforms.Resize(scale, Image.BILINEAR)(img) img = RandomCrop(self.image_size, seed, pad_if_needed=True)(img) # ===================================== img = np.array(img) # load label label = Image.open(self.label_list[index]) # crop the corresponding label # ===================================== # label = F.crop(label, i, j, th, tw) # ===================================== # randomly resize label and random crop # ===================================== if self.mode == 'train': label = transforms.Resize(scale, Image.NEAREST)(label) label = RandomCrop(self.image_size, seed, pad_if_needed=True)(label) # ===================================== label = np.array(label) # augment image and label if self.mode == 'train': seq_det = self.fliplr.to_deterministic() img = seq_det.augment_image(img) label = seq_det.augment_image(label) # image -> [C, H, W] img = Image.fromarray(img) img = self.to_tensor(img).float() if self.loss == 'dice': # label -> [num_classes, H, W] label = one_hot_it_v11_dice(label, self.label_info).astype(np.uint8) label = np.transpose(label, [2, 0, 1]).astype(np.float32) # label = label.astype(np.float32) label = torch.from_numpy(label) return img, label elif self.loss == 'crossentropy': label = one_hot_it_v11(label, self.label_info).astype(np.uint8) # label = label.astype(np.float32) label = torch.from_numpy(label).long() return img, label