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 __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 __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 get_validation_miou(model_g, model_f1, model_f2, quick_test=1e10): if is_debug == 1: quick_test = 2 logger.info("proceed test on cityscapes val set...") model_g.eval() model_f1.eval() model_f2.eval() val_img_transform = Compose([ Scale(train_img_shape, Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]), ]) val_label_transform = Compose([Scale(cityscapes_image_shape, Image.NEAREST), # ToTensor() ]) #notice here, training, validation size difference, this is very tricky. target_loader = data.DataLoader(get_dataset(dataset_name="city16", split="val", img_transform=val_img_transform,label_transform=val_label_transform, test=True, input_ch=3), batch_size=1, pin_memory=True) from tensorpack.utils.stats import MIoUStatistics stat = MIoUStatistics(args.n_class) interp = torch.nn.Upsample(size=(cityscapes_image_shape[1], cityscapes_image_shape[0]), mode='bilinear') for index, (origin_imgs, labels, paths) in tqdm(enumerate(target_loader)): if index > quick_test: break path = paths[0] imgs = Variable(origin_imgs.cuda(), volatile=True) feature = model_g(imgs) outputs = model_f1(feature) if args.use_f2: outputs += model_f2(feature) pred = interp(outputs)[0, :].data.max(0)[1].cpu() feed_predict = np.squeeze(np.uint8(pred.numpy())) feed_label = np.squeeze(np.asarray(labels.numpy())) stat.feed(feed_predict, feed_label) logger.info("tensorpack IoU16: {}".format(stat.mIoU_beautify)) logger.info("tensorpack mIoU16: {}".format(stat.mIoU)) model_g.train() model_f1.train() model_f2.train() return stat.mIoU
def proceed_test(model_g, model_f1, model_f2, quick_test=1e10): logger.info("proceed test on cityscapes val set...") model_g.eval() model_f1.eval() model_f2.eval() test_img_shape = (2048, 1024) val_img_transform = Compose([ Scale(test_img_shape, Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]), ]) val_label_transform = Compose([ Scale(test_img_shape, Image.BILINEAR), # ToTensor() ]) target_loader = data.DataLoader(get_dataset( dataset_name="city16", split="val", img_transform=val_img_transform, label_transform=val_label_transform, test=True, input_ch=3), batch_size=1, pin_memory=True) from tensorpack.utils.stats import MIoUStatistics stat = MIoUStatistics(args.n_class) for index, (origin_imgs, labels, paths) in tqdm(enumerate(target_loader)): if index > quick_test: break path = paths[0] # if index > 10: break imgs = Variable(origin_imgs.cuda(), volatile=True) feature = model_g(imgs) outputs = model_f1(feature) if args.use_f2: outputs += model_f2(feature) pred = outputs[0, :].data.max(0)[1].cpu() feed_predict = np.squeeze(np.uint8(pred.numpy())) feed_label = np.squeeze(np.asarray(labels.numpy())) stat.feed(feed_predict, feed_label) logger.info("tensorpack mIoU: {}".format(stat.mIoU)) logger.info("tensorpack mean_accuracy: {}".format(stat.mean_accuracy)) logger.info("tensorpack accuracy: {}".format(stat.accuracy)) model_g.train() model_f1.train() model_f2.train()
def __init__(self, args): train_img_shape = tuple(args.train_img_shape) self.img_transform = Compose([ Image.fromarray, Scale(train_img_shape, Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]) ]) self.G = torch.load( op.join(args.yaw_checkpoint_dir, 'citycam_to_citycam_model_epoch%s_G.pt' % args.yaw_epoch)) self.C1 = torch.load( op.join(args.yaw_checkpoint_dir, 'citycam_to_citycam_model_epoch%s_C1.pt' % args.yaw_epoch)) self.C2 = torch.load( op.join(args.yaw_checkpoint_dir, 'citycam_to_citycam_model_epoch%s_C2.pt' % args.yaw_epoch)) print('load finished!') self.G.cuda() self.C1.cuda() self.C2.cuda() self.G.eval() self.C1.eval() self.C2.eval() self.input_ch = 3
def __init__(self, args): # Load parameters of the network from training arguments. print("=> loading checkpoint '{}'".format(args.trained_checkpoint)) assert os.path.exists(args.trained_checkpoint), args.trained_checkpoint checkpoint = torch.load(args.trained_checkpoint) train_args = checkpoint["args"] print("----- train args ------") pprint(train_args.__dict__, indent=4) print("-" * 50) self.input_ch = train_args.input_ch assert self.input_ch == 3, self.input_ch self.image_shape = tuple([int(x) for x in train_args.train_img_shape]) print("=> loaded checkpoint '{}'".format(args.trained_checkpoint)) self.img_transform = Compose([ Image.fromarray, Scale(self.image_shape, Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]), ]) self.model = get_full_model(train_args.net, train_args.res, train_args.n_class, train_args.input_ch) self.model.load_state_dict(checkpoint['state_dict']) self.model.eval() if torch.cuda.is_available(): self.model.cuda() self.add_bg_loss = train_args.add_bg_loss self.n_class = train_args.n_class print('=> n_class = %d, add_bg_loss = %s' % (self.n_class, self.add_bg_loss))
def get_unreal_transform(self): ''' Returns the camera to [whatever the camera is attached to] transformation with the Unreal necessary corrections applied. @todo Do we need to expose this? ''' to_unreal_transform = Transform(Rotation(roll=-90, yaw=90), Scale(x=-1)) return self.get_transform() * to_unreal_transform
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]
from torch.autograd import Variable from transform import Colorize from torchvision.transforms import ToPILImage, Compose, ToTensor, CenterCrop, Normalize from transform import Scale, ToLabel, ReLabel from gait import FCN from datasets import CSTestSet from PIL import Image import numpy as np import time import cv2 import os import shutil # os.makedirs("./test") image_transform = Compose([Scale((512, 256), Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]),]) target_transform = Compose([ Scale((512, 256), Image.NEAREST), ToLabel(), ReLabel(), ]) batch_size = 1 dst = CSTestSet("/root/group-incubation-bj", img_transform=image_transform, label_transform=target_transform) testloader = data.DataLoader(dst, batch_size=batch_size, num_workers=8)
from torch.utils import data import torch.optim as optim from torch.autograd import Variable from transform import Colorize from torchvision.transforms import ToPILImage, Compose, ToTensor, CenterCrop from transform import Scale # from resnet import FCN from upsample import FCN # from gcn import FCN from datasets import VOCTestSet from PIL import Image import numpy as np from tqdm import tqdm label_transform = Compose([Scale((256, 256), Image.BILINEAR), ToTensor()]) batch_size = 1 dst = VOCTestSet("./data", transform=label_transform) testloader = data.DataLoader(dst, batch_size=batch_size, num_workers=8) model = torch.nn.DataParallel(FCN(22), device_ids=[0, 1, 2, 3]) # model = FCN(22) model.cuda() model.load_state_dict(torch.load("./pth/fcn-deconv-40.pth")) model.eval() # 10 13 48 86 101
from loss import CrossEntropy2d, CrossEntropyLoss2d from transform import ReLabel, ToLabel, ToSP, Scale, Augment from torchvision.transforms import Compose, CenterCrop, Normalize, ToTensor from PIL import Image import numpy as np import utils from image_augmentor import ImageAugmentor image_augmentor = ImageAugmentor() NUM_CLASSES = 6 MODEL_NAME = "seg-norm" input_transform = Compose([ Scale((512, 256), Image.BILINEAR), Augment(0, image_augmentor), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]), ]) target_transform = Compose([ Scale((512, 256), Image.NEAREST), ToLabel(), ReLabel(), ]) target_2_transform = Compose([ Scale((256, 128), Image.NEAREST), ToLabel(), ReLabel(), ])
# from duc import FCN # from gcn import FCN from datasets_bowl import BowlDataSet from loss import CrossEntropy2d, CrossEntropyLoss2d from visualize import LinePlotter from transform import ReLabel, ToLabel, ToSP, Scale # NUM_CLASSES. NUM_CLASSES = 2 normalize_info = json.load(open('../../input/drn/v1/info.json', 'r')) mean = [m / 255. for m in normalize_info['mean']] std = [m / 255. for m in normalize_info['std']] input_transform = Compose([ Scale((256, 256), Image.BILINEAR), ToTensor(), Normalize(mean, std), ]) target_transform = Compose([ Scale((256, 256), Image.NEAREST), ToSP(256), ToLabel(), # ReLabel(255, 21), ]) trainloader = data.DataLoader(BowlDataSet("../../input/drn/v1/", img_transform=input_transform, label_transform=target_transform), batch_size=16,
learning_rate = 0.00005 epochs = 10 use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") template = open('data/template.obj') content = template.readlines() template.close() decoder_criterion = OxfordLoss() decoder_criterion.a_2d = 10 decoder_criterion.a_3d = 1 decoder_criterion.a_mask = 100 decoder_criterion.a_reg = 0.01 my_transform = Compose([Scale((image_scale, image_scale), Image.BILINEAR), ToTensor()]) root_dir = '/mnt/ext_toshiba/rgb2mesh/DataSets/panoptic-toolbox' dataset_list = ['171026_pose3'] train_dataset = PanopticSet(root_dir, dataset_list=dataset_list, image_size=image_scale, data_par='train', img_transform=my_transform) valid_dataset = PanopticSet(root_dir, dataset_list=dataset_list, image_size=image_scale, data_par='valid', img_transform=my_transform) train_loader = data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True) valid_loader = data.DataLoader(dataset=valid_dataset, batch_size=batch_size, shuffle=False) loaders = {'train':train_loader, 'valid':valid_loader} model = torch.nn.DataParallel(resnet34_Mano(input_option=input_option)) optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Save param dic if resume_flg: json_fn = os.path.join(args.outdir, "param-%s_resume.json" % model_name) else: json_fn = os.path.join(outdir, "param-%s.json" % model_name) check_if_done(json_fn) save_dic_to_json(args.__dict__, json_fn) train_img_shape = tuple([int(x) for x in args.train_img_shape]) from pytorchgo.augmentation.segmentation import SubtractMeans, PIL2NP, RGB2BGR logger.warn( "if you choose deeplab or other weight file, please remember to change the image transform list..." ) img_transform = Compose([ #notice the order!!! Scale(train_img_shape, Image.BILINEAR), PIL2NP(), RGB2BGR(), SubtractMeans(), ToTensor(), #Normalize([.485, .456, .406], [.229, .224, .225]), must delete!!! ]) label_transform = Compose([ Scale(train_img_shape, Image.NEAREST), ToLabel(), ]) val_img_transform = Compose([ Scale(train_img_shape, Image.BILINEAR), PIL2NP(),
ims = torch.cat((ims, ims), 1) ims = ims[:, :32] return ims def get_multiple(self, ): pass if __name__ == '__main__': from transform import (Compose, Normalize, Scale, CenterCrop, CornerCrop, MultiScaleCornerCrop, MultiScaleRandomCrop, RandomHorizontalFlip, ToTensor) D = EPIC_KITCHENS( '/mnt/nisho_data2/hyf/EPIC-annotations/EPIC_train_action_labels.csv', transform=Compose([ Scale([224, 224]), ToTensor(255), Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ])) loader = DataLoader( dataset=D, batch_size=2, shuffle=False, num_workers=8, pin_memory=True, ) print(len(loader)) from tqdm import tqdm for i, sample in tqdm(enumerate(loader)): pass # print(sample['ims'].size()) #(b, 3, cliplen, 224, 224)
configure(tflog_dir, flush_secs=5) # Save param dic if resume_flg: json_fn = os.path.join(args.outdir, "param-%s_resume.json" % model_name) else: json_fn = os.path.join(outdir, "param-%s.json" % model_name) check_if_done(json_fn) save_dic_to_json(args.__dict__, json_fn) train_img_shape = tuple([int(x) for x in args.train_img_shape]) from pytorchgo.augmentation.segmentation import SubtractMeans, PIL2NP, RGB2BGR logger.warn("if you choose deeplab or other weight file, please remember to change the image transform list...") img_transform = Compose([#notice the order!!! Scale(train_img_shape, Image.BILINEAR), PIL2NP(), RGB2BGR(), SubtractMeans(), ToTensor(), #Normalize([.485, .456, .406], [.229, .224, .225]), must delete!!! ]) label_transform = Compose([ Scale(train_img_shape, Image.NEAREST), ToLabel(), ]) val_img_transform = Compose([ Scale(train_img_shape, Image.BILINEAR), PIL2NP(),
args.train_img_shape = checkpoint["args"].train_img_shape print("=> loaded checkpoint '{}'".format(args.trained_checkpoint)) base_outdir = os.path.join(args.outdir, args.mode, model_name) mkdir_if_not_exist(base_outdir) json_fn = os.path.join(base_outdir, "param.json") check_if_done(json_fn) args.machine = os.uname()[1] save_dic_to_json(args.__dict__, json_fn) train_img_shape = tuple([int(x) for x in train_args.train_img_shape]) test_img_shape = tuple([int(x) for x in args.test_img_shape]) img_transform = Compose([ Scale(train_img_shape, Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]), ]) label_transform = Compose([Scale(train_img_shape, Image.BILINEAR), ToTensor()]) tgt_dataset = get_dataset(dataset_name=args.tgt_dataset, split=args.split, img_transform=img_transform, label_transform=label_transform, test=True, input_ch=train_args.input_ch) target_loader = data.DataLoader(tgt_dataset, batch_size=1, pin_memory=True) try: G, F1, F2 = get_models(net_name=train_args.net,
from model import FCN from datasets import CSTestSet from PIL import Image import numpy as np import config import cv2 import os import shutil shutil.rmtree("./test") os.makedirs("./test") from evaluation import get_iou_list image_transform = Compose([ Scale((512, 256), Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]), ]) target_transform = Compose([ Scale((512, 256), Image.NEAREST), ToLabel(), ReLabel(), ]) batch_size = 1 dst = CSTestSet("/root/group-incubation-bj", img_transform=image_transform, label_transform=target_transform)
def __init__(self, args): # Load parameters of the network from training arguments. print("=> loading checkpoint '{}'".format(args.trained_checkpoint)) assert os.path.exists(args.trained_checkpoint), args.trained_checkpoint checkpoint = torch.load(args.trained_checkpoint) train_args = checkpoint["args"] print("----- train args ------") pprint(train_args.__dict__, indent=4) print("-" * 50) self.input_ch = train_args.input_ch self.image_shape = tuple([int(x) for x in train_args.train_img_shape]) print("=> loaded checkpoint '{}'".format(args.trained_checkpoint)) self.img_transform = Compose([ Image.fromarray, Scale(self.image_shape, Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]), ]) try: self.G, self.F1, self.F2 = get_models( net_name=train_args.net, res=train_args.res, input_ch=train_args.input_ch, n_class=train_args.n_class, method=train_args.method, is_data_parallel=train_args.is_data_parallel, use_ae=args.use_ae) except AttributeError: self.G, self.F1, self.F2 = get_models(net_name=train_args.net, res=train_args.res, input_ch=train_args.input_ch, n_class=train_args.n_class, method="MCD", is_data_parallel=False) self.G.load_state_dict(checkpoint['g_state_dict']) self.F1.load_state_dict(checkpoint['f1_state_dict']) if args.use_f2: self.F2.load_state_dict(checkpoint['f2_state_dict']) print("=> loaded checkpoint '{}' (epoch {})".format( args.trained_checkpoint, checkpoint['epoch'])) self.G.eval() self.F1.eval() self.F2.eval() if torch.cuda.is_available(): self.G.cuda() self.F1.cuda() self.F2.cuda() self.use_f2 = args.use_f2 self.add_bg_loss = train_args.add_bg_loss self.n_class = train_args.n_class print('=> n_class = %d, add_bg_loss = %s' % (self.n_class, self.add_bg_loss))
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
scaleStr = "VGA" if noScale else "" nbStr = "NoBall" if nb else "" ngStr = "NoGoal" if ng else "" nrStr = "NoRobot" if nr else "" nlStr = "NoLine" if nl else "" cameraString = "both" if tc == bc else ("top" if tc else "bottom") scale = 1 if noScale else 4 if nb and ng and nr and nl: print("You need to have at least one non-background class!") exit(-1) labSize = (480.0 / scale, 640.0 / scale) 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),
def __init__( self, split='train', # Either train or validation valset=0, # Specifies the number partition for validation images random_transform=False, # Boolean for random data augmentation synth_augmen_types=None, # List of indices for folders of images that will be used as class augmentation synth_augmen_ratio=0, # Percentage (wrt the training images) from augmentation folder that will be included in the train set compute_class_weights=False, # If True it computes class weights for the whole dataset (original + augmented) excluded_classes=None ): # List of integers specifying the classes that are not gonna be used # Dataset Labels: number of categories, names, and associated colors (the very first to be computed) self.update_classes(excluded=excluded_classes) self.root = dataset_path self.split = split self.valset = valset self.files = collections.defaultdict( list ) # pairs of image+label names separated in train and validation self.random_transform = random_transform self.median_frequencies = np.ones(self.num_classes) # Image and label transformations self.target_size = 256 self.img_transform = Compose([ Scale((self.target_size, self.target_size), Image.BILINEAR), ToTensor(), Normalize([.485, .456, .406], [.229, .224, .225]) # Useful when using pre-trained nets ]) self.source_size = 250 self.label_transform = Compose([ ToELFWLabel(self.label_colors, self.source_size), Scale((self.target_size, self.target_size), Image.NEAREST), ]) # Populating the dataset val_set_name = "elfw_set" faces_folder = "faces" labels_folder = "labels" val_set = osp.join( self.root, val_set_name + "_%s.txt" % str(self.valset).zfill(2)) val_file = open(val_set, "r") val_names = [osp.splitext(file.strip())[0] for file in val_file] val_file.close() face_files = [] if self.split == 'train': face_files = os.listdir(osp.join(self.root, faces_folder)) elif self.split == 'validation': face_files = val_names elif self.split == 'test': return else: print("Error: undefined split type!") exit(1) print(bcolors.YELLOW + "Loading ELFW split \'%s\' from %s" % (split, self.root) + bcolors.ENDC) for filename in face_files: name = osp.splitext(filename)[0] if self.split == 'train': if name in val_names: # Skip validation images from training continue img_file = osp.join(self.root, osp.join(faces_folder, "%s.jpg" % name)) label_file = osp.join(self.root, osp.join(labels_folder, "%s.png" % name)) if not osp.exists(label_file): print(bcolors.BOLD + 'Labels not found in ' + label_file + bcolors.ENDC) continue self.files[self.split].append({ "img": img_file, "label": label_file }) # Define the augmentation folders that to be used if self.split == 'train': self.augmentation_folders(synth_augmen_types) # Add images from the synth_augmen_folder (if requested) if self.split == 'train' and self.synth_augmen_folders and synth_augmen_ratio > 0: n_train_images = len(self.files[self.split]) n_aug_images_total = int(synth_augmen_ratio * n_train_images) n_aug_images_part = int(n_aug_images_total / len(self.synth_augmen_folders)) for sf in self.synth_augmen_folders: synth_augmen_folder = sf['path'] synth_aug_files = os.listdir( osp.join(synth_augmen_folder, 'faces')) print((bcolors.BLUE + "Synthetic augmentation: %d out of %d images for %s at %s" + bcolors.ENDC) % \ (n_aug_images_part, len(synth_aug_files), sf['name'], synth_augmen_folder)) # Shuffle all augmentation images and keep adding them until we have as much as n_aug_images random.shuffle(synth_aug_files) c = 0 for aug_filename in synth_aug_files: # remove the extension name = osp.splitext(aug_filename)[0] # Check if this image belongs to the validation set also, this is, if it is an image from # the validation set that has been augmented. In this case, we discard it: # All augmented images are composed of the original name of the person # and an augmentatio ID for the different assets. this ID always starts with '_elfw' # so it's something like name_surname_0003_elfw-sunglasses-12 # we need to know if the name_surname_0003 part is in the val_names list if name[:name.find("_elfw")] in val_names: continue # Get image and labels names img_file = osp.join(synth_augmen_folder, 'faces', "%s.jpg" % name) label_file = osp.join(synth_augmen_folder, 'labels', "%s.png" % name) # Check existence of label file - for security if not osp.exists(label_file): print(bcolors.RED + 'Labels not found in ' + label_file + bcolors.ENDC) continue # Add pair into the training list self.files[self.split].append({ "img": img_file, "label": label_file }) # Check how many augmentation images have been used so far. If max is reached, then break c += 1 if (c == n_aug_images_part): break # Shuffle images so they get mixed across different synthetic augmentation folders random.shuffle(self.files[self.split]) if compute_class_weights and self.split == 'train': print(bcolors.GREEN + "Computing class balancing weights..." + bcolors.ENDC) self.__compute_class_balance_weights__() print( ("Loaded " + bcolors.BOLD + "%s" + bcolors.ENDC + " split with " + bcolors.BOLD + "%d" + bcolors.ENDC + " items") % (self.split, len(self.files[self.split])))
args.tflog_dir = os.path.join(args.outdir, "tflog", model_name) mkdir_if_not_exist(args.pth_dir) mkdir_if_not_exist(args.tflog_dir) json_fn = os.path.join(args.outdir, "param-%s.json" % model_name) check_if_done(json_fn) args.machine = os.uname()[1] save_dic_to_json(args.__dict__, json_fn) start_epoch = 0 train_img_shape = 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(), # RandomVerticalFlip(), # non-realistic RandomHorizontalFlip(), RandomSizedCrop() ] img_transform_list = aug_list + img_transform_list img_transform = Compose(img_transform_list)
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")