-
Notifications
You must be signed in to change notification settings - Fork 5
/
denoise_train.py
executable file
·401 lines (292 loc) · 14.6 KB
/
denoise_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import Denoisenet
import tensorflow as tf
import argparse
from random import shuffle
import time
import os
from tensorflow.python.client import timeline
import denoise_input
PATCH_SHAPE = (128 ,128, 3)
LOGDIR = "./tmp/logs/"
def random_mini_batches(X_train, y_train, mini_batch_size=200, seed = 2):
"""Helper function to extract mini batches from the whole training set.
Input:
X_train: Noisy Patches training set
y_train: Clean Patches training set (long exposures)
mini_batch_size: Number of patches contained in a mini batch
seed: We use different seeds each epoch to maintain randomness.
Output:
minibatches: python list of tuple (X_batch, y_batch)
"""
"""Number of batches and extra batch that does not fit into a whole batch"""
num_batches = X_train.shape[0] // mini_batch_size
res = X_train.shape[0] % mini_batch_size
"""Shuffle entire dataset"""
sidx = np.arange(X_train.shape[0])
np.random.shuffle(sidx)
X = X_train[sidx]
y = y_train[sidx]
"""Create an index array that refer to the multiples of mini_batch_size"""
np.random.seed(seed)
idx = np.arange(X_train.shape[0] - res)
idx = idx.reshape(-1, mini_batch_size)
"""Create a view that refer X_train and y_train up to multiples of mini_batch_size"""
X_train_mul = X[0:num_batches * mini_batch_size, :]
y_train_mul = y[0:num_batches * mini_batch_size, :]
"""Extract the multiple parts into mini_batches"""
X_train_minis = X_train[idx, :]
y_train_minis = y_train[idx, :]
assert X_train_minis.shape == (num_batches, mini_batch_size, PATCH_SHAPE[0], PATCH_SHAPE[1], PATCH_SHAPE[2]), "X shape error: %s" % str(X_train_minis.shape)
assert y_train_minis.shape == (num_batches, mini_batch_size, PATCH_SHAPE[0], PATCH_SHAPE[1], PATCH_SHAPE[2]), "y shape error: %s" % str(y_train_minis.shape)
minibatches = []
for i in range(X_train_minis.shape[0]):
minibatches.append((X_train_minis[i], y_train_minis[i]))
if not res == 0:
"""Append the fractional parts"""
X_train_res = X[num_batches * mini_batch_size:, :]
y_train_res = y[num_batches * mini_batch_size:, :]
minibatches.append((X_train_res, y_train_res))
assert len(minibatches) == num_batches + 1
return minibatches
def train(X, y, learning_rate = 1e-3, mini_batch_size = 16, debug = False):
"""Function to train deep denoise model
Input:
X: numpy array of size (N, PATCH_SHAPE), noisy patches input
y: numpy array of size X.shape, clean patches input
learning_rate: learning rate of gradient descent
mini_batch_size: number of patches of each mini batch
num_epochs: number of epochs to train on
Dependencies:
feed_forward(), where model is defined.
Console Output:
epoch_cost: Summed meaned squred error of each epoch
File Output:
model: Using tf.Saver() to save trained model.
Return:
loss_hist: list of all losses.
"""
with tf.Graph().as_default():
global_step = tf.train.get_or_create_global_step()
# Build Model
with tf.device("cpu:0"):
X_train = tf.placeholder(dtype=tf.float32, shape=(None, 128, 128, 3), name = "X_train")
y_train = tf.placeholder(dtype=tf.float32, shape=(None, 128, 128, 3), name = "y_train")
tf.summary.image('input', X_train, 3)
tf.summary.image('groundtruth', y_train, 3)
with tf.device("gpu:1"):
print ("Y Channel Net")
denoisedY, lossY = Denoisenet.feed_forward(X_train[:, :, :, 0], y_train[:, :, :, 0], scope = "Y")
with tf.device("gpu:2"):
print ("Cb Channel Net")
denoisedCb, lossCb = Denoisenet.feed_forward(X_train[:, :, :, 1], y_train[:, :, :, 1], scope = "Cb")
with tf.device("gpu:3"):
print ("Cr Channel Net")
denoisedCr, lossCr = Denoisenet.feed_forward(X_train[:, :, :, 2], y_train[:, :, :, 2], scope = "Cr")
with tf.device("gpu:0"):
denoised = tf.stack([denoisedY[:,:,:,0], denoisedCb[:,:,:,0], denoisedCr[:,:,:,0]], axis = 3, name = "denoised")
print (denoised.name)
loss = tf.reduce_sum([lossY, lossCb, lossCr], keep_dims = False)
tf.summary.scalar("Mean Squared Error", loss)
tf.summary.image('denoised', denoised, 3)
Y_vars = tf.trainable_variables(scope = "Y")
Cb_vars = tf.trainable_variables(scope = "Cb")
Cr_vars = tf.trainable_variables(scope = "Cr")
with tf.device("gpu:1"):
optim_Y = tf.train.AdamOptimizer(learning_rate = learning_rate)
gvs_Y = optim_Y.compute_gradients(loss, var_list = Y_vars)
trainop_Y = optim_Y.apply_gradients(gvs_Y)
with tf.device("gpu:2"):
optim_Cb = tf.train.AdamOptimizer(learning_rate = learning_rate)
gvs_Cb = optim_Y.compute_gradients(loss, var_list = Cb_vars)
trainop_Cb = optim_Cb.apply_gradients(gvs_Cb)
with tf.device("gpu:3"):
optim_Cr = tf.train.AdamOptimizer(learning_rate = learning_rate)
gvs_Cr = optim_Y.compute_gradients(loss, var_list = Cr_vars)
trainop_Cr = optim_Cr.apply_gradients(gvs_Cr)
init = tf.global_variables_initializer()
# Clear previously saved model
model_dir = "./Model/denoisenet"
for f in os.listdir(model_dir):
os.remove(os.path.join(model_dir, f))
for f in os.listdir(LOGDIR):
os.remove(os.path.join(LOGDIR, f))
config = tf.ConfigProto(allow_soft_placement = True)
with tf.Session(config = config) as sess:
if debug:
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
else:
run_options = tf.RunOptions(trace_level=tf.RunOptions.NO_TRACE)
run_metadata = tf.RunMetadata()
sess.run(init)
saver = tf.train.Saver()
seed = 0 #Seed to shuffle mini batches
smooth_loss = 0
smooth_time = 0
smooth_alpha = 0.9
summ = tf.summary.merge_all()
writer = tf.summary.FileWriter(LOGDIR)
writer.add_graph(sess.graph)
tf.Graph().finalize() #No update to graph should be performed in the loop
i = 0
while(True):
cost = 0.
"""Extract into mini batches and shuffle"""
seed += 1
minibatches = random_mini_batches(X, y, mini_batch_size, seed)
for minibatch in minibatches:
(mini_batch_X, mini_batch_y) = minibatch
mini_batch_X = mini_batch_X.astype('float32') / 255.
mini_batch_y = mini_batch_y.astype('float32') / 255.
mini_batch_count = mini_batch_X.shape[0]
t = time.time()
_, _, _, mini_batch_loss, s, d = sess.run([trainop_Y, trainop_Cb, trainop_Cr, loss, summ, denoised], feed_dict = {X_train : mini_batch_X, y_train: mini_batch_y}, options=run_options, run_metadata=run_metadata)
t = time.time() - t
cost += mini_batch_loss / mini_batch_count
writer.add_summary(s, i)
smooth_loss = (smooth_alpha) * smooth_loss + (1-smooth_alpha) * cost
smooth_time = (smooth_alpha) * smooth_time + (1-smooth_alpha) * t
print ("Loss after iterations %d, %f, smooth, %f, time this iter %f, smooth %f" % (i, cost, smooth_loss, t, smooth_time))
if debug:
# Create the Timeline object, and write it to a json
tl = timeline.Timeline(run_metadata.step_stats)
ctf = tl.generate_chrome_trace_format()
with open('./tmp/timeline.json', 'w') as f:
f.write(ctf)
i += 1
if i % 1000 == 0:
saver.save(sess, './Model/denoisenet/denoise_model_I%d.ckpt' % i, global_step = i)
def train_tensors(X, y, learning_rate = 1e-3, mini_batch_size = 16, debug = False):
"""Function to train deep denoise model
Input:
X: tensor of size (N, PATCH_SHAPE), noisy patches input
y: tensor of size X.shape, clean patches input
learning_rate: learning rate of gradient descent
mini_batch_size: number of patches of each mini batch
num_epochs: number of epochs to train on
Dependencies:
feed_forward(), where model is defined.
Console Output:
epoch_cost: Summed meaned squred error of each epoch
File Output:
model: Using tf.Saver() to save trained model.
Return:
loss_hist: list of all losses.
"""
with tf.Graph().as_default():
global_step = tf.train.get_or_create_global_step()
# Build Model
with tf.device("/gpu:0"):
tf.summary.image('input', X, 3)
tf.summary.image('groundtruth', y, 3)
with tf.device("/gpu:0"):
print ("Y Channel Net")
denoisedY, lossY = Denoisenet.feed_forward(X[:, :, :, 0], y[:, :, :, 0], scope = "Y")
with tf.device("/gpu:1"):
print ("Cb Channel Net")
denoisedCb, lossCb = Denoisenet.feed_forward(X[:, :, :, 1], y[:, :, :, 1], scope = "Cb")
with tf.device("/gpu:2"):
print ("Cr Channel Net")
denoisedCr, lossCr = Denoisenet.feed_forward(X[:, :, :, 2], y[:, :, :, 2], scope = "Cr")
with tf.device("/gpu:0"):
denoised = tf.stack([denoisedY[:,:,:,0], denoisedCb[:,:,:,0], denoisedCr[:,:,:,0]], axis = 3, name = "denoised")
print (denoised.name)
loss = tf.reduce_sum([lossY, lossCb, lossCr], keep_dims = False)
tf.summary.scalar("Mean Squared Error", loss)
tf.summary.image("denoised", denoised, 3)
Y_vars = tf.trainable_variables(scope = "Y")
Cb_vars = tf.trainable_variables(scope = "Cb")
Cr_vars = tf.trainable_variables(scope = "Cr")
with tf.device("/gpu:0"):
optim_Y = tf.train.AdamOptimizer(learning_rate = learning_rate)
gvs_Y = optim_Y.compute_gradients(loss, var_list = Y_vars)
trainop_Y = optim_Y.apply_gradients(gvs_Y)
with tf.device("/gpu:1"):
optim_Cb = tf.train.AdamOptimizer(learning_rate = learning_rate)
gvs_Cb = optim_Y.compute_gradients(loss, var_list = Cb_vars)
trainop_Cb = optim_Cb.apply_gradients(gvs_Cb)
with tf.device("/gpu:2"):
optim_Cr = tf.train.AdamOptimizer(learning_rate = learning_rate)
gvs_Cr = optim_Y.compute_gradients(loss, var_list = Cr_vars)
trainop_Cr = optim_Cr.apply_gradients(gvs_Cr)
init = tf.global_variables_initializer()
# Clear previously saved model
model_dir = "./Model/denoisenet"
for f in os.listdir(model_dir):
os.remove(os.path.join(model_dir, f))
for f in os.listdir(LOGDIR):
os.remove(os.path.join(LOGDIR, f))
config = tf.ConfigProto(allow_soft_placement = True)
with tf.Session(config = config) as sess:
if debug:
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
else:
run_options = tf.RunOptions(trace_level=tf.RunOptions.NO_TRACE)
run_metadata = tf.RunMetadata()
sess.run(init)
saver = tf.train.Saver()
seed = 0 #Seed to shuffle mini batches
summ = tf.summary.merge_all()
writer = tf.summary.FileWriter(LOGDIR)
writer.add_graph(sess.graph)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
i = 0
smooth_loss = 1.
smooth_time = 1.
smooth_alpha = 0.9
tf.Graph().finalize() #No update to graph should be performed in the loop
try:
while not coord.should_stop():
cost = 0.
t = time.time()
_, _, _, mini_batch_loss, s = sess.run([trainop_Y, trainop_Cb, trainop_Cr, loss, summ], options=run_options, run_metadata=run_metadata)
t = time.time() - t
cost += mini_batch_loss / mini_batch_size
writer.add_summary(s, i)
i += 1
smooth_loss = (smooth_alpha) * smooth_loss + (1-smooth_alpha) * cost
smooth_time = (smooth_alpha) * smooth_time + (1-smooth_alpha) * t
print ("Loss after iterations %d, %f, smooth %f, time this iter %f, smooth %f" % (i, cost, smooth_loss, t, smooth_time))
if debug:
# Create the Timeline object, and write it to a json
tl = timeline.Timeline(run_metadata.step_stats)
ctf = tl.generate_chrome_trace_format()
with open('./tmp/timeline.json', 'w') as f:
f.write(ctf)
if i % 1000 == 0:
saver.save(sess, './Model/denoisenet/denoise_model_I%d.ckpt' % i, global_step = i)
except tf.errors.OutOfRangeError:
print('Finished one epoch, %d steps' % i)
finally:
coord.request_stop()
coord.join(threads)
sess.close()
def main(args):
lr = args.learning_rate if args.learning_rate else 1e-3
mbs = args.mini_batch_size if args.mini_batch_size else 16
print ("Training with learning rate %E, batch size %d" % (lr, mbs))
if args.dataset_file:
dataset_file = args.dataset_file
cims, nims = denoise_input.input_tfRecords(dataset_file, mbs)
train_tensors(nims, cims, learning_rate = lr)
else:
clean_patches_root = args.clean_patches_root
noisy_patches_root = args.noisy_patches_root
clean_patches, noisy_patches = denoise_input.load_patches(CleanRoot = clean_patches_root, NoisyRoot = noisy_patches_root)
train(X = noisy_patches, y = clean_patches, learning_rate = lr, mini_batch_size = mbs)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = "Trainer for deep denoise net.")
parser.add_argument('--clean_patches_root', '-cpr', type=str, help="Path to the folder of clean patches", default="./Images/CleanPatches/")
parser.add_argument('--noisy_patches_root', '-npr', type=str, help="Path to the folder of noisy pathces", default="./Images/NoisyPatches/")
parser.add_argument('--dataset_file', '-df', type=str, help="Path to tfrecord file, if defined, cpr and npr will be ignored.")
parser.add_argument('--learning_rate', '-lr', type=float, help="Learning Rate of train")
parser.add_argument('--mini_batch_size', '-mbs', type=int, help="Mini batch size")
args = parser.parse_args()
os.makedirs("./tmp/logs/", exist_ok=True)
main(args)