def main(): set_gpu('0') save_path = 'features/test_new_domain_miniimagenet/' test_set = ImageNet(root='../cross-domain-fsl/dataset/mini-imagenet/test_new_domain') val_loader = DataLoader(dataset=test_set, batch_size=1, shuffle=False, num_workers=8, pin_memory=True) model = resnet50() model = torch.nn.DataParallel(model).cuda() model.load_state_dict(torch.load('save/proto-5/max-acc.pth')) # model_dict = model.state_dict() # pretrained_dict = model_zoo.load_url('https://download.pytorch.org/models/resnet50-19c8e357.pth') # # 1. filter out unnecessary keys # pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict} # # 2. overwrite entries in the existing state dict # print pretrained_dict # model_dict.update(pretrained_dict) # # 3. load the new state dict # model.load_state_dict(model_dict) model = model.cuda() model.eval() # model = torch.nn.DataParallel(model).cuda() features = [[] for i in range(359)] for (image, label) in val_loader: image = image.cuda() label = label.numpy() feature = model(image) feature = feature.data.cpu().numpy() # print feature.shape[0] for j in range(feature.shape[0]): features[int(label[j])].append(feature[j]) for i in range(359): save_file = os.path.join(save_path, str(i)+'.txt') feature_np = np.asarray(features[i]) np.savetxt(save_file, feature_np)
def __init__(self, model_ckpt, serve=False): utils.set_gpu(1) num_classes = 340 image_size = 128 self._is = image_size self.images = tf.placeholder(shape=[None, image_size, image_size, 1], dtype=tf.uint8) image_prep_fn = preprocessing_factory.get_preprocessing( 'inception_v1', is_training=False) images_preped = image_prep_fn(self.images, None, None) class_logits, self.feat = fixed_model.build_net( images_preped, num_classes, False) self.class_probs = tf.nn.softmax(class_logits) #preds = tf.argmax(class_probs, axis=-1) saver = tf.train.Saver(tf.global_variables()) self.sess = tf.Session() saver.restore(self.sess, model_ckpt) self.cls_id_map = pkl.load(file('class_id_map.pkl', 'rb')) self.id_cls_map = {} for k in self.cls_id_map: self.id_cls_map[self.cls_id_map[k]] = k if serve: self.serve_hint()
def main(unused_argv): if os.path.exists(os.path.join(FLAGS.Ensemble_Models, "16_inference")): shutil.rmtree(os.path.join(FLAGS.Ensemble_Models, "16_inference")) utils.set_gpu(1) logging.set_verbosity(tf.logging.INFO) print("tensorflow version: %s" % tf.__version__) #with tf.device("/cpu:0"): evaluate()
def main(opt: argparse.Namespace): # Configure environment utils.set_gpu(opt.gpu) device = torch.device("cuda") # Configure networks separation_model: nn.Module = SeparationNet( ResNetEncoder(1, [16, 32, 64, 128, 512]), ResNetDecoder(1), ResNetDecoder(1)) completion_model: nn.Module = HourGlass(ResNetEncoder(1), ResNetDecoder(1)) triplet_model: nn.Module = TripletNet(ResNetEncoder(1)) # Load checkpoints separation_model.load_state_dict( torch.load(opt.model_path + "_separation.pt")) completion_model.load_state_dict( torch.load(opt.model_path + "_completion.pt")) triplet_model.load_state_dict(torch.load(opt.model_path + "_triplet.pt")) separation_model = separation_model.to(device) completion_model = completion_model.to(device) triplet_model = triplet_model.to(device) # Make sure models are in evaluation mode separation_model.eval() completion_model.eval() triplet_model.eval() # Compute domain confusion train_confusion_results = evaluate_confusion( separation_model, completion_model, triplet_model, device, opt.confusion_train_path, opt.scannet_path, opt.shapenet_path, opt.confusion_num_neighbors) print(train_confusion_results) val_confusion_results = evaluate_confusion(separation_model, completion_model, triplet_model, device, opt.confusion_val_path, opt.scannet_path, opt.shapenet_path, opt.confusion_num_neighbors) print(val_confusion_results) # Compute similarity metrics train_retrieval_accuracy = evaluate_similarity_metrics( separation_model, completion_model, triplet_model, device, opt.similarity_path, opt.scannet_path, opt.shapenet_path)
def main() -> None: args = parse_arguments() u.set_gpu(args.gpu) # create output folder and save arguments in a .txt file outpath = os.path.join( './results/', args.outdir if args.outdir is not None else u.random_code()) os.makedirs(outpath, exist_ok=True) print(colored('Saving to %s' % outpath, 'yellow')) u.write_args(os.path.join(outpath, 'args.txt'), args) # get a list of patches organized as dictionaries with image, mask and name fields patches = extract_patches(args) print(colored('Processing %d patches' % len(patches), 'yellow')) # instantiate a trainer T = Training(args, outpath) # interpolation for i, patch in enumerate(patches): print( colored('\nThe data shape is %s' % str(patch['image'].shape), 'cyan')) std = T.load_data(patch) print(colored('the std of coarse data is %.2e, ' % std, 'cyan'), end="") if np.isclose(std, 0., atol=1e-12): # all the data are corrupted print(colored('skipping...', 'cyan')) T.out_best = T.img * T.mask T.elapsed = 0. else: # TODO add the transfer learning option if i == 0 or (args.start_from_prev and T.net is None): T.build_model() T.build_input() T.optimize() T.save_result() T.clean() print(colored('Interpolation done! Saved to %s' % outpath, 'yellow'))
def import_awa2_graph(self, awa2_weights, specific_split, att_weight): from images_graph_creator import Awa2GraphCreator, ImagesEmbeddings weights_dict = { 'classes_edges': awa2_weights[0], 'labels_edges': awa2_weights[1] } set_gpu(self.args.gpu) graph_preparation = ImagesEmbeddings(self.args) dict_name_class, dict_class_name = graph_preparation.dict_name_class, graph_preparation.dict_class_name seen_classes, unseen_classes = graph_preparation.seen_classes, graph_preparation.unseen_classes embeds_matrix, dict_image_embed, dict_image_class = graph_preparation.images_embed_calculator( ) dict_idx_image_class = { i: dict_name_class[dict_image_class[image]] for i, image in enumerate(list(dict_image_class.keys())) } awa2_graph_creator = Awa2GraphCreator(embeds_matrix, dict_image_embed, dict_name_class, dict_idx_image_class, self.args.graph_percentage, self.args) image_graph = awa2_graph_creator.create_image_graph() kg, dict_class_nodes_translation = awa2_graph_creator.imagenet_knowledge_graph( ) kg = awa2_graph_creator.attributed_graph(kg, att_weight) seen_classes = [dict_class_nodes_translation[c] for c in seen_classes] unseen_classes = [ dict_class_nodes_translation[c] for c in unseen_classes ] split = {'seen': seen_classes, 'unseen': unseen_classes} labels_graph = awa2_graph_creator.create_labels_graph( dict_class_nodes_translation) awa2_graph = awa2_graph_creator.weighted_graph(image_graph, kg, labels_graph, weights_dict) nx.write_gpickle(awa2_graph, 'awa2/train/awa2_graph') if specific_split: return awa2_graph, split else: split = None return awa2_graph, split
def test_proxnet(args): def load_proxnet(args): ckp = torch.load(args.net_path) alpha_learned = ckp['alpha'] net = ProxNet(args).cuda() net.load_state_dict(ckp['net_state_dict']) net.alpha = torch.from_numpy(alpha_learned) net.eval() print('alpha={}'.format(net.alpha)) return net operator = OperatorBatch(sampling=args.sampling.upper()).cuda() H, HT = operator.forward, operator.adjoint bloch = BLOCH().cuda() args.dtype = set_gpu(args.cuda) net = load_proxnet(args) batch_size = 1 test_loader = torch.utils.data.DataLoader(dataset=data.MRFData( mod='test', sampling=args.sampling), batch_size=batch_size, shuffle=False) rmse_m, rmse_x, rmse_y = [], [], [] rmse_torch = lambda a, b: torch.norm(a - b, 2).detach().cpu().numpy( ) / torch.norm(b, 2).detach().cpu().numpy() / batch_size toc = time.time() for x, m, y in test_loader: m, y = m.type(args.dtype), y.type(args.dtype) HTy = HT(y).type(args.dtype) m_seq, x_seq = net(HTy, H, HT, bloch) m_hat = m_seq[-1] rmse_m.append(rmse_torch(m_hat, m)) elapsed = time.time() - toc print('time: {}'.format(elapsed / 16)) print('m error mean:{}, max: {}, std:{}'.format(np.mean(rmse_m), np.max(rmse_m), np.std(rmse_m)))
help='Number of classes in meta training testing set (N in "N-way", default: 1).') parser.add_argument('--alg', type=str, default='iMAML') parser.add_argument('--num_test_task', type=int, default=2, help='Number of test tasks ( default: 1).') parser.add_argument('--num_task', type=int, default=20, help='Number of tasks ( default: 10).') # algorithm settings parser.add_argument('--n_inner', type=int, default=150) parser.add_argument('--inner_lr', type=float, default=1e-5) parser.add_argument('--inner_opt', type=str, default='SGD') parser.add_argument('--outer_lr', type=float, default=1e-5) parser.add_argument('--outer_opt', type=str, default='Adam') parser.add_argument('--lr_sched', type=lambda x: (str(x).lower() == 'true'), default=False) args = parser.parse_args() return args if __name__ == '__main__': args = parse_args() set_seed(args.seed) set_gpu(args.device) check_dir(args) main(args)
def train_proxnet(args): check_paths(args) # init GPU configuration args.dtype = set_gpu(args.cuda) # init seed np.random.seed(args.seed) torch.manual_seed(args.seed) # define training data train_dataset = data.MRFData(mod='train', sampling=args.sampling) train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=args.batch_size, shuffle=True) # init operators (subsampling + subspace dimension reduction + Fourier transformation) operator = OperatorBatch(sampling=args.sampling.upper()).cuda() H, HT = operator.forward, operator.adjoint bloch = BLOCH().cuda() # init PGD-Net (proxnet) proxnet = ProxNet(args).cuda() # init optimizer optimizer = torch.optim.Adam([{ 'params': proxnet.transformnet.parameters(), 'lr': args.lr, 'weight_decay': args.weight_decay }, { 'params': proxnet.alpha, 'lr': args.lr2 }]) scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[20], gamma=0.1) # init loss mse_loss = torch.nn.MSELoss() #.cuda() # init meters log = LOG(args.save_model_dir, filename=args.filename, field_name=[ 'iter', 'loss_m', 'loss_x', 'loss_y', 'loss_total', 'alpha' ]) loss_epoch = 0 loss_m_epoch, loss_x_epoch, loss_y_epoch = 0, 0, 0 # start PGD-Net training print('start training...') for e in range(args.epochs): proxnet.train() loss_m_seq = [] loss_x_seq = [] loss_y_seq = [] loss_total_seq = [] for x, m, y in train_loader: # covert data type (cuda) x, m, y = x.type(args.dtype), m.type(args.dtype), y.type( args.dtype) # add noise noise = args.noise_sigam * torch.randn(y.shape).type(args.dtype) HTy = HT(y + noise).type(args.dtype) # PGD-Net computation (iteration) # output the reconstructions (sequence) of MRF image x and its tissue property map m m_seq, x_seq = proxnet(HTy, H, HT, bloch) loss_x, loss_y, loss_m = 0, 0, 0 for t in range(args.time_step): loss_y += mse_loss(H(x_seq[t]), y) / args.time_step for i in range(3): loss_m += args.loss_weight['m'][i] * mse_loss( m_seq[-1][:, i, :, :], m[:, i, :, :]) loss_x = mse_loss(x_seq[-1], x) # compute loss loss_total = loss_m + args.loss_weight[ 'x'] * loss_x + args.loss_weight['y'] * loss_y # update gradient optimizer.zero_grad() loss_total.backward() optimizer.step() # update meters loss_m_seq.append(loss_m.item()) loss_x_seq.append(loss_x.item()) loss_y_seq.append(loss_y.item()) loss_total_seq.append(loss_total.item()) # (scheduled) update learning rate scheduler.step() # print meters loss_m_epoch = np.mean(loss_m_seq) loss_x_epoch = np.mean(loss_x_seq) loss_y_epoch = np.mean(loss_y_seq) loss_epoch = np.mean(loss_total_seq) log.record(e + 1, loss_m_epoch, loss_x_epoch, loss_y_epoch, loss_epoch, proxnet.alpha.detach().cpu().numpy()) logT( "==>Epoch {}\tloss_m: {:.6f}\tloss_x: {:.6f}\tloss_y: {:.6f}\tloss_total: {:.6f}\talpha: {}" .format(e + 1, loss_m_epoch, loss_x_epoch, loss_y_epoch, loss_epoch, proxnet.alpha.detach().cpu().numpy())) # save checkpoint if args.checkpoint_model_dir is not None and ( e + 1) % args.checkpoint_interval == 0: proxnet.eval() ckpt = { 'epoch': e + 1, 'loss_m': loss_m_epoch, 'loss_x': loss_x_epoch, 'loss_y': loss_y_epoch, 'total_loss': loss_epoch, 'net_state_dict': proxnet.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), 'alpha': proxnet.alpha.detach().cpu().numpy() } torch.save( ckpt, os.path.join(args.checkpoint_model_dir, 'ckp_epoch_{}.pt'.format(e))) proxnet.train() # save model proxnet.eval() state = { 'epoch': args.epochs, 'loss_m': loss_m_epoch, 'loss_x': loss_x_epoch, 'loss_y': loss_y_epoch, 'total_loss': loss_epoch, 'alpha': proxnet.alpha.detach().cpu().numpy(), 'net_state_dict': proxnet.state_dict(), 'optimizer_state_dict': optimizer.state_dict() } save_model_path = os.path.join(args.save_model_dir, log.filename + '.pt') torch.save(state, save_model_path) print("\nDone, trained model saved at", save_model_path)
import time from networks import SRCNN from utils import Tools, set_gpu from configs import Config import tensorflow as tf import os from tqdm import tqdm set_gpu(0) def log10(x): numerator = tf.log(x) denominator = tf.log(tf.constant(10, dtype=numerator.dtype)) return numerator / denominator def train(cfg, data_loder, test_data): TRAIN_INPUTS = tf.placeholder('float32', [None, 33, 33, 1]) TRAIN_LABELS = tf.placeholder('float32', [None, 21, 21, 1]) VAL_INPUTS = tf.placeholder('float32', [None, None, None, 1]) VAL_LABELS = tf.placeholder('float32', [None, None, None, 1]) model = SRCNN() train_inference = model(TRAIN_INPUTS, padding='VALID', name='log') val_inference = model(VAL_INPUTS, padding='SAME', name='log') # train_loss = tf.losses.mean_squared_error(TRAIN_LABELS / 255.0, train_inference) train_loss = tf.reduce_mean( tf.reduce_sum(tf.square(TRAIN_LABELS / 255.0 - train_inference),
def main(): if args.seed is not None: random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) # Make the a directory corresponding to this run for saving results, checkpoints etc. i = 0 while True: run_base_dir = pathlib.Path(f"{args.log_dir}/{args.name}~try={str(i)}") if not run_base_dir.exists(): os.makedirs(run_base_dir) args.name = args.name + f"~try={i}" break i += 1 (run_base_dir / "settings.txt").write_text(str(args)) args.run_base_dir = run_base_dir print(f"=> Saving data in {run_base_dir}") # Get dataloader. data_loader = getattr(data, args.set)() # Track accuracy on all tasks. if args.num_tasks: best_acc1 = [0.0 for _ in range(args.num_tasks)] curr_acc1 = [0.0 for _ in range(args.num_tasks)] adapt_acc1 = [0.0 for _ in range(args.num_tasks)] # Get the model. model = utils.get_model() # If necessary, set the sparsity of the model of the model using the ER sparsity budget (see paper). if args.er_sparsity: for n, m in model.named_modules(): if hasattr(m, "sparsity"): m.sparsity = min( 0.5, args.sparsity * (m.weight.size(0) + m.weight.size(1)) / (m.weight.size(0) * m.weight.size(1) * m.weight.size(2) * m.weight.size(3)), ) print(f"Set sparsity of {n} to {m.sparsity}") # Put the model on the GPU, model = utils.set_gpu(model) # Optionally resume from a checkpoint. if args.resume: if os.path.isfile(args.resume): print(f"=> Loading checkpoint '{args.resume}'") checkpoint = torch.load(args.resume, map_location=f"cuda:{args.multigpu[0]}") best_acc1 = checkpoint["best_acc1"] pretrained_dict = checkpoint["state_dict"] model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } model_dict.update(pretrained_dict) model.load_state_dict(pretrained_dict) print( f"=> Loaded checkpoint '{args.resume}' (epoch {checkpoint['epoch']})" ) else: print(f"=> No checkpoint found at '{args.resume}'") criterion = nn.CrossEntropyLoss().to(args.device) writer = SummaryWriter(log_dir=run_base_dir) # Track the number of tasks learned. num_tasks_learned = 0 trainer = getattr(trainers, args.trainer or "default") print(f"=> Using trainer {trainer}") train, test = trainer.train, trainer.test # Initialize model specific context (editorial note: avoids polluting main file) if hasattr(trainer, "init"): trainer.init(args) # TODO: Put this in another file if args.task_eval is not None: assert 0 <= args.task_eval < args.num_tasks, "Not a valid task idx" print(f"Task {args.set}: {args.task_eval}") model.apply(lambda m: setattr(m, "task", args.task_eval)) assert hasattr( data_loader, "update_task" ), "[ERROR] Need to implement update task method for use with multitask experiments" data_loader.update_task(args.task_eval) optimizer = get_optimizer(args, model) lr_scheduler = schedulers.get_policy(args.lr_policy or "cosine_lr")(optimizer, args) # Train and do inference and normal for args.epochs epcohs. best_acc1 = 0.0 for epoch in range(0, args.epochs): lr_scheduler(epoch, None) train( model, writer, data_loader.train_loader, optimizer, criterion, epoch, task_idx=args.task_eval, data_loader=None, ) curr_acc1 = test( model, writer, criterion, data_loader.val_loader, epoch, task_idx=args.task_eval, ) if curr_acc1 > best_acc1: best_acc1 = curr_acc1 utils.write_result_to_csv( name=f"{args.name}~set={args.set}~task={args.task_eval}", curr_acc1=curr_acc1, best_acc1=best_acc1, save_dir=run_base_dir, ) if args.save: torch.save( { "epoch": args.epochs, "arch": args.model, "state_dict": model.state_dict(), "best_acc1": best_acc1, "curr_acc1": curr_acc1, "args": args, }, run_base_dir / "final.pt", ) return best_acc1 # Iterate through all tasks. for idx in range(args.num_tasks or 0): print(f"Task {args.set}: {idx}") # Tell the model which task it is trying to solve -- in Scenario NNs this is ignored. model.apply(lambda m: setattr(m, "task", idx)) # Update the data loader so that it returns the data for the correct task, also done by passing the task index. assert hasattr( data_loader, "update_task" ), "[ERROR] Need to implement update task method for use with multitask experiments" data_loader.update_task(idx) # Clear the grad on all the parameters. for p in model.parameters(): p.grad = None # Make a list of the parameters relavent to this task. params = [] for n, p in model.named_parameters(): if not p.requires_grad: continue split = n.split(".") if split[-2] in ["scores", "s", "t" ] and (int(split[-1]) == idx or (args.trainer and "nns" in args.trainer)): params.append(p) # train all weights if train_weight_tasks is -1, or num_tasks_learned < train_weight_tasks if (args.train_weight_tasks < 0 or num_tasks_learned < args.train_weight_tasks): if split[-1] == "weight" or split[-1] == "bias": params.append(p) # train_weight_tasks specifies the number of tasks that the weights are trained for. # e.g. in SupSup, train_weight_tasks = 0. in BatchE, train_weight_tasks = 1. # If training weights, use train_weight_lr. Else use lr. lr = (args.train_weight_lr if args.train_weight_tasks < 0 or num_tasks_learned < args.train_weight_tasks else args.lr) # get optimizer, scheduler if args.optimizer == "adam": optimizer = optim.Adam(params, lr=lr, weight_decay=args.wd) elif args.optimizer == "rmsprop": optimizer = optim.RMSprop(params, lr=lr) else: optimizer = optim.SGD(params, lr=lr, momentum=args.momentum, weight_decay=args.wd) train_epochs = args.epochs if args.no_scheduler: scheduler = None else: scheduler = CosineAnnealingLR(optimizer, T_max=train_epochs) # Train on the current task. for epoch in range(1, train_epochs + 1): train( model, writer, data_loader.train_loader, optimizer, criterion, epoch, idx, data_loader, ) # Required for our PSP implementation, not used otherwise. utils.cache_weights(model, num_tasks_learned + 1) curr_acc1[idx] = test(model, writer, criterion, data_loader.val_loader, epoch, idx) if curr_acc1[idx] > best_acc1[idx]: best_acc1[idx] = curr_acc1[idx] if scheduler: scheduler.step() if (args.iter_lim > 0 and len(data_loader.train_loader) * epoch > args.iter_lim): break utils.write_result_to_csv( name=f"{args.name}~set={args.set}~task={idx}", curr_acc1=curr_acc1[idx], best_acc1=best_acc1[idx], save_dir=run_base_dir, ) # Save memory by deleting the optimizer and scheduler. del optimizer, scheduler, params # Increment the number of tasks learned. num_tasks_learned += 1 # If operating in NNS scenario, get the number of tasks learned count from the model. if args.trainer and "nns" in args.trainer: model.apply(lambda m: setattr( m, "num_tasks_learned", min(model.num_tasks_learned, args.num_tasks))) else: model.apply( lambda m: setattr(m, "num_tasks_learned", num_tasks_learned)) # TODO series of asserts with required arguments (eg num_tasks) # args.eval_ckpts contains values of num_tasks_learned for which testing on all tasks so far is performed. # this is done by default when all tasks have been learned, but you can do something like # args.eval_ckpts = [5,10] to also do this when 5 tasks are learned, and again when 10 tasks are learned. if num_tasks_learned in args.eval_ckpts or num_tasks_learned == args.num_tasks: avg_acc = 0.0 avg_correct = 0.0 # Settting task to -1 tells the model to infer task identity instead of being given the task. model.apply(lambda m: setattr(m, "task", -1)) # an "adaptor" is used to infer task identity. # args.adaptor == gt implies we are in scenario GG. # This will cache all of the information the model needs for inferring task identity. if args.adaptor != "gt": utils.cache_masks(model) # Iterate through all tasks. adapt = getattr(adaptors, args.adaptor) for i in range(num_tasks_learned): print(f"Testing {i}: {args.set} ({i})") # model.apply(lambda m: setattr(m, "task", i)) # Update the data loader so it is returning data for the right task. data_loader.update_task(i) # Clear the stored information -- memory leak happens if not. for p in model.parameters(): p.grad = None for b in model.buffers(): b.grad = None torch.cuda.empty_cache() adapt_acc = adapt( model, writer, data_loader.val_loader, num_tasks_learned, i, ) adapt_acc1[i] = adapt_acc avg_acc += adapt_acc torch.cuda.empty_cache() utils.write_adapt_results( name=args.name, task=f"{args.set}_{i}", num_tasks_learned=num_tasks_learned, curr_acc1=curr_acc1[i], adapt_acc1=adapt_acc, task_number=i, ) writer.add_scalar("adapt/avg_acc", avg_acc / num_tasks_learned, num_tasks_learned) utils.clear_masks(model) torch.cuda.empty_cache() if args.save: torch.save( { "epoch": args.epochs, "arch": args.model, "state_dict": model.state_dict(), "best_acc1": best_acc1, "curr_acc1": curr_acc1, "args": args, }, run_base_dir / "final.pt", ) return adapt_acc1
import sys from utils import set_gpu from utils import set_gpu import sys set_gpu(sys.argv) from keras.models import Model from keras.layers import Dense, Input from hyperopt import hp, fmin, tpe, hp, STATUS_OK, Trials, space_eval from keras.losses import mean_squared_error import tensorflow as tf from keras import backend as K import os import utils import inspect import numpy as np script_name = os.path.basename(__file__).split('.')[0] x_train, x_val, x_test = utils.generate_data_medium_2() space = { 'units1': hp.quniform( 'units1', 0, 100, 4 ), #implementation of hq.uniform is weird see github.com/hyperopt/hyperopt/issues/321 'units2': hp.quniform( 'units2', 0, 100, 4 ), #implementation of hq.uniform is weird see github.com/hyperopt/hyperopt/issues/321 'batch_size': hp.choice('batch_size', [128]) }
def main(opt: argparse.Namespace) -> None: utils.set_gpu(opt.gpu) device = torch.device("cuda") run_name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") run_path = os.path.join(opt.output_root, run_name) print(f"Start training {run_path}") print(vars(opt)) # Save config os.makedirs(run_path, exist_ok=True) with open(os.path.join(run_path, "config.json"), "w") as f: json.dump(vars(opt), f, indent=4) # Data train_dataset: Dataset = data.Scan2Cad( opt.scan2cad_file, opt.scannet_path, opt.shapenet_path, "train", ["train"], rotation=opt.rotation_augmentation, flip=opt.flip_augmentation, jitter=opt.jitter_augmentation, transformation=data.to_occupancy_grid, scan_rep="sdf", load_mask=True, add_negatives=True) train_dataloader: DataLoader = DataLoader(train_dataset, shuffle=True, batch_size=opt.batch_size, num_workers=opt.num_workers, pin_memory=True) val_dataset: Dataset = data.Scan2Cad(opt.scan2cad_file, opt.scannet_path, opt.shapenet_path, "validation", ["validation"], rotation=opt.rotation_augmentation, flip=opt.flip_augmentation, jitter=opt.jitter_augmentation, transformation=data.to_occupancy_grid, scan_rep="sdf", load_mask=True, add_negatives=True) val_dataloader: DataLoader = DataLoader(val_dataset, shuffle=False, batch_size=opt.batch_size, num_workers=opt.num_workers, pin_memory=True) # Models separation_model: nn.Module = SeparationNet( ResNetEncoder(1, [16, 32, 64, 128, 512]), ResNetDecoder(1), ResNetDecoder(1)) completion_model: nn.Module = HourGlass(ResNetEncoder(1), ResNetDecoder(1)) triplet_model: nn.Module = TripletNet(ResNetEncoder(1)) separation_model = separation_model.to(device) completion_model = completion_model.to(device) triplet_model = triplet_model.to(device) model_parameters = list(separation_model.parameters()) + \ list(completion_model.parameters()) + \ list(triplet_model.parameters()) optimizer = optim.Adam(model_parameters, lr=opt.learning_rate, weight_decay=opt.weight_decay) criterion_separation = nn.BCEWithLogitsLoss(reduction="none") criterion_completion = nn.BCEWithLogitsLoss(reduction="none") criterion_triplet = nn.TripletMarginLoss(reduction="none", margin=opt.triplet_margin) # Main loop iteration_number = 0 for epoch in range(opt.num_epochs): train_dataloader.dataset.regenerate_negatives() for _, (scan, cad, negative) in enumerate(train_dataloader): utils.stepwise_learning_rate_decay(optimizer, opt.learning_rate, iteration_number, [40000, 80000, 120000]) separation_model.train() completion_model.train() triplet_model.train() losses = forward(scan, cad, negative, separation_model, completion_model, triplet_model, criterion_separation, criterion_completion, criterion_triplet, device) loss_foreground, loss_background, loss_completion, loss_triplet = losses loss_total = loss_foreground + loss_background + loss_completion + loss_triplet # Train step optimizer.zero_grad() loss_total.backward() optimizer.step() # Log to console if iteration_number % opt.log_frequency == opt.log_frequency - 1: print( f"[E{epoch:04d}, I{iteration_number:05d}]\tTotal: {loss_total: 05.3f}", f"\tFG: {loss_foreground: 05.3f}\tBG: {loss_background: 05.3f}", f"\tCompletion: {loss_completion: 05.3f} \tTriplet: {loss_triplet: 05.3f}" ) # Validate if iteration_number % opt.validate_frequency == opt.validate_frequency - 1: with torch.no_grad(): separation_model.eval() completion_model.eval() triplet_model.eval() val_losses = defaultdict(list) # Go through entire validation set for _, (scan_v, cad_v, negative_v) in tqdm(enumerate(val_dataloader), total=len(val_dataloader), leave=False): losses = forward(scan_v, cad_v, negative_v, separation_model, completion_model, triplet_model, criterion_separation, criterion_completion, criterion_triplet, device) loss_foreground, loss_background, loss_completion, loss_triplet = losses loss_total = loss_foreground + loss_background + loss_completion + loss_triplet val_losses["FG"].append(loss_foreground.item()) val_losses["BG"].append(loss_background.item()) val_losses["Completion"].append(loss_completion.item()) val_losses["Triplet"].append(loss_triplet.item()) val_losses["Total"].append(loss_total.item()) # Aggregate losses val_losses_summary = { k: torch.mean(torch.tensor(v)) for k, v in val_losses.items() } print( f"-Val [E{epoch:04d}, I{iteration_number:05d}]\tTotal: {val_losses_summary['Total']:05.3f}", f"\tFG: {val_losses_summary['FG']:05.3f} \tBG: {val_losses_summary['BG']:05.3f}", f"\tCompletion: {val_losses_summary['Completion']:05.3f}", f"\tTriplet: {val_losses_summary['Triplet']:05.3f}") # Save checkpoint if iteration_number % opt.checkpoint_frequency == opt.checkpoint_frequency - 1: checkpoint_name = f"{run_name}_{iteration_number:05d}" torch.save( separation_model.state_dict(), os.path.join(run_path, f"{checkpoint_name}_separation.pt")) torch.save( completion_model.state_dict(), os.path.join(run_path, f"{checkpoint_name}_completion.pt")) torch.save( triplet_model.state_dict(), os.path.join(run_path, f"{checkpoint_name}_triplet.pt")) print(f"Saved model at {run_path}/{checkpoint_name}") iteration_number += 1
) dloader_test = data_loader( dataset=dataset_test, nKnovel=opt.test_way, nKbase=0, nExemplars=opt.val_shot, # num training examples per novel category nTestNovel=opt.val_query * opt.test_way, # num test examples for all the novel categories nTestBase=0, # num test examples for all the base categories batch_size=1, num_workers=0, epoch_size=1 * opt.val_episode, # num of batches per epoch ) set_gpu(str(opt.gpu_ids)[1:-1]) seed = opt.seed np.random.seed(seed) random.seed(seed) torch.manual_seed(seed) torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False check_dir('./experiments/') opt.save_path = os.path.join('experiments', opt.save_path) check_dir(opt.save_path) log_file_path = os.path.join(opt.save_path, "train_log.txt") log(log_file_path, str(vars(opt))) (embedding_net, cls_head) = get_model(opt)
pred = torch.argmax(logits, dim=1) acc = utils.compute_acc(pred, labels) aves_va.update(acc, 1) va_lst.append(acc) print('test epoch {}: acc={:.2f} +- {:.2f} (%)'.format( epoch, aves_va.item() * 100, utils.mean_confidence_interval(va_lst) * 100)) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--config', help='configuration file') parser.add_argument('--gpu', help='gpu device number', type=str, default='0') parser.add_argument('--efficient', help='if True, enables gradient checkpointing', action='store_true') args = parser.parse_args() config = yaml.load(open(args.config, 'r'), Loader=yaml.FullLoader) if len(args.gpu.split(',')) > 1: config['_parallel'] = True config['_gpu'] = args.gpu utils.set_gpu(args.gpu) main(config)
import logging import numpy as np import tensorflow as tf import tensorflow.keras.backend as K from tensorflow.keras import optimizers from models.losses import contrastive_loss from models.metrics import accuracy_for_distance as accuracy from models.siamese import create_siamese_network from models.vggface import create_vggface_network from models.facenet import create_facenet_network from data import load, generate from utils import set_gpu set_gpu() logging.basicConfig( format="%(asctime)s:%(levelname)s - %(message)s", level=logging.INFO ) # pylint: disable=invalid-name # Model parameters input_shape = 48 # also defines image dimensions model_name = "siamese" # TODO: hyperparameters # Training parameters epochs = 10 batch_size = 32
} ''' Construct the TF graph for training ''' if args.mode.lower() == 'train': input_node, z, x, r, o, w, w_in, m, som_m, w_out, b_out, taus\ = construct_tf(net, settings, training_params) print('Constructed the TF graph...') # Loss function and optimizer loss, training_op = loss_op(o, z, training_params) ''' Start the TF session and train the network ''' sess = tf.Session(config=tf.ConfigProto( gpu_options=set_gpu(args.gpu, args.gpu_frac))) init = tf.global_variables_initializer() if args.mode.lower() == 'train': with tf.Session() as sess: print('Training started...') init.run() training_success = False if args.task.lower() == 'go-nogo': # Go-NoGo task u, label = generate_input_stim_go_nogo(settings) target = generate_target_continuous_go_nogo(settings, label) x0, r0, w0, w_in0, taus_gaus0 = \ sess.run([x, r, w, w_in, taus], feed_dict={input_node: u, z: target})
from tqdm import tqdm import tensorflow as tf from datetime import datetime from preprocessing import preprocessing_factory slim = tf.contrib.slim import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('--lr', type=float, help='init learning rate') parser.add_argument('--model', type=str, help='choice of model') args = parser.parse_args() # In[3]: utils.set_gpu(1) # tunable init_learning_rate = args.lr learning_rate_decay_factor = 0.5 num_epochs_per_decay = 5 weight_decay = 0.00004 train_iters = 100000 # fixed batch_size = 512 num_classes = 340 image_size = 128 num_samples_per_epoch = num_classes * 10000 # In[4]:
def main(): # Seed. if args.seed is not None: random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) np.random.seed(args.seed) # If saving models or saving data, create a folder for storing these files. # args.save => saving models, tensorboard logs, etc. # args.save_data => just saving results. if args.save or args.save_data: i = 0 while True: run_base_dir = pathlib.Path( f"{args.log_dir}/{args.name}+try={str(i)}" ) if not run_base_dir.exists(): os.makedirs(run_base_dir) args.name = args.name + f"+try={i}" break i += 1 (run_base_dir / "settings.txt").write_text(str(args)) args.run_base_dir = run_base_dir print(f"=> Saving data in {run_base_dir}") # Get dataloader. data_loader = getattr(data, args.set)() curr_acc1 = 0.0 # Make a list of models, instead of a single model. # This is not for training subspaces, but rather for the ensemble & SWA baselines. models = [utils.get_model() for _ in range(args.num_models)] # when training the SWA baseline, turn off the gradient to all but the first model. if args.trainswa: for i in range(1, args.num_models): for p in models[i].parameters(): p.requires_grad = False # Resume a model from a saved checkpoint. num_models_filled = 0 num_models = -1 if args.resume: for i, resume in enumerate(args.resume): if type(resume) == tuple: # can use a tuple to provide how many models to load. resume, num_models = resume if os.path.isfile(resume): print(f"=> Loading checkpoint '{resume}'") checkpoint = torch.load(resume, map_location="cpu") pretrained_dicts = [ {k[7:]: v for k, v in c.items()} for c in checkpoint["state_dicts"] ] n = 0 for pretrained_dict in pretrained_dicts: print(num_models_filled) model_dict = models[num_models_filled].state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } model_dict.update(pretrained_dict) models[num_models_filled].load_state_dict(model_dict) num_models_filled += 1 n += 1 if num_models > 0 and n >= num_models: break print( f"=> Loaded checkpoint '{resume}' (epoch {checkpoint['epoch']})" ) else: print(f"=> No checkpoint found at '{resume}'") # Put models on the GPU. models = [utils.set_gpu(m) for m in models] # Get training loss. if args.label_smoothing is None: criterion = nn.CrossEntropyLoss() else: print("adding label smoothing!") criterion = LabelSmoothing(smoothing=args.label_smoothing) criterion = criterion.to(args.device) if args.save: writer = SummaryWriter(log_dir=run_base_dir) else: writer = None # Get the "trainer", which specified how the model is trained. trainer = getattr(trainers, args.trainer or "default") print(f"=> Using trainer {trainer}") train, test = trainer.train, trainer.test # Call "init" on the trainer. trainer.init(models, writer, data_loader) # Since we have have a list of models, we also use a list of optimizers & schedulers. # When training subspaces, this list is of length 1. metrics = {} optimizers = [utils.get_optimizer(args, m) for m in models] lr_schedulers = [ schedulers.get_policy(args.lr_policy or "cosine_lr")(o, args) for o in optimizers if o is not None ] # more logic for resuming a checkpoint, specifically concerned with the "pretrained" argument. # if args.pretrained, then we are not resuming. This means that we start from epoch 0. # if not args.pretrained, we are resuming and have to set the epoch, etc. appropriately. init_epoch = 0 num_models_filled = 0 if args.resume and not args.pretrained: for i, resume in enumerate(args.resume): if os.path.isfile(resume): print(f"=> Loading checkpoint '{resume}'") checkpoint = torch.load(resume, map_location="cpu") init_epoch = checkpoint["epoch"] curr_acc1 = checkpoint["curr_acc1"] for opt in checkpoint["optimizers"]: if args.late_start >= 0: continue optimizers[num_models_filled].load_state_dict(opt) num_models_filled += 1 best_acc1 = 0.0 train_loss = 0.0 # Save the initialization. if init_epoch == 0 and args.save: print("saving checkpoint") utils.save_cpt(init_epoch, 0, models, optimizers, best_acc1, curr_acc1) # If the start epoch == the end epoch, just do evaluation "test". if init_epoch == args.epochs: curr_acc1, metrics = test( models, writer, criterion, data_loader, init_epoch, ) if args.save or args.save_data: metrics["epoch"] = init_epoch utils.write_result_to_csv( name=args.name + f"+curr_epoch={init_epoch}", curr_acc1=curr_acc1, best_acc1=best_acc1, train_loss=train_loss, **metrics, ) # Train from init_epoch -> args.epochs. for epoch in range(init_epoch, args.epochs): for lr_scheduler in lr_schedulers: lr_scheduler(epoch, None) train_loss = train( models, writer, data_loader, optimizers, criterion, epoch, ) if type(train_loss) is tuple: train_loss, optimizers = train_loss if ( args.test_freq is None or (epoch % args.test_freq == 0) or epoch == args.epochs - 1 ): curr_acc1, metrics = test( models, writer, criterion, data_loader, epoch, ) if curr_acc1 > best_acc1: best_acc1 = curr_acc1 metrics["epoch"] = epoch + 1 # This is for the SWA baseline -- we need to lookup if this an epoch for which we are saving a checkpoint. # If so we save a checkpoint and move it to the corresponding place in the models list. if args.trainswa and (epoch + 1) in args.swa_save_epochs: j = args.swa_save_epochs.index(epoch + 1) for m1, m2 in zip(models[0].modules(), models[j].modules()): if isinstance(m1, nn.Conv2d): m2.weight = nn.Parameter(m1.weight.clone().detach()) m2.weight.requires_grad = False elif isinstance(m1, nn.BatchNorm2d): m2.weight = nn.Parameter(m1.weight.clone().detach()) m2.bias = nn.Parameter(m1.bias.clone().detach()) m2.weight.requires_grad = False m2.bias.requires_grad = False # Save checkpoint. if ( args.save and args.save_epochs is not None and (epoch + 1) in args.save_epochs ): it = (epoch + 1) * len(data_loader.train_loader) utils.save_cpt( epoch + 1, it, models, optimizers, best_acc1, curr_acc1 ) # Save results. if args.save or args.save_data: utils.write_result_to_csv( name=args.name, curr_acc1=curr_acc1, best_acc1=best_acc1, train_loss=train_loss, **metrics, ) # Save final checkpiont. if args.save: it = args.epochs * len(data_loader.train_loader) utils.save_cpt( args.epochs, it, models, optimizers, best_acc1, curr_acc1 ) return curr_acc1, metrics
def main(): if args.seed is not None: random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) i = 0 while True: run_base_dir = pathlib.Path(f"{args.log_dir}/{args.name}~try={str(i)}") if not run_base_dir.exists(): os.makedirs(run_base_dir) args.name = args.name + "~try={}".format(str(i)) break i += 1 (run_base_dir / "settings.txt").write_text(str(args)) # get the datasets. count = {} for i in range(len(args.set)): count[args.set[i]] = 1 for j in range(i + 1, len(args.set)): if args.set[j] == args.set[i]: args.set[j] = args.set[j] + '-v{}'.format(count[args.set[i]] + 1) count[args.set[i]] += 1 sets = {set: getattr(data, set.split('-')[0])() for set in args.set} best_acc1 = {set: 0.0 for set in args.set} curr_acc1 = {set: 0.0 for set in args.set} model = utils.get_model() model = utils.set_gpu(model) optimizers = {} schedulers = {} for set in args.set: params = [] for n, p in model.named_parameters(): if not p.requires_grad: continue if n.split(".")[-1] in args.set and n.split(".")[-1] != set: continue params.append(p) optimizers[set] = optim.SGD( params, lr=args.lr, momentum=args.momentum, weight_decay=args.wd, ) schedulers[set] = CosineAnnealingLR(optimizers[set], T_max=args.epochs) criterion = nn.CrossEntropyLoss().to(args.device) writer = SummaryWriter(log_dir=run_base_dir) # Save the initial state torch.save( { "epoch": 0, "model": args.model, "state_dict": model.state_dict(), "best_acc1": best_acc1, "curr_acc1": curr_acc1, "args": args, }, run_base_dir / "init.pt", ) if args.hamming: utils.write_hamming(writer, model, 0) for epoch in range(1, args.epochs + 1): for task, loader in sets.items(): model.apply(lambda m: setattr(m, "task", task)) train(model, writer, loader.train_loader, optimizers[task], criterion, epoch, task) curr_acc1[task] = test(model, writer, criterion, loader.val_loader, epoch, task) if curr_acc1[task] > best_acc1[task]: best_acc1[task] = curr_acc1[task] schedulers[task].step() if epoch == args.epochs or (args.save_every > 0 and (epoch % args.save_every) == 0): torch.save( { "epoch": epoch, "arch": args.model, "state_dict": model.state_dict(), "best_acc1": best_acc1, "curr_acc1": curr_acc1, "args": args, }, run_base_dir / "epoch_{}.pt".format(epoch), ) if args.hamming: utils.write_hamming(writer, model, epoch) for set in args.set: utils.write_result_to_csv( name=args.name + "~task={}".format(set), curr_acc1=curr_acc1[task], best_acc1=best_acc1[task], ) if args.hamming: utils.log_hamming(model)
# Description : Usage example of LoopModel # Author : Antoni Burguera (antoni dot burguera at uib dot com) # History : 4-April-2021 - Creation # Citation : Please, refer to the README file to know how to properly cite # us if you use this software. ############################################################################### ############################################################################### # SET GPU ############################################################################### # These must be the first lines to execute. Restart the kernel before. # If you don't have GPU/CUDA or this does not work, just set it to False or # (preferrably) remove these two lines. from utils import set_gpu set_gpu(True) ############################################################################### # IMPORTS ############################################################################### import numpy as np from automodel import AutoModel from loopreader import LoopReader from loopmodel import LoopModel from loopgenerator import LoopGenerator from utils import build_reader_basename,montage ############################################################################### # PARAMETERS ###############################################################################
def main(): print(args.seed_model) if os.path.isfile(args.seed_model): print(f"=> Loading seed model from '{args.seed_model}'") checkpoint = torch.load( args.seed_model, map_location=f"cuda:{args.multigpu[0]}" if torch.cuda.is_available() else torch.device('cpu')) best_acc1 = checkpoint["best_acc1"] pretrained_dict = checkpoint["state_dict"] seed_args = checkpoint['args'] num_tasks_learned = checkpoint['args'].num_tasks args.num_seed_tasks_learned = num_tasks_learned else: raise Exception(f"=> No seed model found at '{args.seed_model}'!") assert not ( (args.num_tasks - args.num_seed_tasks_learned > 1) and (args.conv_type == 'BasisMaskConv') ), 'BasisMaskConv only supports learning one extra task over the mask tasks. Please fix config or change conv_type!' if seed_args.seed is not None: args.seed = seed_args.seed random.seed(int(seed_args.seed)) np.random.seed(int(seed_args.seed)) # Numpy module. torch.manual_seed(int(seed_args.seed)) torch.cuda.manual_seed(int(seed_args.seed)) torch.cuda.manual_seed_all(int( seed_args.seed)) # if you are using multi-GPU. torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True # Make the a directory corresponding to this run for saving results, checkpoints etc. i = 0 while True: # AT comments # getting PermissionError: [Errno 13] Permission denied: '/path' # run_base_dir = "./at_results" run_base_dir = pathlib.Path(f"{args.log_dir}/{args.name}~try={str(i)}") if not run_base_dir.exists(): os.makedirs(run_base_dir) args.name = args.name + f"~try={i}" break i += 1 (run_base_dir / "settings.txt").write_text(str(args)) (run_base_dir / "seed_settings.txt").write_text(str(seed_args)) args.run_base_dir = run_base_dir print(f"=> Saving data in {run_base_dir}") # Get model with correct architecture and load in state dict. model = utils.get_model() model = utils.set_gpu(model) print( f"=> Loaded seed model parameters from '{args.seed_model}' (num tasks: {checkpoint['args'].num_tasks}) (epochs: {checkpoint['epoch']})" ) if seed_args.er_sparsity: for n, m in model.named_modules(): if hasattr(m, "sparsity"): m.sparsity = min( 0.5, seed_args.sparsity * (m.weight.size(0) + m.weight.size(1)) / (m.weight.size(0) * m.weight.size(1) * m.weight.size(2) * m.weight.size(3)), ) print(f"Set sparsity of {n} to {m.sparsity}") model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } model_dict.update(pretrained_dict) model.load_state_dict(model_dict) #model.eval() # Get dataloader. data_loader = getattr(data, args.set)() # Track accuracy on all tasks. if args.num_tasks: best_acc1 = [0.0] * args.num_tasks curr_acc1 = [0.0] * args.num_tasks adapt_acc1 = [0.0] * args.num_tasks criterion = nn.CrossEntropyLoss().to(args.device) writer = SummaryWriter(log_dir=run_base_dir) trainer = getattr(trainers, args.trainer or "default") print(f"=> Using trainer {trainer}") train, test = trainer.train, trainer.test # Initialize model specific context (editorial note: avoids polluting main file) if hasattr(trainer, "init"): trainer.init(args) if args.task_eval is not None: assert 0 <= args.task_eval < args.num_tasks, "Not a valid task idx" print(f"Task {args.set}: {args.task_eval}") # Settting task to -1 tells the model to infer task identity instead of being given the task. model.apply(lambda m: setattr(m, "task", -1)) # an "adaptor" is used to infer task identity. # args.adaptor == gt implies we are in scenario GG. # This will cache all of the information the model needs for inferring task identity. if args.adaptor != "gt": utils.cache_masks(model) # Iterate through all tasks. adapt = getattr(adaptors, args.adaptor) # Update the data loader so it is returning data for the right task. data_loader.update_task(args.task_eval) # Clear the stored information -- memory leak happens if not. for p in model.parameters(): p.grad = None for b in model.buffers(): b.grad = None torch.cuda.empty_cache() adapt_acc = adapt( model, writer, data_loader.val_loader, num_tasks_learned, args.task_eval, ) torch.cuda.empty_cache() utils.write_adapt_results( name=args.name, task=f"{args.set}_{args.task_eval}", num_tasks_learned=num_tasks_learned, curr_acc1=0.0, adapt_acc1=adapt_acc, task_number=args.task_eval, ) utils.clear_masks(model) torch.cuda.empty_cache() return # Iterate through all new tasks that were not used for training masks. for idx in range( args.num_seed_tasks_learned if not args.train_mask_alphas else 0, args.num_tasks): print(f"Task {args.set}: {idx}") # Tell the model which task it is trying to solve -- in Scenario NNs this is ignored. model.apply(lambda m: setattr(m, "task", idx)) # Update the data loader so that it returns the data for the correct task, also done by passing the task index. assert hasattr( data_loader, "update_task" ), "[ERROR] Need to implement update task method for use with multitask experiments" data_loader.update_task(idx) # Clear the grad on all the parameters. for p in model.parameters(): p.grad = None # Make a list of the parameters relavent to this task. params = [] for n, p in model.named_parameters(): if args.conv_type == 'BasisMultitaskMaskConv': if p.requires_grad and int(n.split('.')[-1]) == idx: params.append(p) elif p.requires_grad: params.append(p) # train_weight_tasks specifies the number of tasks that the weights are trained for. # e.g. in SupSup, train_weight_tasks = 0. in BatchE, train_weight_tasks = 1. # If training weights, use train_weight_lr. Else use lr. lr = (args.train_weight_lr if args.train_weight_tasks < 0 or num_tasks_learned < args.train_weight_tasks else args.lr) # get optimizer, scheduler if args.optimizer == "adam": optimizer = optim.Adam(params, lr=lr, weight_decay=args.wd) elif args.optimizer == "rmsprop": optimizer = optim.RMSprop(params, lr=lr) else: optimizer = optim.SGD(params, lr=lr, momentum=args.momentum, weight_decay=args.wd) train_epochs = args.epochs if args.no_scheduler: scheduler = None else: scheduler = CosineAnnealingLR(optimizer, T_max=train_epochs) # Train on the current task. for epoch in range(1, train_epochs + 1): train(model, writer, data_loader.train_loader, optimizer, criterion, epoch, idx, data_loader, True) # Required for our PSP implementation, not used otherwise. utils.cache_weights(model, num_tasks_learned + 1) curr_acc1[idx] = test(model, writer, criterion, data_loader.val_loader, epoch, idx) if curr_acc1[idx] > best_acc1[idx]: best_acc1[idx] = curr_acc1[idx] if scheduler: scheduler.step() if (args.iter_lim > 0 and len(data_loader.train_loader) * epoch > args.iter_lim): break utils.write_result_to_csv( name=f"{args.name}~set={args.set}~task={idx}", curr_acc1=curr_acc1[idx], best_acc1=best_acc1[idx], save_dir=run_base_dir, ) # Save memory by deleting the optimizer and scheduler. del optimizer, scheduler, params # Increment the number of tasks learned. num_tasks_learned += 1 # If operating in NNS scenario, get the number of tasks learned count from the model. if args.trainer and "nns" in args.trainer: model.apply(lambda m: setattr( m, "num_tasks_learned", min(model.num_tasks_learned, args.num_tasks))) else: model.apply( lambda m: setattr(m, "num_tasks_learned", num_tasks_learned)) # Run inference on all the tasks. avg_acc = 0.0 avg_correct = 0.0 # Settting task to -1 tells the model to infer task identity instead of being given the task. model.apply(lambda m: setattr(m, "task", -1)) # an "adaptor" is used to infer task identity. # args.adaptor == gt implies we are in scenario GG. # This will cache all of the information the model needs for inferring task identity. if args.adaptor != "gt": utils.cache_masks(model) # Iterate through all tasks. adapt = getattr(adaptors, args.adaptor) for i in range(args.num_tasks): print(f"Testing {i}: {args.set} ({i})") # model.apply(lambda m: setattr(m, "task", i)) # Update the data loader so it is returning data for the right task. data_loader.update_task(i) # Clear the stored information -- memory leak happens if not. for p in model.parameters(): p.grad = None for b in model.buffers(): b.grad = None torch.cuda.empty_cache() adapt_acc = adapt( model, writer, data_loader.val_loader, num_tasks_learned, i, ) adapt_acc1[i] = adapt_acc avg_acc += adapt_acc torch.cuda.empty_cache() utils.write_adapt_results( name=args.name, task=f"{args.set}_{i}", num_tasks_learned=num_tasks_learned, curr_acc1=curr_acc1[i], adapt_acc1=adapt_acc, task_number=i, ) writer.add_scalar("adapt/avg_acc", avg_acc / num_tasks_learned, num_tasks_learned) utils.clear_masks(model) torch.cuda.empty_cache() if args.save: torch.save( { "epoch": args.epochs, "arch": args.model, "state_dict": model.state_dict(), "best_acc1": best_acc1, "curr_acc1": curr_acc1, "args": args, }, run_base_dir / "basis_final.pt", ) return adapt_acc1
) dloader_val = data_loader( dataset=dataset_val, nKnovel=opt.test_way, nKbase=0, nExemplars=opt.val_shot, # num training examples per novel category nTestNovel=opt.val_query * opt.test_way, # num test examples for all the novel categories nTestBase=0, # num test examples for all the base categories batch_size=1, num_workers=0, epoch_size=1 * opt.val_episode, # num of batches per epoch ) set_gpu(opt.gpu) check_dir('./experiments/') check_dir(opt.save_path) log_file_path = os.path.join(opt.save_path, "train_log.txt") log(log_file_path, str(vars(opt))) (embedding_net, cls_head) = get_model(opt) optimizer = torch.optim.SGD([{ 'params': embedding_net.parameters() }, { 'params': cls_head.parameters() }], lr=0.1, momentum=0.9,
from config.init_param import data_param, occlu_param, fap_param from config.parse_param import parse_param from model_structure.align_v1 import FaceAlignment from model_structure.occlu_detect import OcclusionDetection from ml import load_config from data_gen import load_imgs_labels from utils import get_filenames from utils import heat_map_compute from utils import load_basic_info from utils import logger from utils import set_gpu # load config normalizer, mean_shape = load_config() set_gpu(ratio=0.5) # parse parameter ap = argparse.ArgumentParser() ap.add_argument('-e1', '--epochs1', type=int, default=75, help='epochs1 of face alignment') ap.add_argument('-bs1', '--batch_size1', type=int, default=32, help='batch size of face alignment') ap.add_argument('-lr1', '--init_lr1',
from utils import pprint, set_gpu, count_acc, Averager, euclidean_metric from extensions import * if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--gpu', default='0') parser.add_argument('--load', default='./save/proto-1/max-acc.pth') parser.add_argument('--batch', type=int, default=2000) parser.add_argument('--way', type=int, default=5) parser.add_argument('--shot', type=int, default=1) parser.add_argument('--query', type=int, default=30) parser.add_argument('--folds', type=int, default=2) args = parser.parse_args() pprint(vars(args)) set_gpu(args.gpu) dataset = MiniImageNet('test') sampler = CategoriesSampler(dataset.label, args.batch, args.way, args.folds * args.shot + args.query) loader = DataLoader(dataset, batch_sampler=sampler, num_workers=8, pin_memory=True) model = Convnet().cuda() model.load_state_dict(torch.load(args.load)) model.eval() ave_acc = Averager() s_label = torch.arange(args.train_way).repeat(args.shot).view(args.shot * args.train_way) s_onehot = torch.zeros(s_label.size(0), 20) s_onehot = s_onehot.scatter_(1, s_label.unsqueeze(dim=1), 1).cuda()
def train(args): check_paths(args) dtype = set_gpu(args.cuda) np.random.seed(args.seed) torch.manual_seed(args.seed) # the dataset 'BSDS300' can be downloaded from the below link: # https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/bsds/ train_loader = CVDB_Y('BSDS300', batch_size=3, shuffle=True, crop_size=args.img_shape) sr = SR(scale_factor=args.scale_factor).cuda() sr.eval() H, HT = sr.Forward, sr.Backproj Pr = lambda x: HT(H(x)) Pn = lambda x: x - Pr(x) ddn = DDN(in_channels=1, out_channels=1, operator=sr, F='dncnn', G='unet', connection_type='cascade').type(dtype) optimizer = Adam([{ 'params': ddn.G.parameters(), 'lr': args.lr, 'weight_decay': args.reg_weight['G'] }, { 'params': ddn.F.parameters(), 'lr': args.lr }]) scheduler = torch.optim.lr_scheduler.MultiStepLR( optimizer, milestones=[args.epochs // 2], gamma=0.1) mse_loss = torch.nn.MSELoss().cuda() loss_epoch = 0 print('start training...') for e in range(args.epochs): ddn.train() loss_seq = [] for data in train_loader: x = data[0].type(dtype) if len(x.shape) == 5: x = x.view(-1, x.shape[-3], x.shape[-2], x.shape[-1]) # generate y y = H(x) # init noise n = torch.from_numpy( (np.random.normal(0, args.noise_sigam, y.shape))).type( dtype) # Add Gaussian noise without clipping # calculate the psudo-inverse backprojected reconstruction HTy HTy = HT(y + n).type(dtype) # DDN reconstruction x_hat, F, f, g = ddn(HTy, Pr, Pn) # calculate the loss loss = mse_loss(x_hat, x) + args.reg_weight['F'] * mse_loss(H(F), n) # update parameters (gradient descent) optimizer.zero_grad() loss.backward() optimizer.step() loss_seq.append(loss.item()) scheduler.step() loss_epoch = np.mean(loss_seq) print("==>Epoch {}\tloss_total: {:.6f}".format(e + 1, loss_epoch)) if args.checkpoint_model_dir is not None and ( e + 1) % args.checkpoint_interval == 0: ddn.eval() ckpt = { 'epoch': e + 1, 'total_loss': loss_epoch, 'net_state_dict': ddn.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), } torch.save( ckpt, os.path.join(args.checkpoint_model_dir, 'ckp_epoch_{}.pt'.format(e))) ddn.train() # save model ddn.eval() ckpt = { 'epoch': args.epochs, 'total_loss': loss_epoch, 'net_state_dict': ddn.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), } save_model_path = os.path.join(args.save_model_dir, args.filename + '.pt') torch.save(ckpt, save_model_path) print("\nTraining is Done.\tTrained model saved at {}".format( save_model_path))