def main(): # parse the arguments random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) if args.save_results: utils.saveargs(args, config_file) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) model, model_dict, evaluation = models.setup(checkpoints) print('Model:\n\t{model}\nTotal params:\n\t{npar:.2f}M'.format( model=args.model_type, npar=sum(p.numel() for p in model.parameters()) / 1000000.0)) #### get kernel information #### ndemog = args.ndemog ndemog = list(range(ndemog)) demog_combs = list(combinations(ndemog, 2)) #### get kernel information #### dist_list = get_att_dist(model) pdb.set_trace()
def main(): # parse the arguments args = parser.parse_args() random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) utils.saveargs(args) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) model, criterion = models.setup(checkpoints) # Data Loading dataloader = Dataloader(args) loaders = dataloader.create() # The trainer handles the training loop trainer = Trainer(args, model, criterion) # The trainer handles the evaluation on validation set tester = Tester(args, model, criterion) # start training !!! loss_best = 1e10 for epoch in range(args.nepochs): # train for a single epoch loss_train = trainer.train(epoch, loaders) loss_test = tester.test(epoch, loaders) if loss_best > loss_test: model_best = True loss_best = loss_test checkpoints.save(epoch, model, model_best)
def main(): # parse the arguments args = config.parse_args() if (args.ngpu > 0 and torch.cuda.is_available()): device = "cuda:0" else: device = "cpu" args.device = torch.device(device) random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) if args.save_results: utils.saveargs(args) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) model, criterion, evaluation = models.setup(checkpoints) print('Model:\n\t{model}\nTotal params:\n\t{npar:.2f}M'.format( model=args.model_type, npar=sum(p.numel() for p in model.parameters()) / 1000000.0)) # Data Loading dataloader = Dataloader(args) loaders = dataloader.create() # The trainer handles the training loop trainer = Trainer(args, model, criterion, evaluation) # The trainer handles the evaluation on validation set tester = Tester(args, model, criterion, evaluation) # start training !!! loss_best = 1e10 for epoch in range(args.nepochs): print('\nEpoch %d/%d\n' % (epoch + 1, args.nepochs)) # train for a single epoch loss_train = trainer.train(epoch, loaders) loss_test = tester.test(epoch, loaders) if loss_best > loss_test: model_best = True loss_best = loss_test if args.save_results: checkpoints.save(epoch, model, model_best)
def main(): # Parse the Arguments args = config.parse_args() random.seed(args.manual_seed) tf.set_random_seed(args.manual_seed) now = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S/') args.save = os.path.join(args.result_path, now, 'save') args.logs = os.path.join(args.result_path, now, 'logs') if args.save_results: utils.saveargs(args) # Initialize the Checkpoints Class checkpoints = Checkpoints(args) # Create Model models = Model(args) model, criterion, evaluation = models.setup(checkpoints) # Print Model Summary print('Model summary: {}'.format(model.name)) print(model.summary()) # Data Loading dataloader_obj = Dataloader(args) dataloader = dataloader_obj.create() # Initialize Trainer and Tester trainer = Trainer(args, model, criterion, evaluation) tester = Tester(args, model, criterion, evaluation) # Start Training !!! loss_best = 1e10 for epoch in range(args.nepochs): print('\nEpoch %d/%d' % (epoch + 1, args.nepochs)) # Train and Test for a Single Epoch loss_train = trainer.train(epoch, dataloader["train"]) loss_test = tester.test(epoch, dataloader["test"]) if loss_best > loss_test: model_best = True loss_best = loss_test if args.save_results: checkpoints.save(epoch, model, model_best)
from checkpoints import Checkpoints from evaluation import Evaluate from generation import Generator import os import datetime import utils import copy import config # parse the arguments args = config.parse_args() random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) args.save = os.path.join(args.result_path, 'save') args.logs = os.path.join(args.result_path, 'logs') utils.saveargs(args) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) gogan_model, criterion = models.setup(checkpoints) netD = gogan_model[0] netG = gogan_model[1] netE = gogan_model[2] if args.netD is not '': checkpointD = checkpoints.load(args.netD) netD.load_state_dict(checkpointD) if args.netG is not '':
def main(): # load pop for extrapolation pop = pickle.load(open("Results/CIFAR10_baseline/Run5/pop_extra.pkl", "rb")) for i in range(len(pop)): genome = pop[i].genome # parse the arguments args = parser.parse_args() args.save = os.path.join("Extrapolation_results", "Model_ID_{}".format(pop[i].id)) random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) utils.saveargs(args) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model # genome = [[[0], [0, 0], [0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0, 0], [0]], # [[0], [0, 0], [0, 1, 0], [0, 1, 0, 1], [0, 1, 1, 1, 1], [0]], # [[1], [0, 0], [0, 0, 0], [0, 1, 1, 1], [0, 0, 0, 0, 1], [1]]] # genome = [[[0], [0, 1], [1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1, 1], [0]], # [[0], [0, 1], [0, 0, 0], [0, 0, 1, 1], [1, 1, 1, 1, 1], [0]], # [[0], [0, 0], [0, 0, 1], [1, 1, 0, 1], [1, 1, 0, 1, 1], [1]]] models = Model(args, genome) model, criterion, num_params = models.setup() model = calculate_flops.add_flops_counting_methods(model) # print(model) # Data Loading dataloader = Dataloader(args) loaders = dataloader.create() # The trainer handles the training loop trainer = Trainer(args, model, criterion) # The trainer handles the evaluation on validation set tester = Tester(args, model, criterion) # start training !!! loss_best = 1e10 acc_test_list = [] acc_best = 0 for epoch in range(args.nepochs): # train for a single epoch start_time_epoch = time.time() if epoch == 0: model.start_flops_count() loss_train, acc_train = trainer.train(epoch, loaders) loss_test, acc_test = tester.test(epoch, loaders) acc_test_list.append(acc_test) if acc_test > acc_best: model_best = True # update the best test accu found so found acc_best = acc_test loss_best = loss_test checkpoints.save(epoch, model, model_best) time_elapsed = np.round((time.time() - start_time_epoch), 2) if epoch == 0: n_flops = (model.compute_average_flops_cost() / 1e6 / 2) if np.isnan(np.average(loss_train)): break print( "Epoch {:d}:, test error={:0.2f}, FLOPs={:0.2f}M, n_params={:0.2f}M, {:0.2f} sec" .format(epoch, 100.0 - acc_test, n_flops, num_params / 1e6, time_elapsed)) # save the final model parameter # torch.save(model.state_dict(), # "model_file/model%d.pth" % int(args.genome_id - 1)) pop[i].fitness[0] = acc_best pop[i].fitness[1] = n_flops pop[i].n_params = num_params # error = 100 - np.mean(acc_test_list[-3:]) # accuracy = acc_best # fitness = [acc_best, n_flops, num_params] # with open("output_file/output%d.pkl" % int(args.genome_id - 1), "wb") as f: # pickle.dump(fitness, f) with open("Results/CIFAR10_baseline/Run5/pop_extra_evaluated.pkl", "wb") as f: pickle.dump(pop, f)
def main(): # parse the arguments random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) if args.save_results: utils.saveargs(args, config_file) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) model_dict, evaluation = models.setup(checkpoints) print('Model:\n\t{model}\nTotal params:\n\t{npar:.4f}M'.format( model=args.model_type, npar=sum(p.numel() for p in model_dict['model'].parameters()) / 1000000.0)) #### get kernel information #### ndemog = args.ndemog ndemog = list(range(ndemog)) demog_combs = list(combinations(ndemog, 2)) #### get kernel information #### #### create writer for tensor boader #### if args.save_results: writer = SummaryWriter(args.tblog_dir) else: writer = None #### create writer for tensor boader #### # The trainer handles the training loop trainer = Trainer(args, model_dict['model'], model_dict['loss'], model_dict['optimizer'], writer) # The trainer handles the evaluation on validation set tester = Tester(args, model_dict['model'], evaluation, writer) test_freq = 1 dataloader = Dataloader(args) if args.extract_feat: loaders = dataloader.create(flag='Test') tester.extract_features(loaders, 1) elif args.just_test: loaders = dataloader.create(flag='Test') acc_test,acc_mean = tester.test(loaders, 1) print(acc_test, acc_mean) else: loaders = dataloader.create() if args.dataset_train == 'ClassSamplesDataLoader': loaders['train'] = dataloader.dataset_train # start training !!! acc_best = 0 loss_best = 999 stored_models = {} for epoch in range(args.nepochs-args.epoch_number): epoch += args.epoch_number print('\nEpoch %d/%d\n' % (epoch + 1, args.nepochs)) # train for a single epoch loss_train = trainer.train(loaders, epoch) acc_test=0 if float(epoch) % test_freq == 0: acc_test,acc_mean = tester.test(loaders, epoch) if loss_best > loss_train: loss_best = loss_train acc_best = acc_test if float(epoch) % test_freq == 0 and args.save_results: stored_models['model'] = trainer.model stored_models['loss'] = trainer.criterion stored_models['optimizer'] = trainer.optimizer checkpoints.save(acc_test, stored_models, epoch) if epoch == args.fuse_epoch: update_kernels(args, trainer.model, demog_combs, ndemog) if args.save_results: writer.close()
def main(): # parse the arguments args = config.parse_args() random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) args.save = os.path.join(args.result_path, 'save') args.logs = os.path.join(args.result_path, 'logs') utils.saveargs(args) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) rankgan_model, criterion = models.setup(checkpoints) modelD = rankgan_model[0] modelG = rankgan_model[1] Encoder = rankgan_model[2] prevD, prevG = None, None if args.netD is not '': checkpointD = checkpoints.load(args.netD) modelD.load_state_dict(checkpointD) if args.netG is not '': checkpointG = checkpoints.load(args.netG) modelG.load_state_dict(checkpointG) if args.netE is not '': checkpointE = checkpoints.load(args.netE) Encoder.load_state_dict(checkpointE) if args.prevD is not '': prevD = copy.deepcopy(modelD) checkpointDprev = checkpoints.load(args.prevD) prevD.load_state_dict(checkpointDprev) if args.prevG is not '': prevG = copy.deepcopy(modelG) checkpointGprev = checkpoints.load(args.prevG) prevG.load_state_dict(checkpointGprev) # Data Loading dataloader = Dataloader(args) loader_train = dataloader.create(flag="Train") loader_test = dataloader.create(flag="Test") # The trainer handles the training loop and evaluation on validation set trainer = Trainer(args, modelD, modelG, Encoder, criterion, prevD, prevG) # start training !!! num_stages = args.num_stages stage_epochs = args.stage_epochs for stage in range(args.start_stage, num_stages): # check whether ready to start new stage and if not, optimize discriminator # if stage > 2: # print("Optimizing Discriminator") # trainer.setup_stage(stage, loader_test) # opt_disc_flag = True # epoch = 0 # while opt_disc_flag: # opt_disc_flag = trainer.optimize_discriminator(stage-1, epoch, loader_train) # epoch += 1 # setup trainer for the stage trainer.setup_stage(stage, loader_test) print("Training for Stage {}".format(stage)) for epoch in range(stage_epochs[stage]): # train for a single epoch # cur_time = time.time() # if stage == 2: loss_train = trainer.train(stage, epoch, loader_train) # if stage > 0: # disc_acc = trainer.test(stage, epoch, loader_test) # print("Time taken = {}".format(time.time() - cur_time)) try: torch.save(modelD.state_dict(), '%s/stage_%d_netD.pth' % (args.save, stage)) torch.save(modelG.state_dict(), '%s/stage_%d_netG.pth' % (args.save, stage)) torch.save(Encoder.state_dict(), '%s/stage_%d_netE.pth' % (args.save, stage)) except Exception as e: print(e)
def main(): # parse the arguments random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) if args.save_results: utils.saveargs(args, config_file) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) model, model_dict, evaluation = models.setup(checkpoints) print('Model:\n\t{model}\nTotal params:\n\t{npar:.2f}M'.format( model=args.model_type, npar=sum(p.numel() for p in model['feat'].parameters()) / 1000000.0)) # The trainer handles the training loop trainer = Trainer(args, model, model_dict['loss'], evaluation) # The trainer handles the evaluation on validation set tester = Tester(args, model, model_dict['loss'], evaluation) test_freq = 1 dataloader = Dataloader(args) if args.extract_feat: loaders = dataloader.create(flag='Test') tester.extract_features(loaders) # tester.extract_features_h5py(loaders, len(dataloader.dataset_test)) elif args.just_test: loaders = dataloader.create(flag='Test') acc_test = tester.test(args.epoch_number, loaders) print(acc_test) else: loaders = dataloader.create() if args.dataset_train == 'ClassSamplesDataLoader': loaders['train'] = dataloader.dataset_train # start training !!! acc_best = 0 loss_best = 999 stored_models = {} for epoch in range(args.nepochs - args.epoch_number): epoch += args.epoch_number print('\nEpoch %d/%d\n' % (epoch + 1, args.nepochs)) # train for a single epoch # loss_train = 3.0 loss_train = trainer.train(epoch, loaders, checkpoints, acc_best) if float(epoch) % test_freq == 0: acc_test = tester.test(epoch, loaders) if loss_best > loss_train: model_best = True loss_best = loss_train acc_best = acc_test if args.save_results: stored_models['model'] = model stored_models['loss'] = trainer.criterion checkpoints.save(acc_best, stored_models, epoch, 'final', model_best)
def main(): # parse the arguments args = config.parse_args() random.seed(args.manual_seed) torch.manual_seed(args.manual_seed) args.save = os.path.join(args.result_path, 'save') args.logs = os.path.join(args.result_path, 'logs') utils.saveargs(args) # initialize the checkpoint class checkpoints = Checkpoints(args) # Create Model models = Model(args) rankgan_model, criterion = models.setup(checkpoints) modelD = rankgan_model[0] modelG = rankgan_model[1] Encoder = rankgan_model[2] prevD, prevG = None, None if args.netD is not '': checkpointD = checkpoints.load(args.netD) modelD.load_state_dict(checkpointD) if args.netG is not '': checkpointG = checkpoints.load(args.netG) modelG.load_state_dict(checkpointG) if args.netE is not '': checkpointE = checkpoints.load(args.netE) Encoder.load_state_dict(checkpointE) if args.prevD is not '': prevD = copy.deepcopy(modelD) checkpointDprev = checkpoints.load(args.prevD) prevD.load_state_dict(checkpointDprev) if args.prevG is not '': prevG = copy.deepcopy(modelG) checkpointGprev = checkpoints.load(args.prevG) prevG.load_state_dict(checkpointGprev) # Data Loading dataloader = Dataloader(args) loader_train = dataloader.create(flag="Train") loader_test = dataloader.create(flag="Test") # The trainer handles the training loop and evaluation on validation set trainer = Trainer(args, modelD, modelG, Encoder, criterion, prevD, prevG) for epoch in range(args.nepochs): # train for a single epoch # cur_time = time.time() # if stage == 2: loss_train = trainer.train(epoch, loader_train) # if stage > 0: # disc_acc = trainer.test(stage, epoch, loader_test) # print("Time taken = {}".format(time.time() - cur_time)) try: torch.save(modelD.state_dict(), '%s/netD.pth' % (args.save, stage)) for i in range(args.nranks - 1): torch.save(modelG.state_dict(), '%s/order_%d_netG.pth' % (i + 1, stage)) except Exception as e: print(e)