Example #1
0
    def __call__(self, input, target):
        # do something to both images
        input =  Resize(self.height, Image.BILINEAR)(input)
        target = Resize(self.height, Image.NEAREST)(target)

        if(self.augment):
            # Random hflip
            hflip = random.random()
            if (hflip < 0.5):
                input = input.transpose(Image.FLIP_LEFT_RIGHT)
                target = target.transpose(Image.FLIP_LEFT_RIGHT)
            
            #Random translation 0-2 pixels (fill rest with padding
            transX = random.randint(-2, 2) 
            transY = random.randint(-2, 2)

            input = ImageOps.expand(input, border=(transX,transY,0,0), fill=0)
            target = ImageOps.expand(target, border=(transX,transY,0,0), fill=255) #pad label filling with 255
            input = input.crop((0, 0, input.size[0]-transX, input.size[1]-transY))
            target = target.crop((0, 0, target.size[0]-transX, target.size[1]-transY))   

        input = ToTensor()(input)
        if (self.enc):
            target = Resize(int(self.height/8), Image.NEAREST)(target)
        target = ToLabel()(target)
        target = Relabel(255, 19)(target)

        return input, target
Example #2
0
    def __call__(self, input, target):
        ## do something to both images

        input = Resize((1086, 1351), Image.BILINEAR)(input)
        target = Resize((1086, 1351), Image.NEAREST)(target)
        #input = Resize((512,1024), Image.BILINEAR)(input)
        #target = Resize((512,1024),Image.NEAREST)(target)

        if (self.augment):

            rotation_degree = 1
            shear_degree = 1
            input = RandomAffine(rotation_degree,
                                 None,
                                 None,
                                 shear_degree,
                                 resample=Image.BILINEAR,
                                 fillcolor=0)(input)
            target = RandomAffine(rotation_degree,
                                  None,
                                  None,
                                  shear_degree,
                                  resample=Image.NEAREST,
                                  fillcolor=255)(target)

            w, h = input.size
            nratio = random.uniform(0.5, 1.0)
            ni = random.randint(0, int(h - nratio * h))
            nj = random.randint(0, int(w - nratio * w))
            input = input.crop(
                (nj, ni, int(nj + nratio * w), int(ni + nratio * h)))
            target = target.crop(
                (nj, ni, int(nj + nratio * w), int(ni + nratio * h)))
            input = Resize((512, 1024), Image.BILINEAR)(input)
            target = Resize((512, 1024), Image.NEAREST)(target)

            brightness = 0.1
            contrast = 0.1
            saturation = 0.1
            hue = 0.1
            input = ColorJitter(brightness, contrast, saturation, hue)(input)

            hflip = random.random()
            if (hflip < 0.5):
                input = input.transpose(Image.FLIP_LEFT_RIGHT)
                target = target.transpose(Image.FLIP_LEFT_RIGHT)

        else:
            input = Resize((512, 1024), Image.BILINEAR)(input)
            target = Resize((512, 1024), Image.NEAREST)(target)

        input = ToTensor()(input)

        if (self.enc):
            target = Resize((64, 128), Image.NEAREST)(target)

        target = ToLabel()(target)
        target = Relabel(255, 27)(target)

        return input, target
Example #3
0
    def transform(self, img, lbl):
        img = Resize(self.img_size, Image.BILINEAR)(img)
        lbl = Resize(self.img_size, Image.NEAREST)(lbl)
        if (self.augment):
            hflip = random.random()
            if (hflip < 0.5):
                img = img.transpose(Image.FLIP_LEFT_RIGHT)
                lbl = lbl.transpose(Image.FLIP_LEFT_RIGHT)
            transX = random.randint(-2, 2)
            transY = random.randint(-2, 2)
            img = ImageOps.expand(img, border=(transX, transY, 0, 0), fill=0)
            lbl = ImageOps.expand(lbl, border=(transX, transY, 0, 0), fill=0)
            img = img.crop((0, 0, img.size[0] - transX, img.size[1] - transY))
            lbl = lbl.crop((0, 0, lbl.size[0] - transX, lbl.size[1] - transY))

        if (self.label_scale):
            lbl = Resize([self.img_size[0] // 8, self.img_size[1] // 8],
                         Image.NEAREST)(lbl)
        img = ToTensor()(img)
        # img = Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img)

        lbl = np.array(lbl)
        lbl_copy = lbl.copy()
        for k, v in self.id_to_trainid.items():
            lbl[lbl_copy == k] = v
        lbl = torch.from_numpy(lbl).long()
        return img, lbl
Example #4
0
    def __call__(self, input, gray, target):

        # do something to both images
        if self.height != 8.1:
            input = Resize(self.height, Image.BILINEAR)(input)
            gray = Resize(self.height, Image.BILINEAR)(gray)
            target = Resize(self.height, Image.NEAREST)(target)

# === Data Augmentation === #
        if (self.dataAugment):
            # 1. Random horizenal flip
            hflip = random.random()
            if (hflip < 0.5):
                input = input.transpose(Image.FLIP_LEFT_RIGHT)
                gray = gray.transpose(Image.FLIP_LEFT_RIGHT)
                target = target.transpose(Image.FLIP_LEFT_RIGHT)

            # 2. Random translation 0-2 pixels (fill rest with padding
            transX = random.randint(-2, 2)
            transY = random.randint(-2, 2)
            input = ImageOps.expand(input,
                                    border=(transX, transY, 0, 0),
                                    fill=0)
            gray = ImageOps.expand(gray, border=(transX, transY, 0, 0), fill=0)
            target = ImageOps.expand(target,
                                     border=(transX, transY, 0, 0),
                                     fill=255)  #pad label filling with 255

            input = input.crop(
                (0, 0, input.size[0] - transX, input.size[1] - transY))
            gray = gray.crop(
                (0, 0, gray.size[0] - transX, gray.size[1] - transY))
            target = target.crop(
                (0, 0, target.size[0] - transX, target.size[1] - transY))

        input = ToTensor()(input)
        gray = ToTensor()(gray)
        if (self.encoderOnly):
            target = Resize(int(self.height / 8), Image.NEAREST)(target)
        target = ToLabel()(target)
        target = Relabel(255, 19)(target)  # ignored label 255 -> 19

        input = torch.cat((input, gray), 0)

        return input, target
Example #5
0
    def __call__(self, input, input1, target):
        # Resizing data to required size
        input = Resize((self.height, 320), Image.BILINEAR)(input)  #askalex
        input1 = Resize((self.height, 320),
                        Image.BILINEAR)(input1)  #ChangedByUs
        target = Resize((self.height, 320), Image.NEAREST)(target)

        if (self.augment):
            # Random horizontal flip
            hflip = random.random()
            if (hflip < 0.5):
                input = input.transpose(Image.FLIP_LEFT_RIGHT)
                input1 = input1.transpose(Image.FLIP_LEFT_RIGHT)  #ChangedByUs
                target = target.transpose(Image.FLIP_LEFT_RIGHT)

            #Random translation 0-2 pixels (fill rest with padding)
            transX = random.randint(0, 2)
            transY = random.randint(0, 2)

            input = ImageOps.expand(input,
                                    border=(transX, transY, 0, 0),
                                    fill=0)
            input1 = ImageOps.expand(input1,
                                     border=(transX, transY, 0, 0),
                                     fill=0)  #ChangedByUs
            target = ImageOps.expand(
                target, border=(transX, transY, 0, 0),
                fill=255)  #pad label filling with 7  #askalex- change 255
            input = input.crop(
                (0, 0, input.size[0] - transX, input.size[1] - transY))
            input1 = input1.crop((0, 0, input1.size[0] - transX,
                                  input1.size[1] - transY))  #ChangedByUs
            target = target.crop(
                (0, 0, target.size[0] - transX, target.size[1] - transY))

        input = ToTensor()(input)
        input1 = ToTensor()(input1)  #ChangedByUs

        target = ToLabel()(target)

        #target = Relabel(255, 7)(target)
        return input, input1, target  #ChangedByUs
    def __getitem__(self, index):
        example = self.examples[index]

        img_path = example["img_path"]
        label_img_path = example["label_img_path"]
        with open(img_path, "rb") as f:
            img = Image.open(f).convert("RGB")
        with open(label_img_path, "rb") as f:
            label_img = Image.open(f).convert("P")

        img = Resize(self.new_img_h, Image.BILINEAR)(img)
        label_img = Resize(self.new_img_h, Image.NEAREST)(label_img)

        if self.mode == "train":
            hflip = random.random()
            if (hflip < 0.5):
                img = img.transpose(Image.FLIP_LEFT_RIGHT)
                label_img = label_img.transpose(Image.FLIP_LEFT_RIGHT)

            transX = random.randint(-2, 2)
            transY = random.randint(-2, 2)

            img = ImageOps.expand(img, border=(transX, transY, 0, 0), fill=0)
            label_img = ImageOps.expand(label_img,
                                        border=(transX, transY, 0, 0),
                                        fill=255)  #pad label filling with 255
            img = img.crop((0, 0, img.size[0] - transX, img.size[1] - transY))
            label_img = label_img.crop(
                (0, 0, label_img.size[0] - transX, label_img.size[1] - transY))

        img = self.tran(img)
        if self.only_encode:
            label_img = Resize(int(self.new_img_h / 8),
                               Image.NEAREST)(label_img)

        label_img = torch.from_numpy(np.array(label_img)).long()
        label_img[label_img == 255] = num_classes - 1
        return (img, label_img)