-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
241 lines (200 loc) · 7.62 KB
/
test.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
import sys
import os
import argparse
from pathlib import Path
import time
import json
import tensorflow as tf
from processing import utils
from processing import postprocessing
from processing.preprocessing import Preprocessor
from processing.postprocessing import label_images
from processing.utils import printProgressBar
from skimage.util import img_as_ubyte
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def get_true_classes(filenames):
# retrieve ground truth
y_true = [1 if "good" not in filename.split("/") else 0 for filename in filenames]
return y_true
def is_defective(areas, min_area):
"""Decides if image is defective given the areas of its connected components"""
areas = np.array(areas)
if areas[areas >= min_area].shape[0] > 0:
return 1
return 0
def predict_classes(resmaps, min_area, threshold):
# threshold residual maps with the given threshold
resmaps_th = resmaps > threshold
# compute connected components
_, areas_all = label_images(resmaps_th)
# Decides if images are defective given the areas of their connected components
y_pred = [is_defective(areas, min_area) for areas in areas_all]
return y_pred
def save_segmented_images(resmaps, threshold, filenames, save_dir):
# threshold residual maps with the given threshold
resmaps_th = resmaps > threshold
# create directory to save segmented resmaps
seg_dir = os.path.join(save_dir, "segmentation")
if not os.path.isdir(seg_dir):
os.makedirs(seg_dir)
# save segmented resmaps
for i, resmap_th in enumerate(resmaps_th):
fname = utils.generate_new_name(filenames[i], suffix="seg")
fpath = os.path.join(seg_dir, fname)
plt.imsave(fpath, resmap_th, cmap="gray")
return
def main(args):
# parse arguments
model_path = args.path
save = args.save
# ============= LOAD MODEL AND PREPROCESSING CONFIGURATION ================
# load model and info
model, info, _ = utils.load_model_HDF5(model_path)
# set parameters
input_directory = info["data"]["input_directory"]
architecture = info["model"]["architecture"]
loss = info["model"]["loss"]
rescale = info["preprocessing"]["rescale"]
shape = info["preprocessing"]["shape"]
color_mode = info["preprocessing"]["color_mode"]
vmin = info["preprocessing"]["vmin"]
vmax = info["preprocessing"]["vmax"]
nb_validation_images = info["data"]["nb_validation_images"]
# =================== LOAD VALIDATION PARAMETERS =========================
model_dir_name = os.path.basename(str(Path(model_path).parent))
finetune_dir = os.path.join(
os.getcwd(),
"results",
input_directory,
architecture,
loss,
model_dir_name,
"finetuning",
)
subdirs = os.listdir(finetune_dir)
for subdir in subdirs:
logger.info(
"testing with finetuning parameters from \n{}...".format(
os.path.join(finetune_dir, subdir)
)
)
try:
with open(
os.path.join(finetune_dir, subdir, "finetuning_result.json"), "r"
) as read_file:
validation_result = json.load(read_file)
except FileNotFoundError:
logger.warning("run finetune.py before testing.\nexiting script.")
sys.exit()
min_area = validation_result["best_min_area"]
threshold = validation_result["best_threshold"]
method = validation_result["method"]
dtype = validation_result["dtype"]
# ====================== PREPROCESS TEST IMAGES ==========================
# initialize preprocessor
preprocessor = Preprocessor(
input_directory=input_directory,
rescale=rescale,
shape=shape,
color_mode=color_mode,
)
# get test generator
nb_test_images = preprocessor.get_total_number_test_images()
test_generator = preprocessor.get_test_generator(
batch_size=nb_test_images, shuffle=False
)
# retrieve test images from generator
imgs_test_input = test_generator.next()[0]
# retrieve test image names
filenames = test_generator.filenames
# predict on test images
imgs_test_pred = model.predict(imgs_test_input)
# instantiate TensorImages object
tensor_test = postprocessing.TensorImages(
imgs_input=imgs_test_input,
imgs_pred=imgs_test_pred,
vmin=vmin,
vmax=vmax,
color="grayscale",
method=method,
dtype=dtype,
filenames=filenames,
)
# ====================== CLASSIFICATION ==========================
# retrieve ground truth
y_true = get_true_classes(filenames)
# predict classes on test images
y_pred = predict_classes(
resmaps=tensor_test.resmaps, min_area=min_area, threshold=threshold
)
# confusion matrix
tnr, fp, fn, tpr = confusion_matrix(y_true, y_pred, normalize="true").ravel()
# initialize dictionary to store test results
test_result = {
"min_area": min_area,
"threshold": threshold,
"TPR": tpr,
"TNR": tnr,
"score": (tpr + tnr) / 2,
"method": method,
"dtype": dtype,
}
# ====================== SAVE TEST RESULTS =========================
# create directory to save test results
save_dir = os.path.join(
os.getcwd(),
"results",
input_directory,
architecture,
loss,
model_dir_name,
"test",
subdir,
)
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
# save test result
with open(os.path.join(save_dir, "test_result.json"), "w") as json_file:
json.dump(test_result, json_file, indent=4, sort_keys=False)
# save classification of image files in a .txt file
classification = {
"filenames": filenames,
"predictions": y_pred,
"truth": y_true,
"accurate_predictions": np.array(y_true) == np.array(y_pred),
}
df_clf = pd.DataFrame.from_dict(classification)
with open(os.path.join(save_dir, "classification.txt"), "w") as f:
f.write(
"min_area = {}, threshold = {}, method = {}, dtype = {}\n\n".format(
min_area, threshold, method, dtype
)
)
f.write(df_clf.to_string(header=True, index=True))
# print classification results to console
with pd.option_context("display.max_rows", None, "display.max_columns", None):
print(df_clf)
# save segmented resmaps
if save:
save_segmented_images(tensor_test.resmaps, threshold, filenames, save_dir)
# print test_results to console
logger.info("test results: {}".format(test_result))
if __name__ == "__main__":
# create parser
parser = argparse.ArgumentParser()
parser.add_argument(
"-p", "--path", type=str, required=True, metavar="", help="path to saved model"
)
parser.add_argument(
"-s", "--save", action="store_true", help="save segmented images",
)
args = parser.parse_args()
main(args)
# Examples of command to initiate testing
# python3 test.py -p saved_models/LEGO_light/SV/mvtecCAE/mssim/20-09-2020_14-21-51/mvtecCAE_b8_e148.hdf5