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)) input = ToTensor()(input) if (self.enc): target = Scale(int(self.height / 8), Image.NEAREST)(target) target = ToLabel()(target) target = Relabel(1, 1)(target) target = Relabel(2, 2)(target) target = Relabel(3, 255)(target) target = Relabel(4, 255)(target) target = Relabel(5, 255)(target) target = Relabel(6, 255)(target) target = Relabel(7, 255)(target) target = Relabel(8, 3)(target) target = Relabel(9, 255)(target) target = Relabel(10, 4)(target) target = Relabel(11, 255)(target) target = Relabel(12, 255)(target) target = Relabel(13, 5)(target) target = Relabel(14, 255)(target) target = Relabel(15, 255)(target) target = Relabel(16, 255)(target) target = Relabel(17, 255)(target) target = Relabel(18, 255)(target) target = Relabel(19, 255)(target) target = Relabel(255, 6)(target) return input, target
def transform(self, img, lbl): img = Scale(self.img_size, Image.BILINEAR)(img) lbl = Scale(self.img_size, Image.NEAREST)(lbl) if (self.augment): hflip = random.random() if (hflip < 0.5): img = img.transpose(Image.FLIP_LEFT_RIGHT) lbl = lbl.transpose(Image.FLIP_LEFT_RIGHT) transX = random.randint(-2, 2) transY = random.randint(-2, 2) img = ImageOps.expand(img, border=(transX, transY, 0, 0), fill=0) lbl = ImageOps.expand(lbl, border=(transX, transY, 0, 0), fill=11) img = img.crop((0, 0, img.size[0] - transX, img.size[1] - transY)) lbl = lbl.crop((0, 0, lbl.size[0] - transX, lbl.size[1] - transY)) img = ToTensor()(img) img = Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img) lbl = np.array(lbl) lbl = torch.from_numpy(lbl).long() return img, lbl
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
def __getitem__(self, item): image_file = self.image_list[item] label_file = os.path.join( self.labels_root, os.path.basename(image_file).replace('_ahe.png', '.png')) # print(image_file) with open(image_file, 'rb') as f: image = Image.open(f).convert('RGB') with open(label_file, 'rb') as f: label = Image.open(f).convert('P') image = Scale(256)(image) label = Scale(256)(label) image, x1, y1 = RandomCropImage(224)(image) label = label.crop((x1, y1, x1 + 224, y1 + 224)) if self.input_trans is not None: image = self.input_trans(image) if self.target_trans is not None: label = self.target_trans(label) return image, label
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