-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
296 lines (230 loc) · 10.1 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
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
import scipy.stats as st
import tensorflow as tf
import numpy as np
import sys
from functools import reduce
def cross_entropy(p,q):
return p* tf.log(p/q)+(1-p)*tf.log((1-p)/(1-q))
def kl_divergence(p, q):
return tf.reduce_sum(p * tf.log(p/q))
def compute_gradient(img):
gx = img[:, :, :-1, :] - img[:, :, 1:, :]
gy = img[:, :-1, :, :] - img[:, 1:, :, :]
return gx, gy
def rgb_to_lab(srgb):
#input:[-1,1]
#output:
# L_chan: black and white with input range [0, 100]
# a_chan/b_chan: color channels with input range ~[-110, 110]
# with tf.name_scope("rgb_to_lab"):
srgb_pixels = tf.reshape(srgb, [-1, 3])
# with tf.name_scope("srgb_to_xyz"):
linear_mask = tf.cast(srgb_pixels <= 0.04045, dtype=tf.float32)
exponential_mask = tf.cast(srgb_pixels > 0.04045, dtype=tf.float32)
rgb_pixels = (srgb_pixels / 12.92 * linear_mask) + (((srgb_pixels + 0.055) / 1.055) ** 2.4) * exponential_mask
rgb_to_xyz = tf.constant([
# X Y Z
[0.412453, 0.212671, 0.019334], # R
[0.357580, 0.715160, 0.119193], # G
[0.180423, 0.072169, 0.950227], # B
])
xyz_pixels = tf.matmul(rgb_pixels, rgb_to_xyz)
# https://en.wikipedia.org/wiki/Lab_color_space#CIELAB-CIEXYZ_conversions
# with tf.name_scope("xyz_to_cielab"):
# convert to fx = f(X/Xn), fy = f(Y/Yn), fz = f(Z/Zn)
# normalize for D65 white point
xyz_normalized_pixels = tf.multiply(xyz_pixels, [1/0.950456, 1.0, 1/1.088754])
epsilon = 6.0/29
linear_mask = tf.cast(xyz_normalized_pixels <= (epsilon**3), dtype=tf.float32)
exponential_mask = tf.cast(xyz_normalized_pixels > (epsilon**3), dtype=tf.float32)
fxfyfz_pixels = (xyz_normalized_pixels / (3 * epsilon**2) + 4.0/29) * linear_mask + (xyz_normalized_pixels ** (1.0/3)) * exponential_mask
# convert to lab
fxfyfz_to_lab = tf.constant([
# l a b
[ 0.0, 500.0, 0.0], # fx
[116.0, -500.0, 200.0], # fy
[ 0.0, 0.0, -200.0], # fz
])
lab_pixels = tf.matmul(fxfyfz_pixels, fxfyfz_to_lab) + tf.constant([-16.0, 0.0, 0.0])
return tf.reshape(lab_pixels, tf.shape(srgb))
def preprocess_lab(lab):
# with tf.name_scope("preprocess_lab"):
L_chan, a_chan, b_chan = tf.unstack(lab, axis=3)
# lab=tf.stack((L_chan / 50, a_chan / 110, b_chan / 110),axis=3)
# L_chan: black and white with input range [0, 100]
# a_chan/b_chan: color channels with input range ~[-110, 110], not exact
# [0, 100] => [-1, 1]=>[0,1], ~[-110, 110] => [-1, 1]=>[0,1]
return [L_chan/ 50 - 1, a_chan / 110, b_chan / 110]
# return lab
def log10(x):
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
def _tensor_size(tensor):
from operator import mul
return reduce(mul, (d.value for d in tensor.get_shape()[1:]), 1)
def gauss_kernel(kernlen=21, nsig=3, channels=1):
interval = (2*nsig+1.)/(kernlen)
x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
kern1d = np.diff(st.norm.cdf(x))
kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
kernel = kernel_raw/kernel_raw.sum()
out_filter = np.array(kernel, dtype = np.float32)
out_filter = out_filter.reshape((kernlen, kernlen, 1, 1))
out_filter = np.repeat(out_filter, channels, axis = 2)
return out_filter
def blur(x):
kernel_var = gauss_kernel(21, 3, 3)
return tf.nn.depthwise_conv2d(x, kernel_var, [1, 1, 1, 1], padding='SAME')
def process_command_args(arguments):
# specifying default parameters
batch_size = 50
train_size = 30000
# learning_rate = 5e-4
learning_rate = 1e-4
num_train_iters = 20000
# """
# default values:
# batch_size: 50 - batch size [smaller values can lead to unstable training]
# train_size: 30000 - the number of training patches randomly loaded each eval_step iterations
# eval_step: 1000 - each eval_step iterations the model is saved and the training data is reloaded
# num_train_iters: 20000 - the number of training iterations
# learning_rate: 5e-4 - learning rate
# w_content: 10 - the weight of the content loss
# w_color: 0.5 - the weight of the color loss
# w_texture: 1 - the weight of the texture [adversarial] loss
# w_tv: 2000 - the weight of the total variation loss
# dped_dir: dped/ - path to the folder with DPED dataset
# vgg_dir: vgg_pretrained/imagenet-vgg-verydeep-19.mat - path to the pre-trained VGG-19 network
# """
# w_content = 10
# w_color = 0.5
# w_texture = 1
# w_tv = 2000
# """
# paper weight
# w_content: 1 - the weight of the content loss
# w_color: 0.1 - the weight of the color loss
# w_texture: 0.4 - the weight of the texture [adversarial] loss
# w_tv: 400 - the weight of the total variation loss
# """
w_content = 1
w_color = 0.1
w_texture = 0.4
w_tv = 400
dped_dir = 'dped/'
vgg_dir = 'vgg_pretrained/imagenet-vgg-verydeep-19.mat'
eval_step = 1000
phone = ""
for args in arguments:
if args.startswith("model"):
phone = args.split("=")[1]
if args.startswith("batch_size"):
batch_size = int(args.split("=")[1])
if args.startswith("train_size"):
train_size = int(args.split("=")[1])
if args.startswith("learning_rate"):
learning_rate = float(args.split("=")[1])
if args.startswith("num_train_iters"):
num_train_iters = int(args.split("=")[1])
# -----------------------------------
if args.startswith("w_content"):
w_content = float(args.split("=")[1])
if args.startswith("w_color"):
w_color = float(args.split("=")[1])
if args.startswith("w_texture"):
w_texture = float(args.split("=")[1])
if args.startswith("w_tv"):
w_tv = float(args.split("=")[1])
# -----------------------------------
if args.startswith("dped_dir"):
dped_dir = args.split("=")[1]
if args.startswith("vgg_dir"):
vgg_dir = args.split("=")[1]
if args.startswith("eval_step"):
eval_step = int(args.split("=")[1])
if phone == "":
print("\nPlease specify the camera model by running the script with the following parameter:\n")
print("python train_model.py model={iphone,blackberry,sony,adas}\n")
sys.exit()
if phone not in ["iphone", "sony", "blackberry","adas"]:
print("\nPlease specify the correct camera model:\n")
print("python train_model.py model={iphone,blackberry,sony,adas}\n")
sys.exit()
print("\nThe following parameters will be applied for CNN training:\n")
print("Phone model:", phone)
print("Batch size:", batch_size)
print("Learning rate:", learning_rate)
print("Training iterations:", str(num_train_iters))
print()
print("Content loss:", w_content)
print("Color loss:", w_color)
print("Texture loss:", w_texture)
print("Total variation loss:", str(w_tv))
print()
print("Path to DPED dataset:", dped_dir)
print("Path to VGG-19 network:", vgg_dir)
print("Evaluation step:", str(eval_step))
print()
return phone, batch_size, train_size, learning_rate, num_train_iters, \
w_content, w_color, w_texture, w_tv,\
dped_dir, vgg_dir, eval_step
def process_test_model_args(arguments):
phone = ""
dped_dir = 'dped/'
test_subset = "small"
iteration = "all"
resolution = "orig"
use_gpu = "true"
for args in arguments:
if args.startswith("model"):
phone = args.split("=")[1]
if args.startswith("dped_dir"):
dped_dir = args.split("=")[1]
if args.startswith("test_subset"):
test_subset = args.split("=")[1]
if args.startswith("iteration"):
iteration = args.split("=")[1]
if args.startswith("resolution"):
resolution = args.split("=")[1]
if args.startswith("use_gpu"):
use_gpu = args.split("=")[1]
if phone == "":
print("\nPlease specify the model by running the script with the following parameter:\n")
print("python test_model.py model={iphone,blackberry,sony,iphone_orig,blackberry_orig,sony_orig}\n")
sys.exit()
return phone, dped_dir, test_subset, iteration, resolution, use_gpu
def get_resolutions():
# IMAGE_HEIGHT, IMAGE_WIDTH
res_sizes = {}
res_sizes["adas"] = [571, 1002]
res_sizes["iphone"] = [1536, 2048]
# res_sizes["iphone"] = [1080, 1920]
res_sizes["iphone_orig"] = [1536, 2048]
res_sizes["blackberry"] = [1560, 2080]
res_sizes["blackberry_orig"] = [1560, 2080]
res_sizes["sony"] = [1944, 2592]
res_sizes["sony_orig"] = [1944, 2592]
res_sizes["high"] = [1260, 1680]
res_sizes["medium"] = [1024, 1366]
res_sizes["small"] = [768, 1024]
res_sizes["tiny"] = [600, 800]
return res_sizes
def get_specified_res(res_sizes, phone, resolution):
if resolution == "orig":
IMAGE_HEIGHT = res_sizes[phone][0]
IMAGE_WIDTH = res_sizes[phone][1]
else:
IMAGE_HEIGHT = res_sizes[resolution][0]
IMAGE_WIDTH = res_sizes[resolution][1]
IMAGE_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT * 3
return IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_SIZE
def extract_crop(image, resolution, phone, res_sizes):
if resolution == "orig":
return image
else:
x_up = int((res_sizes[phone][1] - res_sizes[resolution][1]) / 2)
y_up = int((res_sizes[phone][0] - res_sizes[resolution][0]) / 2)
x_down = x_up + res_sizes[resolution][1]
y_down = y_up + res_sizes[resolution][0]
return image[y_up : y_down, x_up : x_down, :]