def test_adjusts_L_mode(self): x_shape = [2, 2, 3] x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1] x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape) x_rgb = Image.fromarray(x_np, mode='RGB') x_l = x_rgb.convert('L') assert F.adjust_brightness(x_l, 2).mode == 'L' assert F.adjust_saturation(x_l, 2).mode == 'L' assert F.adjust_contrast(x_l, 2).mode == 'L' assert F.adjust_hue(x_l, 0.4).mode == 'L' assert F.adjust_gamma(x_l, 0.5).mode == 'L'
def test_adjust_gamma(self): x_shape = [2, 2, 3] x_data = [0, 5, 13, 54, 135, 226, 37, 8, 234, 90, 255, 1] x_np = np.array(x_data, dtype=np.uint8).reshape(x_shape) x_pil = Image.fromarray(x_np, mode='RGB') # test 0 y_pil = F.adjust_gamma(x_pil, 1) y_np = np.array(y_pil) assert np.allclose(y_np, x_np) # test 1 y_pil = F.adjust_gamma(x_pil, 0.5) y_np = np.array(y_pil) y_ans = [0, 35, 57, 117, 185, 240, 97, 45, 244, 151, 255, 15] y_ans = np.array(y_ans, dtype=np.uint8).reshape(x_shape) assert np.allclose(y_np, y_ans) # test 2 y_pil = F.adjust_gamma(x_pil, 2) y_np = np.array(y_pil) y_ans = [0, 0, 0, 11, 71, 200, 5, 0, 214, 31, 255, 0] y_ans = np.array(y_ans, dtype=np.uint8).reshape(x_shape) assert np.allclose(y_np, y_ans)
def torchvision(self, img): return torchvision.adjust_gamma(img, gamma=0.5)
def __call__(self, img, mask): assert img.size == mask.size return tf.adjust_gamma(img, random.uniform(1, 1 + self.gamma)), mask
def __call__(self, img): if random.random() < 0.5: gamma = random.uniform(self.gamma_range[0], self.gamma_range[1]) return TrF.adjust_gamma(img, gamma) else: return img
def torchvision(self, img): return torchvision.adjust_gamma(img, gamma=0.5)
def __call__(self, imgs): return [ F.adjust_gamma(img=img, gamma=self.gamma, gain=self.gain) for img in imgs ]
def __call__(self, img): return F.adjust_gamma(img, self.gamma_factor, 1)
def __call__(self, img, masks): return tf.adjust_gamma(img, random.uniform(1, 1 + self.gamma)), masks
def gamma(self, gamma_ratio): self.data = TF.adjust_gamma(self.data, gamma_ratio, gain=1)
def __call__(self, image, target): if random.random() < self.prob: gamma_factor = random.uniform(0.5, 2) image = F.adjust_gamma(image, gamma_factor) return image, target
def __call__(self, img, label): assert img.size == label.size return tf.adjust_gamma(img, random.uniform(1, 1 + self.gamma)), label
def torchvision_transform(self, img): return torchvision.adjust_gamma(img, gamma=0.5)
def __call__(self, image, target): image= F.to_pil_image(image) image= F.adjust_gamma(image, 3) image= F.to_tensor(image) return image, target
def __getitem__(self, index): """ 内建函数,当对该类的实例进行类似字典的操作时,就会自动执行该函数,并返会对应的值 这是必须要重载的函数,就是实现给定索引,返回对应的图像 给出图像编号,返回变换后的输入图像和对应的label :param index: 图像编号 :return: """ imgPath = self.img_lst[index] self.name = imgPath.split("/")[-1][0:2] + ".tif" gtPath = self.gt_dct["gt"][index] maskPath = self.mask_lst[index] simple_transform = transforms.ToTensor() img = Image.open(imgPath) w, h = img.size img = img.resize(self.scale_size, Image.BICUBIC) gt = Image.open(gtPath).convert("L") mask = Image.open(maskPath).convert("L") gt = gt.resize(self.scale_size, Image.BICUBIC) mask = mask.resize(self.scale_size, Image.BICUBIC) gt = np.array(gt, dtype=np.uint8) gt[gt >= 128] = 255 gt[gt < 128] = 0 gt = Image.fromarray(gt) mask = np.array(mask, dtype=np.uint8) mask[mask < 128] = 0 mask[mask >= 128] = 1 mask = Image.fromarray(mask) if self.channel == 1: img = img.convert("L") img = np.array(img, dtype=np.uint8) img = linear_stretch(img) # 灰度线性拉伸 clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) img = clahe.apply(np.array(img, dtype=np.uint8)) # CLAHE img = np.array(img, dtype=np.uint8) img = Image.fromarray(img) img = TF.adjust_gamma(img, gamma=1.2, gain=1) # gamma校正 img_transform = transforms.ToTensor() else: img = img.convert("RGB") img = np.array(img, dtype=np.uint8) img = Image.fromarray(img) img_transform = transforms.Compose([ # transforms.Grayscale(num_output_channels=3), # transforms.ColorJitter(brightness=0.7, contrast=0.6, saturation=0.5), # 随机改变图片的亮度、对比度和饱和度 transforms.ToTensor(), ]) if "manual" in self.gt_dct: # test manualPath = self.gt_dct["manual"][index] manual = Image.open(manualPath).convert("L").resize( self.scale_size, Image.BICUBIC) manual = np.array(manual, dtype=np.uint8) manual[manual >= 128] = 255 manual[manual < 128] = 0 manual = Image.fromarray(manual) img = simple_transform(img) gt = simple_transform(gt) mask = simple_transform(mask) manual = simple_transform(manual) return img, gt, mask, manual, (w, h) else: # training # augumentation rotate = 10 angel = random.randint(-rotate, rotate) img = img.rotate(angel) gt = gt.rotate(angel) mask = mask.rotate(angel) # img, gt, mask = Crop(img, gt, mask, max_attempts=3, crop_size=self.crop_size) img = img_transform(img) gt = simple_transform(gt) mask = simple_transform(mask) return img, gt, mask
def transform(self, img, depth, region, segments): """transform :param img: :param depth: """ img = img[:, :, :] img = img.astype(np.float32) / 255 #print(img.shape) # Resize scales images from 0 to 255, thus we need # to divide by 255.0 #img = torch.from_numpy(img).float() depth = torch.from_numpy(depth).float().unsqueeze(0).unsqueeze(0) #print(d) segments = torch.from_numpy(segments).float().unsqueeze(0) region = torch.from_numpy(region).float().unsqueeze(0) #img = img.astype(float) / 255.0 # NHWC -> NCHW #img = img.transpose(1,2,0) topil = transforms.ToPILImage() totensor = transforms.ToTensor() normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) img = totensor(img) image = img.unsqueeze(0) + 0 #image=image/torch.max(image) #print(img.shape,depth.shape) #depth=depth[0,:,:] #depth = depth.astype(float)/32 #depth = np.round(depth) #depth = m.imresize(depth, (self.img_size[0], self.img_size[1]), 'nearest', mode='F') #depth = depth.astype(int) #depth=np.reshape(depth,[1,depth.shape[0],depth.shape[1]]) #classes = np.unique(depth) #print(classes) #depth = depth.transpose(2,0,1) #if not np.all(classes == np.unique(depth)): # print("WARN: resizing segmentss yielded fewer classes") #if not np.all(classes < self.n_classes): # raise ValueError("Segmentation map contained invalid class values") # img=F.interpolate(img,scale_factor=1/2,mode='bilinear',align_corners=False).squeeze()[:,6:-6,8:-8] # image=F.interpolate(image,scale_factor=1/2,mode='bilinear',align_corners=False).squeeze()[:,6:-6,8:-8] # #print(depth.shape) # depth=F.interpolate(depth,scale_factor=1/2,mode='bilinear',align_corners=False).squeeze()[6:-6,8:-8] # region=F.interpolate(region,scale_factor=1/2,mode='bilinear',align_corners=False).squeeze()[6:-6,8:-8] # segments=F.interpolate(segments,scale_factor=1/2,mode='bilinear',align_corners=False).squeeze()[6:-6,8:-8] if self.split == 'train': one = torch.ones(1).float() zero = torch.zeros(1).float() scale = random.uniform(1, 1.3) mask = (depth > alpha) & (depth < beta) mask = mask.float() #print(torch.sum(mask)) h = int(240 * scale) w = int(320 * scale) md = torch.max(depth) #print(md) mr = torch.max(region) ms = torch.max(segments) #print(torch.max(image)) img = tf.resize(topil(img.squeeze(0)), [h, w]) image = tf.resize(topil(image.squeeze(0)), [h, w]) #print(torch.max(totensor(image))) depth = tf.resize(topil(depth.squeeze(0) / md), [h, w]) mask = tf.resize(topil(mask.squeeze(0)), [h, w]) #print(segments.shape) segments = tf.resize(topil(segments.squeeze(0) / ms), [h, w]) region = tf.resize(topil(region.squeeze(0) / mr), [h, w]) i, j, h, w = transforms.RandomCrop.get_params( img, output_size=[228, 304]) r = random.uniform(-5, 5) sigma = random.uniform(0, 0.04) img = tf.rotate(img, r) image = tf.rotate(image, r) depth = tf.rotate(depth, r) mask = tf.rotate(mask, r) segments = tf.rotate(segments, r) region = tf.rotate(region, r) img = tf.crop(img, i, j, h, w) image = tf.crop(image, i, j, h, w) depth = tf.crop(depth, i, j, h, w) mask = tf.crop(mask, i, j, h, w) segments = tf.crop(segments, i, j, h, w) region = tf.crop(region, i, j, h, w) if random.uniform(0, 1) > 0.5: img = tf.hflip(img) image = tf.hflip(image) depth = tf.hflip(depth) mask = tf.hflip(mask) segments = tf.hflip(segments) region = tf.hflip(region) # brightness=random.uniform(0, 0.4) # contrast=random.uniform(0, 0.4) # saturation=random.uniform(0, 0.4) # hue=random.uniform(0, 0.2) color = transforms.ColorJitter(0.4, 0.4, 0.4, 0.4) img = color(img) gamma = random.uniform(0.7, 1.5) img = tf.adjust_gamma(img, gamma) img = totensor(img) r = random.uniform(0.8, 1.2) g = random.uniform(0.8, 1.2) b = random.uniform(0.8, 1.2) img[:, :, 0] *= r img[:, :, 1] *= g img[:, :, 2] *= b gaussian = torch.zeros_like(img).normal_() * sigma img = img + gaussian img = img.clamp(min=0, max=1) image = img + 0 img = normalize(img) #image=totensor(image) #print(torch.max(image)) #print(torch.max(depth),scale) depth = totensor(depth) * md / scale mask = totensor(mask) #print(torch.sum(mask)) depth = torch.where(mask > 0, depth, zero) #print(torch.max(depth)) #print(torch.max(depth),scale) region = totensor(region) * mr segments = totensor(segments) * ms depth = torch.where(depth > beta, beta * zero, depth) depth = torch.where(depth < alpha, alpha * zero, depth) #exit() else: one = torch.ones(1).float() zero = torch.zeros(1).float() mask = (depth > alpha) & (depth < beta) mask = mask.float() img = img.unsqueeze(0) img = F.interpolate(img, scale_factor=1 / 2, mode='bilinear', align_corners=False).squeeze() image = F.interpolate(image, scale_factor=1 / 2, mode='bilinear', align_corners=False).squeeze() #print(depth.shape) #depth=F.interpolate(depth,scale_factor=1/2,mode='bilinear',align_corners=False).squeeze() #mask=F.interpolate(mask,scale_factor=1/2,mode='bilinear',align_corners=False).squeeze() #print(torch.sum(mask)) #depth=torch.where(mask>=1,depth,torch.zeros(1).float()) depth = torch.where(depth > beta, beta * zero, depth) depth = torch.where(depth < alpha, alpha * zero, depth) #depth=depth.squeeze() region = F.interpolate(region, scale_factor=1 / 2, mode='bilinear', align_corners=False).squeeze() segments = F.interpolate(segments, scale_factor=1 / 2, mode='bilinear', align_corners=False).squeeze() image = img[..., 6:-6, 8:-8] + 0 img = normalize(img)[..., 6:-6, 8:-8] #exit() # img=img.squeeze() # #print(depth.shape) # depth=depth.squeeze() # region=region.squeeze() # segments=segments.squeeze() #print(img.shape,image.shape,region.shape,segments.shape) return img, depth, region, segments, image