-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtf_vae_mnist.py
More file actions
131 lines (86 loc) · 3.39 KB
/
Copy pathtf_vae_mnist.py
File metadata and controls
131 lines (86 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import tensorflow as tf
import pickle
import matplotlib.pyplot as plt
from PIL import Image
class Loader():
def __init__(self, path = '', max_items = 60000):
self.path = path
self.max_count = max_items
self.counter = 0
def sample(self,batch_size = 32, use_cuda = False):
data = np.zeros((batch_size, 784))
ind = np.random.randint(0,self.max_count, (batch_size))
for i in range(batch_size):
name = self.path+'{}'.format(ind[i])
image = pickle.load(open(name,'rb'))
data[i,:] = image.reshape(784)
self.counter = (self.counter+1)%self.max_count
data /= 255.
return data
def make_grid(x, rows = 4, decal = 2):
# x is a (batch_size, nb_channels, height, width) nd-array
col = int(x.shape[0]/rows)
image = np.zeros(((x.shape[2] + decal)*rows, (decal + x.shape[3])*col, 3))
ligne = 0
column = 0
for i in range(x.shape[0]):
current = x[i,:,:,:]
current = np.transpose(current, [1,2,0])
image[decal + ligne*(x.shape[2]):(ligne+1)*(x.shape[2]) + decal, decal + column*(x.shape[3]):(column+1)*(x.shape[3]) + decal,:] = current
column = (column + 1)%col
if(column == 0):
ligne += 1
# input(image.shape)
return image
dense = tf.layers.dense
# relu = tf.nn.relu(alpha = 0.2)
code_size = 20
x = tf.placeholder(tf.float32, shape = [None, 784], name = 'Input_x')
with tf.variable_scope('Encoder'):
e1 = dense(x, 512, activation = tf.nn.relu, name = 'encoding_1')
e1 = dense(e1, 384, activation = tf.nn.relu, name = 'encoding_2')
e1= dense(e1, 256, activation = tf.nn.relu, name = 'encoding_3')
with tf.variable_scope('Reparametrization'):
z_means = dense(e1, code_size, name = 'means')
z_stds = dense(e1, code_size, name = 'logvar')
with tf.variable_scope('Code'):
z = z_means + tf.random_normal(tf.shape(z_stds))*tf.exp(z_stds)
with tf.variable_scope('Decoder'):
d1 = dense(z, 256, activation = tf.nn.relu, name = 'decoding_1')
d1 = dense(d1, 384, activation = tf.nn.relu, name = 'decoding_2')
d2 = dense(d1, 512, activation = tf.nn.relu, name = 'decoding_3')
out = dense(d2, 784, name = 'reconstruction', activation = tf.nn.sigmoid)
with tf.variable_scope('Losses'):
kl_loss = -0.5*tf.reduce_sum(1. + z_stds - tf.pow(z_means,2) - tf.exp(z_stds), axis = 1)
# recon_loss = tf.losses.mean_squared_error(x, out, reduction_indices = 1)
recon_loss = tf.reduce_sum(tf.square(out - x), axis = 1)
with tf.variable_scope('Training'):
full_loss = tf.reduce_mean(kl_loss + recon_loss)
update_vae = tf.train.AdamOptimizer(5e-4).minimize(full_loss)
epochs = 2500
loader = Loader('/home/mehdi/Codes/MNIST/',60000)
f, ax = plt.subplots(1,2)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
mean_loss = 0.
for epoch in range(1,epochs+1):
data_x = loader.sample(64)
loss, _ = sess.run([full_loss, update_vae], feed_dict = {x:data_x})
mean_loss += np.mean(loss)
if epoch% 100 == 0:
# input(loss)
print('Epoch: {} | Loss: {:.6f}'.format(epoch, mean_loss/100.))
mean_loss = 0.
if epoch % 100 == 0:
data_x = loader.sample(32)
result = sess.run([out], feed_dict = {z:np.random.normal(0.,1., (32,code_size))})[0]
recons = sess.run([out], feed_dict = {x:data_x})[0]
for a in ax:
a.clear()
ax[0].imshow(make_grid(recons.reshape(32,1,28,28)))
ax[1].imshow(make_grid(result.reshape(32,1,28,28)))
ax[0].set_title('Recons')
ax[1].set_title('Prods')
plt.pause(0.1)
plt.show()