示例#1
0
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)
示例#2
0
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)
示例#3
0
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)
示例#4
0
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)
示例#5
0
文件: main.py 项目: thakurab/Stat517
print("\t\tBatches (Test):\t", len(loader_test))

# The trainer handles the training loop and evaluation on validation set
trainer = Trainer(args, model, criterion)

if args.oneshot == 0:
    # start training !!!
    loss_best = [1e10 for _ in model]
    epoch = 0
    while True:
        # train for a single epoch
        loss_train = trainer.train(epoch, loader_train)
        loss_test = trainer.test(epoch, loader_test)

        if (epoch % 500 == 0):
            checkpoints.save(epoch, model)

        if any(test < best
               for test, best in zip(loss_test.values(), loss_best)):
            model_best = True
            loss_best = loss_test.values()
            best_epoch = epoch
            checkpoints.keep(epoch, model)

        if (epoch > 0) and (epoch % args.save_steps == 0):
            checkpoints.save()

        if epoch == args.nepochs:
            break
        epoch += 1
    checkpoints.save()
示例#6
0
文件: main.py 项目: gongsixue/GAC
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()
示例#7
0
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)
loader_train, loader_test = dataloader.create()

# The trainer handles the training loop and evaluation on validation set
trainer = Trainer(args, model, criterion)

# start training !!!
acc_best = 0
for epoch in range(args.nepochs):

    # train for a single epoch
    acc_train = trainer.train(epoch, loader_train)
    acc_test = trainer.test(epoch, loader_test)

    if acc_best < acc_test:
        model_best = True
        acc_best = acc_test
        checkpoints.save(epoch, model, model_best)
示例#8
0
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)