-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
308 lines (261 loc) · 11.5 KB
/
model.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
'''
DnCNN
paper: Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising
a tensorflow version of the network DnCNN
just for personal exercise
author: momogary
'''
import tensorflow as tf
import numpy as np
import math, os
from glob import glob
from ops import *
from utils import *
from six.moves import xrange
import time
class DnCNN(object):
def __init__(self, sess, image_size=40, batch_size=128,
output_size=40, input_c_dim=1, output_c_dim=1,
sigma=25, clip_b=0.025, lr=0.01, epoch=50,
ckpt_dir='./checkpoint', sample_dir='./sample',
test_save_dir='./test',
dataset='BSD400', testset='Set12'):
self.sess = sess
self.is_gray = (input_c_dim == 1)
self.batch_size = batch_size
self.image_size = image_size
self.output_size = output_size
self.input_c_dim = input_c_dim
self.output_c_dim = output_c_dim
self.sigma = sigma
self.clip_b = clip_b
self.lr = lr
self.numEpoch = epoch
self.ckpt_dir = ckpt_dir
self.trainset = dataset
self.testset = testset
self.sample_dir = sample_dir
self.test_save_dir = test_save_dir
self.epoch = epoch
self.save_every_iter = 1
self.eval_every_iter = 1
# Adam setting (default setting)
self.beta1 = 0.9
self.beta2 = 0.999
self.alpha = 0.01
self.epsilon = 1e-8
self.build_model()
def build_model(self):
# input : [batchsize, image_size, image_size, channel]
self.X = tf.placeholder(tf.float32, \
[None, self.image_size, self.image_size, self.input_c_dim], \
name='noisy_image')
self.X_ = tf.placeholder(tf.float32, \
[None, self.image_size, self.image_size, self.input_c_dim], \
name='clean_image')
# layer 1
with tf.variable_scope('conv1'):
layer_1_output = self.layer(self.X, [3, 3, self.input_c_dim, 64], useBN=False)
# layer 2 to 16
with tf.variable_scope('conv2'):
layer_2_output = self.layer(layer_1_output, [3, 3, 64, 64])
with tf.variable_scope('conv3'):
layer_3_output = self.layer(layer_2_output, [3, 3, 64, 64])
with tf.variable_scope('conv4'):
layer_4_output = self.layer(layer_3_output, [3, 3, 64, 64])
with tf.variable_scope('conv5'):
layer_5_output = self.layer(layer_4_output, [3, 3, 64, 64])
with tf.variable_scope('conv6'):
layer_6_output = self.layer(layer_5_output, [3, 3, 64, 64])
with tf.variable_scope('conv7'):
layer_7_output = self.layer(layer_6_output, [3, 3, 64, 64])
with tf.variable_scope('conv8'):
layer_8_output = self.layer(layer_7_output, [3, 3, 64, 64])
with tf.variable_scope('conv9'):
layer_9_output = self.layer(layer_8_output, [3, 3, 64, 64])
with tf.variable_scope('conv10'):
layer_10_output = self.layer(layer_9_output, [3, 3, 64, 64])
with tf.variable_scope('conv11'):
layer_11_output = self.layer(layer_10_output, [3, 3, 64, 64])
with tf.variable_scope('conv12'):
layer_12_output = self.layer(layer_11_output, [3, 3, 64, 64])
with tf.variable_scope('conv13'):
layer_13_output = self.layer(layer_12_output, [3, 3, 64, 64])
with tf.variable_scope('conv14'):
layer_14_output = self.layer(layer_13_output, [3, 3, 64, 64])
with tf.variable_scope('conv15'):
layer_15_output = self.layer(layer_14_output, [3, 3, 64, 64])
with tf.variable_scope('conv16'):
layer_16_output = self.layer(layer_15_output, [3, 3, 64, 64])
# layer 17
with tf.variable_scope('conv17'):
self.Y = self.layer(layer_16_output, [3, 3, 64, self.output_c_dim], useBN=False)
# L2 loss
self.Y_ = self.X - self.X_ # noisy image - clean image
self.loss = (1.0 / self.batch_size) * tf.nn.l2_loss(self.Y - self.Y_)
optimizer = tf.train.AdamOptimizer(self.lr, name='AdamOptimizer')
self.train_step = optimizer.minimize(self.loss)
# create this init op after all variables specified
self.init = tf.global_variables_initializer()
self.saver = tf.train.Saver()
print("[*] Initialize model successfully...")
def conv_layer(self, inputdata, weightshape, b_init, stridemode):
# weights
W = tf.get_variable('weights', weightshape, initializer = \
tf.constant_initializer(get_conv_weights(weightshape, self.sess)))
b = tf.get_variable('biases', [1, weightshape[-1]], initializer = \
tf.constant_initializer(b_init))
# convolutional layer
return tf.nn.conv2d(inputdata, W, strides=stridemode, padding="SAME") + b # SAME with zero padding
def bn_layer(self, logits, output_dim, b_init = 0.0):
alpha = tf.get_variable('bn_alpha', [1, output_dim], initializer = \
tf.constant_initializer(get_bn_weights([1, output_dim], self.clip_b, self.sess)))
beta = tf.get_variable('bn_beta', [1, output_dim], initializer = \
tf.constant_initializer(b_init))
return batch_normalization(logits, alpha, beta, isCovNet = True)
def layer(self, inputdata, filter_shape, b_init = 0.0, stridemode=[1,1,1,1], useBN = True):
logits = self.conv_layer(inputdata, filter_shape, b_init, stridemode)
if useBN:
output = tf.nn.relu(self.bn_layer(logits, filter_shape[-1]))
else:
output = tf.nn.relu(logits)
return output
def train(self):
# init the variables
self.sess.run(self.init)
# get data
test_files = glob('./data/test/{}/*.png'.format(self.testset))
test_data = load_images(test_files) # list of array of different size, 4-D
data = load_data(filepath='./data/img_clean_pats.npy')
numBatch = int(data.shape[0] / self.batch_size)
print("[*] Data shape = " + str(data.shape))
counter = 0
print("[*] Start training : ")
start_time = time.time()
for epoch in xrange(self.epoch):
for batch_id in xrange(numBatch):
batch_images = data[batch_id*self.batch_size:(batch_id+1)*self.batch_size, :, :, :]
train_images = add_noise(batch_images, self.sigma, self.sess)
_, loss = self.sess.run([self.train_step, self.loss], \
feed_dict={self.X:train_images, self.X_:batch_images})
print("Epoch: [%2d] [%4d/%4d] time: %4.4f, loss: %.6f" \
% (epoch + 1, batch_id + 1, numBatch,
time.time() - start_time, loss))
counter += 1
if np.mod(counter, self.eval_every_iter) == 0:
self.evaluate(epoch, counter, test_data)
# save the model
if np.mod(counter, self.save_every_iter) == 0:
self.save(counter)
print("[*] Finish training.")
def save(self,counter):
model_name = "DnCNN.model"
model_dir = "%s_%s_%s" % (self.trainset, \
self.batch_size, self.image_size)
checkpoint_dir = os.path.join(self.ckpt_dir, model_dir)
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
print("[*] Saving model...")
self.saver.save(self.sess,
os.path.join(checkpoint_dir, model_name),
global_step=counter)
def sampler(self, image):
# set reuse flag to True
#tf.get_variable_scope().reuse_variables()
self.X_test = tf.placeholder(tf.float32, \
image.shape, name='noisy_image_test')
# layer 1 (adpat to the input image)
with tf.variable_scope('conv1', reuse=True):
layer_1_output = self.layer(self.X_test, [3, 3, self.input_c_dim, 64], useBN=False)
# layer 2 to 16
with tf.variable_scope('conv2', reuse=True):
layer_2_output = self.layer(layer_1_output, [3, 3, 64, 64])
with tf.variable_scope('conv3', reuse=True):
layer_3_output = self.layer(layer_2_output, [3, 3, 64, 64])
with tf.variable_scope('conv4', reuse=True):
layer_4_output = self.layer(layer_3_output, [3, 3, 64, 64])
with tf.variable_scope('conv5', reuse=True):
layer_5_output = self.layer(layer_4_output, [3, 3, 64, 64])
with tf.variable_scope('conv6', reuse=True):
layer_6_output = self.layer(layer_5_output, [3, 3, 64, 64])
with tf.variable_scope('conv7', reuse=True):
layer_7_output = self.layer(layer_6_output, [3, 3, 64, 64])
with tf.variable_scope('conv8', reuse=True):
layer_8_output = self.layer(layer_7_output, [3, 3, 64, 64])
with tf.variable_scope('conv9', reuse=True):
layer_9_output = self.layer(layer_8_output, [3, 3, 64, 64])
with tf.variable_scope('conv10', reuse=True):
layer_10_output = self.layer(layer_9_output, [3, 3, 64, 64])
with tf.variable_scope('conv11', reuse=True):
layer_11_output = self.layer(layer_10_output, [3, 3, 64, 64])
with tf.variable_scope('conv12', reuse=True):
layer_12_output = self.layer(layer_11_output, [3, 3, 64, 64])
with tf.variable_scope('conv13', reuse=True):
layer_13_output = self.layer(layer_12_output, [3, 3, 64, 64])
with tf.variable_scope('conv14', reuse=True):
layer_14_output = self.layer(layer_13_output, [3, 3, 64, 64])
with tf.variable_scope('conv15', reuse=True):
layer_15_output = self.layer(layer_14_output, [3, 3, 64, 64])
with tf.variable_scope('conv16', reuse=True):
layer_16_output = self.layer(layer_15_output, [3, 3, 64, 64])
# layer 17
with tf.variable_scope('conv17', reuse=True):
self.Y_test = self.layer(layer_16_output, [3, 3, 64, self.output_c_dim], useBN=False)
def load(self, checkpoint_dir):
'''Load checkpoint file'''
print("[*] Reading checkpoint...")
model_dir = "%s_%s_%s" % (self.trainset, self.batch_size, self.image_size)
checkpoint_dir = os.path.join(checkpoint_dir, model_dir)
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name))
return True
else:
return False
def forward(self, noisy_image):
'''forward once'''
self.sampler(noisy_image)
return self.sess.run(self.Y_test, feed_dict={self.X_test:noisy_image})
def test(self):
"""Test DnCNN"""
# init variables
tf.initialize_all_variables().run()
test_files = glob(os.path.join(self.test_save_dir, '{}/*.png'.format(self.testset)))
# load testing input
print("[*] Loading test images ...")
test_data = load_images(test_files) # list of array of different size
if self.load(self.ckpt_dir):
print(" [*] Load SUCCESS")
else:
print(" [!] Load failed...")
psnr_sum = 0
for idx in xrange(len(test_files)):
noisy_image = add_noise(1 / 255.0 * test_data[idx], self.sigma, self.sess) # ndarray
predicted_noise = self.forward(noisy_image)
output_clean_image = (noisy_image - predicted_noise) * 255
# calculate PSNR
psnr = cal_psnr(test_data[idx], output_clean_image)
psnr_sum += psnr
save_images(test_data[idx], noisy_image * 255, output_clean_image, \
os.path.join(self.test_save_dir, '%s/test%d_%d_%d.png' % (self.testset, idx, epoch, counter)))
avg_psnr = psnr_sum / len(test_files)
print("--- Average PSNR %.2f ---" % avg_psnr)
def evaluate(self, epoch, counter, test_data):
'''Evaluate the model during training'''
#print("[*] Evaluating...")
psnr_sum = 0
for idx in xrange(len(test_files)):
noisy_image = add_noise(1 / 255.0 * test_data[idx], self.sigma, self.sess) # ndarray
predicted_noise = self.forward(noisy_image)
output_clean_image = (noisy_image - predicted_noise) * 255
groundtruth = np.clip(255 * test_data[idx], 0, 255).astype('uint8')
noisyimage = np.clip(noisy_image * 255, 0, 255).astype('uint8')
outputimage = np.clip(output_clean_image, 0, 255).astype('uint8')
# calculate PSNR
psnr = cal_psnr(groundtruth, outputimage)
psnr_sum += psnr
save_images(groundtruth, noisyimage, outputimage, \
os.path.join(self.sample_dir, 'test%d_%d_%d.png' % (idx, epoch, counter)))
avg_psnr = psnr_sum / len(test_files)
print("--- Test ---- Average PSNR %.2f ---" % avg_psnr)