def main():
    ##############################################################
    # Settings
    parser = argparse.ArgumentParser(description='Model AFN')
    parser.add_argument('--train-dir',
                        help='train feature dir')
    parser.add_argument('--train-utt2label',
                        help='train utt2label')
    parser.add_argument('--eval-dir',
                        help='eval feature dir')
    parser.add_argument('--eval-utt2label',
                        help='eval utt2label')
    parser.add_argument('--model-path',
                        help='path to the pretrained model')
    parser.add_argument('--test-batch-size', type=int, default=100, metavar='N',
                        help='input batch size for testing (default: 100)')
    parser.add_argument('--logging-dir', required=True,
                        help='model save directory')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--seg', default=None,
                        help='seg method')
    parser.add_argument('--seg-win', type=int,
                        help='segmented window size')
    parser.add_argument('--quant-method', required=True,
                        help='quantization method: dynamic static')
    args = parser.parse_args()

    torch.cuda.empty_cache()

    # Init model & Setup logs
    if args.seg is None:
        model = AttenResNet4(atten_activation, atten_channel, size1=(257, M), static_quant=True)
        run_name = "quant_pred-AFN4-1091-orig" + time.strftime("-%Y_%m_%d")
    else:
        if args.seg_win not in (64, 128, 256, 512):
            raise ValueError("Invalid segment window! Must be 64, 128, 256, or 512")
        model = AttenResNet4DeformAll(atten_activation, atten_channel, size1=(257, args.seg_win))  # noqa
        run_name = "quant_pred-AFN4De-" + str(args.seg_win) + "-" + args.seg + time.strftime("-%Y_%m_%d")  # noqa
    logger = setup_logs(args.logging_dir, run_name)

    logger.info("use_cuda is False. Only runnable on CPU!")

    # Setting random seeds for reproducibility.
    np.random.seed(args.seed)
    random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True  # CUDA determinism

    device = torch.device('cpu')
    model.to(device)

    ##############################################################
    # Loading the dataset & the best model
    params = {}

    logger.info('===> loading eval dataset: ' + args.eval_utt2label)
    train_set = SpoofDataset(args.train_dir, args.train_utt2label)
    train_loader = data.DataLoader(
        train_set,
        batch_size=BATCH_SIZE,
        shuffle=True,
        **params
    )  # set shuffle to True
    eval_set = SpoofDataset(args.eval_dir, args.eval_utt2label)
    eval_loader = data.DataLoader(
        eval_set,
        batch_size=TEST_BATCH_SIZE,
        shuffle=False,
        **params
    )  # set shuffle to False
    logger.info('===> loading best model for prediction: ' + args.model_path)
    checkpoint = torch.load(
        os.path.join(args.model_path),
        map_location=device
    )
    model.load_state_dict(checkpoint['state_dict'])

    ##############################################################
    # apply network quantization & prediction
    model = apply_net_quant(
        model=model,
        logger=logger,
        quant_method=args.quant_method,
        calibration_loader=train_loader
    )

    t_start_eval = timer()
    eval_loss, eval_eer = prediction(args, model, device, eval_loader, args.eval_utt2label, rnn)  # noqa
    t_end_eval = timer()
    logger.info("#### Total prediction time: {}".format(t_end_eval - t_start_eval))
    ###########################################################
    logger.info("################## Success #########################\n\n")
Exemplo n.º 2
0
def main():
    ##############################################################
    # Settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--train-scp',
                        required=True,
                        help='kaldi train scp file')
    parser.add_argument('--train-utt2label',
                        required=True,
                        help='train utt2label')
    parser.add_argument('--validation-scp',
                        required=True,
                        help='kaldi dev scp file')
    parser.add_argument('--validation-utt2label',
                        required=True,
                        help='dev utt2label')
    parser.add_argument('--eval-scp', help='kaldi eval scp file')
    parser.add_argument('--eval-utt2label', help='eval utt2label')
    parser.add_argument('--logging-dir',
                        required=True,
                        help='model save directory')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--log-interval',
                        type=int,
                        default=10,
                        metavar='N',
                        help='how many batches to wait before logging '
                        'training status')
    parser.add_argument('--hidden-dim',
                        type=int,
                        default=100,
                        help='number of neurones in the hidden dimension')
    parser.add_argument('--plot-wd', help='training plot directory')
    args = parser.parse_args()
    use_cuda = not args.no_cuda and torch.cuda.is_available()
    print('use_cuda is', use_cuda)
    # print('temperature is', temperature)

    # Global timer
    global_timer = timer()

    # Setup logs
    logger = setup_logs(args.logging_dir, run_name)

    # Setting random seeds for reproducibility.
    np.random.seed(args.seed)
    random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)
    torch.backends.cudnn.deterministic = True  # CUDA determinism

    device = torch.device("cuda:0" if use_cuda else "cpu")
    model.to(device)
    ##############################################################
    # Loading the dataset
    params = {
        'num_workers': 16,
        'pin_memory': True,
        'worker_init_fn': np.random.seed(args.seed)
    } if use_cuda else {}

    logger.info('===> loading train and dev dataset')
    training_set = SpoofDataset(args.train_scp, args.train_utt2label)
    validation_set = SpoofDataset(args.validation_scp,
                                  args.validation_utt2label)
    train_loader = data.DataLoader(training_set,
                                   batch_size=batch_size,
                                   shuffle=True,
                                   **params)  # set shuffle to True
    validation_loader = data.DataLoader(validation_set,
                                        batch_size=test_batch_size,
                                        shuffle=False,
                                        **params)  # set shuffle to False

    logger.info('===> loading eval dataset')
    eval_set = SpoofDataset(args.eval_scp, args.eval_utt2label)
    eval_loader = data.DataLoader(eval_set,
                                  batch_size=test_batch_size,
                                  shuffle=False,
                                  **params)  # set shuffle to False

    optimizer = optim.Adam(model.parameters(), lr=args.lr, amsgrad=True)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                     'min',
                                                     factor=0.5,
                                                     patience=1)

    model_params = sum(p.numel() for p in model.parameters()
                       if p.requires_grad)
    logger.info('### Model summary below###\n {}\n'.format(str(model)))
    logger.info('===> Model total parameter: {}\n'.format(model_params))
    ###########################################################
    # Start training
    best_eer, best_loss = np.inf, np.inf
    early_stopping, max_patience = 0, 5  # early stopping and maximum patience
    print(run_name)
    for epoch in range(1, args.epochs + 1):
        epoch_timer = timer()

        # Train and validate
        train(args, model, device, train_loader, optimizer, epoch, rnn)
        # train(args, model, device, train_loader, optimizer, epoch,
        #       args.train_scp, args.train_utt2label, args.plot_wd, rnn=False)
        val_loss, eer = validation(args, model, device, validation_loader,
                                   args.validation_scp,
                                   args.validation_utt2label, rnn)
        scheduler.step(val_loss)
        # Save
        if select_best == 'eer':
            is_best = eer < best_eer
            best_eer = min(eer, best_eer)
        elif select_best == 'val':
            is_best = val_loss < best_loss
            best_loss = min(val_loss, best_loss)
        snapshot(
            args.logging_dir, run_name, is_best, {
                'epoch': epoch + 1,
                'best_eer': best_eer,
                'state_dict': model.state_dict(),
                'validation_loss': val_loss,
                'optimizer': optimizer.state_dict(),
            })
        # Early stopping
        if is_best == 1:
            early_stopping = 0
        else:
            early_stopping += 1
        end_epoch_timer = timer()
        logger.info("#### End epoch {}/{}, "
                    "elapsed time: {}".format(epoch, args.epochs,
                                              end_epoch_timer - epoch_timer))
        if early_stopping == max_patience:
            break
    ###########################################################
    # Prediction
    logger.info('===> loading best model for prediction')
    checkpoint = torch.load(
        os.path.join(args.logging_dir, run_name + '-model_best.pth'))
    model.load_state_dict(checkpoint['state_dict'])

    eval_loss, eval_eer = prediction(args, model, device, eval_loader,
                                     args.eval_scp, args.eval_utt2label, rnn)
    ###########################################################
    end_global_timer = timer()
    logger.info("################## Success #########################")
    logger.info("Total elapsed time: %s" % (end_global_timer - global_timer))
Exemplo n.º 3
0
def main():
    ##############################################################
    # Settings
    parser = argparse.ArgumentParser(description='Model AFN')
    parser.add_argument('--eval-dir',
                        help='eval feature dir')
    parser.add_argument('--eval-utt2label',
                        help='train utt2label')
    parser.add_argument('--model-path',
                        help='path to the pretrained model')
    parser.add_argument('--test-batch-size', type=int, default=100, metavar='N',
                        help='input batch size for testing (default: 100)')
    parser.add_argument('--logging-dir', required=True,
                        help='model save directory')
    parser.add_argument('--no-cuda', action='store_true', default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument('--seg', default=None,
                        help='seg method')
    parser.add_argument('--seg-win', type=int,
                        help='segmented window size')
    parser.add_argument('--decomp-rank', type=int,
                        help='rank value for decomposition')
    parser.add_argument('--decomp-type', default='tucker',
                        help='decomposition type')

    args = parser.parse_args()

    torch.cuda.empty_cache()

    # Init model & Setup logs
    if args.seg is None:
        model = AttenResNet4(atten_activation, atten_channel, size1=(257, M))
        run_name = "decomp_pred-AFN4-1091-orig" + time.strftime("-%Y_%m_%d")
    else:
        if args.seg_win not in (64, 128, 256, 512):
            raise ValueError("Invalid segment window! Must be 64, 128, 256, or 512")
        model = AttenResNet4DeformAll(atten_activation, atten_channel, size1=(257, args.seg_win))  # noqa
        run_name = "decomp_pred-AFN4De-" + str(args.seg_win) + "-" + args.seg + time.strftime("-%Y_%m_%d")  # noqa
    logger = setup_logs(args.logging_dir, run_name)

    use_cuda = not args.no_cuda and torch.cuda.is_available()
    logger.info("use_cuda is {}".format(use_cuda))

    # Setting random seeds for reproducibility.
    np.random.seed(args.seed)
    random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True  # CUDA determinism

    device = torch.device("cuda" if use_cuda else "cpu")
    model.to(device)

    ##############################################################
    # Loading the dataset
    params = {'num_workers': 0,
              'pin_memory': False,
              'worker_init_fn': np.random.seed(args.seed)} if use_cuda else {}

    logger.info('===> loading eval dataset: ' + args.eval_utt2label)
    eval_set = SpoofDataset(args.eval_dir, args.eval_utt2label)
    eval_loader = data.DataLoader(
        eval_set,
        batch_size=TEST_BATCH_SIZE,
        shuffle=False,
        **params
    )  # set shuffle to False

    ##############################################################
    # apply network decomposition and load fine-tuned weights
    model.to('cpu')
    model = apply_net_decomp(
        model=model,
        logger=logger,
        rank=args.decomp_rank,
        decomp_type=args.decomp_type
    )
    logger.info('===> loading fine-tuned model for prediction: ' + args.model_path)
    checkpoint = torch.load(
        os.path.join(args.model_path),
        map_location=device
    )
    model.load_state_dict(checkpoint['state_dict'])
    model.to(device)

    t_start_eval = timer()
    eval_loss, eval_eer = prediction(args, model, device, eval_loader, args.eval_utt2label, rnn)  # noqa
    t_end_eval = timer()
    logger.info("#### Total prediction time: {}".format(t_end_eval - t_start_eval))
    ###########################################################
    logger.info("################## Success #########################\n\n")
Exemplo n.º 4
0
def main():
    ##############################################################
    # Settings
    parser = argparse.ArgumentParser(description='Model AFN')
    parser.add_argument('--train-dir', required=True, help='train feature dir')
    parser.add_argument('--train-utt2label',
                        required=True,
                        help='train utt2label')
    parser.add_argument('--validation-dir',
                        required=True,
                        help='dev feature dir')
    parser.add_argument('--validation-utt2label',
                        required=True,
                        help='dev utt2label')
    parser.add_argument('--eval-dir', help='eval feature dir')
    parser.add_argument('--eval-utt2label', help='train utt2label')
    parser.add_argument('--logging-dir',
                        required=True,
                        help='model save directory')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        metavar='N',
                        help='number of epochs to train (default: 10)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging training status')
    parser.add_argument('--seg', default=None, help='seg method')
    parser.add_argument('--seg-win', type=int, help='segmented window size')
    args = parser.parse_args()

    torch.cuda.empty_cache()

    # Init model & Setup logs
    if args.seg is None:
        model = AttenResNet4(atten_activation, atten_channel, size1=(257, M))
        run_name = "AFN4" + time.strftime("-%Y_%m_%d-%H_%M_%S-") + str(
            M) + "-orig"
    else:
        model = AttenResNet4DeformAll(atten_activation,
                                      atten_channel,
                                      size1=(257, args.seg_win))  # noqa
        run_name = "AFN4De" + time.strftime("-%Y_%m_%d-%H_%M_%S-") + str(
            args.seg_win) + "-" + args.seg  # noqa
    logger = setup_logs(args.logging_dir, run_name)

    use_cuda = not args.no_cuda and torch.cuda.is_available()
    logger.info("use_cuda is {}".format(use_cuda))

    # Global timer
    global_timer = timer()

    # Setting random seeds for reproducibility.
    np.random.seed(args.seed)
    random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True  # CUDA determinism

    device = torch.device("cuda" if use_cuda else "cpu")
    model.to(device)
    ##############################################################
    # Loading the dataset
    params = {
        'num_workers': 0,
        'pin_memory': False,
        'worker_init_fn': np.random.seed(args.seed)
    } if use_cuda else {}

    logger.info('===> loading train and dev dataset')
    training_set = SpoofDataset(args.train_dir, args.train_utt2label)
    validation_set = SpoofDataset(args.validation_dir,
                                  args.validation_utt2label)
    train_loader = data.DataLoader(training_set,
                                   batch_size=BATCH_SIZE,
                                   shuffle=True,
                                   **params)  # set shuffle to True
    validation_loader = data.DataLoader(validation_set,
                                        batch_size=TEST_BATCH_SIZE,
                                        shuffle=False,
                                        **params)  # set shuffle to False

    optimizer = optim.Adam(model.parameters(), lr=args.lr, amsgrad=True)
    scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                     'min',
                                                     factor=0.5,
                                                     patience=1)  # noqa

    model_params = sum(p.numel() for p in model.parameters()
                       if p.requires_grad)
    logger.info('#### Model summary below ####\n {}\n'.format(str(model)))
    logger.info('===> Model total # parameter: {}\n'.format(model_params))
    ###########################################################
    # Training
    best_eer, best_loss = np.inf, np.inf
    early_stopping, max_patience = 0, 5  # early stopping and maximum patience

    total_train_time = []
    for epoch in range(1, args.epochs + 1):
        epoch_timer = timer()

        # Train and validate
        train(args, model, device, train_loader, optimizer, epoch, rnn)
        val_loss, eer = validation(args, model, device, validation_loader,
                                   args.validation_utt2label, rnn)
        scheduler.step(val_loss)
        # Save
        if SELECT_BEST == 'eer':
            is_best = eer < best_eer
            best_eer = min(eer, best_eer)
        elif SELECT_BEST == 'val':
            is_best = val_loss < best_loss
            best_loss = min(val_loss, best_loss)
        snapshot(
            args.logging_dir, run_name, is_best, {
                'epoch': epoch + 1,
                'best_eer': best_eer,
                'state_dict': model.state_dict(),
                'validation_loss': val_loss,
                'optimizer': optimizer.state_dict(),
            })
        # Early stopping
        if is_best == 1:
            early_stopping = 0
        else:
            early_stopping += 1
        end_epoch_timer = timer()
        logger.info("#### End epoch {}/{}, elapsed time: {}".format(
            epoch, args.epochs, end_epoch_timer - epoch_timer))  # noqa
        total_train_time.append(end_epoch_timer - epoch_timer)
        if early_stopping == max_patience:
            break
    logger.info("#### Avg. training+validation time per epoch: {}".format(
        np.average(total_train_time)))  # noqa
    ###########################################################
    # Prediction
    if args.eval_dir and args.eval_utt2label:
        logger.info('===> loading eval dataset')
        eval_set = SpoofDataset(args.eval_dir, args.eval_utt2label)
        eval_loader = data.DataLoader(eval_set,
                                      batch_size=TEST_BATCH_SIZE,
                                      shuffle=False,
                                      **params)  # set shuffle to False

        logger.info('===> loading best model for prediction')
        checkpoint = torch.load(
            os.path.join(args.logging_dir, run_name + '-model_best.pth'))
        model.load_state_dict(checkpoint['state_dict'])
        t_start_eval = timer()
        eval_loss, eval_eer = prediction(args, model, device, eval_loader,
                                         args.eval_utt2label, rnn)  # noqa
        end_global_timer = timer()
        logger.info(
            "#### Total prediction time: {}".format(end_global_timer -
                                                    t_start_eval))  # noqa
    ###########################################################
    logger.info("################## Success #########################")
    logger.info("Total elapsed time: %s" % (end_global_timer - global_timer))