forked from wbhu/DnCNN-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
306 lines (263 loc) · 13.2 KB
/
model.py
File metadata and controls
306 lines (263 loc) · 13.2 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
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
import time
from glob import glob
import numpy as np
import tensorflow as tf
import random
import os
import cv2
def dncnn(input, is_training, output_channels=3):
layerIndex = 1
with tf.variable_scope('block%d' % layerIndex):
output = tf.layers.conv2d(input, 64, 3, padding='same', activation=tf.nn.relu)
layerIndex += 1
for layers in range(2, 19 + 1):
with tf.variable_scope('block%d' % layerIndex):
output = tf.layers.conv2d(output, 64, 3, padding='same', name='conv%d' % layerIndex, use_bias=False)
output = tf.nn.relu(tf.layers.batch_normalization(output, training=is_training))
layerIndex += 1
with tf.variable_scope('block%d' % layerIndex):
output = tf.layers.conv2d(output, output_channels, 3, padding='same', use_bias=False)
return input - output # Residual learning
filepaths = glob('./data/train/original/*.png') #takes all the paths of the png files in the train folder
filepaths = sorted(filepaths) #Order the list of files
filepaths_noisy = glob('./data/train/noisy/*.png')
filepaths_noisy = sorted(filepaths_noisy)
ind = list(range(len(filepaths)))
class denoiser(object):
def __init__(self, sess, input_c_dim=3, batch_size=128):
self.sess = sess
self.input_c_dim = input_c_dim
# build model
self.Y_ = tf.placeholder(tf.float32, [None, None, None, self.input_c_dim], name='clean_image')
self.is_training = tf.placeholder(tf.bool, name='is_training')
self.X = tf.placeholder(tf.float32, [None, None, None, self.input_c_dim], name='noisy')
self.Y = tf.identity(dncnn(self.X, is_training=self.is_training), name='denoised')
self.loss = (1.0 / batch_size) * tf.nn.l2_loss(self.Y_ - self.Y)
#self.loss = (1.0 / batch_size) * tf.reduce_sum(tf.square(self.Y_ - self.Y)) * 0.5
#self.loss = (1.0 / batch_size) * tf.reduce_sum(tf.abs(self.Y_ - self.Y))
self.lr = tf.placeholder(tf.float32, name='learning_rate')
self.dataset = dataset(sess)
optimizer = tf.train.AdamOptimizer(self.lr, name='AdamOptimizer')
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
self.train_op = optimizer.minimize(self.loss)
init = tf.global_variables_initializer()
self.sess.run(init)
print("[*] Initialize model successfully...")
def evaluate(self, iter_num, eval_files, noisy_files):
print("[*] Evaluating...")
psnr_sum = 0
for i in range(10):
clean_image = cv2.imread(eval_files[i])
clean_image = clean_image.astype('float32') / 255.0
clean_image = clean_image[np.newaxis, ...]
noisy = cv2.imread(noisy_files[i])
noisy = noisy.astype('float32') / 255.0
noisy = noisy[np.newaxis, ...]
output_clean_image = self.sess.run(
[self.Y],feed_dict={self.Y_: clean_image, self.X: noisy, self.is_training: False})
psnr = psnr_scaled(clean_image, output_clean_image)
print("img%d PSNR: %.2f" % (i + 1, psnr))
psnr_sum += psnr
avg_psnr = psnr_sum / 10
print("--- Test ---- Average PSNR %.2f ---" % avg_psnr)
def train(self, eval_files, noisy_files, batch_size, ckpt_dir, epoch, lr, eval_every_epoch=1):
numBatch = int(len(filepaths) * 2)
# load pretrained model
load_model_status, global_step = self.load(ckpt_dir)
if load_model_status:
iter_num = global_step
start_epoch = global_step // numBatch
start_step = global_step % numBatch
print("[*] Model restore success!")
else:
iter_num = 0
start_epoch = 0
start_step = 0
print("[*] Not find pretrained model!")
# make summary
tf.summary.scalar('loss', self.loss)
tf.summary.scalar('lr', self.lr)
#writer = tf.summary.FileWriter('./logs', self.sess.graph)
merged = tf.summary.merge_all()
clip_all_weights = tf.get_collection("max_norm")
print("[*] Start training, with start epoch %d start iter %d : " % (start_epoch, iter_num))
start_time = time.time()
self.evaluate(iter_num, eval_files, noisy_files) # eval_data value range is 0-255
for epoch in range(start_epoch, epoch):
batch_noisy = np.zeros((batch_size,64,64,3),dtype='float32')
batch_images = np.zeros((batch_size,64,64,3),dtype='float32')
lossSum = 0
for batch_id in range(start_step, numBatch):
try:
res = self.dataset.get_batch() # If we get an error retrieving a batch of patches we have to reinitialize the dataset
except KeyboardInterrupt:
raise
except:
self.dataset = dataset(self.sess) # Dataset re init
res = self.dataset.get_batch()
if batch_id==0:
batch_noisy = np.zeros((batch_size,64,64,3),dtype='float32')
batch_images = np.zeros((batch_size,64,64,3),dtype='float32')
ind1 = range(res.shape[0]//2)
ind1 = np.multiply(ind1,2)
for i in range(batch_size):
random.shuffle(ind1)
ind2 = random.randint(0,8-1)
batch_noisy[i] = res[ind1[0],ind2]
batch_images[i] = res[ind1[0]+1,ind2]
# for i in range(64):
# cv2.imshow('raw',batch_images[i])
# cv2.imshow('noisy',batch_noisy[i])
_, loss, summary = self.sess.run([self.train_op, self.loss, merged],
feed_dict={self.Y_: batch_images, self.X: batch_noisy, self.lr: lr[epoch],
self.is_training: True})
self.sess.run(clip_all_weights)
print("Epoch: [%2d] [%4d/%4d] time: %4.4f, loss: %.6f"
% (epoch + 1, batch_id + 1, numBatch, time.time() - start_time, loss))
iter_num += 1
lossSum += loss
#writer.add_summary(summary, iter_num)
if np.mod(epoch + 1, eval_every_epoch) == 0: ##Evaluate and save model
self.evaluate(iter_num, eval_files, noisy_files)
self.save(iter_num, ckpt_dir)
logEntry = "--- Epoch [%d] Average loss %.6f ---\n" % (epoch + 1, lossSum / numBatch)
print(logEntry)
with open(ckpt_dir + "/log.txt", "a") as file_object:
file_object.write(logEntry)
print("[*] Training finished.")
def save(self, iter_num, ckpt_dir, model_name='DnCNN-tensorflow'):
saver = tf.train.Saver()
checkpoint_dir = ckpt_dir
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
print("[*] Saving model...")
saver.save(self.sess,
os.path.join(checkpoint_dir, model_name),
global_step=iter_num)
def load(self, checkpoint_dir):
print("[*] Reading checkpoint...")
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
full_path = tf.train.latest_checkpoint(checkpoint_dir)
global_step = int(full_path.split('/')[-1].split('-')[-1])
saver.restore(self.sess, full_path)
#tf.compat.v1.saved_model.simple_save(self.sess, './savedmodel', inputs={"X": self.X, "Y_": self.Y_}, outputs={"Y": self.Y})
#self.save(global_step, './savedmodel')
frozen_graph = freeze_session(self.sess, output_names=[self.Y.op.name])
tf.train.write_graph(frozen_graph, "./savedmodel", "saved_model.pb", as_text=False)
print("Saved, input=%s, output=%s" % (self.X.op.name, self.Y.op.name))
return True, global_step
else:
return False, 0
def test(self, eval_files, noisy_files, ckpt_dir, save_dir):
"""Test DnCNN"""
# init variables
tf.global_variables_initializer().run()
assert len(eval_files) != 0, 'No testing data!'
load_model_status, global_step = self.load(ckpt_dir)
assert load_model_status == True, '[!] Load weights FAILED...'
print(" [*] Load weights SUCCESS...")
psnr_sum = 0
for i in range(len(eval_files)):
clean_image = cv2.imread(eval_files[i])
clean_image = clean_image.astype('float32') / 255.0
clean_image = clean_image[np.newaxis, ...]
noisy = cv2.imread(noisy_files[i])
noisy = noisy.astype('float32') / 255.0
noisy = noisy[np.newaxis, ...]
output_clean_image = self.sess.run(
[self.Y],feed_dict={self.Y_: clean_image, self.X: noisy,
self.is_training: False})
out1 = np.asarray(output_clean_image)
psnr = psnr_scaled(clean_image, out1[0,0])
psnr1 = psnr_scaled(clean_image, noisy)
print("img%d PSNR: %.2f , noisy PSNR: %.2f" % (i + 1, psnr, psnr1))
psnr_sum += psnr
cv2.imwrite('./data/denoised/%04d.png'%(i),out1[0,0]*255.0)
avg_psnr = psnr_sum / len(eval_files)
print("--- Test ---- Average PSNR %.2f ---" % avg_psnr)
class dataset(object):
def __init__(self,sess):
self.sess = sess
seed = time.time()
random.seed(seed)
random.shuffle(ind)
filenames = list()
for i in range(len(filepaths)):
filenames.append(filepaths_noisy[ind[i]])
filenames.append(filepaths[ind[i]])
# Parameters
num_patches = 8 # number of patches to extract from each image
patch_size = 64 # size of the patches
num_parallel_calls = 1 # number of threads
batch_size = 32 # size of the batch
get_patches_fn = lambda image: get_patches(image, num_patches=num_patches, patch_size=patch_size)
dataset = (
tf.data.Dataset.from_tensor_slices(filenames)
.map(im_read, num_parallel_calls=num_parallel_calls)
.map(get_patches_fn, num_parallel_calls=num_parallel_calls)
.batch(batch_size)
.prefetch(batch_size)
)
iterator = dataset.make_one_shot_iterator()
self.iter = iterator.get_next()
def get_batch(self):
res = self.sess.run(self.iter)
return res
def im_read(filename):
"""Decode the png image from the filename and convert to [0, 1]."""
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_png(image_string, channels=3)
# This will convert to float values in [0, 1]
image = tf.image.convert_image_dtype(image_decoded, tf.float32)
return image
def get_patches(image, num_patches, patch_size):
"""Get `num_patches` from the image"""
patches = []
for i in range(num_patches):
point1 = random.randint(0, 180 - patch_size) # 116 comes from the image source size (180) - the patch dimension (64)
point2 = random.randint(0, 180 - patch_size)
patch = tf.image.crop_to_bounding_box(image, point1, point2, patch_size, patch_size)
patches.append(patch)
patches = tf.stack(patches)
assert patches.get_shape().dims == [num_patches, patch_size, patch_size, 3]
return patches
def cal_psnr(im1, im2): # PSNR function for 0-255 values
mse = ((im1.astype(np.float) - im2.astype(np.float)) ** 2).mean()
psnr = 10 * np.log10(255 ** 2 / mse)
return psnr
def psnr_scaled(im1, im2): # PSNR function for 0-1 values
mse = ((im1 - im2) ** 2).mean()
if mse == 0:
return 0
mse = mse * (255 ** 2)
psnr = 10 * np.log10(255 **2 / mse)
return psnr
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
"""
Freezes the state of a session into a pruned computation graph.
Creates a new computation graph where variable nodes are replaced by
constants taking their current value in the session. The new graph will be
pruned so subgraphs that are not necessary to compute the requested
outputs are removed.
@param session The TensorFlow session to be frozen.
@param keep_var_names A list of variable names that should not be frozen,
or None to freeze all the variables in the graph.
@param output_names Names of the relevant graph outputs.
@param clear_devices Remove the device directives from the graph for better portability.
@return The frozen graph definition.
"""
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = tf.graph_util.convert_variables_to_constants(
session, input_graph_def, output_names, freeze_var_names)
return frozen_graph