当前位置: 首页>>代码示例>>Python>>正文


Python models.Model方法代码示例

本文整理汇总了Python中models.Model方法的典型用法代码示例。如果您正苦于以下问题:Python models.Model方法的具体用法?Python models.Model怎么用?Python models.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models的用法示例。


在下文中一共展示了models.Model方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: visualize

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def visualize(args):
  saved_path = constant.EXP_ROOT
  model = models.Model(args, constant.ANSWER_NUM_DICT[args.goal])
  model.cuda()
  model.eval()
  model.load_state_dict(torch.load(saved_path + '/' + args.model_id + '_best.pt')["state_dict"])

  label2id = constant.ANS2ID_DICT["open"] 
  visualize = SummaryWriter("../visualize/" + args.model_id)
  # label_list = ["person", "leader", "president", "politician", "organization", "company", "athlete","adult",  "male",  "man", "television_program", "event"]
  label_list = list(label2id.keys())
  ids = [label2id[_] for _ in label_list]
  if args.gcn:
    # connection_matrix = model.decoder.label_matrix + model.decoder.weight * model.decoder.affinity
    connection_matrix = model.decoder.label_matrix + model.decoder.weight * model.decoder.affinity
    label_vectors = model.decoder.transform(connection_matrix.mm(model.decoder.linear.weight) / connection_matrix.sum(1, keepdim=True))
  else:
    label_vectors = model.decoder.linear.weight.data

  interested_vectors = torch.index_select(label_vectors, 0, torch.tensor(ids).to(torch.device("cuda")))
  visualize.add_embedding(interested_vectors, metadata=label_list, label_img=None) 
开发者ID:xwhan,项目名称:Extremely-Fine-Grained-Entity-Typing,代码行数:23,代码来源:main.py

示例2: train

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def train(args):
    """ The main function for doing one iteration training on a input.

    Args:
        args: a Namespace object with the required parameters
            obtained from the function process_args()
    """

    arg_string = pprint.pformat(vars(args))
    logger.info("Arguments for the experiment\n{0}".format(arg_string))

    if args.random_seed != 0:
        np.random.seed(args.random_seed)
    minibatch_count = args.minibatch_count
    minibatch_size = args.minibatch_size
    feature_dim = args.feature_dim

    if args.tar_file == '':
        utt_to_chunks, minibatch_info = process_range_file(args.ranges_file, minibatch_count, minibatch_size)
        if args.sequential_loading:
            train_data, train_labels = load_ranges_info(utt_to_chunks, minibatch_info, minibatch_size, args.scp_file,
                                                        feature_dim)
        else:
            train_data, train_labels = load_ranges_data(utt_to_chunks, minibatch_info, minibatch_size, args.scp_file,
                                                        feature_dim)
        if args.shuffle:
            shuffle_indices = np.random.permutation(np.arange(minibatch_count))
            train_data = train_data[shuffle_indices]
            train_labels = train_labels[shuffle_indices]
        data_loader = DataLoader(train_data, train_labels, args.sequential_loading, queue_size=16)
    else:
        data_loader = TarFileDataLoader(args.tar_file, logger=None, queue_size=16)

    if 'ModelWithoutDropoutReluAdversarial' in args.input_dir:
        model = models.ModelWithoutDropoutReluAdversarial()
        model.train_one_iteration(data_loader, args, logger)
    else:
        model = models.Model()
        model.train_one_iteration(data_loader, args, logger) 
开发者ID:hsn-zeinali,项目名称:x-vector-kaldi-tf,代码行数:41,代码来源:train_dnn_one_iteration.py

示例3: run

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def run():
    # arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-eid', '--experiment_id', type=str)
    parser.add_argument('-d', '--model_directory', type=str, default=MODEL_DIRECTORY)
    parser.add_argument('-dd', '--dataset_directory', type=str, default=DATASET_DIRECTORY)
    parser.add_argument('-cls', '--class_ids', type=str, default=CLASS_IDS_ALL)
    parser.add_argument('-s', '--seed', type=int, default=RANDOM_SEED)
    parser.add_argument('-g', '--gpu', type=int, default=GPU)
    args = parser.parse_args()
    directory_output = os.path.join(args.model_directory, args.experiment_id)

    # set random seed, gpu
    random.seed(args.seed)
    np.random.seed(args.seed)
    cp.random.seed(args.seed)
    chainer.cuda.get_device(args.gpu).use()

    # load dataset
    dataset_test = datasets.ShapeNet(args.dataset_directory, args.class_ids.split(','), 'test')

    # setup model & optimizer
    model = models.Model()
    model.to_gpu()
    chainer.serializers.load_npz(os.path.join(directory_output, 'model.npz'), model)

    # evaluate
    reporter = chainer.Reporter()
    observation = {}
    with reporter.scope(observation):
        training.validation(None, model, dataset_test)
    for key in sorted(observation.keys()):
        key_display = key
        for class_id in CLASS_NAMES.keys():
            key_display = key_display.replace(class_id, CLASS_NAMES[class_id])
        print '%s: %.4f' % (key_display, observation[key]) 
开发者ID:hiroharu-kato,项目名称:mesh_reconstruction,代码行数:38,代码来源:test.py

示例4: eval_dnn

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def eval_dnn(args):
    """ The main function for doing evaluation on a trained network.

    Args:
        args: a Namespace object with the required parameters
            obtained from the function process_args()
    """

    model_dir = args.model_dir
    use_gpu = args.use_gpu == 'yes'
    min_chunk_size = args.min_chunk_size
    chunk_size = args.chunk_size
    # First change the output files temp ones and at the end rename them
    wspecifier, ark, scp = process_wspecifier(args.vector_wspecifier)

    if ark is not None and os.path.exists(ark) and scp is not None and os.path.exists(scp):
        logger.info('Both output ark and scp files exist. Return from this call.')
        return
    model = Model()
    with kaldi_io.open_or_fd(args.feature_rspecifier) as input_fid:
        with kaldi_io.open_or_fd(wspecifier) as output_fid:
            model.make_embedding(input_fid, output_fid, model_dir, min_chunk_size, chunk_size, use_gpu, logger)

    # rename output files
    if ark is not None:
        os.rename(ark + '.tmp.ark', ark)
    # first load scp and correct them to point to renamed ark file.
    if scp is not None:
        with open(scp + '.tmp.scp', 'rt') as fid_in:
            with open(scp + '.tmp', 'wr') as fid_out:
                text = fid_in.read()
                text = text.replace('ark.tmp.ark', 'ark')
                # Sometimes there is no \n at the end of file ank cause a Kaldi error.
                # For preventing this error juts check the last char and append \n if not exist
                if text[-1] != '\n':
                    text += '\n'
                fid_out.write(text)
        os.rename(scp + '.tmp', scp)
        # after create scp file  now we can delete temp file
        # os.remove(scp + '.tmp.scp') 
开发者ID:hsn-zeinali,项目名称:x-vector-kaldi-tf,代码行数:42,代码来源:extract_embedding.py

示例5: eval_dnn

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def eval_dnn(args):
    """ The main function for doing evaluation on a trained network.

    Args:
        args: a Namespace object with the required parameters
            obtained from the function process_args()
    """

    input_dir = args.input_dir
    use_gpu = args.use_gpu == 'yes'
    data_loader = TarFileDataLoader(args.tar_file, logger=None, queue_size=16)
    model = Model()
    model.eval(data_loader, input_dir, use_gpu, logger) 
开发者ID:hsn-zeinali,项目名称:x-vector-kaldi-tf,代码行数:15,代码来源:eval_dnn.py

示例6: _test

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def _test(args):
  assert args.load
  test_fname = args.eval_data
  model = models.Model(args, constant.ANSWER_NUM_DICT[args.goal])
  model.cuda()
  model.eval()
  # load_model(args.reload_model_name, constant.EXP_ROOT, args.model_id, model)

  if args.goal == "onto":
    saved_path = constant.EXP_ROOT_ONTO
  else:
    saved_path = constant.EXP_ROOT
  model.load_state_dict(torch.load(saved_path + '/' + args.model_id + '_best.pt')["state_dict"])
  
  data_gens = get_datasets([(test_fname, 'test', args.goal)], args, eval_epoch=1)
  for name, dataset in [(test_fname, data_gens[0])]:
    print('Processing... ' + name)

    batch = next(dataset)
    eval_batch, _ = to_torch(batch)
    loss, output_logits = model(eval_batch, args.goal)

    threshes = np.arange(0,1,0.02)
    # threshes = [0.65, 0.68, 0.7, 0.71]
    # threshes = [0.5]
    p_and_r = []
    for thresh in tqdm(threshes):
      total_gold_pred = []
      total_probs = []
      total_ys = []
      print('\nthresh {}'.format(thresh))
      output_index = get_output_index(output_logits, thresh)
      output_prob = model.sigmoid_fn(output_logits).data.cpu().clone().numpy()
      y = eval_batch['y'].data.cpu().clone().numpy()
      gold_pred = get_gold_pred_str(output_index, y, args.goal)

      total_probs.extend(output_prob)
      total_ys.extend(y)
      total_gold_pred.extend(gold_pred)
      # mrr_val = mrr(total_probs, total_ys)

      # json.dump(gold_pred, open('nomulti_predictions.json', 'w'))
      # np.save('y', total_ys)
      # np.save('probs', total_probs)

      # print('mrr_value: ', mrr_val)
      # result, eval_str = metric_dicts(total_gold_pred)
      result, eval_str = fine_grained_eval(total_gold_pred)

      # fine_grained_eval(total_gold_pred)

      p_and_r.append([result["ma_precision"], result["ma_recall"]])
      print(eval_str)

    np.save(saved_path + '/{}_pr_else_dev'.format(args.model_id), p_and_r) 
开发者ID:xwhan,项目名称:Extremely-Fine-Grained-Entity-Typing,代码行数:57,代码来源:main.py

示例7: build_model

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def build_model(config, word2id, id2word):
    # ------ Model ------#
    seq2seq = Seq2Seq(
        vocab_size=len(word2id),
        word_embed_size=config.embed_size,
        num_layers=config.num_layers,
        dropout_p=config.dropout,
        enc_hidden_size=config.enc_hidden_size,
        dec_hidden_size=config.dec_hidden_size,
        use_focus=config.use_focus,
        tie=config.weight_tie,
        task=config.task,
        rnn=config.rnn,
        model=config.model,
        feature_rich=config.feature_rich,
        n_mixture=config.n_mixture if config.mixture_decoder else None
    )
    print('Created Seq2Seq!')

    if config.use_focus and not config.eval_focus_oracle:
        selector = FocusSelector(
            word_embed_size=config.embed_size,
            num_layers=config.num_layers,
            enc_hidden_size=config.enc_hidden_size,
            dec_hidden_size=config.dec_hidden_size,
            n_mixture=config.n_mixture,
            dropout_p=0.2,
            rnn=config.rnn,
            seq2seq_model=config.model,
            task=config.task,
            threshold=config.threshold,
            feature_rich=config.feature_rich
        )

        # Selector share all embeddings with Seq2Seq model
        if config.feature_rich:
            selector.add_embedding(
                seq2seq.word_embed,
                seq2seq.answer_position_embed,
                seq2seq.ner_embed,
                seq2seq.pos_embed,
                seq2seq.case_embed)
        else:
            selector.add_embedding(
                seq2seq.word_embed)
        print('Created Focus Selector!')
    else:
        selector = None

    model = Model(seq2seq, selector)

    model.word2id = word2id
    model.id2word = id2word

    print(model)
    return model 
开发者ID:clovaai,项目名称:FocusSeq2Seq,代码行数:58,代码来源:build_utils.py

示例8: run

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def run():
    # arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-eid', '--experiment_id', type=str)
    parser.add_argument('-d', '--directory', type=str, default=DIRECTORY)
    parser.add_argument('-i', '--input_image', type=str)
    parser.add_argument('-oi', '--output_image', type=str)
    parser.add_argument('-oo', '--output_obj', type=str)
    parser.add_argument('-s', '--seed', type=int, default=RANDOM_SEED)
    parser.add_argument('-g', '--gpu', type=int, default=GPU)
    args = parser.parse_args()
    directory_output = os.path.join(args.directory, args.experiment_id)

    # set random seed, gpu
    random.seed(args.seed)
    np.random.seed(args.seed)
    cp.random.seed(args.seed)
    chainer.cuda.get_device(args.gpu).use()

    # load dataset
    image_in = skimage.io.imread(args.input_image).astype('float32') / 255
    if image_in.ndim != 3 or image_in.shape[-1] != 4:
        raise Exception('Input must be a RGBA image.')
    images_in = image_in.transpose((2, 0, 1))[None, :, :, :]
    images_in = chainer.cuda.to_gpu(images_in)

    # setup model & optimizer
    model = models.Model()
    model.to_gpu()
    chainer.serializers.load_npz(os.path.join(directory_output, 'model.npz'), model)

    # reconstruct .obj
    vertices, faces = model.reconstruct(images_in)
    neural_renderer.save_obj(args.output_obj, vertices.data.get()[0], faces.get()[0])

    # render reconstructed shape
    ones = chainer.cuda.to_gpu(np.ones((16,), 'float32'))
    distances = 2.732 * ones
    elevations = 30. * ones
    azimuths = chainer.cuda.to_gpu(np.arange(0, 360, 360. / 16.).astype('float32')) * ones
    viewpoints = neural_renderer.get_points_from_angles(distances, elevations, azimuths)
    images_out = model.reconstruct_and_render(chainer.functions.tile(images_in, (16, 1, 1, 1)), viewpoints)
    image_out = tile_images(images_out.data.get())
    image_out = (image_out * 255).clip(0, 255).astype('uint8')
    skimage.io.imsave(args.output_image, image_out) 
开发者ID:hiroharu-kato,项目名称:mesh_reconstruction,代码行数:47,代码来源:reconstruct.py

示例9: run

# 需要导入模块: import models [as 别名]
# 或者: from models import Model [as 别名]
def run():
    # arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-eid', '--experiment_id', type=str)
    parser.add_argument('-d', '--model_directory', type=str, default=MODEL_DIRECTORY)
    parser.add_argument('-dd', '--dataset_directory', type=str, default=DATASET_DIRECTORY)
    parser.add_argument('-cls', '--class_ids', type=str, default=CLASS_IDS_ALL)
    parser.add_argument('-bs', '--batch_size', type=int, default=BATCH_SIZE)
    parser.add_argument('-ls', '--lambda_smoothness', type=float, default=LAMBDA_SMOOTHNESS)
    parser.add_argument('-lr', '--learning_rate', type=float, default=LEARNING_RATE)
    parser.add_argument('-lrp', '--lr_reduce_point', type=float, default=LR_REDUCE_POINT)
    parser.add_argument('-ni', '--num_iterations', type=int, default=NUM_ITERATIONS)
    parser.add_argument('-li', '--log_interval', type=int, default=LOG_INTERVAL)
    parser.add_argument('-s', '--seed', type=int, default=RANDOM_SEED)
    parser.add_argument('-g', '--gpu', type=int, default=GPU)
    args = parser.parse_args()
    directory_output = os.path.join(args.model_directory, args.experiment_id)

    # set random seed, gpu
    random.seed(args.seed)
    np.random.seed(args.seed)
    cp.random.seed(args.seed)
    chainer.cuda.get_device(args.gpu).use()

    # load dataset
    dataset_train = datasets.ShapeNet(args.dataset_directory, args.class_ids.split(','), 'train')
    dataset_val = datasets.ShapeNet(args.dataset_directory, args.class_ids.split(','), 'val')
    train_iter = training.MyIterator(dataset_train, args.batch_size)

    # setup model & optimizer
    model = models.Model(lambda_smoothness=args.lambda_smoothness)
    model.to_gpu()
    optimizer = neural_renderer.Adam(args.learning_rate)
    optimizer.setup(model)

    # setup trainer
    updater = chainer.training.StandardUpdater(train_iter, optimizer, converter=training.my_convertor)
    trainer = chainer.training.Trainer(updater, stop_trigger=(args.num_iterations, 'iteration'), out=directory_output)
    trainer.extend(chainer.training.extensions.LogReport(trigger=(args.log_interval, 'iteration')))
    trainer.extend(chainer.training.extensions.PrintReport(
        ['iteration', 'main/loss_silhouettes', 'main/loss_smoothness', 'val/iou', 'elapsed_time']))
    trainer.extend(chainer.training.extensions.ProgressBar(update_interval=10))
    trainer.extend(
        functools.partial(training.validation, model=model, dataset=dataset_val),
        name='validation',
        priority=chainer.training.PRIORITY_WRITER,
        trigger=(args.log_interval, 'iteration'))
    trainer.extend(
        functools.partial(
            training.lr_shift, optimizer=optimizer, iterations=[args.num_iterations * args.lr_reduce_point]),
        name='lr_shift',
        trigger=(1, 'iteration'))

    # main loop
    trainer.run()

    # save model
    chainer.serializers.save_npz(os.path.join(directory_output, 'model.npz'), model) 
开发者ID:hiroharu-kato,项目名称:mesh_reconstruction,代码行数:60,代码来源:train.py


注:本文中的models.Model方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。