Skip to content

Commit

Permalink
chore(data): Adapt to COCO format dataset (#1052)
Browse files Browse the repository at this point in the history
chore(data): Adapt to COCO format dataset
  • Loading branch information
yyqgood committed Jan 12, 2022
1 parent d669fd3 commit 6c874ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions tools/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def main(exp, args, num_gpu):

evaluator = exp.get_evaluator(args.batch_size, is_distributed, args.test, args.legacy)
evaluator.per_class_mAP = True
evaluator.per_class_AR = True

torch.cuda.set_device(rank)
model.cuda(rank)
Expand Down
29 changes: 28 additions & 1 deletion yolox/evaluators/coco_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,33 @@
)


def per_class_AR_table(coco_eval, class_names=COCO_CLASSES, headers=["class", "AR"], colums=6):
per_class_AR = {}
recalls = coco_eval.eval["recall"]
# dimension of recalls: [TxKxAxM]
# recall has dims (iou, cls, area range, max dets)
assert len(class_names) == recalls.shape[1]

for idx, name in enumerate(class_names):
recall = recalls[:, idx, 0, -1]
recall = recall[recall > -1]
ar = np.mean(recall) if recall.size else float("nan")
per_class_AR[name] = float(ar * 100)

num_cols = min(colums, len(per_class_AR) * len(headers))
result_pair = [x for pair in per_class_AR.items() for x in pair]
row_pair = itertools.zip_longest(*[result_pair[i::num_cols] for i in range(num_cols)])
table_headers = headers * (num_cols // len(headers))
table = tabulate(
row_pair, tablefmt="pipe", floatfmt=".3f", headers=table_headers, numalign="left",
)
return table


def per_class_mAP_table(coco_eval, class_names=COCO_CLASSES, headers=["class", "AP"], colums=6):
per_class_mAP = {}
precisions = coco_eval.eval["precision"]
# dimension of precisions: [TxRxKxAxM]
# precision has dims (iou, recall, cls, area range, max dets)
assert len(class_names) == precisions.shape[2]

Expand Down Expand Up @@ -66,6 +90,7 @@ def __init__(
num_classes: int,
testdev: bool = False,
per_class_mAP: bool = False,
per_class_AR: bool = False,
):
"""
Args:
Expand Down Expand Up @@ -254,7 +279,9 @@ def evaluate_prediction(self, data_dict, statistics):
cocoEval.summarize()
info += redirect_string.getvalue()
if self.per_class_mAP:
info += "per class mAP:\n" + per_class_mAP_table(cocoEval)
info += "per class mAP:\n" + per_class_mAP_table(cocoEval) + "\n"
if self.per_class_AR:
info += "per class AR:\n" + per_class_AR_table(cocoEval) + "\n"
return cocoEval.stats[0], cocoEval.stats[1], info
else:
return 0, 0, info
3 changes: 2 additions & 1 deletion yolox/exp/yolox_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self):
self.data_dir = None
self.train_ann = "instances_train2017.json"
self.val_ann = "instances_val2017.json"
self.test_ann = "instances_test2017.json"

# --------------- transform config ----------------- #
self.mosaic_prob = 1.0
Expand Down Expand Up @@ -242,7 +243,7 @@ def get_eval_loader(self, batch_size, is_distributed, testdev=False, legacy=Fals

valdataset = COCODataset(
data_dir=self.data_dir,
json_file=self.val_ann if not testdev else "image_info_test-dev2017.json",
json_file=self.val_ann if not testdev else self.test_ann,
name="val2017" if not testdev else "test2017",
img_size=self.test_size,
preproc=ValTransform(legacy=legacy),
Expand Down

0 comments on commit 6c874ce

Please sign in to comment.