You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

660 lines
31KB

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. """
  3. Common modules
  4. """
  5. import json
  6. import math
  7. import platform
  8. import warnings
  9. from collections import OrderedDict, namedtuple
  10. from copy import copy
  11. from pathlib import Path
  12. import cv2
  13. import numpy as np
  14. import pandas as pd
  15. import requests
  16. import torch
  17. import torch.nn as nn
  18. import yaml
  19. from PIL import Image
  20. from torch.cuda import amp
  21. from utils.datasets import exif_transpose, letterbox
  22. from utils.general import (LOGGER, check_requirements, check_suffix, check_version, colorstr, increment_path,
  23. make_divisible, non_max_suppression, scale_coords, xywh2xyxy, xyxy2xywh)
  24. from utils.plots import Annotator, colors, save_one_box
  25. from utils.torch_utils import copy_attr, time_sync
  26. def autopad(k, p=None): # kernel, padding
  27. # Pad to 'same'
  28. if p is None:
  29. p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
  30. return p
  31. class Conv(nn.Module):
  32. # Standard convolution
  33. def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
  34. super().__init__()
  35. self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
  36. self.bn = nn.BatchNorm2d(c2)
  37. self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
  38. def forward(self, x):
  39. return self.act(self.bn(self.conv(x)))
  40. def forward_fuse(self, x):
  41. return self.act(self.conv(x))
  42. class DWConv(Conv):
  43. # Depth-wise convolution class
  44. def __init__(self, c1, c2, k=1, s=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
  45. super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
  46. class TransformerLayer(nn.Module):
  47. # Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance)
  48. def __init__(self, c, num_heads):
  49. super().__init__()
  50. self.q = nn.Linear(c, c, bias=False)
  51. self.k = nn.Linear(c, c, bias=False)
  52. self.v = nn.Linear(c, c, bias=False)
  53. self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads)
  54. self.fc1 = nn.Linear(c, c, bias=False)
  55. self.fc2 = nn.Linear(c, c, bias=False)
  56. def forward(self, x):
  57. x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x
  58. x = self.fc2(self.fc1(x)) + x
  59. return x
  60. class TransformerBlock(nn.Module):
  61. # Vision Transformer https://arxiv.org/abs/2010.11929
  62. def __init__(self, c1, c2, num_heads, num_layers):
  63. super().__init__()
  64. self.conv = None
  65. if c1 != c2:
  66. self.conv = Conv(c1, c2)
  67. self.linear = nn.Linear(c2, c2) # learnable position embedding
  68. self.tr = nn.Sequential(*(TransformerLayer(c2, num_heads) for _ in range(num_layers)))
  69. self.c2 = c2
  70. def forward(self, x):
  71. if self.conv is not None:
  72. x = self.conv(x)
  73. b, _, w, h = x.shape
  74. p = x.flatten(2).permute(2, 0, 1)
  75. return self.tr(p + self.linear(p)).permute(1, 2, 0).reshape(b, self.c2, w, h)
  76. class Bottleneck(nn.Module):
  77. # Standard bottleneck
  78. def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
  79. super().__init__()
  80. c_ = int(c2 * e) # hidden channels
  81. self.cv1 = Conv(c1, c_, 1, 1)
  82. self.cv2 = Conv(c_, c2, 3, 1, g=g)
  83. self.add = shortcut and c1 == c2
  84. def forward(self, x):
  85. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  86. class BottleneckCSP(nn.Module):
  87. # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
  88. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  89. super().__init__()
  90. c_ = int(c2 * e) # hidden channels
  91. self.cv1 = Conv(c1, c_, 1, 1)
  92. self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
  93. self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
  94. self.cv4 = Conv(2 * c_, c2, 1, 1)
  95. self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
  96. self.act = nn.SiLU()
  97. self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
  98. def forward(self, x):
  99. y1 = self.cv3(self.m(self.cv1(x)))
  100. y2 = self.cv2(x)
  101. return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
  102. class C3(nn.Module):
  103. # CSP Bottleneck with 3 convolutions
  104. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  105. super().__init__()
  106. c_ = int(c2 * e) # hidden channels
  107. self.cv1 = Conv(c1, c_, 1, 1)
  108. self.cv2 = Conv(c1, c_, 1, 1)
  109. self.cv3 = Conv(2 * c_, c2, 1) # act=FReLU(c2)
  110. self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
  111. # self.m = nn.Sequential(*[CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)])
  112. def forward(self, x):
  113. return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))
  114. class C3TR(C3):
  115. # C3 module with TransformerBlock()
  116. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
  117. super().__init__(c1, c2, n, shortcut, g, e)
  118. c_ = int(c2 * e)
  119. self.m = TransformerBlock(c_, c_, 4, n)
  120. class C3SPP(C3):
  121. # C3 module with SPP()
  122. def __init__(self, c1, c2, k=(5, 9, 13), n=1, shortcut=True, g=1, e=0.5):
  123. super().__init__(c1, c2, n, shortcut, g, e)
  124. c_ = int(c2 * e)
  125. self.m = SPP(c_, c_, k)
  126. class C3Ghost(C3):
  127. # C3 module with GhostBottleneck()
  128. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
  129. super().__init__(c1, c2, n, shortcut, g, e)
  130. c_ = int(c2 * e) # hidden channels
  131. self.m = nn.Sequential(*(GhostBottleneck(c_, c_) for _ in range(n)))
  132. class SPP(nn.Module):
  133. # Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729
  134. def __init__(self, c1, c2, k=(5, 9, 13)):
  135. super().__init__()
  136. c_ = c1 // 2 # hidden channels
  137. self.cv1 = Conv(c1, c_, 1, 1)
  138. self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
  139. self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
  140. def forward(self, x):
  141. x = self.cv1(x)
  142. with warnings.catch_warnings():
  143. warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
  144. return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
  145. class SPPF(nn.Module):
  146. # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
  147. def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13))
  148. super().__init__()
  149. c_ = c1 // 2 # hidden channels
  150. self.cv1 = Conv(c1, c_, 1, 1)
  151. self.cv2 = Conv(c_ * 4, c2, 1, 1)
  152. self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
  153. def forward(self, x):
  154. x = self.cv1(x)
  155. with warnings.catch_warnings():
  156. warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
  157. y1 = self.m(x)
  158. y2 = self.m(y1)
  159. return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))
  160. class Focus(nn.Module):
  161. # Focus wh information into c-space
  162. def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
  163. super().__init__()
  164. self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
  165. # self.contract = Contract(gain=2)
  166. def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
  167. return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
  168. # return self.conv(self.contract(x))
  169. class GhostConv(nn.Module):
  170. # Ghost Convolution https://github.com/huawei-noah/ghostnet
  171. def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
  172. super().__init__()
  173. c_ = c2 // 2 # hidden channels
  174. self.cv1 = Conv(c1, c_, k, s, None, g, act)
  175. self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)
  176. def forward(self, x):
  177. y = self.cv1(x)
  178. return torch.cat([y, self.cv2(y)], 1)
  179. class GhostBottleneck(nn.Module):
  180. # Ghost Bottleneck https://github.com/huawei-noah/ghostnet
  181. def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride
  182. super().__init__()
  183. c_ = c2 // 2
  184. self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw
  185. DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw
  186. GhostConv(c_, c2, 1, 1, act=False)) # pw-linear
  187. self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False),
  188. Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity()
  189. def forward(self, x):
  190. return self.conv(x) + self.shortcut(x)
  191. class Contract(nn.Module):
  192. # Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40)
  193. def __init__(self, gain=2):
  194. super().__init__()
  195. self.gain = gain
  196. def forward(self, x):
  197. b, c, h, w = x.size() # assert (h / s == 0) and (W / s == 0), 'Indivisible gain'
  198. s = self.gain
  199. x = x.view(b, c, h // s, s, w // s, s) # x(1,64,40,2,40,2)
  200. x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40)
  201. return x.view(b, c * s * s, h // s, w // s) # x(1,256,40,40)
  202. class Expand(nn.Module):
  203. # Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160)
  204. def __init__(self, gain=2):
  205. super().__init__()
  206. self.gain = gain
  207. def forward(self, x):
  208. b, c, h, w = x.size() # assert C / s ** 2 == 0, 'Indivisible gain'
  209. s = self.gain
  210. x = x.view(b, s, s, c // s ** 2, h, w) # x(1,2,2,16,80,80)
  211. x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2)
  212. return x.view(b, c // s ** 2, h * s, w * s) # x(1,16,160,160)
  213. class Concat(nn.Module):
  214. # Concatenate a list of tensors along dimension
  215. def __init__(self, dimension=1):
  216. super().__init__()
  217. self.d = dimension
  218. def forward(self, x):
  219. return torch.cat(x, self.d)
  220. class DetectMultiBackend(nn.Module):
  221. # YOLOv5 MultiBackend class for python inference on various backends
  222. def __init__(self, weights='yolov5s.pt', device=None, dnn=False, data=None):
  223. # Usage:
  224. # PyTorch: weights = *.pt
  225. # TorchScript: *.torchscript
  226. # CoreML: *.mlmodel
  227. # OpenVINO: *.xml
  228. # TensorFlow: *_saved_model
  229. # TensorFlow: *.pb
  230. # TensorFlow Lite: *.tflite
  231. # TensorFlow Edge TPU: *_edgetpu.tflite
  232. # ONNX Runtime: *.onnx
  233. # OpenCV DNN: *.onnx with dnn=True
  234. # TensorRT: *.engine
  235. from models.experimental import attempt_download, attempt_load # scoped to avoid circular import
  236. super().__init__()
  237. w = str(weights[0] if isinstance(weights, list) else weights)
  238. suffix = Path(w).suffix.lower()
  239. suffixes = ['.pt', '.torchscript', '.onnx', '.engine', '.tflite', '.pb', '', '.mlmodel', '.xml']
  240. check_suffix(w, suffixes) # check weights have acceptable suffix
  241. pt, jit, onnx, engine, tflite, pb, saved_model, coreml, xml = (suffix == x for x in suffixes) # backends
  242. stride, names = 64, [f'class{i}' for i in range(1000)] # assign defaults
  243. w = attempt_download(w) # download if not local
  244. if data: # data.yaml path (optional)
  245. with open(data, errors='ignore') as f:
  246. names = yaml.safe_load(f)['names'] # class names
  247. if pt: # PyTorch
  248. model = attempt_load(weights if isinstance(weights, list) else w, map_location=device)
  249. stride = int(model.stride.max()) # model stride
  250. names = model.module.names if hasattr(model, 'module') else model.names # get class names
  251. self.model = model # explicitly assign for to(), cpu(), cuda(), half()
  252. elif jit: # TorchScript
  253. LOGGER.info(f'Loading {w} for TorchScript inference...')
  254. extra_files = {'config.txt': ''} # model metadata
  255. model = torch.jit.load(w, _extra_files=extra_files)
  256. if extra_files['config.txt']:
  257. d = json.loads(extra_files['config.txt']) # extra_files dict
  258. stride, names = int(d['stride']), d['names']
  259. elif dnn: # ONNX OpenCV DNN
  260. LOGGER.info(f'Loading {w} for ONNX OpenCV DNN inference...')
  261. check_requirements(('opencv-python>=4.5.4',))
  262. net = cv2.dnn.readNetFromONNX(w)
  263. elif onnx: # ONNX Runtime
  264. LOGGER.info(f'Loading {w} for ONNX Runtime inference...')
  265. cuda = torch.cuda.is_available()
  266. check_requirements(('onnx', 'onnxruntime-gpu' if cuda else 'onnxruntime'))
  267. import onnxruntime
  268. providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']
  269. session = onnxruntime.InferenceSession(w, providers=providers)
  270. elif xml: # OpenVINO
  271. LOGGER.info(f'Loading {w} for OpenVINO inference...')
  272. check_requirements(('openvino-dev',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/
  273. import openvino.inference_engine as ie
  274. core = ie.IECore()
  275. network = core.read_network(model=w, weights=Path(w).with_suffix('.bin')) # *.xml, *.bin paths
  276. executable_network = core.load_network(network, device_name='CPU', num_requests=1)
  277. elif engine: # TensorRT
  278. LOGGER.info(f'Loading {w} for TensorRT inference...')
  279. import tensorrt as trt # https://developer.nvidia.com/nvidia-tensorrt-download
  280. check_version(trt.__version__, '8.0.0', verbose=True) # version requirement
  281. Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr'))
  282. logger = trt.Logger(trt.Logger.INFO)
  283. with open(w, 'rb') as f, trt.Runtime(logger) as runtime:
  284. model = runtime.deserialize_cuda_engine(f.read())
  285. bindings = OrderedDict()
  286. for index in range(model.num_bindings):
  287. name = model.get_binding_name(index)
  288. dtype = trt.nptype(model.get_binding_dtype(index))
  289. shape = tuple(model.get_binding_shape(index))
  290. data = torch.from_numpy(np.empty(shape, dtype=np.dtype(dtype))).to(device)
  291. bindings[name] = Binding(name, dtype, shape, data, int(data.data_ptr()))
  292. binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items())
  293. context = model.create_execution_context()
  294. batch_size = bindings['images'].shape[0]
  295. elif coreml: # CoreML
  296. LOGGER.info(f'Loading {w} for CoreML inference...')
  297. import coremltools as ct
  298. model = ct.models.MLModel(w)
  299. else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)
  300. if saved_model: # SavedModel
  301. LOGGER.info(f'Loading {w} for TensorFlow SavedModel inference...')
  302. import tensorflow as tf
  303. model = tf.keras.models.load_model(w)
  304. elif pb: # GraphDef https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt
  305. LOGGER.info(f'Loading {w} for TensorFlow GraphDef inference...')
  306. import tensorflow as tf
  307. def wrap_frozen_graph(gd, inputs, outputs):
  308. x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped
  309. return x.prune(tf.nest.map_structure(x.graph.as_graph_element, inputs),
  310. tf.nest.map_structure(x.graph.as_graph_element, outputs))
  311. graph_def = tf.Graph().as_graph_def()
  312. graph_def.ParseFromString(open(w, 'rb').read())
  313. frozen_func = wrap_frozen_graph(gd=graph_def, inputs="x:0", outputs="Identity:0")
  314. elif tflite: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
  315. if 'edgetpu' in w.lower(): # Edge TPU
  316. LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...')
  317. import tflite_runtime.interpreter as tfli
  318. delegate = {'Linux': 'libedgetpu.so.1', # install https://coral.ai/software/#edgetpu-runtime
  319. 'Darwin': 'libedgetpu.1.dylib',
  320. 'Windows': 'edgetpu.dll'}[platform.system()]
  321. interpreter = tfli.Interpreter(model_path=w, experimental_delegates=[tfli.load_delegate(delegate)])
  322. else: # Lite
  323. LOGGER.info(f'Loading {w} for TensorFlow Lite inference...')
  324. import tensorflow as tf
  325. interpreter = tf.lite.Interpreter(model_path=w) # load TFLite model
  326. interpreter.allocate_tensors() # allocate
  327. input_details = interpreter.get_input_details() # inputs
  328. output_details = interpreter.get_output_details() # outputs
  329. self.__dict__.update(locals()) # assign all variables to self
  330. def forward(self, im, augment=False, visualize=False, val=False):
  331. # YOLOv5 MultiBackend inference
  332. b, ch, h, w = im.shape # batch, channel, height, width
  333. if self.pt or self.jit: # PyTorch
  334. y = self.model(im) if self.jit else self.model(im, augment=augment, visualize=visualize)
  335. return y if val else y[0]
  336. elif self.dnn: # ONNX OpenCV DNN
  337. im = im.cpu().numpy() # torch to numpy
  338. self.net.setInput(im)
  339. y = self.net.forward()
  340. elif self.onnx: # ONNX Runtime
  341. im = im.cpu().numpy() # torch to numpy
  342. y = self.session.run([self.session.get_outputs()[0].name], {self.session.get_inputs()[0].name: im})[0]
  343. elif self.xml: # OpenVINO
  344. im = im.cpu().numpy() # FP32
  345. desc = self.ie.TensorDesc(precision='FP32', dims=im.shape, layout='NCHW') # Tensor Description
  346. request = self.executable_network.requests[0] # inference request
  347. request.set_blob(blob_name='images', blob=self.ie.Blob(desc, im)) # name=next(iter(request.input_blobs))
  348. request.infer()
  349. y = request.output_blobs['output'].buffer # name=next(iter(request.output_blobs))
  350. elif self.engine: # TensorRT
  351. assert im.shape == self.bindings['images'].shape, (im.shape, self.bindings['images'].shape)
  352. self.binding_addrs['images'] = int(im.data_ptr())
  353. self.context.execute_v2(list(self.binding_addrs.values()))
  354. y = self.bindings['output'].data
  355. elif self.coreml: # CoreML
  356. im = im.permute(0, 2, 3, 1).cpu().numpy() # torch BCHW to numpy BHWC shape(1,320,192,3)
  357. im = Image.fromarray((im[0] * 255).astype('uint8'))
  358. # im = im.resize((192, 320), Image.ANTIALIAS)
  359. y = self.model.predict({'image': im}) # coordinates are xywh normalized
  360. box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels
  361. conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float)
  362. y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1)
  363. else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)
  364. im = im.permute(0, 2, 3, 1).cpu().numpy() # torch BCHW to numpy BHWC shape(1,320,192,3)
  365. if self.saved_model: # SavedModel
  366. y = self.model(im, training=False).numpy()
  367. elif self.pb: # GraphDef
  368. y = self.frozen_func(x=self.tf.constant(im)).numpy()
  369. elif self.tflite: # Lite
  370. input, output = self.input_details[0], self.output_details[0]
  371. int8 = input['dtype'] == np.uint8 # is TFLite quantized uint8 model
  372. if int8:
  373. scale, zero_point = input['quantization']
  374. im = (im / scale + zero_point).astype(np.uint8) # de-scale
  375. self.interpreter.set_tensor(input['index'], im)
  376. self.interpreter.invoke()
  377. y = self.interpreter.get_tensor(output['index'])
  378. if int8:
  379. scale, zero_point = output['quantization']
  380. y = (y.astype(np.float32) - zero_point) * scale # re-scale
  381. y[..., 0] *= w # x
  382. y[..., 1] *= h # y
  383. y[..., 2] *= w # w
  384. y[..., 3] *= h # h
  385. y = torch.tensor(y) if isinstance(y, np.ndarray) else y
  386. return (y, []) if val else y
  387. def warmup(self, imgsz=(1, 3, 640, 640), half=False):
  388. # Warmup model by running inference once
  389. if self.pt or self.jit or self.onnx or self.engine: # warmup types
  390. if isinstance(self.device, torch.device) and self.device.type != 'cpu': # only warmup GPU models
  391. im = torch.zeros(*imgsz).to(self.device).type(torch.half if half else torch.float) # input image
  392. self.forward(im) # warmup
  393. class AutoShape(nn.Module):
  394. # YOLOv5 input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS
  395. conf = 0.25 # NMS confidence threshold
  396. iou = 0.45 # NMS IoU threshold
  397. agnostic = False # NMS class-agnostic
  398. multi_label = False # NMS multiple labels per box
  399. classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for COCO persons, cats and dogs
  400. max_det = 1000 # maximum number of detections per image
  401. amp = False # Automatic Mixed Precision (AMP) inference
  402. def __init__(self, model):
  403. super().__init__()
  404. LOGGER.info('Adding AutoShape... ')
  405. copy_attr(self, model, include=('yaml', 'nc', 'hyp', 'names', 'stride', 'abc'), exclude=()) # copy attributes
  406. self.dmb = isinstance(model, DetectMultiBackend) # DetectMultiBackend() instance
  407. self.pt = not self.dmb or model.pt # PyTorch model
  408. self.model = model.eval()
  409. def _apply(self, fn):
  410. # Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers
  411. self = super()._apply(fn)
  412. if self.pt:
  413. m = self.model.model.model[-1] if self.dmb else self.model.model[-1] # Detect()
  414. m.stride = fn(m.stride)
  415. m.grid = list(map(fn, m.grid))
  416. if isinstance(m.anchor_grid, list):
  417. m.anchor_grid = list(map(fn, m.anchor_grid))
  418. return self
  419. @torch.no_grad()
  420. def forward(self, imgs, size=640, augment=False, profile=False):
  421. # Inference from various sources. For height=640, width=1280, RGB images example inputs are:
  422. # file: imgs = 'data/images/zidane.jpg' # str or PosixPath
  423. # URI: = 'https://ultralytics.com/images/zidane.jpg'
  424. # OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3)
  425. # PIL: = Image.open('image.jpg') or ImageGrab.grab() # HWC x(640,1280,3)
  426. # numpy: = np.zeros((640,1280,3)) # HWC
  427. # torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values)
  428. # multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images
  429. t = [time_sync()]
  430. p = next(self.model.parameters()) if self.pt else torch.zeros(1) # for device and type
  431. autocast = self.amp and (p.device.type != 'cpu') # Automatic Mixed Precision (AMP) inference
  432. if isinstance(imgs, torch.Tensor): # torch
  433. with amp.autocast(enabled=autocast):
  434. return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference
  435. # Pre-process
  436. n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images
  437. shape0, shape1, files = [], [], [] # image and inference shapes, filenames
  438. for i, im in enumerate(imgs):
  439. f = f'image{i}' # filename
  440. if isinstance(im, (str, Path)): # filename or uri
  441. im, f = Image.open(requests.get(im, stream=True).raw if str(im).startswith('http') else im), im
  442. im = np.asarray(exif_transpose(im))
  443. elif isinstance(im, Image.Image): # PIL Image
  444. im, f = np.asarray(exif_transpose(im)), getattr(im, 'filename', f) or f
  445. files.append(Path(f).with_suffix('.jpg').name)
  446. if im.shape[0] < 5: # image in CHW
  447. im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
  448. im = im[..., :3] if im.ndim == 3 else np.tile(im[..., None], 3) # enforce 3ch input
  449. s = im.shape[:2] # HWC
  450. shape0.append(s) # image shape
  451. g = (size / max(s)) # gain
  452. shape1.append([y * g for y in s])
  453. imgs[i] = im if im.data.contiguous else np.ascontiguousarray(im) # update
  454. shape1 = [make_divisible(x, self.stride) for x in np.stack(shape1, 0).max(0)] # inference shape
  455. x = [letterbox(im, new_shape=shape1 if self.pt else size, auto=False)[0] for im in imgs] # pad
  456. x = np.stack(x, 0) if n > 1 else x[0][None] # stack
  457. x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW
  458. x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32
  459. t.append(time_sync())
  460. with amp.autocast(enabled=autocast):
  461. # Inference
  462. y = self.model(x, augment, profile) # forward
  463. t.append(time_sync())
  464. # Post-process
  465. y = non_max_suppression(y if self.dmb else y[0], self.conf, iou_thres=self.iou, classes=self.classes,
  466. agnostic=self.agnostic, multi_label=self.multi_label, max_det=self.max_det) # NMS
  467. for i in range(n):
  468. scale_coords(shape1, y[i][:, :4], shape0[i])
  469. t.append(time_sync())
  470. return Detections(imgs, y, files, t, self.names, x.shape)
  471. class Detections:
  472. # YOLOv5 detections class for inference results
  473. def __init__(self, imgs, pred, files, times=(0, 0, 0, 0), names=None, shape=None):
  474. super().__init__()
  475. d = pred[0].device # device
  476. gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device=d) for im in imgs] # normalizations
  477. self.imgs = imgs # list of images as numpy arrays
  478. self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
  479. self.names = names # class names
  480. self.files = files # image filenames
  481. self.times = times # profiling times
  482. self.xyxy = pred # xyxy pixels
  483. self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
  484. self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized
  485. self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized
  486. self.n = len(self.pred) # number of images (batch size)
  487. self.t = tuple((times[i + 1] - times[i]) * 1000 / self.n for i in range(3)) # timestamps (ms)
  488. self.s = shape # inference BCHW shape
  489. def display(self, pprint=False, show=False, save=False, crop=False, render=False, save_dir=Path('')):
  490. crops = []
  491. for i, (im, pred) in enumerate(zip(self.imgs, self.pred)):
  492. s = f'image {i + 1}/{len(self.pred)}: {im.shape[0]}x{im.shape[1]} ' # string
  493. if pred.shape[0]:
  494. for c in pred[:, -1].unique():
  495. n = (pred[:, -1] == c).sum() # detections per class
  496. s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
  497. if show or save or render or crop:
  498. annotator = Annotator(im, example=str(self.names))
  499. for *box, conf, cls in reversed(pred): # xyxy, confidence, class
  500. label = f'{self.names[int(cls)]} {conf:.2f}'
  501. if crop:
  502. file = save_dir / 'crops' / self.names[int(cls)] / self.files[i] if save else None
  503. crops.append({'box': box, 'conf': conf, 'cls': cls, 'label': label,
  504. 'im': save_one_box(box, im, file=file, save=save)})
  505. else: # all others
  506. annotator.box_label(box, label, color=colors(cls))
  507. im = annotator.im
  508. else:
  509. s += '(no detections)'
  510. im = Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im # from np
  511. if pprint:
  512. LOGGER.info(s.rstrip(', '))
  513. if show:
  514. im.show(self.files[i]) # show
  515. if save:
  516. f = self.files[i]
  517. im.save(save_dir / f) # save
  518. if i == self.n - 1:
  519. LOGGER.info(f"Saved {self.n} image{'s' * (self.n > 1)} to {colorstr('bold', save_dir)}")
  520. if render:
  521. self.imgs[i] = np.asarray(im)
  522. if crop:
  523. if save:
  524. LOGGER.info(f'Saved results to {save_dir}\n')
  525. return crops
  526. def print(self):
  527. self.display(pprint=True) # print results
  528. LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {tuple(self.s)}' %
  529. self.t)
  530. def show(self):
  531. self.display(show=True) # show results
  532. def save(self, save_dir='runs/detect/exp'):
  533. save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/detect/exp', mkdir=True) # increment save_dir
  534. self.display(save=True, save_dir=save_dir) # save results
  535. def crop(self, save=True, save_dir='runs/detect/exp'):
  536. save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/detect/exp', mkdir=True) if save else None
  537. return self.display(crop=True, save=save, save_dir=save_dir) # crop results
  538. def render(self):
  539. self.display(render=True) # render results
  540. return self.imgs
  541. def pandas(self):
  542. # return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0])
  543. new = copy(self) # return copy
  544. ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns
  545. cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns
  546. for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]):
  547. a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update
  548. setattr(new, k, [pd.DataFrame(x, columns=c) for x in a])
  549. return new
  550. def tolist(self):
  551. # return a list of Detections objects, i.e. 'for result in results.tolist():'
  552. r = range(self.n) # iterable
  553. x = [Detections([self.imgs[i]], [self.pred[i]], [self.files[i]], self.times, self.names, self.s) for i in r]
  554. # for d in x:
  555. # for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']:
  556. # setattr(d, k, getattr(d, k)[0]) # pop out of list
  557. return x
  558. def __len__(self):
  559. return self.n
  560. class Classify(nn.Module):
  561. # Classification head, i.e. x(b,c1,20,20) to x(b,c2)
  562. def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups
  563. super().__init__()
  564. self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1)
  565. self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g) # to x(b,c2,1,1)
  566. self.flat = nn.Flatten()
  567. def forward(self, x):
  568. z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list
  569. return self.flat(self.conv(z)) # flatten to x(b,c2)