-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
pascalvoc.py
418 lines (382 loc) · 18.5 KB
/
pascalvoc.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
###########################################################################################
# #
# This sample shows how to evaluate object detections applying the following metrics: #
# * Precision x Recall curve ----> used by VOC PASCAL 2012) #
# * Average Precision (AP) ----> used by VOC PASCAL 2012) #
# #
# Developed by: Rafael Padilla ([email protected]) #
# SMT - Signal Multimedia and Telecommunications Lab #
# COPPE - Universidade Federal do Rio de Janeiro #
# Last modification: Feb 12th 2021 #
###########################################################################################
####################################################################################################
# #
# THE CURRENT VERSION WAS UPDATED WITH A VISUAL INTERFACE, INCLUDING MORE METRICS AND SUPPORTING #
# OTHER FILE FORMATS. PLEASE ACCESS IT ACCESSED AT: #
# #
# https://github.com/rafaelpadilla/review_object_detection_metrics #
# #
# @Article{electronics10030279, #
# author = {Padilla, Rafael and Passos, Wesley L. and Dias, Thadeu L. B. and Netto, #
# Sergio L. and da Silva, Eduardo A. B.}, #
# title = {A Comparative Analysis of Object Detection Metrics with a Companion #
# Open-Source Toolkit}, #
# journal = {Electronics}, #
# volume = {10}, #
# year = {2021}, #
# number = {3}, #
# article-number = {279}, #
# url = {https://www.mdpi.com/2079-9292/10/3/279}, #
# issn = {2079-9292}, #
# doi = {10.3390/electronics10030279}, } #
####################################################################################################
####################################################################################################
# If you use this project, please consider citing: #
# #
# @INPROCEEDINGS {padillaCITE2020, #
# author = {R. {Padilla} and S. L. {Netto} and E. A. B. {da Silva}}, #
# title = {A Survey on Performance Metrics for Object-Detection Algorithms}, #
# booktitle = {2020 International Conference on Systems, Signals and Image Processing (IWSSIP)},#
# year = {2020}, #
# pages = {237-242},} #
# #
# This work is published at: https://github.com/rafaelpadilla/Object-Detection-Metrics #
####################################################################################################
import argparse
import glob
import os
import shutil
import sys
import _init_paths
from BoundingBox import BoundingBox
from BoundingBoxes import BoundingBoxes
from Evaluator import *
from utils import BBFormat
# Validate formats
def ValidateFormats(argFormat, argName, errors):
if argFormat == 'xywh':
return BBFormat.XYWH
elif argFormat == 'xyrb':
return BBFormat.XYX2Y2
elif argFormat is None:
return BBFormat.XYWH # default when nothing is passed
else:
errors.append('argument %s: invalid value. It must be either \'xywh\' or \'xyrb\'' %
argName)
# Validate mandatory args
def ValidateMandatoryArgs(arg, argName, errors):
if arg is None:
errors.append('argument %s: required argument' % argName)
else:
return True
def ValidateImageSize(arg, argName, argInformed, errors):
errorMsg = 'argument %s: required argument if %s is relative' % (argName, argInformed)
ret = None
if arg is None:
errors.append(errorMsg)
else:
arg = arg.replace('(', '').replace(')', '')
args = arg.split(',')
if len(args) != 2:
errors.append('%s. It must be in the format \'width,height\' (e.g. \'600,400\')' %
errorMsg)
else:
if not args[0].isdigit() or not args[1].isdigit():
errors.append(
'%s. It must be in INdiaTEGER the format \'width,height\' (e.g. \'600,400\')' %
errorMsg)
else:
ret = (int(args[0]), int(args[1]))
return ret
# Validate coordinate types
def ValidateCoordinatesTypes(arg, argName, errors):
if arg == 'abs':
return CoordinatesType.Absolute
elif arg == 'rel':
return CoordinatesType.Relative
elif arg is None:
return CoordinatesType.Absolute # default when nothing is passed
errors.append('argument %s: invalid value. It must be either \'rel\' or \'abs\'' % argName)
def ValidatePaths(arg, nameArg, errors):
if arg is None:
errors.append('argument %s: invalid directory' % nameArg)
elif os.path.isdir(arg) is False and os.path.isdir(os.path.join(currentPath, arg)) is False:
errors.append('argument %s: directory does not exist \'%s\'' % (nameArg, arg))
# elif os.path.isdir(os.path.join(currentPath, arg)) is True:
# arg = os.path.join(currentPath, arg)
else:
arg = os.path.join(currentPath, arg)
return arg
def getBoundingBoxes(directory,
isGT,
bbFormat,
coordType,
allBoundingBoxes=None,
allClasses=None,
imgSize=(0, 0)):
"""Read txt files containing bounding boxes (ground truth and detections)."""
if allBoundingBoxes is None:
allBoundingBoxes = BoundingBoxes()
if allClasses is None:
allClasses = []
# Read ground truths
os.chdir(directory)
files = glob.glob("*.txt")
files.sort()
# Read GT detections from txt file
# Each line of the files in the groundtruths folder represents a ground truth bounding box
# (bounding boxes that a detector should detect)
# Each value of each line is "class_id, x, y, width, height" respectively
# Class_id represents the class of the bounding box
# x, y represents the most top-left coordinates of the bounding box
# x2, y2 represents the most bottom-right coordinates of the bounding box
for f in files:
nameOfImage = f.replace(".txt", "")
fh1 = open(f, "r")
for line in fh1:
line = line.replace("\n", "")
if line.replace(' ', '') == '':
continue
splitLine = line.split(" ")
if isGT:
# idClass = int(splitLine[0]) #class
idClass = (splitLine[0]) # class
x = float(splitLine[1])
y = float(splitLine[2])
w = float(splitLine[3])
h = float(splitLine[4])
bb = BoundingBox(nameOfImage,
idClass,
x,
y,
w,
h,
coordType,
imgSize,
BBType.GroundTruth,
format=bbFormat)
else:
# idClass = int(splitLine[0]) #class
idClass = (splitLine[0]) # class
confidence = float(splitLine[1])
x = float(splitLine[2])
y = float(splitLine[3])
w = float(splitLine[4])
h = float(splitLine[5])
bb = BoundingBox(nameOfImage,
idClass,
x,
y,
w,
h,
coordType,
imgSize,
BBType.Detected,
confidence,
format=bbFormat)
allBoundingBoxes.addBoundingBox(bb)
if idClass not in allClasses:
allClasses.append(idClass)
fh1.close()
return allBoundingBoxes, allClasses
# Get current path to set default folders
currentPath = os.path.dirname(os.path.abspath(__file__))
VERSION = '0.2 (beta)'
with open('message.txt', 'r') as f:
message = f'\n\n{f.read()}\n\n'
print(message)
parser = argparse.ArgumentParser(
prog='Object Detection Metrics - Pascal VOC',
description=
f'{message}\nThis project applies the most popular metrics used to evaluate object detection '
'algorithms.\nThe current implemention runs the Pascal VOC metrics.\nFor further references, '
'please check:\nhttps://github.com/rafaelpadilla/Object-Detection-Metrics',
epilog="Developed by: Rafael Padilla ([email protected])")
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + VERSION)
# Positional arguments
# Mandatory
parser.add_argument('-gt',
'--gtfolder',
dest='gtFolder',
default=os.path.join(currentPath, 'groundtruths'),
metavar='',
help='folder containing your ground truth bounding boxes')
parser.add_argument('-det',
'--detfolder',
dest='detFolder',
default=os.path.join(currentPath, 'detections'),
metavar='',
help='folder containing your detected bounding boxes')
# Optional
parser.add_argument('-t',
'--threshold',
dest='iouThreshold',
type=float,
default=0.5,
metavar='',
help='IOU threshold. Default 0.5')
parser.add_argument('-gtformat',
dest='gtFormat',
metavar='',
default='xywh',
help='format of the coordinates of the ground truth bounding boxes: '
'(\'xywh\': <left> <top> <width> <height>)'
' or (\'xyrb\': <left> <top> <right> <bottom>)')
parser.add_argument('-detformat',
dest='detFormat',
metavar='',
default='xywh',
help='format of the coordinates of the detected bounding boxes '
'(\'xywh\': <left> <top> <width> <height>) '
'or (\'xyrb\': <left> <top> <right> <bottom>)')
parser.add_argument('-gtcoords',
dest='gtCoordinates',
default='abs',
metavar='',
help='reference of the ground truth bounding box coordinates: absolute '
'values (\'abs\') or relative to its image size (\'rel\')')
parser.add_argument('-detcoords',
default='abs',
dest='detCoordinates',
metavar='',
help='reference of the ground truth bounding box coordinates: '
'absolute values (\'abs\') or relative to its image size (\'rel\')')
parser.add_argument('-imgsize',
dest='imgSize',
metavar='',
help='image size. Required if -gtcoords or -detcoords are \'rel\'')
parser.add_argument('-sp',
'--savepath',
dest='savePath',
metavar='',
help='folder where the plots are saved')
parser.add_argument('-np',
'--noplot',
dest='showPlot',
action='store_false',
help='no plot is shown during execution')
args = parser.parse_args()
iouThreshold = args.iouThreshold
# Arguments validation
errors = []
# Validate formats
gtFormat = ValidateFormats(args.gtFormat, '-gtformat', errors)
detFormat = ValidateFormats(args.detFormat, '-detformat', errors)
# Groundtruth folder
if ValidateMandatoryArgs(args.gtFolder, '-gt/--gtfolder', errors):
gtFolder = ValidatePaths(args.gtFolder, '-gt/--gtfolder', errors)
else:
# errors.pop()
gtFolder = os.path.join(currentPath, 'groundtruths')
if os.path.isdir(gtFolder) is False:
errors.append('folder %s not found' % gtFolder)
# Coordinates types
gtCoordType = ValidateCoordinatesTypes(args.gtCoordinates, '-gtCoordinates', errors)
detCoordType = ValidateCoordinatesTypes(args.detCoordinates, '-detCoordinates', errors)
imgSize = (0, 0)
if gtCoordType == CoordinatesType.Relative: # Image size is required
imgSize = ValidateImageSize(args.imgSize, '-imgsize', '-gtCoordinates', errors)
if detCoordType == CoordinatesType.Relative: # Image size is required
imgSize = ValidateImageSize(args.imgSize, '-imgsize', '-detCoordinates', errors)
# Detection folder
if ValidateMandatoryArgs(args.detFolder, '-det/--detfolder', errors):
detFolder = ValidatePaths(args.detFolder, '-det/--detfolder', errors)
else:
# errors.pop()
detFolder = os.path.join(currentPath, 'detections')
if os.path.isdir(detFolder) is False:
errors.append('folder %s not found' % detFolder)
if args.savePath is not None:
savePath = ValidatePaths(args.savePath, '-sp/--savepath', errors)
else:
savePath = os.path.join(currentPath, 'results')
# Validate savePath
# If error, show error messages
if len(errors) != 0:
print("""usage: Object Detection Metrics [-h] [-v] [-gt] [-det] [-t] [-gtformat]
[-detformat] [-save]""")
print('Object Detection Metrics: error(s): ')
[print(e) for e in errors]
sys.exit()
# Check if path to save results already exists and is not empty
if os.path.isdir(savePath) and os.listdir(savePath):
key_pressed = ''
while key_pressed.upper() not in ['Y', 'N']:
print(f'Folder {savePath} already exists and may contain important results.\n')
print(f'Enter \'Y\' to continue. WARNING: THIS WILL REMOVE ALL THE CONTENTS OF THE FOLDER!')
print(f'Or enter \'N\' to abort and choose another folder to save the results.')
key_pressed = input('')
if key_pressed.upper() == 'N':
print('Process canceled')
sys.exit()
# Clear folder and save results
shutil.rmtree(savePath, ignore_errors=True)
os.makedirs(savePath)
# Show plot during execution
showPlot = args.showPlot
# print('iouThreshold= %f' % iouThreshold)
# print('savePath = %s' % savePath)
# print('gtFormat = %s' % gtFormat)
# print('detFormat = %s' % detFormat)
# print('gtFolder = %s' % gtFolder)
# print('detFolder = %s' % detFolder)
# print('gtCoordType = %s' % gtCoordType)
# print('detCoordType = %s' % detCoordType)
# print('showPlot %s' % showPlot)
# Get groundtruth boxes
allBoundingBoxes, allClasses = getBoundingBoxes(gtFolder,
True,
gtFormat,
gtCoordType,
imgSize=imgSize)
# Get detected boxes
allBoundingBoxes, allClasses = getBoundingBoxes(detFolder,
False,
detFormat,
detCoordType,
allBoundingBoxes,
allClasses,
imgSize=imgSize)
allClasses.sort()
evaluator = Evaluator()
acc_AP = 0
validClasses = 0
# Plot Precision x Recall curve
detections = evaluator.PlotPrecisionRecallCurve(
allBoundingBoxes, # Object containing all bounding boxes (ground truths and detections)
IOUThreshold=iouThreshold, # IOU threshold
method=MethodAveragePrecision.EveryPointInterpolation,
showAP=True, # Show Average Precision in the title of the plot
showInterpolatedPrecision=False, # Don't plot the interpolated precision curve
savePath=savePath,
showGraphic=showPlot)
f = open(os.path.join(savePath, 'results.txt'), 'w')
f.write('Object Detection Metrics\n')
f.write('https://github.com/rafaelpadilla/Object-Detection-Metrics\n\n\n')
f.write('Average Precision (AP), Precision and Recall per class:')
# each detection is a class
for metricsPerClass in detections:
# Get metric values per each class
cl = metricsPerClass['class']
ap = metricsPerClass['AP']
precision = metricsPerClass['precision']
recall = metricsPerClass['recall']
totalPositives = metricsPerClass['total positives']
total_TP = metricsPerClass['total TP']
total_FP = metricsPerClass['total FP']
if totalPositives > 0:
validClasses = validClasses + 1
acc_AP = acc_AP + ap
prec = ['%.2f' % p for p in precision]
rec = ['%.2f' % r for r in recall]
ap_str = "{0:.2f}%".format(ap * 100)
# ap_str = "{0:.4f}%".format(ap * 100)
print('AP: %s (%s)' % (ap_str, cl))
f.write('\n\nClass: %s' % cl)
f.write('\nAP: %s' % ap_str)
f.write('\nPrecision: %s' % prec)
f.write('\nRecall: %s' % rec)
mAP = acc_AP / validClasses
mAP_str = "{0:.2f}%".format(mAP * 100)
print('mAP: %s' % mAP_str)
f.write('\n\n\nmAP: %s' % mAP_str)