-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.py
179 lines (135 loc) · 4.76 KB
/
utils.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
import os
import re
import sys
import glob
import scipy.misc
from itertools import cycle
import numpy as np
import tensorflow as tf
from libs import vgg16
from PIL import Image
LEARNING_RATE = 0.0002
BATCH_SIZE = 5
BATCH_SHAPE = [BATCH_SIZE, 256, 256, 3]
SKIP_STEP = 10
N_EPOCHS = 100
N_IMAGES = 1000
CKPT_DIR = './Checkpoints/'
IMG_DIR = './Images/'
GRAPH_DIR = './Graphs/'
TRAINING_SET_DIR= './dataset/training/'
VALIDATION_SET_DIR='./dataset/validation/'
METRICS_SET_DIR='./dataset/metrics/'
TRAINING_DIR_LIST = []
ADVERSARIAL_LOSS_FACTOR = 0.5
PSNR_LOSS_FACTOR = -1.0
SSIM_LOSS_FACTOR = -0.1
metrics_image = scipy.misc.imread(METRICS_SET_DIR+'gt.png', mode='RGB').astype('float32')
CLIP = [-0.01,0.01]
CRITIC_NUM = 5
def initialize(sess):
saver = tf.train.Saver()
writer = tf.summary.FileWriter(GRAPH_DIR, sess.graph)
if not os.path.exists(CKPT_DIR):
os.makedirs(CKPT_DIR)
if not os.path.exists(IMG_DIR):
os.makedirs(IMG_DIR)
ckpt = tf.train.get_checkpoint_state(os.path.dirname(CKPT_DIR))
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
return saver
def get_training_dir_list():
training_list = [d[1] for d in os.walk(TRAINING_SET_DIR)]
global TRAINING_DIR_LIST
TRAINING_DIR_LIST = training_list[0]
return TRAINING_DIR_LIST
def load_next_training_batch():
batch = next(pool)
return batch
def load_validation():
filelist = sorted(glob.glob(VALIDATION_SET_DIR + '/*.png'), key=alphanum_key)
validation = np.array([np.array(scipy.misc.imread(fname, mode='RGB').astype('float32')) for fname in filelist])
return validation
def training_dataset_init():
filelist = sorted(glob.glob(TRAINING_SET_DIR + '/*.png'), key=alphanum_key)
batch = np.array([np.array(scipy.misc.imread(fname, mode='RGB').astype('float32')) for fname in filelist])
batch = split(batch, BATCH_SIZE)
training_dir_list = get_training_dir_list()
global pool
pool = cycle(batch)
def imsave(filename, image):
scipy.misc.imsave(IMG_DIR+filename+'.png', image)
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [ tryint(c) for c in re.split('([0-9]+)', s) ]
def split(arr, size):
arrs = []
while len(arr) > size:
pice = arr[:size]
arrs.append(pice)
arr = arr[size:]
arrs.append(arr)
return arrs
def lrelu(x, leak=0.2, name='lrelu'):
with tf.variable_scope(name):
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * x + f2 * abs(x)
def tf_log10(x):
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
def PSNR(y_true, y_pred):
max_pixel = 255.0
return 10.0 * tf_log10((max_pixel ** 2) / (tf.reduce_mean(tf.square(y_pred - y_true))))
def _tf_fspecial_gauss(size, sigma=1.5):
"""Function to mimic the 'fspecial' gaussian MATLAB function"""
x_data, y_data = np.mgrid[-size//2 + 1:size//2 + 1, -size//2 + 1:size//2 + 1]
x_data = np.expand_dims(x_data, axis=-1)
x_data = np.expand_dims(x_data, axis=-1)
y_data = np.expand_dims(y_data, axis=-1)
y_data = np.expand_dims(y_data, axis=-1)
x = tf.constant(x_data, dtype=tf.float32)
y = tf.constant(y_data, dtype=tf.float32)
g = tf.exp(-((x**2 + y**2)/(2.0*sigma**2)))
return g / tf.reduce_sum(g)
def SSIM_one(img1, img2, k1=0.01, k2=0.02, L=1, window_size=11):
"""
The function is to calculate the ssim score
"""
img1 = tf.expand_dims(img1, -1)
img2 = tf.expand_dims(img2, -1)
window = _tf_fspecial_gauss(window_size)
mu1 = tf.nn.conv2d(img1, window, strides = [1, 1, 1, 1], padding = 'VALID')
mu2 = tf.nn.conv2d(img2, window, strides = [1, 1, 1, 1], padding = 'VALID')
mu1_sq = mu1 * mu1
mu2_sq = mu2 * mu2
mu1_mu2 = mu1 * mu2
sigma1_sq = tf.nn.conv2d(img1*img1, window, strides = [1 ,1, 1, 1], padding = 'VALID') - mu1_sq
sigma2_sq = tf.nn.conv2d(img2*img2, window, strides = [1, 1, 1, 1], padding = 'VALID') - mu2_sq
sigma1_2 = tf.nn.conv2d(img1*img2, window, strides = [1, 1, 1, 1], padding = 'VALID') - mu1_mu2
c1 = (k1*L)**2
c2 = (k2*L)**2
ssim_map = ((2*mu1_mu2 + c1)*(2*sigma1_2 + c2)) / ((mu1_sq + mu2_sq + c1)*(sigma1_sq + sigma2_sq + c2))
return tf.reduce_mean(ssim_map)
def SSIM_three(img1, img2):
rgb1 = tf.unstack(img1, axis=3)
r1 = rgb1[0]
g1 = rgb1[1]
b1 = rgb1[2]
rgb2 = tf.unstack(img2, axis=3)
r2 = rgb2[0]
g2 = rgb2[1]
b2 = rgb2[2]
ssim_r = SSIM_one(r1, r2)
ssim_g = SSIM_one(g1, g2)
ssim_b = SSIM_one(b1, b2)
ssim = tf.reduce_mean(ssim_r + ssim_g + ssim_b) / 3
return ssim