-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsae.py
135 lines (97 loc) · 3.77 KB
/
sae.py
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
132
133
134
135
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
#====== random seeds ==================
# makes sure results remain comparable
np.random.seed(0)
tf.set_random_seed(0)
#====== custom modules ==================
import datasets
import uniio
#====== fetch the data ==================
sdf_data = datasets.read_data_sets('training_data')
n_samples = sdf_data.train.num_examples
#====== your part ==================
sess = tf.InteractiveSession()
#============ model definition =============
# define the computational graph
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
n_code = 1000
x_image = tf.placeholder(tf.float32, shape=[None, 64, 64, 3])
x = tf.reshape(x_image, [-1,12288])
alphas = tf.placeholder(tf.float32, shape=[None, 1])
W_1 = weight_variable([12288, n_code])
b_1 = bias_variable([n_code])
z=tf.nn.tanh(tf.matmul(x, W_1) + b_1)
W_2 = weight_variable([n_code, 12288])
b_2 = bias_variable([12288])
y = tf.nn.tanh(tf.matmul(z, W_2) + b_2)
y_image = tf.reshape(y, [-1,64,64,3])
#============ training your model =============
l2_loss = tf.nn.l2_loss(y - x)
norm = tf.nn.l2_loss(x)
weight_penalty = tf.add_n([tf.nn.l2_loss(v) for v in tf.trainable_variables()])
loss = l2_loss + 0.001*weight_penalty
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
init_op = tf.initialize_all_variables()
saver = tf.train.Saver()
sess.run(init_op)
# train the model
#'''
for i in range(1000):
batch = sdf_data.train.next_batch(1)
if i%100 == 0:
train_loss = loss.eval(feed_dict={x_image:batch[0], alphas: batch[1]})
print("step %d, training loss %g"%(i, train_loss))
train_step.run(feed_dict={x_image: batch[0], alphas: batch[1]})
# save the trained model
model_file = saver.save(sess, "model.ckpt")
print("Trained model saved to %s"%model_file)
#'''
# alternatively restore the model
# this will be used for your presentation instead of training
#saver.restore(sess, "model.ckpt")
#============ score =============
#Do not alter this part
err = l2_loss.eval(feed_dict={x_image: sdf_data.test.inputs, alphas: sdf_data.test.labels})
print("validation loss: %g"%err)
err = err / norm.eval(feed_dict={x_image: sdf_data.test.inputs, alphas: sdf_data.test.labels})
score = (1 - n_code / float(64*64*3)) * (1 - err)
print("Your score is %g"%score)
#============ validating your model =============
for i in range(5):
ref = sdf_data.test.inputs[i]
gen = sess.run(y_image, feed_dict={x_image:[ref]})
fig, [ax1, ax2]= plt.subplots(1, 2, figsize=(6, 3))
_ = ax1.quiver(ref[:,:,0], ref[:,:,1], pivot='tail', color='k', scale=1 / 1)
_ = ax2.quiver(gen[0,:,:,0], gen[0,:,:,1], pivot='tail', color='k', scale=1 / 1)
ax1.set_xlim(0, 60)
ax1.set_ylim(0, 60)
ax2.set_xlim(0, 60)
ax2.set_ylim(0, 60)
ax1.get_xaxis().set_visible(False)
ax1.get_yaxis().set_visible(False)
ax2.get_xaxis().set_visible(False)
ax2.get_yaxis().set_visible(False)
plt.show()
# Docker users should uncomment the following line
# It will save the graphs to the disk for viewing
fig.savefig("validate_%g.png" % i)
#====== write the data ==================
print("Saving outputs...")
save_dir = 'output_data'
head, _ = uniio.readuni('./training_data/vel_000000.uni')
all_data = np.concatenate((sdf_data.train.inputs, sdf_data.test.inputs), axis=0)
all_labels = np.concatenate((sdf_data.train.labels, sdf_data.test.labels), axis=0)
N = all_labels.shape[0]
for i in range(N):
enc = 100*sess.run(y_image, feed_dict={x_image:[all_data[i]]})
loc = save_dir + '/vel_%06d.uni' % all_labels[i]
uniio.writeuni(loc, head, enc)
print("Output data succesfully saved to %s"%save_dir)
sess.close()