Пример #1
0
    def __call__(self, img, lbl):
        if random.random() < self.p:
            img = F.vflip(img)
            lbl = F.vflip(lbl)

        return img, lbl
Пример #2
0
    def __getitem__(self, index):
        """Reads an image from a file and preprocesses it and returns."""
        image_path = self.image_paths[index]
        filename = image_path.split('_')[-1][:-len(".jpg")]
        GT_path = self.GT_paths + 'ISIC_' + filename + '_segmentation.png'

        image = Image.open(image_path)
        GT = Image.open(GT_path)

        # 宽高比
        aspect_ratio = image.size[1] / image.size[0]

        Transform = []

        ResizeRange = random.randint(300, 320)
        Transform.append(
            T.Resize((int(ResizeRange * aspect_ratio), ResizeRange)))
        p_transform = random.random()

        if (self.mode == 'train') and p_transform <= self.augmentation_prob:
            RotationDegree = random.randint(0, 3)
            RotationDegree = self.RotationDegree[RotationDegree]
            if (RotationDegree == 90) or (RotationDegree == 270):
                aspect_ratio = 1 / aspect_ratio

            Transform.append(T.RandomRotation(
                (RotationDegree, RotationDegree)))

            RotationRange = random.randint(-10, 10)
            Transform.append(T.RandomRotation((RotationRange, RotationRange)))
            CropRange = random.randint(250, 270)
            Transform.append(
                T.CenterCrop((int(CropRange * aspect_ratio), CropRange)))
            Transform = T.Compose(Transform)

            image = Transform(image)
            GT = Transform(GT)

            ShiftRange_left = random.randint(0, 20)
            ShiftRange_upper = random.randint(0, 20)
            ShiftRange_right = image.size[0] - random.randint(0, 20)
            ShiftRange_lower = image.size[1] - random.randint(0, 20)
            image = image.crop(box=(ShiftRange_left, ShiftRange_upper,
                                    ShiftRange_right, ShiftRange_lower))
            GT = GT.crop(box=(ShiftRange_left, ShiftRange_upper,
                              ShiftRange_right, ShiftRange_lower))

            if random.random() < 0.5:
                image = F.hflip(image)
                GT = F.hflip(GT)

            if random.random() < 0.5:
                image = F.vflip(image)
                GT = F.vflip(GT)

            Transform = T.ColorJitter(brightness=0.2, contrast=0.2, hue=0.02)

            image = Transform(image)

            Transform = []

        Transform.append(
            T.Resize(
                (int(256 * aspect_ratio) - int(256 * aspect_ratio) % 16, 256)))
        Transform.append(T.ToTensor())
        Transform = T.Compose(Transform)

        image = Transform(image)
        GT = Transform(GT)

        Norm_ = T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        image = Norm_(image)

        return image, GT
Пример #3
0
    def __call__(self, imgs):
        if isinstance(imgs, Image.Image):
            imgs = [imgs]

        assert len(imgs) >= 1
        size = None
        for img_no, img in enumerate(imgs):
            assert isinstance(img, Image.Image)
        #     if img_no == 0:
        #         size = img.size
        #     else:
        #         assert size == img.size

        trans_imgs = copy.deepcopy(imgs)

        for trans in self.transformation_list:
            if isinstance(trans, transforms.RandomRotation):
                angle = trans.get_params(trans.degrees)
                for img_no, trans_img in enumerate(trans_imgs):
                    trans_imgs[img_no] = F.rotate(trans_img, angle,
                                                  trans.resample, trans.expand,
                                                  trans.center)
            elif isinstance(trans, transforms.RandomCrop):
                i, j, th, tw = trans.get_params(trans_imgs[0], trans.size)
                for img_no, trans_img in enumerate(trans_imgs):
                    trans_imgs[img_no] = F.crop(trans_img, i, j, th, tw)
            elif isinstance(trans, transforms.RandomResizedCrop):
                i, j, h, w = trans.get_params(trans_imgs[0], trans.scale,
                                              trans.ratio)
                for img_no, trans_img in enumerate(trans_imgs):
                    trans_imgs[img_no] = F.resized_crop(
                        trans_img, i, j, h, w, trans.size, trans.interpolation)
            elif isinstance(trans, transforms.ColorJitter):
                color_transform = trans.get_params(trans.brightness,
                                                   trans.contrast,
                                                   trans.saturation, trans.hue)
                for img_no, trans_img in enumerate(trans_imgs):
                    trans_imgs[img_no] = color_transform(trans_img)
            elif isinstance(trans, transforms.RandomHorizontalFlip):
                random_value = random.random()
                if random_value < trans.p:
                    for img_no, trans_img in enumerate(trans_imgs):
                        trans_imgs[img_no] = F.hflip(trans_img)
            elif isinstance(trans, transforms.RandomVerticalFlip):
                random_value = random.random()
                if random_value < trans.p:
                    for img_no, trans_img in enumerate(trans_imgs):
                        trans_imgs[img_no] = F.vflip(trans_img)
            elif isinstance(trans,
                            (transforms.Resize, transforms.CenterCrop,
                             transforms.ToTensor, transforms.Normalize)):
                for img_no, trans_img in enumerate(trans_imgs):
                    trans_imgs[img_no] = trans(trans_img)
            else:
                print(trans)
                raise NotImplementedError()

        if len(imgs) == 1:
            trans_imgs = trans_imgs[0]

        return trans_imgs
Пример #4
0
 def __call__(self, image, target):
     if random.random() < self.prob:
         image = F.vflip(image)
         target = target.transpose(1)
     return image, target
    def transform_triplets(self, img, gt1, gt2):

        # resize image and covert to tensor
        img = TF.to_pil_image(img)
        img = TF.resize(img, [self.img_size, self.img_size])

        gt1 = TF.to_pil_image(gt1)
        gt1 = TF.resize(gt1, [self.img_size, self.img_size])

        gt2 = TF.to_pil_image(gt2)
        gt2 = TF.resize(gt2, [self.img_size, self.img_size])

        if self.with_random_hflip and random.random() > 0.5:
            img = TF.hflip(img)
            gt1 = TF.hflip(gt1)
            gt2 = TF.hflip(gt2)

        if self.with_random_vflip and random.random() > 0.5:
            img = TF.vflip(img)
            gt1 = TF.vflip(gt1)
            gt2 = TF.vflip(gt2)

        if self.with_random_rot90 and random.random() > 0.5:
            img = TF.rotate(img, 90)
            gt1 = TF.rotate(gt1, 90)
            gt2 = TF.rotate(gt2, 90)

        if self.with_random_rot180 and random.random() > 0.5:
            img = TF.rotate(img, 180)
            gt1 = TF.rotate(gt1, 180)
            gt2 = TF.rotate(gt2, 180)

        if self.with_random_rot270 and random.random() > 0.5:
            img = TF.rotate(img, 270)
            gt1 = TF.rotate(gt1, 270)
            gt2 = TF.rotate(gt2, 270)

        if self.with_color_jittering and random.random() > 0.5:
            img = TF.adjust_hue(img, hue_factor=random.random() * 0.5 -
                                0.25)  # -0.25 ~ +0.25
            img = TF.adjust_saturation(
                img,
                saturation_factor=random.random() * 0.8 + 0.8)  # 0.8 ~ +1.6
            gt1 = TF.adjust_hue(gt1, hue_factor=random.random() * 0.5 -
                                0.25)  # -0.25 ~ +0.25
            gt1 = TF.adjust_saturation(
                gt1,
                saturation_factor=random.random() * 0.8 + 0.8)  # 0.8 ~ +1.6
            gt2 = TF.adjust_hue(gt2, hue_factor=random.random() * 0.5 -
                                0.25)  # -0.25 ~ +0.25
            gt2 = TF.adjust_saturation(
                gt2,
                saturation_factor=random.random() * 0.8 + 0.8)  # 0.8 ~ +1.6

        if self.with_random_crop and random.random() > 0.5:
            i, j, h, w = transforms.RandomResizedCrop(size=self.img_size). \
                get_params(img=img, scale=(0.5, 1.0), ratio=self.crop_ratio)
            img = TF.resized_crop(img,
                                  i,
                                  j,
                                  h,
                                  w,
                                  size=(self.img_size, self.img_size))
            gt1 = TF.resized_crop(gt1,
                                  i,
                                  j,
                                  h,
                                  w,
                                  size=(self.img_size, self.img_size))
            gt2 = TF.resized_crop(gt2,
                                  i,
                                  j,
                                  h,
                                  w,
                                  size=(self.img_size, self.img_size))

        # to tensor
        img = TF.to_tensor(img)
        gt1 = TF.to_tensor(gt1)
        gt2 = TF.to_tensor(gt2)

        return img, gt1, gt2
Пример #6
0
 def __call__(self, image):
     if random.random() < self.prob:
         image = F.vflip(image)
     return image
Пример #7
0
    def __getitem__(self, idx, seed=None):
        path = self.paths[idx]

        scanner = np.load(path)
        scan, mask = scanner["scan"], scanner["mask"]

        if self.train_test == "train":
            random.seed(seed)
            v_flip = random.random() < self.flip_proba
            h_flip = random.random() < self.flip_proba

            if self.mode == 'concatenate':
                scans = []
                masks = []
                to_pil = Convert()
                to_tens = T.ToTensor()
                for img, msk in zip(scan, mask):
                    img, msk = to_pil(img), to_pil(np.uint8(msk))
                    if v_flip:
                        img = TF.hflip(img)
                        msk = TF.hflip(msk)
                    if h_flip:
                        img = TF.vflip(img)
                        msk = TF.vflip(msk)
                    img, msk = to_tens(img).unsqueeze(0).float(), to_tens(
                        msk).unsqueeze(0).float()
                    if self.noise_magnitude is not None:
                        noise = torch.randn_like(img)
                        img += noise
                    scans.append(img)
                    masks.append(msk)
                scans = torch.cat(scans, dim=1)
                masks = torch.cat(masks, dim=1)
                output = torch.cat([scans, masks], dim=0)

            elif self.mode == 'mask':
                output = np.expand_dims(mask, axis=1)
                output = torch.cat([self.transform(x) for x in output], dim=0)
            elif self.mode == 'scan':
                output = np.expand_dims(scan, axis=1)
                output = torch.cat([self.transform(x) for x in output], dim=0)

        else:
            if self.mode == 'mask':
                output = torch.Tensor(mask).unsqueeze(1).float()
            elif self.mode == 'scan':
                output = torch.Tensor(scan).unsqueeze(1).float()
            elif self.mode == 'concatenate':
                scan = torch.Tensor(scan).unsqueeze(0).float()
                mask = torch.Tensor(mask).unsqueeze(0).float()
                output = torch.cat([scan, mask], dim=0)

        output = {"images": output}
        patient_id = int(get_id_from_path(path))
        if self.csv_dir:
            patient_info = self.ground_truth.loc[self.ground_truth.PatientID ==
                                                 patient_id]
            output['y'] = self.scaler.transform([[patient_info.iloc[0, 1]]])[0]
            output['info'] = np.array(
                [patient_id, int(patient_info.iloc[0, 2])])
        else:
            output['info'] = np.array([patient_id, 1.])

        return output
Пример #8
0
def _verticalFlip(img, segMask=None):
    img = F.vflip(img)
    if segMask is not None:
        segMask = F.vflip(segMask)
    return img, segMask
 def vflip(self, x):
     return F.vflip(x)
Пример #10
0
 def __call__(self, image, segmentation):
     if random.random() < 0.5:
         image = TF.vflip(image)
         segmentation = TF.vflip(segmentation)
     return image, segmentation
Пример #11
0
 def transform_image(self, image):
     return F.vflip(image)
Пример #12
0
def apply_randomTransform(image_left, image_right, size=(224, 224)):

    # Resize
    resize = transforms.Resize(size=size)
    image_left = resize(image_left)
    image_right = resize(image_right)

    # Random crop
    #if random.random() > 0.5:
    #    i, j, h, w = transforms.RandomCrop.get_params(image_left, output_size=(224, 224))
    #   image_left = TF.crop(image_left, i, j, h, w)
    #   image_right = TF.crop(image_right, i, j, h, w)

    # Random horizontal flipping
    if random.random() > 0.5:
        image_left = TF.hflip(image_left)
        image_right = TF.hflip(image_right)

    # Random vertical flipping
    if random.random() > 0.5:
        image_left = TF.vflip(image_left)
        image_right = TF.vflip(image_right)

    # Random rotation
    if random.random() > 0.5:
        angle = random.randint(0, 360)
        image_left = TF.rotate(image_left, angle)
        image_right = TF.rotate(image_right, angle)

    # Random grayscale
    # if random.random() > 0.5:
    #     image_left = TF.to_grayscale(image_left, 3)
    #     image_right = TF.to_grayscale(image_right, 3)

    # Random scale
    if random.random() > 0.5:
        scale = random.uniform(0.2, 2)
        image_left = TF.affine(image_left,
                               angle=0,
                               translate=(0, 0),
                               scale=scale,
                               shear=0)
        image_right = TF.affine(image_right,
                                angle=0,
                                translate=(0, 0),
                                scale=scale,
                                shear=0)

    # Random shear
    if random.random() > 0.5:
        shear = random.randint(-180, 180)
        image_left = TF.affine(image_left,
                               angle=0,
                               translate=(0, 0),
                               scale=1,
                               shear=shear)
        image_right = TF.affine(image_right,
                                angle=0,
                                translate=(0, 0),
                                scale=1,
                                shear=shear)

    return image_left, image_right
Пример #13
0
    def transform(self, img1, img2):

        # resize image and covert to tensor
        img1 = TF.to_pil_image(img1)
        img1 = TF.resize(img1, [self.img_size, self.img_size], interpolation=3)
        img2 = TF.to_pil_image(img2)
        img2 = TF.resize(img2, [self.img_size, self.img_size], interpolation=3)

        if self.with_random_hflip and random.random() > 0.5:
            img1 = TF.hflip(img1)
            img2 = TF.hflip(img2)

        if self.with_random_vflip and random.random() > 0.5:
            img1 = TF.vflip(img1)
            img2 = TF.vflip(img2)

        if self.with_random_rot90 and random.random() > 0.5:
            img1 = TF.rotate(img1, 90)
            img2 = TF.rotate(img2, 90)

        if self.with_random_rot180 and random.random() > 0.5:
            img1 = TF.rotate(img1, 180)
            img2 = TF.rotate(img2, 180)

        if self.with_random_rot270 and random.random() > 0.5:
            img1 = TF.rotate(img1, 270)
            img2 = TF.rotate(img2, 270)

        if self.with_random_crop and random.random() > 0.5:
            i, j, h, w = transforms.RandomResizedCrop(size=self.img_size). \
                get_params(img=img1, scale=(0.5, 1.0), ratio=(0.9, 1.1))
            img1 = TF.resized_crop(img1,
                                   i,
                                   j,
                                   h,
                                   w,
                                   size=(self.img_size, self.img_size))
            img2 = TF.resized_crop(img2,
                                   i,
                                   j,
                                   h,
                                   w,
                                   size=(self.img_size, self.img_size))

        if self.with_random_patch:
            i, j, h, w = transforms.RandomResizedCrop(size=self.img_size). \
                get_params(img=img1, scale=(1/16.0, 1/9.0), ratio=(0.9, 1.1))
            img1 = TF.resized_crop(img1,
                                   i,
                                   j,
                                   h,
                                   w,
                                   size=(self.img_size, self.img_size))
            img2 = TF.resized_crop(img2,
                                   i,
                                   j,
                                   h,
                                   w,
                                   size=(self.img_size, self.img_size))

        # to tensor
        img1 = TF.to_tensor(img1)
        img2 = TF.to_tensor(img2)

        return img1, img2
Пример #14
0
 def __call__(self, img, mask):
     if random.random() < self.p:
         return F.vflip(img), F.vflip(mask)
     return img, mask
Пример #15
0
 def __call__(self, imglist):
     assert len(imglist) <= 2
     if random.random() < self.p:
         return [TF.vflip(img) for img in imglist]
     return imglist
Пример #16
0
 def __call__(self,imgs):
   if random.random() < self.p:
     return [tvF.vflip(img) for img in imgs]
   return imgs
Пример #17
0
 def __call__(self, image, mask):
     if random.random() < self.prob:
         image = F.vflip(image)
         mask = F.vflip(mask)
     return image, mask
Пример #18
0
 def torchvision_transform(self, img):
     return torchvision.vflip(img)
Пример #19
0
 def randomVflip(self, img):
     return F.vflip(img)
Пример #20
0
 def __call__(self, sample):
     sample['image'] = tff.vflip(sample['image'])
     sample['target'] = tff.vflip(sample['target'])
     return sample
Пример #21
0
def _verticalFlip(img, bboxes=None, labels=None):
    img = F.vflip(img)
    if bboxes is not None and len(labels):
        bboxes[:,1] = img.size[1] - (bboxes[:,1] + bboxes[:,3])
    return img, bboxes, labels
Пример #22
0
 def __call__(self, img, anns):
     if random.random() < self.p:
         img = VF.vflip(img)
         anns = HF.vflip(anns, img.size)
         return img, anns
     return img, anns
Пример #23
0
 def data_argument(self, img, seed1, seed2):
     if seed1 > 0.5: img = TF.vflip(img)
     if seed2 > 0.5: img = TF.hflip(img)
     return img
Пример #24
0
    def __getitem__(self, idx):
        if self.phase == 'val':
            idx = idx + self.total_data_num * 4
        elif self.phase == 'test':
            idx = idx + self.total_data_num * 5

        imidx = self.imgs[idx].split("_")[1].replace(".png", "")
        # print("the image index is: ",imidx)
        img_path = os.path.join(self.root_dir, self.imgs[idx])
        # print(img_path)
        # depth_path = os.path.join(self.root_dir, imidx+".npy")
        depth_path = os.path.join(self.root_dir, self.d_imgs[idx])
        # print(depth_path)
        img_rgb = Image.open(img_path)
        depth_npy = np.load(depth_path)
        # print(depth_npy)
        depth_npy[np.isnan(depth_npy)] = max_d = np.nanmax(depth_npy)
        # print(depth_npy.shape)

        ############ edited by wei change to float, can not run
        # img_depth = Image.fromarray(depth_npy, mode='F')
        img_depth = Image.fromarray(depth_npy)
        # print(img_depth)
        transform = T.Compose([T.ToTensor()])

        if self.phase == 'test':
            if self.use_transform:
                img_rgb = transform(img_rgb)
                img_depth = transform(img_depth)
                # mask = transform(mask)
            img_depth = normalize(img_depth)
            sample = {'rgb': img_rgb, 'X': img_depth}

        #### train and val datasets
        else:
            corners_label = Image.open(
                os.path.join(self.root_dir, imidx + '_labels_orange.png'))
            edges_label = Image.open(
                os.path.join(self.root_dir, imidx + '_labels_yellow.png'))
            inner_edges_label = Image.open(
                os.path.join(self.root_dir, imidx + '_labels_green.png'))

            if self.use_transform:
                if random.random() > 0.5:
                    # vertical flip
                    img_rgb = tf.hflip(img_rgb)
                    img_depth = tf.hflip(img_depth)
                    corners_label = tf.hflip(corners_label)
                    edges_label = tf.hflip(edges_label)
                    inner_edges_label = tf.hflip(inner_edges_label)
                    # horizontal flip
                if random.random() > 0.5:
                    img_rgb = tf.vflip(img_rgb)
                    img_depth = tf.vflip(img_depth)
                    corners_label = tf.vflip(corners_label)
                    edges_label = tf.vflip(edges_label)
                    inner_edges_label = tf.vflip(inner_edges_label)
                    # rotation
                if random.random() > 0.9:
                    angle = T.RandomRotation.get_params([-30, 30])
                    img_rgb = tf.rotate(img_rgb, angle, resample=Image.NEAREST)
                    img_depth = tf.rotate(img_depth,
                                          angle,
                                          resample=Image.NEAREST)
                    corners_label = tf.rotate(corners_label,
                                              angle,
                                              resample=Image.NEAREST)
                    edges_label = tf.rotate(edges_label,
                                            angle,
                                            resample=Image.NEAREST)
                    inner_edges_label = tf.rotate(inner_edges_label,
                                                  angle,
                                                  resample=Image.NEAREST)

            img_rgb = transform(img_rgb)
            img_depth = transform(img_depth)

            corners_label = transform(corners_label)
            edges_label = transform(edges_label)
            inner_edges_label = transform(inner_edges_label)

            ##### concatenate the label to 3 * 480 * 640
            ##### three channel GT with each channel a detected colors
            label = torch.cat((corners_label, edges_label, inner_edges_label),
                              0)
            img_depth = normalize(img_depth)

            sample = {'rgb': img_rgb, 'X': img_depth, 'Y': label}
        return sample
Пример #25
0
    def data_aug_func(self,
                      scene=None,
                      current_scene_image=None,
                      obs_traj=None,
                      pred_traj=None,
                      obs_traj_rel=None,
                      pred_traj_rel=None,
                      walls=None):

        # seed = torch.utils.data.get_worker_info().seed
        # print("Working seed {}".format( seed))
        # torch.manual_seed(seed)

        img = current_scene_image["scaled_image"]

        k = obs_traj.size(0)
        xy = torch.cat((obs_traj, pred_traj), dim=1)

        if self.format == "pixel":
            scale2orig = 1 / current_scene_image["scale_factor"]
        elif self.format == "meter":
            scale2orig = self.img_scaling
        else:
            assert False, " Not valid format '{}': 'meters' or 'pixel'".format(
                self.format)
        alpha = torch.rand(1) * 2 * np.pi

        center = torch.tensor(img.size) / 2.
        corners = torch.tensor([[0, 0], [0, img.height],
                                [img.width, img.height], [img.width, 0]])

        rand_num = torch.randint(0, 3, (1, ))

        if rand_num != 0:
            if rand_num == 1:

                img = TF.hflip(img)
                xy[:, :, 0] = img.width * scale2orig - xy[:, :, 0]

                if self.wall_available:
                    for i in range(len(walls)):
                        walls[i][:,
                                 0] = img.width * scale2orig - walls[i][:, 0]

            elif rand_num == 2:
                img = TF.vflip(img)
                xy[:, :, 1] = img.height * scale2orig - xy[:, :, 1]
                # transform wall
                if self.wall_available:
                    for i in range(len(walls)):
                        walls[i][:,
                                 1] = img.height * scale2orig - walls[i][:, 1]

        img = TF.rotate(img, alpha / np.pi * 180, expand=True)
        corners_trans = rotate(corners, center, alpha)
        offset = corners_trans.min(axis=0)[0]
        corners_trans -= offset

        if self.wall_available:
            wall_points = []
            for wall in walls.copy():
                wall_points.append(
                    (rotate(torch.from_numpy(wall), center * scale2orig, alpha)
                     - offset * scale2orig).numpy())
            walls = wall_points

        else:
            walls = False

        xy = xy.reshape(k * self.seq_len, -1)
        xy = rotate(xy, center * scale2orig, alpha) - offset * scale2orig

        xy = xy.reshape(k, self.seq_len, -1)

        scaling_factor_global = self.img_scaling / self.scaling_global
        scaling_factor_local = self.img_scaling / self.scaling_local

        global_image = img.resize(
            (int(round(img.width * scaling_factor_global)),
             int(round(img.height * scaling_factor_global))), Image.ANTIALIAS)
        local_image = img.resize(
            (int(round(img.width * scaling_factor_local)),
             int(round(img.height * scaling_factor_local))), Image.ANTIALIAS)

        scene_image = {
            "ratio": self.images[scene]["ratio"],
            "scene": scene,
            "scaled_image": copy.copy(img),
            "global_image": copy.copy(global_image),
            "local_image": copy.copy(local_image)
        }

        dxdy = xy[:, 1:] - xy[:, :-1]
        obs_traj = xy[:, :self.obs_len]
        pred_traj = xy[:, self.obs_len:]

        feature_list = []
        cropped_img_list = []
        prob_mask_list = []

        for idx in np.arange(0, k):
            features_id, prob_mask = self.gen_global_patches(
                scene_image, obs_traj[idx], pred_traj[idx])

            feature_list.append(features_id)
            prob_mask_list.append(prob_mask)

        global_patch = torch.cat(feature_list)
        prob_mask_tensor = torch.cat(prob_mask_list)
        feature_list = []
        for idx in np.arange(0, k):
            feature_pred = []
            for time in range(self.pred_len):

                feature_pred.append(
                    self.gen_local_patches(scene_image,
                                           xy[idx, self.obs_len - 1 + time],
                                           xy[idx, time + self.obs_len]))
            feature_pred = torch.cat(feature_pred)
            feature_list.append(feature_pred)

        local_patch = torch.stack(feature_list, dim=0)

        scene_image = (k) * [scene_image]

        return obs_traj, pred_traj, dxdy[:, :self.obs_len -
                                         1], dxdy[:, self.obs_len -
                                                  1:], scene_image, global_patch, prob_mask_tensor, walls, local_patch
Пример #26
0
 def __call__(self, sample):
     if random.random() < self.p:
         sample['input'] = [F.vflip(input) for input in sample['input']]
         sample['gt'] = [F.vflip(gt) for gt in sample['gt']]
     return sample
Пример #27
0
 def torchvision(self, img):
     return torchvision.vflip(img)
Пример #28
0
 def __call__(self, image):
     return F.vflip(image)
Пример #29
0
    def __getitem__(self, index):
        """Reads an image from a file and preprocesses it and returns."""

        # Random factor extraction
        def random_factor(factor):
            assert factor > 0
            return_factor = factor * np.random.randn() + 1
            if return_factor < 1 - factor:
                return_factor = 1 - factor
            if return_factor > 1 + factor:
                return_factor = 1 + factor
            return return_factor

        # Load image and mask files
        image_path, mask_path = self.data_paths[index]  # Random index
        image = Image.open(image_path)
        mask = Image.open(mask_path) if mask_path is not None else None

        # Data augmentation
        image = np.array(image).astype(np.float32) / 255.0

        if self.phase != "test":
            # Random brightness & contrast & gamma adjustment (linear and nonlinear intensity transform)
            brightness_factor = random_factor(0.12)
            contrast_factor = random_factor(0.20)
            gamma_factor = random_factor(0.45)
            image = np.array(image).astype(np.float32)

            image = (brightness_factor - 1.0) + image
            image = 0.5 + contrast_factor * (image - 0.5)
            image = np.clip(image, 0.0, 1.0)
            image = image**gamma_factor

            # Image standard deviation normalization
            image = image / np.std(image)
            image = F.to_pil_image(image, mode="F")

            # Random cropping
            i, j, h, w = T.RandomCrop.get_params(image,
                                                 output_size=self.patch_size)
            image = F.crop(image, i, j, h, w)
            mask = F.crop(mask, i, j, h, w)

            # Random horizontal flipping
            if random.random() < 0.5:
                image = F.hflip(image)
                mask = F.hflip(mask)

            # Random vertical flipping
            if random.random() < 0.5:
                image = F.vflip(image)
                mask = F.vflip(mask)

            # # Random shearing (takes too long time...)
            # if random.random() < 0.5:
            #     shear = 4 * random.random() - 2
            #     image = F.affine(image, angle=0, translate=(0, 0), scale=1, shear=shear, resample=Image.NEAREST)
            #     mask = F.affine(mask, angle=0, translate=(0, 0), scale=1, shear=shear, resample=Image.NEAREST)

            # # Random noise addition (white & speckle)
            # image0 = image
            # image0[image0 < 0] = 0
            # speckle_level = np.abs(0.1 * np.random.randn())
            # white_level = np.abs(0.05 * np.random.randn())
            # speckle = speckle_level * (np.random.exponential(scale=image0).astype(np.float32) - image)
            # white_noise = white_level * np.random.randn(image.shape[0], image.shape[1]).astype(np.float32)
            # image = image + white_noise + speckle
        else:
            image = image / np.std(image)

        # ToTensor
        transform = list()
        transform.append(
            T.ToTensor())  # ToTensor should be included before returning.
        transform = T.Compose(transform)

        image = transform(image)

        # fig, axes = plt.subplots(1, 2)
        # axes[0].imshow(image0, vmax=1.0, vmin=0.0)
        # axes[1].imshow(image[0, :, :].numpy(), vmax=1.0, vmin=0.0)
        # plt.title("%.2f %.2f %.2f" % (brightness_factor, contrast_factor, gamma_factor))
        # plt.show()

        # Mask labeling & Weight
        if mask is not None:
            mask = (np.array(mask) / 255).astype(np.float32)
            weight = np.zeros(mask.shape, dtype=np.float32)
            if self.sample_weight is None:
                self.sample_weight = (1.0, ) * self.num_classes

            # mask_label = np.ndarray(mask.shape + (self.num_classes,), dtype=np.float32)
            # for c in range(self.num_classes):
            #     mask_label[:, :, c] = (mask == c).astype(np.float32)
            #     weight += (mask == c).astype(np.float32) * self.sample_weight[c]

            transform = list()
            transform.append(
                T.ToTensor())  # ToTensor should be included before returning.
            transform = T.Compose(transform)

            # mask = transform(mask_label)
            mask = transform(mask)
            weight = transform(weight)

            return image, mask, weight
        else:
            return image
Пример #30
0
 def torchvision(self, img):
     return torchvision.vflip(img)
Пример #31
0
 def __call__(self, images, target):
     if random.random() < self.prob:
         images = [F.vflip(image) for image in images]
         target = target.transpose(1)
     return images, target