-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
365 lines (330 loc) · 13 KB
/
inference.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
from multiprocessing import cpu_count
import os
import re
import argparse
import numpy as np
import torch
from torch.utils.data import DataLoader
from PIL import Image, ImageDraw
from tqdm import tqdm
from torchmetrics.detection.mean_ap import MeanAveragePrecision
from dataset_pollen import PollenDataset, PollenFoodQSDataset
from constants import inference_batch_size, device, cpu_device, models_path, inferenced_img_path, num_classes, logs_path
from plot import convert_name_to_title
pattern_name = re.compile("model (.*).pt")
pattern_dataset = re.compile(".*[iI][nN][fF][oO]:.*Data set: (.*)")
def convert_name_to_dataset_type(name):
"""get the data set type used to train a model with a certain name
Parameters
----------
name : str
name of the model
Returns
-------
str
data set used to train the model
"""
log_file = os.path.join(logs_path, f"training {name}.log")
dataset_type = "FoodQS"
if os.path.isfile(log_file):
with open(log_file, "r") as f:
line = f.readline()
while line != "":
m = pattern_dataset.match(line)
if m is not None:
dataset_type = m.group(1)
line = f.readline()
return dataset_type
def add_bounding_boxes(image, predictions, dataset, threshold: float=0.0):
"""add bounding boxes from model inference
Parameters
----------
image : Image
image to add bounding boxes too
predictions : dict
predictions from the model
dataset : torch.utils.data.Dataset
dataset used to train model
threshold : float
threshold for scores of added bounding boxes
Returns
-------
Image
image with bounding boxes
"""
boxes = predictions['boxes']
labels = predictions['labels']
scores = predictions['scores']
image_draw = ImageDraw.Draw(image)
for i, box in enumerate(boxes):
if scores[i] < threshold:
continue
xmin, ymin, xmax, ymax = box
try:
outline_color = dataset.get_color_for_num(labels[i])
label_text = dataset.get_name_for_num(labels[i])
except KeyError:
outline_color = (255, 0, 0)
label_text = "unknown"
image_draw.rectangle((xmin, ymin, xmax, ymax), fill=None, outline=outline_color)
image_draw.text((xmin, ymin), f"{label_text} {scores[i]:.4f}")
return image
def add_bounding_boxes_labels(image, targets, labels):
"""add bounding boxes from labeled data set
Parameters
----------
image : Image
image to add bounding boxes too
targets : list
list of bounding boxes
labels : list
list of labels for the bounding boxes
Returns
-------
Image
image with bounding boxes
"""
image_draw = ImageDraw.Draw(image)
for i, t in enumerate(targets):
if t[2] == 0.0 and t[3] == 0.0:
continue
xmin, ymin, xmax, ymax = t
image_draw.rectangle((xmin, ymin, xmax, ymax), fill=None, outline=dataset.get_color_for_num(labels[i]))
image_draw.text((xmin, ymin), dataset.get_name_for_num(labels[i]))
return image
def get_ground_truth_and_union(w, h, x_targets, x_labels):
"""get ground truth and union for IoU calculation
Parameters
----------
w : int
image width
h : int
image height
x_targets : list
bounding boxes from data set
x_labels : list
labels from data set
Returns
-------
tuple
tuple of ground truth and union, one of each for each class
"""
ground_truth = [np.full((w, h), False) for i in range(num_classes)]
union = [np.full((w, h), False) for i in range(num_classes)]
# construct ground truth mask for each class
for i, t in enumerate(x_targets):
for x in range(int(t[0]), int(t[2] + 1)):
if x >= w:
continue
for y in range(int(t[1]), int(t[3] + 1)):
if y >= h:
continue
l = int(x_labels[i].item())
ground_truth[l][x, y] = True
union[l][x, y] = True
return ground_truth, union
def intersection_over_union(image, x_targets, x_labels, y_targets, y_labels, y_scores, threshold=0.5):
"""calculate the intersection over union
Parameters
----------
image : Image
input image
x_targets : list
bounding boxes from data set
x_labels : list
labels from data set
y_targets : list
bounding boxes from model inference
y_labels : list
labels from model inference
y_scores : list
scores from model inference
threshold : float
threshold for bounding boxes to use
Returns
-------
list
IoU for each class
"""
w, h = image.size
intersection = [np.full((w, h), False) for i in range(num_classes)]
# construct ground truth mask for each class
ground_truth, union = get_ground_truth_and_union(w, h, x_targets, x_labels)
# calculate intersection
for i, t in enumerate(y_targets):
if y_scores[i] < threshold:
continue
for x in range(int(t[0]), int(t[2] + 1)):
if x >= w:
continue
for y in range(int(t[1]), int(t[3] + 1)):
if y >= h:
continue
l = int(y_labels[i].item())
if l >= len(union):
continue
union[l][x, y] = True
if ground_truth[l][x, y]:
intersection[l][x, y] = True
i_o_u = [0.0 for i in range(num_classes)]
for i in range(num_classes):
current_union = union[i].sum()
if current_union > 0.0:
i_o_u[i] = intersection[i].sum() / current_union
return i_o_u
def inference(net, generate_stats: bool=False, threshold: float=0.9, dataset_type: str="FoodQS"):
"""perform inference with a model
Parameters
----------
net : nn.Module
model to use
generate_stats : bool
whether to generate stats, i.e. mAP values
threshold : float
threshold for adding bounding boxes to images
dataset_type : str
data set to use
Returns
-------
tuple
tuple of output images, IoU values, mAP metrics, metrics per class, class number to name mapping, count of images with at least one bounding box
"""
dataset = PollenFoodQSDataset() if dataset_type == "FoodQS" else PollenDataset()
dataloader = DataLoader(dataset, batch_size=inference_batch_size, shuffle=False, num_workers=cpu_count())
net.eval()
current_i = 0
output_images = []
i_o_u = [0.0 for i in range(num_classes)] if generate_stats else None
metric = MeanAveragePrecision() if generate_stats else None
metric_per_class = []
generate_metric_per_class = generate_stats and dataset_type != "FoodQS"
if generate_metric_per_class:
metric_per_class = [MeanAveragePrecision() for i in range(num_classes)]
num_to_class_name = [dataset.get_name_for_num(num) for num in range(num_classes)]
img_with_bbox = 0
with torch.no_grad():
for i, data in enumerate(tqdm(dataloader)):
inputs = data[0].to(device)
predictions = net(inputs)
inputs = inputs.to(cpu_device)
for j in range(len(inputs)):
current_img = dataset.get_image(current_i)
boxes = predictions[j]['boxes']
labels = predictions[j]['labels']
scores = predictions[j]['scores']
x_targets, x_labels = data[2][j], data[3][j]
pred_dict = [
{
"boxes": boxes.to(cpu_device),
"labels": labels.to(cpu_device),
"scores": scores.to(cpu_device)
}
]
target_dict = [
{
"boxes": x_targets,
"labels": x_labels
}
]
if len(boxes) > 0:
img_with_bbox += 1
output_images.append(add_bounding_boxes(current_img, predictions[j], dataset, threshold))
if generate_stats:
# current_i_o_u = intersection_over_union(current_img, x_targets, x_labels, boxes, labels, scores)
# for k in range(num_classes):
# i_o_u[k] += current_i_o_u[k]
metric.update(pred_dict, target_dict)
if generate_metric_per_class:
index_for_class = int(data[3][j])
metric_per_class[index_for_class].update(pred_dict, target_dict)
current_i += 1
if generate_stats:
for k in range(num_classes):
i_o_u[k] /= len(dataset)
return output_images, i_o_u, metric, metric_per_class, num_to_class_name, img_with_bbox
def inference_model(fmodel, name, generate_stats: bool=False, threshold: float=0.9, count_images_with_bbox: bool = False):
"""perform inference with a model from a certain file
Parameters
----------
fmodel : str
model file name
name : str
name of the model
generate_stats : bool
whether to generate stats, i.e. mAP values
threshold : float
threshold for adding bounding boxes to images
count_images_with_bbox : bool
whether to count images with at least one bounding box
"""
print(f"Performing inference with {name} ({convert_name_to_title(name)})...")
model_file = os.path.join(models_path, fmodel)
model = torch.load(model_file)
images, i_o_u, metric, metric_per_class, num_to_class_name, img_with_bbox = inference(model, generate_stats, threshold, dataset_type=convert_name_to_dataset_type(name))
model_image_path = os.path.join(inferenced_img_path, name)
os.makedirs(model_image_path, exist_ok=True)
for i, img in enumerate(images):
img.save(os.path.join(model_image_path, f"{i}.jpg"))
if generate_stats:
# print(f"IoU for {name}: {i_o_u}")
print(f"mAP for {name}: {metric.compute()}")
if len(metric_per_class) > 0:
print("mAP per class:")
for i, metric in enumerate(metric_per_class):
print(f"mAP for {num_to_class_name[i]}: {metric_per_class[i].compute()}")
if count_images_with_bbox:
print(f"{img_with_bbox} images have at least one bounding box")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog = 'inference',
description = 'perform inference with the models')
parser.add_argument('-l', '--labels', action='store_true')
parser.add_argument('-s', '--stats', action='store_true')
parser.add_argument('-t', '--threshold', type=float)
parser.add_argument('-c', '--count', action='store_true')
args = parser.parse_args()
threshold = 0.9 if args.threshold is None else args.threshold
count_images_with_bbox = False if args.count is None or args.count == False else True
if args.labels:
# FoodQS
dataset = PollenFoodQSDataset(k=100)
image_path = os.path.join(inferenced_img_path, "labels")
os.makedirs(image_path, exist_ok=True)
for i in tqdm(range(len(dataset))):
x, y, targets, labels = dataset[i]
img = dataset.get_image(i)
img = add_bounding_boxes_labels(img, targets, labels)
img.save(os.path.join(image_path, f"{i}.jpg"))
dataset = PollenFoodQSDataset(k=100, full_size=True)
image_path = os.path.join(inferenced_img_path, "labels full size")
os.makedirs(image_path, exist_ok=True)
for i in tqdm(range(len(dataset))):
x, y, targets, labels = dataset[i]
img = dataset.get_image(i)
img = add_bounding_boxes_labels(img, targets, labels)
img.save(os.path.join(image_path, f"{i}.jpg"))
# Pollen
dataset = PollenDataset()
image_path = os.path.join(inferenced_img_path, "labels pollen")
os.makedirs(image_path, exist_ok=True)
for i in tqdm(range(len(dataset))):
x, y, targets, labels = dataset[i]
img = dataset.get_image(i)
img = add_bounding_boxes_labels(img, targets, labels)
img.save(os.path.join(image_path, f"{i}.jpg"))
dataset = PollenDataset(full_size=True)
image_path = os.path.join(inferenced_img_path, "labels pollen full size")
os.makedirs(image_path, exist_ok=True)
for i in tqdm(range(len(dataset))):
x, y, targets, labels = dataset[i]
img = dataset.get_image(i)
img = add_bounding_boxes_labels(img, targets, labels)
img.save(os.path.join(image_path, f"{i}.jpg"))
else:
models = os.listdir(models_path)
for fmodel in models:
m = pattern_name.fullmatch(fmodel)
if m is None:
print(f"Skipping {fmodel}, name format wrong.")
continue
inference_model(fmodel, m.group(1), args.stats, threshold, count_images_with_bbox)