示例#1
0
    def __getitem__(self, index):
        #print(self.d_path+self.input_paths[index])
        image = Image.open(self.d_path+self.input_paths[index]).convert('RGB')
        # image_hsv = Image.open(self.input_paths[index]).convert('HSV')
        label = Image.open(self.g_path+self.label_paths[index]).convert('P')

        image = self.img_resize(image)
        # image_hsv = self.img_resize(image_hsv)
        label = self.label_resize(label)
        # brightness_factor = 1 + random.uniform(-0.4,0.4)
        # contrast_factor = 1 + random.uniform(-0.4,0.4)
        # saturation_factor = 1 + random.uniform(-0.4,0.4)
        # hue_factor = random.uniform(-0.1,0.1)
        # gamma = 1 + random.uniform(-0.1,0.1)

        #randomly flip images
        if random.random() > 0.5:
            image = HorizontalFlip()(image)
            # image_hsv = HorizontalFlip()(image_hsv)
            label = HorizontalFlip()(label)
        if random.random() > 0.5:
            image = VerticalFlip()(image)
            # image_hsv = VerticalFlip()(image_hsv)
            label = VerticalFlip()(label)

        #randomly crop image to size 128*128
        w, h = image.size
        th, tw = (128,128)
        x1 = random.randint(0, w - tw)
        y1 = random.randint(0, h - th)
        if w == tw and h == th:
            image = image
            # image_hsv = image_hsv
            label = label
        else:
            if random.random() > 0.5:
                image = image.resize((128,128),Image.BILINEAR)
                # image_hsv = image_hsv.resize((128,128),Image.BILINEAR)
                label = label.resize((128,128),Image.NEAREST)
            else:
                image = image.crop((x1, y1, x1 + tw, y1 + th))
                # image_hsv = image_hsv.crop((x1, y1, x1 + tw, y1 + th))
                label = label.crop((x1, y1, x1 + tw, y1 + th))
        # angle = random.randint(-20, 20)
        # image = image.rotate(angle, resample=Image.BILINEAR)
        # image_hsv = image_hsv.rotate(angle, resample=Image.BILINEAR)
        # label = label.rotate(angle, resample=Image.NEAREST)
        image = self.img_transform(image)
        # image_hsv = self.hsv_transform(image_hsv)
        # image = torch.cat([image,image_hsv],0)


        label = self.label_transform(label)

        return image, label
 def __init__(self,
              root,
              split="train",
              img_transform=None,
              label_transform=None,
              test=True,
              label_type=None):
     self.root = root
     self.split = split
     # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
     self.files = collections.defaultdict(list)
     self.img_transform = img_transform
     self.label_transform = label_transform
     self.h_flip = HorizontalFlip()
     self.v_flip = VerticalFlip()
     self.test = test
     data_dir = root
     # for split in ["train", "trainval", "val"]:
     imgsets_dir = osp.join(data_dir, "leftImg8bit/%s.txt" % split)
     with open(imgsets_dir) as imgset_file:
         for name in imgset_file:
             name = name.strip()
             img_file = osp.join(data_dir, "leftImg8bit/%s" % name)
             if label_type == "label16":
                 name = name.replace('leftImg8bit', 'gtFine_label16IDs')
             else:
                 name = name.replace('leftImg8bit', 'gtFine_labelTrainIds')
             label_file = osp.join(data_dir, "gtFine/%s" % name)
             self.files[split].append({
                 "img": img_file,
                 "label": label_file
             })
 def __init__(self,
              root,
              split="train",
              img_transform=None,
              label_transform=None,
              test=True,
              input_ch=3):
     assert input_ch == 3
     self.root = root
     self.split = split
     # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
     self.files = collections.defaultdict(list)
     self.img_transform = img_transform
     self.label_transform = label_transform
     self.h_flip = HorizontalFlip()
     self.v_flip = VerticalFlip()
     self.test = test
     data_dir = root
     # for split in ["train", "trainval", "val"]:
     imgsets_dir = os.listdir(data_dir)
     for name in imgsets_dir:
         img_file = osp.join(data_dir, "%s" % name)
         self.files[split].append({
             "img": img_file,
         })
示例#4
0
    def __init__(self, root, split="images", img_transform=None, label_transform=None,
                 test=False, input_ch=3):
        # Note; split "train" and "images" are SAME!!!

        assert split in ["images", "test", "train"]

        assert input_ch in [1, 3, 4]
        self.input_ch = input_ch
        self.root = root
        self.split = split
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.files = collections.defaultdict(list)
        self.img_transform = img_transform
        self.label_transform = label_transform
        self.h_flip = HorizontalFlip()
        self.v_flip = VerticalFlip()
        self.test = test
        data_dir = root

        imgsets_dir = osp.join(data_dir, "%s.txt" % split)

        with open(imgsets_dir) as imgset_file:
            for name in imgset_file:
                name = name.strip()
                img_file = osp.join(data_dir, "%s" % name)
                # name = name.replace('leftImg8bit','gtFine_labelTrainIds')
                label_file = osp.join(data_dir, "%s" % name.replace('images', 'labels_gt'))
                self.files[split].append({
                    "img": img_file,
                    "label": label_file
                })
示例#5
0
    def __init__(self, root, split="train", img_transform=None, label_transform=None, test=True,
                 label_type=None):
        self.root = root
        self.split = split
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.files = collections.defaultdict(list)
        self.img_transform = img_transform
        self.label_transform = label_transform
        self.h_flip = HorizontalFlip()
        self.v_flip = VerticalFlip()
        self.test = test
        # for split in ["train", "trainval", "val"]:
        if split == "train":
            img_path = 'data/filelist/cityscapes_imagelist_train.txt'
            label_path = "data/filelist/cityscapes_labellist_train_label16.txt"
        elif split == "val":
            img_path = 'data/filelist/cityscapes_imagelist_val.txt'
            label_path = "data/filelist/cityscapes_labellist_val_label16.txt"
        else:
            raise ValueError


        with open(img_path,"r") as f:
            rgb_fn_list = [os.path.join(self.root,"leftImg8bit", split, tmp.strip()) for tmp in f.readlines()]

        with open(label_path, "r") as f:
            gt_fn_list =  [os.path.join(self.root, "label16_for_synthia", tmp.strip()) for tmp in f.readlines()]

        for rgb_fn, gt_fn in zip(rgb_fn_list, gt_fn_list):
            self.files[split].append({
                "img": rgb_fn,
                "label": gt_fn
            })
示例#6
0
    def __init__(self,
                 root,
                 split="test",
                 img_transform=None,
                 label_transform=None):
        self.root = root
        self.split = split
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.files = collections.defaultdict(list)
        self.img_transform = img_transform
        self.label_transform = label_transform
        self.h_flip = HorizontalFlip()
        self.v_flip = VerticalFlip()

        data_dir = osp.join(root, "MSCOCO")
        # for split in ["train", "trainval", "val"]:
        imgsets_dir = osp.join(data_dir, "%s.txt" % split)
        with open(imgsets_dir) as imgset_file:
            for name in imgset_file:
                name = name.strip()
                img_file = osp.join(data_dir, "train2014_org/%s.jpg" % name)
                label_file = osp.join(data_dir, "train2014_gt/%s.png" % name)
                self.files[split].append({
                    "img": img_file,
                    "label": label_file
                })
示例#7
0
    def __init__(self,
                 root,
                 split="train",
                 img_transform=None,
                 label_transform=None,
                 mask_transform=None):
        self.root = root
        self.split = split
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.files = collections.defaultdict(list)
        self.img_transform = img_transform
        self.label_transform = label_transform
        self.mask_transform = mask_transform
        self.h_flip = HorizontalFlip()
        self.v_flip = VerticalFlip()

        data_dir = osp.join(root, "eyedata", split)
        # for split in ["train", "trainval", "val"]:
        imgsets_dir = osp.join(data_dir, "img")
        for name in os.listdir(imgsets_dir):
            name = os.path.splitext(name)[0]
            img_file = osp.join(data_dir, "img/%s.tif" % name)
            label_file = osp.join(data_dir, "label/%s.gif" % name)
            mask_file = osp.join(data_dir, "mask/%s.gif" % name)
            self.files[split].append({
                "img": img_file,
                "label": label_file,
                "mask": mask_file
            })
 def __init__(self, root, name, split="train", img_transform=None, label_transform=None, test=True, input_ch=3,
              label_type=None):
     self.root = root
     self.split = split
     self.name = name
     # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
     self.files = collections.defaultdict(list)
     self.img_transform = img_transform
     self.label_transform = label_transform
     self.h_flip = HorizontalFlip()
     self.v_flip = VerticalFlip()
     self.test = test
     data_dir = '/media/VSlab/ethanchen20xx/datasets/NTHU_512/'
     # for split in ["train", "trainval", "val"]:        
     imgsets_dir = osp.join(root, "imgs", split, "%s.txt"%name)
     with open(imgsets_dir) as imgset_file:
         for name in imgset_file:
             name = name.strip()
             img_file = osp.join(data_dir,"imgs", name)
             # no label in this dataset
             label_file = img_file #osp.join(data_dir, 'labels', name)
             self.files[split].append({
                 "img": img_file,
                 "label": label_file
             })
示例#9
0
    def __init__(self,
                 root,
                 split="train",
                 img_transform=None,
                 label_transform=None):
        self.root = root
        self.split = split
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.files = collections.defaultdict(list)
        self.img_transform = img_transform
        self.label_transform = label_transform
        self.h_flip = HorizontalFlip()
        self.v_flip = VerticalFlip()

        data_dir = osp.join(root, "cityspace")
        # for split in ["train", "trainval", "val"]:
        imgsets_dir = osp.join(data_dir, "%s.txt" % split)
        with open(imgsets_dir) as imgset_file:
            for name in imgset_file:
                name = name.strip()
                img_file = osp.join(
                    data_dir, "leftImg8bit/train/%s_leftImg8bit.png" % name)
                # print img_file

                label_file = osp.join(
                    data_dir,
                    "gtFine/train/%s_gtFine_labelTrainIds.png" % name)
                # print label_file
                # exit()

                self.files[split].append({
                    "img": img_file,
                    "label": label_file
                })
示例#10
0
    def __init__(self, root, img_transform=None, label_transform=None):
        self.root = root
        self.mode = 'train'
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.files = collections.defaultdict(list)
        self.img_transform = img_transform
        self.label_transform = label_transform
        self.h_flip = HorizontalFlip()
        self.v_flip = VerticalFlip()

        for mode in ['train', 'val']:
            image_basenames = [osp.basename(f) for f in glob.glob(osp.join(self.root, mode, 'rgb/*.png'))]
            for name in image_basenames:
                img_file = osp.join(self.root, mode, 'rgb', name)
                label_file = osp.join(self.root, mode, 'gt', name)
                self.files[mode].append({
                    "img": img_file,
                    "label": label_file
                })
示例#11
0
    def __init__(self, root, split="trainval", img_transform=None, label_transform=None):
        self.root = root
        self.split = split
        self.tmp = []
        # self.mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
        self.files = collections.defaultdict(list)
        self.img_transform = img_transform
        self.label_transform = label_transform
        self.h_flip = HorizontalFlip()
        self.v_flip = VerticalFlip()

        data_dir = osp.join(root, "VOC2012")

        # for split in ["train", "trainval", "val"]:

        imgsets_dir = osp.join(data_dir, "ImageSets/Segmentation/%s.txt" % split)
        split_dir = sorted(glob.glob('/home/asilla/hanh/VOC2012/split_data' + '/*'))
        for image in split_dir:
            basename = os.path.splitext(os.path.basename(image))[0]
            self.tmp.append(basename)
        # print(self.tmp)
        # exit()
        with open(imgsets_dir) as imgset_file:
            for name in imgset_file:
                name = name.strip()
                if name in self.tmp:
                    continue
                img_file = osp.join(data_dir, "JPEGImages/%s.jpg" % name)
                print(img_file)
                label_file = osp.join(data_dir, "SegmentationClass/%s.png" % name)
                print(label_file)
                if not os.path.exists(img_file):
                    continue
                if not os.path.exists(label_file):
                    continue
                self.files[split].append({
                    "img": img_file,
                    "label": label_file
                })
        print(self.files[split])
        print("data load success")
示例#12
0
    input_transform = Compose([
        Scale(scale, Image.BILINEAR),
        ToYUV(),
        ToTensor(),
        Normalize([.5, 0, 0], [.5, .5, .5]),
    ])
    target_transform = Compose([
        Scale(scale, Image.NEAREST),
        ToTensor(),
        ToLabel(),
    ])

    input_transform_tr = Compose([
        Scale(scale, Image.BILINEAR),
        HorizontalFlip(),
        VerticalFlip(),
        ColorJitter(brightness=0.5, contrast=0.5, saturation=0.4, hue=0.3),
        ToYUV(),
        ToTensor(),
        Normalize([.5, 0, 0], [.5, .5, .5]),
    ])
    target_transform_tr = Compose([
        Scale(scale, Image.NEAREST),
        HorizontalFlip(),
        VerticalFlip(),
        ToTensor(),
        ToLabel(),
    ])

    seed = 12345678
示例#13
0
def main():
    # Code to open an image, and apply a transform
    # open the image
    # image = Image.open("Ally.jpg")
    # w = image.width
    # h = image.height
    # print((w, h))
    # Create a transformation to apply to the image
    # shift = Shift(-w/2, -h/2)
    # rotate = Rotation(math.pi/2)
    # scale = Scale(2)
    # shift2 = Shift(h/2, w/2)
    # combined = PositionTransform()
    # combined = combined.combine(shift)
    # combined = combined.combine(rotate)
    # combined = combined.combine(scale)
    # combined = combined.combine(shift2)
    # inverse the transformation (to apply it)
    # t = combined.inverse()

    # Image.transform(size, method, data=None, resample=0, fill=1, fillcolor=None)
    # img2 = image.transform((h, w), Image.AFFINE, _get_image_transform(t))
    # img2.save("Test.jpg")

    # Code to create a mosaic 4x4 tile world map at level 2
    # tm = TileMosaic(OSMTileRequester(), 2, 0, 3, 0, 3)
    # tm.save("world2.png")

    # Sample coordinates to avoid caring about the transformation just now
    bng_coords = [(300000, 600000), (300000, 601000), (301000, 601000),
                  (301000, 600000)]
    gwm_coords = [(-398075.709110655, 7417169.44503078),
                  (-398115.346383602, 7418925.37709793),
                  (-396363.034574031, 7418964.91393662),
                  (-396323.792660911, 7417208.95976453)]

    bng_x = [bng[0] for bng in bng_coords]
    bng_y = [bng[1] for bng in bng_coords]
    bng_box = (min(bng_x), min(bng_y), max(bng_x), max(bng_y))

    gwm_x = [gwm[0] for gwm in gwm_coords]
    gwm_y = [gwm[1] for gwm in gwm_coords]
    gwm_box = (min(gwm_x), min(gwm_y), max(gwm_x), max(gwm_y))

    # If the coords above relate to a 400x400 map, calculate the resolution
    bng_map_size = 600
    bng_res = (bng_box[2] - bng_box[0]) / bng_map_size
    print(bng_res)

    # Use the GlobalMercator class to calculate the optimal zoom level to use
    gwm = GlobalMercator()
    gwm_zoom = gwm.ZoomForPixelSize(bng_res)
    print(gwm_zoom)

    # Calculate the min/max tile x and y for the given area at the calculates zoom level
    tiles_x = []
    tiles_y = []
    for coord in gwm_coords:
        tx, ty = gwm.MetersToTile(coord[0], coord[1], gwm_zoom)
        tiles_x.append(tx)
        tiles_y.append(ty)
        print(f"{gwm_zoom} {tx} {ty}")
        # print(OSMTileRequester().request_tile(gwm_zoom, tx, ty))

    # Create a mosaic image from these tiles
    start_x = min(tiles_x)
    end_x = max(tiles_x)
    start_y = min(tiles_y)
    end_y = max(tiles_y)
    gwm_mosaic = TileMosaic(OSMTileRequester(), gwm_zoom, start_x, end_x,
                            start_y, end_y)
    gwm_mosaic.save("mosaic.png")

    # Get the bbox for these tiles
    gwm_mosaic_box_tl = gwm.TileBounds(start_x, start_y, gwm_zoom)
    gwm_mosaic_box_tr = gwm.TileBounds(end_x, start_y, gwm_zoom)
    gwm_mosaic_box_bl = gwm.TileBounds(start_x, end_y, gwm_zoom)
    gwm_mosaic_box_br = gwm.TileBounds(end_x, end_y, gwm_zoom)
    gwm_mosaic_box = (min(gwm_mosaic_box_tl[0], gwm_mosaic_box_bl[0]),
                      min(gwm_mosaic_box_bl[3], gwm_mosaic_box_br[3]),
                      max(gwm_mosaic_box_tr[2], gwm_mosaic_box_br[2]),
                      max(gwm_mosaic_box_tl[1], gwm_mosaic_box_tr[1]))

    print(gwm_mosaic_box)

    test = [(0, 0), (400, 0), (400, 400), (0, 400)]

    # Create a transformation to convert pixels of the target BNG image to the GWM mosaic image
    bng_img_to_gwm_image = PositionTransform()
    # Translate/scale image px to BNG
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(HorizontalFlip())
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(Scale(bng_res))
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(
        Shift(bng_box[0], bng_box[3]))
    test_coords(test, bng_img_to_gwm_image)

    # Transform BNG to GWM coords
    bng_gwm_transform = SamplePointTransform(bng_coords[0], bng_coords[1],
                                             bng_coords[2], gwm_coords[0],
                                             gwm_coords[1], gwm_coords[2])
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(bng_gwm_transform)
    test_coords(test, bng_img_to_gwm_image)

    # Translate/scale GWM coords to GWM mosaic image coords
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(
        Shift(-gwm_mosaic_box[0], -gwm_mosaic_box[3]))
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(
        Scale(1 / gwm.Resolution(gwm_zoom)))
    bng_img_to_gwm_image = bng_img_to_gwm_image.combine(HorizontalFlip())

    test_coords(test, bng_img_to_gwm_image)

    bng_result = gwm_mosaic.image.transform(
        (bng_map_size, bng_map_size), Image.AFFINE,
        _get_image_transform(bng_img_to_gwm_image))

    bng_result.save("BNG.jpg")