コード例 #1
0
ファイル: train.py プロジェクト: chinatian/glow
def infer(sess, model, hps, iterator):
    # Example of using model in inference mode. Load saved model using hps.restore_path
    # Can provide x, y from files instead of dataset iterator
    # If model is uncondtional, always pass y = np.zeros([bs], dtype=np.int32)
    if hps.direct_iterator:
        iterator = iterator.get_next()

    xs = []
    zs = []
    for it in range(hps.full_test_its):
        if hps.direct_iterator:
            # replace with x, y, attr if you're getting CelebA attributes, also modify get_data
            x, y = sess.run(iterator)
        else:
            x, y = iterator()

        z = model.encode(x, y)
        x = model.decode(y, z)
        xs.append(x)
        zs.append(z)

    x = np.concatenate(xs, axis=0)
    z = np.concatenate(zs, axis=0)
    np.save('logs/x.npy', x)
    np.save('logs/z.npy', z)
    return zs
コード例 #2
0
def infer(sess, model, hps, iterator):
    # Example of using model in inference mode. Load saved model using hps.restore_path
    # Can provide x, y from files instead of dataset iterator
    # If model is uncondtional, always pass y = np.zeros([bs], dtype=np.int32)
    if hps.direct_iterator:
        iterator = iterator.get_next()

    xs = []
    zs = []
    for it in range(hps.full_test_its):
        if hps.direct_iterator:
            # replace with x, y, attr if you're getting CelebA attributes, also modify get_data
            x, y = sess.run(iterator)
        else:
            x, y = iterator()

        z = model.encode(x, y)
        x = model.decode(y, z)
        xs.append(x)
        zs.append(z)

    x = np.concatenate(xs, axis=0)
    z = np.concatenate(zs, axis=0)
    np.save('logs/x.npy', x)
    np.save('logs/z.npy', z)
    return zs
コード例 #3
0
def run_one_epoch(data_type, dataloader, trainer, epoch, run_type, collector=None):
	t0 = time.time()
	assert data_type in ['dev', 'test']
	assert run_type in ['teacher_force', 'generation']
	model, optimizer, scheduler, tokenizer = trainer

	LOSS, match, bi_match = {'bi': 0, 'lm': 0, 'mention': 0, 'reference': 0, 'total': 0}, [], [] # result container
	coref_lines = []
	iterator = enumerate(tqdm(dataloader, desc="Epoch {} {}".format(epoch, run_type), disable=args.disable_display))

	if args.disable_display:
		print('Evaluation progress is not showing')

	for step, batch in iterator:
		if run_type == 'teacher_force':
			loss, _, _, _, _, _, _ = model(input_ids=batch['input_ids'], attention_mask=batch['attention_mask'], \
											token_type_ids=batch['token_type_ids'], labels=batch['label_ids'], \
											mention_labels=batch['mention_label_ids'], batch=batch, coref_links=batch['coref_label'])
			for k, v in loss.items():
				LOSS[k] += v.item()

		else:
			decode_output = decode(args, batch, model, tokenizer)
			score_fn(args, decode_output, batch, match, collector, qr_metric, coref_lines, bi_match)

	# log
	if run_type == 'teacher_force':
		for k, v in LOSS.items():
			LOSS[k] /= (step+1)
		print_loss(epoch, data_type, LOSS, t0)
		return LOSS
	else: # record decoding result
		res = {}
		if 'qr' in args.task:
			qr_res = qr_metric.get_metric(reset=True)
			qr_res['Exact match'] = sum(match) / len(match) * 100
			get_binary_res(bi_match, qr_res, args)
			res['qr'] = qr_res
		else:
			res['qr'] = {}

		if 'coref' in args.task:
			# prepare conll files
			key_path = args.dev_conll if data_type == 'dev' else args.test_conll
			response_path = 'temp/{}.response'.format(args.model_name) # a temp file for calculating coref score
			with open(response_path, 'w') as f:
				f.writelines(coref_lines)
			res['coref'] = coref_evaluate(key_path, response_path, args)
		else:
			res['coref'] = {}

		print_score(args, epoch, data_type, res, t0)
		return res
コード例 #4
0
def reply(history):
  clean_history = [clean(str(m).strip()) for m in tokenize(history)]
  src = " ".join([e + " _eos" for e in clean_history])
  tgt = ""
  fct = best_fact(clean_history[-1])

  print(src)
  print(fct)

  input_seq, input_lens, target_seq, target_lens = model.prep_batch([(src,tgt,fct)])
  output = model.decode(input_seq, input_lens)
  return output[0]
コード例 #5
0
def greedy_decode(model, src, src_mask, max_len, start_symbol):
    memory = model.encode(src, src_mask)
    ys = torch.ones(1, 1).fill_(start_symbol).type_as(src.data)
    for i in range(max_len - 1):
        out = model.decode(
            memory, src_mask, Variable(ys),
            Variable(subsequent_mask(ys.size(1)).type_as(src.data)))
        prob = model.generator(out[:, -1])
        _, next_word = torch.max(prob, dim=1)
        next_word = next_word.data[0]
        ys = torch.cat(
            [ys, torch.ones(1, 1).type_as(src.data).fill_(next_word)], dim=1)
    return ys
コード例 #6
0
def sample(opt):

    # Initialize TensorFlow session.
    tf.InteractiveSession()

    assert(opt.model_path or opt.pretrained), 'specify weights path or pretrained model'

    if opt.model_path:
        raise NotImplementedError
    elif opt.pretrained:
        assert(opt.pretrained  == 'celebahq')
        # make sure to git clone glow repository first
        sys.path.append('resources/glow/demo')
        import model
        eps_std = 0.7
        eps_size = model.eps_size

    rng = np.random.RandomState(opt.seed)
    attr = np.random.RandomState(opt.seed+1)
    tags = []
    amts = []

    for batch_start in tqdm(range(0, opt.num_samples, opt.batch_size)):
        # Generate latent vectors.
        bs = min(opt.num_samples, batch_start + opt.batch_size) - batch_start
        feps = rng.normal(scale=eps_std, size=[bs, eps_size])

        if opt.manipulate:
            tag = attr.randint(len(model._TAGS), size=bs)
            amt = attr.uniform(-1, 1, size=(bs, 1))
            dzs = model.z_manipulate[tag]
            feps = feps + amt * dzs
            tags.append(tag)
            amts.append(amt)

        images = model.decode(feps)

        # Save images as PNG.
        for idx in range(images.shape[0]):
            filename = os.path.join(opt.output_path, 'seed%03d_sample%06d.%s'
                                        % (opt.seed, batch_start + idx,
                                           opt.format))
            im = PIL.Image.fromarray(images[idx], 'RGB')
            if opt.resize:
                im = im.resize((opt.resize, opt.resize), PIL.Image.LANCZOS)
            im.save(filename)

    if opt.manipulate:
        outfile = os.path.join(opt.output_path, 'manipulations.npz')
        np.savez(outfile, tags=np.concatenate(tags), amts=np.concatenate(amts))
コード例 #7
0
ファイル: train.py プロジェクト: hologerry/pix2pix-flow
def infer(sess, model, hps, iterators, its):
    from tqdm import tqdm
    assert hps.restore_path_A != ''
    assert hps.restore_path_B != ''

    xs_A, xs_B = [], []
    zs_A, zs_B = [], []
    for it in tqdm(range(its)):
        x_A, y_A = iterators['A']()
        x_B, y_B = iterators['B']()

        # A2B
        z_A = model.encode(x_A, y_A, 'model_A')
        x_B_recon = model.decode(y_B, z_A, 'model_B')
        xs_B.append(x_B_recon)
        zs_A.append(z_A)

        # B2A
        z_B = model.encode(x_B, y_B, 'model_B')
        x_A_recon = model.decode(y_A, z_B, 'model_A')
        xs_A.append(x_A_recon)
        zs_B.append(z_B)

    x_A = np.concatenate(xs_A, axis=0)
    z_A = np.concatenate(zs_A, axis=0)
    x_B = np.concatenate(xs_B, axis=0)
    z_B = np.concatenate(zs_B, axis=0)

    np.save(os.path.join(hps.logdir, 'z_A'), z_A)
    np.save(os.path.join(hps.logdir, 'z_B'), z_B)

    from utils import npy2img
    npy2img(os.path.join(hps.logdir, 'B2A'), x_A)
    npy2img(os.path.join(hps.logdir, 'A2B'), x_B)

    return x_A, z_A, x_B, z_B
コード例 #8
0
ファイル: train.py プロジェクト: lhz1029/glow
def infer(sess, model, hps, iterator):
    # Example of using model in inference mode. Load saved model using hps.restore_path
    # Can provide x, y from files instead of dataset iterator
    # If model is uncondtional, always pass y = np.zeros([bs], dtype=np.int32)
    print('in infer')
    if hps.direct_iterator:
        iterator = iterator.get_next()
    # if hps.use_samples:
    #     iterator = tf.data.Dataset.from_tensor_slices(np.random.normal(size=(500, 32 * 32 * 3))).batch(hps.n_batch_test)
    #     iterator = iterator.prefetch(10)
    #     iterator = iterator.make_one_shot_iterator()

    xs = []
    zs = []
    losses = []
    for it in range(hps.full_test_its):
        # for it in range(10):
        if hps.direct_iterator:
            # replace with x, y, attr if you're getting CelebA attributes, also modify get_data
            x, y = sess.run(iterator)
        else:
            x, y = iterator()
        if hps.use_samples:
            # z = iterator.get_next()
            # z = tf.Print(z, [tf.shape(z), tf.reduce_mean(z)]) #, tf.math.reduce_std(z)])
            # y = tf.zeros([hps.n_batch_test], dtype=np.int32)
            z = np.random.normal(size=(hps.n_batch_test, 32 * 32 * 3))
            # print(z.mean(), z.std())
        else:
            z = model.encode(x, y)
            print(z.shape)
        x = model.decode(y, z)
        loss = model.calculate_likelihood(x, y)
        xs.append(x)
        zs.append(z)
        losses.append(loss)

    x = np.concatenate(xs, axis=0)
    z = np.concatenate(zs, axis=0)
    l = np.concatenate(losses, axis=0)
    np.save(os.path.join(hps.logdir, 'x.npy'), x)
    np.save(os.path.join(hps.logdir, 'z.npy'), z)
    np.save(os.path.join(hps.logdir, 'l.npy'), l)
    # from scipy.stats import norm
    # rv = norm()
    # unifs = rv.cdf(z)
    # np.save(os.path.join(hps.logdir, 'unifs.npy'), unifs)
    return zs
コード例 #9
0
def greedy_decode(model,
                  src,
                  src_mask,
                  src_lengths,
                  max_len=100,
                  sos_index=1,
                  eos_index=None):
    """Greedily decode a sentence."""

    with torch.no_grad():
        encoder_hidden, encoder_final = model.encode(src, src_mask,
                                                     src_lengths)
        prev_y = torch.ones(1, 1).fill_(sos_index).type_as(src)
        trg_mask = torch.ones_like(prev_y)

    output = []
    attention_scores = []
    hidden = None

    for i in range(max_len):
        with torch.no_grad():
            out, hidden, pre_output = model.decode(encoder_hidden,
                                                   encoder_final, src_mask,
                                                   prev_y, trg_mask, hidden)

            # we predict from the pre-output layer, which is
            # a combination of Decoder state, prev emb, and context
            prob = model.generator(pre_output[:, -1])

        _, next_word = torch.max(prob, dim=1)
        next_word = next_word.data.item()
        output.append(next_word)
        prev_y = torch.ones(1, 1).type_as(src).fill_(next_word)
        attention_scores.append(model.decoder.attention.alphas.cpu().numpy())

    output = np.array(output)

    # cut off everything starting from </s>
    # (only when eos_index provided)
    if eos_index is not None:
        first_eos = np.where(output == eos_index)[0]
        if len(first_eos) > 0:
            output = output[:first_eos[0]]

    return output, np.concatenate(attention_scores, axis=1)
コード例 #10
0
ファイル: explore.py プロジェクト: argsim/argsim
def analyze(z, use_dim=[], seed=25):
    ''' z =  np.array[2, dim], mu of two sentences'''
    ''' use_dim = list of int describing which dimension should be used '''

    # select random path from z1 to z2
    np.random.seed(seed)
    if use_dim == []:
        rdm_path = np.arange(len(z[0]))
    else:
        rdm_path = use_dim
    np.random.shuffle(rdm_path)

    # walk the path and print  at every step
    path = np.copy(z[0])
    for idx, dim in enumerate(rdm_path):
        path[dim] = z[1][dim]
        output = decode(sess, vae, [z[0], path, z[1]]).tolist()
        _ = [vocab.decode_ids(output[idx]) for idx in range(3)]
        print(idx, dim, _[1])
コード例 #11
0
def _decode_and_evaluate(name,
                         model,
                         sess,
                         trans_file,
                         ref_file,
                         subword_option,
                         beam_width,
                         tgt_eos,
                         num_translations_per_input=1,
                         decode=True):
    """Decode a test set and compute a score according to the evaluation task."""
    # Decode
    if decode:
        utils.print_out("  decoding to output %s." % trans_file)

        start_time = time.time()
        num_sentences = 0
        with codecs.getwriter("utf-8")(tf.gfile.GFile(trans_file, mode="wb")) as trans_f:
            trans_f.write("")  # Write empty string to ensure file is created.

            num_translations_per_input = max(min(num_translations_per_input, beam_width), 1)
            while True:
                try:
                    outputs, _ = model.decode(sess)
                    if beam_width == 0:
                        outputs = np.expand_dims(outputs, 0)

                    batch_size = outputs.shape[1]
                    num_sentences += batch_size

                    for sent_id in range(batch_size):
                        for beam_id in range(num_translations_per_input):
                            translation = _get_translation(
                                outputs[beam_id],
                                sent_id,
                                tgt_eos=tgt_eos,
                                subword_option=subword_option)
                            trans_f.write((translation + b"\n").decode("utf-8"))
                except tf.errors.OutOfRangeError:
                    break
    return
コード例 #12
0
            torch.load(
                os.path.join(save_dir, 'autoEncoder_preTrained300.model')))
        print(model)
        with torch.no_grad():
            model.eval()
            test_data = []
            test_data = src

            for i in range(0, len(src)):
                input_lines_src, output_lines_src, lens_src, mask_src = get_batch(
                    src, word2idx, i, 1, max_len)
                print("Total:", len(src), " Current:", i, "Tracker On")
                if i > 911:
                    print(input_lines_src)
                decoder_logit, hid_state, trg = model(input_lines_src)
                prob_2d, word_probs = decode(model, decoder_logit, vocab_size)
                #word_probs = word_probs.data.cpu().numpy().argmax(axis=-1)
                output_lines_src = output_lines_src.data.cpu().numpy()
                print("GPU memory consumption" + str(i) + " " +
                      str(torch.cuda.memory_allocated()))

                hidden_embed.append(hid_state.data.cpu().numpy())
                print("Hidden embeddings list size:",
                      sys.getsizeof(hidden_embed))
                #encoder_op.append(decoder_logit[0].data.cpu().numpy())
                doc_embedding.append(trg[0].data.cpu().numpy())
                print("Doc embedding list size:", sys.getsizeof(doc_embedding))
            #for i in encoder_op:
            #    array = i/np.linalg.norm(encoder_op)
            #    docVector.append(array)
            #print(doc_embedding)
コード例 #13
0
ファイル: anal.py プロジェクト: smartdesignlab/BEGAN-wheel
def main():

    #load configuration
    conf, _ = get_config()
    pp.pprint(conf)
    if conf.is_gray :
        n_channel=1
    else:      
        n_channel=3
    
    n_grid_row = int(np.sqrt(conf.n_batch))
    
    z = tf.random_uniform((conf.n_batch, conf.n_z), minval=-1.0, maxval=1.0)
    # execute generator
    g_net,_ = generate(z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=False, reuse=False) 
    # execute discriminator
    e_net,_, _ = encode(g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=False, reuse=False)
    d_net,_ = decode(e_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel,is_train=False, reuse=False)
   
    g_img=tf.clip_by_value((g_net + 1)*127.5, 0, 255)
    d_img=tf.clip_by_value((d_net + 1)*127.5, 0, 255)
    # start session
    sess = tf.InteractiveSession()
    init = tf.global_variables_initializer() 
    sess.run(init)

    # init directories
    checkpoint_dir = os.path.join(conf.log_dir,conf.curr_time)
    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)

    #saver = tf.train.import_meta_graph(npz_path+'began2_model.ckpt.meta')
    #saver.restore(sess, tf.train.latest_checkpoint(npz_path))
    saver = tf.train.Saver()
    saver.restore(sess, os.path.join(conf.load_dir, conf.ckpt_nm))
    
    #load real image 
    data_files = glob(os.path.join(conf.data_dir,conf.dataset, "*"))
    shuffle(data_files)
    x_fix = data_files[0:conf.n_batch]
    x_fix=[get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in x_fix]
    x_fix = np.array(x_fix).astype(np.float32)
    if(conf.is_gray == 1):
        s,h,w = x_fix.shape
        x_fix = x_fix.reshape(s,h, w,n_channel )   
         
    n_loop = 1
    
    def getRealAR():
        # run ae        
        x_im =sess.run(d_img,feed_dict={g_net:x_fix})  
        save_images(x_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, 'anal_AE_X.png'))
    
    def getRandomG():
     
        f_g = open(checkpoint_dir+ '/g_img.csv', 'a')
        
        # generate images from generator and ae
        for i in range(5):
            z_test =np.random.uniform(low=-1, high=1, size=(conf.n_batch, 64)).astype(np.float32)
            g_im =sess.run(g_img,feed_dict={z:z_test})  
            save_images(g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_G.png'))
        #    g_im = g_im/127.5 - 1.
        #    ae_g_im =sess.run(d_img,feed_dict={g_net:g_im})  
        #    save_images(ae_g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_AE_G.png'))
            
        
            for j in range(g_im.shape[0]):
                f_g.write(str(g_im[j].tolist()).replace("[", "").replace("]", "")+ '\n')
        f_g.close()
        
    def getFixedG(f_in):
        l_z = list()
        with open(f_in,'r') as file:    
            for line in file:
               l_z.append(np.fromstring(line, dtype=float, sep=','))
        file.close()
        n_loop = int(len(l_z)/64)
        
        l_z = np.asarray(l_z)
        
        for i in range(n_loop):
            fr = 64*i
            to = 64*(i+1)
            z_test =l_z[fr:to]
            g_im =sess.run(g_img,feed_dict={z:z_test})  
            save_images(g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, '_anal_fix_G.png'))
            #g_im = g_im/127.5 - 1.
            #ae_g_im =sess.run(d_img,feed_dict={g_net:g_im})  
            #save_images(ae_g_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_AE_G.png'))
    def getRandomAE():
        # generate images from discriminator and ae
        for i in range(n_loop):
            z_test =np.random.uniform(low=-1, high=1, size=(conf.n_batch, conf.n_img_out_pix, conf.n_img_out_pix,n_channel)).astype(np.float32)
            d_im =sess.run(d_img,feed_dict={g_net:z_test})  
            save_images(d_im, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_D.png'))
        
    def saveFeatures():
        # get latent value from real images (10*n_batch)
        for i in range(n_loop):
            shuffle(data_files)
            f_test = data_files[0:conf.n_batch]
            x_test=[get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in f_test]
            x_test = np.array(x_test).astype(np.float32)
            if(conf.is_gray == 1):
                s,h,w = x_test.shape
                x_test = x_test.reshape(s,h, w,n_channel ) 
        
            latent =sess.run(e_net,feed_dict={g_net:x_test}) 
            
            f_latent = open(checkpoint_dir+ '/latent.csv', 'a')
            for k in range(latent.shape[0]):
                f_latent.write(str(latent[k].tolist()).replace("[", "").replace("]", "")+ '\n')
            f_latent.close()
            
    def getFeatures():
        f_path=checkpoint_dir+'/latent.csv'#'C:/samples/img_download/wheels/wheeldesign/output/began2_anal/17-11-28-14-52/latent.csv'    
        data = pd.read_csv(f_path)
        
        n_latent = data.shape[1]
        mean = [None]*n_latent
        std = [None]*n_latent
        for i in range(n_latent):
            #i+=1
            latent = np.array(data.iloc[:, i:i+1])
            mean[i] = np.mean(latent)
            std[i] = np.std(latent)
            
        plt.show()
        return mean, std
    
    def generateFeature(mean, std):
        z_size = len(mean)
        feature = [None]*z_size
        for i in range(z_size):
            feature[i] = np.random.normal(loc=mean[i], scale=std[i], size=z_size*n_loop)
        return feature   
    
    
    def generateImage(feature):
        feature = np.array(feature)
        idx=0
        for i in range(n_loop):
            
            f_net = feature[:,idx:idx+64]
            f_img =sess.run(d_img,feed_dict={e_net:f_net}) 
            save_images(f_img, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, str(i)+'_anal_G_df.png'))
            idx+=64
    
    def getDiscMeanFeature(mean):
        mean = np.array(mean)
        mean = mean-2
       
        m_net = [None]*64
        for i in range(64):
            m_net[i] = mean +1/63 *i
        d_mnfd =sess.run(d_img,feed_dict={e_net:m_net})                       
        save_images(d_mnfd, [n_grid_row,n_grid_row],os.path.join(checkpoint_dir, 'anal_D_Mean_df.png'))    
    
    #getFixedG(conf.log_dir+'anal/g_df/z.csv')
    
    #getRealAR()
    getRandomG()
    #getRandomAE()
           
    #saveFeatures()
    #z_mean, z_std = getFeatures()
    #z_feature = generateFeature(z_mean, z_std)
    #shuffle(z_feature)
    #generateImage(z_feature)
    #getDiscMeanFeature(z_mean)
       
    sess.close()
コード例 #14
0
def main(conf):

    logger = logging.getLogger("desc")
    logger.setLevel(logging.INFO)
    fileHandler = logging.FileHandler(os.path.join(base_dir, 'log.txt'))
    logger.addHandler(fileHandler)
    #streamHandler = logging.StreamHandler()
    #logger.addHandler(streamHandler)

    if conf.is_gray:
        n_channel = 1
    else:
        n_channel = 3

    # init directories
    checkpoint_dir = os.path.join(base_dir, conf.curr_time)
    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)

    ##========================= DEFINE MODEL ===========================##
    #z = tf.random_uniform(conf.n_batch, conf.n_z), minval=-1.0, maxval=1.0)
    z = readz(os.path.join(base_dir, 'z.csv'), conf.n_batch)

    x_net = tf.placeholder(
        tf.float32, [conf.n_batch, conf.n_img_pix, conf.n_img_pix, n_channel],
        name='real_images')

    k_t = tf.Variable(0., trainable=False, name='k_t')

    # execute generator
    g_net, g_vars, g_conv = generate(z,
                                     conf.n_img_out_pix,
                                     conf.n_conv_hidden,
                                     n_channel,
                                     is_train=True,
                                     reuse=False)

    # execute discriminator
    e_g_net, enc_vars, e_g_conv = encode(g_net,
                                         conf.n_z,
                                         conf.n_img_out_pix,
                                         conf.n_conv_hidden,
                                         is_train=True,
                                         reuse=False)
    d_g_net, dec_vars, d_g_conv = decode(e_g_net,
                                         conf.n_z,
                                         conf.n_img_out_pix,
                                         conf.n_conv_hidden,
                                         n_channel,
                                         is_train=True,
                                         reuse=False)

    e_x_net, _, e_x_conv = encode(x_net,
                                  conf.n_z,
                                  conf.n_img_out_pix,
                                  conf.n_conv_hidden,
                                  is_train=True,
                                  reuse=True)
    d_x_net, _, d_x_conv = decode(e_x_net,
                                  conf.n_z,
                                  conf.n_img_out_pix,
                                  conf.n_conv_hidden,
                                  n_channel,
                                  is_train=True,
                                  reuse=True)

    g_img = tf.clip_by_value((g_net + 1) * 127.5, 0, 255)
    #x_img=tf.clip_by_value((x_net + 1)*127.5, 0, 255)
    d_g_img = tf.clip_by_value((d_g_net + 1) * 127.5, 0, 255)
    d_x_img = tf.clip_by_value((d_x_net + 1) * 127.5, 0, 255)

    d_vars = enc_vars + dec_vars

    d_loss_g = tf.reduce_mean(tf.abs(d_g_net - g_net))
    d_loss_x = tf.reduce_mean(tf.abs(d_x_net - x_net))

    d_loss = d_loss_x - k_t * d_loss_g
    g_loss = tf.reduce_mean(tf.abs(d_g_net - g_net))

    d_loss_prev = d_loss
    g_loss_prev = g_loss
    k_t_prev = k_t

    g_optim = tf.train.AdamOptimizer(conf.g_lr).minimize(g_loss,
                                                         var_list=g_vars)
    d_optim = tf.train.AdamOptimizer(conf.d_lr).minimize(d_loss,
                                                         var_list=d_vars)

    balance = conf.gamma * d_loss_x - g_loss
    measure = d_loss_x + tf.abs(balance)

    with tf.control_dependencies([d_optim, g_optim]):
        k_update = tf.assign(
            k_t, tf.clip_by_value(k_t + conf.lambda_k * balance, 0, 1))

    # start session
    sess = tf.InteractiveSession()
    init = tf.global_variables_initializer()
    sess.run(init)

    loadWeight(sess, conf)

    x_fix = readx(os.path.join(base_dir, 'x.csv'), conf.n_batch)
    x_fix = x_fix.reshape(conf.n_batch, conf.n_conv_hidden, conf.n_img_out_pix,
                          n_channel)

    n_loop = 2
    for itr in range(n_loop):

        fetch_dict = {
            "kupdate": k_update,
            "m": measure,
            "b": balance,
            'gnet': g_net,
            'dgnet': d_g_net,
            'dxnet': d_x_net,
            'xnet': x_net,
            'gconv': g_conv,
            'egconv': e_g_conv,
            'dgconv': d_g_conv,
            'exconv': e_x_conv,
            'dxconv': d_x_conv,
            "dlossx": d_loss_x,
            "gloss": g_loss,
            "dloss": d_loss,
            "kt": k_t,
            'gimg': g_img,
            'dgimg': d_g_img,
            'dximg': d_x_img,
        }

        result = sess.run(fetch_dict, feed_dict={x_net: x_fix})

        logger.info('measure: ' + str(result['m']))
        logger.info('balance: ' + str(result['b']))
        logger.info('gloss: ' + str(result['gloss']))
        logger.info('dloss: ' + str(result['dloss']))
        logger.info('dlossx: ' + str(result['dlossx']))
        logger.info('k_t: ' + str(result['kt']))

        if itr == 0:

            gconv = result['gconv']
            for i in range(len(gconv)):
                conv = np.clip((gconv[i] + 1) * 127.5, 0, 255)
                s, h, w, c = conv.shape
                for j in range(c):
                    c_img = conv[:, :, :, j:j + 1]
                    save_image(
                        c_img,
                        os.path.join(
                            checkpoint_dir, 'gen_' + str(i) + '_' + str(j) +
                            '_' + str(h) + '_conv.png'))

            dgconv = result['dgconv']
            for i in range(len(dgconv)):
                conv = np.clip((dgconv[i] + 1) * 127.5, 0, 255)
                s, h, w, c = conv.shape
                for j in range(c):
                    c_img = conv[:, :, :, j:j + 1]
                    save_image(
                        c_img,
                        os.path.join(
                            checkpoint_dir, 'dec_g_' + str(i) + '_' + str(j) +
                            '_' + str(h) + '_conv.png'))

            dxconv = result['dxconv']
            for i in range(len(dxconv)):
                conv = np.clip((dxconv[i] + 1) * 127.5, 0, 255)
                s, h, w, c = conv.shape
                for j in range(c):
                    c_img = conv[:, :, :, j:j + 1]
                    save_image(
                        c_img,
                        os.path.join(
                            checkpoint_dir, 'dec_x_' + str(i) + '_' + str(j) +
                            '_' + str(h) + '_conv.png'))

            exconv = result['exconv']
            for i in range(len(exconv)):
                conv = np.clip((exconv[i] + 1) * 127.5, 0, 255)
                s, h, w, c = conv.shape
                for j in range(c):
                    c_img = conv[:, :, :, j:j + 1]
                    save_image(
                        c_img,
                        os.path.join(
                            checkpoint_dir, 'enc_x_' + str(i) + '_' + str(j) +
                            '_' + str(h) + '_conv.png'))

            egconv = result['egconv']
            for i in range(len(egconv)):
                conv = np.clip((egconv[i] + 1) * 127.5, 0, 255)
                s, h, w, c = conv.shape
                for j in range(c):
                    c_img = conv[:, :, :, j:j + 1]
                    save_image(
                        c_img,
                        os.path.join(
                            checkpoint_dir, 'enc_g_' + str(i) + '_' + str(j) +
                            '_' + str(h) + '_conv.png'))

        gnet = result['gnet']
        dgnet = result['dgnet']
        dxnet = result['dxnet']
        xnet = result['xnet']
        for i in range(conf.n_batch):
            logger.info(
                'g_net: ' +
                str(gnet[i].tolist()).replace("[", "").replace("]", ""))
            logger.info(
                'd_g_net: ' +
                str(dgnet[i].tolist()).replace("[", "").replace("]", ""))
            logger.info(
                'x_net: ' +
                str(xnet[i].tolist()).replace("[", "").replace("]", ""))
            logger.info(
                'd_x_net: ' +
                str(dxnet[i].tolist()).replace("[", "").replace("]", ""))

        gimg = result['gimg']
        dgimg = result['dgimg']
        dximg = result['dximg']
        save_image(gimg,
                   os.path.join(checkpoint_dir,
                                str(itr) + '_final_g_img.png'))
        save_image(
            dgimg, os.path.join(checkpoint_dir,
                                str(itr) + '_final_d_g_img.png'))
        save_image(
            dximg, os.path.join(checkpoint_dir,
                                str(itr) + '_final_d_x_img.png'))

    sess.close()
コード例 #15
0
def main():

    #load configuration
    conf, _ = get_config()
    pp.pprint(conf)
    if conf.is_gray :
        n_channel=1
    else:
        n_channel=3

    n_grid_row = int(np.sqrt(conf.n_batch))
    
    ##========================= DEFINE MODEL ===========================##
    z = tf.random_uniform(
                (conf.n_batch, conf.n_z), minval=-1.0, maxval=1.0)
    x_net =  tf.placeholder(tf.float32, [conf.n_batch, conf.n_img_pix, conf.n_img_pix, n_channel], name='real_images')
    k_t = tf.Variable(0., trainable=False, name='k_t')
 
    # define generator
    g_net, g_vars = generate(z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel,  is_train=True, reuse=False)
        
    # define discriminator
    e_g_net, enc_vars,_ = encode(g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=True, reuse=False)
    d_g_net, dec_vars = decode(e_g_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=False)
    e_x_net, _,_ = encode(x_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, is_train=True, reuse=True)
    d_x_net, _ = decode(e_x_net, conf.n_z, conf.n_img_out_pix, conf.n_conv_hidden, n_channel, is_train=True, reuse=True)
    
    # image de-normalization
    g_img=tf.clip_by_value((g_net + 1)*127.5, 0, 255)
    d_g_img=tf.clip_by_value((d_g_net + 1)*127.5, 0, 255)
    d_x_img=tf.clip_by_value((d_x_net + 1)*127.5, 0, 255)
    
    d_vars = enc_vars + dec_vars

    # define discriminator and generator losses
    d_loss_g = tf.reduce_mean(tf.abs(d_g_net - g_net))
    d_loss_x = tf.reduce_mean(tf.abs(d_x_net - x_net))
    d_loss= d_loss_x - k_t * d_loss_g
    g_loss = tf.reduce_mean(tf.abs(d_g_net - g_net))

    # define optimizer
    d_optim = tf.train.AdamOptimizer(conf.d_lr).minimize(d_loss, var_list=d_vars)
    g_optim = tf.train.AdamOptimizer(conf.g_lr).minimize(g_loss, var_list=g_vars)

    balance = conf.gamma * d_loss_x - g_loss
    measure = d_loss_x + tf.abs(balance)

    with tf.control_dependencies([d_optim, g_optim]):
        k_update = tf.assign(k_t, tf.clip_by_value(k_t + conf.lambda_k * balance, 0, 1))

    # define summary for tensorboard
    summary_op = tf.summary.merge([
            tf.summary.image("G", g_img),
            tf.summary.image("AE_G", d_g_img),
            tf.summary.image("AE_x", d_x_img),
            tf.summary.scalar("loss/dloss", d_loss),
            tf.summary.scalar("loss/d_loss_real", d_loss_x),
            tf.summary.scalar("loss/d_loss_fake", d_loss_g),
            tf.summary.scalar("loss/gloss", g_loss),
            tf.summary.scalar("misc/m", measure),
            tf.summary.scalar("misc/kt", k_t),
            tf.summary.scalar("misc/balance", balance),
        ])

    # start session
    sess = tf.InteractiveSession()#config=tf.ConfigProto(log_device_placement=True))
    init = tf.global_variables_initializer()
    sess.run(init)

    # init directories
    checkpoint_dir = os.path.join(conf.log_dir,conf.curr_time)
    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)

    # init summary writer for tensorboard
    summary_writer = tf.summary.FileWriter(checkpoint_dir,sess.graph)

    saver = tf.train.Saver()
    if(conf.is_reload):
        saver.restore(sess, os.path.join(conf.load_dir, conf.ckpt_nm))

    # load real image info and shuffle them
    data_files = glob(os.path.join(conf.data_dir,conf.dataset, "*"))
    shuffle(data_files)

    # save real fixed image
    x_fix = data_files[0:conf.n_batch]
    x_fix=[get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in x_fix]
    x_fix = np.array(x_fix).astype(np.float32)
    x_fix = x_fix.reshape(x_fix.shape[0],x_fix.shape[1], x_fix.shape[2],n_channel )
    save_images(x_fix, [n_grid_row,n_grid_row],'{}/x_fix.png'.format(checkpoint_dir))

    cost_file = open(checkpoint_dir+ "/cost.txt", 'w', conf.n_buffer)
    n_step=0
    for epoch in range(conf.n_epoch):
        ## shuffle data
        shuffle(data_files)

        ## load image data
        n_iters = int(len(data_files)/conf.n_batch)

        for idx in range(0, n_iters):
            # make image batch
            f_batch = data_files[idx*conf.n_batch:(idx+1)*conf.n_batch]
            data_batch = [get_image(f, conf.n_img_pix, is_crop=conf.is_crop, resize_w=conf.n_img_out_pix, is_grayscale = conf.is_gray) for f in f_batch]
            img_batch = np.array(data_batch).astype(np.float32)
            
            if conf.is_gray :
                s,h,w = img_batch.shape
                img_batch = img_batch.reshape(s, h, w, n_channel )
                
            fetch_dict = {
                "kupdate": k_update,
                "m": measure,
            }
            if n_step % conf.n_save_log_step == 0:
                fetch_dict.update({
                    "summary": summary_op,
                    "gloss": g_loss,
                    "dloss": d_loss,
                    "kt": k_t,
                })

            start_time = time.time()
            # run the session!
            result = sess.run(fetch_dict, feed_dict={x_net:img_batch})
            
            # get the result
            m = result['m']

            if n_step % conf.n_save_log_step == 0:
                summary_writer.add_summary(result['summary'], n_step)
                summary_writer.flush()

                # write cost to a file
                gloss = result['gloss']
                dloss = result['dloss']
                kt = result['kt']
                cost_file.write("Epoch: ["+str(epoch)+"/"+str(conf.n_epoch)+"] ["+str(idx)+"/"+str(n_iters)+"] time: "+str(time.time() - start_time)+", d_loss: "+str(dloss)+", g_loss:"+ str(gloss)+" measure: "+str(m)+", k_t: "+ str(kt)+ "\n")

            # save generated image file
            if n_step % conf.n_save_img_step == 0:

                g_sample, g_ae, x_ae = sess.run([g_img, d_g_img,d_x_img] ,feed_dict={x_net: x_fix})

                save_image(g_sample,os.path.join(checkpoint_dir, '{}_G.png'.format(n_step)))
                save_image(g_ae, os.path.join(checkpoint_dir,  '{}_AE_G.png'.format(n_step)))
                save_image(x_ae, os.path.join(checkpoint_dir, '{}_AE_X.png'.format(n_step)))
                
            n_step+=1

        # save checkpoint    
        saver.save(sess, os.path.join(checkpoint_dir,str(epoch)+"_"+str(n_step)+"_began2_model.ckpt") ) 

    # save final checkpoint  
    saver.save(sess, os.path.join(checkpoint_dir,"final_began2_model.ckpt"))
    
    cost_file.close()
    
    sess.close()
コード例 #16
0
    # Prepare batch
    batch_indices = indices[batch*args.batch_size:(batch+1)*args.batch_size]
    batch_rows = [valid[i] for i in batch_indices]

    if args.bs_predictor:
      input_seq, input_lens, bs = model.prep_batch(batch_rows)
      predicted_bs = model.predict(input_seq, input_lens)
      bs_predictions.append((predicted_bs.data.cpu().numpy(), bs.data.cpu().numpy()))
    elif args.dm_predictor:
      bs, da, db = model.prep_batch(batch_rows)
      predicted_da = model.predict(bs, db)
      bs_predictions.append((predicted_da.data.cpu().numpy(), da.data.cpu().numpy()))
    elif args.nlg_predictor:
      target_seq, target_lens, db, da, bs = model.prep_batch(batch_rows)
      # Get predicted sentences for batch
      predicted_sentences = model.decode(50, db, da, bs)

      # Add predicted to list
      for i,sent in enumerate(predicted_sentences):
        all_predicted[batch_rows[i][-2]].append(sent) 
    else:
      input_seq, input_lens, target_seq, target_lens, db, bs, da = model.prep_batch(batch_rows)

      # Get predicted sentences for batch
      predicted_sentences = model.decode(input_seq, input_lens, 50, db, bs, da)

      # Add predicted to list
      for i,sent in enumerate(predicted_sentences):
        all_predicted[batch_rows[i][-2]].append(sent) 

  json.dump(all_predicted, open('temp.json', 'w+'))
コード例 #17
0
                              args=args,
                              test=True).cuda()

# TEST EVALUATION
best_epoch = args.epoch
model.load("{0}/model_{1}.bin".format(args.save_path, best_epoch))
model.transformer.eval()

# Iterate over batches
num_batches = math.ceil(len(valid_freq) / args.batch_size)
cum_loss = 0
cum_words = 0
predicted_sentences = []
indices = list(range(len(valid_freq)))
for batch in tqdm(range(num_batches)):
    # Prepare batch
    batch_indices = indices[batch * args.batch_size:(batch + 1) *
                            args.batch_size]
    batch_rows = [valid_freq[i] for i in batch_indices]

    # Encode batch. If facts are being used, they'll be prepended to the input
    input_seq, input_lens, target_seq, target_lens = model.prep_batch(
        batch_rows)

    # Decode batch
    predicted_sentences += model.decode(input_seq, input_lens)

# Save predictions
open("{0}/valid_freq_out.tgt".format(args.save_path),
     "w+").writelines([l + "\n" for l in predicted_sentences])
コード例 #18
0
import numpy
import random
from keras.models import load_model
from model import decode, read_folder, read_png
from show import plt

model = load_model("model.h5")
characters = open("model.characters").read()

test_folder = "check/"
test_file_list, _characters = read_folder(test_folder)
test_file = random.choice(test_file_list)
x_test, y_test, n_class_test = read_png([test_file], characters)

y_pred = model.predict(x_test)
title = 'real: %s\npred:%s' % (decode(y_test,
                                      characters), decode(y_pred, characters))
print title

all_count = 0
right_count = 0
for test_file in test_file_list:
    x_test, y_test, n_class_test = read_png([test_file], characters)
    y_pred = model.predict(x_test)
    y_p = decode(y_pred, characters)
    y_t = decode(y_test, characters)
    print "real", y_t, "pred", y_p
    all_count += 1
    if y_p == y_t:
        right_count += 1
print "all", all_count, "right", right_count, "percent", 1.0 * right_count / all_count
"""
コード例 #19
0
ファイル: explore_infer.py プロジェクト: argsim/argsim
def auto(z, steps=256):
    for s in sp.decode(vocab, decode(sess, vae, z, steps)):
        print(s)
コード例 #20
0
ファイル: train.py プロジェクト: dung98pt/chem_vae_branch
from keras.losses import mse, binary_crossentropy
from keras.utils import to_categorical
import numpy as np
from keras.models import load_model
from keras import optimizers
from keras.callbacks import ModelCheckpoint
from tgru_k2_gpu import TerminalGRU 
from keras.models import Sequential
from keras.layers import Dense, Conv1D, Dropout, MaxPooling1D, Flatten, Dense, BatchNormalization,RepeatVector,GRU,Input,Lambda
from data_loader import load_data
from model import encode, decode
# Tải dữ liệu 
X_train, X_test, X_val = load_data()

encode = encode()
decode = decode()

def VAE():
  inputs = Input(shape=(120,35))
  z_mean, z_log_var, z = encode(inputs)
  x = decode([inputs, z])
  VAE = Model(inputs, x, name = "VAE")
  return VAE, z_mean, z_log_var
vae, z_mean, z_log_var  = VAE()
vae.summary() 

''' Test thử 3 hàm loss'''
def vae_loss_binary(x, x_reconstruction):
    xent_loss = K.sum(binary_crossentropy(x, x_reconstruction) , axis = -1)
    kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
    return K.mean(xent_loss + kl_loss)
コード例 #21
0
        model.cuda()

    optimizer = optim.Adam(model.parameters(), lr=LR)

    for epoch in range(1, args.epochs + 1):
        utils.train(model, train_loader, epoch, optimizer, is_conditional=True)
        utils.eval(model, val_loader, epoch)

        if epoch % 10 == 0:
            sample = Variable(torch.randn(SAMPLES, HIDDEN2))
            if USE_CUDA:
                sample = sample.cuda()

            c = torch.zeros(SAMPLES).long().random_(0, 10).float()

            sample = model.decode(sample, Variable(c)).cpu()
            # pdb.set_trace()
            save_image(sample.data.view(SAMPLES, 1, img_width, img_height),
                       'results/sample_meh_' + str(epoch) + '.png')

elif args.model == 'VAE':
    model = model.VAE(img_width * img_height, HIDDEN1, HIDDEN2)
    if USE_CUDA:
        model.cuda()

    optimizer = optim.Adam(model.parameters(), lr=LR)

    for epoch in range(1, args.epochs + 1):
        utils.train(model, train_loader, epoch, optimizer)
        #utils.eval(model, val_loader, epoch)
コード例 #22
0
ファイル: explore_centroids.py プロジェクト: argsim/argsim
from util_io import load_txt, save_txt
from util_np import np, partition, vpack
from util_np import vpack
import pandas as pd
import util_sp as sp

# load data
df = pd.read_csv(path_csv)
emb = np.load(path_emb)
emb_sp = np.load(path_emb_sp)

# load sentencepiece model
vocab = sp.load_spm(path_vocab)

# Load the model
model = vAe('infer')
# Restore the session
sess = tf.InteractiveSession()
tf.train.Saver().restore(sess, path_ckpt)

###########################
# generate from centroids #
###########################

for col in "euc euc_sp cos cos_sp".split():
    cluster = df["cls_{}".format(col)].values
    centroids = np.stack(
        [np.mean(emb[cluster == c], axis=0) for c in range(cluster.max() + 1)])
    y = decode(sess, model, centroids, steps=512)
    save_txt("../trial/centroids_{}".format(col), sp.decode(vocab, y))
コード例 #23
0
ファイル: train.py プロジェクト: dung98pt/chem_vae_branch
def VAE():
  inputs = Input(shape=(120,35))
  z_mean, z_log_var, z = encode(inputs)
  x = decode([inputs, z])
  VAE = Model(inputs, x, name = "VAE")
  return VAE, z_mean, z_log_var