-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.py
283 lines (211 loc) · 8.5 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
#####################
# Utility functions #
#####################
from functools import reduce
import tensorflow as tf
import numpy as np
import sys
import os
NUM_DEFAULT_TRAIN_ITERS = [100000, 35000, 20000, 20000, 5000, 5000]
def process_command_args(arguments):
# Specifying the default parameters for training/validation
# --- data path ---
dataset_dir = 'raw_images/'
model_dir = 'models/'
result_dir = 'results/'
vgg_dir = 'vgg_pretrained/imagenet-vgg-verydeep-19.mat'
dslr_dir = 'fujifilm/'
phone_dir = 'mediatek_raw/'
# --- architecture ---
arch = "punet"
level = 0
inst_norm = False
num_maps_base = 16
# --- model weights ---
restore_iter = None
# --- input size ---
patch_w = 256 # default size for MAI dataset
patch_h = 256 # default size for MAI dataset
# --- training options ---
batch_size = 32
train_size = 5000
learning_rate = 5e-5
eval_step = 1000
num_train_iters = None
# --- more options ---
save_mid_imgs = False
for args in arguments:
# --- data path ---
if args.startswith("dataset_dir"):
dataset_dir = args.split("=")[1]
if args.startswith("model_dir"):
model_dir = args.split("=")[1]
if args.startswith("result_dir"):
result_dir = args.split("=")[1]
if args.startswith("vgg_dir"):
vgg_dir = args.split("=")[1]
if args.startswith("dslr_dir"):
dslr_dir = args.split("=")[1]
if args.startswith("phone_dir"):
phone_dir = args.split("=")[1]
# --- architecture ---
if args.startswith("arch"):
arch = args.split("=")[1]
if args.startswith("level"):
level = int(args.split("=")[1])
if args.startswith("inst_norm"):
inst_norm = eval(args.split("=")[1])
if args.startswith("num_maps_base"):
num_maps_base = int(args.split("=")[1])
# --- model weights ---
if args.startswith("restore_iter"):
restore_iter = int(args.split("=")[1])
# --- input size ---
if args.startswith("patch_w"):
patch_w = int(args.split("=")[1])
if args.startswith("patch_h"):
patch_h = int(args.split("=")[1])
# --- training options ---
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("eval_step"):
eval_step = int(args.split("=")[1])
if args.startswith("num_train_iters"):
num_train_iters = int(args.split("=")[1])
# --- more options ---
if args.startswith("save_mid_imgs"):
save_mid_imgs = eval(args.split("=")[1])
# choose architecture
if arch == "punet":
name_model = "punet"
# obtain restore iteration info
if not os.path.isdir(model_dir):
os.mkdir(model_dir)
if restore_iter is None: # no need to get the last iteration if specified
restore_iter = get_last_iter(model_dir, name_model)
if num_train_iters is None:
num_train_iters = NUM_DEFAULT_TRAIN_ITERS[level]
print("The following parameters will be applied for training:")
print("Model architecture: " + arch)
print("Restore Iteration: " + str(restore_iter))
print("Batch size: " + str(batch_size))
print("Training size: " + str(train_size))
print("Learning rate: " + str(learning_rate))
print("Training iterations: " + str(num_train_iters))
print("Evaluation step: " + str(eval_step))
print("Path to the dataset: " + dataset_dir)
print("Path to Raw-to-RGB model network: " + model_dir)
print("Path to result images: " + result_dir)
print("Path to VGG-19 network: " + vgg_dir)
print("Path to RGB data from DSLR: " + dslr_dir)
print("Path to Raw data from phone: " + phone_dir)
return dataset_dir, model_dir, result_dir, vgg_dir, dslr_dir, phone_dir,\
arch, level, inst_norm, num_maps_base, restore_iter, patch_w, patch_h,\
batch_size, train_size, learning_rate, eval_step, num_train_iters, save_mid_imgs
def process_test_model_args(arguments):
# Specifying the default parameters for testing
# --- data path ---
dataset_dir = 'raw_images/'
test_dir = 'fujifilm_full_resolution/'
model_dir = 'models/'
result_dir = 'results/'
# --- architecture ---
arch = "punet"
level = 0
inst_norm = False
num_maps_base = 16
# --- model weights ---
orig_model = False
rand_param = False
restore_iter = None
# --- input size ---
img_h = 2976 # default size
img_w = 3968 # default size
# --- more options ---
use_gpu = True
save_model = True
test_image = False
for args in arguments:
# --- data path ---
if args.startswith("dataset_dir"):
dataset_dir = args.split("=")[1]
if args.startswith("test_dir"):
test_dir = args.split("=")[1]
if args.startswith("model_dir"):
model_dir = args.split("=")[1]
if args.startswith("result_dir"):
result_dir = args.split("=")[1]
# --- architecture ---
if args.startswith("arch"):
arch = args.split("=")[1]
if args.startswith("level"):
level = 0 if arch == "pynet_0" else int(args.split("=")[1])
if args.startswith("inst_norm"):
inst_norm = eval(args.split("=")[1])
if args.startswith("num_maps_base"):
num_maps_base = int(args.split("=")[1])
# --- model weights ---
if args.startswith("orig"):
orig_model = eval(args.split("=")[1])
if args.startswith("rand"):
rand_param = eval(args.split("=")[1])
if args.startswith("restore_iter"):
restore_iter = int(args.split("=")[1])
# --- input size ---
if args.startswith("img_h"):
img_h = int(args.split("=")[1])
if args.startswith("img_w"):
img_w = int(args.split("=")[1])
# --- more options ---
if args.startswith("use_gpu"):
use_gpu = eval(args.split("=")[1])
if args.startswith("save"):
save_model = eval(args.split("=")[1])
if args.startswith("test_image"):
test_image = eval(args.split("=")[1])
# choose architecture
if arch == "punet":
name_model = "punet"
# obtain restore iteration info (necessary if no pre-trained model or not random weights)
if restore_iter is None and not orig_model and not rand_param: # need to restore a model
restore_iter = get_last_iter(model_dir, name_model)
if restore_iter == -1:
print("Error: Cannot find any pre-trained models for " + name_model + ".")
sys.exit()
print("The following parameters will be applied for testing:")
print("Model architecture: " + arch)
print("Restore Iteration: " + str(restore_iter))
print("Path to the dataset: " + dataset_dir)
print("Path to Raw-to-RGB model network: " + model_dir)
print("Path to result images: " + result_dir)
print("Path to testing data: " + test_dir)
return dataset_dir, test_dir, model_dir, result_dir,\
arch, level, inst_norm, num_maps_base, orig_model, rand_param, restore_iter,\
img_h, img_w, use_gpu, save_model, test_image
def get_last_iter(model_dir, name_model):
saved_models = [int(model_file.split(".")[0].split("_")[-1])
for model_file in os.listdir(model_dir)
if model_file.startswith(name_model)]
if len(saved_models) > 0:
return np.max(saved_models)
else:
return -1
def log10(x):
numerator = tf.math.log(x)
denominator = tf.math.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 export_pb(sess, output_node_name, output_dir='.', export_pb_name='test.pb'):
gd = sess.graph.as_graph_def()
# replace variables in a graph with constants for exporting pb
output_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants(
sess, gd, [output_node_name]
)
tf.io.write_graph(output_graph_def, output_dir, export_pb_name, as_text=False)
print(f'Save {export_pb_name} in {output_dir} Done')