Ejemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(
        description='find the right learnrate for a particular' +
        'architecture / set of other hyperparameters')
    parser.add_argument('splits', type=str, help='on which splits to train')
    parser.add_argument(
        'model',
        choices=models.get_model_classes(),
        help='any classname of model as defined in "models.py"')
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--n_steps', type=int, default=1024)
    parser.add_argument('--plotname',
                        type=str,
                        default='find_learnrate.pdf',
                        help='the name of the plot')
    parser.add_argument('--device', type=str, default='cuda')
    args = parser.parse_args()

    cuda = False
    if args.device.startswith('cpu'):
        cuda = False
    elif args.device.startswith('cuda'):
        cuda = True
    else:
        print('unknown device type "{}"'.format(args.device))
        exit(-1)

    ##########################################
    # prepare train data
    train_filenames = filenames_from_splitfile(
        os.path.join(args.splits, 'train'))
    train_sequences = datasets.get_sequences(train_filenames)

    batch_size = args.batch_size
    n_steps = args.n_steps
    # one train loader for all train sequences
    train_dataset = ConcatDataset(train_sequences)
    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              sampler=ChunkedRandomSampler(
                                  train_dataset, batch_size * n_steps),
                              num_workers=1,
                              pin_memory=True,
                              drop_last=True)

    net_class = getattr(models, args.model, None)
    if net_class is None:
        raise RuntimeError(
            'could not find model class named "{}" in "models.py"'.format(
                args.model))

    net = net_class()
    optimizer = OneCyclePolicy(net.parameters(),
                               learnrates=np.linspace(1e-5, 50, n_steps),
                               momenta=np.ones(n_steps) * 0.9,
                               nesterov=True)

    if cuda:
        net.cuda()

    losses, learning_rates = find_learnrate(cuda=cuda,
                                            net=net,
                                            optimizer=optimizer,
                                            loader=train_loader)

    fig, ax = plt.subplots()
    ax.set_title('find minimum, divide by 10, that is your initial guess!\n' +
                 'then either reduce or increase learnrate,\n' +
                 'until first few hundred updates do not diverge.')
    ax.semilogx(learning_rates, losses)
    ax.set_xlabel('learning rate (log scale)')
    ax.set_ylabel('loss')
    fig.tight_layout()
    fig.savefig(args.plotname)
Ejemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(
        description='train a convolutional network' + 'on the MAPS dataset')
    parser.add_argument('splits', type=str, help='on which splits to train')
    parser.add_argument('run_path', type=str, help='where to write run state')
    parser.add_argument(
        'model',
        choices=models.get_model_classes(),
        help='any classname of model as defined in "models.py"')
    parser.add_argument('--batch_size', type=int, default=128)
    parser.add_argument('--epochs', type=int, default=150)
    parser.add_argument('--capacity', type=int, default=32)
    parser.add_argument('--hcnn_undertones', type=int, default=0)
    parser.add_argument('--hcnn_overtones', type=int, default=1)
    parser.add_argument('--hcnn_onlyinput', type=int, default=0)
    parser.add_argument('--device', type=str, default='cuda')
    args = parser.parse_args()

    cuda = False
    if args.device.startswith('cpu'):
        cuda = False
    elif args.device.startswith('cuda'):
        cuda = True
    else:
        print('unknown device type "{}"'.format(args.device))
        exit(-1)

    run_path = args.run_path
    if os.path.exists(run_path):
        print('run_path "{}" already exists!'.format(run_path))
        # exit(-1)

    ##########################################
    # prepare train data
    train_filenames = filenames_from_splitfile(
        os.path.join(args.splits, 'train'))
    train_sequences = datasets.get_sequences(train_filenames)

    train_dataset = ConcatDataset(train_sequences)

    batch_size = args.batch_size
    n_epochs = args.epochs

    # go with the original definition of an 'epoch' (aka the whole dataset ...)
    # as opposed to 'arbitrary number of steps until we validate'
    # n_steps = 8192
    n_steps = len(train_dataset) // batch_size
    print('adjusted n_steps', n_steps)

    # one train loader for all train sequences
    train_loader = DataLoader(train_dataset,
                              batch_size=batch_size,
                              sampler=ChunkedRandomSampler(
                                  train_dataset, batch_size * n_steps),
                              num_workers=1,
                              pin_memory=True,
                              drop_last=True)

    # prepare valid data as individual sequences
    valid_filenames = filenames_from_splitfile(
        os.path.join(args.splits, 'valid'))

    # to get all of the sequence data, just write this:
    valid_sequences = datasets.get_sequences(valid_filenames)

    valid_loaders = []
    for valid_sequence in valid_sequences:
        valid_loader = DataLoader(valid_sequence,
                                  batch_size=1024,
                                  sampler=SequentialSampler(valid_sequence),
                                  num_workers=0,
                                  pin_memory=False,
                                  drop_last=False)
        valid_loaders.append(valid_loader)

    log_dir = os.path.join(run_path, 'tensorboard')
    ensure_empty_directory_exists(log_dir)
    logger = SummaryWriter(log_dir=log_dir, flush_secs=10)

    net_class = getattr(models, args.model, None)
    if net_class is None:
        raise RuntimeError(
            'could not find model class named "{}" in "models.py"'.format(
                args.model))

    net = net_class(args)

    if args.model == 'AllConv2016':
        print('choosing AllConv2016 learnrate and learnrate schedule!')
        # this does not train all that well ... validation loss stays high all the time?
        optimizer = optim.SGD(net.parameters(),
                              lr=1.0,
                              momentum=0.9,
                              weight_decay=1e-5,
                              nesterov=False)
        milestones = list(range(10, n_epochs, 10))
        scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
                                                   milestones,
                                                   gamma=0.5)
    else:
        print('choosing VGGNet2016 learnrate and learnrate schedule!')
        # this leads to the results from 2016 in ~5 epochs
        optimizer = optim.SGD(net.parameters(),
                              lr=0.1,
                              momentum=0.9,
                              weight_decay=1e-5,
                              nesterov=False)
        scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer,
                                                         mode='min',
                                                         factor=0.5,
                                                         patience=20,
                                                         verbose=True,
                                                         min_lr=1e-4)

    if cuda:
        net.cuda()

    summary(net, (1, 5, 229))

    train(cuda=cuda,
          run_path=run_path,
          net=net,
          optimizer=optimizer,
          scheduler=scheduler,
          n_epochs=n_epochs,
          train_loader=train_loader,
          valid_loader=valid_loaders,
          logger=logger)
Ejemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description='evaluate a convolutional network on the MAPS dataset')
    parser.add_argument('model', choices=models.get_model_classes(),
                        help='any classname of model as defined in "models.py"')
    parser.add_argument('net_state', type=str,
                        help='which network state to use')
    parser.add_argument('splitfile', type=str,
                        help='the list of files that will be evaluated')
    parser.add_argument('--start_end', type=str, default="0,750", help='start and end frame; the default value boils down to the first 30[s] at a framerate of 25[fps]')
    parser.add_argument('--device', type=str, default='cuda')
    args = parser.parse_args()

    cuda = False
    if args.device.startswith('cpu'):
        cuda = False
    elif args.device.startswith('cuda'):
        cuda = True
    else:
        print('unknown device type "{}"'.format(args.device))
        exit(-1)

    # prepare test data as individual sequences
    test_filenames = filenames_from_splitfile(args.splitfile)

    start_end = None
    try:
        if args.start_end is not None:
            start, end = tuple(int(t) for t in args.start_end.split(','))
            duration = end - start
            if duration <= 0:
                raise ValueError('negative durations are unacceptable ...')
            duration_in_s = float(duration) / 25.
        print('evaluating on frames {} of the data ({} [s])'.format((start, end), duration_in_s))
        start_end = (start, end)
        if abs(duration_in_s - 30.) < 0.5:
            print('using the SAME evaluation protocol (as in the paper)')
        else:
            print('using a DIFFERENT evalution protocol (not as in the paper)')
    except Exception as e:
        print('evaluating on ALL the data')
        print('using a DIFFERENT evalution protocol (not as in the paper)')

    test_sequences = datasets.get_sequences(test_filenames, start_end)
    test_loaders = []
    for test_sequence in test_sequences:
        test_loader = DataLoader(
            test_sequence,
            batch_size=1024,
            sampler=SequentialSampler(test_sequence),
            num_workers=0,
            pin_memory=False,
            drop_last=False
        )
        test_loaders.append(test_loader)

    net_class = getattr(models, args.model, None)
    if net_class is None:
        raise RuntimeError('could not find model class named "{}" in "models.py"'.format(args.model))

    net = net_class()
    net.load_state_dict(torch.load(args.net_state))

    if cuda:
        net.cuda()

    loss, p, r, f = evaluate_multiple_loaders(cuda, net, test_loaders)
    print('l {:8.4f} p {:8.4f}, r {:8.4f}, f {:8.4f}'.format(loss, p, r, f))