예제 #1
0
def main():
    # avoid printing TF debugging information
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    # TODO: allow parameters from command line or leave everything in json files?
    hp, evaluation, run, env, design = parse_arguments()
    # Set size for use with tf.image.resize_images with align_corners=True.
    # For example,
    #   [1 4 7] =>   [1 2 3 4 5 6 7]    (length 3*(3-1)+1)
    # instead of
    # [1 4 7] => [1 1 2 3 4 5 6 7 7]  (length 3*3)
    final_score_sz = hp.response_up * (design.score_sz - 1) + 1
    # build TF graph once for all
    #filename, image, templates_z, scores = siam.build_tracking_graph(final_score_sz, design, env)

    siamNet = siam.Siamese(design.batch_size)
    image, z_crops, x_crops, templates_z, scores, loss, train_step, distance_to_gt, summary, tz, max_pos_x, max_pos_y = siamNet.build_tracking_graph_train(
        final_score_sz, design, env, hp)

    batched_data = read_tfrecord(os.path.join(env.tfrecord_path,
                                              env.tfrecord_filename),
                                 num_epochs=design.num_epochs,
                                 batch_size=design.batch_size)

    trainer(hp, run, design, final_score_sz, image, templates_z, scores, loss,
            train_step, distance_to_gt, batched_data, z_crops, x_crops,
            siamNet, summary, tz, max_pos_x, max_pos_y)
def main():
    # avoid printing TF debugging information
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    # TODO: allow parameters from command line or leave everything in json files?
    hp, evaluation, run, env, design = parse_arguments()
    # Set size for use with tf.image.resize_images with align_corners=True.
    # For example,
    #   [1 4 7] =>   [1 2 3 4 5 6 7]    (length 3*(3-1)+1)
    # instead of
    # [1 4 7] => [1 1 2 3 4 5 6 7 7]  (length 3*3)
    final_score_sz = hp.response_up * (design.score_sz - 1) + 1

    # build the computational graph of Siamese fully-convolutional network
    siamNet = siam.Siamese(design.batch_size)
    # get tensors that will be used during training
    image, z_crops, x_crops, templates_z, scores, loss, train_step, distance_to_gt, summary = siamNet.build_tracking_graph_train(
        final_score_sz, design, env, hp)

    # read tfrecodfile holding all the training data
    data_reader = src.read_training_dataset.myReader(design.resize_width,
                                                     design.resize_height,
                                                     design.channel)
    batched_data = data_reader.read_tfrecord(os.path.join(
        env.tfrecord_path, env.tfrecord_filename),
                                             num_epochs=design.num_epochs,
                                             batch_size=design.batch_size)

    # run trainer
    trainer(hp, run, design, final_score_sz, batched_data, image, templates_z,
            scores, loss, train_step, distance_to_gt, z_crops, x_crops,
            siamNet, summary)
예제 #3
0
def semi_train(task_name,sed_model_name,at_model_name,augmentation):
	""""
	Training with semi-supervised learning (Guiding learning)
	Args:
		task_name: string
			the name of the task
                sed_model_name: string
			the name of the the PS-model
		at_model_name: string
			the name of the the PT-model
                augmentation: bool
			whether to add Gaussian noise to the input of the PT-model
	Return:

        """
	#prepare for training of the PS-model
	LOG.info('config preparation for %s'%at_model_name)
	train_sed=trainer.trainer(task_name,sed_model_name,False)

	#prepare for training of the PT-model
	LOG.info('config preparation for %s'%sed_model_name)
	train_at=trainer.trainer(task_name,at_model_name,False)

	#connect the outputs of the two models to produce a model for end-to-end learning
	creat_model_at=train_at.model_struct.graph()
	creat_model_sed=train_sed.model_struct.graph()
	LEN=train_sed.data_loader.LEN
	DIM=train_sed.data_loader.DIM	
	inputs=Input((LEN,DIM))

	#add Gaussian noise
	if augmentation:
		at_inputs=GaussianNoise(0.15)(inputs)
	else:
		at_inputs=inputs

	at_out=creat_model_at(at_inputs,False)
	sed_out=creat_model_sed(inputs,False)
	out=concatenate([at_out,sed_out],axis=-1)
	models=Model(inputs,out)

	#start training (all intermediate files are saved in the PS-model dir)
	LOG.info('------------start training------------')	
	train_sed.train(models)

	#copy the final model to the PT-model dir from the PS-model dir
	shutil.copyfile(train_sed.best_model_path,train_at.best_model_path) 

	#predict results for validation set and test set (the PT-model)
	LOG.info('------------result of %s------------'%at_model_name)
	train_at.save_at_result()	#audio tagging result

	#predict results for validation set and test set (the PS-model)
	LOG.info('------------result of %s------------'%sed_model_name)
	train_sed.save_at_result()	#audio tagging result
	train_sed.save_sed_result()	#event detection result
예제 #4
0
def test(task_name, model_name, model_path = None, at_preds={}, sed_preds={}):
	""""
	Test with prepared model dir.
	The format of the model dir must be consistent with the required format.
	Args:
		task_name: string
			the name of the task
		model_name: string
			the name of the model
		model_path: string
			the path of model weights (if None, set defaults)
		at_preds: dict
		sed_preds: dict

	Return:
		at_preds: dict
			{'vali': numpy.array, 'test': numpy.array}
			audio tagging prediction (possibilities) on both set
		sed_preds: dict
			{'vali': numpy.array, 'test': numpy.array}
			detection prediction (possibilities) on both set
	
        """
	#prepare for testing
	train = trainer.trainer(task_name,model_name,True)
	if not model_path == None:
		train.best_model_path = model_path
	#predict results for validation set and test set
	at_preds_out = train.save_at_result(at_preds)	#audio tagging result
	sed_preds_out = train.save_sed_result(sed_preds)	#event detection result
	return at_preds_out, sed_preds_out
예제 #5
0
def main():

    # add arguments
    parser = argparse.ArgumentParser()

    parser.add_argument("--resume_id", type=str, default="")

    parser.add_argument("--data_dir", type=str, required=True)
    parser.add_argument("--generate_every", type=int, default=1)
    parser.add_argument("--epochs", type=int, default=50)
    parser.add_argument("--res", type=int, default=32)
    parser.add_argument("--rotate", action="store_true")

    # loggings parameters
    parser.add_argument("--logs",
                        type=str,
                        default=None,
                        help="logs by tensorboardX")
    parser.add_argument("--local_test",
                        type=str2bool,
                        default=False,
                        help="local test verbose")
    parser.add_argument("--model_name",
                        type=str,
                        default="dcgan",
                        help="model name for saving")
    parser.add_argument("--test",
                        type=str2bool,
                        default=False,
                        help="call tester.py")
    parser.add_argument("--use_visdom",
                        type=str2bool,
                        default=False,
                        help="visualization by visdom")
    args = parser.parse_args()

    # list params
    # params.print_params()

    # run program
    if args.test == False:
        trainer(args)
    else:
        tester(args)
예제 #6
0
def test(task_name,model_name):
	""""
	Test with prepared model dir.
	The format of the model dir must be consistent with the required format.
	Args:
		task_name: string
			the name of the task
		model_name: string
			the name of the model
	Return:
	
        """
	#prepare for testing
	train=trainer.trainer(task_name,model_name,True)

	#predict results for validation set and test set
	train.save_at_result()	#audio tagging result
	train.save_sed_result()	#event detection result
예제 #7
0
def supervised_train(task_name,sed_model_name,augmentation):
	""""
	Training with only weakly-supervised learning
	Args:
		task_name: string
			the name of the task
		sed_model_name:	string
			the name of the model
		augmentation:	bool
			whether to add Gaussian noise Layer
	Return:

	"""
	LOG.info('config preparation for %s'%sed_model_name)
	#prepare for training
	train_sed=trainer.trainer(task_name,sed_model_name,False)
	
	#creat model using the model structure prepared in [train_sed]
	creat_model_sed=train_sed.model_struct.graph()
	LEN=train_sed.data_loader.LEN
	DIM=train_sed.data_loader.DIM
	inputs=Input((LEN,DIM))

	#add Gaussian noise Layer
	if augmentation:
		inputs_t=GaussianNoise(0.15)(inputs)
	else:
		inputs_t=inputs
	outs=creat_model_sed(inputs_t,False)

	#the model used for training
	models=Model(inputs,outs)

	LOG.info('------------start training------------')
	train_sed.train(extra_model=models,train_mode='supervised')

	#predict results for validation set and test set
	train_sed.save_at_result()	#audio tagging result
	train_sed.save_sed_result()	#event detection result
예제 #8
0
    print(accuracy_score(test_y, clf_t_predict))
    x3 = clf_t.predict(trt)

    clf_s = DecisionTreeClassifier(criterion='entropy', min_samples_leaf=3)
    clf_s.fit(trs, train_y)
    clf_s_predict = clf_s.predict(tes)
    print("Only Summary with Tree Classifier(Accuracy):")
    print(accuracy_score(test_y, clf_t_predict))
    x4 = clf_s.predict(trs)

    print("\t\t\t------------------------------------------------\t\t\t")
    print("\t\t\t\t\tDNN Training\t\t\t")
    print("\t\t\t------------------------------------------------\t\t\t")

    # for title
    th_title, model_title = sr.trainer(train_x=trt, train_y=train_ri.labels)
    print("Only Title with RNN-Trainer(accuracy):")
    pt = sr.judge(model=model_title, test_x=tet, test_y=test_ri.labels)
    # # for summary
    th_summary, model_summary = sr.trainer(train_x=trs, train_y=train_ri.labels)
    print("Only Summary with RNN-Trainer(accuracy):")
    ps = sr.judge(model=model_summary, test_x=tes, test_y=test_ri.labels)

    #  Use LR as ensemble method.
    x1 = model_title.predict_classes(trt)
    x2 = model_summary.predict_classes(trs)
    ensemble_model = LogisticRegression()

    esb_x = [[xx1[0], xx2[0]] for (xx1, xx2) in zip(x1, x2)]

    ensemble_model.fit(esb_x, train_ri.labels)
예제 #9
0
    configs = {
        'n_epochs': args.n_epochs,
        'word_embed_dim': args.word_embed_dim,
        'char_embed_dim': args.char_embed_dim,
        'char_hidden_dim': args.char_hidden_dim,
        'word_hidden_dim': args.word_hidden_dim,
        'optimizer': args.optimizer,
        'lr': args.lr,
        'use_gpu': use_gpu,
        'save_model': args.save_model
    }

    if not args.folder:
        if args.language == 'all':
            for lang in languages:
                trainer(languages[lang], configs)
        else:
            if args.language not in list(languages.keys()):
                raise ValueError(f'language {args.language} not found')
            trainer(languages[args.language], configs)
    else:
        if args.language == 'all':
            raise ValueError(
                'Cannot train all language with designated folder. '
                'Please remove --folder arguments to train all languages')
        path = root_path() / 'data' / 'ud-treebanks-v2.3' / args.folder
        if not path.exists():
            raise ValueError('Folder not found')
        lang_dataset = LanguageDataset(args.language, path)
        trainer(lang_dataset, configs)
예제 #10
0
        transforms.RandomCrop(imsize),
        transforms.RandomHorizontalFlip()
    ])
    dataset = TextDataset(cfg.DATA_DIR,
                          split_dir,
                          base_size=cfg.TREE.BASE_SIZE,
                          transform=image_transform)
    assert dataset
    dataloader = torch.utils.data.DataLoader(dataset,
                                             batch_size=cfg.TRAIN.BATCH_SIZE,
                                             drop_last=True,
                                             shuffle=bshuffle,
                                             num_workers=int(cfg.WORKERS))

    # Define models and go to train/evaluate
    algo = trainer(output_dir, dataloader, dataset.n_words, dataset.ixtoword)

    start_t = time.time()
    if cfg.TRAIN.FLAG:
        algo.train()
    else:
        '''generate images from pre-extracted embeddings'''
        if cfg.B_VALIDATION:
            algo.sampling(
                split_dir)  # generate images for the whole valid dataset
        else:
            gen_example(dataset.wordtoix,
                        algo)  # generate images for customized captions
    end_t = time.time()
    LOGGER.info(f'Total time for training: {end_t - start_t}')
예제 #11
0
def main(_):
    FLAGS = tf.app.flags.FLAGS

    # ========================================= parameter part begins ========================================== #
    Dx = FLAGS.Dx
    print_freq = FLAGS.print_freq

    # evaluation parameters
    if FLAGS.g_dist_type in ["poisson", "multinomial", "multinomial_compose"]:
        y_hat_bar_plot_to_normalize = True
    else:
        raise ValueError("Unsupported emission!")

    FLAGS.epochs = [int(epoch) for epoch in FLAGS.epochs.split(",")]

    tf.set_random_seed(FLAGS.seed)
    np.random.seed(FLAGS.seed)

    # ============================================= dataset part ============================================= #
    # generate data from simulation
    repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    data_dir = DATA_DIR_DICT[FLAGS.data_type]
    data_dir = os.path.join(repo_dir, data_dir)
    if FLAGS.interpolation_type == 'none':
        FLAGS.interpolation_type = None

    hidden_train, hidden_test, obs_train, obs_test, input_train, input_test, theta, params = \
        load_data(data_dir, train_num=FLAGS.train_num, test_num=FLAGS.test_num)

    FLAGS.n_train, FLAGS.n_test = n_train, n_test = len(obs_train), len(obs_test)

    hidden_train, hidden_test, obs_train, obs_test, input_train, input_test, \
        mask_train, mask_test, time_interval_train, time_interval_test = \
        interpolate_data(hidden_train, hidden_test,
                         obs_train, obs_test,
                         input_train, input_test,
                         interpolation_type=FLAGS.interpolation_type,
                         pseudo_count=FLAGS.pseudo_count)

    # clip saving_test_num to avoid it > n_train or n_test
    min_time_train = min([obs.shape[0] for obs in obs_train])
    min_time_test = min([obs.shape[0] for obs in obs_test])
    min_time = min(min_time_train, min_time_test)
    FLAGS.MSE_steps = min(FLAGS.MSE_steps, min_time - 2)
    FLAGS.saving_train_num = min(FLAGS.saving_train_num, n_train)
    FLAGS.saving_test_num = min(FLAGS.saving_test_num, n_test)

    print("finished preparing dataset")

    # ============================================== model part ============================================== #
    SSM_model = SSM(FLAGS, theta)

    # at most one of them can be set to True
    assert FLAGS.PSVO + FLAGS.SVO + FLAGS.AESMC + FLAGS.IWAE < 2

    # SMC class to calculate loss
    if FLAGS.PSVO:
        SMC_train = PSVO(SSM_model, FLAGS)
    elif FLAGS.SVO:
        SMC_train = SVO(SSM_model, FLAGS)
    elif FLAGS.AESMC:
        SMC_train = AESMC(SSM_model, FLAGS)
    elif FLAGS.IWAE:
        SMC_train = IWAE(SSM_model, FLAGS)
    else:
        raise ValueError("Choose one of objectives among: PSVO, SVO, AESMC, IWAE")

    # =========================================== data saving part =========================================== #
    # create dir to save results
    Experiment_params = {"n_train":       n_train,
                         "n_test":        n_test,
                         "np":            FLAGS.n_particles,
                         "lr":            FLAGS.lr,
                         "epochs":        FLAGS.epochs,
                         "seed":          FLAGS.seed,
                         "rslt_dir_name": FLAGS.rslt_dir_name}

    RLT_DIR = create_RLT_DIR(Experiment_params)
    save_experiment_param(RLT_DIR, FLAGS)
    print("checkpoint_dir:", RLT_DIR)

    # ============================================= training part ============================================ #
    mytrainer = trainer(SSM_model, SMC_train, FLAGS)
    mytrainer.set_data_saving(RLT_DIR)
    mytrainer.init_train(obs_train, obs_test, input_train, input_test, mask_train, mask_test,
                         time_interval_train, time_interval_test)

    plot_start_idx, plot_start_epoch = 0, 0
    cum_epoch = 0
    for checkpoint_idx, epoch in enumerate(FLAGS.epochs):
        cum_epoch += epoch
        print("\n\nStart training {}...".format(checkpoint_idx))

        checkpoint_dir = RLT_DIR + "checkpoint_{}/".format(checkpoint_idx)
        print("Creating checkpoint_{} directory...".format(checkpoint_idx))
        os.makedirs(checkpoint_dir)

        mytrainer.set_saving_dir(checkpoint_dir)

        history, log = mytrainer.train(print_freq, epoch)

        # ======================================== data saving part ======================================== #

        with open(checkpoint_dir + "history.json", "w") as f:
            json.dump(history, f, indent=4, cls=NumpyEncoder)

        Xs, y_hat = log["Xs_resampled"], log["y_hat"]
        Xs_train, y_hat_train = mytrainer.evaluate([Xs, y_hat], mytrainer.train_feed_dict)
        Xs_test, y_hat_test = mytrainer.evaluate([Xs, y_hat], mytrainer.test_feed_dict)

        learned_model_dict = {"Xs_test": Xs_test, "y_hat_val_test": y_hat_test.copy()}

        plot_y_hat_bar_plot(checkpoint_dir + "y_hat_train_bar_plots", y_hat_train, obs_train, mask=mask_train,
                            saving_num=FLAGS.saving_train_num, to_normalize=y_hat_bar_plot_to_normalize)
        plot_y_hat_bar_plot(checkpoint_dir + "y_hat_test_bar_plots", y_hat_test, obs_test, mask=mask_test,
                            saving_num=FLAGS.saving_test_num, to_normalize=y_hat_bar_plot_to_normalize)

        testing_data_dict = {"hidden_test": hidden_test[0:FLAGS.saving_test_num],
                             "obs_test": obs_test[0:FLAGS.saving_test_num],
                             "input_test": input_test[0:FLAGS.saving_test_num]}

        if FLAGS.f_tran_type in ["ilr_clv", "ilr_clv_taxon"]:
            f_tran_params = mytrainer.sess.run(SSM_model.f_tran.params,
                                               {SSM_model.training: False,
                                                SSM_model.annealing_frac: cum_epoch / np.sum(FLAGS.epochs)})
            with open(data_dir, "rb") as f:
                data = pickle.load(f)
            plot_interaction_matrix(checkpoint_dir + "interaction", f_tran_params, data)
        else:
            f_tran_params = None

        data_dict = {"testing_data_dict": testing_data_dict,
                     "learned_model_dict": learned_model_dict,
                     "f_tran_params": f_tran_params}

        with open(checkpoint_dir + "data.p", "wb") as f:
            pickle.dump(data_dict, f)

        plot_log_ZSMC(checkpoint_dir, history["log_ZSMC_trains"][plot_start_idx:],
                      history["log_ZSMC_tests"][plot_start_idx:], plot_start_epoch, print_freq)
        plot_start_idx += int(epoch / print_freq) + 1
        plot_start_epoch += epoch

        y_hat_train, y_train = mytrainer.evaluate([mytrainer.y_hat_N_BxTxDy, mytrainer.y_N_BxTxDy],
                                                  mytrainer.train_all_feed_dict)
        y_hat_test, y_test = mytrainer.evaluate([mytrainer.y_hat_N_BxTxDy, mytrainer.y_N_BxTxDy],
                                                mytrainer.test_all_feed_dict)
        with open(os.path.join(checkpoint_dir, "y_hat.p"), "wb") as f:
            pickle.dump({"y_hat_train": y_hat_train, "y_train": y_train,
                         "y_hat_test": y_hat_test, "y_test": y_test},
                        f)

        print("finish plotting!")
예제 #12
0
                                   }

    model = getattr(models, args.model)(embeddings, args.hidden_size, tag_dic,
                                        device=device,
                                        use_crf=args.use_crf,
                                        relearn_embeddings=args.relearn_embeddings,
                                        word_dropout=args.word_dropout,
                                        locked_dropout=args.locked_dropout,
                                        sort=args.sort,
                                        lambdas_generator=args.lambdas_generator,
                                        lambdas_generator_params=lambdas_generator_params,
                                        tag_name=args.tag_name,
                                        cell_type=args.cell_type,
                                        bidirectional=args.bidirectional,)

    model_trainer = trainer.trainer(model, corpus, SGD, args.base_path)

    history = model_trainer.train(args.epochs,
                                  n_passes=args.n_passes,
                                  train_with_dev=args.train_with_dev,
                                  mixup_training=not(args.model == "Normal"),
                                  embedding_storage_mode=args.embeddings_storage_mode,
                                  batch_size=args.batch_size,
                                  shuffle=not args.no_shuffle,
                                  learning_rate=args.learning_rate,
                                  learning_rate_decay=args.learning_rate_decay,
                                  learning_rate_min=args.learning_rate_min,
                                  patience=args.patience,
                                  step_size=args.step_size,
                                  monitor_train=args.monitor_train,
                                  monitor_dev=args.monitor_dev,