def __call__(self, input_orig, target_orig): # do something to both images input = Resize(self.height, Image.BILINEAR)(input_orig) target = Resize(self.height, Image.NEAREST)(target_orig) 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) input_orig = ToTensor()(input_orig) if (self.enc): target = Resize(int(self.height/8), Image.NEAREST)(target) target = ToLabel()(target) target_orig = ToLabel()(target_orig) target = Relabel(255, 1)(target) target_orig = Relabel(255, 1)(target_orig) return input, target, input_orig, target_orig
def __init__(self, root): size = (128,128) self.root = root if not os.path.exists(self.root): raise Exception("[!] {} not exists.".format(root)) self.img_transform = Compose([ Scale(size, Image.BILINEAR), ToTensor(), Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]), ]) self.hsv_transform = Compose([ Scale(size, Image.BILINEAR), ToTensor(), ]) self.label_transform = Compose([ Scale(size, Image.NEAREST), ToLabel(), ReLabel(255, 1), ]) #sort file names self.input_paths = sorted(glob(os.path.join(self.root, '{}/*.jpg'.format("ISIC-2017_Test_v2_Data")))) self.label_paths = sorted(glob(os.path.join(self.root, '{}/*.png'.format("ISIC-2017_Test_v2_Part1_GroundTruth")))) self.name = os.path.basename(root) if len(self.input_paths) == 0 or len(self.label_paths) == 0: raise Exception("No images/labels are found in {}".format(self.root))
def __init__(self, root): self.size = (180,135) self.root = root if not os.path.exists(self.root): raise Exception("[!] {} not exists.".format(root)) self.img_resize = Compose([ Scale(self.size, Image.BILINEAR), # We can do some colorjitter augmentation here # ColorJitter(brightness=0, contrast=0, saturation=0, hue=0), ]) self.label_resize = Compose([ Scale(self.size, Image.NEAREST), ]) self.img_transform = Compose([ ToTensor(), Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]), ]) self.hsv_transform = Compose([ ToTensor(), ]) self.label_transform = Compose([ ToLabel(), ReLabel(255, 1), ]) #sort file names self.d_path = "/scratch/prathyuakundi/MIA_data/ISIC-2017_Training_Data/" self.g_path = "/scratch/prathyuakundi/MIA_data/ISIC-2017_Training_Part1_GroundTruth/ISIC-2017_Training_Part1_GroundTruth/" self.input_paths = sorted(next(os.walk(self.d_path))[2]) self.label_paths = sorted(next(os.walk(self.g_path))[2]) self.name = os.path.basename(root) if len(self.input_paths) == 0 or len(self.label_paths) == 0: raise Exception("No images/labels are found in {}".format(self.root))
def __call__(self, input, target): # do something to both images input = cv2.resize(input, self.size, interpolation=cv2.INTER_LINEAR) target = cv2.resize(target,self.size,interpolation=cv2.INTER_NEAREST) if self.rescale: input = (input/255.).astype(np.float32) # target = target/255. target = target.astype(np.float32) if(self.augment): # Random hflip hflip = random.random() if (hflip < 0.5): input = cv2.flip(input,1) target = cv2.flip(target, 1) #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) target = ToLabel()(target) target = Relabel(255, 3)(target) return input, target
def __init__(self, root): self.size = (180,135) self.root = root if not os.path.exists(self.root): raise Exception("[!] {} not exists.".format(root)) self.img_resize = Compose([ #like torch.nn.Sequential Scale(self.size, Image.BILINEAR), #deprecated in favor of Resize # We can do some colorjitter augmentation here # ColorJitter(brightness=0, contrast=0, saturation=0, hue=0), ]) self.label_resize = Compose([ Scale(self.size, Image.NEAREST), ]) self.img_transform = Compose([ ToTensor(), Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225]), ]) self.hsv_transform = Compose([ ToTensor(), ]) self.label_transform = Compose([ ToLabel(), ReLabel(255, 1), ]) #sort file names self.input_paths = sorted(glob(os.path.join(self.root, '{}/*.jpg'.format("ISIC-2017_Training_Data")))) self.label_paths = sorted(glob(os.path.join(self.root, '{}/*.png'.format("ISIC-2017_Training_Part1_GroundTruth")))) self.name = os.path.basename(root) if len(self.input_paths) == 0 or len(self.label_paths) == 0: raise Exception("No images/labels are found in {}".format(self.root))
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)) #TODO future: additional augments #CenterCrop(256) #Normalize([.485, .456, .406], [.229, .224, .225]), 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
def __call__(self, input, tinput, target): input = Resize(self.height, Image.BILINEAR)(input) tinput = Resize(self.height, Image.BILINEAR)(tinput) target = Resize(self.height, Image.NEAREST)(target) input = ToTensor()(input) tinput = ToTensor()(tinput) target = ToLabel()(target) target = Relabel(255, 19)(target) return input, tinput, target
def prepared_train_data(): input_transform = Compose([ ToTensor(), ]) target_transform = Compose([ ToLabel(), ]) glaucoma = OrigaDataset(input_transform=input_transform, target_transform=target_transform) loader = DataLoader(glaucoma, num_workers=4, batch_size=12) return loader
def prepared_test_data(): input_transform = Compose([ ToTensor(), ]) target_transform = Compose([ ToLabel(), ]) glaucoma = OrigaDataset(data_type=Constants.TYPE_TEST, input_transform=input_transform, target_transform=target_transform) loader = DataLoader(glaucoma, num_workers=4, batch_size=1) return loader
def __getitem__(self, index): filename = self.filenames[index] filenameGt = self.filenamesGt[index] with open(image_path_city(self.images_root, filename), 'rb') as f: image = load_image(f).convert('RGB') label_array = None if self.file_format == "npy": label_array = np.load(image_path_city(self.labels_root, filenameGt)) elif self.file_format == "csv": label_array = genfromtxt(image_path_city(self.labels_root, filenameGt), delimiter=',', dtype="float32") else: print("Unsupported file format " + self.file_format) ### NASTY THING, BREAKS CLASSIFICATION, REMOVE!!!! # label_array[label_array == 0] = -1 ############################ label = Image.fromarray(label_array, 'F') # Image transformation which is also expected to return a tensor. if self.co_transform is not None: image1, image2, label = self.co_transform(image, label) else: image1 = image image2 = image # Convert to tensor image1 = ToTensor()(image1) image2 = ToTensor()(image2) if self.tensor_type == 'long': label = ToLabel()(label) elif self.tensor_type == 'float': label = ToFloatLabel()(label) # Sanitize labels. if self.file_format == "csv": label[label != label] = -1 n_nan = np.count_nonzero(np.isnan(label.numpy())) if n_nan > 0: print("File " + filenameGt + " produces nan " + str(n_nan)) return image1, image2, label
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
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 __init__(self, fpath, augmentation=None, with_targets=True): if not os.path.isfile(fpath): raise FileNotFoundError( "Could not find dataset file: '{}'".format(fpath)) if not augmentation: augmentation = [] n_augmentation = math.factorial( len(augmentation)) if len(augmentation) > 0 else 0 augmentation_combinations = list( itertools.product([0, 1], repeat=n_augmentation)) self.with_targets = with_targets self.size = (180, 135) self.input_resize = Scale(self.size, Image.BILINEAR) self.target_resize = Scale(self.size, Image.NEAREST) self.input_transform = Compose([ ToTensor(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) self.target_transform = Compose([ ToLabel(), ReLabel(255, 1), ]) self.augmentation = augmentation with open(fpath, "r") as f: lines = filter(lambda l: bool(l), f.read().split("\n")) if self.with_targets: data = [(input.strip(), target.strip()) for input, target in funcy.walk( lambda l: l.split(" "), lines)] else: data = [(input.strip(), None) for input in lines] self.data = [(d, transform_list) for transform_list in augmentation_combinations for d in data]
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) target = ToLabel()(target) #target = Relabel(255, 19)(target) #TODO target = Relabel(0, 0)(target) target = Relabel(25, 1)(target) target = Relabel(50, 2)(target) target = Relabel(75, 3)(target) target = Relabel(100, 4)(target) target = Relabel(125, 5)(target) target = Relabel(150, 6)(target) target = Relabel(175, 7)(target) target = Relabel(200, 8)(target) target = Relabel(225, 9)(target) target = Relabel(250, 10)(target) target = Relabel(255, 10)(target) return input, target
from transform import Relabel, ToLabel, Colorize import visdom NUM_CHANNELS = 3 NUM_CLASSES = 20 image_transform = ToPILImage() input_transform_cityscapes = Compose([ Resize((512, 1024), Image.BILINEAR), ToTensor(), #Normalize([.485, .456, .406], [.229, .224, .225]), ]) target_transform_cityscapes = Compose([ Resize((512, 1024), Image.NEAREST), ToLabel(), Relabel(255, 19), #ignore label to 19 ]) cityscapes_trainIds2labelIds = Compose([ Relabel(19, 255), Relabel(18, 33), Relabel(17, 32), Relabel(16, 31), Relabel(15, 28), Relabel(14, 27), Relabel(13, 26), Relabel(12, 25), Relabel(11, 24), Relabel(10, 23), Relabel(9, 22),
def __init__(self, args, batch_size=64, source='svhn', target='mnist', learning_rate=0.0002, interval=100, optimizer='adam', num_k=4, all_use=False, checkpoint_dir=None, save_epoch=10): self.batch_size = batch_size self.source = source self.target = target self.num_k = num_k self.checkpoint_dir = checkpoint_dir self.save_epoch = save_epoch self.use_abs_diff = args.use_abs_diff self.all_use = all_use if self.source == 'svhn': self.scale = True else: self.scale = False print('dataset loading') if self.source == 'citycam' or self.target == 'citycam': import sys, os sys.path.append( os.path.join(os.path.dirname(__file__), '..', 'segmentation')) from transform import ReLabel, ToLabel, Scale, RandomSizedCrop, RandomHorizontalFlip, RandomRotation from PIL import Image from torchvision.transforms import Compose, Normalize, ToTensor from datasets import ConcatDataset, get_dataset, check_src_tgt_ok from models.model_util import get_models, get_optimizer train_img_shape = ( 64, 64) # tuple([int(x) for x in args.train_img_shape]) img_transform_list = [ Scale(train_img_shape, Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]) ] # if args.augment: # aug_list = [ # RandomRotation(), # RandomHorizontalFlip(), # RandomSizedCrop() # ] # img_transform_list = aug_list + img_transform_list img_transform = Compose(img_transform_list) label_transform = Compose([ Scale(train_img_shape, Image.NEAREST), ToLabel(), ReLabel( 255, 12 ) # args.n_class - 1), # Last Class is "Void" or "Background" class ]) src_dataset_test = get_dataset(dataset_name='citycam', split='synthetic-Sept19', img_transform=img_transform, label_transform=label_transform, test=True, input_ch=3, keys_dict={ 'image': 'image', 'yaw': 'label', 'yaw_raw': 'label_raw' }) tgt_dataset_test = get_dataset( dataset_name='citycam', split= 'real-Sept23-train, objectid IN (SELECT objectid FROM properties WHERE key="yaw")', img_transform=img_transform, label_transform=label_transform, test=True, input_ch=3, keys_dict={ 'image': 'image', 'yaw': 'label', 'yaw_raw': 'label_raw' }) self.dataset_test = torch.utils.data.DataLoader( #src_dataset_test, tgt_dataset_test, batch_size=args.batch_size, shuffle=False, pin_memory=True) dataset_train = get_dataset(dataset_name='citycam', split='synthetic-Sept19', img_transform=img_transform, label_transform=label_transform, test=False, input_ch=3, keys_dict={ 'image': 'S_image', 'yaw': 'S_label', 'yaw_raw': 'S_label_raw' }) self.dataset_train = torch.utils.data.DataLoader( dataset_train, batch_size=args.batch_size, shuffle=True, pin_memory=True) else: from datasets_dir.dataset_read import dataset_read self.datasets_test, self.dataset_train = dataset_read( target, source, self.batch_size, scale=self.scale, all_use=self.all_use) self.G = Generator(source=source, target=target) print('load finished!') self.C1 = Classifier(source=source, target=target) self.C2 = Classifier(source=source, target=target) if args.eval_only: self.G.torch.load('%s/%s_to_%s_model_epoch%s_G.pt' % (self.checkpoint_dir, self.source, self.target, args.resume_epoch)) self.G.torch.load('%s/%s_to_%s_model_epoch%s_G.pt' % (self.checkpoint_dir, self.source, self.target, self.checkpoint_dir, args.resume_epoch)) self.G.torch.load('%s/%s_to_%s_model_epoch%s_G.pt' % (self.checkpoint_dir, self.source, self.target, args.resume_epoch)) self.G.cuda() self.C1.cuda() self.C2.cuda() self.interval = interval self.set_optimizer(which_opt=optimizer, lr=learning_rate) self.lr = learning_rate