forked from quic/aimet-model-zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrgan_quanteval.py
372 lines (318 loc) · 12.5 KB
/
srgan_quanteval.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
#pylint: disable=E0401,E1101,W0621,R0915,R0914,R0912
# -*- mode: python -*-
# =============================================================================
# @@-COPYRIGHT-START-@@
#
# Copyright (c) 2022 of Qualcomm Innovation Center, Inc. All rights reserved.
#
# @@-COPYRIGHT-END-@@
# =============================================================================
"""Quantsim evaluation script for srgan"""
from functools import partial
import argparse
import tarfile
import urllib.request
import glob
import os
import numpy as np
import tensorflow.compat.v1 as tf
from model.srgan import generator
from mmcv.image.colorspace import rgb2ycbcr
from aimet_tensorflow.bias_correction import (
QuantParams,
BiasCorrectionParams,
BiasCorrection,
)
from aimet_tensorflow.cross_layer_equalization import equalize_model
from aimet_tensorflow import quantsim
from skimage.metrics import structural_similarity as ssim
from skimage.metrics import peak_signal_noise_ratio as psnr
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
tf.disable_v2_behavior()
def make_dataset(filenames):
"""make dataset"""
ds = tf.data.Dataset.from_tensor_slices(filenames)
ds = ds.map(tf.io.read_file)
ds = ds.map(lambda x: tf.image.decode_png(x, channels=3))
return ds
def evaluate_session(
sess,
image_files,
input_name,
output_name,
mode="y_channel",
output_dir=None):
"""
:param sess: a tensorflow session on which we run evaluation
:param image_files: a sequence containing sequence of image filenames as strings
:param input_name: a string indicating the input tensor's name
:param output_name: a string indicating the output tensor's name
:param mode: a string indicating on which space to evalute the PSNR & SSIM metrics.
Accepted values are ['y_channel', 'rgb']
:param output_dir: If specified, super resolved images will be saved under the path
:return: a tuple containing the computed values of (PSNR, SSIME) sequences
"""
if mode == "rgb":
print("Testing on RGB channels...")
elif mode == "y_channel":
print("Testing on Y channel...")
else:
raise ValueError(
"evaluation mode not supported!"
"Must be one of `RGB` or `y_channel`")
# batch size needed to align with input shape (?, ?, ?, 3)
batch_size = 1
with sess.graph.as_default():
lr_image_files, hr_image_files = image_files
# make a dataset from input and reference images
lr_valid_ds = make_dataset(lr_image_files)
lr_valid_ds = lr_valid_ds.map(lambda x: tf.cast(x, dtype=tf.float32))
hr_valid_ds = make_dataset(hr_image_files)
valid_ds = tf.data.Dataset.zip((lr_valid_ds, hr_valid_ds))
# make an iterator from the dataset, batch applied here
valid_ds = valid_ds.batch(batch_size)
valid_ds_iter = valid_ds.make_one_shot_iterator()
imgs = valid_ds_iter.get_next()
# crop border width 4 as suggested in https://arxiv.org/abs/1609.04802
crop_border = 4
psnr_values = []
ssim_values = []
for lr_image_file in lr_image_files:
lr_img, hr_img = sess.run(imgs)
# get inference images
sr_img = sess.run(
sess.graph.get_tensor_by_name(output_name),
{sess.graph.get_tensor_by_name(input_name): lr_img},
)
sr_img = tf.clip_by_value(sr_img, 0, 255)
sr_img = tf.round(sr_img)
sr_img = tf.cast(sr_img, tf.uint8)
sr_img = sess.run(sr_img)
if output_dir:
sr_img_png = tf.image.encode_png(sr_img[0])
# use the input image's name as output image's name by default
_, filename = os.path.split(lr_image_file)
filename = os.path.join(output_dir, filename)
save_img = tf.io.write_file(filename, sr_img_png)
sess.run(save_img)
if mode == "y_channel":
sr_img = rgb2ycbcr(sr_img, y_only=True)
hr_img = rgb2ycbcr(hr_img, y_only=True)
sr_img = np.expand_dims(sr_img, axis=-1)
hr_img = np.expand_dims(hr_img, axis=-1)
sr_img = sr_img[:, crop_border:-
crop_border, crop_border:-crop_border, :]
hr_img = hr_img[:, crop_border:-
crop_border, crop_border:-crop_border, :]
psnr_value = psnr(hr_img[0], sr_img[0], data_range=255)
ssim_value = ssim(
hr_img[0, :, :, 0],
sr_img[0, :, :, 0],
multichannel=False,
data_range=255.0,
)
psnr_values.append(psnr_value)
ssim_values.append(ssim_value)
return psnr_values, ssim_values
def download_weights():
"""Downloading weights and config"""
if not os.path.exists("weights/srgan/gan_generator.h5"):
URL = "https://martin-krasser.de/sisr/weights-srgan.tar.gz"
urllib.request.urlretrieve(URL, "weights-srgan.tar.gz")
with tarfile.open("weights-srgan.tar.gz") as pth_weights:
pth_weights.extractall("./")
# Config file
if not os.path.exists("default_config.json"):
URL = "https://raw.githubusercontent.com/quic/aimet/release-aimet-1.22/TrainingExtensions/common/src/python/aimet_common/quantsim_config/default_config.json"
urllib.request.urlretrieve(URL, "default_config.json")
def parse_args():
"""argument parser"""
parser = argparse.ArgumentParser(
prog="srgan_quanteval",
description="Evaluate the pre and post quantized SRGAN model",
)
parser.add_argument(
"--dataset-path",
help="Parent directory with *LR.png and *SR.png images",
type=str,
)
parser.add_argument(
"--use-cuda",
help="Whether to use cuda, True by default",
default=True,
type=bool,
)
parser.add_argument(
"--default-output-bw",
help="Default bitwidth (4-31) to use for quantizing layer inputs and outputs",
default=16,
choices=range(
4,
32),
type=int,
)
parser.add_argument(
"--default-param-bw",
help="Default bitwidth (4-31) to use for quantizing layer parameters",
default=8,
choices=range(4, 32),
type=int,
)
parser.add_argument(
"--output-dir",
help="If specified, output images of quantized model will be saved under this directory",
default=None,
type=str,
)
return parser.parse_args()
def main(args):
"""main evaluation script"""
# configuration for efficient use of gpu
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
quant_scheme = "tf_enhanced"
download_weights()
print("Loading srgan generator...")
gen_graph = tf.Graph()
with gen_graph.as_default():
gen_sess = tf.Session(config=config, graph=gen_graph)
with gen_sess.as_default():
srgan_generator = generator()
srgan_generator.load_weights("weights/srgan/gan_generator.h5")
# sort files by filenames, assuming names match in both paths
lr_images_files = sorted(
glob.glob(
os.path.join(
args.dataset_path,
"*LR.png")))
hr_images_files = sorted(
glob.glob(
os.path.join(
args.dataset_path,
"*HR.png")))
# check if number of images align
if len(lr_images_files) != len(hr_images_files):
raise RuntimeError(
"length of image files doesn`t match,"
"need same number of images for both"
"low resolution and high resolution!"
)
image_files = (lr_images_files, hr_images_files)
bc_lr_data = lr_images_files
comp_encodings_lr_data = lr_images_files
comp_encodings_hr_data = hr_images_files
comp_encodings_data = (comp_encodings_lr_data, comp_encodings_hr_data)
### ===========Original Model=============###
# Evaluate original model on gpu
psnr_vals, ssim_vals = evaluate_session(
gen_sess, image_files, srgan_generator.input.name, srgan_generator.output.name)
psnr_val_orig_fp32 = np.mean(psnr_vals)
ssim_val_orig_fp32 = np.mean(ssim_vals)
# Evaluate the original model on device
sim = quantsim.QuantizationSimModel(
gen_sess,
starting_op_names=[srgan_generator.input.op.name],
output_op_names=[srgan_generator.output.op.name],
quant_scheme=quant_scheme,
default_output_bw=args.default_output_bw,
default_param_bw=args.default_param_bw,
config_file="default_config.json",
)
partial_eval = partial(
evaluate_session,
input_name=srgan_generator.input.name,
output_name="lambda_3/mul_quantized:0",
)
sim.compute_encodings(partial_eval, comp_encodings_data)
psnr_vals, ssim_vals = evaluate_session(
sim.session,
image_files,
srgan_generator.input.name,
"lambda_3/mul_quantized:0",
output_dir=args.output_dir,
)
psnr_val_orig_int8 = np.mean(psnr_vals)
ssim_val_orig_int8 = np.mean(ssim_vals)
### ===========Optimized Model=============###
# Re-initialize graph for optimized model
gen_graph = tf.Graph()
with gen_graph.as_default():
gen_sess = tf.Session(config=config, graph=gen_graph)
with gen_sess.as_default():
srgan_generator = generator()
srgan_generator.load_weights("weights/srgan/gan_generator.h5")
# Evaluate optimized model on gpu
psnr_vals, ssim_vals = evaluate_session(
gen_sess, image_files, srgan_generator.input.name, srgan_generator.output.name)
psnr_val_optim_fp32 = np.mean(psnr_vals)
ssim_val_optim_fp32 = np.mean(ssim_vals)
print("Applying cross layer equalization (CLE) to session...")
gen_sess = equalize_model(
gen_sess,
start_op_names=srgan_generator.input.op.name,
output_op_names=srgan_generator.output.op.name,
)
print("Applying Bias Correction (BC) to session...")
# the dataset being evaluated might have varying image sizes
# so right now only use batch size 1
batch_size = 1
num_imgs = len(bc_lr_data)
quant_params = QuantParams(use_cuda=args.use_cuda, quant_mode=quant_scheme)
bias_correction_params = BiasCorrectionParams(
batch_size=batch_size,
num_quant_samples=min(num_imgs, 10),
num_bias_correct_samples=min(num_imgs, 500),
input_op_names=[srgan_generator.input.op.name],
output_op_names=[srgan_generator.output.op.name],
)
ds = make_dataset(bc_lr_data)
ds = ds.batch(batch_size)
gen_sess = BiasCorrection.correct_bias(
gen_sess, bias_correction_params, quant_params, ds
)
# creating quantsim object which inserts quantizer ops
sim = quantsim.QuantizationSimModel(
gen_sess,
starting_op_names=[srgan_generator.input.op.name],
output_op_names=[srgan_generator.output.op.name],
quant_scheme=quant_scheme,
default_output_bw=args.default_output_bw,
default_param_bw=args.default_param_bw,
config_file="default_config.json",
)
# compute activation encodings
# usually achieves good results when data being used for computing
# encodings are representative of its task
partial_eval = partial(
evaluate_session,
input_name=srgan_generator.input.name,
output_name="lambda_3/mul_quantized:0",
)
sim.compute_encodings(partial_eval, comp_encodings_data)
psnr_vals, ssim_vals = evaluate_session(
sim.session,
image_files,
srgan_generator.input.name,
"lambda_3/mul_quantized:0",
output_dir=args.output_dir,
)
psnr_val_optim_int8 = np.mean(psnr_vals)
ssim_val_optim_int8 = np.mean(ssim_vals)
print()
print("Evaluation Summary:")
print(
f"Original Model | 32-bit environment | Mean PSNR: {psnr_val_orig_fp32:.5f} | Mean SSIM {ssim_val_orig_fp32:.5f}"
)
print(
f"Original Model | {args.default_param_bw}-bit environment | Mean PSNR: {psnr_val_orig_int8:.5f} | Mean SSIM {ssim_val_orig_int8:.5f}"
)
print(
f"Optimized Model | 32-bit environment | Mean PSNR: {psnr_val_optim_fp32:.5f} | Mean SSIM {ssim_val_optim_fp32:.5f}"
)
print(
f"Optimized Model | {args.default_param_bw}-bit environment | Mean PSNR: {psnr_val_optim_int8:.5f} | Mean SSIM {ssim_val_optim_int8:.5f}"
)
if __name__ == "__main__":
args = parse_args()
main(args)