<jupyter_start><jupyter_text>Generate handwritten digits with trained ResCVAE net<jupyter_code># Copyright (C) 2018 <NAME> <<EMAIL>>
import matplotlib
matplotlib.use('Agg')
%matplotlib inline
import matplotlib.pyplot as plt

import numpy as np
import tensorflow as tf
from rescvae.rescvae import rescvae
from rescvae.utils import utils
from rescvae.configs import config_mnist
from rescvae.configs import config_train<jupyter_output><empty_output><jupyter_text>Load mnist data<jupyter_code># Step1 load MNITST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=False, validation_size=5000)
cf = config_mnist
tr_cf = config_train<jupyter_output><empty_output><jupyter_text>Build the net<jupyter_code>net = rescvae(cf)
net.cvae_build()
net.net_print()<jupyter_output>(?, 14, 14, 8)
(?, 7, 7, 16)
(?, 14, 14, 8)
(?, 28, 28, 1)
Layer ID    Layer type    Layer shape
0 Input layer [None, 28, 28, 11]
1 Block_en_0 [None, 28, 28, 11]
2 Block_en_1 [None, 14, 14, 8]
3 En_flatten [None, 784]
4 En_mu [None, 128]
5 En_sigma [None, 128]
6 z [None, 128]
7 De_flatten [None, 784]
8 De_flatten_to_conv [None, 7, 7, 16]
9 Block_de_0 [None, 14, 14, 8]
10 Block_de_1 [None, 28, 28, 1]
11 Output layer [None, 28, 28, 1]
12 Flatten [None, 28, 28, 1]<jupyter_text>Restore the net<jupyter_code>net.get_learning_rate()
net.get_loss()
net.get_opt()

# load the net
import os
foldname = "./rescvae_mnist"
name = 'rescvae.pkl'
net.sess, name = utils.load_net(os.path.join(foldname, name))<jupyter_output>INFO:tensorflow:Restoring parameters from ./rescvae_mnist/model_rescvae.ckpt<jupyter_text>Generate new samples<jupyter_code>n_examples = 10

fig, axs = plt.subplots(cf.numclass, n_examples, figsize=(n_examples, cf.numclass))
for i in range(cf.numclass):
    z_gen = np.random.normal(0, 1, [n_examples, cf.z_length])
    # label
    labels = np.zeros([n_examples, cf.numclass])
    labels[:,i] = np.random.uniform(0.9,1.0)
    imgs = net.sess.run(
        net.output_flatten_de, 
        feed_dict = {net.z: z_gen, net.conditions: labels, 
                     net.is_training: False, 
                     net.keep_prob: 1.0})
    img_test = [np.reshape(imgs[i], [cf.rs, cf.rs]) for i in range(len(imgs))]
    for example_i in range(n_examples):
        # original
        axs[i][example_i].imshow(img_test[example_i], cmap='gray')
        axs[i][example_i].axis('off')

fig.show()
plt.draw()
plt.savefig("mnist_generated.png", bbox_inches='tight', dpi=200)<jupyter_output>/home/mzx/.local/lib/python3.6/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
  % get_backend())
{'fraction_non_alphanumeric': 0.13022917424518007, 'fraction_numerical': 0.044379774463441254, 'mean_word_length': 4.83864118895966, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '224442', 'license': 'MIT', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1063, 'n_words': 251, 'path': 'demo/demo-mnist/notebook-rescvae-mnist-generation.ipynb', 'repo_name': 'myinxd/rescvae', 'size': '2761'}