-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_model.py
198 lines (164 loc) · 5.88 KB
/
test_model.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
'''
MIT License
Copyright (c) 2021 Tauhid Khan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from time import time
from tqdm import tqdm
import argparse
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow.keras import models
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
from ZeroDCE.dataset import TfdataPipeline
from command_line_scripts.utils import get_model
tf.random.set_seed(42)
# def get_model(model_path: str):
# assert isinstance(model_path, str) , 'model_path must be a string'
# tf.print(
# "[info] loading model from disk...."
# )
# model = models.load_model(model_path, compile=False)
# tf.print(
# "loaded model {}".format(model)
# )
# return model
def datapipeline(dataset_path: str, img_h: int = 128, img_w:int = 256) -> tf.data.Dataset:
assert isinstance(dataset_path, str)
# load dataset.
# NOTE: Always set the batch size to 1 for testing.
tfpipeline = TfdataPipeline(
BASE_DATASET_DIR=dataset_path, IMG_H=img_h, IMG_W=img_w, batch_size=1)
test_data = tfpipeline.data_loader(dataset_type='test')
return test_data
# A function to plot images and their corresponding enhanced images side by side in grid.
def plot_image(img:list, enhanced_img:list, model_name:str, save_fig:bool=True):
assert isinstance(img, list)
assert isinstance(enhanced_img, list)
assert len(img) == len(enhanced_img)
# create figure with subplots
fig = plt.figure(figsize=(15, 15))
gs = gridspec.GridSpec(nrows=2, ncols=len(img), figure=fig)
for i in range(len(img)):
ax = fig.add_subplot(gs[i])
ax.imshow(img[i])
ax.title.set_text('Original Image')
ax.axis('off')
ax = fig.add_subplot(gs[i+len(img)])
ax.imshow(enhanced_img[i])
ax.title.set_text('Enhanced Image')
ax.axis('off')
plt.tight_layout()
plt.suptitle(f'Original vs Enhanced Image: {model_name}')
if save_fig:
plt.savefig(f'image_assets/test_image_plot_{model_name}.png')
plt.show()
def run_test(
model_path:str,
dataset_path: str = 'lol_datasetv2/',
img_h: int = 128,
img_w: int = 256,
save_plot: int = 0,
load_random_data: int = 0
):
assert isinstance(model_path, str), 'model_path must be a string'
assert isinstance(dataset_path, str), 'dataset_path must be a string'
assert isinstance(img_h, int), 'img_h must be an integer'
assert isinstance(img_w, int), 'img_w must be an integer'
assert save_plot in [0, 1], 'save_plot must be either 0 or 1'
assert load_random_data in [0, 1], 'load_random_data must be either 0 or 1'
assert os.path.exists(model_path), 'model_path does not exist'
assert os.path.exists(dataset_path), 'dataset_path does not exist'
model_name = model_path.split('/')[-2]
# load model
model = get_model(model_path)
# load dataset
if load_random_data == 1:
test_data = tf.unstack(tf.random.normal([100, 1, img_h, img_w, 3]), axis=0)
else:
test_data = datapipeline(dataset_path, img_h=img_h, img_w=img_w)
# run test
results = []
inputs = []
times = []
for data in tqdm(test_data):
start = time()
enhanced_img, _ = model(data)
end = time() - start
enhanced_img = tf.squeeze(enhanced_img, axis=0)
data = tf.squeeze(data, axis=0)
results.append(enhanced_img)
inputs.append(data)
times.append(end)
average_time = sum(times[2:])/len(times[2:])
# plot results
tf.print(f'Average inference time: {round(average_time, 2)*1000} ms')
if save_plot == 1:
plot_image(img=inputs[:5], enhanced_img=results[:5], model_name=model_name, save_fig=save_plot)
def main():
parser = argparse.ArgumentParser(
description='Test model on test dataset'
)
parser.add_argument(
'--model_path',
type=str,
required=True,
help='path to the saved model folder'
)
parser.add_argument(
'--dataset_path',
type=str,
default='lol_datasetv2/',
help='path to the dataset'
)
parser.add_argument(
'--img_h',
type=int,
default=128,
help='image height'
)
parser.add_argument(
'--img_w',
type=int,
default=256,
help='Image width'
)
parser.add_argument(
'--save_plot',
type=int,
default=0,
help='save plot of original vs enhanced image. 0: no, 1: yes'
)
parser.add_argument(
'--load_random_data',
type=int,
default=0,
help='load random data. 0: no, 1: yes'
)
args = parser.parse_args()
run_test(
model_path=args.model_path,
dataset_path=args.dataset_path,
img_h=args.img_h,
img_w=args.img_w,
save_plot=args.save_plot,
load_random_data=args.load_random_data
)
if __name__ == '__main__':
main()