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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import random
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
|
| 6 |
+
from models.common import Conv, DWConv
|
| 7 |
+
from utils.google_utils import attempt_download
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class CrossConv(nn.Module):
|
| 11 |
+
# Cross Convolution Downsample
|
| 12 |
+
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False):
|
| 13 |
+
# ch_in, ch_out, kernel, stride, groups, expansion, shortcut
|
| 14 |
+
super(CrossConv, self).__init__()
|
| 15 |
+
c_ = int(c2 * e) # hidden channels
|
| 16 |
+
self.cv1 = Conv(c1, c_, (1, k), (1, s))
|
| 17 |
+
self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g)
|
| 18 |
+
self.add = shortcut and c1 == c2
|
| 19 |
+
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class Sum(nn.Module):
|
| 25 |
+
# Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
|
| 26 |
+
def __init__(self, n, weight=False): # n: number of inputs
|
| 27 |
+
super(Sum, self).__init__()
|
| 28 |
+
self.weight = weight # apply weights boolean
|
| 29 |
+
self.iter = range(n - 1) # iter object
|
| 30 |
+
if weight:
|
| 31 |
+
self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) # layer weights
|
| 32 |
+
|
| 33 |
+
def forward(self, x):
|
| 34 |
+
y = x[0] # no weight
|
| 35 |
+
if self.weight:
|
| 36 |
+
w = torch.sigmoid(self.w) * 2
|
| 37 |
+
for i in self.iter:
|
| 38 |
+
y = y + x[i + 1] * w[i]
|
| 39 |
+
else:
|
| 40 |
+
for i in self.iter:
|
| 41 |
+
y = y + x[i + 1]
|
| 42 |
+
return y
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class MixConv2d(nn.Module):
|
| 46 |
+
# Mixed Depthwise Conv https://arxiv.org/abs/1907.09595
|
| 47 |
+
def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True):
|
| 48 |
+
super(MixConv2d, self).__init__()
|
| 49 |
+
groups = len(k)
|
| 50 |
+
if equal_ch: # equal c_ per group
|
| 51 |
+
i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices
|
| 52 |
+
c_ = [(i == g).sum() for g in range(groups)] # intermediate channels
|
| 53 |
+
else: # equal weight.numel() per group
|
| 54 |
+
b = [c2] + [0] * groups
|
| 55 |
+
a = np.eye(groups + 1, groups, k=-1)
|
| 56 |
+
a -= np.roll(a, 1, axis=1)
|
| 57 |
+
a *= np.array(k) ** 2
|
| 58 |
+
a[0] = 1
|
| 59 |
+
c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
|
| 60 |
+
|
| 61 |
+
self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)])
|
| 62 |
+
self.bn = nn.BatchNorm2d(c2)
|
| 63 |
+
self.act = nn.LeakyReLU(0.1, inplace=True)
|
| 64 |
+
|
| 65 |
+
def forward(self, x):
|
| 66 |
+
return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class Ensemble(nn.ModuleList):
|
| 70 |
+
# Ensemble of models
|
| 71 |
+
def __init__(self):
|
| 72 |
+
super(Ensemble, self).__init__()
|
| 73 |
+
|
| 74 |
+
def forward(self, x, augment=False):
|
| 75 |
+
y = []
|
| 76 |
+
for module in self:
|
| 77 |
+
y.append(module(x, augment)[0])
|
| 78 |
+
# y = torch.stack(y).max(0)[0] # max ensemble
|
| 79 |
+
# y = torch.stack(y).mean(0) # mean ensemble
|
| 80 |
+
y = torch.cat(y, 1) # nms ensemble
|
| 81 |
+
return y, None # inference, train output
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class ORT_NMS(torch.autograd.Function):
|
| 88 |
+
'''ONNX-Runtime NMS operation'''
|
| 89 |
+
@staticmethod
|
| 90 |
+
def forward(ctx,
|
| 91 |
+
boxes,
|
| 92 |
+
scores,
|
| 93 |
+
max_output_boxes_per_class=torch.tensor([100]),
|
| 94 |
+
iou_threshold=torch.tensor([0.45]),
|
| 95 |
+
score_threshold=torch.tensor([0.25])):
|
| 96 |
+
device = boxes.device
|
| 97 |
+
batch = scores.shape[0]
|
| 98 |
+
num_det = random.randint(0, 100)
|
| 99 |
+
batches = torch.randint(0, batch, (num_det,)).sort()[0].to(device)
|
| 100 |
+
idxs = torch.arange(100, 100 + num_det).to(device)
|
| 101 |
+
zeros = torch.zeros((num_det,), dtype=torch.int64).to(device)
|
| 102 |
+
selected_indices = torch.cat([batches[None], zeros[None], idxs[None]], 0).T.contiguous()
|
| 103 |
+
selected_indices = selected_indices.to(torch.int64)
|
| 104 |
+
return selected_indices
|
| 105 |
+
|
| 106 |
+
@staticmethod
|
| 107 |
+
def symbolic(g, boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold):
|
| 108 |
+
return g.op("NonMaxSuppression", boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
class TRT_NMS(torch.autograd.Function):
|
| 112 |
+
'''TensorRT NMS operation'''
|
| 113 |
+
@staticmethod
|
| 114 |
+
def forward(
|
| 115 |
+
ctx,
|
| 116 |
+
boxes,
|
| 117 |
+
scores,
|
| 118 |
+
background_class=-1,
|
| 119 |
+
box_coding=1,
|
| 120 |
+
iou_threshold=0.45,
|
| 121 |
+
max_output_boxes=100,
|
| 122 |
+
plugin_version="1",
|
| 123 |
+
score_activation=0,
|
| 124 |
+
score_threshold=0.25,
|
| 125 |
+
):
|
| 126 |
+
batch_size, num_boxes, num_classes = scores.shape
|
| 127 |
+
num_det = torch.randint(0, max_output_boxes, (batch_size, 1), dtype=torch.int32)
|
| 128 |
+
det_boxes = torch.randn(batch_size, max_output_boxes, 4)
|
| 129 |
+
det_scores = torch.randn(batch_size, max_output_boxes)
|
| 130 |
+
det_classes = torch.randint(0, num_classes, (batch_size, max_output_boxes), dtype=torch.int32)
|
| 131 |
+
return num_det, det_boxes, det_scores, det_classes
|
| 132 |
+
|
| 133 |
+
@staticmethod
|
| 134 |
+
def symbolic(g,
|
| 135 |
+
boxes,
|
| 136 |
+
scores,
|
| 137 |
+
background_class=-1,
|
| 138 |
+
box_coding=1,
|
| 139 |
+
iou_threshold=0.45,
|
| 140 |
+
max_output_boxes=100,
|
| 141 |
+
plugin_version="1",
|
| 142 |
+
score_activation=0,
|
| 143 |
+
score_threshold=0.25):
|
| 144 |
+
out = g.op("TRT::EfficientNMS_TRT",
|
| 145 |
+
boxes,
|
| 146 |
+
scores,
|
| 147 |
+
background_class_i=background_class,
|
| 148 |
+
box_coding_i=box_coding,
|
| 149 |
+
iou_threshold_f=iou_threshold,
|
| 150 |
+
max_output_boxes_i=max_output_boxes,
|
| 151 |
+
plugin_version_s=plugin_version,
|
| 152 |
+
score_activation_i=score_activation,
|
| 153 |
+
score_threshold_f=score_threshold,
|
| 154 |
+
outputs=4)
|
| 155 |
+
nums, boxes, scores, classes = out
|
| 156 |
+
return nums, boxes, scores, classes
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
class ONNX_ORT(nn.Module):
|
| 160 |
+
'''onnx module with ONNX-Runtime NMS operation.'''
|
| 161 |
+
def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=640, device=None):
|
| 162 |
+
super().__init__()
|
| 163 |
+
self.device = device if device else torch.device("cpu")
|
| 164 |
+
self.max_obj = torch.tensor([max_obj]).to(device)
|
| 165 |
+
self.iou_threshold = torch.tensor([iou_thres]).to(device)
|
| 166 |
+
self.score_threshold = torch.tensor([score_thres]).to(device)
|
| 167 |
+
self.max_wh = max_wh # if max_wh != 0 : non-agnostic else : agnostic
|
| 168 |
+
self.convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]],
|
| 169 |
+
dtype=torch.float32,
|
| 170 |
+
device=self.device)
|
| 171 |
+
|
| 172 |
+
def forward(self, x):
|
| 173 |
+
boxes = x[:, :, :4]
|
| 174 |
+
conf = x[:, :, 4:5]
|
| 175 |
+
scores = x[:, :, 5:]
|
| 176 |
+
scores *= conf
|
| 177 |
+
boxes @= self.convert_matrix
|
| 178 |
+
max_score, category_id = scores.max(2, keepdim=True)
|
| 179 |
+
dis = category_id.float() * self.max_wh
|
| 180 |
+
nmsbox = boxes + dis
|
| 181 |
+
max_score_tp = max_score.transpose(1, 2).contiguous()
|
| 182 |
+
selected_indices = ORT_NMS.apply(nmsbox, max_score_tp, self.max_obj, self.iou_threshold, self.score_threshold)
|
| 183 |
+
X, Y = selected_indices[:, 0], selected_indices[:, 2]
|
| 184 |
+
selected_boxes = boxes[X, Y, :]
|
| 185 |
+
selected_categories = category_id[X, Y, :].float()
|
| 186 |
+
selected_scores = max_score[X, Y, :]
|
| 187 |
+
X = X.unsqueeze(1).float()
|
| 188 |
+
return torch.cat([X, selected_boxes, selected_categories, selected_scores], 1)
|
| 189 |
+
|
| 190 |
+
class ONNX_TRT(nn.Module):
|
| 191 |
+
'''onnx module with TensorRT NMS operation.'''
|
| 192 |
+
def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None ,device=None):
|
| 193 |
+
super().__init__()
|
| 194 |
+
assert max_wh is None
|
| 195 |
+
self.device = device if device else torch.device('cpu')
|
| 196 |
+
self.background_class = -1,
|
| 197 |
+
self.box_coding = 1,
|
| 198 |
+
self.iou_threshold = iou_thres
|
| 199 |
+
self.max_obj = max_obj
|
| 200 |
+
self.plugin_version = '1'
|
| 201 |
+
self.score_activation = 0
|
| 202 |
+
self.score_threshold = score_thres
|
| 203 |
+
|
| 204 |
+
def forward(self, x):
|
| 205 |
+
boxes = x[:, :, :4]
|
| 206 |
+
conf = x[:, :, 4:5]
|
| 207 |
+
scores = x[:, :, 5:]
|
| 208 |
+
scores *= conf
|
| 209 |
+
num_det, det_boxes, det_scores, det_classes = TRT_NMS.apply(boxes, scores, self.background_class, self.box_coding,
|
| 210 |
+
self.iou_threshold, self.max_obj,
|
| 211 |
+
self.plugin_version, self.score_activation,
|
| 212 |
+
self.score_threshold)
|
| 213 |
+
return num_det, det_boxes, det_scores, det_classes
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
class End2End(nn.Module):
|
| 217 |
+
'''export onnx or tensorrt model with NMS operation.'''
|
| 218 |
+
def __init__(self, model, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None, device=None):
|
| 219 |
+
super().__init__()
|
| 220 |
+
device = device if device else torch.device('cpu')
|
| 221 |
+
assert isinstance(max_wh,(int)) or max_wh is None
|
| 222 |
+
self.model = model.to(device)
|
| 223 |
+
self.model.model[-1].end2end = True
|
| 224 |
+
self.patch_model = ONNX_TRT if max_wh is None else ONNX_ORT
|
| 225 |
+
self.end2end = self.patch_model(max_obj, iou_thres, score_thres, max_wh, device)
|
| 226 |
+
self.end2end.eval()
|
| 227 |
+
|
| 228 |
+
def forward(self, x):
|
| 229 |
+
x = self.model(x)
|
| 230 |
+
x = self.end2end(x)
|
| 231 |
+
return x
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def attempt_load(weights, map_location=None):
|
| 238 |
+
# Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
|
| 239 |
+
model = Ensemble()
|
| 240 |
+
for w in weights if isinstance(weights, list) else [weights]:
|
| 241 |
+
attempt_download(w)
|
| 242 |
+
ckpt = torch.load(w, map_location=map_location) # load
|
| 243 |
+
model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model
|
| 244 |
+
|
| 245 |
+
# Compatibility updates
|
| 246 |
+
for m in model.modules():
|
| 247 |
+
if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]:
|
| 248 |
+
m.inplace = True # pytorch 1.7.0 compatibility
|
| 249 |
+
elif type(m) is nn.Upsample:
|
| 250 |
+
m.recompute_scale_factor = None # torch 1.11.0 compatibility
|
| 251 |
+
elif type(m) is Conv:
|
| 252 |
+
m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility
|
| 253 |
+
|
| 254 |
+
if len(model) == 1:
|
| 255 |
+
return model[-1] # return model
|
| 256 |
+
else:
|
| 257 |
+
print('Ensemble created with %s\n' % weights)
|
| 258 |
+
for k in ['names', 'stride']:
|
| 259 |
+
setattr(model, k, getattr(model[-1], k))
|
| 260 |
+
return model # return ensemble
|
| 261 |
+
|
| 262 |
+
|