def __call__(self, input, target): ## do something to both images input = Resize((1086, 1351), Image.BILINEAR)(input) target = Resize((1086, 1351), Image.NEAREST)(target) #input = Resize((512,1024), Image.BILINEAR)(input) #target = Resize((512,1024),Image.NEAREST)(target) if (self.augment): rotation_degree = 1 shear_degree = 1 input = RandomAffine(rotation_degree, None, None, shear_degree, resample=Image.BILINEAR, fillcolor=0)(input) target = RandomAffine(rotation_degree, None, None, shear_degree, resample=Image.NEAREST, fillcolor=255)(target) w, h = input.size nratio = random.uniform(0.5, 1.0) ni = random.randint(0, int(h - nratio * h)) nj = random.randint(0, int(w - nratio * w)) input = input.crop( (nj, ni, int(nj + nratio * w), int(ni + nratio * h))) target = target.crop( (nj, ni, int(nj + nratio * w), int(ni + nratio * h))) input = Resize((512, 1024), Image.BILINEAR)(input) target = Resize((512, 1024), Image.NEAREST)(target) brightness = 0.1 contrast = 0.1 saturation = 0.1 hue = 0.1 input = ColorJitter(brightness, contrast, saturation, hue)(input) hflip = random.random() if (hflip < 0.5): input = input.transpose(Image.FLIP_LEFT_RIGHT) target = target.transpose(Image.FLIP_LEFT_RIGHT) else: input = Resize((512, 1024), Image.BILINEAR)(input) target = Resize((512, 1024), Image.NEAREST)(target) input = ToTensor()(input) if (self.enc): target = Resize((64, 128), Image.NEAREST)(target) target = ToLabel()(target) target = Relabel(255, 27)(target) return input, target
def __call__(self, input, target): # do something to both images input = Scale(self.height, Image.BILINEAR)(input) target = Scale(self.height, Image.NEAREST)(target) if(self.augment): # Random hflip hflip = random.random() if (hflip < 0.5): input = input.transpose(Image.FLIP_LEFT_RIGHT) target = target.transpose(Image.FLIP_LEFT_RIGHT) input = ToTensor()(input) target = ToLabel()(target) for iter in range(1,19): target = Relabel(iter, 255)(target) target = Relabel(19, 1)(target) target = Relabel(20, 2)(target) target = Relabel(21, 3)(target) target = Relabel(22, 4)(target) target = Relabel(23, 5)(target) target = Relabel(24, 6)(target) target = Relabel(25, 7)(target) target = Relabel(26, 8)(target) target = Relabel(27, 9)(target) return input, target
def __call__(self, input, target): # do something to both images input = Scale(self.height, Image.BILINEAR)(input) target = Scale(self.height, Image.NEAREST)(target) if (self.augment): # Random hflip hflip = random.random() if (hflip < 0.5): input = input.transpose(Image.FLIP_LEFT_RIGHT) target = target.transpose(Image.FLIP_LEFT_RIGHT) #Random translation 0-2 pixels (fill rest with padding transX = random.randint(-2, 2) transY = random.randint(-2, 2) input = ImageOps.expand(input, border=(transX, transY, 0, 0), fill=0) target = ImageOps.expand(target, border=(transX, transY, 0, 0), fill=255) #pad label filling with 255 input = input.crop( (0, 0, input.size[0] - transX, input.size[1] - transY)) target = target.crop( (0, 0, target.size[0] - transX, target.size[1] - transY)) #TODO future: additional augments #CenterCrop(256) #Normalize([.485, .456, .406], [.229, .224, .225]), input = ToTensor()(input) if (self.enc): target = Scale(int(self.height / 8), Image.NEAREST)(target) target = ToLabel()(target) target = Relabel(255, 19)(target) return input, target
cv2.imshow("test", cv_image) cv2.waitKey(-1) def showTensorImage(tensor_image): pil_image = transforms.ToPILImage()(tensor_image).convert('RGB') showImage(pil_image) if __name__ == "__main__": from torchvision.transforms import Compose, CenterCrop, Normalize from torchvision.transforms import ToTensor, ToPILImage from piwise.transform import Relabel, ToLabel, Colorize image_transform = ToPILImage() input_transform = Compose([ CenterCrop(30), ToTensor(), #Normalize([.485, .456, .406], [.229, .224, .225]), ]) target_transform = Compose([ CenterCrop(30), ToLabel(), #Relabel(255, 21), ]) dataset = VOC12("/data_1/data/VOC2012/VOCdevkit/VOC2012", input_transform, target_transform) for image, label in dataset: print(label) #showTensorImage(image)
def __call__(self, input, target): # do something to both images input = Scale(self.height, Image.BILINEAR)(input) target = Scale(self.height, Image.NEAREST)(target) if (self.augment): # Random hflip hflip = random.random() if (hflip < 0.5): input = input.transpose(Image.FLIP_LEFT_RIGHT) target = target.transpose(Image.FLIP_LEFT_RIGHT) degree = random.randint(-20, 20) input = input.rotate(degree, resample=Image.BILINEAR, expand=True) target = target.rotate(degree, resample=Image.NEAREST, expand=True) w, h = input.size nratio = random.uniform(0.5, 1.0) ni = random.randint(0, int(h - nratio * h)) nj = random.randint(0, int(w - nratio * w)) input = input.crop( (nj, ni, int(nj + nratio * w), int(ni + nratio * h))) target = target.crop( (nj, ni, int(nj + nratio * w), int(ni + nratio * h))) input = Resize((480, 640), Image.BILINEAR)(input) target = Resize((480, 640), Image.NEAREST)(target) brightness_factor = random.uniform(0.8, 1.2) contrast_factor = random.uniform(0.8, 1.2) saturation_factor = random.uniform(0.8, 1.2) #sharpness_factor=random.uniform(0.0,2.0) hue_factor = random.uniform(-0.2, 0.2) enhancer1 = ImageEnhance.Brightness(input) input = enhancer1.enhance(brightness_factor) enhancer2 = ImageEnhance.Contrast(input) input = enhancer2.enhance(contrast_factor) enhancer3 = ImageEnhance.Color(input) input = enhancer3.enhance(saturation_factor) #enhancer4=ImageEnhance.Sharpness(input) #input=enhancer4.enhance(sharpness_factor) input_mode = input.mode h, s, v = input.convert('HSV').split() np_h = np.array(h, dtype=np.uint8) with np.errstate(over='ignore'): np_h += np.uint8(hue_factor * 255) h = Image.fromarray(np_h, 'L') input = Image.merge('HSV', (h, s, v)).convert(input_mode) else: input = Resize((480, 640), Image.BILINEAR)(input) target = Resize((480, 640), Image.NEAREST)(target) input = ToTensor()(input) if (self.enc): target = Resize((60, 80), Image.NEAREST)(target) target = ToLabel()(target) target = Relabel(255, 27)(target) return input, target