5151from tqdm import tqdm
5252from yolox .data .datasets .coco import remove_useless_info
5353from yolox .evaluators import COCOEvaluator as YOLOX_COCOEvaluator
54+ from yolox .utils import xyxy2xywh
5455
5556from compressai_vision .datasets import deccode_compressed_rle
5657from compressai_vision .registry import register_evaluator
@@ -924,9 +925,10 @@ def __init__(self):
924925 self .batch_size = 1
925926
926927 dataloader = dummy_dataloader ()
927- self ._evaluator = YOLOX_COCOEvaluator (
928- dataloader , dataset .input_size , - 1 , - 1 , - 1
929- )
928+
929+ self ._class_ids = class_ids
930+ self ._img_size = dataset .input_size
931+ self ._evaluator = YOLOX_COCOEvaluator (dataloader , None , - 1 , - 1 , - 1 )
930932 self .reset ()
931933
932934 def reset (self ):
@@ -940,8 +942,8 @@ def digest(self, gt, pred):
940942 img_widths = [gt [0 ]["width" ]]
941943 img_ids = [gt [0 ]["image_id" ]]
942944
943- data_list_elem , image_wise_data = self ._evaluator . convert_to_coco_format (
944- pred , [img_heights , img_widths ], img_ids , return_outputs = True
945+ data_list_elem , image_wise_data = self ._convert_to_coco_format (
946+ pred , [img_heights , img_widths ], img_ids
945947 )
946948 self .data_list .extend (data_list_elem )
947949 self .output_data .update (image_wise_data )
@@ -975,6 +977,55 @@ def results(self, save_path: str = None):
975977
976978 return {"AP" : listed_items [0 ] * 100 , "AP50" : listed_items [1 ] * 100 }
977979
980+ def _convert_to_coco_format (self , outputs , info_imgs , ids ):
981+ # reference : yolox > evaluators > coco_evaluator > convert_to_coco_format
982+ data_list = []
983+ image_wise_data = defaultdict (dict )
984+ for output , img_h , img_w , img_id in zip (
985+ outputs , info_imgs [0 ], info_imgs [1 ], ids
986+ ):
987+ if output is None :
988+ continue
989+ output = output .cpu ()
990+
991+ bboxes = output [:, 0 :4 ]
992+
993+ # preprocessing: resize
994+ scale = min (
995+ self ._img_size [0 ] / float (img_h ), self ._img_size [1 ] / float (img_w )
996+ )
997+ bboxes /= scale
998+ cls = output [:, 6 ]
999+ scores = output [:, 4 ] * output [:, 5 ]
1000+
1001+ image_wise_data .update (
1002+ {
1003+ img_id : {
1004+ "bboxes" : [box .numpy ().tolist () for box in bboxes ],
1005+ "scores" : [score .numpy ().item () for score in scores ],
1006+ "categories" : [
1007+ self ._class_ids [int (cls [ind ])]
1008+ for ind in range (bboxes .shape [0 ])
1009+ ],
1010+ }
1011+ }
1012+ )
1013+
1014+ bboxes = xyxy2xywh (bboxes )
1015+
1016+ for ind in range (bboxes .shape [0 ]):
1017+ label = self ._class_ids [int (cls [ind ])]
1018+ pred_data = {
1019+ "image_id" : img_id ,
1020+ "category_id" : label ,
1021+ "bbox" : bboxes [ind ].numpy ().tolist (),
1022+ "score" : scores [ind ].numpy ().item (),
1023+ "segmentation" : [],
1024+ } # COCO json format
1025+ data_list .append (pred_data )
1026+
1027+ return data_list , image_wise_data
1028+
9781029
9791030@register_evaluator ("MMPOSE-COCO-EVAL" )
9801031class MMPOSECOCOEval (BaseEvaluator ):
0 commit comments