コード例 #1
0
ファイル: main.py プロジェクト: PaddlePaddle/PaddleHelix
def main():
    root_dir = '.'
    args = get_args(root_dir)
    paddle.set_device('gpu:' + str(args.gpu_id))

    model = ContextAwareRelationNet(args)
    count_model_params(model)
    trainer = Meta_Trainer(args, model)

    t1 = time()
    print('Initial Evaluation')
    best_avg_auc = 0
    for epoch in range(1, args.epochs + 1):
        print('----------------- Epoch:', epoch, ' -----------------')
        trainer.train_step()

        if epoch % args.eval_steps == 0 or epoch == 1 or epoch == args.epochs:
            print('Evaluation on epoch', epoch)
            best_avg_auc = trainer.test_step()

        if epoch % args.save_steps == 0:
            trainer.save_model()
        print('Time cost (min):', round((time() - t1) / 60, 3))
        t1 = time()

    trainer.conclude()

    if args.save_logs:
        trainer.save_result_log()

    print('Train done.')
    print('Best Avg AUC:', best_avg_auc)
コード例 #2
0
def main():
    # Load parameters
    params = get_args()

    mouse_prec = params['mouse_prec']
    mouse_speed = params['mouse_speed']
    mouse = MouseController(mouse_prec, mouse_speed)
    models = load_models(params)

    # Load input feed
    input_type = params['input_type']
    if input_type=='cam':
        input_file = None
    else:
        input_file = params['input_file_path']

    feed=InputFeeder(input_type=input_type, input_file=input_file)
    feed.load_data()
    for batch in feed.next_batch():
        if batch is not None:
            image, pos = main_loop(batch, models)
            cv2.imshow('frame', image)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            mouse.move(pos[0], pos[1])
            # break
        else:
            break
    feed.close()
コード例 #3
0
ファイル: mytodo.py プロジェクト: Vl330/MyToDo
def main():
    args = parser.get_args()
    path = args["path"]
    if not os.path.exists(path):
        print("File does not exist")
        sys.exit(1)
    todos = utils.load_todos(path)
    utils.get_action(args, todos)
コード例 #4
0
ファイル: main.py プロジェクト: peter-lukac/RawNet
def main():
    # parse arguments
    args = get_args()

    # make experiment reproducible
    # Possible slower training
    if args.reproducible:
        torch.manual_seed(args.seed)
        np.random.seed(args.seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

    # DDP setting
    # Single node, single&multi GPU environment is covered
    args.ngpus_per_node = torch.cuda.device_count()
    args.bs = args.bs // args.ngpus_per_node
    args.world_size = args.ngpus_per_node
    args.rank = 0
    mp.spawn(main_worker,
             nprocs=args.ngpus_per_node,
             args=(args.ngpus_per_node, args))
コード例 #5
0
def main():

    # Parse arguments from command line
    args = parser.get_args()
    config = ConfigParser.ConfigParser()

    if args.keywords is None or len(args.keywords) == 0:
        raise argparse.ArgumentTypeError("No keywords!")

    # Read in from Config file
    config.read(CONFIG)
    consumer_key = config.get("keys", "ConsumerKey")
    consumer_secret = config.get("keys", "ConsumerSecret")
    access_token = config.get("keys", "AccessToken")
    access_secret = config.get("keys", "AccessSecret")

    # Verify credentials
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)

    # Collect tweets based on arguments (or default vals)
    tweets = []
    streamListener = stream_listener(tweets, args.max_tweets)
    stream = tweepy.Stream(auth=auth,
                           listener=streamListener,
                           timeout=args.timeout * 60)

    print "Collecting tweets..."
    stream.filter(track=args.keywords)

    tweet_text = ""

    for twt in tweets:
        tweet_text += twt.clean_text() + ". "

    markov = markov_generator.markov_text_generator(tweet_text,
                                                    args.state_size)
    sentence = markov.generate_markov_text(args.max_words)
    print sentence
コード例 #6
0
def main():
    args = get_args()

    dataset = CSVDataset(args.csv_file, args.root_dir, args.cols,
                         args.preprocess_fn, args.state, args.next_state,
                         args.reward, args.info, args.done)

    agent = ValueAgentDataset(args.network, dataset, args.batch_size,
                              args.gamma, args.device, args.optimizer, args.lr,
                              args.hidden_dim, args.criterion, args.update)

    summary = SummaryWriter(args.summary_dir)
    #summary.add_hparams()

    num_iter = 0
    for epoch in range(args.num_epochs):

        value_loss, value_mean, value_std = agent.train()
        summary.add_scalar('Loss/Value', value_loss, num_iter)
        summary.add_scalar('Stats/Value Mean', value_mean, num_iter)
        summary.add_scalar('Stats/Value Std', value_std, num_iter)
        num_iter += 1
    summary.close()
コード例 #7
0
def main():
    args = parser.get_args()

    file_contents = None
    if args.method == "write":
        # TODO: this is dangerous if the file is too large
        #       Read through file based off system memory
        with open(args.filepath, 'rb') as f:
            file_contents = f.read()

    filename = args.filename if args.filename else getFilename(args.filepath)

    if not args.user:
        raise argparse.ArgumentTypeError("Missing user!")

    if (not args.secret_key or not filename) and args.method != "list-all":
        raise argparse.ArgumentTypeError(
            "Missing secret key or filename or filepath")

    http_args = {
        "method": args.method,
        "decentralized-db-user": args.user,
        "secret-key": args.secret_key,
        "filename": filename,
        "file": file_contents
    }

    resp = requests.get(args.endpoint, http_args)
    if resp.status_code != requests.codes.ok:
        resp.raise_for_status()

    json_data = resp.json()

    if args.method == "read":
        json_data['filename'] = filename

    output.format_output(json_data)
コード例 #8
0
def main():
    #parse arguments 
    args = get_args()

    #device setting
    cuda = torch.cuda.is_available()
    device = torch.device('cuda' if cuda else 'cpu')

    #strictly reproducible, with potential speed loss as a trade-off!
    if args.reproducible:
        np.random.seed(args.seed)
        torch.manual_seed(args.seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

    #pre-process DB for first time
    #get DB list: SED
    if 'SED' in args.task:
        if not os.path.exists(args.DB_SED+'log_mel_spec_label/'):
            lines_SED = get_utt_list(args.DB_SED+args.wav_SED)
            from preprocess import extract_log_mel_spec_sed
            print(extract_log_mel_spec_sed(lines_SED, args))

        lines_SED = get_utt_list(args.DB_SED+'log_mel_spec_label/', ext='h5')
        trn_lines_SED, evl_lines_SED = split_dcase2020_sed(lines_SED)
        if args.verbose > 0:
            print('SED DB statistics')
            print('# tot samp: {}\n'\
              '# trn samp: {}\n'\
              '# evl samp: {}\n'.format(len(lines_SED), len(trn_lines_SED), len(evl_lines_SED)))
            print(sed_labels)
            print(sed_label2idx)
            print(sed_idx2label)
        del lines_SED

    #get DB list: ASC
    if 'ASC' in args.task:
        lines_ASC = get_utt_list(args.DB_ASC+args.wav_ASC)

        if os.path.exists(args.DB_ASC+args.meta_scp):
            d_label_ASC, l_label_ASC = pk.load(open(args.DB_ASC+args.d_label_ASC, 'rb'))
        else:
            with open(args.DB_ASC+args.meta_scp) as f:
                l_meta_ASC = f.readlines()
            d_label_ASC, l_label_ASC = make_d_label(l_meta_ASC[1:])
            pk.dump([d_label_ASC, l_label_ASC], open(args.DB_ASC+args.d_label_ASC, 'wb'))

        trn_lines_ASC = split_dcase2020_fold_strict(fold_scp = args.DB_ASC+args.fold_trn, lines = lines_ASC)
        evl_lines_ASC = split_dcase2020_fold_strict(fold_scp = args.DB_ASC+args.fold_evl, lines = lines_ASC)
        if args.verbose > 0 :
            print('ASC DB statistics')
            print('# trn samp: {}\n# evl samp: {}'.format(len(trn_lines_ASC), len(evl_lines_ASC)))
            print(d_label_ASC)
            print(l_label_ASC)

    #get DB list: Audio tagging
    if 'TAG' in args.task:
        df_TAG = pd.read_csv(args.DB_TAG+'train_curated.csv')
        tmp_df = pd.read_csv(args.DB_TAG+'sample_submission.csv')
        l_label_TAG = tmp_df.columns[1:].tolist()  #get 80 audio tagging labels as list
        del tmp_df
    
        for l in l_label_TAG:
            df_TAG[l] = df_TAG['labels'].apply(lambda x: l in x)
        df_TAG['path'] = args.DB_TAG + 'train_curated/' + df_TAG['fname']
        # all arguments must be fixed for reproducing original 
        # fold configuration reported in Akiyams et al.'s paper.
        trn_idx_TAG, evl_idx_TAG = list(KFold(
            n_splits=5,
            shuffle=True,
            random_state=42).split(np.arange(len(df_TAG))))[0]
        df_trn_TAG = df_TAG.iloc[trn_idx_TAG].reset_index(drop=True)
        df_evl_TAG = df_TAG.iloc[evl_idx_TAG].reset_index(drop=True)
        del df_TAG
    
        if args.verbose > 0:
            print('Audio tagging DB statistics')
            print('# trn samp: {}\n# evl samp: {}'.format(len(df_trn_TAG.fname), len(df_evl_TAG.fname)))
            print(l_label_TAG)
    

    #####
    #define dataset generators
    #####
    #SED
    if 'SED' in args.task:
        largs = {'trn_lines': trn_lines_SED,
                 'evl_lines': evl_lines_SED
        }
        trnset_gen_SED, evlset_gen_SED = get_loaders_SED(largs, args)
        trnset_gen_SED_itr = cycle(trnset_gen_SED)
    else:
        trnset_gen_SED_itr = None

    #ASC
    if 'ASC' in args.task:
        largs = {
            'trn_lines': trn_lines_ASC,
            'evl_lines': evl_lines_ASC,
            'd_label': d_label_ASC,
        }
        trnset_gen_ASC, evlset_gen_ASC = get_loaders_ASC(largs, args)
        trnset_gen_ASC_itr = cycle(trnset_gen_ASC)
    else:
        trnset_gen_ASC_itr = None

    #TAG
    if 'TAG' in args.task:
        largs = {
            'trn': df_trn_TAG,
            'evl': df_evl_TAG,
            'l_label': l_label_TAG
        }
        trnset_gen_TAG, evlset_gen_TAG = get_loaders_TAG(largs, args)
        trnset_gen_TAG_itr = cycle(trnset_gen_TAG)
    else:
        trnset_gen_TAG_itr = None 

    #set save directory
    save_dir = args.save_dir+args.name+'/'
    if not os.path.exists(save_dir): os.makedirs(save_dir)
    if not os.path.exists(save_dir+'results/'): os.makedirs(save_dir+'results/')
    if not os.path.exists(save_dir+'weights/'): os.makedirs(save_dir+'weights/')
    
    #log parameters to local and comet_ml server
    f_params = open(save_dir+'f_params.txt', 'w')
    for k, v in sorted(vars(args).items()):
        if args.verbose > 0: print(k, v)
        f_params.write('{}:\t{}\n'.format(k, v))
    f_params.close()

    #define model
    module = importlib.import_module('models.{}'.format(args.model_scp))
    _model = getattr(module, args.model_name)
    model = _model(**args.model)
    model_summ = summary(model, (1, 128, 251), mode = args.task)
    nb_params = sum([param.view(-1).size()[0] for param in model.parameters()])
    if args.verbose >0: print('nb_params: %d'%nb_params)
    with open(save_dir+'modelsumm.txt', 'w') as f: f.write(str(model_summ))

    #load weights
    if 'fine-tune' in args.name:
        pre_trained_model = (
            'Joint/weights/best_ASC.pt'     if 'ASC' in args.task else
            'Joint/weights/best_lwlrap.pt'  if 'TAG' in args.task else
            'Joint/weights/best_SED.pt'     if 'SED' in args.task else None
        )
        model.load_state_dict(torch.load(args.save_dir+pre_trained_model))
    elif 'Eval' in args.name:
        pre_trained_model = (
            'fine-tuneASC/weights/best_ASC.pt'      if 'ASC' in args.task else
            'fine-tuneTAG/weights/best_lwlrap.pt'   if 'TAG' in args.task else
            'fine-tuneSED/weights/best_SED.pt'      if 'SED' in args.task else None
        )
        model.load_state_dict(torch.load(args.save_dir+pre_trained_model))
    model = model.to(device)
    
    if 'Eval' in args.name:
        if 'ASC' in args.task:
            acc, conf_mat = evaluate_ASC(model = model,
                evlset_gen = evlset_gen_ASC,
                device = device,
                args = args,
            )
            print('ASC acc:\t{}'.format(acc))

        if 'SED' in args.task:
            er, f1 = evaluate_SED(model = model,
                evlset_gen = evlset_gen_SED,
                device = device,
                args = args,
            )
            print('ER:{}\tF1:{}\t'.format(er, f1))

        if 'TAG' in args.task:
            lwlrap = evaluate_TAG(model = model,
                evlset_gen = evlset_gen_TAG,
                device = device,
                args = args
            )
            print('Lwlrap:{}\t'.format(lwlrap))
    else:
        #set ojbective funtions
        criterion = {
            'bce_SED': binary_cross_entropy,
            'cce_ASC': nn.CrossEntropyLoss().cuda(),
            'bce_TAG': nn.BCEWithLogitsLoss().cuda()
        }

        #set optimizer
        params = list(model.parameters())
        if args.optimizer.lower() == 'sgd':
            optimizer = torch.optim.SGD(params,
                lr = args.lr,
                momentum = args.opt_mom,
                weight_decay = args.wd,
                nesterov = args.nesterov)
        elif args.optimizer.lower() == 'adam':
            optimizer = torch.optim.Adam(model.parameters(),
                lr = args.lr,
                weight_decay = args.wd,
                amsgrad = args.amsgrad)
        else:
            raise NotImplementedError('Optimizer not implemented, got:{}'.format(args.optimizer))

        #set learning rate decay
        if bool(args.do_lr_decay):
            if args.lr_decay == 'cosine':
                lr_scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=args.nb_iter_per_epoch * args.lrdec_t0, eta_min=0.000001)
            else:
                raise NotImplementedError('Not implemented yet')
        else:
            lr_scheduler = None

        f_eval = open(save_dir + 'eval_results.txt', 'a', buffering=1)
        metric_man = metric_manager(
            task=args.task,
            save_dir=save_dir+'weights/',
            model=model,
            save_best_only=args.save_best_only
        ) 

        for epoch in tqdm(range(args.epoch), ncols=100):
            train_joint_3task(
                model=model,
                args=args,
                trnset_gen_ASC=trnset_gen_ASC_itr,
                trnset_gen_SED=trnset_gen_SED_itr,
                trnset_gen_TAG=trnset_gen_TAG_itr,
                epoch=epoch,
                device=device,
                criterion=criterion,
                optimizer=optimizer,
                lr_scheduler=lr_scheduler
            )
            description = 'Epoch{}:\t'.format(epoch)

            if 'ASC' in args.task:
                acc, conf_mat = evaluate_ASC(
                    model=model,
                    evlset_gen=evlset_gen_ASC,
                    device=device,
                    args=args,
                )
                description += 'Acc:{}\t'.format(acc)
                metric_man.update_ASC(epoch=epoch, acc=acc, conf_mat=conf_mat, l_label=l_label_ASC)

            if 'SED' in args.task:
                er, f1 = evaluate_SED(
                    model=model,
                    evlset_gen=evlset_gen_SED,
                    device=device,
                    args=args,
                )
                description += 'ER:{}\tF1:{}\t'.format(er, f1)
                metric_man.update_SED(epoch=epoch, er=er, f1=f1)

            if 'TAG' in args.task:
                lwlrap = evaluate_TAG(
                    model=model,
                    evlset_gen=evlset_gen_TAG,
                    device=device,
                    args=args
                )
                description += 'Lwlrap:{}\t'.format(lwlrap)
                metric_man.update_TAG(epoch=epoch, lwlrap=lwlrap)
            
            f_eval.write(description+'\n')
        f_eval.close()
コード例 #9
0
# coding: UTF-8
import sys
import os
import utils
import store
import parser

if __name__ == '__main__':
    args = parser.get_args()
    cls = args.func
    must_reload = []
    user = os.environ["MF_ID"]
    password = os.environ["MF_PASSWORD"]
    driver = utils.login(user, password,
            force_reload=(cls in must_reload))
    utils.set_group(driver, args.group)
    result = getattr(utils, cls)(driver, args)
    driver.quit()
    store.persist(result, cls)
    sys.exit()
コード例 #10
0
ファイル: ET_h5_converter.py プロジェクト: icamplis/ET_to_h5
                    else:
                        times_df.to_hdf(separate_output, session_ID, mode='a')

                    saccs_df.to_hdf(separate_output,
                                    session_ID + '_saccades',
                                    mode='a')
                    fixes_df.to_hdf(separate_output,
                                    session_ID + '_fixations',
                                    mode='a')
                    blinks_df.to_hdf(separate_output,
                                     session_ID + '_blinks',
                                     mode='a')

                if combined:
                    combined_df = create_combined_df(times_df, saccs_df,
                                                     fixes_df, blinks_df)
                    if first_combined_entry:
                        combined_df.to_hdf(combined_output,
                                           session_ID + '_combined',
                                           mode='w')
                        first_combined_entry = False
                    else:
                        combined_df.to_hdf(combined_output,
                                           session_ID + '_combined',
                                           mode='a')


if __name__ == "__main__":
    args = get_args()
    convert(args.path, args.combined, args.separate, args.BIDS)
コード例 #11
0
def main():
    args = parser.get_args()

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    # assert torch.cuda.is_available(), "No GPU/CUDA is detected!"

    assert args.do_train or args.do_eval, \
     "Either do_train or do_eval has to be True!"
    assert not(args.do_train and args.do_eval), \
     "do_train and do_eval cannot be both True!"

    # To track results from different commits (temporary)
    if args.commit_sha == None:
        args.run_id = args.run_id + '_' + str(sha)
    else:
        args.run_id = args.run_id + '_' + args.commit_sha

    if not args.run_id == None:
        args.output_dir = os.path.join(args.output_dir, args.run_id)
    if not (os.path.exists(args.output_dir)) and args.do_train:
        os.makedirs(args.output_dir)
    if args.do_eval:
        # output_dir has to exist if doing evaluation
        assert os.path.exists(args.output_dir), \
         "Output directory {} doesn't exist!".format(args.output_dir)
        # if args.data_split_mode=='testing':
        # 	# Checkpoint has to exist if doing evaluation with testing split
        # 	assert os.path.exists(args.checkpoint_path), \
        # 		"Checkpoint doesn't exist!"
    '''
	Configure a log file
	'''
    if args.do_train:
        log_path = os.path.join(args.output_dir, 'training.log')
    if args.do_eval:
        log_path = os.path.join(args.output_dir, 'evaluation.log')
    logging.basicConfig(filename=log_path,
                        level=logging.INFO,
                        filemode='w',
                        format='%(asctime)s - %(name)s %(message)s',
                        datefmt='%m-%d %H:%M')
    '''
	Log important info
	'''
    logger = logging.getLogger(__name__)
    logger.info("***** Code info *****")
    logger.info("  Git commit sha: %s", sha)
    '''
	Print important info
	'''
    print('Model architecture:', args.model_architecture)
    print('Training folds:', args.training_folds)
    print('Evaluation folds:', args.evaluation_folds)
    print('Device being used:', device)
    print('Output directory:', args.output_dir)
    print('Logging in:\t {}'.format(log_path))
    print('Input image formet:', args.image_format)
    print('Loss function: {}'.format(args.loss))

    if args.do_inference:
        '''
		Create an instance of a resnet model and load a checkpoint
		'''
        output_channels = 4
        if args.model_architecture == 'resnet7_2_1':
            resnet_model = resnet7_2_1(
                pretrained=True,
                pretrained_model_path=args.checkpoint_path,
                output_channels=output_channels)
        resnet_model = resnet_model.to(device)
        '''
		Load the input image
		'''
        image = load_image(args.image_path)
        '''
		Run model inference on the image
		'''
        pred = main_utils.inference(resnet_model, image)
        pred = pred[0]
        severity = sum([i * pred[i] for i in range(len(pred))])

        print(f"{args.image_path} has severity of {severity}")

        return

    if args.do_train:
        '''
		Create tensorboard and checkpoint directories if they don't exist
		'''
        args.tsbd_dir = os.path.join(args.output_dir, 'tsbd')
        args.checkpoints_dir = os.path.join(args.output_dir, 'checkpoints')
        directories = [args.tsbd_dir, args.checkpoints_dir]
        for directory in directories:
            if not (os.path.exists(directory)):
                os.makedirs(directory)
        # Avoid overwriting previous tensorboard and checkpoint data
        args.tsbd_dir = os.path.join(
            args.tsbd_dir, 'tsbd_{}'.format(len(os.listdir(args.tsbd_dir))))
        if not os.path.exists(args.tsbd_dir):
            os.makedirs(args.tsbd_dir)
        args.checkpoints_dir = os.path.join(
            args.checkpoints_dir,
            'checkpoints_{}'.format(len(os.listdir(args.checkpoints_dir))))
        if not os.path.exists(args.checkpoints_dir):
            os.makedirs(args.checkpoints_dir)
        '''
		Create an instance of a resnet model
		'''
        output_channels = 4
        if args.model_architecture == 'resnet7_2_1':
            resnet_model = resnet7_2_1(output_channels=output_channels)
        resnet_model = resnet_model.to(device)
        '''
		Train the model
		'''
        print("***** Training the model *****")
        main_utils.train(args, device, resnet_model)
        print("***** Finished training *****")

    if args.do_eval:

        def run_eval_on_checkpoint(checkpoint_path):
            '''
			Create an instance of a resnet model and load a checkpoint
			'''
            output_channels = 4
            if args.model_architecture == 'resnet7_2_1':
                resnet_model = resnet7_2_1(
                    pretrained=True,
                    pretrained_model_path=checkpoint_path,
                    output_channels=output_channels)
            resnet_model = resnet_model.to(device)
            '''
			Evaluate the model
			'''
            print("***** Evaluating the model *****")
            eval_results, embeddings, labels_raw = main_utils.evaluate(
                args, device, resnet_model)
            print("***** Finished evaluation *****")

            return eval_results, embeddings, labels_raw

        eval_results, _, _ = run_eval_on_checkpoint(
            checkpoint_path=args.checkpoint_path)

        results_path = os.path.join(args.output_dir, 'eval_results.json')
        with open(results_path, 'w') as fp:
            json.dump(eval_results, fp)
コード例 #12
0
def main():
    args = get_args()

    if not os.path.exists(args.out_dir):
        os.mkdir(args.out_dir)
    logfile = os.path.join(args.out_dir, 'output.log')

    file_handler = logging.FileHandler(logfile)
    file_handler.setFormatter(logging.Formatter('%(levelname)-8s %(asctime)-12s %(message)s'))
    logger.addHandler(file_handler)      

    logger.info(args)

    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    resize_size = args.resize
    crop_size = args.crop

    assert args.data_loader == 'torch'
    train_loader, test_loader = get_loaders(args)

    if 'vit' in args.model:
        num_classes = args.num_classes
        from timm_vit.vit import (
            vit_base_patch2, vit_base_patch16_224_in21k, vit_large_patch16_224_in21k)
        model = eval(args.model)(
            pretrained=(not args.scratch), 
            img_size=crop_size, num_classes=num_classes, patch_size=args.patch, args=args).cuda()
        logger.info('Model {}'.format(model))
    else:
        model = eval(args.model)(in_dim=crop_size).cuda()
    model.train()

    if args.load:
        checkpoint = torch.load(args.load)
        model.load_state_dict(checkpoint['state_dict'])

    if args.method == 'natural':
        if not args.eval:
            ds_train = input_pipeline.get_data(
                dataset=args.data, mode='train',
                repeats=None, mixup_alpha=0, batch_size=args.batch_size, shuffle_buffer=50_000,
                tfds_data_dir=args.tfds_dir, tfds_manual_dir=args.tfds_dir, resize_size=resize_size,
                crop_size=crop_size,
                inception_crop=(not args.no_inception_crop))
            logger.info('VIT ds_train {}'.format(ds_train))
        ds_test = input_pipeline.get_data(
            dataset=args.data, mode='test',
            repeats=1, batch_size=args.batch_size_eval, tfds_data_dir=args.tfds_dir, 
            tfds_manual_dir=args.tfds_dir, crop_size=crop_size)
        logger.info('VIT ds_test {}'.format(ds_test))        
        if args.eval:
            evaluate_natural(args, model, ds_test, verbose=True)
        else:
            train_natural(args, model, ds_train, ds_test)
    elif args.method in ['fgsm', 'pgd', 'trades']:
        if args.eval_all:
            acc = []
            for i in range(1, args.epochs+1):
                logger.info('Evaluating epoch {}'.format(i))
                checkpoint = torch.load(os.path.join(args.out_dir, 'checkpoint_{}'.format(i)))
                model.load_state_dict(checkpoint['state_dict'])
                loss_, acc_ = evaluate_pgd(args, model, test_loader)
                logger.info('Acc: {:.5f}'.format(acc_))
                acc.append(acc_)
            print(acc)
        elif args.eval_aa:
            evaluate_aa(args, model)
        elif args.eval:
            evaluate_pgd(args, model, test_loader)
        else:
            train_adv(args, model, train_loader, test_loader)
    else:
        raise ValueError(args.method)
コード例 #13
0
def main():
    #parse arguments
    args = get_args()

    #make experiment reproducible if specified
    if args.reproducible:
        torch.manual_seed(args.seed)
        np.random.seed(args.seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

    #device setting
    cuda = torch.cuda.is_available()
    device = torch.device('cuda' if cuda else 'cpu')
    print('Device: {}'.format(device))

    #get utt_lists & define labels
    l_dev = sorted(get_utt_list(args.DB_vox2 + args.dev_wav))
    l_val = sorted(get_utt_list(args.DB + args.val_wav))
    l_eval = sorted(get_utt_list(args.DB + args.eval_wav))
    d_label_vox2 = get_label_dic_Voxceleb(l_dev)
    args.model['nb_classes'] = len(list(d_label_vox2.keys()))

    #def make_validation_trial(l_utt, nb_trial, dir_val_trial):
    if bool(args.make_val_trial):
        make_validation_trial(l_utt=l_val,
                              nb_trial=args.nb_val_trial,
                              dir_val_trial=args.DB + 'val_trial.txt')
    with open(args.DB + 'val_trial.txt', 'r') as f:
        l_val_trial = f.readlines()
    with open(args.DB + 'veri_test.txt', 'r') as f:
        l_eval_trial = f.readlines()

    #define dataset generators
    devset = Dataset_VoxCeleb2(list_IDs=l_dev,
                               labels=d_label_vox2,
                               nb_samp=args.nb_samp,
                               base_dir=args.DB_vox2 + args.dev_wav)
    devset_gen = data.DataLoader(devset,
                                 batch_size=args.bs,
                                 shuffle=True,
                                 drop_last=True,
                                 num_workers=args.nb_worker)
    valset = Dataset_VoxCeleb2(list_IDs=l_val,
                               return_label=False,
                               nb_samp=args.nb_samp,
                               base_dir=args.DB + args.val_wav)
    valset_gen = data.DataLoader(valset,
                                 batch_size=args.bs,
                                 shuffle=False,
                                 drop_last=False,
                                 num_workers=args.nb_worker)
    TA_evalset = TA_Dataset_VoxCeleb2(
        list_IDs=l_eval,
        return_label=False,
        window_size=args.window_size,  # 20% of nb_samp
        nb_samp=args.nb_samp,
        base_dir=args.DB + args.eval_wav)
    TA_evalset_gen = data.DataLoader(TA_evalset,
                                     batch_size=1,
                                     shuffle=False,
                                     drop_last=False,
                                     num_workers=args.nb_worker)

    #set save directory
    save_dir = args.save_dir + args.name + '/'
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    if not os.path.exists(save_dir + 'results/'):
        os.makedirs(save_dir + 'results/')
    if not os.path.exists(save_dir + 'models/'):
        os.makedirs(save_dir + 'models/')

    #log experiment parameters to local and comet_ml server
    f_params = open(save_dir + 'f_params.txt', 'w')
    for k, v in sorted(vars(args).items()):
        print(k, v)
        f_params.write('{}:\t{}\n'.format(k, v))
    for k, v in sorted(args.model.items()):
        print(k, v)
        f_params.write('{}:\t{}\n'.format(k, v))
    f_params.close()

    #define model
    if bool(args.mg):
        model_1gpu = RawNet2(args.model)
        if args.load_model:
            model_1gpu.load_state_dict(torch.load(args.load_model_dir))
        nb_params = sum(
            [param.view(-1).size()[0] for param in model_1gpu.parameters()])
        model = nn.DataParallel(model_1gpu).to(device)
    else:
        model = RawNet2(args.model).to(device)
        if args.load_model:
            model.load_state_dict(torch.load(args.load_model_dir))
        nb_params = sum(
            [param.view(-1).size()[0] for param in model.parameters()])
    if not args.load_model: model.apply(init_weights)
    print('nb_params: {}'.format(nb_params))

    #set ojbective funtions
    criterion = {}
    criterion['cce'] = nn.CrossEntropyLoss()

    #set optimizer
    params = [
        {
            'params': [
                param for name, param in model.named_parameters()
                if 'bn' not in name
            ]
        },
        {
            'params': [
                param for name, param in model.named_parameters()
                if 'bn' in name
            ],
            'weight_decay':
            0
        },
    ]
    if args.optimizer.lower() == 'sgd':
        optimizer = torch.optim.SGD(params,
                                    lr=args.lr,
                                    momentum=args.opt_mom,
                                    weight_decay=args.wd,
                                    nesterov=args.nesterov)
    elif args.optimizer.lower() == 'adam':
        optimizer = torch.optim.Adam(params,
                                     lr=args.lr,
                                     weight_decay=args.wd,
                                     amsgrad=args.amsgrad)
    else:
        raise NotImplementedError('Add other optimizers if needed')
    if args.load_model:
        optimizer.load_state_dict(torch.load(args.load_model_opt_dir))

    #set learning rate decay
    if bool(args.do_lr_decay):
        if args.lr_decay == 'keras':
            lr_scheduler = torch.optim.lr_scheduler.LambdaLR(
                optimizer, lr_lambda=lambda step: keras_lr_decay(step))
        elif args.lr_decay == 'cosine':
            raise NotImplementedError('Not implemented yet')
        else:
            raise NotImplementedError('Not implemented yet')

    ##########################################
    #Train####################################
    ##########################################
    best_TA_eval_eer = 99.
    f_eer = open(save_dir + 'eers.txt', 'a', buffering=1)
    for epoch in tqdm(range(args.epoch)):
        #train phase
        train_model(model=model,
                    db_gen=devset_gen,
                    args=args,
                    optimizer=optimizer,
                    lr_scheduler=lr_scheduler,
                    criterion=criterion,
                    device=device,
                    epoch=epoch)

        TA_eval_eer = time_augmented_evaluate_model(mode='eval',
                                                    model=model,
                                                    db_gen=TA_evalset_gen,
                                                    l_utt=l_eval,
                                                    save_dir=save_dir,
                                                    epoch=epoch,
                                                    device=device,
                                                    l_trial=l_eval_trial,
                                                    args=args)
        f_eer.write('epoch:%d, TA_eval_eer:%.4f\n' % (epoch, TA_eval_eer))

        save_model_dict = model_1gpu.state_dict(
        ) if args.mg else model.state_dict()
        if float(TA_eval_eer) < best_TA_eval_eer:
            print('New best TA_EER: %f' % float(TA_eval_eer))
            best_TA_eval_eer = float(TA_eval_eer)

            torch.save(
                save_model_dict,
                save_dir + 'models/TA_%d_%.4f.pt' % (epoch, TA_eval_eer))
            torch.save(optimizer.state_dict(),
                       save_dir + 'models/best_opt_eval.pt')

    f_eer.close()