Create postprocessor
Browse files- postprocessor +79 -0
postprocessor
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""by lyuwenyu
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
import torchvision
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
__all__ = ['RTDETRPostProcessor']
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class RTDETRPostProcessor(nn.Module):
|
| 15 |
+
__share__ = ['num_classes', 'use_focal_loss', 'num_top_queries', 'remap_mscoco_category']
|
| 16 |
+
|
| 17 |
+
def __init__(self, num_classes=80, use_focal_loss=True, num_top_queries=300, remap_mscoco_category=False) -> None:
|
| 18 |
+
super().__init__()
|
| 19 |
+
self.use_focal_loss = use_focal_loss
|
| 20 |
+
self.num_top_queries = num_top_queries
|
| 21 |
+
self.num_classes = num_classes
|
| 22 |
+
self.remap_mscoco_category = remap_mscoco_category
|
| 23 |
+
self.deploy_mode = False
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def extra_repr(self) -> str:
|
| 27 |
+
return f'use_focal_loss={self.use_focal_loss}, num_classes={self.num_classes}, num_top_queries={self.num_top_queries}'
|
| 28 |
+
|
| 29 |
+
# def forward(self, outputs, orig_target_sizes):
|
| 30 |
+
def forward(self, outputs, orig_target_sizes):
|
| 31 |
+
|
| 32 |
+
logits, boxes = outputs['pred_logits'], outputs['pred_boxes']
|
| 33 |
+
# orig_target_sizes = torch.stack([t["orig_size"] for t in targets], dim=0)
|
| 34 |
+
|
| 35 |
+
bbox_pred = torchvision.ops.box_convert(boxes, in_fmt='cxcywh', out_fmt='xyxy')
|
| 36 |
+
bbox_pred *= orig_target_sizes.repeat(1, 2).unsqueeze(1)
|
| 37 |
+
|
| 38 |
+
if self.use_focal_loss:
|
| 39 |
+
scores = F.sigmoid(logits)
|
| 40 |
+
scores, index = torch.topk(scores.flatten(1), self.num_top_queries, axis=-1)
|
| 41 |
+
labels = index % self.num_classes
|
| 42 |
+
index = index // self.num_classes
|
| 43 |
+
boxes = bbox_pred.gather(dim=1, index=index.unsqueeze(-1).repeat(1, 1, bbox_pred.shape[-1]))
|
| 44 |
+
|
| 45 |
+
else:
|
| 46 |
+
scores = F.softmax(logits)[:, :, :-1]
|
| 47 |
+
scores, labels = scores.max(dim=-1)
|
| 48 |
+
boxes = bbox_pred
|
| 49 |
+
if scores.shape[1] > self.num_top_queries:
|
| 50 |
+
scores, index = torch.topk(scores, self.num_top_queries, dim=-1)
|
| 51 |
+
labels = torch.gather(labels, dim=1, index=index)
|
| 52 |
+
boxes = torch.gather(boxes, dim=1, index=index.unsqueeze(-1).tile(1, 1, boxes.shape[-1]))
|
| 53 |
+
|
| 54 |
+
# TODO for onnx export
|
| 55 |
+
if self.deploy_mode:
|
| 56 |
+
return labels, boxes, scores
|
| 57 |
+
|
| 58 |
+
# TODO
|
| 59 |
+
if self.remap_mscoco_category:
|
| 60 |
+
from ...data.coco import mscoco_label2category
|
| 61 |
+
labels = torch.tensor([mscoco_label2category[int(x.item())] for x in labels.flatten()])\
|
| 62 |
+
.to(boxes.device).reshape(labels.shape)
|
| 63 |
+
|
| 64 |
+
results = []
|
| 65 |
+
for lab, box, sco in zip(labels, boxes, scores):
|
| 66 |
+
result = dict(labels=lab, boxes=box, scores=sco)
|
| 67 |
+
results.append(result)
|
| 68 |
+
|
| 69 |
+
return results
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def deploy(self, ):
|
| 73 |
+
self.eval()
|
| 74 |
+
self.deploy_mode = True
|
| 75 |
+
return self
|
| 76 |
+
|
| 77 |
+
@property
|
| 78 |
+
def iou_types(self, ):
|
| 79 |
+
return ('bbox', )
|