forked from wbhu/DnCNN-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (64 loc) · 3.28 KB
/
main.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
import argparse
from glob import glob
import tensorflow as tf
from model import denoiser
from utils import *
parser = argparse.ArgumentParser(description='')
parser.add_argument('--epoch', dest='epoch', type=int, default=50, help='# of epoch')
parser.add_argument('--batch_size', dest='batch_size', type=int, default=128, help='# images in batch')
parser.add_argument('--lr', dest='lr', type=float, default=0.001, help='initial learning rate for adam')
parser.add_argument('--use_gpu', dest='use_gpu', type=int, default=1, help='gpu flag, 1 for GPU and 0 for CPU')
parser.add_argument('--sigma', dest='sigma', type=int, default=25, help='noise level')
parser.add_argument('--phase', dest='phase', default='train', help='train or test')
parser.add_argument('--checkpoint_dir', dest='ckpt_dir', default='./checkpoint', help='models are saved here')
parser.add_argument('--sample_dir', dest='sample_dir', default='./sample', help='sample are saved here')
parser.add_argument('--test_dir', dest='test_dir', default='./test', help='test sample are saved here')
parser.add_argument('--eval_set', dest='eval_set', default='Set12', help='dataset for eval in training')
parser.add_argument('--test_set', dest='test_set', default='BSD68', help='dataset for testing')
args = parser.parse_args()
def denoiser_train(denoiser, lr):
with load_data(filepath='./data/img_clean_pats.npy') as data:
# if there is a small memory, please comment this line and uncomment the line99 in model.py
data = data.astype(np.float32) / 255.0 # normalize the data to 0-1
eval_files = glob('./data/test/{}/*.png'.format(args.eval_set))
eval_data = load_images(eval_files) # list of array of different size, 4-D, pixel value range is 0-255
denoiser.train(data, eval_data, batch_size=args.batch_size, ckpt_dir=args.ckpt_dir, epoch=args.epoch, lr=lr,
sample_dir=args.sample_dir)
def denoiser_test(denoiser):
test_files = glob('./data/test/{}/*.png'.format(args.test_set))
denoiser.test(test_files, ckpt_dir=args.ckpt_dir, save_dir=args.test_dir)
def main(_):
if not os.path.exists(args.ckpt_dir):
os.makedirs(args.ckpt_dir)
if not os.path.exists(args.sample_dir):
os.makedirs(args.sample_dir)
if not os.path.exists(args.test_dir):
os.makedirs(args.test_dir)
lr = args.lr * np.ones([args.epoch])
lr[30:] = lr[0] / 10.0
if args.use_gpu:
# added to control the gpu memory
print("GPU\n")
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
model = denoiser(sess, sigma=args.sigma)
if args.phase == 'train':
denoiser_train(model, lr=lr)
elif args.phase == 'test':
denoiser_test(model)
else:
print('[!]Unknown phase')
exit(0)
else:
print("CPU\n")
with tf.Session() as sess:
model = denoiser(sess, sigma=args.sigma)
if args.phase == 'train':
denoiser_train(model, lr=lr)
elif args.phase == 'test':
denoiser_test(model)
else:
print('[!]Unknown phase')
exit(0)
if __name__ == '__main__':
tf.app.run()