-
Notifications
You must be signed in to change notification settings - Fork 15
/
train_real.py
129 lines (96 loc) · 4.79 KB
/
train_real.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
from __future__ import division
from __future__ import print_function
import os, time, scipy.io
import tensorflow as tf
from tensorflow.contrib.layers.python.layers import initializers
import numpy as np
import glob
import re
from utils import *
from model import *
if __name__ == '__main__':
input_dir = './dataset/real/'
checkpoint_dir = './checkpoint/real/'
result_dir = './result/real/'
PS = 512 # patch size, if your GPU memory is not enough, modify it
save_freq = 100
train_fns = glob.glob(input_dir + 'Batch_*')
origin_imgs = [None] * len(train_fns)
noise_imgs = [None] * len(train_fns)
for i in range(len(train_fns)):
origin_imgs[i] = []
noise_imgs[i] = []
# model setting
in_image = tf.placeholder(tf.float32, [None, None, None, 3])
gt_image = tf.placeholder(tf.float32, [None, None, None, 3])
est_noise, out_image = CBDNet(in_image)
G_loss = tf.losses.mean_squared_error(gt_image, out_image) + \
0.05 * tf.reduce_mean(tf.square(tf.image.image_gradients(est_noise)))
lr = tf.placeholder(tf.float32)
G_opt = tf.train.AdamOptimizer(learning_rate=lr).minimize(G_loss)
# load model
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt:
print('loaded', checkpoint_dir)
saver.restore(sess, ckpt.model_checkpoint_path)
allpoint = glob.glob(checkpoint_dir + 'epoch-*')
lastepoch = 0
for point in allpoint:
cur_epoch = re.findall(r'epoch-(\d+)', point)
lastepoch = np.maximum(lastepoch, int(cur_epoch[0]))
learning_rate = 1e-4
for epoch in range(lastepoch, 2001):
losses = AverageMeter()
cnt=0
if epoch > 1000:
learning_rate = 1e-5
for ind in np.random.permutation(len(train_fns)):
train_fn = train_fns[ind]
if not len(origin_imgs[ind]):
train_origin_fns = glob.glob(train_fn + '/*Reference.bmp')
train_noise_fns = glob.glob(train_fn + '/*Noisy.bmp')
origin_img = ReadImg(train_origin_fns[0])
origin_imgs[ind] = np.expand_dims(origin_img, axis=0)
for train_noise_fn in train_noise_fns:
noise_img = ReadImg(train_noise_fn)
noise_imgs[ind].append(np.expand_dims(noise_img, axis=0))
st = time.time()
for nind in np.random.permutation(len(noise_imgs[ind])):
H = origin_imgs[ind].shape[1]
W = origin_imgs[ind].shape[2]
ps_temp = min(H, W, PS) - 1
xx = np.random.randint(0, W-ps_temp)
yy = np.random.randint(0, H-ps_temp)
temp_origin_img = origin_imgs[ind][:, yy:yy+ps_temp, xx:xx+ps_temp, :]
temp_noise_img = noise_imgs[ind][nind][:, yy:yy+ps_temp, xx:xx+ps_temp, :]
if np.random.randint(2, size=1)[0] == 1:
temp_origin_img = np.flip(temp_origin_img, axis=1)
temp_noise_img = np.flip(temp_noise_img, axis=1)
if np.random.randint(2, size=1)[0] == 1:
temp_origin_img = np.flip(temp_origin_img, axis=0)
temp_noise_img = np.flip(temp_noise_img, axis=0)
if np.random.randint(2, size=1)[0] == 1:
temp_origin_img = np.transpose(temp_origin_img, (0, 2, 1, 3))
temp_noise_img = np.transpose(temp_noise_img, (0, 2, 1, 3))
cnt += 1
st = time.time()
_, G_current, output = sess.run(
[G_opt, G_loss, out_image],
feed_dict={in_image:temp_noise_img, gt_image:temp_origin_img, lr:learning_rate}
)
output = np.clip(output, 0, 1)
losses.update(G_current)
print("%d %d Loss=%.4f Time=%.3f"%(epoch, cnt, losses.avg, time.time()-st))
if epoch % save_freq == 0:
if not os.path.isdir(result_dir + '%04d'%epoch):
os.makedirs(result_dir + '%04d'%epoch)
temp = np.concatenate((temp_origin_img[0, :, :, :], temp_noise_img[0, :, :, :], output[0, :, :, :]), axis=1)
scipy.misc.toimage(temp*255, high=255, low=0, cmin=0, cmax=255).save(result_dir + '%04d/train_%d_%d.jpg'%(epoch, ind, nind))
saver.save(sess, checkpoint_dir + 'model.ckpt')
if not os.path.isdir(checkpoint_dir + 'epoch-' + str(epoch)):
os.mkdir(checkpoint_dir + 'epoch-' + str(epoch))
if os.path.isdir(checkpoint_dir + 'epoch-' + str(epoch - 1)):
os.rmdir(checkpoint_dir + 'epoch-' + str(epoch - 1))