Ejemplo n.º 1
0
            logging.info('Running K-fold Cross Validation: Fold ' +
                         str(i + 1) + " of " + str(k_folds))
            kfold_i = i
            data_config['k_fold'] = [
                k_folds, kfold_i, experiment_config['k_fold_valid']
            ]
        else:
            kfold_i = None

        # Load the datasets
        train_dataset, valid_dataset, test_dataset = get_datasets(
            **data_config)

        if train_dataset.__len__() > 0:
            train_data_loader = DataLoader(train_dataset,
                                           batch_size=batch_size,
                                           cores=args.cores)
            logging.info('Loaded %g training samples', len(train_dataset))
        else:
            train_data_loader = None
        if valid_dataset.__len__() > 0:
            valid_data_loader = DataLoader(valid_dataset,
                                           batch_size=batch_size,
                                           cores=args.cores)
            logging.info('Loaded %g validation samples', len(valid_dataset))
        else:
            valid_data_loader = None
        if test_dataset.__len__() > 0:
            test_data_loader = DataLoader(test_dataset,
                                          batch_size=batch_size,
                                          cores=args.cores)
    out_path = args.out_path
    topic = args.topic

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    n_gpu = torch.cuda.device_count()
    print("device", device, "n_gpu", n_gpu)

    logger = ResultLogger(path=os.path.join(log_dir,
                                            '{}log.json'.format(desc)),
                          **args.__dict__)
    text_encoder = TextEncoder(args.encoder_path, args.bpe_path)
    encoder = text_encoder.encoder
    n_vocab = len(text_encoder.encoder)

    print("Encoding dataset...")
    dataLoader = DataLoader()
    ((trX, trY), (vaX, vaY),
     (teX, )) = encode_dataset(*dataLoader.veracity(data_dir, topic=topic),
                               encoder=text_encoder)

    encoder['_start_'] = len(encoder)
    encoder['_classify_'] = len(encoder)
    clf_token = encoder['_classify_']
    n_special = 2
    max_len = n_ctx - 2
    # Define maximum context as the minimum of [512, x] where x is the max sentence length
    n_ctx = min(
        max([len(x[:max_len])
             for x in trX] + [len(x[:max_len])
                              for x in vaX] + [len(x[:max_len])
                                               for x in teX]) + 3, n_ctx)
    train_data_function = FreewayForwardDataset(
                                   train_data_file,
                                   number_condition=largs.number_condition,
                                   steps_ahead=largs.steps_ahead,
                                   max_pixel_used=config.freeway_max_pixel,
                                   min_pixel_used=config.freeway_min_pixel,
                                   augment_file=aug_train_data_file)
    test_data_function = FreewayForwardDataset(
                                   test_data_file,
                                   number_condition=largs.number_condition,
                                   steps_ahead=largs.steps_ahead,
                                   max_pixel_used=config.freeway_max_pixel,
                                   min_pixel_used=config.freeway_min_pixel,
                                   augment_file=aug_test_data_file)

    data_loader = DataLoader(train_data_function, test_data_function,
                                   batch_size=args.num_to_sample)


    args.size_training_set = len(train_data_function)
    hsize = data_loader.train_loader.data.shape[1]
    wsize = data_loader.train_loader.data.shape[2]

    info = model_dict['info']
    largs = info['args'][-1]

    try:
        print(largs.encoder_output_size)
    except:
        largs.encoder_output_size = 1000

    encoder_model = ConvVAE(largs.code_length,
        train_data_file,
        number_condition=args.number_condition,
        steps_ahead=args.steps_ahead,
        max_pixel_used=config.freeway_max_pixel,
        min_pixel_used=config.freeway_min_pixel,
        augment_file=aug_train_data_file)
    test_data_function = FreewayForwardDataset(
        test_data_file,
        number_condition=args.number_condition,
        steps_ahead=args.steps_ahead,
        max_pixel_used=config.freeway_max_pixel,
        min_pixel_used=config.freeway_min_pixel,
        augment_file=aug_test_data_file)

    data_loader = DataLoader(train_data_function,
                             test_data_function,
                             batch_size=args.batch_size)

    info = {
        'train_cnts': [],
        'train_losses': [],
        'train_kl_losses': [],
        'train_rec_losses': [],
        'test_cnts': [],
        'test_losses': [],
        'test_kl_losses': [],
        'test_rec_losses': [],
        'save_times': [],
        'args': [args],
        'last_save': 0,
        'last_plot': 0,
Ejemplo n.º 5
0
        train_data_file,
        number_condition=largs.number_condition,
        steps_ahead=largs.steps_ahead,
        max_pixel_used=config.freeway_max_pixel,
        min_pixel_used=config.freeway_min_pixel,
        augment_file=aug_train_data_file)
    test_data_function = FreewayForwardDataset(
        test_data_file,
        number_condition=largs.number_condition,
        steps_ahead=largs.steps_ahead,
        max_pixel_used=config.freeway_max_pixel,
        min_pixel_used=config.freeway_min_pixel,
        augment_file=aug_test_data_file)

    data_loader = DataLoader(train_data_function,
                             test_data_function,
                             batch_size=args.num_to_sample)

    args.size_training_set = len(train_data_function)
    hsize = data_loader.train_loader.data.shape[1]
    wsize = data_loader.train_loader.data.shape[2]

    info = model_dict['info']
    largs = info['args'][-1]

    encoder_model = ConvVAE(largs.code_length,
                            input_size=largs.number_condition,
                            encoder_output_size=largs.encoder_output_size)
    encoder_model.load_state_dict(model_dict['vae_state_dict'])

    pcnn_decoder = GatedPixelCNN(input_dim=1,
Ejemplo n.º 6
0
parser.add_argument("--exname", default="PoseAgent", type=str)
parser.add_argument("--yaml_file", default="settings/ycb.yaml")
args = parser.parse_args()

settings = SETTINGS(yaml_file=args.yaml_file)
settings.merge_args(args)

# LOAD DATASET
train_dataset = []
if settings.DATASET == "ycb" or settings.DATASET == "all":
    from datasets import YCBDataset
    train_dataset.append(YCBDataset(settings, "train", settings.CLASS_ID))
train_dataloader = DataLoader(ConcatDataset(train_dataset),
                              batch_size=settings.BATCH_SIZE,
                              shuffle=True,
                              collate_fn=meshes_collate,
                              num_workers=settings.NUM_WORKERS,
                              pin_memory=False,
                              drop_last=True)
if settings.IRL:
    # LOAD IRL DEMOSTRATION TODO
    pass
    demo_dataset = []
    demo_dataloader = []

agent = Agent(settings, device=settings.DEVICE).to(settings.DEVICE)
optimizer = optim.Adam([{
    "params": agent.flownet.parameters(),
    "lr": settings.BACKBONE_LEARNING_RATE
}, {
    "params": agent.actor.parameters()
Ejemplo n.º 7
0
    parser.add_argument("--type", type=str)
    parser.add_argument("--prediction_type",type=str, default="predict_n_steps")
    parser.add_argument("--predict_n_steps", type=int, default=10)
    parser.add_argument("--show_last_n_steps", type=int, default=30)
    args = parser.parse_args()
    params_dict = load_params(args.path)
    test_dataset = CryptoDataset(
        [pd.read_csv("/home/cehmann/workspaces/stonks/data/5 min/world/cryptocurrencies/bts.v.txt").iloc[-1000:].reset_index()],
        sample_length=params_dict["data"]["sample_length"],
    )

    if args.type == "lstm":
        model = AutoregressiveLstm.load_from_checkpoint(args.path)
    else:
        model = Transformer.load_from_checkpoint(args.path)
    test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False)
    for batch in test_dataloader:

        if args.prediction_type=="predict_n_steps":

            minus_last_ten = batch[:, :, 0:-args.predict_n_steps]
            normed = batch
            predictions = (
                predict_n_steps(model, minus_last_ten, n_steps=args.predict_n_steps)
                .detach()
                .numpy()
            )

            for i, name in enumerate(["<CLOSE>"]):
                plt.plot(normed[0, i, -args.show_last_n_steps :], color="blue")
                plt.plot(
Ejemplo n.º 8
0
log_file_path = base_path + 'cew_training.log'
csv_logger = CSVLogger(log_file_path, append=False)
#early_stop = EarlyStopping('val_loss', patience=patience)
reduce_lr = ReduceLROnPlateau('val_loss',
                              factor=0.1,
                              patience=int(patience / 4),
                              verbose=1)
trained_models_path = base_path + 'cew_blink_detect'
model_names = trained_models_path + '.{epoch:02d}-{val_acc:.2f}.hdf5'
model_checkpoint = ModelCheckpoint(model_names,
                                   'val_loss',
                                   verbose=1,
                                   save_best_only=True)
callbacks = [model_checkpoint, csv_logger, early_stop, reduce_lr]

# loading dataset
data_loader = DataLoader('cew')
faces, eyestates = data_loader.get_data()
print("out shapes: ", faces.shape, eyestates.shape)
faces = preprocess_input(faces)
num_samples, num_classes = eyestates.shape
train_data, val_data = split_data(faces, eyestates, validation_split)
train_faces, train_eyestates = train_data
model.fit_generator(data_generator.flow(train_faces, train_eyestates,
                                        batch_size),
                    steps_per_epoch=len(train_faces) / batch_size,
                    epochs=num_epochs,
                    verbose=1,
                    callbacks=callbacks,
                    validation_data=val_data)
                        format=log_format,
                        handlers=[
                            logging.StreamHandler(),
                            logging.FileHandler(output_dir +
                                                '/interpret/analysis.log')
                        ])
    logging.info('Initializing')

    # Load the datasets
    data_config['split'] = [1.0, 0, 0]
    data_config['augment'] = 1
    batch_size = 1
    dataset, _, __ = get_datasets(**data_config)
    del _
    del __
    data_loader = DataLoader(dataset, batch_size=batch_size, cores=args.cores)
    logging.info('Loaded %g samples', len(dataset))

    # Load the trainer
    trainer = get_trainer(output_dir=output_dir, **experiment_config)

    # Build the model
    trainer.build_model(**model_config)
    trainer.print_model_summary()
    trainer.saver = tf.train.Saver()

    # Run analysis of model
    with tf.Session() as sess:

        # Setup the variable initialisation
        sess.run(tf.global_variables_initializer())