| 16 | |
| 17 | |
| 18 | class Decoder: |
| 19 | |
| 20 | def __init__(self, model_type: ModelType, model_only: bool = False): |
| 21 | self.model_type = model_type |
| 22 | self.model_only = model_only |
| 23 | self.boxes_pro = [] |
| 24 | self.scores_pro = [] |
| 25 | self.labels_pro = [] |
| 26 | self.is_logging = False |
| 27 | |
| 28 | def __call__(self, |
| 29 | feats: Union[List, Tuple], |
| 30 | conf_thres: float, |
| 31 | num_labels: int = 80, |
| 32 | **kwargs) -> Tuple: |
| 33 | if not self.is_logging: |
| 34 | print('Only support decode in batch==1') |
| 35 | self.is_logging = True |
| 36 | self.boxes_pro.clear() |
| 37 | self.scores_pro.clear() |
| 38 | self.labels_pro.clear() |
| 39 | |
| 40 | if self.model_only: |
| 41 | # transpose channel to last dim for easy decoding |
| 42 | feats = [ |
| 43 | np.ascontiguousarray(feat[0].transpose(1, 2, 0)) |
| 44 | for feat in feats |
| 45 | ] |
| 46 | else: |
| 47 | # ax620a horizonX3 transpose channel to last dim by default |
| 48 | feats = [np.ascontiguousarray(feat) for feat in feats] |
| 49 | if self.model_type == ModelType.YOLOV5: |
| 50 | self.__yolov5_decode(feats, conf_thres, num_labels, **kwargs) |
| 51 | elif self.model_type == ModelType.YOLOX: |
| 52 | self.__yolox_decode(feats, conf_thres, num_labels, **kwargs) |
| 53 | elif self.model_type in (ModelType.PPYOLOE, ModelType.PPYOLOEP): |
| 54 | self.__ppyoloe_decode(feats, conf_thres, num_labels, **kwargs) |
| 55 | elif self.model_type == ModelType.YOLOV6: |
| 56 | self.__yolov6_decode(feats, conf_thres, num_labels, **kwargs) |
| 57 | elif self.model_type == ModelType.YOLOV7: |
| 58 | self.__yolov7_decode(feats, conf_thres, num_labels, **kwargs) |
| 59 | elif self.model_type == ModelType.RTMDET: |
| 60 | self.__rtmdet_decode(feats, conf_thres, num_labels, **kwargs) |
| 61 | elif self.model_type == ModelType.YOLOV8: |
| 62 | self.__yolov8_decode(feats, conf_thres, num_labels, **kwargs) |
| 63 | else: |
| 64 | raise NotImplementedError |
| 65 | return self.boxes_pro, self.scores_pro, self.labels_pro |
| 66 | |
| 67 | def __yolov5_decode(self, |
| 68 | feats: List[ndarray], |
| 69 | conf_thres: float, |
| 70 | num_labels: int = 80, |
| 71 | **kwargs): |
| 72 | anchors: Union[List, Tuple] = kwargs.get( |
| 73 | 'anchors', |
| 74 | [[(10, 13), (16, 30), |
| 75 | (33, 23)], [(30, 61), (62, 45), |