示例#1
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'
示例#2
0
    def test_adjust_brightness(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_brightness(x_pil, 1)
        y_np = np.array(y_pil)
        assert np.allclose(y_np, x_np)

        # test 1
        y_pil = F.adjust_brightness(x_pil, 0.5)
        y_np = np.array(y_pil)
        y_ans = [0, 2, 6, 27, 67, 113, 18, 4, 117, 45, 127, 0]
        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_brightness(x_pil, 2)
        y_np = np.array(y_pil)
        y_ans = [0, 10, 26, 108, 255, 255, 74, 16, 255, 180, 255, 2]
        y_ans = np.array(y_ans, dtype=np.uint8).reshape(x_shape)
        assert np.allclose(y_np, y_ans)
 def __call__(self, img, mask):
     assert img.size == mask.size
     return tf.adjust_brightness(img,
                                 random.uniform(1 - self.bf,
                                                1 + self.bf)), mask
 def __call__(self, x):
     x = TF.adjust_gamma(x, gamma=1)
     x = TF.adjust_saturation(x, self.saturation_factor)
     x = TF.adjust_brightness(x, self.brightness_factor)
     x = TF.adjust_contrast(x, self.contrast_factor)
     return x
示例#5
0
    def __call__(self, inputs):

        img1 = inputs[0]
        img2 = inputs[1]
        depth = inputs[2]
        phase = inputs[3]
        fb = inputs[4]

        h = img1.height
        w = img1.width
        w0 = w

        if self.size == [-1]:
            divisor = 32.0
            h = int(math.ceil(h / divisor) * divisor)
            w = int(math.ceil(w / divisor) * divisor)
            self.size = (h, w)

        scale_transform = transforms.Compose(
            [transforms.Resize(self.size, Image.BICUBIC)])

        img1 = scale_transform(img1)
        if img2 is not None:
            img2 = scale_transform(img2)

        if fb is not None:
            scale = float(self.size[1]) / float(w0)
            fb = fb * scale
        if phase == 'test':
            return img1, img2, depth, fb

        if depth is not None:
            scale_transform_d = transforms.Compose(
                [transforms.Resize(self.size, Image.BICUBIC)])
            depth = scale_transform_d(depth)

        if not self.size == 0:

            if depth is not None:
                arr_depth = np.array(depth, dtype=np.float32)
                arr_depth /= 65535.0  # cm->m, /10

                arr_depth[arr_depth < 0.0] = 0.0
                depth = Image.fromarray(arr_depth, 'F')

        if self.flip and not (img2 is not None and depth is not None):

            flip_prob = random.random()
            flip_transform = transforms.Compose(
                [RandomHorizontalFlip(flip_prob)])
            if img2 is None:
                img1 = flip_transform(img1)
            else:
                if flip_prob < 0.5:
                    img1_ = img1
                    img2_ = img2
                    img1 = flip_transform(img2_)
                    img2 = flip_transform(img1_)
            if depth is not None:
                depth = flip_transform(depth)

        if self.rotation and not (img2 is not None and depth is not None):
            if random.random() < 0.5:
                degree = random.randrange(-500, 500) / 100
                img1 = F.rotate(img1, degree, Image.BICUBIC)
                if depth is not None:
                    depth = F.rotate(depth, degree, Image.BILINEAR)
                if img2 is not None:
                    img2 = F.rotate(img2, degree, Image.BICUBIC)
        if depth is not None:
            depth = np.array(depth, dtype=np.float32)
            depth = depth * 2.0
            depth -= 1.0

        if self.augment:
            if random.random() < 0.5:

                brightness = random.uniform(0.8, 1.0)
                contrast = random.uniform(0.8, 1.0)
                saturation = random.uniform(0.8, 1.0)

                img1 = F.adjust_brightness(img1, brightness)
                img1 = F.adjust_contrast(img1, contrast)
                img1 = F.adjust_saturation(img1, saturation)

                if img2 is not None:
                    img2 = F.adjust_brightness(img2, brightness)
                    img2 = F.adjust_contrast(img2, contrast)
                    img2 = F.adjust_saturation(img2, saturation)
        return img1, img2, depth, fb
示例#6
0
def apply_data_augmentation(img, da_config):
    applied_da = list()
    # Convert to PIL Image
    img = img[:, :, 0] if img.shape[2] == 1 else img
    img = Image.fromarray(img)
    # Apply data augmentation
    if "dpi" in da_config.keys(
    ) and np.random.rand() < da_config["dpi"]["proba"]:
        valid_factor = False
        while not valid_factor:
            factor = np.random.uniform(da_config["dpi"]["min_factor"],
                                       da_config["dpi"]["max_factor"])
            valid_factor = True
            if ("max_width" in da_config["dpi"].keys() and factor*img.size[0] > da_config["dpi"]["max_width"]) or \
                ("max_height" in da_config["dpi"].keys() and factor * img.size[1] > da_config["dpi"]["max_height"]):
                valid_factor = False
            if ("min_width" in da_config["dpi"].keys() and factor*img.size[0] < da_config["dpi"]["min_width"]) or \
                ("min_height" in da_config["dpi"].keys() and factor * img.size[1] < da_config["dpi"]["min_height"]):
                valid_factor = False
        img = DPIAdjusting(factor)(img)
        applied_da.append("dpi: factor {}".format(factor))
    if "perspective" in da_config.keys(
    ) and np.random.rand() < da_config["perspective"]["proba"]:
        scale = np.random.uniform(da_config["perspective"]["min_factor"],
                                  da_config["perspective"]["max_factor"])
        img = RandomPerspective(distortion_scale=scale,
                                p=1,
                                interpolation=Image.BILINEAR,
                                fill=255)(img)
        applied_da.append("perspective: scale {}".format(scale))
    elif "elastic_distortion" in da_config.keys(
    ) and np.random.rand() < da_config["elastic_distortion"]["proba"]:
        magnitude = np.random.randint(
            1, da_config["elastic_distortion"]["max_magnitude"] + 1)
        kernel = np.random.randint(
            1, da_config["elastic_distortion"]["max_kernel"] + 1)
        magnitude_w, magnitude_h = (
            magnitude, 1) if np.random.randint(2) == 0 else (1, magnitude)
        img = ElasticDistortion(grid=(kernel, kernel),
                                magnitude=(magnitude_w, magnitude_h),
                                min_sep=(1, 1))(img)
        applied_da.append(
            "elastic_distortion: magnitude ({}, {})  - kernel ({}, {})".format(
                magnitude_w, magnitude_h, kernel, kernel))
    elif "random_transform" in da_config.keys(
    ) and np.random.rand() < da_config["random_transform"]["proba"]:
        img = RandomTransform(da_config["random_transform"]["max_val"])(img)
        applied_da.append("random_transform")
    if "dilation_erosion" in da_config.keys(
    ) and np.random.rand() < da_config["dilation_erosion"]["proba"]:
        kernel_h = np.random.randint(
            da_config["dilation_erosion"]["min_kernel"],
            da_config["dilation_erosion"]["max_kernel"] + 1)
        kernel_w = np.random.randint(
            da_config["dilation_erosion"]["min_kernel"],
            da_config["dilation_erosion"]["max_kernel"] + 1)
        if np.random.randint(2) == 0:
            img = Erosion((kernel_w, kernel_h),
                          da_config["dilation_erosion"]["iterations"])(img)
            applied_da.append("erosion:  kernel ({}, {})".format(
                kernel_w, kernel_h))
        else:
            img = Dilation((kernel_w, kernel_h),
                           da_config["dilation_erosion"]["iterations"])(img)
            applied_da.append("dilation:  kernel ({}, {})".format(
                kernel_w, kernel_h))

    if "contrast" in da_config.keys(
    ) and np.random.rand() < da_config["contrast"]["proba"]:
        factor = np.random.uniform(da_config["contrast"]["min_factor"],
                                   da_config["contrast"]["max_factor"])
        img = adjust_contrast(img, factor)
        applied_da.append("contrast: factor {}".format(factor))
    if "brightness" in da_config.keys(
    ) and np.random.rand() < da_config["brightness"]["proba"]:
        factor = np.random.uniform(da_config["brightness"]["min_factor"],
                                   da_config["brightness"]["max_factor"])
        img = adjust_brightness(img, factor)
        applied_da.append("brightness: factor {}".format(factor))
    if "color_jittering" in da_config.keys(
    ) and np.random.rand() < da_config["color_jittering"]["proba"]:
        img = ColorJitter(
            contrast=da_config["color_jittering"]["factor_contrast"],
            brightness=da_config["color_jittering"]["factor_brightness"],
            saturation=da_config["color_jittering"]["factor_saturation"],
            hue=da_config["color_jittering"]["factor_hue"],
        )(img)
        applied_da.append("jittering")
    if "sign_flipping" in da_config.keys(
    ) and np.random.rand() < da_config["sign_flipping"]["proba"]:
        img = SignFlipping()(img)
        applied_da.append("sign_flipping")
    if "crop" in da_config.keys(
    ) and np.random.rand() < da_config["crop"]["proba"]:
        new_w, new_h = [int(t * da_config["crop"]["ratio"]) for t in img.size]
        img = RandomCrop((new_h, new_w))(img)
        applied_da.append("random_crop")
    elif "fixed_crop" in da_config.keys(
    ) and np.random.rand() < da_config["fixed_crop"]["proba"]:
        img = RandomCrop(
            (da_config["fixed_crop"]["h"], da_config["fixed_crop"]["w"]))(img)
        applied_da.append("fixed_crop")
    # convert to numpy array
    img = np.array(img)
    img = np.expand_dims(img, axis=2) if len(img.shape) == 2 else img
    return img, applied_da
示例#7
0
 def torchvision(self, img):
     img = torchvision.adjust_brightness(img, brightness_factor=1.5)
     img = torchvision.adjust_contrast(img, contrast_factor=1.5)
     return img
示例#8
0
    def __getitem__(self, index):
        ' Get sample'

        # Load image
        id = self.ids[index]
        if self.coco:
            image = self.coco.loadImgs(id)[0]['file_name']
        im = Image.open('{}/{}'.format(self.path, image)).convert("RGB")

        # Randomly sample scale for resize during training
        resize = self.resize
        if isinstance(resize, list):
            resize = random.randint(self.resize[0], self.resize[-1])

        ratio = resize / min(im.size)
        if ratio * max(im.size) > self.max_size:
            ratio = self.max_size / max(im.size)
        im = im.resize((int(ratio * d) for d in im.size), Image.BILINEAR)

        if self.training:
            # Get annotations
            boxes, categories = self._get_target(id)
            boxes *= ratio

            # Random rotation, if self.rotate_augment
            random_angle = random.randint(0, 3) * 90
            if self.rotate_augment and random_angle != 0:
                # rotate by random_angle degrees.
                im = im.rotate(random_angle)
                x, y, w, h = boxes[:, 0].clone(), boxes[:, 1].clone(
                ), boxes[:, 2].clone(), boxes[:, 3].clone()
                if random_angle == 90:
                    boxes[:, 0] = y
                    boxes[:, 1] = im.size[1] - x - w
                    boxes[:, 2] = h
                    boxes[:, 3] = w
                elif random_angle == 180:
                    boxes[:, 0] = im.size[0] - x - w
                    boxes[:, 1] = im.size[1] - y - h
                elif random_angle == 270:
                    boxes[:, 0] = im.size[0] - y - h
                    boxes[:, 1] = x
                    boxes[:, 2] = h
                    boxes[:, 3] = w

            # Random horizontal flip
            if random.randint(0, 1):
                im = im.transpose(Image.FLIP_LEFT_RIGHT)
                boxes[:, 0] = im.size[0] - boxes[:, 0] - boxes[:, 2]

            # Apply image brightness, contrast etc augmentation
            if self.augment_brightness:
                brightness_factor = random.normalvariate(
                    1, self.augment_brightness)
                brightness_factor = max(0, brightness_factor)
                im = adjust_brightness(im, brightness_factor)
            if self.augment_contrast:
                contrast_factor = random.normalvariate(1,
                                                       self.augment_contrast)
                contrast_factor = max(0, contrast_factor)
                im = adjust_contrast(im, contrast_factor)
            if self.augment_hue:
                hue_factor = random.normalvariate(0, self.augment_hue)
                hue_factor = max(-0.5, hue_factor)
                hue_factor = min(0.5, hue_factor)
                im = adjust_hue(im, hue_factor)
            if self.augment_saturation:
                saturation_factor = random.normalvariate(
                    1, self.augment_saturation)
                saturation_factor = max(0, saturation_factor)
                im = adjust_saturation(im, saturation_factor)

            target = torch.cat([boxes, categories], dim=1)

        # Convert to tensor and normalize
        data = torch.ByteTensor(torch.ByteStorage.from_buffer(im.tobytes()))
        data = data.float().div(255).view(*im.size[::-1], len(im.mode))
        data = data.permute(2, 0, 1)

        for t, mean, std in zip(data, self.mean, self.std):
            t.sub_(mean).div_(std)

        # Apply padding
        pw, ph = ((self.stride - d % self.stride) % self.stride
                  for d in im.size)
        data = F.pad(data, (0, pw, 0, ph))

        if self.training:
            return data, target

        return data, id, ratio
示例#9
0
 def __call__(self, input: Tensor) -> Tensor:
     return TF.adjust_brightness(input, self.brightness_factor)
示例#10
0
def Adjust_brightness(image):
    return F.adjust_brightness(image, 1.9)
示例#11
0
 def __call__(self, imgs):
     return [
         F.adjust_brightness(img=img, brightness_factor=self.bright_factor)
         for img in imgs
     ]
示例#12
0
 def __call__(self, sample):
     image, target = sample['image'], sample['target']  
     # 0 gives a black image, 1 gives the original image while 2 increases the brightness by a factor of 2.
     ratio = random.randint(8, 12) / 10
     image = tf.adjust_brightness(image, ratio)
     return {'image': image, 'target': target}
    def __getitem__(self, index):
        if self.test:
            g = self.X_test.get_group(self.keys[index])
            cont_gaze = []
            for i, row in g.iterrows():
                path = row['path']
                x_min = row['bbox_x_min']
                y_min = row['bbox_y_min']
                x_max = row['bbox_x_max']
                y_max = row['bbox_y_max']
                eye_x = row['eye_x']
                eye_y = row['eye_y']
                gaze_x = row['gaze_x']
                gaze_y = row['gaze_y']
                cont_gaze.append([gaze_x, gaze_y
                                  ])  # all ground truth gaze are stacked up
            for j in range(len(cont_gaze), 20):
                cont_gaze.append(
                    [-1,
                     -1])  # pad dummy gaze to match size for batch processing
            cont_gaze = torch.FloatTensor(cont_gaze)
            gaze_inside = True  # always consider test samples as inside
        else:
            path = self.X_train.iloc[index]
            x_min, y_min, x_max, y_max, eye_x, eye_y, gaze_x, gaze_y, inout = self.y_train.iloc[
                index]
            gaze_inside = bool(inout)

        # expand face bbox a bit
        k = 0.1
        x_min -= k * abs(x_max - x_min)
        y_min -= k * abs(y_max - y_min)
        x_max += k * abs(x_max - x_min)
        y_max += k * abs(y_max - y_min)

        img = Image.open(os.path.join(self.data_dir, path))
        img = img.convert('RGB')
        width, height = img.size
        x_min, y_min, x_max, y_max = map(float, [x_min, y_min, x_max, y_max])

        if self.imshow:
            img.save("origin_img.jpg")

        if self.test:
            imsize = torch.IntTensor([width, height])
        else:
            ## data augmentation

            # Jitter (expansion-only) bounding box size
            if np.random.random_sample() <= 0.5:
                k = np.random.random_sample() * 0.2
                x_min -= k * abs(x_max - x_min)
                y_min -= k * abs(y_max - y_min)
                x_max += k * abs(x_max - x_min)
                y_max += k * abs(y_max - y_min)

            # Random Crop
            if np.random.random_sample() <= 0.5:
                # Calculate the minimum valid range of the crop that doesn't exclude the face and the gaze target
                crop_x_min = np.min([gaze_x * width, x_min, x_max])
                crop_y_min = np.min([gaze_y * height, y_min, y_max])
                crop_x_max = np.max([gaze_x * width, x_min, x_max])
                crop_y_max = np.max([gaze_y * height, y_min, y_max])

                # Randomly select a random top left corner
                if crop_x_min >= 0:
                    crop_x_min = np.random.uniform(0, crop_x_min)
                if crop_y_min >= 0:
                    crop_y_min = np.random.uniform(0, crop_y_min)

                # Find the range of valid crop width and height starting from the (crop_x_min, crop_y_min)
                crop_width_min = crop_x_max - crop_x_min
                crop_height_min = crop_y_max - crop_y_min
                crop_width_max = width - crop_x_min
                crop_height_max = height - crop_y_min
                # Randomly select a width and a height
                crop_width = np.random.uniform(crop_width_min, crop_width_max)
                crop_height = np.random.uniform(crop_height_min,
                                                crop_height_max)

                # Crop it
                img = TF.crop(img, crop_y_min, crop_x_min, crop_height,
                              crop_width)

                # Record the crop's (x, y) offset
                offset_x, offset_y = crop_x_min, crop_y_min

                # convert coordinates into the cropped frame
                x_min, y_min, x_max, y_max = x_min - offset_x, y_min - offset_y, x_max - offset_x, y_max - offset_y
                # if gaze_inside:
                gaze_x, gaze_y = (gaze_x * width - offset_x) / float(crop_width), \
                                 (gaze_y * height - offset_y) / float(crop_height)
                # else:
                #     gaze_x = -1; gaze_y = -1

                width, height = crop_width, crop_height

            # Random flip
            if np.random.random_sample() <= 0.5:
                img = img.transpose(Image.FLIP_LEFT_RIGHT)
                x_max_2 = width - x_min
                x_min_2 = width - x_max
                x_max = x_max_2
                x_min = x_min_2
                gaze_x = 1 - gaze_x

            # Random color change
            if np.random.random_sample() <= 0.5:
                img = TF.adjust_brightness(img,
                                           brightness_factor=np.random.uniform(
                                               0.5, 1.5))
                img = TF.adjust_contrast(img,
                                         contrast_factor=np.random.uniform(
                                             0.5, 1.5))
                img = TF.adjust_saturation(img,
                                           saturation_factor=np.random.uniform(
                                               0, 1.5))

        head_channel = imutils.get_head_box_channel(
            x_min,
            y_min,
            x_max,
            y_max,
            width,
            height,
            resolution=self.input_size,
            coordconv=False).unsqueeze(0)

        # Crop the face
        face = img.crop((int(x_min), int(y_min), int(x_max), int(y_max)))

        if self.imshow:
            img.save("img_aug.jpg")
            face.save('face_aug.jpg')

        if self.transform is not None:
            img = self.transform(img)
            face = self.transform(face)

        # generate the heat map used for deconv prediction
        gaze_heatmap = torch.zeros(
            self.output_size, self.output_size)  # set the size of the output
        if self.test:  # aggregated heatmap
            num_valid = 0
            for gaze_x, gaze_y in cont_gaze:
                if gaze_x != -1:
                    num_valid += 1
                    gaze_heatmap = imutils.draw_labelmap(
                        gaze_heatmap,
                        [gaze_x * self.output_size, gaze_y * self.output_size],
                        3,
                        type='Gaussian')
            gaze_heatmap /= num_valid
        else:
            # if gaze_inside:
            gaze_heatmap = imutils.draw_labelmap(
                gaze_heatmap,
                [gaze_x * self.output_size, gaze_y * self.output_size],
                3,
                type='Gaussian')

        if self.imshow:
            fig = plt.figure(111)
            img = 255 - imutils.unnorm(img.numpy()) * 255
            img = np.clip(img, 0, 255)
            plt.imshow(np.transpose(img, (1, 2, 0)))
            plt.imshow(imresize(gaze_heatmap,
                                (self.input_size, self.input_size)),
                       cmap='jet',
                       alpha=0.3)
            plt.imshow(imresize(1 - head_channel.squeeze(0),
                                (self.input_size, self.input_size)),
                       alpha=0.2)
            plt.savefig('viz_aug.png')

        if self.test:
            return img, face, head_channel, gaze_heatmap, cont_gaze, imsize, path
        else:
            return img, face, head_channel, gaze_heatmap, path, gaze_inside
    def __getitem__(self, index):
        sequence_path = self.all_sequence_paths[index]
        df = pd.read_csv(
            sequence_path,
            header=None,
            index_col=False,
            names=['path', 'xmin', 'ymin', 'xmax', 'ymax', 'gazex', 'gazey'])
        show_name = sequence_path.split('/')[-3]
        clip = sequence_path.split('/')[-2]
        seq_len = len(df.index)

        # moving-avg smoothing
        window_size = 11  # should be odd number
        df['xmin'] = myutils.smooth_by_conv(window_size, df, 'xmin')
        df['ymin'] = myutils.smooth_by_conv(window_size, df, 'ymin')
        df['xmax'] = myutils.smooth_by_conv(window_size, df, 'xmax')
        df['ymax'] = myutils.smooth_by_conv(window_size, df, 'ymax')

        if not self.test:
            # cond for data augmentation
            cond_jitter = np.random.random_sample()
            cond_flip = np.random.random_sample()
            cond_color = np.random.random_sample()
            if cond_color < 0.5:
                n1 = np.random.uniform(0.5, 1.5)
                n2 = np.random.uniform(0.5, 1.5)
                n3 = np.random.uniform(0.5, 1.5)
            cond_crop = np.random.random_sample()

            # if longer than seq_len_limit, cut it down to the limit with the init index randomly sampled
            if seq_len > self.seq_len_limit:
                sampled_ind = np.random.randint(0,
                                                seq_len - self.seq_len_limit)
                seq_len = self.seq_len_limit
            else:
                sampled_ind = 0

            if cond_crop < 0.5:
                sliced_x_min = df['xmin'].iloc[sampled_ind:sampled_ind +
                                               seq_len]
                sliced_x_max = df['xmax'].iloc[sampled_ind:sampled_ind +
                                               seq_len]
                sliced_y_min = df['ymin'].iloc[sampled_ind:sampled_ind +
                                               seq_len]
                sliced_y_max = df['ymax'].iloc[sampled_ind:sampled_ind +
                                               seq_len]

                sliced_gaze_x = df['gazex'].iloc[sampled_ind:sampled_ind +
                                                 seq_len]
                sliced_gaze_y = df['gazey'].iloc[sampled_ind:sampled_ind +
                                                 seq_len]

                check_sum = sliced_gaze_x.sum() + sliced_gaze_y.sum()
                all_outside = check_sum == -2 * seq_len

                # Calculate the minimum valid range of the crop that doesn't exclude the face and the gaze target
                if all_outside:
                    crop_x_min = np.min(
                        [sliced_x_min.min(),
                         sliced_x_max.min()])
                    crop_y_min = np.min(
                        [sliced_y_min.min(),
                         sliced_y_max.min()])
                    crop_x_max = np.max(
                        [sliced_x_min.max(),
                         sliced_x_max.max()])
                    crop_y_max = np.max(
                        [sliced_y_min.max(),
                         sliced_y_max.max()])
                else:
                    crop_x_min = np.min([
                        sliced_gaze_x.min(),
                        sliced_x_min.min(),
                        sliced_x_max.min()
                    ])
                    crop_y_min = np.min([
                        sliced_gaze_y.min(),
                        sliced_y_min.min(),
                        sliced_y_max.min()
                    ])
                    crop_x_max = np.max([
                        sliced_gaze_x.max(),
                        sliced_x_min.max(),
                        sliced_x_max.max()
                    ])
                    crop_y_max = np.max([
                        sliced_gaze_y.max(),
                        sliced_y_min.max(),
                        sliced_y_max.max()
                    ])

                # Randomly select a random top left corner
                if crop_x_min >= 0:
                    crop_x_min = np.random.uniform(0, crop_x_min)
                if crop_y_min >= 0:
                    crop_y_min = np.random.uniform(0, crop_y_min)

                # Get image size
                path = os.path.join(self.data_dir, show_name, clip,
                                    df['path'].iloc[0])
                img = Image.open(path)
                img = img.convert('RGB')
                width, height = img.size

                # Find the range of valid crop width and height starting from the (crop_x_min, crop_y_min)
                crop_width_min = crop_x_max - crop_x_min
                crop_height_min = crop_y_max - crop_y_min
                crop_width_max = width - crop_x_min
                crop_height_max = height - crop_y_min
                # Randomly select a width and a height
                crop_width = np.random.uniform(crop_width_min, crop_width_max)
                crop_height = np.random.uniform(crop_height_min,
                                                crop_height_max)
        else:
            sampled_ind = 0


        faces, images, head_channels, heatmaps, paths, gazes, imsizes, gaze_inouts = [], [], [], [], [], [], [], []
        index_tracker = -1
        for i, row in df.iterrows():
            index_tracker = index_tracker + 1
            if not self.test:
                if index_tracker < sampled_ind or index_tracker >= (
                        sampled_ind + self.seq_len_limit):
                    continue

            face_x1 = row['xmin']  # note: Already in image coordinates
            face_y1 = row['ymin']  # note: Already in image coordinates
            face_x2 = row['xmax']  # note: Already in image coordinates
            face_y2 = row['ymax']  # note: Already in image coordinates
            gaze_x = row['gazex']  # note: Already in image coordinates
            gaze_y = row['gazey']  # note: Already in image coordinates

            impath = os.path.join(self.data_dir, show_name, clip, row['path'])
            img = Image.open(impath)
            img = img.convert('RGB')

            width, height = img.size
            imsize = torch.FloatTensor([width, height])
            # imsizes.append(imsize)

            face_x1, face_y1, face_x2, face_y2 = map(
                float, [face_x1, face_y1, face_x2, face_y2])
            gaze_x, gaze_y = map(float, [gaze_x, gaze_y])
            if gaze_x == -1 and gaze_y == -1:
                gaze_inside = False
            else:
                if gaze_x < 0:  # move gaze point that was sliglty outside the image back in
                    gaze_x = 0
                if gaze_y < 0:
                    gaze_y = 0
                gaze_inside = True

            if not self.test:
                ## data augmentation
                # Jitter (expansion-only) bounding box size.
                if cond_jitter < 0.5:
                    k = cond_jitter * 0.1
                    face_x1 -= k * abs(face_x2 - face_x1)
                    face_y1 -= k * abs(face_y2 - face_y1)
                    face_x2 += k * abs(face_x2 - face_x1)
                    face_y2 += k * abs(face_y2 - face_y1)
                    face_x1 = np.clip(face_x1, 0, width)
                    face_x2 = np.clip(face_x2, 0, width)
                    face_y1 = np.clip(face_y1, 0, height)
                    face_y2 = np.clip(face_y2, 0, height)

                # Random Crop
                if cond_crop < 0.5:
                    # Crop it
                    img = TF.crop(img, crop_y_min, crop_x_min, crop_height,
                                  crop_width)

                    # Record the crop's (x, y) offset
                    offset_x, offset_y = crop_x_min, crop_y_min

                    # convert coordinates into the cropped frame
                    face_x1, face_y1, face_x2, face_y2 = face_x1 - offset_x, face_y1 - offset_y, face_x2 - offset_x, face_y2 - offset_y
                    if gaze_inside:
                        gaze_x, gaze_y = (gaze_x- offset_x), \
                                         (gaze_y - offset_y)
                    else:
                        gaze_x = -1
                        gaze_y = -1

                    width, height = crop_width, crop_height

                # Flip?
                if cond_flip < 0.5:
                    img = img.transpose(Image.FLIP_LEFT_RIGHT)
                    x_max_2 = width - face_x1
                    x_min_2 = width - face_x2
                    face_x2 = x_max_2
                    face_x1 = x_min_2
                    if gaze_x != -1 and gaze_y != -1:
                        gaze_x = width - gaze_x

                # Random color change
                if cond_color < 0.5:
                    img = TF.adjust_brightness(img, brightness_factor=n1)
                    img = TF.adjust_contrast(img, contrast_factor=n2)
                    img = TF.adjust_saturation(img, saturation_factor=n3)

            # Face crop
            face = img.copy().crop(
                (int(face_x1), int(face_y1), int(face_x2), int(face_y2)))

            # Head channel image
            head_channel = imutils.get_head_box_channel(
                face_x1,
                face_y1,
                face_x2,
                face_y2,
                width,
                height,
                resolution=self.input_size,
                coordconv=False).unsqueeze(0)
            if self.transform is not None:
                img = self.transform(img)
                face = self.transform(face)

            # Deconv output
            if gaze_inside:
                gaze_x /= float(width)  # fractional gaze
                gaze_y /= float(height)
                gaze_heatmap = torch.zeros(
                    self.output_size,
                    self.output_size)  # set the size of the output
                gaze_map = imutils.draw_labelmap(
                    gaze_heatmap,
                    [gaze_x * self.output_size, gaze_y * self.output_size],
                    3,
                    type='Gaussian')
                gazes.append(torch.FloatTensor([gaze_x, gaze_y]))
            else:
                gaze_map = torch.zeros(self.output_size, self.output_size)
                gazes.append(torch.FloatTensor([-1, -1]))
            faces.append(face)
            images.append(img)
            head_channels.append(head_channel)
            heatmaps.append(gaze_map)
            gaze_inouts.append(torch.FloatTensor([int(gaze_inside)]))

        if self.imshow:
            for i in range(len(faces)):
                fig = plt.figure(111)
                img = 255 - imutils.unnorm(images[i].numpy()) * 255
                img = np.clip(img, 0, 255)
                plt.imshow(np.transpose(img, (1, 2, 0)))
                plt.imshow(imresize(heatmaps[i],
                                    (self.input_size, self.input_size)),
                           cmap='jet',
                           alpha=0.3)
                plt.imshow(imresize(1 - head_channels[i].squeeze(0),
                                    (self.input_size, self.input_size)),
                           alpha=0.2)
                plt.savefig(
                    os.path.join('debug',
                                 'viz_%d_inout=%d.png' % (i, gaze_inouts[i])))
                plt.close('all')

        faces = torch.stack(faces)
        images = torch.stack(images)
        head_channels = torch.stack(head_channels)
        heatmaps = torch.stack(heatmaps)
        gazes = torch.stack(gazes)
        gaze_inouts = torch.stack(gaze_inouts)
        # imsizes = torch.stack(imsizes)
        # print(faces.shape, images.shape, head_channels.shape, heatmaps.shape)

        if self.test:
            return images, faces, head_channels, heatmaps, gazes, gaze_inouts
        else:  # train
            return images, faces, head_channels, heatmaps, gaze_inouts
示例#15
0
 def adjustBrightness(self, x):
     return F.adjust_brightness(x, self.brightness_factor)
示例#16
0
 def torchvision(self, img):
     return torchvision.adjust_brightness(img, brightness_factor=1.5)
示例#17
0
 def __call__(self, img, masks):
     return tf.adjust_brightness(img,
                                 random.uniform(1 - self.bf,
                                                1 + self.bf)), masks
示例#18
0
    def __getitem__(self, index):
        data_path = self.data_list[index % self.total]
        xml_path = self.label_list[index % self.total]

        xml_tree = ET.parse(xml_path)
        xml_root = xml_tree.getroot()

        filename = xml_root.find('filename').text
        hwc = np.array([
            int(xml_root.find('size').find('height').text),
            int(xml_root.find('size').find('width').text),
            int(xml_root.find('size').find('depth').text)
        ])

        labels = []
        bboxes = []
        for obj in xml_root.findall('object'):
            labels.append(VOC_LABELS[obj.find('name').text][0])

            bbox = []
            bbox.append(float(obj.find('bndbox').find('xmin').text))
            bbox.append(float(obj.find('bndbox').find('ymin').text))
            bbox.append(float(obj.find('bndbox').find('xmax').text))
            bbox.append(float(obj.find('bndbox').find('ymax').text))
            bboxes.append(bbox)

        labels = np.array(labels).astype(np.int)
        bboxes = np.array(bboxes).astype(np.int)

        # --------------------------------------------------------------

        rand_scale = np.random.uniform(0.85, 1.0)
        crop_size = int(np.min([hwc[0], hwc[1]]) * rand_scale)

        rand_i = np.random.randint(0, hwc[0] -
                                   crop_size) if hwc[0] > crop_size else 0
        rand_j = np.random.randint(0, hwc[1] -
                                   crop_size) if hwc[1] > crop_size else 0

        rand_hue = np.random.uniform(-0.3, 0.3)
        rand_num = [np.random.uniform(0.6, 1.5) for i in range(4)]

        img = Image.open(data_path)
        img = fn.resized_crop(img,
                              rand_i,
                              rand_j,
                              crop_size,
                              crop_size, (self.img_size, self.img_size),
                              interpolation=2)

        ratio = self.img_size / crop_size
        bboxes = np.concatenate([
            np.maximum(bboxes[:, 0:1], rand_j) - rand_j,
            np.maximum(bboxes[:, 1:2], rand_i) - rand_i,
            np.minimum(bboxes[:, 2:3], rand_j + crop_size) - rand_j,
            np.minimum(bboxes[:, 3:], rand_i + crop_size) - rand_i
        ],
                                axis=-1) * ratio

        if self.train:
            img = fn.adjust_hue(img, rand_hue)
            img = fn.adjust_contrast(img, rand_num[0])
            img = fn.adjust_brightness(img, rand_num[1])
            img = fn.adjust_saturation(img, rand_num[2])

            if rand_num[3] > 1.05:
                img = fn.hflip(img)
                bboxes = np.concatenate(
                    [(bboxes[:, 2:3] - self.img_size) * (-1), bboxes[:, 1:2],
                     (bboxes[:, 0:1] - self.img_size) * (-1), bboxes[:, 3:]],
                    axis=-1)

        img = np.array(img).transpose(2, 0, 1)
        bboxes = bboxes.astype(np.int)

        return img, filename, hwc, labels, bboxes
示例#19
0
 def torchvision(self, img):
     return torchvision.adjust_brightness(img, brightness_factor=1.5)
示例#20
0
 def bright(self, O, B):
     brightness_factor = self.rand_state.uniform(0.3, 1.7)
     O = TF.adjust_brightness(img=O, brightness_factor=brightness_factor)
     B = TF.adjust_brightness(img=B, brightness_factor=brightness_factor)
     return O, B
示例#21
0
 def torchvision(self, img):
     img = torchvision.adjust_hue(img, hue_factor=0.1)
     img = torchvision.adjust_saturation(img, saturation_factor=1.2)
     img = torchvision.adjust_brightness(img, brightness_factor=1.2)
     return img
def makeAgumentedTest(test, test_labels):
    num_image = test.shape[0]
    translatedImages = []
    brightenedImages = []
    darkenedImages = []
    hContrasted = []
    lContrasted = []
    flippedImages = []
    invertedImages = []

    for i in range(num_image):
        img  = test[i, :, :, :]

        img = Image.fromarray(img)
        # Do Translation
        translatedIMG = transforms.affine(img, angle=0, translate=(50, 50), scale=1, shear=0)
        # Do Brigthened
        brightenedIMG = transforms.adjust_brightness(img, 1.5)
        # Do Darkened
        darkenedIMG = transforms.adjust_brightness(img, 0.75)
        # Do High Contrast
        highContrastIMG = transforms.adjust_contrast(img, 1.5)
        # Do Low Contrast
        lowContrastIMG  = transforms.adjust_contrast(img, 0.75)
        # Flipped Upside Down
        flippedIMG = transforms.hflip(img)
        # Colors Inverted
        invertedIMG = ImageOps.invert(img)

        translatedImages.append(np.expand_dims(np.array(translatedIMG), axis=0))
        brightenedImages.append(np.expand_dims(np.array(brightenedIMG), axis=0))
        darkenedImages.append(np.expand_dims(np.array(darkenedIMG), axis=0))
        hContrasted.append(np.expand_dims(np.array(highContrastIMG), axis=0))
        lContrasted.append(np.expand_dims(np.array(lowContrastIMG), axis=0))
        flippedImages.append(np.expand_dims(np.array(flippedIMG), axis=0))
        invertedImages.append(np.expand_dims(np.array(invertedIMG), axis=0))


    translatedImages = np.vstack(translatedImages)
    brightenedImages = np.vstack(brightenedImages)
    darkenedImages = np.vstack(darkenedImages)
    hContrasted = np.vstack(hContrasted)
    lContrasted = np.vstack(lContrasted)
    flippedImages = np.vstack(flippedImages)
    invertedImages = np.vstack(invertedImages)


    augmentedTest = {
        'translated': translatedImages,
        'brightened': brightenedImages,
        'darkened': darkenedImages,
        'high_contrast': hContrasted,
        'low_contrast': lContrasted,
        'flipped': flippedImages,
        'inverted': invertedImages,
        'labels': test_labels
    }

    with open('augmented_test.pkl', 'wb') as outfile:
        pickle.dump(augmentedTest, outfile)

    print('Done!')
 def __call__(self, img):
     out = F.adjust_brightness(img, self.brightness)
     out = F.adjust_contrast(out, self.contrast)
     out = F.adjust_hue(out, self.hue)
     return out
示例#24
0
def ColorAdjust(image, factor):
    image = F.adjust_brightness(image, factor)
    image = F.adjust_contrast(image, factor)
    image = F.adjust_saturation(image, factor)
    return F.to_tensor(image)
示例#25
0
 def __call__(self, image, target):
     if random.random() < self.prob:
         brightness_factor = random.uniform(0.5, 2)
         image = F.adjust_brightness(image, brightness_factor)
     return image, target
示例#26
0
    def data_augment(self, images, edges, imgh, imgw):
        images = Image.fromarray(np.uint8(np.asarray(images)), mode="RGB")
        mask = Image.fromarray(np.uint8(np.ones_like(edges)))
        edges = Image.fromarray(np.uint8(edges))

        if np.random.binomial(1, 0.8) > 0:
            brightness_factor = np.random.uniform(0.8, 1.2)
            images = F.adjust_brightness(images, brightness_factor)
        if np.random.binomial(1, 0.8) > 0:
            contrast_factor = np.random.uniform(0.5, 2)
            images = F.adjust_contrast(images, contrast_factor)
        if np.random.binomial(1, 0.8) > 0:
            hue_factor = np.random.uniform(-0.2, 0.2)
            images = F.adjust_hue(images, hue_factor)
        if np.random.binomial(1, 0.8) > 0:
            saturation_factor = np.random.uniform(0.8, 1.2)
            images = F.adjust_saturation(images, saturation_factor)

        if np.random.binomial(1, 0.8) > 0:
            angle = random.randint(-2, 2)
            translate = (random.randint(-int(imgw * 0.1), int(imgw * 0.1)),
                         random.randint(-int(imgh * 0.1), int(imgh * 0.1)))
            scale = np.random.uniform(0.9, 1.1)
            if scale < 1:  # scale:[0.9,1.1]
                scale = scale / 2 + 0.5
            shear = random.randint(-2, 2)
            images = F.affine(images,
                              angle,
                              translate,
                              scale,
                              shear,
                              resample=Image.BICUBIC)
            edges = F.affine(edges,
                             angle,
                             translate,
                             scale,
                             shear,
                             resample=Image.BICUBIC)
            mask = F.affine(mask,
                            angle,
                            translate,
                            scale,
                            shear,
                            resample=Image.BICUBIC)

        if np.random.binomial(1, 0.5) > 0:
            images = F.hflip(images)
            edges = F.hflip(edges)
            mask = F.hflip(mask)

        if np.random.binomial(1, 0.5) > 0:
            images = F.vflip(images)
            edges = F.vflip(edges)
            mask = F.vflip(mask)
        images = np.asarray(images)
        edges = np.asarray(edges)
        mask = np.asarray(mask)

        # https://github.com/mdbloice/Augmentor
        images = [[images, edges, mask]]
        p = Augmentor.DataPipeline(images)
        p.random_distortion(1, 10, 10, 10)
        g = p.generator(batch_size=1)
        augmented_images = next(g)
        images = augmented_images[0][0]
        edges = augmented_images[0][1]
        mask = augmented_images[0][2]

        edges = edges.copy()
        edges[edges < 128] = 0
        edges[edges >= 128] = 1
        edges = (morphology.skeletonize(edges)).astype(np.uint8)

        edges[edges == 1] = 255
        edges = self.resize(edges, imgh, imgw)
        mask = self.resize(mask, imgh, imgw)

        # 1.not thin edge truth
        edges[edges > 0] = 255

        # 2.thin edge truth
        # edges[edges>0]=1
        # edges = (morphology.skeletonize(edges)).astype(np.uint8)
        # edges[edges==1] = 255

        edges = (255 - edges) * mask
        return images, edges, mask * 255
    def __call__(self, img, target):
        factor = random.uniform(-self.factor, self.factor)
        img = F.adjust_brightness(img, 1 + factor)

        return img, target
示例#28
0
 def __call__(self, image):
     factor = np.random.uniform(self.low, self.high)
     return TF.adjust_brightness(image, factor)
示例#29
0
    def __getitem__(self, index):

        squeen = self.sequeueslists[index]
        image_filenames = [
            join(squeen, x) for x in listdir(squeen) if is_image_file(x)
        ]

        randi = 0
        cropsize = self.crop_size
        hr_scale = Resize((cropsize, cropsize), interpolation=Image.BICUBIC)

        # first image of seq
        imgname = join(squeen, str(randi + 1) + '.jpg')
        hr_image = Image.open(imgname)
        w, h = hr_image.size
        ragey = random.randint(0, h - cropsize)
        rangx = random.randint(0, w - cropsize)

        ################################
        hr_image = self.seq_randomcrop(hr_image, ragey, rangx, cropsize,
                                       cropsize)

        hfp = random.random()
        if hfp < 0.5:
            hr_image = self.randomHflip(hr_image)

        vfp = random.random()
        if vfp < 0.5:
            hr_image = self.randomVflip(hr_image)

        brightness = 0.2
        contrast = 0.2
        saturation = 0.1
        hue = 0.1
        transforms = []

        brightness_factor = np.random.uniform(max(0, 1 - brightness),
                                              1 + brightness)
        transforms.append(
            Lambda(lambda img: F.adjust_brightness(img, brightness_factor)))

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

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

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

        np.random.shuffle(transforms)
        color_transform = Compose(transforms)

        hr_image = color_transform(hr_image)
        ################################

        hr_image = ToTensor()(hr_image)

        lr_image = self.lr_transform(hr_image)
        # Y channel
        hr_image = torch.unsqueeze(hr_image, dim=0)
        bic_hr = torch.unsqueeze(ToTensor()(hr_scale(lr_image)), dim=0)
        lr_image = torch.unsqueeze(ToTensor()(lr_image), dim=0)

        t0 = lr_image
        t1 = bic_hr
        t2 = hr_image

        for i in range(randi + 1, randi + self.relation):
            imgname = join(squeen, str(i + 1) + '.jpg')
            hr_image = Image.open(imgname)
            # data argument
            hr_image = self.seq_randomcrop(hr_image, rangx, ragey, cropsize,
                                           cropsize)
            if hfp < 0.5:
                hr_image = self.randomHflip(hr_image)

            if vfp < 0.5:
                hr_image = self.randomVflip(hr_image)

            hr_image = color_transform(hr_image)

            hr_image = ToTensor()(hr_image)

            lr_image = self.lr_transform(hr_image)
            # Y channel
            hr_image = torch.unsqueeze(hr_image, dim=0)
            bic_hr = torch.unsqueeze(ToTensor()(hr_scale(lr_image)), dim=0)
            lr_image = torch.unsqueeze(ToTensor()(lr_image), dim=0)

            t0 = torch.cat((t0, lr_image), 0)
            t1 = torch.cat((t1, bic_hr), 0)
            t2 = torch.cat((t2, hr_image), 0)

        return t0, t1, t2
示例#30
0
 def colorop(img, bright, contrast):
     _img = TF.adjust_brightness(img, bright)
     _img = TF.adjust_contrast(_img, contrast)
     return _img
示例#31
0
    def __getitem__(self, idx):
        rgb, depth, gt, confidence, K = self._load_data(idx)

        if self.augment and self.mode == 'train':
            # Top crop if needed
            if self.args.top_crop > 0:
                width, height = rgb.size
                rgb = TF.crop(rgb, self.args.top_crop, 0,
                              height - self.args.top_crop, width)
                depth = TF.crop(depth, self.args.top_crop, 0,
                                height - self.args.top_crop, width)
                confidence = TF.crop(confidence, self.args.top_crop, 0,
                                     height - self.args.top_crop, width)
                gt = TF.crop(gt, self.args.top_crop, 0,
                             height - self.args.top_crop, width)
                K[3] = K[3] - self.args.top_crop

            width, height = rgb.size

            _scale = np.random.uniform(1.0, 1.5)
            scale = np.int(height * _scale)
            degree = np.random.uniform(-5.0, 5.0)
            flip = np.random.uniform(0.0, 1.0)

            # Horizontal flip
            if flip > 0.5:
                rgb = TF.hflip(rgb)
                depth = TF.hflip(depth)
                confidence = TF.hflip(confidence)
                gt = TF.hflip(gt)
                K[2] = width - K[2]

            # Rotation
            rgb = TF.rotate(rgb, angle=degree, resample=Image.BICUBIC)
            depth = TF.rotate(depth, angle=degree, resample=Image.NEAREST)
            confidence = TF.rotate(confidence,
                                   angle=degree,
                                   resample=Image.NEAREST)
            gt = TF.rotate(gt, angle=degree, resample=Image.NEAREST)

            # Color jitter
            brightness = np.random.uniform(0.6, 1.4)
            contrast = np.random.uniform(0.6, 1.4)
            saturation = np.random.uniform(0.6, 1.4)

            rgb = TF.adjust_brightness(rgb, brightness)
            rgb = TF.adjust_contrast(rgb, contrast)
            rgb = TF.adjust_saturation(rgb, saturation)

            # Resize
            rgb = TF.resize(rgb, scale, Image.BICUBIC)
            depth = TF.resize(depth, scale, Image.NEAREST)
            confidence = TF.resize(confidence, scale, Image.NEAREST)
            gt = TF.resize(gt, scale, Image.NEAREST)

            K[0] = K[0] * _scale
            K[1] = K[1] * _scale
            K[2] = K[2] * _scale
            K[3] = K[3] * _scale

            # Crop
            width, height = rgb.size

            assert self.height <= height and self.width <= width, \
                "patch size is larger than the input size"

            h_start = random.randint(0, height - self.height)
            w_start = random.randint(0, width - self.width)

            rgb = TF.crop(rgb, h_start, w_start, self.height, self.width)
            depth = TF.crop(depth, h_start, w_start, self.height, self.width)
            confidence = TF.crop(confidence, h_start, w_start, self.height,
                                 self.width)
            gt = TF.crop(gt, h_start, w_start, self.height, self.width)

            K[2] = K[2] - w_start
            K[3] = K[3] - h_start

            rgb = TF.to_tensor(rgb)
            rgb = TF.normalize(rgb, (0.485, 0.456, 0.406),
                               (0.229, 0.224, 0.225),
                               inplace=True)

            depth = TF.to_tensor(np.array(depth))
            depth = depth / _scale

            confidence = TF.to_tensor(np.array(confidence))
            confidence = confidence / _scale

            gt = TF.to_tensor(np.array(gt))
            gt = gt / _scale
        elif self.mode in ['train', 'val']:
            # Top crop if needed
            if self.args.top_crop > 0:
                width, height = rgb.size
                rgb = TF.crop(rgb, self.args.top_crop, 0,
                              height - self.args.top_crop, width)
                depth = TF.crop(depth, self.args.top_crop, 0,
                                height - self.args.top_crop, width)
                confidence = TF.crop(confidence, self.args.top_crop, 0,
                                     height - self.args.top_crop, width)
                gt = TF.crop(gt, self.args.top_crop, 0,
                             height - self.args.top_crop, width)
                K[3] = K[3] - self.args.top_crop

            # Crop
            width, height = rgb.size

            assert self.height <= height and self.width <= width, \
                "patch size is larger than the input size"

            h_start = random.randint(0, height - self.height)
            w_start = random.randint(0, width - self.width)

            rgb = TF.crop(rgb, h_start, w_start, self.height, self.width)
            depth = TF.crop(depth, h_start, w_start, self.height, self.width)
            confidence = TF.crop(confidence, h_start, w_start, self.height,
                                 self.width)
            gt = TF.crop(gt, h_start, w_start, self.height, self.width)

            K[2] = K[2] - w_start
            K[3] = K[3] - h_start

            rgb = TF.to_tensor(rgb)
            rgb = TF.normalize(rgb, (0.485, 0.456, 0.406),
                               (0.229, 0.224, 0.225),
                               inplace=True)

            depth = TF.to_tensor(np.array(depth))
            confidence = TF.to_tensor(np.array(confidence))

            gt = TF.to_tensor(np.array(gt))
        else:
            if self.args.top_crop > 0 and self.args.test_crop:
                width, height = rgb.size
                rgb = TF.crop(rgb, self.args.top_crop, 0,
                              height - self.args.top_crop, width)
                depth = TF.crop(depth, self.args.top_crop, 0,
                                height - self.args.top_crop, width)
                confidence = TF.crop(confidence, self.args.top_crop, 0,
                                     height - self.args.top_crop, width)
                gt = TF.crop(gt, self.args.top_crop, 0,
                             height - self.args.top_crop, width)
                K[3] = K[3] - self.args.top_crop

            rgb = TF.to_tensor(rgb)
            rgb = TF.normalize(rgb, (0.485, 0.456, 0.406),
                               (0.229, 0.224, 0.225),
                               inplace=True)

            depth = TF.to_tensor(np.array(depth))
            confidence = TF.to_tensor(np.array(confidence))
            gt = TF.to_tensor(np.array(gt))

        if self.args.num_sample > 0:
            depth, confidence = self.get_sparse_depth(depth, confidence,
                                                      self.args.num_sample)

        output = {
            'rgb': rgb,
            'dep': depth,
            'confidence': confidence,
            'gt': gt,
            'K': torch.Tensor(K)
        }

        return output
def melanoma_image_aug(image_path):
    '''
    Transformations:
    horizontal flip
    vertical flip
    scale = 0.9
    shear = 8.0
    rotation = 150.0
    brightness factor = 223 / 255
    saturation factor = 0.5
    '''
    # full_rot = 180
    # scale = (0.8, 1.2)
    # shear = 10
    cutout = int(16 * (600 / 224))
    cutout_coordinate = [100, 200]

    melanoma_image = Image.open(image_path)

    horizontal_flip = transforms.RandomHorizontalFlip(p=1.0)

    # vertical_flip = transforms.RandomVerticalFlip(p=1.0)
    # Cutout
    cutout = Cutout_v0(n_holes=1,
                       length=cutout,
                       x=cutout_coordinate[0],
                       y=cutout_coordinate[1])

    all_transforms_melanoma_image = melanoma_image

    horizontal_flip_melanoma_image = horizontal_flip(melanoma_image)
    all_transforms_melanoma_image = horizontal_flip(
        all_transforms_melanoma_image)

    # vertical_flip_melanoma_image = vertical_flip(melanoma_image)
    # all_transforms_melanoma_image = vertical_flip(all_transforms_melanoma_image)

    scale_melanoma_image = functional.affine(melanoma_image,
                                             angle=0,
                                             translate=[0, 0],
                                             shear=[0.0],
                                             scale=0.9)
    all_transforms_melanoma_image = functional.affine(
        all_transforms_melanoma_image,
        angle=0,
        translate=[0, 0],
        shear=[0.0],
        scale=0.9)

    shear_melanoma_image = functional.affine(melanoma_image,
                                             angle=0,
                                             translate=[0, 0],
                                             shear=[8.0],
                                             scale=1)
    all_transforms_melanoma_image = functional.affine(
        all_transforms_melanoma_image,
        angle=0,
        translate=[0, 0],
        shear=[8.0],
        scale=1)

    rotate_melanoma_image = functional.affine(melanoma_image,
                                              angle=150,
                                              translate=[0, 0],
                                              shear=[0.0],
                                              scale=1,
                                              resample=Image.BILINEAR)
    all_transforms_melanoma_image = functional.affine(
        all_transforms_melanoma_image,
        angle=150,
        translate=[0, 0],
        shear=[0.0],
        scale=1,
        resample=Image.BILINEAR)

    brightness_melanoma_image = functional.adjust_brightness(
        melanoma_image, brightness_factor=1 - 32. / 255.)
    all_transforms_melanoma_image = functional.adjust_brightness(
        all_transforms_melanoma_image, brightness_factor=1 - 32. / 255.)

    saturation_melanoma_image = functional.adjust_saturation(
        melanoma_image, saturation_factor=0.5)
    all_transforms_melanoma_image = functional.adjust_saturation(
        all_transforms_melanoma_image, saturation_factor=0.5)

    cutout_melanoma_image = cutout(melanoma_image)
    all_transforms_melanoma_image = cutout(all_transforms_melanoma_image)

    melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\orginial.jpg"
    )
    horizontal_flip_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\horizontal_flip.jpg"
    )
    # vertical_flip_melanoma_image.save(r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\vertical_flip.jpg")
    scale_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\scale.jpg")
    shear_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\shear.jpg")
    rotate_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\rotate.jpg")
    brightness_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\brightness.jpg"
    )
    saturation_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\saturation.jpg"
    )
    cutout_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\cutout.jpg")

    all_transforms_melanoma_image.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\all_transforms.jpg"
    )
    random.seed(42)

    scale = random.uniform(0.8, 1.2)
    shear = random.uniform(-10, 10)
    rotate = random.uniform(-180, 180)
    brightness = random.uniform(1 - 32. / 255., 1 + 32. / 255.)
    saturation = random.uniform(0.5, 1.5)
    cutout_coordiante = [
        random.randrange(100, 400),
        random.randrange(100, 400)
    ]

    print("Rand 1")
    print("Scale " + str(scale))
    print("Shear " + str(shear))
    print("Rotate " + str(rotate))
    print("Brigthness " + str(brightness))
    print("Saturation " + str(saturation))
    print("Cutout coordinates" + str(cutout_coordiante))

    all_transforms_1 = deterministic_daisy_lab_image_data_augmenter(
        melanoma_image,
        cutoutCordinates=cutout_coordiante,
        scale=scale,
        shear=shear,
        rotate=rotate,
        brightness=brightness,
        saturation=saturation,
        horizontalFlip=True)

    all_transforms_1.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\rand_1.jpg")

    scale = random.uniform(0.8, 1.2)
    shear = random.uniform(-10, 10)
    rotate = random.uniform(-180, 180)
    brightness = random.uniform(1 - 32. / 255., 1 + 32. / 255.)
    saturation = random.uniform(0.5, 1.5)
    cutout_coordiante = [
        random.randrange(100, 400),
        random.randrange(100, 400)
    ]

    print("Rand 2")
    print("Scale " + str(scale))
    print("Shear " + str(shear))
    print("Rotate " + str(rotate))
    print("Brigthness " + str(brightness))
    print("Saturation " + str(saturation))
    print("Cutout " + str(cutout_coordiante))

    all_transforms_2 = deterministic_daisy_lab_image_data_augmenter(
        melanoma_image,
        cutoutCordinates=cutout_coordiante,
        scale=scale,
        shear=shear,
        rotate=rotate,
        brightness=brightness,
        saturation=saturation,
        horizontalFlip=False)

    all_transforms_2.save(
        r"C:\Users\Bruger\PycharmProjects\Bachelor\Article_figures\rand_2.jpg")
示例#33
0
 def torchvision(self, img):
     img = torchvision.adjust_hue(img, hue_factor=0.1)
     img = torchvision.adjust_saturation(img, saturation_factor=1.2)
     img = torchvision.adjust_brightness(img, brightness_factor=1.2)
     return img
 def __call__(self, img):
     return F.adjust_brightness(img, self.brightness)