-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathdetr.py
145 lines (118 loc) · 4.83 KB
/
detr.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
import os
import json
from rfdetr.config import RFDETRBaseConfig, RFDETRLargeConfig, TrainConfig, ModelConfig
from rfdetr.main import Model, download_pretrain_weights
from functools import partial
from logging import getLogger
import torch
import torchvision.transforms.functional as F
from typing import Union
from PIL import Image
import numpy as np
from collections import defaultdict
import supervision as sv
logger = getLogger(__name__)
class RFDETR:
means = [0.485, 0.456, 0.406]
stds = [0.229, 0.224, 0.225]
def __init__(self, **kwargs):
self.model_config = self.get_model_config(**kwargs)
self.maybe_download_pretrain_weights()
self.model = self.get_model(self.model_config)
self.callbacks = defaultdict(list)
def maybe_download_pretrain_weights(self):
download_pretrain_weights(self.model_config.pretrain_weights)
def get_model_config(self, **kwargs):
return ModelConfig(**kwargs)
def train(self, **kwargs):
config = self.get_train_config(**kwargs)
self.train_from_config(config, **kwargs)
def export(self, **kwargs):
self.model.export(**kwargs)
def train_from_config(self, config: TrainConfig, **kwargs):
with open(
os.path.join(config.dataset_dir, "train", "_annotations.coco.json"), "r"
) as f:
anns = json.load(f)
num_classes = len(anns["categories"])
if self.model_config.num_classes != num_classes:
logger.warning(
f"num_classes mismatch: model has {self.model_config.num_classes} classes, but your dataset has {num_classes} classes\n"
f"reinitializing your detection head with {num_classes} classes."
)
self.model.reinitialize_detection_head(num_classes)
train_config = config.dict()
model_config = self.model_config.dict()
model_config.pop("num_classes")
for k, v in train_config.items():
if k in model_config:
model_config.pop(k)
if k in kwargs:
kwargs.pop(k)
all_kwargs = {**model_config, **train_config, **kwargs, "num_classes": num_classes}
self.model.train(
**all_kwargs,
callbacks=self.callbacks,
)
def get_train_config(self, **kwargs):
return TrainConfig(**kwargs)
def get_model(self, config: ModelConfig):
return Model(**config.dict())
def predict(
self,
image_or_path: Union[str, Image.Image, np.ndarray, torch.Tensor],
threshold: float = 0.5,
**kwargs,
):
self.model.model.eval()
with torch.inference_mode():
if isinstance(image_or_path, str):
image_or_path = Image.open(image_or_path)
w, h = image_or_path.size
if not isinstance(image_or_path, torch.Tensor):
image = F.to_tensor(image_or_path)
_, h, w = image.shape
else:
logger.warning(
"image_or_path is a torch.Tensor\n",
"we expect an image divided by 255 at (C, H, W)",
)
assert image_or_path.shape[0] == 3, "image must have 3 channels"
h, w = image_or_path.shape[1:]
image = image.to(self.model.device)
image = F.normalize(image, self.means, self.stds)
image = F.resize(image, (self.model.resolution, self.model.resolution))
predictions = self.model.model.forward(image[None, :])
bboxes = predictions["pred_boxes"]
results = self.model.postprocessors["bbox"](
predictions,
target_sizes=torch.tensor([[h, w]], device=self.model.device),
)
scores, labels, boxes = [], [], []
for result in results:
scores.append(result["scores"])
labels.append(result["labels"])
boxes.append(result["boxes"])
scores = torch.stack(scores)
labels = torch.stack(labels)
boxes = torch.stack(boxes)
keep_inds = scores > threshold
boxes = boxes[keep_inds]
labels = labels[keep_inds]
scores = scores[keep_inds]
detections = sv.Detections(
xyxy=boxes.cpu().numpy(),
class_id=labels.cpu().numpy(),
confidence=scores.cpu().numpy(),
)
return detections
class RFDETRBase(RFDETR):
def get_model_config(self, **kwargs):
return RFDETRBaseConfig(**kwargs)
def get_train_config(self, **kwargs):
return TrainConfig(**kwargs)
class RFDETRLarge(RFDETR):
def get_model_config(self, **kwargs):
return RFDETRLargeConfig(**kwargs)
def get_train_config(self, **kwargs):
return TrainConfig(**kwargs)