-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain.py
executable file
ยท182 lines (152 loc) ยท 4.54 KB
/
train.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import numpy as np
import canton as ct
from canton import *
import matplotlib.pyplot as plt
# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# get the
def cifar():
from keras.datasets import cifar10
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
return X_train
def encoder():
c=Can()
def conv(nip,nop,tail=True):
c.add(Conv2D(nip,nop,k=3,usebias=True))
if tail:
# c.add(BatchNorm(nop))
c.add(Act('elu'))
c.add(Lambda(lambda x:x-0.5))
conv(3,16)
conv(16,32)
conv(32,64)
conv(64,128,tail=False)
c.chain()
return c
def decoder():
c=Can()
def conv(nip,nop,tail=True):
c.add(Conv2D(nip,nop,k=3,usebias=True))
if tail:
# c.add(BatchNorm(nop))
c.add(Act('elu'))
conv(128,64)
conv(64,32)
conv(32,16)
conv(16,3,tail=False)
c.add(Act('sigmoid'))
c.chain()
return c
def get_trainer():
x = ph([None,None,3])
# augment the training set by adding random gain and bias pertubation
sx = tf.shape(x)
input_gain = tf.random_uniform(
minval=0.6,
maxval=1.4,
shape=[sx[0],1,1,1])
input_bias = tf.random_uniform(
minval=-.2,
maxval=.2,
shape=[sx[0],1,1,1])
noisy_x = x * input_gain + input_bias
noisy_x = tf.clip_by_value(noisy_x,clip_value_max=1.,clip_value_min=0.)
code_noise = tf.Variable(0.1)
linear_code = enc(noisy_x)
# add gaussian before sigmoid to encourage binary code
noisy_code = linear_code + \
tf.random_normal(stddev=code_noise,shape=tf.shape(linear_code))
binary_code = Act('sigmoid')(noisy_code)
y = dec(binary_code)
loss = tf.reduce_mean((y-noisy_x)**2) + tf.reduce_mean(binary_code**2) * 0.01
opt = tf.train.AdamOptimizer()
train_step = opt.minimize(loss,
var_list=enc.get_weights()+dec.get_weights())
def feed(batch,cnoise):
sess = ct.get_session()
res = sess.run([train_step,loss],feed_dict={
x:batch,
code_noise:cnoise,
})
return res[1]
set_training_state(False)
quantization_threshold = tf.Variable(0.5)
binary_code_test = tf.cast(binary_code>quantization_threshold,tf.float32)
y_test = dec(binary_code_test)
def test(batch,quanth):
sess = ct.get_session()
res = sess.run([binary_code_test,y_test,binary_code,y,noisy_x],feed_dict={
x:batch,
quantization_threshold:quanth,
})
return res
return feed,test
def r(ep=1,cnoise=0.1):
np.random.shuffle(xt)
length = len(xt)
bs = 20 #interval second
for i in range(ep):
print('ep',i)
for j in range(0,length,bs):
minibatch = xt[j:j+bs]
loss = feed(minibatch,cnoise)
print(j,'loss:',loss)
if j%1000==0:
show()
def show(threshold=.5):
from cv2tools import vis,filt
bs = 16
j = np.random.choice(len(xt)-16)
minibatch = xt[j:j+bs]
code, rec, code2, rec2, noisy_x = test(minibatch,threshold)
code = np.transpose(code[0:1],axes=(3,1,2,0))
code2 = np.transpose(code2[0:1],axes=(3,1,2,0))
vis.show_batch_autoscaled(code, name='code(quant)', limit=600.)
vis.show_batch_autoscaled(code2, name='code2(no quant)', limit=600.)
vis.show_batch_autoscaled(noisy_x,name='input')
vis.show_batch_autoscaled(rec,name='recon(quant)')
vis.show_batch_autoscaled(rec2,name='recon(no quant)')
def save():
print(enc, "enc")
enc.save_weights('enc.npy')
dec.save_weights('dec.npy')
def load():
enc.load_weights('enc.npy')
dec.load_weights('dec.npy')
enc,dec = encoder(),decoder()
enc.summary()
dec.summary()
xt = cifar()
if __name__ == '__main__':
feed,test = get_trainer()
get_session().run(ct.gvi())
# for i in range(5):
# r(cnoise=15.0)
# save weights to file
# save()
# load weights from file
# load()
# test model on randomly sampled CIFAR
# randomly select input image
index = np.random.randint(len(xt))
# plot the image
plt.imshow(xt[index])
plt.gray()
# print(xt[index])
# load()
# show()
# for i in [15.0]:
# print(i)
# r(cnoise=i)
# save()
#