Exemple #1
0
def eval_model(path, sc, verbose=2):
    checkpoint = torch.load(path, map_location=lambda storage, loc: storage)
    bidirec = checkpoint['args'].bidirec
    eos = checkpoint['args'].eos
    with open(os.path.join(save_dir_root, checkpoint['args'].vocab),
              'rb') as f:
        vocab = pickle.load(f)

    model = checkpoint['net']
    if args.cuda:
        model.cuda()
    if verbose >= 2:
        print(checkpoint['args'])
        print(model)
    model.eval()

    for idx, row in sc.sentence_df.iterrows():
        losses = []
        for choice_name in sc.choice_names:
            word_tokens = tokenize_(row[choice_name])
            word_ids = [vocab.word_to_id(token) for token in word_tokens]
            Vocabulary.append_eos(word_ids, eos, eos and bidirec)

            total_loss = eval_text(model, word_ids, bidirec=bidirec)
            losses.append(total_loss)

        pred = np.argmin(losses) + 1
        sc.sentence_df.loc[idx, 'pred'] = pred
        if verbose >= 2:
            print(row['No'], row['A'], pred, np.round(losses, 2), sep=' ')

    accuracy = np.mean([sc.sentence_df.A == sc.sentence_df.pred])
    print("{} {:4.2f}%".format(path, 100 * accuracy))
Exemple #2
0
def main(unused_argv):

    vocab = Vocabulary(FLAGS.vocab_path)

    model_path = '/data-private/nas/src/Multi_Model/AE_MSR2019:10:02:01:29:18/ckp30'
    model_name = 'ckp'
    model_config = ModelConfig()
    train_config = TrainingConfig()

    val_dataset = build_dataset(model_config.val_tfrecord_list, batch_size=model_config.batch_size, is_training=False)
    iterator = tf.data.Iterator.from_structure(train_dataset.output_types, train_dataset.output_shapes)
    val_init_op = iterator.make_initializer(val_dataset)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.gpu_options.per_process_gpu_memory_fraction = 0.9
    config.intra_op_parallelism_threads = 24
    config.inter_op_parallelism_threads = 24

    model = Model(model_config=model_config, iterator=iterator, train_config=train_config)

    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())
    vL = tf.trainable_variables()
    r_saver = tf.train.Saver(var_list=vL, max_to_keep=FLAGS.NUM_EPOCH)
    r_saver.restore(sess, model_path)

    print('Model compiled')

    sess.run(val_init_op)
    val_pairs = []
    while True:
        try:
        out_indices, loss1, y = model.eval(sess)
        print('pred: ', out_indices)
        print('ground truth: ', y)
        print('loss: ', loss1)
        val_total_loss += loss1
        print('\n   [%d ]' % (i))
        for j in range(len(y)):
            unpadded_out = None
            if 1 in out_indices[j]:
                idx_1 = np.where(out_indices[j] == 1)[0][0]
                unpadded_out = out_indices[j][:idx_1]
            else:
                unpadded_out = out_indices[j]
                idx_1 = np.where(y[j] == 1)[0][0]
                unpadded_y = y[j][:idx_1]
                predic = ''.join([vocab.id_to_word[k] for k in unpadded_out])
                label = ''.join([vocab.id_to_word[i] for i in unpadded_y])
                val_pairs.append((predic, label))
        except:
            break
        counts, cer = cer_s(val_pairs)
        print('Current error rate is : %.4f' % cer) 
Exemple #3
0
def main(args):
    model_dir = 'attention' + datetime.datetime.now().strftime(
        '%Y:%m:%d:%H:%M:%S')
    model_name = 'ckp'

    vocab = Vocabulary(args['vocab_path'])

    # data_loader = var_len_train_batch_generator(args['data_dir'], args['batch_size'], args['num_threads'])

    model = Model(data_dir=args['data_dir'],
                  word2idx=vocab.word_to_id,
                  depth=args['depth'],
                  img_height=args['height'],
                  img_width=args['weight'],
                  beam_width=args['beam_width'],
                  batch_size=args['batch_size'])

    model.sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    summary_writer = model.summary_writer

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(model.sess, coord)

    print('—*-*-*-*-*-*-*-Model compiled-*-*-*-*-*-*-*-')

    if NUM_TRAIN_SAMPLE % args['batch_size'] == 0:
        num_iteration = NUM_TRAIN_SAMPLE // args['batch_size']
    else:
        num_iteration = NUM_TRAIN_SAMPLE // args['batch_size'] + 1
    with tf.device('/device:GPU:0'):
        for epoch in range(args['num_epochs']):
            print('[Epoch %d] begin ' % epoch)
            for i in tqdm(range(num_iteration)):
                loss = model.train()
                # summary, loss = model.partial_fit()
                print('\n   [%d ] Loss: %.4f' % (i, loss))
                if i % 100 == 0:
                    summary = model.merged_summary()
                    summary_writer.add_summary(summary, i)

            print('[Epoch %d] end ' % epoch)
            cer = model.eval(vocab.id_to_word)
            print('Current cer: %.4f' % cer)
            saver.save(model.sess,
                       os.path.join(model_dir, model_name + str(epoch)))
            summary_writer.close()

    coord.request_stop()
    coord.join(threads)
def main(unused_argv):

    vocab = Vocabulary(FLAGS.vocab_path)

    model_dir = 'MSR' + datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S')
    model_name = 'ckp'
    model_config = ModelConfig()
    train_config = TrainingConfig()

    train_dataset = build_dataset(model_config.train_tfrecord_list, batch_size=model_config.batch_size, shuffle=True)
    val_dataset = build_dataset(model_config.val_tfrecord_list, batch_size=model_config.batch_size, is_training=False)
    iterator = tf.data.Iterator.from_structure(train_dataset.output_types, train_dataset.output_shapes)
    train_init_op = iterator.make_initializer(train_dataset)
    val_init_op = iterator.make_initializer(val_dataset)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.gpu_options.per_process_gpu_memory_fraction = 0.9
    config.intra_op_parallelism_threads = 24
    config.inter_op_parallelism_threads = 24

    model = Model(model_config=model_config, iterator=iterator, train_config=train_config)

    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())

    saver = tf.train.Saver(max_to_keep=FLAGS.NUM_EPOCH)

    summary_writer = tf.summary.FileWriter('logs_no_mh/msr' +
                                                    datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S'),
                                                    graph=sess.graph)

    print('Model compiled')

    count = 0
    for epoch in range(FLAGS.NUM_EPOCH):

        print('[Epoch %d] train begin ' % epoch)
        train_total_loss = 0
        sess.run(train_init_op)
        i = 0
        while True:
            try:

                loss = model.train(sess)
                train_total_loss += loss
                print('\n   [%d ] Loss: %.4f' % (i, loss))
                if count % 100 == 0:
                    train_summary = model.merge(sess)
                    summary_writer.add_summary(train_summary, count)
                count += 1
                i += 1
            except:
                print('break')
                break
        train_loss = train_total_loss / max(i, 1)
        epoch_summary = tf.Summary(value=[tf.Summary.Value(tag="train_loss", simple_value=train_loss)])
        summary_writer.add_summary(epoch_summary, epoch)
        saver.save(sess, os.path.join(model_dir, model_name + str(epoch)))
        print('[Epoch %d] train end ' % epoch)
        print('Epoch %d] eval begin' % epoch)
        val_total_loss = 0
        sess.run(val_init_op)
        val_pairs = []
        i = 0
        if epoch > -1:
            while True:
                try:
                    out_indices,loss1, y = model.eval(sess)
                    #print('pred: ', out_indices)
                    #print('ground truth: ', y)
                    print('loss: ', loss1)
                    val_total_loss += loss1
                    print('\n   [%d ]' % (i))
                    for j in range(len(y)):
                        unpadded_out = None
                        if 1 in out_indices[j]:
                            idx_1 = np.where(out_indices[j] == 1)[0][0]
                            unpadded_out = out_indices[j][:idx_1]
                        else:
                            unpadded_out = out_indices[j]
                        idx_1 = np.where(y[j] == 1)[0][0]
                        unpadded_y = y[j][:idx_1]
                        predic = ''.join([vocab.id_to_word[k] for k in unpadded_out])
                        label = ''.join([vocab.id_to_word[i] for i in unpadded_y])
                        val_pairs.append((predic, label))
                    i += 1
                except:
                    break
            avg_loss = val_total_loss / max(i, 1)
            print("avg_loss",avg_loss)
            counts, cer = cer_s(val_pairs)

            summary = tf.Summary(value=[tf.Summary.Value(tag="cer", simple_value=cer),
                                        tf.Summary.Value(tag="val_loss", simple_value=avg_loss)])
            summary_writer.add_summary(summary, epoch)
            print('Current error rate is : %.4f' % cer)
        print('Epoch %d] eval end' % epoch)

        #############################################################

    summary_writer.close()
Exemple #5
0
logging.debug("running {}".format(__file__))

# Set the random seed manually for reproducibility.
torch.manual_seed(args.seed)
if args.cuda and torch.cuda.is_available():
    torch.cuda.manual_seed(args.seed)

# Use pre built vocabulary if it exists
vocab_path = '{}{}'.format(save_dir_root, args.vocab)
if os.path.exists(vocab_path):
    with open(vocab_path, 'rb') as f:
        vocab = pickle.load(f)
    update = False
else:
    vocab = Vocabulary(args.lower, args.min_cnt)
    update = True

tr_txts = get_txts(args.corpus, 'train')
va_txts = get_txts(args.corpus, 'valid')
if args.bptt > 0:
    tr_input = TextInput(tr_txts, vocab, args.tokenizer, update)
    va_input = TextInput(va_txts, vocab, args.tokenizer, update)
    batchify = (
        lambda mode, bsz, seed:
        (tr_input
         if mode == 'tr' else va_input).batchify(bsz, args.bptt, True, seed))
else:
    eos = args.eos
    bos = args.eos and args.bidirec
    tr_input = LineInput(tr_txts, vocab, args.tokenizer, update, bos, eos,
def main(unused_argv):

    vocab = Vocabulary(FLAGS.vocab_path)
    #vocab.id_to_word['-1'] = -1

    model_dir = 'AVSR_LAB_transformer_1word' + datetime.datetime.now(
    ).strftime('%Y:%m:%d:%H:%M:%S')
    model_name = 'ckp'
    model_config = ModelConfig()
    train_config = TrainingConfig()

    train_dataset = build_dataset(model_config.train_tfrecord_list,
                                  batch_size=model_config.batch_size,
                                  shuffle=True)
    val_dataset = build_dataset(model_config.val_tfrecord_list,
                                batch_size=model_config.batch_size,
                                is_training=False)
    iterator = tf.data.Iterator.from_structure(train_dataset.output_types,
                                               train_dataset.output_shapes)
    train_init_op = iterator.make_initializer(train_dataset)
    val_init_op = iterator.make_initializer(val_dataset)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    config.gpu_options.per_process_gpu_memory_fraction = 0.9
    config.intra_op_parallelism_threads = 24
    config.inter_op_parallelism_threads = 24

    model = Model(model_config=model_config,
                  iterator=iterator,
                  train_config=train_config)

    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())

    saver = tf.train.Saver(max_to_keep=FLAGS.NUM_EPOCH)

    summary_writer = tf.summary.FileWriter(
        'logs_no_mh/asr_transformer' +
        datetime.datetime.now().strftime('%Y:%m:%d:%H:%M:%S'),
        graph=sess.graph)

    print('Model compiled')

    count = 0
    for epoch in range(FLAGS.NUM_EPOCH):

        print('[Epoch %d] train begin ' % epoch)
        train_total_loss = 0
        sess.run(train_init_op)
        i = 0
        while True:
            try:

                loss = model.train(sess)
                train_total_loss += loss
                print('\n   [%d ] Loss: %.4f' % (i, loss))
                if count % 100 == 0:
                    train_summary = model.merge(sess)
                    summary_writer.add_summary(train_summary, count)
                count += 1
                i += 1
            except:
                print('break')
                break
        train_loss = train_total_loss / max(i, 1)
        epoch_summary = tf.Summary(value=[
            tf.Summary.Value(tag="train_loss", simple_value=train_loss)
        ])
        summary_writer.add_summary(epoch_summary, epoch)
        saver.save(sess, os.path.join(model_dir, model_name + str(epoch)))
        print('[Epoch %d] train end ' % epoch)

        #############################################################

    summary_writer.close()