예제 #1
0
def build_sampler_env(load_dir,
                      batch_size=64,
                      enc_seq_len=64,
                      dec_seq_len=201):
    enc_g = tf.Graph()
    with enc_g.as_default():
        with tf.device("/cpu:0"):
            enc_sess = tf.Session()
            enc_model = load_model(load_dir,
                                   enc_sess,
                                   False,
                                   decoding=False,
                                   seq_length=enc_seq_len,
                                   batch_size=batch_size)

    dec_g = tf.Graph()
    with dec_g.as_default():
        dec_sess = tf.Session()
        dec_model = load_model(load_dir,
                               dec_sess,
                               False,
                               decoding=True,
                               seq_length=dec_seq_len,
                               batch_size=batch_size)
    return enc_model, enc_sess, enc_g, dec_model, dec_sess, dec_g
예제 #2
0
def get_discriminator(opt, input_size, checkpoint=None):
    discriminator = d_models[opt.model.d_arch](input_size=input_size)

    if checkpoint is not None and 'discriminator' in checkpoint:
        load_model(discriminator, checkpoint['discriminator'])

    if len(opt.run.gpus) > 1:
        discriminator = torch.nn.DataParallel(discriminator).cuda()
    elif len(opt.run.gpus) > 0:
        discriminator = discriminator.cuda()

    return discriminator
예제 #3
0
def test_hidden(args):
    with tf.Session() as sess:
        model, chars, vocab = load_model(args.save_dir, sess, training=False)

        hidden_norms = model.get_hidden_l2_norm(sess, vocab, args.phrase)
        for h_norm, char in zip(hidden_norms, args.phrase):
            print(np.squeeze(h_norm), char)
예제 #4
0
def sample(args):
    with tf.Session() as sess:
        model = load_model(args.save_dir, sess, training=False)
        transformer = load_transformer(args.save_dir)
        chars = transformer.tokens
        vocab = transformer.vocab
        for i in range(args.n_samples):
            print('sample ', i)
            print(model.sample(sess, chars, vocab, args.n, args.prime, args.sample)) # .encode('utf-8'))
예제 #5
0
def get_teacher_student_models(opt, checkpoint=None):
    # Create teacher model
    teacher = t_models[opt.model.t_arch](num_outputs=opt.model.n_classes) if opt.model.n_classes is not None else \
              t_models[opt.model.t_arch]()
    num_outputs = teacher.num_outputs

    # Freeze teacher weights
    for param in teacher.parameters():
        param.requires_grad = False

    # Create student model
    teacher_features_size = get_last_features_size(teacher)
    # features_size argument is used to add extra layer(s) for adjusting student final feature size to the same size
    # as teacher. If this extra layer not required, it can be removed later by calling remove_adjust_features function.
    student = s_models[opt.model.s_arch](num_outputs=num_outputs,
                                         features_size=teacher_features_size)

    if checkpoint is None:
        checkpoint = dict()
        if opt.model.t_pretrain_path is not None:
            checkpoint['teacher'] = torch.load(opt.model.t_pretrain_path)
        if opt.model.s_pretrain_path is not None:
            checkpoint['student'] = torch.load(opt.model.s_pretrain_path)

    if 'teacher' in checkpoint:
        load_model(teacher, checkpoint['teacher'])
    if 'student' in checkpoint:
        load_model(student, checkpoint['student'])

    if len(opt.run.gpus) > 1:
        teacher = torch.nn.DataParallel(teacher).cuda()
        student = torch.nn.DataParallel(student).cuda()
    elif len(opt.run.gpus) > 0:
        teacher = teacher.cuda()
        student = student.cuda()

    return teacher, student
예제 #6
0
def rebuild_phrase(args):
    with tf.Session() as sess:
        model, chars, vocab = load_model(args.save_dir, sess, training=False)

        words = args.phrase.split(args.stop_sym)
        prime = args.prime + args.stop_sym
        rez = words[0] + args.stop_sym
        for word in words:
            prime = prime + word + args.stop_sym
            new_phrase = model.sample(sess, chars, vocab, args.n, prime, args.sample, args.stop_sym)
            new_word = new_phrase.split(args.stop_sym)[-1]
            rez += new_word + args.stop_sym
        print(prime)
        print()
        print()
        print(rez)
예제 #7
0
    float_model_file = 'mobilenet_pretrained_float.pth'

    train_batch_size = 30
    eval_batch_size = 10
    num_eval_batches = 100
    num_train_batches = 10
    num_calibration_batches = 10

    DEBUG_FLOAT_BENCHMARK = True
    DEBUG_DYNAMIC = True
    DEBUG_DYNAMIC_x86 = True
    DEBUG_QAT = True

    data_loader, data_loader_test = prepare_data_loaders(data_path)
    criterion = nn.CrossEntropyLoss()
    float_model = load_model(saved_model_dir + float_model_file)

    if DEBUG_FLOAT_BENCHMARK:
        float_model_scripted = savemodel_scripted(
            float_model,
            saved_model_dir + 'mobilenet_pretrained_float_scripted.pth')
        print_model_stats(float_model_scripted,
                          criterion,
                          data_loader_test,
                          num_eval_batches=num_eval_batches,
                          eval_batch_size=eval_batch_size)

    # dynamic
    if DEBUG_DYNAMIC:
        modelPostDynamic = quantization_post_dynamic(
            saved_model_dir + float_model_file, criterion, data_loader,
예제 #8
0
save_to = './data/merged_sampled.json'
model_path = './models/shm_c3'
loops = 25

with open(file_name, 'r', encoding='utf-8') as f:
    text = f.read()

sentences = text.split('\n')
batch_size = 325
batches = []
for i in range(len(sentences)//batch_size):
    batch = sentences[i*batch_size:(i+1)*batch_size]
    batches.append(batch)

sess = tf.Session()
model = load_model(model_path, sess, training=False, decoding=True, seq_length=64, batch_size=batch_size)
transformer = load_transformer(model_path)

states = []
print('calculating states')
for i, batch in enumerate(batches):
    states.append(model.calculate_states(sess, transformer, phrases=batch))
    print(i+1, '/', len(batches),  end='\r')

# dictionary = load_dictionary("./data/")

dictionary = None

g = tf.Graph()
with g.as_default():
    sess_sample = tf.Session()