bplusplus 0.1.1__py3-none-any.whl → 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of bplusplus might be problematic. Click here for more details.

Files changed (95) hide show
  1. bplusplus/__init__.py +5 -3
  2. bplusplus/{collect_images.py → collect.py} +3 -3
  3. bplusplus/prepare.py +573 -0
  4. bplusplus/train_validate.py +8 -64
  5. bplusplus/yolov5detect/__init__.py +1 -0
  6. bplusplus/yolov5detect/detect.py +444 -0
  7. bplusplus/yolov5detect/export.py +1530 -0
  8. bplusplus/yolov5detect/insect.yaml +8 -0
  9. bplusplus/yolov5detect/models/__init__.py +0 -0
  10. bplusplus/yolov5detect/models/common.py +1109 -0
  11. bplusplus/yolov5detect/models/experimental.py +130 -0
  12. bplusplus/yolov5detect/models/hub/anchors.yaml +56 -0
  13. bplusplus/yolov5detect/models/hub/yolov3-spp.yaml +52 -0
  14. bplusplus/yolov5detect/models/hub/yolov3-tiny.yaml +42 -0
  15. bplusplus/yolov5detect/models/hub/yolov3.yaml +52 -0
  16. bplusplus/yolov5detect/models/hub/yolov5-bifpn.yaml +49 -0
  17. bplusplus/yolov5detect/models/hub/yolov5-fpn.yaml +43 -0
  18. bplusplus/yolov5detect/models/hub/yolov5-p2.yaml +55 -0
  19. bplusplus/yolov5detect/models/hub/yolov5-p34.yaml +42 -0
  20. bplusplus/yolov5detect/models/hub/yolov5-p6.yaml +57 -0
  21. bplusplus/yolov5detect/models/hub/yolov5-p7.yaml +68 -0
  22. bplusplus/yolov5detect/models/hub/yolov5-panet.yaml +49 -0
  23. bplusplus/yolov5detect/models/hub/yolov5l6.yaml +61 -0
  24. bplusplus/yolov5detect/models/hub/yolov5m6.yaml +61 -0
  25. bplusplus/yolov5detect/models/hub/yolov5n6.yaml +61 -0
  26. bplusplus/yolov5detect/models/hub/yolov5s-LeakyReLU.yaml +50 -0
  27. bplusplus/yolov5detect/models/hub/yolov5s-ghost.yaml +49 -0
  28. bplusplus/yolov5detect/models/hub/yolov5s-transformer.yaml +49 -0
  29. bplusplus/yolov5detect/models/hub/yolov5s6.yaml +61 -0
  30. bplusplus/yolov5detect/models/hub/yolov5x6.yaml +61 -0
  31. bplusplus/yolov5detect/models/segment/yolov5l-seg.yaml +49 -0
  32. bplusplus/yolov5detect/models/segment/yolov5m-seg.yaml +49 -0
  33. bplusplus/yolov5detect/models/segment/yolov5n-seg.yaml +49 -0
  34. bplusplus/yolov5detect/models/segment/yolov5s-seg.yaml +49 -0
  35. bplusplus/yolov5detect/models/segment/yolov5x-seg.yaml +49 -0
  36. bplusplus/yolov5detect/models/tf.py +797 -0
  37. bplusplus/yolov5detect/models/yolo.py +495 -0
  38. bplusplus/yolov5detect/models/yolov5l.yaml +49 -0
  39. bplusplus/yolov5detect/models/yolov5m.yaml +49 -0
  40. bplusplus/yolov5detect/models/yolov5n.yaml +49 -0
  41. bplusplus/yolov5detect/models/yolov5s.yaml +49 -0
  42. bplusplus/yolov5detect/models/yolov5x.yaml +49 -0
  43. bplusplus/yolov5detect/utils/__init__.py +97 -0
  44. bplusplus/yolov5detect/utils/activations.py +134 -0
  45. bplusplus/yolov5detect/utils/augmentations.py +448 -0
  46. bplusplus/yolov5detect/utils/autoanchor.py +175 -0
  47. bplusplus/yolov5detect/utils/autobatch.py +70 -0
  48. bplusplus/yolov5detect/utils/aws/__init__.py +0 -0
  49. bplusplus/yolov5detect/utils/aws/mime.sh +26 -0
  50. bplusplus/yolov5detect/utils/aws/resume.py +41 -0
  51. bplusplus/yolov5detect/utils/aws/userdata.sh +27 -0
  52. bplusplus/yolov5detect/utils/callbacks.py +72 -0
  53. bplusplus/yolov5detect/utils/dataloaders.py +1385 -0
  54. bplusplus/yolov5detect/utils/docker/Dockerfile +73 -0
  55. bplusplus/yolov5detect/utils/docker/Dockerfile-arm64 +40 -0
  56. bplusplus/yolov5detect/utils/docker/Dockerfile-cpu +42 -0
  57. bplusplus/yolov5detect/utils/downloads.py +136 -0
  58. bplusplus/yolov5detect/utils/flask_rest_api/README.md +70 -0
  59. bplusplus/yolov5detect/utils/flask_rest_api/example_request.py +17 -0
  60. bplusplus/yolov5detect/utils/flask_rest_api/restapi.py +49 -0
  61. bplusplus/yolov5detect/utils/general.py +1294 -0
  62. bplusplus/yolov5detect/utils/google_app_engine/Dockerfile +25 -0
  63. bplusplus/yolov5detect/utils/google_app_engine/additional_requirements.txt +6 -0
  64. bplusplus/yolov5detect/utils/google_app_engine/app.yaml +16 -0
  65. bplusplus/yolov5detect/utils/loggers/__init__.py +476 -0
  66. bplusplus/yolov5detect/utils/loggers/clearml/README.md +222 -0
  67. bplusplus/yolov5detect/utils/loggers/clearml/__init__.py +0 -0
  68. bplusplus/yolov5detect/utils/loggers/clearml/clearml_utils.py +230 -0
  69. bplusplus/yolov5detect/utils/loggers/clearml/hpo.py +90 -0
  70. bplusplus/yolov5detect/utils/loggers/comet/README.md +250 -0
  71. bplusplus/yolov5detect/utils/loggers/comet/__init__.py +551 -0
  72. bplusplus/yolov5detect/utils/loggers/comet/comet_utils.py +151 -0
  73. bplusplus/yolov5detect/utils/loggers/comet/hpo.py +126 -0
  74. bplusplus/yolov5detect/utils/loggers/comet/optimizer_config.json +135 -0
  75. bplusplus/yolov5detect/utils/loggers/wandb/__init__.py +0 -0
  76. bplusplus/yolov5detect/utils/loggers/wandb/wandb_utils.py +210 -0
  77. bplusplus/yolov5detect/utils/loss.py +259 -0
  78. bplusplus/yolov5detect/utils/metrics.py +381 -0
  79. bplusplus/yolov5detect/utils/plots.py +517 -0
  80. bplusplus/yolov5detect/utils/segment/__init__.py +0 -0
  81. bplusplus/yolov5detect/utils/segment/augmentations.py +100 -0
  82. bplusplus/yolov5detect/utils/segment/dataloaders.py +366 -0
  83. bplusplus/yolov5detect/utils/segment/general.py +160 -0
  84. bplusplus/yolov5detect/utils/segment/loss.py +198 -0
  85. bplusplus/yolov5detect/utils/segment/metrics.py +225 -0
  86. bplusplus/yolov5detect/utils/segment/plots.py +152 -0
  87. bplusplus/yolov5detect/utils/torch_utils.py +482 -0
  88. bplusplus/yolov5detect/utils/triton.py +90 -0
  89. bplusplus-1.1.0.dist-info/METADATA +179 -0
  90. bplusplus-1.1.0.dist-info/RECORD +92 -0
  91. bplusplus/build_model.py +0 -38
  92. bplusplus-0.1.1.dist-info/METADATA +0 -97
  93. bplusplus-0.1.1.dist-info/RECORD +0 -8
  94. {bplusplus-0.1.1.dist-info → bplusplus-1.1.0.dist-info}/LICENSE +0 -0
  95. {bplusplus-0.1.1.dist-info → bplusplus-1.1.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,495 @@
1
+ # Ultralytics YOLOv5 🚀, AGPL-3.0 license
2
+ """
3
+ YOLO-specific modules.
4
+
5
+ Usage:
6
+ $ python models/yolo.py --cfg yolov5s.yaml
7
+ """
8
+
9
+ import argparse
10
+ import contextlib
11
+ import math
12
+ import os
13
+ import platform
14
+ import sys
15
+ from copy import deepcopy
16
+ from pathlib import Path
17
+
18
+ import torch
19
+ import torch.nn as nn
20
+
21
+ FILE = Path(__file__).resolve()
22
+ ROOT = FILE.parents[1] # YOLOv5 root directory
23
+ if str(ROOT) not in sys.path:
24
+ sys.path.append(str(ROOT)) # add ROOT to PATH
25
+ if platform.system() != "Windows":
26
+ ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
27
+
28
+ from models.common import (
29
+ C3,
30
+ C3SPP,
31
+ C3TR,
32
+ SPP,
33
+ SPPF,
34
+ Bottleneck,
35
+ BottleneckCSP,
36
+ C3Ghost,
37
+ C3x,
38
+ Classify,
39
+ Concat,
40
+ Contract,
41
+ Conv,
42
+ CrossConv,
43
+ DetectMultiBackend,
44
+ DWConv,
45
+ DWConvTranspose2d,
46
+ Expand,
47
+ Focus,
48
+ GhostBottleneck,
49
+ GhostConv,
50
+ Proto,
51
+ )
52
+ from models.experimental import MixConv2d
53
+ from utils.autoanchor import check_anchor_order
54
+ from utils.general import LOGGER, check_version, check_yaml, colorstr, make_divisible, print_args
55
+ from utils.plots import feature_visualization
56
+ from utils.torch_utils import (
57
+ fuse_conv_and_bn,
58
+ initialize_weights,
59
+ model_info,
60
+ profile,
61
+ scale_img,
62
+ select_device,
63
+ time_sync,
64
+ )
65
+
66
+ try:
67
+ import thop # for FLOPs computation
68
+ except ImportError:
69
+ thop = None
70
+
71
+
72
+ class Detect(nn.Module):
73
+ """YOLOv5 Detect head for processing input tensors and generating detection outputs in object detection models."""
74
+
75
+ stride = None # strides computed during build
76
+ dynamic = False # force grid reconstruction
77
+ export = False # export mode
78
+
79
+ def __init__(self, nc=80, anchors=(), ch=(), inplace=True):
80
+ """Initializes YOLOv5 detection layer with specified classes, anchors, channels, and inplace operations."""
81
+ super().__init__()
82
+ self.nc = nc # number of classes
83
+ self.no = nc + 5 # number of outputs per anchor
84
+ self.nl = len(anchors) # number of detection layers
85
+ self.na = len(anchors[0]) // 2 # number of anchors
86
+ self.grid = [torch.empty(0) for _ in range(self.nl)] # init grid
87
+ self.anchor_grid = [torch.empty(0) for _ in range(self.nl)] # init anchor grid
88
+ self.register_buffer("anchors", torch.tensor(anchors).float().view(self.nl, -1, 2)) # shape(nl,na,2)
89
+ self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
90
+ self.inplace = inplace # use inplace ops (e.g. slice assignment)
91
+
92
+ def forward(self, x):
93
+ """Processes input through YOLOv5 layers, altering shape for detection: `x(bs, 3, ny, nx, 85)`."""
94
+ z = [] # inference output
95
+ for i in range(self.nl):
96
+ x[i] = self.m[i](x[i]) # conv
97
+ bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
98
+ x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
99
+
100
+ if not self.training: # inference
101
+ if self.dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
102
+ self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
103
+
104
+ if isinstance(self, Segment): # (boxes + masks)
105
+ xy, wh, conf, mask = x[i].split((2, 2, self.nc + 1, self.no - self.nc - 5), 4)
106
+ xy = (xy.sigmoid() * 2 + self.grid[i]) * self.stride[i] # xy
107
+ wh = (wh.sigmoid() * 2) ** 2 * self.anchor_grid[i] # wh
108
+ y = torch.cat((xy, wh, conf.sigmoid(), mask), 4)
109
+ else: # Detect (boxes only)
110
+ xy, wh, conf = x[i].sigmoid().split((2, 2, self.nc + 1), 4)
111
+ xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
112
+ wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
113
+ y = torch.cat((xy, wh, conf), 4)
114
+ z.append(y.view(bs, self.na * nx * ny, self.no))
115
+
116
+ return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)
117
+
118
+ def _make_grid(self, nx=20, ny=20, i=0, torch_1_10=check_version(torch.__version__, "1.10.0")):
119
+ """Generates a mesh grid for anchor boxes with optional compatibility for torch versions < 1.10."""
120
+ d = self.anchors[i].device
121
+ t = self.anchors[i].dtype
122
+ shape = 1, self.na, ny, nx, 2 # grid shape
123
+ y, x = torch.arange(ny, device=d, dtype=t), torch.arange(nx, device=d, dtype=t)
124
+ yv, xv = torch.meshgrid(y, x, indexing="ij") if torch_1_10 else torch.meshgrid(y, x) # torch>=0.7 compatibility
125
+ grid = torch.stack((xv, yv), 2).expand(shape) - 0.5 # add grid offset, i.e. y = 2.0 * x - 0.5
126
+ anchor_grid = (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape)
127
+ return grid, anchor_grid
128
+
129
+
130
+ class Segment(Detect):
131
+ """YOLOv5 Segment head for segmentation models, extending Detect with mask and prototype layers."""
132
+
133
+ def __init__(self, nc=80, anchors=(), nm=32, npr=256, ch=(), inplace=True):
134
+ """Initializes YOLOv5 Segment head with options for mask count, protos, and channel adjustments."""
135
+ super().__init__(nc, anchors, ch, inplace)
136
+ self.nm = nm # number of masks
137
+ self.npr = npr # number of protos
138
+ self.no = 5 + nc + self.nm # number of outputs per anchor
139
+ self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
140
+ self.proto = Proto(ch[0], self.npr, self.nm) # protos
141
+ self.detect = Detect.forward
142
+
143
+ def forward(self, x):
144
+ """Processes input through the network, returning detections and prototypes; adjusts output based on
145
+ training/export mode.
146
+ """
147
+ p = self.proto(x[0])
148
+ x = self.detect(self, x)
149
+ return (x, p) if self.training else (x[0], p) if self.export else (x[0], p, x[1])
150
+
151
+
152
+ class BaseModel(nn.Module):
153
+ """YOLOv5 base model."""
154
+
155
+ def forward(self, x, profile=False, visualize=False):
156
+ """Executes a single-scale inference or training pass on the YOLOv5 base model, with options for profiling and
157
+ visualization.
158
+ """
159
+ return self._forward_once(x, profile, visualize) # single-scale inference, train
160
+
161
+ def _forward_once(self, x, profile=False, visualize=False):
162
+ """Performs a forward pass on the YOLOv5 model, enabling profiling and feature visualization options."""
163
+ y, dt = [], [] # outputs
164
+ for m in self.model:
165
+ if m.f != -1: # if not from previous layer
166
+ x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
167
+ if profile:
168
+ self._profile_one_layer(m, x, dt)
169
+ x = m(x) # run
170
+ y.append(x if m.i in self.save else None) # save output
171
+ if visualize:
172
+ feature_visualization(x, m.type, m.i, save_dir=visualize)
173
+ return x
174
+
175
+ def _profile_one_layer(self, m, x, dt):
176
+ """Profiles a single layer's performance by computing GFLOPs, execution time, and parameters."""
177
+ c = m == self.model[-1] # is final layer, copy input as inplace fix
178
+ o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1e9 * 2 if thop else 0 # FLOPs
179
+ t = time_sync()
180
+ for _ in range(10):
181
+ m(x.copy() if c else x)
182
+ dt.append((time_sync() - t) * 100)
183
+ if m == self.model[0]:
184
+ LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} module")
185
+ LOGGER.info(f"{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}")
186
+ if c:
187
+ LOGGER.info(f"{sum(dt):10.2f} {'-':>10s} {'-':>10s} Total")
188
+
189
+ def fuse(self):
190
+ """Fuses Conv2d() and BatchNorm2d() layers in the model to improve inference speed."""
191
+ LOGGER.info("Fusing layers... ")
192
+ for m in self.model.modules():
193
+ if isinstance(m, (Conv, DWConv)) and hasattr(m, "bn"):
194
+ m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
195
+ delattr(m, "bn") # remove batchnorm
196
+ m.forward = m.forward_fuse # update forward
197
+ self.info()
198
+ return self
199
+
200
+ def info(self, verbose=False, img_size=640):
201
+ """Prints model information given verbosity and image size, e.g., `info(verbose=True, img_size=640)`."""
202
+ model_info(self, verbose, img_size)
203
+
204
+ def _apply(self, fn):
205
+ """Applies transformations like to(), cpu(), cuda(), half() to model tensors excluding parameters or registered
206
+ buffers.
207
+ """
208
+ self = super()._apply(fn)
209
+ m = self.model[-1] # Detect()
210
+ if isinstance(m, (Detect, Segment)):
211
+ m.stride = fn(m.stride)
212
+ m.grid = list(map(fn, m.grid))
213
+ if isinstance(m.anchor_grid, list):
214
+ m.anchor_grid = list(map(fn, m.anchor_grid))
215
+ return self
216
+
217
+
218
+ class DetectionModel(BaseModel):
219
+ """YOLOv5 detection model class for object detection tasks, supporting custom configurations and anchors."""
220
+
221
+ def __init__(self, cfg="yolov5s.yaml", ch=3, nc=None, anchors=None):
222
+ """Initializes YOLOv5 model with configuration file, input channels, number of classes, and custom anchors."""
223
+ super().__init__()
224
+ if isinstance(cfg, dict):
225
+ self.yaml = cfg # model dict
226
+ else: # is *.yaml
227
+ import yaml # for torch hub
228
+
229
+ self.yaml_file = Path(cfg).name
230
+ with open(cfg, encoding="ascii", errors="ignore") as f:
231
+ self.yaml = yaml.safe_load(f) # model dict
232
+
233
+ # Define model
234
+ ch = self.yaml["ch"] = self.yaml.get("ch", ch) # input channels
235
+ if nc and nc != self.yaml["nc"]:
236
+ LOGGER.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}")
237
+ self.yaml["nc"] = nc # override yaml value
238
+ if anchors:
239
+ LOGGER.info(f"Overriding model.yaml anchors with anchors={anchors}")
240
+ self.yaml["anchors"] = round(anchors) # override yaml value
241
+ self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist
242
+ self.names = [str(i) for i in range(self.yaml["nc"])] # default names
243
+ self.inplace = self.yaml.get("inplace", True)
244
+
245
+ # Build strides, anchors
246
+ m = self.model[-1] # Detect()
247
+ if isinstance(m, (Detect, Segment)):
248
+
249
+ def _forward(x):
250
+ """Passes the input 'x' through the model and returns the processed output."""
251
+ return self.forward(x)[0] if isinstance(m, Segment) else self.forward(x)
252
+
253
+ s = 256 # 2x min stride
254
+ m.inplace = self.inplace
255
+ m.stride = torch.tensor([s / x.shape[-2] for x in _forward(torch.zeros(1, ch, s, s))]) # forward
256
+ check_anchor_order(m)
257
+ m.anchors /= m.stride.view(-1, 1, 1)
258
+ self.stride = m.stride
259
+ self._initialize_biases() # only run once
260
+
261
+ # Init weights, biases
262
+ initialize_weights(self)
263
+ self.info()
264
+ LOGGER.info("")
265
+
266
+ def forward(self, x, augment=False, profile=False, visualize=False):
267
+ """Performs single-scale or augmented inference and may include profiling or visualization."""
268
+ if augment:
269
+ return self._forward_augment(x) # augmented inference, None
270
+ return self._forward_once(x, profile, visualize) # single-scale inference, train
271
+
272
+ def _forward_augment(self, x):
273
+ """Performs augmented inference across different scales and flips, returning combined detections."""
274
+ img_size = x.shape[-2:] # height, width
275
+ s = [1, 0.83, 0.67] # scales
276
+ f = [None, 3, None] # flips (2-ud, 3-lr)
277
+ y = [] # outputs
278
+ for si, fi in zip(s, f):
279
+ xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max()))
280
+ yi = self._forward_once(xi)[0] # forward
281
+ # cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
282
+ yi = self._descale_pred(yi, fi, si, img_size)
283
+ y.append(yi)
284
+ y = self._clip_augmented(y) # clip augmented tails
285
+ return torch.cat(y, 1), None # augmented inference, train
286
+
287
+ def _descale_pred(self, p, flips, scale, img_size):
288
+ """De-scales predictions from augmented inference, adjusting for flips and image size."""
289
+ if self.inplace:
290
+ p[..., :4] /= scale # de-scale
291
+ if flips == 2:
292
+ p[..., 1] = img_size[0] - p[..., 1] # de-flip ud
293
+ elif flips == 3:
294
+ p[..., 0] = img_size[1] - p[..., 0] # de-flip lr
295
+ else:
296
+ x, y, wh = p[..., 0:1] / scale, p[..., 1:2] / scale, p[..., 2:4] / scale # de-scale
297
+ if flips == 2:
298
+ y = img_size[0] - y # de-flip ud
299
+ elif flips == 3:
300
+ x = img_size[1] - x # de-flip lr
301
+ p = torch.cat((x, y, wh, p[..., 4:]), -1)
302
+ return p
303
+
304
+ def _clip_augmented(self, y):
305
+ """Clips augmented inference tails for YOLOv5 models, affecting first and last tensors based on grid points and
306
+ layer counts.
307
+ """
308
+ nl = self.model[-1].nl # number of detection layers (P3-P5)
309
+ g = sum(4**x for x in range(nl)) # grid points
310
+ e = 1 # exclude layer count
311
+ i = (y[0].shape[1] // g) * sum(4**x for x in range(e)) # indices
312
+ y[0] = y[0][:, :-i] # large
313
+ i = (y[-1].shape[1] // g) * sum(4 ** (nl - 1 - x) for x in range(e)) # indices
314
+ y[-1] = y[-1][:, i:] # small
315
+ return y
316
+
317
+ def _initialize_biases(self, cf=None):
318
+ """
319
+ Initializes biases for YOLOv5's Detect() module, optionally using class frequencies (cf).
320
+
321
+ For details see https://arxiv.org/abs/1708.02002 section 3.3.
322
+ """
323
+ # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
324
+ m = self.model[-1] # Detect() module
325
+ for mi, s in zip(m.m, m.stride): # from
326
+ b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
327
+ b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
328
+ b.data[:, 5 : 5 + m.nc] += (
329
+ math.log(0.6 / (m.nc - 0.99999)) if cf is None else torch.log(cf / cf.sum())
330
+ ) # cls
331
+ mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
332
+
333
+
334
+ Model = DetectionModel # retain YOLOv5 'Model' class for backwards compatibility
335
+
336
+
337
+ class SegmentationModel(DetectionModel):
338
+ """YOLOv5 segmentation model for object detection and segmentation tasks with configurable parameters."""
339
+
340
+ def __init__(self, cfg="yolov5s-seg.yaml", ch=3, nc=None, anchors=None):
341
+ """Initializes a YOLOv5 segmentation model with configurable params: cfg (str) for configuration, ch (int) for channels, nc (int) for num classes, anchors (list)."""
342
+ super().__init__(cfg, ch, nc, anchors)
343
+
344
+
345
+ class ClassificationModel(BaseModel):
346
+ """YOLOv5 classification model for image classification tasks, initialized with a config file or detection model."""
347
+
348
+ def __init__(self, cfg=None, model=None, nc=1000, cutoff=10):
349
+ """Initializes YOLOv5 model with config file `cfg`, input channels `ch`, number of classes `nc`, and `cuttoff`
350
+ index.
351
+ """
352
+ super().__init__()
353
+ self._from_detection_model(model, nc, cutoff) if model is not None else self._from_yaml(cfg)
354
+
355
+ def _from_detection_model(self, model, nc=1000, cutoff=10):
356
+ """Creates a classification model from a YOLOv5 detection model, slicing at `cutoff` and adding a classification
357
+ layer.
358
+ """
359
+ if isinstance(model, DetectMultiBackend):
360
+ model = model.model # unwrap DetectMultiBackend
361
+ model.model = model.model[:cutoff] # backbone
362
+ m = model.model[-1] # last layer
363
+ ch = m.conv.in_channels if hasattr(m, "conv") else m.cv1.conv.in_channels # ch into module
364
+ c = Classify(ch, nc) # Classify()
365
+ c.i, c.f, c.type = m.i, m.f, "models.common.Classify" # index, from, type
366
+ model.model[-1] = c # replace
367
+ self.model = model.model
368
+ self.stride = model.stride
369
+ self.save = []
370
+ self.nc = nc
371
+
372
+ def _from_yaml(self, cfg):
373
+ """Creates a YOLOv5 classification model from a specified *.yaml configuration file."""
374
+ self.model = None
375
+
376
+
377
+ def parse_model(d, ch):
378
+ """Parses a YOLOv5 model from a dict `d`, configuring layers based on input channels `ch` and model architecture."""
379
+ LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
380
+ anchors, nc, gd, gw, act, ch_mul = (
381
+ d["anchors"],
382
+ d["nc"],
383
+ d["depth_multiple"],
384
+ d["width_multiple"],
385
+ d.get("activation"),
386
+ d.get("channel_multiple"),
387
+ )
388
+ if act:
389
+ Conv.default_act = eval(act) # redefine default activation, i.e. Conv.default_act = nn.SiLU()
390
+ LOGGER.info(f"{colorstr('activation:')} {act}") # print
391
+ if not ch_mul:
392
+ ch_mul = 8
393
+ na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
394
+ no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
395
+
396
+ layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
397
+ for i, (f, n, m, args) in enumerate(d["backbone"] + d["head"]): # from, number, module, args
398
+ m = eval(m) if isinstance(m, str) else m # eval strings
399
+ for j, a in enumerate(args):
400
+ with contextlib.suppress(NameError):
401
+ args[j] = eval(a) if isinstance(a, str) else a # eval strings
402
+
403
+ n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain
404
+ if m in {
405
+ Conv,
406
+ GhostConv,
407
+ Bottleneck,
408
+ GhostBottleneck,
409
+ SPP,
410
+ SPPF,
411
+ DWConv,
412
+ MixConv2d,
413
+ Focus,
414
+ CrossConv,
415
+ BottleneckCSP,
416
+ C3,
417
+ C3TR,
418
+ C3SPP,
419
+ C3Ghost,
420
+ nn.ConvTranspose2d,
421
+ DWConvTranspose2d,
422
+ C3x,
423
+ }:
424
+ c1, c2 = ch[f], args[0]
425
+ if c2 != no: # if not output
426
+ c2 = make_divisible(c2 * gw, ch_mul)
427
+
428
+ args = [c1, c2, *args[1:]]
429
+ if m in {BottleneckCSP, C3, C3TR, C3Ghost, C3x}:
430
+ args.insert(2, n) # number of repeats
431
+ n = 1
432
+ elif m is nn.BatchNorm2d:
433
+ args = [ch[f]]
434
+ elif m is Concat:
435
+ c2 = sum(ch[x] for x in f)
436
+ # TODO: channel, gw, gd
437
+ elif m in {Detect, Segment}:
438
+ args.append([ch[x] for x in f])
439
+ if isinstance(args[1], int): # number of anchors
440
+ args[1] = [list(range(args[1] * 2))] * len(f)
441
+ if m is Segment:
442
+ args[3] = make_divisible(args[3] * gw, ch_mul)
443
+ elif m is Contract:
444
+ c2 = ch[f] * args[0] ** 2
445
+ elif m is Expand:
446
+ c2 = ch[f] // args[0] ** 2
447
+ else:
448
+ c2 = ch[f]
449
+
450
+ m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
451
+ t = str(m)[8:-2].replace("__main__.", "") # module type
452
+ np = sum(x.numel() for x in m_.parameters()) # number params
453
+ m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
454
+ LOGGER.info(f"{i:>3}{str(f):>18}{n_:>3}{np:10.0f} {t:<40}{str(args):<30}") # print
455
+ save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
456
+ layers.append(m_)
457
+ if i == 0:
458
+ ch = []
459
+ ch.append(c2)
460
+ return nn.Sequential(*layers), sorted(save)
461
+
462
+
463
+ if __name__ == "__main__":
464
+ parser = argparse.ArgumentParser()
465
+ parser.add_argument("--cfg", type=str, default="yolov5s.yaml", help="model.yaml")
466
+ parser.add_argument("--batch-size", type=int, default=1, help="total batch size for all GPUs")
467
+ parser.add_argument("--device", default="", help="cuda device, i.e. 0 or 0,1,2,3 or cpu")
468
+ parser.add_argument("--profile", action="store_true", help="profile model speed")
469
+ parser.add_argument("--line-profile", action="store_true", help="profile model speed layer by layer")
470
+ parser.add_argument("--test", action="store_true", help="test all yolo*.yaml")
471
+ opt = parser.parse_args()
472
+ opt.cfg = check_yaml(opt.cfg) # check YAML
473
+ print_args(vars(opt))
474
+ device = select_device(opt.device)
475
+
476
+ # Create model
477
+ im = torch.rand(opt.batch_size, 3, 640, 640).to(device)
478
+ model = Model(opt.cfg).to(device)
479
+
480
+ # Options
481
+ if opt.line_profile: # profile layer by layer
482
+ model(im, profile=True)
483
+
484
+ elif opt.profile: # profile forward-backward
485
+ results = profile(input=im, ops=[model], n=3)
486
+
487
+ elif opt.test: # test all models
488
+ for cfg in Path(ROOT / "models").rglob("yolo*.yaml"):
489
+ try:
490
+ _ = Model(cfg)
491
+ except Exception as e:
492
+ print(f"Error in {cfg}: {e}")
493
+
494
+ else: # report fused model summary
495
+ model.fuse()
@@ -0,0 +1,49 @@
1
+ # Ultralytics YOLOv5 🚀, AGPL-3.0 license
2
+
3
+ # Parameters
4
+ nc: 80 # number of classes
5
+ depth_multiple: 1.0 # model depth multiple
6
+ width_multiple: 1.0 # layer channel multiple
7
+ anchors:
8
+ - [10, 13, 16, 30, 33, 23] # P3/8
9
+ - [30, 61, 62, 45, 59, 119] # P4/16
10
+ - [116, 90, 156, 198, 373, 326] # P5/32
11
+
12
+ # YOLOv5 v6.0 backbone
13
+ backbone:
14
+ # [from, number, module, args]
15
+ [
16
+ [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17
+ [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18
+ [-1, 3, C3, [128]],
19
+ [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20
+ [-1, 6, C3, [256]],
21
+ [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22
+ [-1, 9, C3, [512]],
23
+ [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
24
+ [-1, 3, C3, [1024]],
25
+ [-1, 1, SPPF, [1024, 5]], # 9
26
+ ]
27
+
28
+ # YOLOv5 v6.0 head
29
+ head: [
30
+ [-1, 1, Conv, [512, 1, 1]],
31
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
32
+ [[-1, 6], 1, Concat, [1]], # cat backbone P4
33
+ [-1, 3, C3, [512, False]], # 13
34
+
35
+ [-1, 1, Conv, [256, 1, 1]],
36
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
37
+ [[-1, 4], 1, Concat, [1]], # cat backbone P3
38
+ [-1, 3, C3, [256, False]], # 17 (P3/8-small)
39
+
40
+ [-1, 1, Conv, [256, 3, 2]],
41
+ [[-1, 14], 1, Concat, [1]], # cat head P4
42
+ [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
43
+
44
+ [-1, 1, Conv, [512, 3, 2]],
45
+ [[-1, 10], 1, Concat, [1]], # cat head P5
46
+ [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
47
+
48
+ [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
49
+ ]
@@ -0,0 +1,49 @@
1
+ # Ultralytics YOLOv5 🚀, AGPL-3.0 license
2
+
3
+ # Parameters
4
+ nc: 80 # number of classes
5
+ depth_multiple: 0.67 # model depth multiple
6
+ width_multiple: 0.75 # layer channel multiple
7
+ anchors:
8
+ - [10, 13, 16, 30, 33, 23] # P3/8
9
+ - [30, 61, 62, 45, 59, 119] # P4/16
10
+ - [116, 90, 156, 198, 373, 326] # P5/32
11
+
12
+ # YOLOv5 v6.0 backbone
13
+ backbone:
14
+ # [from, number, module, args]
15
+ [
16
+ [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17
+ [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18
+ [-1, 3, C3, [128]],
19
+ [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20
+ [-1, 6, C3, [256]],
21
+ [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22
+ [-1, 9, C3, [512]],
23
+ [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
24
+ [-1, 3, C3, [1024]],
25
+ [-1, 1, SPPF, [1024, 5]], # 9
26
+ ]
27
+
28
+ # YOLOv5 v6.0 head
29
+ head: [
30
+ [-1, 1, Conv, [512, 1, 1]],
31
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
32
+ [[-1, 6], 1, Concat, [1]], # cat backbone P4
33
+ [-1, 3, C3, [512, False]], # 13
34
+
35
+ [-1, 1, Conv, [256, 1, 1]],
36
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
37
+ [[-1, 4], 1, Concat, [1]], # cat backbone P3
38
+ [-1, 3, C3, [256, False]], # 17 (P3/8-small)
39
+
40
+ [-1, 1, Conv, [256, 3, 2]],
41
+ [[-1, 14], 1, Concat, [1]], # cat head P4
42
+ [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
43
+
44
+ [-1, 1, Conv, [512, 3, 2]],
45
+ [[-1, 10], 1, Concat, [1]], # cat head P5
46
+ [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
47
+
48
+ [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
49
+ ]
@@ -0,0 +1,49 @@
1
+ # Ultralytics YOLOv5 🚀, AGPL-3.0 license
2
+
3
+ # Parameters
4
+ nc: 80 # number of classes
5
+ depth_multiple: 0.33 # model depth multiple
6
+ width_multiple: 0.25 # layer channel multiple
7
+ anchors:
8
+ - [10, 13, 16, 30, 33, 23] # P3/8
9
+ - [30, 61, 62, 45, 59, 119] # P4/16
10
+ - [116, 90, 156, 198, 373, 326] # P5/32
11
+
12
+ # YOLOv5 v6.0 backbone
13
+ backbone:
14
+ # [from, number, module, args]
15
+ [
16
+ [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17
+ [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18
+ [-1, 3, C3, [128]],
19
+ [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20
+ [-1, 6, C3, [256]],
21
+ [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22
+ [-1, 9, C3, [512]],
23
+ [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
24
+ [-1, 3, C3, [1024]],
25
+ [-1, 1, SPPF, [1024, 5]], # 9
26
+ ]
27
+
28
+ # YOLOv5 v6.0 head
29
+ head: [
30
+ [-1, 1, Conv, [512, 1, 1]],
31
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
32
+ [[-1, 6], 1, Concat, [1]], # cat backbone P4
33
+ [-1, 3, C3, [512, False]], # 13
34
+
35
+ [-1, 1, Conv, [256, 1, 1]],
36
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
37
+ [[-1, 4], 1, Concat, [1]], # cat backbone P3
38
+ [-1, 3, C3, [256, False]], # 17 (P3/8-small)
39
+
40
+ [-1, 1, Conv, [256, 3, 2]],
41
+ [[-1, 14], 1, Concat, [1]], # cat head P4
42
+ [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
43
+
44
+ [-1, 1, Conv, [512, 3, 2]],
45
+ [[-1, 10], 1, Concat, [1]], # cat head P5
46
+ [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
47
+
48
+ [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
49
+ ]
@@ -0,0 +1,49 @@
1
+ # Ultralytics YOLOv5 🚀, AGPL-3.0 license
2
+
3
+ # Parameters
4
+ nc: 80 # number of classes
5
+ depth_multiple: 0.33 # model depth multiple
6
+ width_multiple: 0.50 # layer channel multiple
7
+ anchors:
8
+ - [10, 13, 16, 30, 33, 23] # P3/8
9
+ - [30, 61, 62, 45, 59, 119] # P4/16
10
+ - [116, 90, 156, 198, 373, 326] # P5/32
11
+
12
+ # YOLOv5 v6.0 backbone
13
+ backbone:
14
+ # [from, number, module, args]
15
+ [
16
+ [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17
+ [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18
+ [-1, 3, C3, [128]],
19
+ [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20
+ [-1, 6, C3, [256]],
21
+ [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22
+ [-1, 9, C3, [512]],
23
+ [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
24
+ [-1, 3, C3, [1024]],
25
+ [-1, 1, SPPF, [1024, 5]], # 9
26
+ ]
27
+
28
+ # YOLOv5 v6.0 head
29
+ head: [
30
+ [-1, 1, Conv, [512, 1, 1]],
31
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
32
+ [[-1, 6], 1, Concat, [1]], # cat backbone P4
33
+ [-1, 3, C3, [512, False]], # 13
34
+
35
+ [-1, 1, Conv, [256, 1, 1]],
36
+ [-1, 1, nn.Upsample, [None, 2, "nearest"]],
37
+ [[-1, 4], 1, Concat, [1]], # cat backbone P3
38
+ [-1, 3, C3, [256, False]], # 17 (P3/8-small)
39
+
40
+ [-1, 1, Conv, [256, 3, 2]],
41
+ [[-1, 14], 1, Concat, [1]], # cat head P4
42
+ [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
43
+
44
+ [-1, 1, Conv, [512, 3, 2]],
45
+ [[-1, 10], 1, Concat, [1]], # cat head P5
46
+ [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
47
+
48
+ [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
49
+ ]