Beispiel #1
0
def main(_):
    print("Start preprocessing data...")
    if FLAGS.actions == "random":
        data = data_preprocessing.data_prep(FLAGS.dataset, FLAGS.num_actions,
                                            FLAGS.total_num_actions)
    else:
        actions = FLAGS.actions.split(",")
        data = data_preprocessing.data_prep(FLAGS.dataset, FLAGS.num_actions,
                                            FLAGS.total_num_actions, actions)

    video_dir = os.path.normpath(
        os.path.join(FLAGS.video_dir, FLAGS.dataset,
                     "{0}_actions".format(FLAGS.num_actions),
                     '_'.join(data.actions)))
    tf.reset_default_graph()
    print("Create model...")
    model = seq2seq_model.Seq2SeqModel(len(data.dim_use), FLAGS.source_seq_len,
                                       FLAGS.target_seq_len, FLAGS.rnn_size,
                                       FLAGS.batch_size, FLAGS.learning_rate)
    sess = training.get_session()
    #perform segmentations with pre-trained model
    if FLAGS.load > 0:
        model = training.load_model(sess, model, train_dir, FLAGS.load)
    else:
        #train new model
        if FLAGS.load <= 0:
            current_step = 0
            print("Creating model with fresh parameters.")
            sess.run(tf.global_variables_initializer())
        #continue training given a checkpoint
        else:
            current_step = FLAGS.load + 1
            model = training.load_model(sess, model, train_dir, FLAGS.load)
        print("Training is in process...")
        training.train_model(sess, model, train_dir, data, model.batch_size,
                             current_step, FLAGS.iterations)
    print("Finding temporal segments...")
    test_states, all_states, cuts = segmentation.find_cuts(
        sess, model, data.norm_complete_train)
    print("Doing clustering...")
    pred_labels, reduced_states, labels_true, all_labels = segmentation.clustering(
        model, cuts, test_states, all_states, data.trainData,
        FLAGS.num_actions)
    labels_true, labels_pred = segmentation.order_labels(
        labels_true, all_labels)
    colors = viz.get_color(labels_true)
    xyz_gt = get_gt(data.complete_train)
    print("Generate results...")
    print("length of gt:{0}, pred:{1},reduced:{2}:".format(
        len(labels_true), len(labels_pred), len(reduced_states)))
    generate_video(video_dir, labels_true, labels_pred, xyz_gt, reduced_states,
                   colors, FLAGS.gen_video)
Beispiel #2
0
def kfold(dataset, config, train_id, num_completed=0):
    # Initialize the model
    model = load_model(config["model"])
    logging.info("Total Data: %s samples" % len(dataset))
    logging.info("Running %sfold validation" % config["kfold"])
    best_vals = []

    completed = set(range(num_completed))
    for i, (train_data, val_data) in enumerate(
            dataset.kfold(k=config["kfold"],
                          shuffle=True,
                          seed=np.random.randint(2**32))):
        if i in completed:
            continue
        logging.info("Training Fold%s" % (i + 1))
        train_id = train_id + "_fold%s" % i
        # TODO add load model functinoality
        model, tr_logs, val_logs = train_model(model,
                                               train_id,
                                               config["optimizer"],
                                               train_data,
                                               val_data,
                                               epochs=config["epochs"],
                                               batch_size=config["batch_size"],
                                               plot=PLOT,
                                               load_model=False)
        best_vals.append(min(val_logs['loss']))

    logging.info("Average val loss: %s" % (sum(best_vals) / len(best_vals)))
    return model
Beispiel #3
0
    def __init_tacotron(self, tacotron_model_path, map_location):

        temp_model = torch.load(tacotron_model_path, map_location=map_location)
        state_dict = temp_model['state_dict']

        # load trained tacotron 2 model:
        hparams = tacotron_params

        model = load_model(hparams)
        model.load_state_dict(state_dict)
        _ = model.eval()

        return model
Beispiel #4
0
def instantiate_players(mode):
    full_mode_name = {
        'dd': 'two dummies',
        'hd': 'human vs dummy',
        'ai': 'ai vs dummy'
    }
    print("Game mode:", full_mode_name[mode], '\n')
    instructions_for_creation = {
        'dd': [DummyPlayer(name) for name in NAMES_FOR_TESTING],
        'hd': [
            combatent(NAMES_FOR_TESTING[i])
            for i, combatent in enumerate([HumanPlayer, DummyPlayer])
        ],
        'ai': [
            AiPlayer(NAMES_FOR_TESTING[0], load_model(NAMES_FOR_TESTING[0])),
            AiPlayer(NAMES_FOR_TESTING[1], load_model(NAMES_FOR_TESTING[1]))
        ]
    }

    pair_of_players = instructions_for_creation[mode]
    relate_opponents(pair_of_players)
    return pair_of_players
Beispiel #5
0
def test(dataset, test_img_sizes, config, train_id, model=None):
    if model is None:
        model = load_model(config["model"])

    if not config["kfold"]:
        raise NotImplementedError("Non-kfold testing is not implemented")

    predictions = 0.
    for i in range(config["kfold"]):
        logging.info("Predicting fold %s/%s" % (i + 1, config["kfold"]))
        predictions = predictions + test_model(model, train_id + "_fold%s" % i,
                                               dataset, config["batch_size"])
    predictions = predictions / config["kfold"]

    _, _, _, submission_file = create_filenames(train_id)
    # Make the submission
    dsb.save_submission(dataset.ids, expit(predictions), test_img_sizes,
                        submission_file)
from text import text_to_sequence

import sys

sys.path.append('waveglow/')

predicted_melspec_folder = '/homedtic/apeiro/GST_Tacotron2_pitch_intensity_contour_sparse_SoftMax/Predicted_melspec/'
audio_path = '/homedtic/apeiro/GST_Tacotron2_pitch_intensity_contour_sparse_SoftMax/Synth_wavs/'
common_clip_name = '_78000steps_softmax_3tokens_'

hparams = tacotron_params
MAX_WAV_VALUE = 32768.0

# load trained tacotron 2 model:
checkpoint_path = "outputs/checkpoint_78000"
model = load_model(hparams)
model.load_state_dict(torch.load(checkpoint_path)['state_dict'])
_ = model.eval()

# load pre trained waveglow model for mel2audio:
waveglow_path = 'waveglow/waveglow_old.pt'
waveglow = torch.load(waveglow_path)['model']
waveglow.cuda()
'''
# short sentences:

test_text_short = ("That is not only my accusation.",
                   "provided by other agencies.",
                   "There were others less successful.")

# medium sentences:
Beispiel #7
0
def main(args=None):
    '''
    Initializes and trains model with its specified configuration, 
    evaluates each on the test set, and records its performance to 
    tensorboard.
    '''
    print("Using GPU: {}".format(torch.cuda.is_available()))

    model = getattr(training.Models, Constants.model_str)
    optimizer = training.init_optimizer(model, args.optimizer)
    loss_func = torch.nn.CrossEntropyLoss()

    # handle model loading, if the flag is set
    if args.load_model == "wide-resnet":
        model = torchvision.models.wide_resnet50_2(pretrained=False,
                                                   progress=True)
    elif args.load_model:
        model, optimizer, pretrained_epochs, loss = training.load_model(
            args.load_model, model, optimizer)
        print("Loaded model with {} epochs and {} loss.".format(
            pretrained_epochs, loss))

    Constants.save_str = display.build_model_save_str(args)
    writer = SummaryWriter(
        Path(__file__).parent.parent / "runs" / Constants.save_str)

    train_dlr, valid_dlr, test_dlr = data_loading.build_wrapped_dl(
        args.augmentation, args.dataset, verbose=args.verbose)

    bar = progressbar.ProgressBar(max_value=args.epochs * len(train_dlr) *
                                  Constants.batch_size,
                                  max_error=False)

    # show images and model graph
    images, _ = iter(train_dlr).__next__()
    display.show_images(writer,
                        images,
                        Constants.batch_size,
                        title="Images",
                        verbose=args.verbose)
    display.show_graph(writer, model, images)

    print(
        'Training {} model with a \'{}\' optimization and \'{}\' augmentation over {} epochs'
        .format(Constants.model_str, args.optimizer, args.augmentation,
                args.epochs),
        file=open(
            Path(__file__).parent.parent / "logs" /
            '{}.txt'.format(Constants.save_str), 'a'))

    try:  # run, log training
        for epoch in range(args.epochs):

            with torch.no_grad():  # validation
                validation_acc, validation_loss = training.run_epoch(
                    model,
                    loss_func,
                    valid_dlr,
                    verbose=args.verbose,
                    fast=args.fast)

            # training
            train_acc, train_loss = training.run_epoch(model, loss_func,
                                                       train_dlr, epoch, bar,
                                                       optimizer, args.verbose,
                                                       args.fast)

            display.write_epoch_stats(writer, epoch, validation_acc,
                                      validation_loss, train_acc, train_loss)

    except:  # gracefully handle crashes during training
        training.save_model(model, optimizer, train_loss,
                            "CRASH-{}".format(Constants.save_str), epoch)
        raise Exception(
            'Error on epoch {} of training. Data dumped.'.format(epoch))

    if args.save_model:
        training.save_model(model, optimizer, train_loss, Constants.save_str,
                            args.epochs)

    test_acc, _ = training.run_epoch(model,
                                     loss_func,
                                     test_dlr,
                                     verbose=args.verbose,
                                     fast=args.fast)
    display.print_final_model_stats(train_acc, validation_acc, test_acc)

    writer.close()
Beispiel #8
0
def main(args):

    if args.test:
        print("Testing mode.")
        args.fast = 1
        args.choices = 20
        args.setsize = 1

    if not args.load_model:
        saved_models_path = Path(
            __file__).parent.parent.resolve().parent / 'saved_models'
        model_path = saved_models_path / saved_models_path.iterdir().__next__(
        ).name
    else:
        model_path = Path(
            __file__).parent.parent.resolve().parent / args.load_model
    print("Using evaluator model: {}".format(model_path))

    # load evaluator model
    model = getattr(training.Models, Constants.model_str)
    model, _, epoch, loss = training.load_model(model_path, model)
    loss_func = torch.nn.CrossEntropyLoss()

    GMaxupConsts.save_str = display.build_gmaxup_save_str(args)
    writer = SummaryWriter(
        Path(__file__).parent / '../runs/{}'.format(GMaxupConsts.save_str))

    # dictionary containing statistics on which transformations gmaxup selects
    augment_stats = {}
    for _, _, _, augment_str in augmentations.augment_list(args.range):
        augment_stats[augment_str] = {
            "count": 0,
            "average_loss": 0.0,
            "incorrect_preds": 0,
            "average_mag": 0
        }

    augmented_batch = []

    # initializes loading bar
    dataset_batches = GMaxupConsts.dataset_batches_dict[args.dataset]
    samples_per_batch = args.fast if args.fast else GMaxupConsts.samples_per_batch
    bar = progressbar.ProgressBar(max_value=len(dataset_batches) *
                                  samples_per_batch,
                                  max_error=False)

    # iterates through all batches of the dataset
    for batch_num, curr_batch_str in enumerate(dataset_batches):
        images, labels = data_loading.load_data_by_path(
            Path(__file__).parent.parent.resolve().parent / 'saved_data' /
            args.dataset / curr_batch_str)

        # iterates through all samples in a batch
        for sample_num, (x, y) in enumerate(zip(images, labels)):
            if sample_num < samples_per_batch:  # early exit if -f
                run_gmaxup_on_sample(sample_num, x, y, loss_func, model,
                                     augmented_batch, args, augment_stats,
                                     writer)
                bar.update(batch_num * samples_per_batch + sample_num)

        for key in augment_stats:
            if augment_stats[key]["count"] > 0:
                augment_stats[key]["average_loss"] /= augment_stats[key][
                    "count"]
                augment_stats[key]["average_mag"] /= augment_stats[key][
                    "count"]

        if args.test:
            break

    augmented_cifar10_ds = data_loading.DatasetFromTupleList(augmented_batch)
    with open(
            Path(__file__).parent.parent / "gmaxup_data" /
            GMaxupConsts.save_str, 'wb') as handle:
        pickle.dump(augmented_cifar10_ds,
                    handle,
                    protocol=pickle.HIGHEST_PROTOCOL)

    display.print_augment_stats(augment_stats)
    nEpochs=st.sidebar.slider('Training Epochs', value=35, min_value=1, max_value=100),
    lr=0.001,
    seed=123,
)

st.sidebar.markdown("## Hardware Options")

device_opts = DeviceOptions(
    cuda=st.sidebar.checkbox('Use CUDA', value=False),
    threads=st.sidebar.slider('Dataloader Threads', value=4, min_value=1, max_value=16),
    batchSize=st.sidebar.slider('Training Batch Size', value=4, min_value=1, max_value=256),
    testBatchSize=st.sidebar.slider('Testing Batch Size', value=100, min_value=1, max_value=256),
)

st.code(f"Using: upscale={training_opts.upscale_factor}x, "
        f"nEpochs={training_opts.nEpochs}")

model = load_model(training_opts, device_opts)

input_image = st.file_uploader("Upload an image", ["png", "jpg"], encoding=None)
if input_image is None:
    input_image = 'leon.png'

st.image(open_image(input_image))

st.write('Super Resolution:')
st.image(super_resolve(model, input_image, False))

st.write('Naive Upscale:')
st.image(resize_naive(input_image, training_opts.upscale_factor))
from combatents import AiPlayer
from match import Match
from training import load_model, MatchThread
from tqdm import tqdm

CYCLES = 500
NUM_THREADS = 20
NAMES = ['Carlos', 'Emar']

win_register = {name: 0 for name in NAMES}
win_register[None] = 0
models = [load_model(name) for name in NAMES]

for i in tqdm(range(CYCLES)):
    matches = [
        Match([AiPlayer(NAMES[i], models[i]) for i in range(2)])
        for _ in range(NUM_THREADS)
    ]
    threads = [MatchThread(match) for match in matches]
    for thread in threads:
        thread.start()

    for thread in threads:
        thread.join()

    for match in matches:
        win_register[match.get_winner().name] += 1

for name in NAMES:
    print(f"{name}'s win count: {win_register[name]}.")
Beispiel #11
0
        # prepare image generator
        real_gen = ImageDataGenerator(rescale=None, preprocessing_function=pre)

        # train model
        train(wgan, latent_dim, n_epochs, n_epochs, n_batch, n_blocks,
              real_gen, DATA_DIR, SAVE_DIR, args.dynamic_resize)

    elif mode == 'resume':
        g_model_dir = args.g_model_path
        d_model_dir = args.d_model_path

        # size of the latent space
        latent_dim = 512

        wgan, n_blocks, cur_block = load_model(g_model_dir, d_model_dir,
                                               latent_dim)
        print('loaded')

        # prepare image generator
        real_gen = ImageDataGenerator(rescale=None, preprocessing_function=pre)

        # train model
        if not args.extra_epochs:
            train(wgan, latent_dim, n_epochs, n_epochs, n_batch, int(n_blocks),
                  real_gen, DATA_DIR, SAVE_DIR, args.dynamic_resize,
                  int(cur_block))
        else:
            extra_epochs(wgan, latent_dim, n_epochs, n_epochs, n_batch,
                         int(n_blocks), real_gen, DATA_DIR, SAVE_DIR,
                         args.dynamic_resize, int(cur_block))
            train(wgan, latent_dim, n_epochs, n_epochs, n_batch, int(n_blocks),