Ejemplo n.º 1
0
    def custom_transform(self, image, segmentation):
        # 0.5 probability of performing one transformation
        if random.random() > 0.5:
            p = random.random()

            # Rotate
            if p < 0.3:
                angle = random.randint(-30, 30)
                image = TF.rotate(image, angle)
                segmentation = TF.rotate(segmentation, angle)

            # Horizontal flip
            elif p < 0.6:
                TF.hflip(image)
                TF.hflip(segmentation)

            # Vertical flip
            elif p < 0.9:
                TF.vflip(image)
                TF.vflip(segmentation)

            # Colours
            else:
                TF.adjust_brightness(image, 1.4)
                TF.adjust_hue(image, 0.3)
                TF.adjust_contrast(image, 1.3)
                TF.adjust_saturation(image, 0.7)

        return image, segmentation
Ejemplo n.º 2
0
    def __call__(self, seq_blur, seq_clear):
        seq_blur  = [Image.fromarray(np.uint8(img)) for img in seq_blur]
        seq_clear = [Image.fromarray(np.uint8(img)) for img in seq_clear]
        if self.brightness > 0:
            brightness_factor = np.random.uniform(max(0, 1 - self.brightness), 1 + self.brightness)
            seq_blur  = [F.adjust_brightness(img, brightness_factor) for img in seq_blur]
            seq_clear = [F.adjust_brightness(img, brightness_factor) for img in seq_clear]

        if self.contrast > 0:
            contrast_factor = np.random.uniform(max(0, 1 - self.contrast), 1 + self.contrast)
            seq_blur  = [F.adjust_contrast(img, contrast_factor) for img in seq_blur]
            seq_clear = [F.adjust_contrast(img, contrast_factor) for img in seq_clear]

        if self.saturation > 0:
            saturation_factor = np.random.uniform(max(0, 1 - self.saturation), 1 + self.saturation)
            seq_blur  = [F.adjust_saturation(img, saturation_factor) for img in seq_blur]
            seq_clear = [F.adjust_saturation(img, saturation_factor) for img in seq_clear]

        if self.hue > 0:
            hue_factor = np.random.uniform(-self.hue, self.hue)
            seq_blur  = [F.adjust_hue(img, hue_factor) for img in seq_blur]
            seq_clear = [F.adjust_hue(img, hue_factor) for img in seq_clear]

        seq_blur  = [np.asarray(img) for img in seq_blur]
        seq_clear = [np.asarray(img) for img in seq_clear]

        seq_blur  = [img.clip(0,255) for img in seq_blur]
        seq_clear = [img.clip(0,255) for img in seq_clear]

        return seq_blur, seq_clear
Ejemplo n.º 3
0
def CustomTransform(LR, HR, adjust_brightness, adjust_contrast,
                    adjust_saturation, adjust_hue, adjust_gamma, rotate, hflip,
                    vflip):
    """
    to execute data augmentation.
    except :param LR and :param HR, all other params accept bool args.
    """
    if adjust_brightness:
        brightness_factor = random.gauss(1, 0.05)
        while brightness_factor > 2 or brightness_factor < 0:
            brightness_factor = random.gauss(1, 0.05)
        HR = f.adjust_brightness(HR, brightness_factor)
        LR = f.adjust_brightness(LR, brightness_factor)

    if adjust_contrast:
        contrast_factor = random.gauss(1, 0.05)
        while contrast_factor > 2 or contrast_factor < 0:
            contrast_factor = random.gauss(1, 0.05)
        HR = f.adjust_contrast(HR, contrast_factor)
        LR = f.adjust_contrast(LR, contrast_factor)

    if adjust_saturation:
        saturation_factor = random.gauss(1, 0.05)
        while saturation_factor > 2 or saturation_factor < 0:
            saturation_factor = random.gauss(1, 0.05)
        HR = f.adjust_saturation(HR, saturation_factor)
        LR = f.adjust_saturation(LR, saturation_factor)

    if adjust_hue:
        hue_factor = random.gauss(0, 0.025)
        while hue_factor > 0.5 or hue_factor < -0.5:
            hue_factor = random.gauss(0, 0.025)
        HR = f.adjust_hue(HR, hue_factor)
        LR = f.adjust_hue(LR, hue_factor)

    if adjust_gamma:
        gamma = random.gauss(1, 0.025)
        while gamma > 0.5 or gamma < -0.5:
            gamma = random.gauss(0, 0.025)
        HR = f.adjust_gamma(HR, gamma)
        LR = f.adjust_gamma(LR, gamma)

    if rotate:
        angle = random.choice([-90, 0, 90])
        HR = f.rotate(HR, angle)
        LR = f.rotate(LR, angle)

    if hflip:
        flip = random.choice([True, False])
        if flip:
            HR = f.hflip(HR)
            LR = f.hflip(LR)

    if vflip:
        flip = random.choice([True, False])
        if flip:
            HR = f.vflip(HR)
            LR = f.vflip(LR)

    return LR, HR
Ejemplo n.º 4
0
def image_change_contrast(img, contrast=0, use_random=False):
    """Given the image, change the contrast of the image.

    Param:
        - contrast: In F.adjust_contrast, the value of contrast will give
        the followig result:
            0 will give a solid gray image
            1 will give the original image
            2 will increase the contrast of the image by the factor of 2
        - random: determine whether we need to randomly generate a contrast
        factor in the valid range.
    Return:
        - image in BGR order
    """
    if contrast > 0:
        # convert opencv image to PIL image
        pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

        if use_random:
            contrast = random.uniform(max(0, 1 - contrast), 1 + contrast)
            pil_img = F.adjust_contrast(pil_img, contrast)
        else:
            pil_img = F.adjust_contrast(pil_img, contrast)

        img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)

    return img
Ejemplo n.º 5
0
    def __call__(self, sample):
        if np.random.random() < 0.5:
            contrast_factor = np.random.uniform(0.8, 1.2)
            sample['left'] = F.adjust_contrast(sample['left'], contrast_factor)
            sample['right'] = F.adjust_contrast(sample['right'], contrast_factor)

        return sample
Ejemplo n.º 6
0
    def __call__(self,
                 img: Image.Image,
                 boxes: Optional[torch.Tensor] = None,
                 labels: Optional[torch.Tensor] = None):
        if random() < self._prob:
            brightness_factor = uniform(self._min, self._max_brightness_factor)
            img = F.adjust_brightness(img, brightness_factor)

        contrast_factor = uniform(self._min, self._max_contrast_factor)
        hue_factor = uniform(-self._min, self._max_hue_factor)
        saturation_factor = uniform(self._min, self._max_saturation_factor)

        # 50% chance applying contrast followed by hue and saturation or vice versa
        if random() < 0.5:
            if random() < self._prob:
                img = F.adjust_contrast(img, contrast_factor)

            if random() < self._prob:
                img = img.convert("HSV")
                img = F.adjust_hue(img, hue_factor)
                img = img.convert("RGB")
                img = F.adjust_saturation(img, saturation_factor)

        else:
            if random() < self._prob:
                img = img.convert("HSV")
                img = F.adjust_hue(img, hue_factor)
                img = img.convert("RGB")
                img = F.adjust_saturation(img, saturation_factor)

            if random() < self._prob:
                img = F.adjust_contrast(img, contrast_factor)

        return img, boxes, labels
Ejemplo n.º 7
0
def train_transform(lr: PIL.BmpImagePlugin.BmpImageFile,
                    hr: PIL.BmpImagePlugin.BmpImageFile):
    r"""Unified transformation of training dataset."""
    # Rotate the image by angle.
    angle = transforms.RandomRotation.get_params([-180, 180])
    lr = lr.rotate(angle)
    hr = hr.rotate(angle)

    # Horizontally flip the given image randomly with a given probability.
    p = random.random()
    if p > 0.5:
        lr = F.hflip(lr)
        lr = F.vflip(lr)
        hr = F.hflip(hr)
        hr = F.vflip(hr)

    # Randomly change the brightness, contrast, saturation and hue of an image.
    lr = F.adjust_brightness(lr, brightness_factor=p)
    lr = F.adjust_contrast(lr, contrast_factor=p)
    lr = F.adjust_saturation(lr, saturation_factor=p)
    hr = F.adjust_brightness(hr, brightness_factor=p)
    hr = F.adjust_contrast(hr, contrast_factor=p)
    hr = F.adjust_saturation(hr, saturation_factor=p)

    # PIL to tensor.
    lr = F.to_tensor(lr)
    hr = F.to_tensor(hr)

    return lr, hr
Ejemplo n.º 8
0
def data_augmentation(x, y, mode='train'):
    """
    x, y: np array, [B, C, H, W]
    """
    x_list = list()
    y_list = list()
    for i in range(x.shape[0]):
        # gamma correction
        xi = skimage.exposure.adjust_gamma(x[i, ...], 0.4)
        yi = skimage.exposure.adjust_gamma(y[i, ...], 0.4)

        # color transfer
        if mode == 'train':
            xi = color_transform(xi, T)
            yi = color_transform(yi, T)

        xi = Image.fromarray(xi)
        yi = Image.fromarray(yi)

        if mode == 'train':
            pick = random.randint(0, 4)
            if pick == 0:
                # random brightness
                brightness_factor = 1.0 + random.uniform(0, 0.3)
                xi = ttf.adjust_brightness(xi, brightness_factor)
                yi = ttf.adjust_brightness(yi, brightness_factor)
            elif pick == 1:
                # random saturation
                saturation_factor = 1.0 + random.uniform(-0.2, 0.5)
                xi = ttf.adjust_saturation(xi, saturation_factor)
                yi = ttf.adjust_saturation(yi, saturation_factor)
            elif pick == 2:
                # random hue
                hue_factor = random.uniform(-0.2, 0.2)
                xi = ttf.adjust_hue(xi, hue_factor)
                yi = ttf.adjust_hue(yi, hue_factor)
            elif pick == 3:
                # random contrast
                contrast_factor = 1.0 + random.uniform(-0.2, 0.4)
                xi = ttf.adjust_contrast(xi, contrast_factor)
                yi = ttf.adjust_contrast(yi, contrast_factor)
            elif pick == 4:
                # random swap color channel
                permute = np.random.permutation(3)
                xi = np.array(xi)
                yi = np.array(yi)
                xi = xi[..., permute]
                yi = yi[..., permute]
        xi = np.clip(np.array(xi) / 255.0, 0, 1.0)
        yi = np.clip(np.array(yi) / 255.0, 0, 1.0)
        x_list.append(xi)
        y_list.append(yi)
    x_ret = torch.tensor(np.stack(x_list, axis=0), dtype=torch.float)
    y_ret = torch.tensor(np.stack(y_list, axis=0), dtype=torch.float)
    x_ret = normalize_lf(x_ret)
    y_ret = normalize_lf(y_ret)

    return x_ret.to(device), y_ret.to(device)
Ejemplo n.º 9
0
    def selective_color_distort(self, image, mask):
        # make skin mask using face, neck, and ear segments
        skin = mask[0] + mask[16] + mask[6] + mask[7]
        # ensure there's no clipping
        skin[skin > 1] = 1
        skin = skin.repeat((3, 1, 1))

        # torch transforms only work on PIL images, so first
        # the image tensors must be converted to PIL images
        background = TF.to_pil_image(image)
        face = TF.to_pil_image(image)

        # generate parameters for color jitter
        # taken from https://arxiv.org/pdf/2002.05709.pdf page 12
        jitter_params = torch.rand(4) * torch.tensor([.8, .8, .8, .2])
        if torch.rand(1).item() > .2:
            # apply transforms to background
            background = TF.adjust_brightness(background,
                                              .2 + jitter_params[0] * 2)
            background = TF.adjust_contrast(background,
                                            .2 + jitter_params[1] * 2)
            background = TF.adjust_saturation(background,
                                              .2 + jitter_params[2] * 2)
            background = TF.adjust_hue(background, jitter_params[3] * 2 - .2)

            # apply identical brightness/contrast/saturation transform to face
            face = TF.adjust_brightness(face, .2 + jitter_params[0] * 2)
            face = TF.adjust_saturation(face, .2 + jitter_params[2] * 2)

            # only apply 50% of the contrast transform to the face
            face = TF.adjust_contrast(face, .4 + jitter_params[1])
            # only apply 25% of the hue transform to the face
            face = TF.adjust_hue(face, jitter_params[3] * .5 - .05)

        # note: the possibility of neither color jitter or grayscale or both
        #       color jitter and grayscale is intended behavior. the random
        #       draws are meant to be different for these two if statements
        if torch.rand(1).item() > .8:
            background = TF.to_grayscale(background, num_output_channels=3)
            face = TF.to_grayscale(face, num_output_channels=3)

        # convert PIL images back to tensors
        background = TF.to_tensor(background)
        face = TF.to_tensor(face)

        # construct final image as convex combination of face pixels
        # and mask pixels. where the mask has higher confidence (closer
        # to 1), the face image will be used
        distorted = (1 - skin) * background + skin * face

        return distorted.type(torch.float32)
Ejemplo n.º 10
0
        def wrapper(img):

            nonlocal aug_params

            if aug_target == 'batch' or aug_target == 'test-batch':
                pass  # nothing to do

            # Deciding transform parameters
            elif aug_target == 'sample':
                aug_params['rotate']['angle'] = get_random_rotate_angle(
                    aug_params['rotate']['train_range'])
                aug_params['bright']['factor'] = get_random_factor(
                    aug_params['bright']['train_range'])
                aug_params['contrast']['factor'] = get_random_factor(
                    aug_params['contrast']['train_range'])

            elif aug_target == 'test-sample':
                aug_params['rotate']['angle'] = get_random_rotate_angle(
                    aug_params['rotate']['test_range'])
                aug_params['bright']['factor'] = get_random_factor(
                    aug_params['bright']['test_range'])
                aug_params['contrast']['factor'] = get_random_factor(
                    aug_params['contrast']['test_range'])

            else:
                raise ValueError('Invalid aug_target: %s' % (aug_target))

            # Process the image
            if aug_type == 'rotate':
                img = TF.rotate(img, aug_params['rotate']['angle'])
#                 print('rotate angle:', aug_params['rotate']['angle'])

            elif aug_type == 'bright':
                img = TF.adjust_brightness(img, aug_params['bright']['factor'])
#                 print('bright factor:', aug_params['bright']['factor'])

            elif aug_type == 'contrast':
                img = TF.adjust_contrast(img, aug_params['contrast']['factor'])


#                 print('contrast factor:', aug_params['contrast']['factor'])

            elif aug_type == 'mix':
                img = TF.adjust_contrast(img, aug_params['contrast']['factor'])
                img = TF.adjust_brightness(img, aug_params['bright']['factor'])
                img = TF.rotate(img, aug_params['rotate']['angle'])

            else:
                raise ValueError('Invalid aug_type: %s' % (aug_type))

            return img
Ejemplo n.º 11
0
def random_adjust_contrast(lr: Any, hr: Any) -> list[Any, Any]:
    """Set ``PIL.Image`` to randomly adjust the image contrast.
    Args:
        lr: Low-resolution image data read by ``PIL.Image``.
        hr: High-resolution image data read by ``PIL.Image``.
    Returns:
        Low-resolution image and high-resolution image with randomly adjusted contrast.
    """
    # Randomly adjust the contrast gain range.
    factor = random.uniform(0.5, 2)
    lr = F.adjust_contrast(lr, factor)
    hr = F.adjust_contrast(hr, factor)

    return lr, hr
Ejemplo n.º 12
0
def augmentation(image, seg, hand=None, p=.5):
    if hand:
        if random.random() < p:
            angle = random.randint(-45, 45)
            image = TF.rotate(image, angle)
            hand = TF.rotate(hand, angle, fill=0.)
            seg = TF.rotate(
                seg, angle, fill=2
            )  #if rotation leaves edges black fill value with 2 to ignore
        if random.random() < p:
            image = TF.hflip(image)
            hand = TF.hflip(hand)
            seg = TF.hflip(seg)
        if random.random() < p:
            image = TF.vflip(image)
            hand = TF.vflip(hand)
            seg = TF.vflip(seg)
        if random.random() < p:
            image = TF.adjust_saturation(image, random.uniform(
                .5, 3))  #only applicable to image
        if random.random() < p:
            image = TF.adjust_contrast(image, random.uniform(
                .5, 3))  #only applicable to image

        image = TF.to_tensor(image)
        hand = TF.to_tensor(hand)
        segmentation = TF.to_tensor(seg)
        imageHand = torch.cat([image, hand], dim=0)
        imageHand = TF.normalize(imageHand, mean=mean, std=std)
        return imageHand, segmentation
    else:
        if random.random() < p:
            angle = random.randint(-45, 45)
            image = TF.rotate(image, angle)
            seg = TF.rotate(seg, angle, fill=2)
        if random.random() < p:
            image = TF.hflip(image)
            seg = TF.hflip(seg)
        if random.random() < p:
            image = TF.vflip(image)
            seg = TF.vflip(seg)
        if random.random() < p:
            image = TF.adjust_saturation(image, random.uniform(.5, 3))
        if random.random() < p:
            image = TF.adjust_contrast(image, random.uniform(.5, 3))

        image = TF.to_tensor(image)
        segmentation = TF.to_tensor(seg)
        image = TF.normalize(image, mean=imgNetMean, std=imgNetStd)
        return image, segmentation
    def __getitem__(self, index):
        
        left = self.left[index]
        
        right = self.right[index]
        disp_L = self.disp_L[index]
        left_img = self.loader(left)
        right_img = self.loader(right)
        dataL = self.dploader(disp_L)
        dataL = np.clip(dataL, 0, 192)   
        stddev = np.random.uniform(5,10)

        factor = 1. + random.uniform(-0.5, 0.5)
        left_img = adjust_brightness(left_img, factor)
        right_img = adjust_brightness(right_img, factor)


        factor = 1. + random.uniform(-0.5, 0.5)
        left_img = adjust_contrast(left_img, factor)
        right_img = adjust_contrast(right_img, factor)


        left_img = np.asarray(left_img)
        right_img = np.asarray(right_img)
        

        noise = np.random.normal(0, stddev, left_img.shape)
        left_img = np.clip(left_img + noise, 0, 255)
        noise = np.random.normal(0, stddev, left_img.shape)
        right_img = np.clip(right_img + noise, 0, 255)

        left_img = motion_blur(left_img)
        right_img = motion_blur(right_img)


        left_img = Image.fromarray(np.uint8(left_img))
        right_img = Image.fromarray(np.uint8(right_img))


        dataL = np.ascontiguousarray(dataL, dtype=np.float32)

        processed = preprocess.get_transform(
            augment=False, normalize=self.normalize)
        left_img = processed(left_img)
        right_img = processed(right_img)


        return left_img, right_img, dataL
Ejemplo n.º 14
0
    def __call__(self, input, target):


        #Horizontal flip
        if h_flip:
            flip = random.random() < 0.5
            if flip:
                input=input.transpose(Image.FLIP_LEFT_RIGHT)
                target = target.transpose(Image.FLIP_LEFT_RIGHT)


        #Vertical flip
        if v_flip:
            flip = random.random() < 0.5
            if flip:
                input = input.transpose(Image.FLIP_TOP_BOTTOM)
                target = target.transpose(Image.FLIP_TOP_BOTTOM)


        #RandomCrop
        if Random_crop:
            i, j, h, w = transforms.RandomCrop.get_params(input, output_size=(224, 224))
            input = TF.crop(input, i, j, h, w)
            target = TF.crop(target, i, j, h, w)


        #RandomRotation
        i = transforms.RandomRotation.get_params(angle)
        input = TF.rotate(input, i)
        target = TF.rotate(target, i)



        #ColorJitter
        b,c,s,h=transforms.ColorJitter.get_params(ColorJitter[0],ColorJitter[1],ColorJitter[2],ColorJitter[3])

        input=TF.adjust_brightness(input,b)
        input=TF.adjust_contrast(input,c)
        input = TF.adjust_saturation(input, s)
        input = TF.adjust_hue(input, h)

        target=TF.adjust_brightness(target,b)
        target=TF.adjust_contrast(target,c)
        target = TF.adjust_saturation(target, s)
        target = TF.adjust_hue(target, h)


        return input, target
    def forward(self, img):
        """
        Args:
            img (PIL Image or Tensor): Input image.

        Returns:
            PIL Image or Tensor: Color jittered image.
        """

        #Check if image is bright enough
        #code form https://stackoverflow.com/questions/3490727/what-are-some-methods-to-analyze-image-brightness-using-python
        im = img.convert('L')
        stat = ImageStat.Stat(im)
        #print(stat.rms[0])


        fn_idx, brightness_factor, contrast_factor, saturation_factor, hue_factor = \
            self.get_params(self.brightness, self.contrast, self.saturation, self.hue)

        for fn_id in fn_idx:
            if fn_id == 0 and brightness_factor is not None and (
                    stat.rms[0] >= 50 or brightness_factor >= 1):
                img = functional.adjust_brightness(img, brightness_factor)
            elif fn_id == 1 and contrast_factor is not None and (
                    stat.rms[0] >= 50 or contrast_factor >= 1):
                img = functional.adjust_contrast(img, contrast_factor)
            elif fn_id == 2 and saturation_factor is not None:
                img = functional.adjust_saturation(img, saturation_factor)
            elif fn_id == 3 and hue_factor is not None:
                img = functional.adjust_hue(img, hue_factor)

        return img
Ejemplo n.º 16
0
    def randomize_parameters(self):
        self.p = random.random()

        brightness_factor = random.uniform(self.brightness[0],
                                           self.brightness[1])
        contrast_factor = random.uniform(self.contrast[0], self.contrast[1])
        saturation_factor = random.uniform(self.saturation[0],
                                           self.saturation[1])
        hue_factor = random.uniform(self.hue[0], self.hue[1])

        transforms = []
        transforms.append(
            torchvision.transforms.Lambda(
                lambda img: F.adjust_brightness(img, brightness_factor)))
        transforms.append(
            torchvision.transforms.Lambda(
                lambda img: F.adjust_contrast(img, contrast_factor)))
        transforms.append(
            torchvision.transforms.Lambda(
                lambda img: F.adjust_saturation(img, saturation_factor)))
        transforms.append(
            torchvision.transforms.Lambda(
                lambda img: F.adjust_hue(img, hue_factor)))

        random.shuffle(transforms)
        self.transform = torchvision.transforms.Compose(transforms)
Ejemplo n.º 17
0
def augmentations(img):
    crop_h = 32
    crop_w = 128
    i = torch.randint(height - crop_h + 1, size=(1, )).item()
    j = torch.randint(width - crop_w + 1, size=(1, )).item()
    img = F2.crop(img, i, j, crop_h, crop_w)
    img = F2.resize(img, (height, width))
    fn_idx = torch.randperm(4)
    for fn_id in fn_idx:
        if fn_id == 0 and torch.rand(1) < 0.2:
            brg = float(torch.empty(1).uniform_(0.6, 2))
            img = F2.adjust_brightness(img, brg)
        elif fn_id == 1 and torch.rand(1) < 0.2:
            con = float(torch.empty(1).uniform_(0.6, 2))
            img = F2.adjust_contrast(img, con)
        elif fn_id == 2 and torch.rand(1) < 0.2:
            sat = float(torch.empty(1).uniform_(0.6, 2))
            img = F2.adjust_saturation(img, sat)
        elif fn_id == 3 and torch.rand(1) < 0.2:
            hue = float(torch.empty(1).uniform_(-0.5, 0.5))
            img = F2.adjust_hue(img, hue)

    if torch.rand(1) < 0.2:
        img = F2.hflip(img)
    if torch.rand(1) < 0.2:
        img = F2.vflip(img)

    return img
Ejemplo n.º 18
0
    def get_params(brightness, contrast, saturation, hue):
        """Get a randomized transform to be applied on image.

        Arguments are same as that of __init__.

        Returns:
            Transform which randomly adjusts brightness, contrast and
            saturation in a random order.
        """
        transforms = []
        if brightness > 0:
            brightness_factor = random.uniform(max(0, 1 - brightness), 1 + brightness)
            transforms.append(Lambda(lambda img: F.adjust_brightness(img, brightness_factor)))

        if contrast > 0:
            contrast_factor = random.uniform(max(0, 1 - contrast), 1 + contrast)
            transforms.append(Lambda(lambda img: F.adjust_contrast(img, contrast_factor)))

        if saturation > 0:
            saturation_factor = random.uniform(max(0, 1 - saturation), 1 + saturation)
            transforms.append(Lambda(lambda img: F.adjust_saturation(img, saturation_factor)))

        if hue > 0:
            hue_factor = random.uniform(-hue, hue)
            transforms.append(Lambda(lambda img: F.adjust_hue(img, hue_factor)))

        random.shuffle(transforms)
        transform = Compose(transforms)

        return transform
    def randomize_parameters(self):
        transforms = []

        if self.brightness is not None:
            brightness_factor = random.uniform(self.brightness[0],
                                               self.brightness[1])
            transforms.append(
                Lambda(
                    lambda img: F.adjust_brightness(img, brightness_factor)))

        if self.contrast is not None:
            contrast_factor = random.uniform(self.contrast[0],
                                             self.contrast[1])
            transforms.append(
                Lambda(lambda img: F.adjust_contrast(img, contrast_factor)))

        if self.saturation is not None:
            saturation_factor = random.uniform(self.saturation[0],
                                               self.saturation[1])
            transforms.append(
                Lambda(
                    lambda img: F.adjust_saturation(img, saturation_factor)))

        if self.hue is not None:
            hue_factor = random.uniform(self.hue[0], self.hue[1])
            transforms.append(
                Lambda(lambda img: F.adjust_hue(img, hue_factor)))

        random.shuffle(transforms)
        self.transform = Compose(transforms)
Ejemplo n.º 20
0
    def adjust_contrast(self, img, do, prob, cf):
        if do:
            if random.random() < prob:
                c = random.uniform(1 - cf, 1 + cf)  # -7,7
                img = tff.adjust_contrast(img, contrast_factor=c)

        return img
Ejemplo n.º 21
0
 def __call__(self, image, target):
     if self.prob == 0:
         return image, target
     if random.random() < self.prob:
         factor = np.random.uniform(0.25, 0.75)
         image = F.adjust_contrast(image, factor)
     return image, target
Ejemplo n.º 22
0
    def forward(self, img, mask):
        """
        Args:
            img (PIL Image or Tensor): Input image.

        Returns:
            PIL Image or Tensor: Color jittered image.
        """
        fn_idx = torch.randperm(4)
        for fn_id in fn_idx:
            if fn_id == 0 and self.brightness is not None:
                brightness = self.brightness
                brightness_factor = torch.tensor(1.0).uniform_(
                    brightness[0], brightness[1]).item()
                img = F.adjust_brightness(img, brightness_factor)

            if fn_id == 1 and self.contrast is not None:
                contrast = self.contrast
                contrast_factor = torch.tensor(1.0).uniform_(
                    contrast[0], contrast[1]).item()
                img = F.adjust_contrast(img, contrast_factor)

            if fn_id == 2 and self.saturation is not None:
                saturation = self.saturation
                saturation_factor = torch.tensor(1.0).uniform_(
                    saturation[0], saturation[1]).item()
                img = F.adjust_saturation(img, saturation_factor)

            if fn_id == 3 and self.hue is not None:
                hue = self.hue
                hue_factor = torch.tensor(1.0).uniform_(hue[0], hue[1]).item()
                img = F.adjust_hue(img, hue_factor)

        return img, mask
Ejemplo n.º 23
0
    def get_params(brightness, contrast, saturation, hue):
        """Get a randomized transform to be applied on image.
        Arguments are same as that of __init__.
        Returns:
            Transform which randomly adjusts brightness, contrast and
            saturation in a random order.
        """
        transforms = []

        if brightness is not None:
            brightness_factor = random.uniform(brightness[0], brightness[1])
            transforms.append(torchvision.transforms.Lambda(lambda img: F.adjust_brightness(img, brightness_factor)))

        if contrast is not None:
            contrast_factor = random.uniform(contrast[0], contrast[1])
            transforms.append(torchvision.transforms.Lambda(lambda img: F.adjust_contrast(img, contrast_factor)))

        if saturation is not None:
            saturation_factor = random.uniform(saturation[0], saturation[1])
            transforms.append(torchvision.transforms.Lambda(lambda img: F.adjust_saturation(img, saturation_factor)))

        if hue is not None:
            hue_factor = random.uniform(hue[0], hue[1])
            transforms.append(torchvision.transforms.Lambda(lambda img: F.adjust_hue(img, hue_factor)))

        random.shuffle(transforms)
        transform = torchvision.transforms.Compose(transforms)

        return transform
Ejemplo n.º 24
0
    def forward(self, img):
        brightness_factor, contrast_factor, saturation_factor, hue_factor = 0, 0, 0, 0
        fn_idx = torch.randperm(4)
        for fn_id in fn_idx:
            if fn_id == 0 and self.brightness is not None:
                brightness = self.brightness
                brightness_factor = torch.tensor(1.0).uniform_(brightness[0], brightness[1]).item()
                img = F.adjust_brightness(img, brightness_factor)

            if fn_id == 1 and self.contrast is not None:
                contrast = self.contrast
                contrast_factor = torch.tensor(1.0).uniform_(contrast[0], contrast[1]).item()
                img = F.adjust_contrast(img, contrast_factor)

            if fn_id == 2 and self.saturation is not None:
                saturation = self.saturation
                saturation_factor = torch.tensor(1.0).uniform_(saturation[0], saturation[1]).item()
                img = F.adjust_saturation(img, saturation_factor)

            if fn_id == 3 and self.hue is not None:
                hue = self.hue
                hue_factor = torch.tensor(1.0).uniform_(hue[0], hue[1]).item()
                img = F.adjust_hue(img, hue_factor)

        return img, [brightness_factor, contrast_factor, saturation_factor, hue_factor]
Ejemplo n.º 25
0
    def __call__(self, x, params=None):
        """
        Args:
            x (numpy.ndarray or list): Image (H x W x C) or pose (3) or bounding box (4)

        Returns:
            numpy.ndarray or list: Transformed images.
        """
        if params is None:
            params = self.get_params(self.brightness, self.contrast, self.saturation, self.hue)
        fn_idx, brightness_factor, contrast_factor, saturation_factor, hue_factor = params
        if is_img(x):     # x is an image
            x = Image.fromarray(x)
            for fn_id in fn_idx:
                if fn_id == 0 and brightness_factor is not None:
                    x = F.adjust_brightness(x, brightness_factor)
                elif fn_id == 1 and contrast_factor is not None:
                    x = F.adjust_contrast(x, contrast_factor)
                elif fn_id == 2 and saturation_factor is not None:
                    x = F.adjust_saturation(x, saturation_factor)
                elif fn_id == 3 and hue_factor is not None:
                    x = F.adjust_hue(x, hue_factor)

            return np.array(x)
        elif isinstance(x, (list, tuple)):
            return [self.__call__(a, None if self.randomize_per_image else params) for a in x]

        return x
Ejemplo n.º 26
0
 def augment_PIL(self, img, labels):
     if np.random.rand() > 0.4:
         img = tvf.adjust_brightness(img, uniform(0.3, 1.5))
     if np.random.rand() > 0.7:
         factor = 2**uniform(-1, 1)
         img = tvf.adjust_contrast(img, factor)  # 0.5 ~ 2
     if np.random.rand() > 0.7:
         img = tvf.adjust_hue(img, uniform(-0.1, 0.1))
     if np.random.rand() > 0.6:
         factor = uniform(0, 2)
         if factor > 1:
             factor = 1 + uniform(0, 2)
         img = tvf.adjust_saturation(img, factor)  # 0 ~ 3
     if np.random.rand() > 0.5:
         img = tvf.adjust_gamma(img, uniform(0.5, 3))
     # horizontal flip
     if np.random.rand() > 0.5:
         img, labels = augUtils.hflip(img, labels)
     # vertical flip
     if np.random.rand() > 0.5:
         img, labels = augUtils.vflip(img, labels)
     # # random rotation
     rand_degree = np.random.rand() * 360
     if self.coco:
         img, labels = augUtils.rotate(img,
                                       rand_degree,
                                       labels,
                                       expand=True)
     else:
         img, labels = augUtils.rotate(img,
                                       rand_degree,
                                       labels,
                                       expand=False)
     return img, labels
Ejemplo n.º 27
0
    def get_params(brightness, contrast, saturation):
        """Get a randomized transform to be applied on image.

            Arguments are same as that of __init__.

            Returns:
                Transform which randomly adjusts brightness, contrast and
                saturation in a random order.
            """
        transforms = []

        if brightness is not None:
            brightness_factor = random.gauss(brightness[0], brightness[1])
            transforms.append(
                Lambda(lambda img: adjust_brightness(img, brightness_factor)))

        if contrast is not None:
            contrast_factor = random.gauss(contrast[0], contrast[1])
            transforms.append(
                Lambda(lambda img: adjust_contrast(img, contrast_factor)))

        if saturation is not None:
            saturation_factor = random.gauss(saturation[0], saturation[1])
            transforms.append(
                Lambda(lambda img: adjust_saturation(img, saturation_factor)))

        random.shuffle(transforms)
        transform = Compose(transforms)

        print(brightness_factor)

        return transform
Ejemplo n.º 28
0
 def __call__(self, img):
     contrast_factor = np.random.uniform(self.low, self.high)
     if not isinstance(img, Image.Image):
         img = np.clip(img * 255, 0, 255).astype(np.uint8)
         img = Image.fromarray(img)
     img = functional.adjust_contrast(img, contrast_factor)
     return img
Ejemplo n.º 29
0
    def recolor(im, recolor_net, aug=False):
        N = im.shape[0]
        if aug:
            im_ = []
            for i in range(N):
                im_i = im[i]

                brightness_factor = float(torch.Tensor(1).uniform_(0.8, 1.2))
                contrast_factor = float(torch.Tensor(1).uniform_(0.8, 1.2))
                gamma = float(torch.Tensor(1).uniform_(0.8, 1.2))
                hue_factor = float(torch.Tensor(1).uniform_(-0.1, 0.1))

                im_i = Ft.adjust_brightness(im_i, brightness_factor)
                im_i = Ft.adjust_contrast(im_i, contrast_factor)
                im_i = Ft.to_tensor(
                    Ft.adjust_gamma(Ft.to_pil_image(im_i.cpu()),
                                    gamma)).cuda()
                im_i = Ft.to_tensor(
                    Ft.adjust_hue(Ft.to_pil_image(im_i.cpu()),
                                  hue_factor)).cuda()

                im_.append(im_i)

            im = torch.stack(im_, dim=0)

        imout = recolor_net(im - 0.5) + im - 0.5
        imout = F.sigmoid(imout * 5)
        return imout
Ejemplo n.º 30
0
 def adjustContrast(self, image, mask):
     factor = transforms.RandomRotation.get_params([0, 10])  # 这里调增广后的数据的对比度
     image = tf.adjust_contrast(image, factor)
     # mask = tf.adjust_contrast(mask,factor)
     image = tf.to_tensor(image)
     mask = tf.to_tensor(mask)
     return image, mask
Ejemplo n.º 31
0
    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'
Ejemplo n.º 32
0
    def test_adjust_contrast(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_contrast(x_pil, 1)
        y_np = np.array(y_pil)
        assert np.allclose(y_np, x_np)

        # test 1
        y_pil = F.adjust_contrast(x_pil, 0.5)
        y_np = np.array(y_pil)
        y_ans = [43, 45, 49, 70, 110, 156, 61, 47, 160, 88, 170, 43]
        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_contrast(x_pil, 2)
        y_np = np.array(y_pil)
        y_ans = [0, 0, 0, 22, 184, 255, 0, 0, 255, 94, 255, 0]
        y_ans = np.array(y_ans, dtype=np.uint8).reshape(x_shape)
        assert np.allclose(y_np, y_ans)