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, })
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 })
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 })
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 })
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 })
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 })
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 })
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")
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 np.random.seed(seed)