Spaces:
Sleeping
Sleeping
Upload 39 files
Browse files- models/__init__.py +0 -0
- models/__pycache__/__init__.cpython-310.pyc +0 -0
- models/__pycache__/__init__.cpython-311.pyc +0 -0
- models/__pycache__/__init__.cpython-312.pyc +0 -0
- models/__pycache__/common.cpython-310.pyc +0 -0
- models/__pycache__/common.cpython-311.pyc +0 -0
- models/__pycache__/common.cpython-312.pyc +0 -0
- models/__pycache__/experimental.cpython-310.pyc +0 -0
- models/__pycache__/experimental.cpython-311.pyc +0 -0
- models/__pycache__/yolo.cpython-310.pyc +0 -0
- models/__pycache__/yolo.cpython-311.pyc +0 -0
- models/common.py +748 -0
- models/custom_yolov5s.yaml +49 -0
- models/experimental.py +104 -0
- models/hub/anchors.yaml +59 -0
- models/hub/yolov3-spp.yaml +51 -0
- models/hub/yolov3-tiny.yaml +41 -0
- models/hub/yolov3.yaml +51 -0
- models/hub/yolov5-bifpn.yaml +48 -0
- models/hub/yolov5-fpn.yaml +42 -0
- models/hub/yolov5-p2.yaml +54 -0
- models/hub/yolov5-p34.yaml +41 -0
- models/hub/yolov5-p6.yaml +56 -0
- models/hub/yolov5-p7.yaml +67 -0
- models/hub/yolov5-panet.yaml +48 -0
- models/hub/yolov5l6.yaml +60 -0
- models/hub/yolov5m6.yaml +60 -0
- models/hub/yolov5n6.yaml +60 -0
- models/hub/yolov5s-ghost.yaml +48 -0
- models/hub/yolov5s-transformer.yaml +48 -0
- models/hub/yolov5s6.yaml +60 -0
- models/hub/yolov5x6.yaml +60 -0
- models/tf.py +574 -0
- models/yolo.py +338 -0
- models/yolov5l.yaml +48 -0
- models/yolov5m.yaml +48 -0
- models/yolov5n.yaml +48 -0
- models/yolov5s.yaml +48 -0
- models/yolov5x.yaml +48 -0
models/__init__.py
ADDED
File without changes
|
models/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (144 Bytes). View file
|
|
models/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (160 Bytes). View file
|
|
models/__pycache__/__init__.cpython-312.pyc
ADDED
Binary file (148 Bytes). View file
|
|
models/__pycache__/common.cpython-310.pyc
ADDED
Binary file (31.9 kB). View file
|
|
models/__pycache__/common.cpython-311.pyc
ADDED
Binary file (67.8 kB). View file
|
|
models/__pycache__/common.cpython-312.pyc
ADDED
Binary file (57 kB). View file
|
|
models/__pycache__/experimental.cpython-310.pyc
ADDED
Binary file (4.81 kB). View file
|
|
models/__pycache__/experimental.cpython-311.pyc
ADDED
Binary file (9.42 kB). View file
|
|
models/__pycache__/yolo.cpython-310.pyc
ADDED
Binary file (13.1 kB). View file
|
|
models/__pycache__/yolo.cpython-311.pyc
ADDED
Binary file (28.4 kB). View file
|
|
models/common.py
ADDED
@@ -0,0 +1,748 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
2 |
+
"""
|
3 |
+
Common modules
|
4 |
+
"""
|
5 |
+
|
6 |
+
import json
|
7 |
+
import math
|
8 |
+
import platform
|
9 |
+
import warnings
|
10 |
+
from collections import OrderedDict, namedtuple
|
11 |
+
from copy import copy
|
12 |
+
from pathlib import Path
|
13 |
+
|
14 |
+
import cv2
|
15 |
+
import numpy as np
|
16 |
+
import pandas as pd
|
17 |
+
import requests
|
18 |
+
import torch
|
19 |
+
import torch.nn as nn
|
20 |
+
import yaml
|
21 |
+
from PIL import Image
|
22 |
+
from torch.cuda import amp
|
23 |
+
|
24 |
+
from utils.dataloaders import exif_transpose, letterbox
|
25 |
+
from utils.general import (LOGGER, check_requirements, check_suffix, check_version, colorstr, increment_path,
|
26 |
+
make_divisible, non_max_suppression, scale_coords, xywh2xyxy, xyxy2xywh)
|
27 |
+
from utils.plots import Annotator, colors, save_one_box
|
28 |
+
from utils.torch_utils import copy_attr, time_sync
|
29 |
+
|
30 |
+
|
31 |
+
def autopad(k, p=None): # kernel, padding
|
32 |
+
# Pad to 'same'
|
33 |
+
if p is None:
|
34 |
+
p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
|
35 |
+
return p
|
36 |
+
|
37 |
+
|
38 |
+
class Conv(nn.Module):
|
39 |
+
# Standard convolution
|
40 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
41 |
+
super().__init__()
|
42 |
+
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
|
43 |
+
self.bn = nn.BatchNorm2d(c2)
|
44 |
+
self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
|
45 |
+
|
46 |
+
def forward(self, x):
|
47 |
+
return self.act(self.bn(self.conv(x)))
|
48 |
+
|
49 |
+
def forward_fuse(self, x):
|
50 |
+
return self.act(self.conv(x))
|
51 |
+
|
52 |
+
|
53 |
+
class DWConv(Conv):
|
54 |
+
# Depth-wise convolution class
|
55 |
+
def __init__(self, c1, c2, k=1, s=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
56 |
+
super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
|
57 |
+
|
58 |
+
|
59 |
+
class DWConvTranspose2d(nn.ConvTranspose2d):
|
60 |
+
# Depth-wise transpose convolution class
|
61 |
+
def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0): # ch_in, ch_out, kernel, stride, padding, padding_out
|
62 |
+
super().__init__(c1, c2, k, s, p1, p2, groups=math.gcd(c1, c2))
|
63 |
+
|
64 |
+
|
65 |
+
class TransformerLayer(nn.Module):
|
66 |
+
# Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance)
|
67 |
+
def __init__(self, c, num_heads):
|
68 |
+
super().__init__()
|
69 |
+
self.q = nn.Linear(c, c, bias=False)
|
70 |
+
self.k = nn.Linear(c, c, bias=False)
|
71 |
+
self.v = nn.Linear(c, c, bias=False)
|
72 |
+
self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads)
|
73 |
+
self.fc1 = nn.Linear(c, c, bias=False)
|
74 |
+
self.fc2 = nn.Linear(c, c, bias=False)
|
75 |
+
|
76 |
+
def forward(self, x):
|
77 |
+
x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x
|
78 |
+
x = self.fc2(self.fc1(x)) + x
|
79 |
+
return x
|
80 |
+
|
81 |
+
|
82 |
+
class TransformerBlock(nn.Module):
|
83 |
+
# Vision Transformer https://arxiv.org/abs/2010.11929
|
84 |
+
def __init__(self, c1, c2, num_heads, num_layers):
|
85 |
+
super().__init__()
|
86 |
+
self.conv = None
|
87 |
+
if c1 != c2:
|
88 |
+
self.conv = Conv(c1, c2)
|
89 |
+
self.linear = nn.Linear(c2, c2) # learnable position embedding
|
90 |
+
self.tr = nn.Sequential(*(TransformerLayer(c2, num_heads) for _ in range(num_layers)))
|
91 |
+
self.c2 = c2
|
92 |
+
|
93 |
+
def forward(self, x):
|
94 |
+
if self.conv is not None:
|
95 |
+
x = self.conv(x)
|
96 |
+
b, _, w, h = x.shape
|
97 |
+
p = x.flatten(2).permute(2, 0, 1)
|
98 |
+
return self.tr(p + self.linear(p)).permute(1, 2, 0).reshape(b, self.c2, w, h)
|
99 |
+
|
100 |
+
|
101 |
+
class Bottleneck(nn.Module):
|
102 |
+
# Standard bottleneck
|
103 |
+
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
104 |
+
super().__init__()
|
105 |
+
c_ = int(c2 * e) # hidden channels
|
106 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
107 |
+
self.cv2 = Conv(c_, c2, 3, 1, g=g)
|
108 |
+
self.add = shortcut and c1 == c2
|
109 |
+
|
110 |
+
def forward(self, x):
|
111 |
+
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
112 |
+
|
113 |
+
|
114 |
+
class BottleneckCSP(nn.Module):
|
115 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
116 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
117 |
+
super().__init__()
|
118 |
+
c_ = int(c2 * e) # hidden channels
|
119 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
120 |
+
self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
|
121 |
+
self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
|
122 |
+
self.cv4 = Conv(2 * c_, c2, 1, 1)
|
123 |
+
self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
|
124 |
+
self.act = nn.SiLU()
|
125 |
+
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
|
126 |
+
|
127 |
+
def forward(self, x):
|
128 |
+
y1 = self.cv3(self.m(self.cv1(x)))
|
129 |
+
y2 = self.cv2(x)
|
130 |
+
return self.cv4(self.act(self.bn(torch.cat((y1, y2), 1))))
|
131 |
+
|
132 |
+
|
133 |
+
class CrossConv(nn.Module):
|
134 |
+
# Cross Convolution Downsample
|
135 |
+
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False):
|
136 |
+
# ch_in, ch_out, kernel, stride, groups, expansion, shortcut
|
137 |
+
super().__init__()
|
138 |
+
c_ = int(c2 * e) # hidden channels
|
139 |
+
self.cv1 = Conv(c1, c_, (1, k), (1, s))
|
140 |
+
self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g)
|
141 |
+
self.add = shortcut and c1 == c2
|
142 |
+
|
143 |
+
def forward(self, x):
|
144 |
+
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
145 |
+
|
146 |
+
|
147 |
+
class C3(nn.Module):
|
148 |
+
# CSP Bottleneck with 3 convolutions
|
149 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
150 |
+
super().__init__()
|
151 |
+
c_ = int(c2 * e) # hidden channels
|
152 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
153 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
154 |
+
self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
|
155 |
+
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
|
156 |
+
|
157 |
+
def forward(self, x):
|
158 |
+
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
|
159 |
+
|
160 |
+
|
161 |
+
class C3x(C3):
|
162 |
+
# C3 module with cross-convolutions
|
163 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
|
164 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
165 |
+
c_ = int(c2 * e)
|
166 |
+
self.m = nn.Sequential(*(CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)))
|
167 |
+
|
168 |
+
|
169 |
+
class C3TR(C3):
|
170 |
+
# C3 module with TransformerBlock()
|
171 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
|
172 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
173 |
+
c_ = int(c2 * e)
|
174 |
+
self.m = TransformerBlock(c_, c_, 4, n)
|
175 |
+
|
176 |
+
|
177 |
+
class C3SPP(C3):
|
178 |
+
# C3 module with SPP()
|
179 |
+
def __init__(self, c1, c2, k=(5, 9, 13), n=1, shortcut=True, g=1, e=0.5):
|
180 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
181 |
+
c_ = int(c2 * e)
|
182 |
+
self.m = SPP(c_, c_, k)
|
183 |
+
|
184 |
+
|
185 |
+
class C3Ghost(C3):
|
186 |
+
# C3 module with GhostBottleneck()
|
187 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
|
188 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
189 |
+
c_ = int(c2 * e) # hidden channels
|
190 |
+
self.m = nn.Sequential(*(GhostBottleneck(c_, c_) for _ in range(n)))
|
191 |
+
|
192 |
+
|
193 |
+
class SPP(nn.Module):
|
194 |
+
# Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729
|
195 |
+
def __init__(self, c1, c2, k=(5, 9, 13)):
|
196 |
+
super().__init__()
|
197 |
+
c_ = c1 // 2 # hidden channels
|
198 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
199 |
+
self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
|
200 |
+
self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
|
201 |
+
|
202 |
+
def forward(self, x):
|
203 |
+
x = self.cv1(x)
|
204 |
+
with warnings.catch_warnings():
|
205 |
+
warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
|
206 |
+
return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
|
207 |
+
|
208 |
+
|
209 |
+
class SPPF(nn.Module):
|
210 |
+
# Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
|
211 |
+
def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13))
|
212 |
+
super().__init__()
|
213 |
+
c_ = c1 // 2 # hidden channels
|
214 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
215 |
+
self.cv2 = Conv(c_ * 4, c2, 1, 1)
|
216 |
+
self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
|
217 |
+
|
218 |
+
def forward(self, x):
|
219 |
+
x = self.cv1(x)
|
220 |
+
with warnings.catch_warnings():
|
221 |
+
warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
|
222 |
+
y1 = self.m(x)
|
223 |
+
y2 = self.m(y1)
|
224 |
+
return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))
|
225 |
+
|
226 |
+
|
227 |
+
class Focus(nn.Module):
|
228 |
+
# Focus wh information into c-space
|
229 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
230 |
+
super().__init__()
|
231 |
+
self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
|
232 |
+
# self.contract = Contract(gain=2)
|
233 |
+
|
234 |
+
def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
|
235 |
+
return self.conv(torch.cat((x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]), 1))
|
236 |
+
# return self.conv(self.contract(x))
|
237 |
+
|
238 |
+
|
239 |
+
class GhostConv(nn.Module):
|
240 |
+
# Ghost Convolution https://github.com/huawei-noah/ghostnet
|
241 |
+
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
|
242 |
+
super().__init__()
|
243 |
+
c_ = c2 // 2 # hidden channels
|
244 |
+
self.cv1 = Conv(c1, c_, k, s, None, g, act)
|
245 |
+
self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)
|
246 |
+
|
247 |
+
def forward(self, x):
|
248 |
+
y = self.cv1(x)
|
249 |
+
return torch.cat((y, self.cv2(y)), 1)
|
250 |
+
|
251 |
+
|
252 |
+
class GhostBottleneck(nn.Module):
|
253 |
+
# Ghost Bottleneck https://github.com/huawei-noah/ghostnet
|
254 |
+
def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride
|
255 |
+
super().__init__()
|
256 |
+
c_ = c2 // 2
|
257 |
+
self.conv = nn.Sequential(
|
258 |
+
GhostConv(c1, c_, 1, 1), # pw
|
259 |
+
DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw
|
260 |
+
GhostConv(c_, c2, 1, 1, act=False)) # pw-linear
|
261 |
+
self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), Conv(c1, c2, 1, 1,
|
262 |
+
act=False)) if s == 2 else nn.Identity()
|
263 |
+
|
264 |
+
def forward(self, x):
|
265 |
+
return self.conv(x) + self.shortcut(x)
|
266 |
+
|
267 |
+
|
268 |
+
class Contract(nn.Module):
|
269 |
+
# Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40)
|
270 |
+
def __init__(self, gain=2):
|
271 |
+
super().__init__()
|
272 |
+
self.gain = gain
|
273 |
+
|
274 |
+
def forward(self, x):
|
275 |
+
b, c, h, w = x.size() # assert (h / s == 0) and (W / s == 0), 'Indivisible gain'
|
276 |
+
s = self.gain
|
277 |
+
x = x.view(b, c, h // s, s, w // s, s) # x(1,64,40,2,40,2)
|
278 |
+
x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40)
|
279 |
+
return x.view(b, c * s * s, h // s, w // s) # x(1,256,40,40)
|
280 |
+
|
281 |
+
|
282 |
+
class Expand(nn.Module):
|
283 |
+
# Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160)
|
284 |
+
def __init__(self, gain=2):
|
285 |
+
super().__init__()
|
286 |
+
self.gain = gain
|
287 |
+
|
288 |
+
def forward(self, x):
|
289 |
+
b, c, h, w = x.size() # assert C / s ** 2 == 0, 'Indivisible gain'
|
290 |
+
s = self.gain
|
291 |
+
x = x.view(b, s, s, c // s ** 2, h, w) # x(1,2,2,16,80,80)
|
292 |
+
x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2)
|
293 |
+
return x.view(b, c // s ** 2, h * s, w * s) # x(1,16,160,160)
|
294 |
+
|
295 |
+
|
296 |
+
class Concat(nn.Module):
|
297 |
+
# Concatenate a list of tensors along dimension
|
298 |
+
def __init__(self, dimension=1):
|
299 |
+
super().__init__()
|
300 |
+
self.d = dimension
|
301 |
+
|
302 |
+
def forward(self, x):
|
303 |
+
return torch.cat(x, self.d)
|
304 |
+
|
305 |
+
|
306 |
+
class DetectMultiBackend(nn.Module):
|
307 |
+
# YOLOv5 MultiBackend class for python inference on various backends
|
308 |
+
def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False, data=None, fp16=False, fuse=True):
|
309 |
+
# Usage:
|
310 |
+
# PyTorch: weights = *.pt
|
311 |
+
# TorchScript: *.torchscript
|
312 |
+
# ONNX Runtime: *.onnx
|
313 |
+
# ONNX OpenCV DNN: *.onnx with --dnn
|
314 |
+
# OpenVINO: *.xml
|
315 |
+
# CoreML: *.mlmodel
|
316 |
+
# TensorRT: *.engine
|
317 |
+
# TensorFlow SavedModel: *_saved_model
|
318 |
+
# TensorFlow GraphDef: *.pb
|
319 |
+
# TensorFlow Lite: *.tflite
|
320 |
+
# TensorFlow Edge TPU: *_edgetpu.tflite
|
321 |
+
from models.experimental import attempt_download, attempt_load # scoped to avoid circular import
|
322 |
+
|
323 |
+
super().__init__()
|
324 |
+
w = str(weights[0] if isinstance(weights, list) else weights)
|
325 |
+
pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs = self.model_type(w) # get backend
|
326 |
+
w = attempt_download(w) # download if not local
|
327 |
+
fp16 &= (pt or jit or onnx or engine) and device.type != 'cpu' # FP16
|
328 |
+
stride, names = 32, [f'class{i}' for i in range(1000)] # assign defaults
|
329 |
+
if data: # assign class names (optional)
|
330 |
+
with open(data, errors='ignore') as f:
|
331 |
+
names = yaml.safe_load(f)['names']
|
332 |
+
|
333 |
+
if pt: # PyTorch
|
334 |
+
model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
|
335 |
+
stride = max(int(model.stride.max()), 32) # model stride
|
336 |
+
names = model.module.names if hasattr(model, 'module') else model.names # get class names
|
337 |
+
model.half() if fp16 else model.float()
|
338 |
+
self.model = model # explicitly assign for to(), cpu(), cuda(), half()
|
339 |
+
elif jit: # TorchScript
|
340 |
+
LOGGER.info(f'Loading {w} for TorchScript inference...')
|
341 |
+
extra_files = {'config.txt': ''} # model metadata
|
342 |
+
model = torch.jit.load(w, _extra_files=extra_files)
|
343 |
+
model.half() if fp16 else model.float()
|
344 |
+
if extra_files['config.txt']:
|
345 |
+
d = json.loads(extra_files['config.txt']) # extra_files dict
|
346 |
+
stride, names = int(d['stride']), d['names']
|
347 |
+
elif dnn: # ONNX OpenCV DNN
|
348 |
+
LOGGER.info(f'Loading {w} for ONNX OpenCV DNN inference...')
|
349 |
+
check_requirements(('opencv-python>=4.5.4',))
|
350 |
+
net = cv2.dnn.readNetFromONNX(w)
|
351 |
+
elif onnx: # ONNX Runtime
|
352 |
+
LOGGER.info(f'Loading {w} for ONNX Runtime inference...')
|
353 |
+
cuda = torch.cuda.is_available()
|
354 |
+
check_requirements(('onnx', 'onnxruntime-gpu' if cuda else 'onnxruntime'))
|
355 |
+
import onnxruntime
|
356 |
+
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']
|
357 |
+
session = onnxruntime.InferenceSession(w, providers=providers)
|
358 |
+
meta = session.get_modelmeta().custom_metadata_map # metadata
|
359 |
+
if 'stride' in meta:
|
360 |
+
stride, names = int(meta['stride']), eval(meta['names'])
|
361 |
+
elif xml: # OpenVINO
|
362 |
+
LOGGER.info(f'Loading {w} for OpenVINO inference...')
|
363 |
+
check_requirements(('openvino',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/
|
364 |
+
from openvino.runtime import Core, Layout, get_batch
|
365 |
+
ie = Core()
|
366 |
+
if not Path(w).is_file(): # if not *.xml
|
367 |
+
w = next(Path(w).glob('*.xml')) # get *.xml file from *_openvino_model dir
|
368 |
+
network = ie.read_model(model=w, weights=Path(w).with_suffix('.bin'))
|
369 |
+
if network.get_parameters()[0].get_layout().empty:
|
370 |
+
network.get_parameters()[0].set_layout(Layout("NCHW"))
|
371 |
+
batch_dim = get_batch(network)
|
372 |
+
if batch_dim.is_static:
|
373 |
+
batch_size = batch_dim.get_length()
|
374 |
+
executable_network = ie.compile_model(network, device_name="CPU") # device_name="MYRIAD" for Intel NCS2
|
375 |
+
output_layer = next(iter(executable_network.outputs))
|
376 |
+
meta = Path(w).with_suffix('.yaml')
|
377 |
+
if meta.exists():
|
378 |
+
stride, names = self._load_metadata(meta) # load metadata
|
379 |
+
elif engine: # TensorRT
|
380 |
+
LOGGER.info(f'Loading {w} for TensorRT inference...')
|
381 |
+
import tensorrt as trt # https://developer.nvidia.com/nvidia-tensorrt-download
|
382 |
+
check_version(trt.__version__, '7.0.0', hard=True) # require tensorrt>=7.0.0
|
383 |
+
Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr'))
|
384 |
+
logger = trt.Logger(trt.Logger.INFO)
|
385 |
+
with open(w, 'rb') as f, trt.Runtime(logger) as runtime:
|
386 |
+
model = runtime.deserialize_cuda_engine(f.read())
|
387 |
+
bindings = OrderedDict()
|
388 |
+
fp16 = False # default updated below
|
389 |
+
for index in range(model.num_bindings):
|
390 |
+
name = model.get_binding_name(index)
|
391 |
+
dtype = trt.nptype(model.get_binding_dtype(index))
|
392 |
+
shape = tuple(model.get_binding_shape(index))
|
393 |
+
data = torch.from_numpy(np.empty(shape, dtype=np.dtype(dtype))).to(device)
|
394 |
+
bindings[name] = Binding(name, dtype, shape, data, int(data.data_ptr()))
|
395 |
+
if model.binding_is_input(index) and dtype == np.float16:
|
396 |
+
fp16 = True
|
397 |
+
binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items())
|
398 |
+
context = model.create_execution_context()
|
399 |
+
batch_size = bindings['images'].shape[0]
|
400 |
+
elif coreml: # CoreML
|
401 |
+
LOGGER.info(f'Loading {w} for CoreML inference...')
|
402 |
+
import coremltools as ct
|
403 |
+
model = ct.models.MLModel(w)
|
404 |
+
else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)
|
405 |
+
if saved_model: # SavedModel
|
406 |
+
LOGGER.info(f'Loading {w} for TensorFlow SavedModel inference...')
|
407 |
+
import tensorflow as tf
|
408 |
+
keras = False # assume TF1 saved_model
|
409 |
+
model = tf.keras.models.load_model(w) if keras else tf.saved_model.load(w)
|
410 |
+
elif pb: # GraphDef https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt
|
411 |
+
LOGGER.info(f'Loading {w} for TensorFlow GraphDef inference...')
|
412 |
+
import tensorflow as tf
|
413 |
+
|
414 |
+
def wrap_frozen_graph(gd, inputs, outputs):
|
415 |
+
x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped
|
416 |
+
ge = x.graph.as_graph_element
|
417 |
+
return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))
|
418 |
+
|
419 |
+
gd = tf.Graph().as_graph_def() # graph_def
|
420 |
+
with open(w, 'rb') as f:
|
421 |
+
gd.ParseFromString(f.read())
|
422 |
+
frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs="Identity:0")
|
423 |
+
elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
|
424 |
+
try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu
|
425 |
+
from tflite_runtime.interpreter import Interpreter, load_delegate
|
426 |
+
except ImportError:
|
427 |
+
import tensorflow as tf
|
428 |
+
Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate,
|
429 |
+
if edgetpu: # Edge TPU https://coral.ai/software/#edgetpu-runtime
|
430 |
+
LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...')
|
431 |
+
delegate = {
|
432 |
+
'Linux': 'libedgetpu.so.1',
|
433 |
+
'Darwin': 'libedgetpu.1.dylib',
|
434 |
+
'Windows': 'edgetpu.dll'}[platform.system()]
|
435 |
+
interpreter = Interpreter(model_path=w, experimental_delegates=[load_delegate(delegate)])
|
436 |
+
else: # Lite
|
437 |
+
LOGGER.info(f'Loading {w} for TensorFlow Lite inference...')
|
438 |
+
interpreter = Interpreter(model_path=w) # load TFLite model
|
439 |
+
interpreter.allocate_tensors() # allocate
|
440 |
+
input_details = interpreter.get_input_details() # inputs
|
441 |
+
output_details = interpreter.get_output_details() # outputs
|
442 |
+
elif tfjs:
|
443 |
+
raise Exception('ERROR: YOLOv5 TF.js inference is not supported')
|
444 |
+
else:
|
445 |
+
raise Exception(f'ERROR: {w} is not a supported format')
|
446 |
+
self.__dict__.update(locals()) # assign all variables to self
|
447 |
+
|
448 |
+
def forward(self, im, augment=False, visualize=False, val=False):
|
449 |
+
# YOLOv5 MultiBackend inference
|
450 |
+
b, ch, h, w = im.shape # batch, channel, height, width
|
451 |
+
if self.fp16 and im.dtype != torch.float16:
|
452 |
+
im = im.half() # to FP16
|
453 |
+
|
454 |
+
if self.pt: # PyTorch
|
455 |
+
y = self.model(im, augment=augment, visualize=visualize)[0]
|
456 |
+
elif self.jit: # TorchScript
|
457 |
+
y = self.model(im)[0]
|
458 |
+
elif self.dnn: # ONNX OpenCV DNN
|
459 |
+
im = im.cpu().numpy() # torch to numpy
|
460 |
+
self.net.setInput(im)
|
461 |
+
y = self.net.forward()
|
462 |
+
elif self.onnx: # ONNX Runtime
|
463 |
+
im = im.cpu().numpy() # torch to numpy
|
464 |
+
y = self.session.run([self.session.get_outputs()[0].name], {self.session.get_inputs()[0].name: im})[0]
|
465 |
+
elif self.xml: # OpenVINO
|
466 |
+
im = im.cpu().numpy() # FP32
|
467 |
+
y = self.executable_network([im])[self.output_layer]
|
468 |
+
elif self.engine: # TensorRT
|
469 |
+
assert im.shape == self.bindings['images'].shape, (im.shape, self.bindings['images'].shape)
|
470 |
+
self.binding_addrs['images'] = int(im.data_ptr())
|
471 |
+
self.context.execute_v2(list(self.binding_addrs.values()))
|
472 |
+
y = self.bindings['output'].data
|
473 |
+
elif self.coreml: # CoreML
|
474 |
+
im = im.permute(0, 2, 3, 1).cpu().numpy() # torch BCHW to numpy BHWC shape(1,320,192,3)
|
475 |
+
im = Image.fromarray((im[0] * 255).astype('uint8'))
|
476 |
+
# im = im.resize((192, 320), Image.ANTIALIAS)
|
477 |
+
y = self.model.predict({'image': im}) # coordinates are xywh normalized
|
478 |
+
if 'confidence' in y:
|
479 |
+
box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels
|
480 |
+
conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float)
|
481 |
+
y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1)
|
482 |
+
else:
|
483 |
+
k = 'var_' + str(sorted(int(k.replace('var_', '')) for k in y)[-1]) # output key
|
484 |
+
y = y[k] # output
|
485 |
+
else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)
|
486 |
+
im = im.permute(0, 2, 3, 1).cpu().numpy() # torch BCHW to numpy BHWC shape(1,320,192,3)
|
487 |
+
if self.saved_model: # SavedModel
|
488 |
+
y = (self.model(im, training=False) if self.keras else self.model(im)).numpy()
|
489 |
+
elif self.pb: # GraphDef
|
490 |
+
y = self.frozen_func(x=self.tf.constant(im)).numpy()
|
491 |
+
else: # Lite or Edge TPU
|
492 |
+
input, output = self.input_details[0], self.output_details[0]
|
493 |
+
int8 = input['dtype'] == np.uint8 # is TFLite quantized uint8 model
|
494 |
+
if int8:
|
495 |
+
scale, zero_point = input['quantization']
|
496 |
+
im = (im / scale + zero_point).astype(np.uint8) # de-scale
|
497 |
+
self.interpreter.set_tensor(input['index'], im)
|
498 |
+
self.interpreter.invoke()
|
499 |
+
y = self.interpreter.get_tensor(output['index'])
|
500 |
+
if int8:
|
501 |
+
scale, zero_point = output['quantization']
|
502 |
+
y = (y.astype(np.float32) - zero_point) * scale # re-scale
|
503 |
+
y[..., :4] *= [w, h, w, h] # xywh normalized to pixels
|
504 |
+
|
505 |
+
if isinstance(y, np.ndarray):
|
506 |
+
y = torch.tensor(y, device=self.device)
|
507 |
+
return (y, []) if val else y
|
508 |
+
|
509 |
+
def warmup(self, imgsz=(1, 3, 640, 640)):
|
510 |
+
# Warmup model by running inference once
|
511 |
+
warmup_types = self.pt, self.jit, self.onnx, self.engine, self.saved_model, self.pb
|
512 |
+
if any(warmup_types) and self.device.type != 'cpu':
|
513 |
+
im = torch.zeros(*imgsz, dtype=torch.half if self.fp16 else torch.float, device=self.device) # input
|
514 |
+
for _ in range(2 if self.jit else 1): #
|
515 |
+
self.forward(im) # warmup
|
516 |
+
|
517 |
+
@staticmethod
|
518 |
+
def model_type(p='path/to/model.pt'):
|
519 |
+
# Return model type from model path, i.e. path='path/to/model.onnx' -> type=onnx
|
520 |
+
from export import export_formats
|
521 |
+
suffixes = list(export_formats().Suffix) + ['.xml'] # export suffixes
|
522 |
+
check_suffix(p, suffixes) # checks
|
523 |
+
p = Path(p).name # eliminate trailing separators
|
524 |
+
pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, xml2 = (s in p for s in suffixes)
|
525 |
+
xml |= xml2 # *_openvino_model or *.xml
|
526 |
+
tflite &= not edgetpu # *.tflite
|
527 |
+
return pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs
|
528 |
+
|
529 |
+
@staticmethod
|
530 |
+
def _load_metadata(f='path/to/meta.yaml'):
|
531 |
+
# Load metadata from meta.yaml if it exists
|
532 |
+
with open(f, errors='ignore') as f:
|
533 |
+
d = yaml.safe_load(f)
|
534 |
+
return d['stride'], d['names'] # assign stride, names
|
535 |
+
|
536 |
+
|
537 |
+
class AutoShape(nn.Module):
|
538 |
+
# YOLOv5 input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS
|
539 |
+
conf = 0.25 # NMS confidence threshold
|
540 |
+
iou = 0.45 # NMS IoU threshold
|
541 |
+
agnostic = False # NMS class-agnostic
|
542 |
+
multi_label = False # NMS multiple labels per box
|
543 |
+
classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for COCO persons, cats and dogs
|
544 |
+
max_det = 1000 # maximum number of detections per image
|
545 |
+
amp = False # Automatic Mixed Precision (AMP) inference
|
546 |
+
|
547 |
+
def __init__(self, model, verbose=True):
|
548 |
+
super().__init__()
|
549 |
+
if verbose:
|
550 |
+
LOGGER.info('Adding AutoShape... ')
|
551 |
+
copy_attr(self, model, include=('yaml', 'nc', 'hyp', 'names', 'stride', 'abc'), exclude=()) # copy attributes
|
552 |
+
self.dmb = isinstance(model, DetectMultiBackend) # DetectMultiBackend() instance
|
553 |
+
self.pt = not self.dmb or model.pt # PyTorch model
|
554 |
+
self.model = model.eval()
|
555 |
+
|
556 |
+
def _apply(self, fn):
|
557 |
+
# Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers
|
558 |
+
self = super()._apply(fn)
|
559 |
+
if self.pt:
|
560 |
+
m = self.model.model.model[-1] if self.dmb else self.model.model[-1] # Detect()
|
561 |
+
m.stride = fn(m.stride)
|
562 |
+
m.grid = list(map(fn, m.grid))
|
563 |
+
if isinstance(m.anchor_grid, list):
|
564 |
+
m.anchor_grid = list(map(fn, m.anchor_grid))
|
565 |
+
return self
|
566 |
+
|
567 |
+
@torch.no_grad()
|
568 |
+
def forward(self, imgs, size=640, augment=False, profile=False):
|
569 |
+
# Inference from various sources. For height=640, width=1280, RGB images example inputs are:
|
570 |
+
# file: imgs = 'data/images/zidane.jpg' # str or PosixPath
|
571 |
+
# URI: = 'https://ultralytics.com/images/zidane.jpg'
|
572 |
+
# OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3)
|
573 |
+
# PIL: = Image.open('image.jpg') or ImageGrab.grab() # HWC x(640,1280,3)
|
574 |
+
# numpy: = np.zeros((640,1280,3)) # HWC
|
575 |
+
# torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values)
|
576 |
+
# multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images
|
577 |
+
|
578 |
+
t = [time_sync()]
|
579 |
+
p = next(self.model.parameters()) if self.pt else torch.zeros(1, device=self.model.device) # for device, type
|
580 |
+
autocast = self.amp and (p.device.type != 'cpu') # Automatic Mixed Precision (AMP) inference
|
581 |
+
if isinstance(imgs, torch.Tensor): # torch
|
582 |
+
with amp.autocast(autocast):
|
583 |
+
return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference
|
584 |
+
|
585 |
+
# Pre-process
|
586 |
+
n, imgs = (len(imgs), list(imgs)) if isinstance(imgs, (list, tuple)) else (1, [imgs]) # number, list of images
|
587 |
+
shape0, shape1, files = [], [], [] # image and inference shapes, filenames
|
588 |
+
for i, im in enumerate(imgs):
|
589 |
+
f = f'image{i}' # filename
|
590 |
+
if isinstance(im, (str, Path)): # filename or uri
|
591 |
+
im, f = Image.open(requests.get(im, stream=True).raw if str(im).startswith('http') else im), im
|
592 |
+
im = np.asarray(exif_transpose(im))
|
593 |
+
elif isinstance(im, Image.Image): # PIL Image
|
594 |
+
im, f = np.asarray(exif_transpose(im)), getattr(im, 'filename', f) or f
|
595 |
+
files.append(Path(f).with_suffix('.jpg').name)
|
596 |
+
if im.shape[0] < 5: # image in CHW
|
597 |
+
im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
|
598 |
+
im = im[..., :3] if im.ndim == 3 else np.tile(im[..., None], 3) # enforce 3ch input
|
599 |
+
s = im.shape[:2] # HWC
|
600 |
+
shape0.append(s) # image shape
|
601 |
+
g = (size / max(s)) # gain
|
602 |
+
shape1.append([y * g for y in s])
|
603 |
+
imgs[i] = im if im.data.contiguous else np.ascontiguousarray(im) # update
|
604 |
+
shape1 = [make_divisible(x, self.stride) if self.pt else size for x in np.array(shape1).max(0)] # inf shape
|
605 |
+
x = [letterbox(im, shape1, auto=False)[0] for im in imgs] # pad
|
606 |
+
x = np.ascontiguousarray(np.array(x).transpose((0, 3, 1, 2))) # stack and BHWC to BCHW
|
607 |
+
x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32
|
608 |
+
t.append(time_sync())
|
609 |
+
|
610 |
+
with amp.autocast(autocast):
|
611 |
+
# Inference
|
612 |
+
y = self.model(x, augment, profile) # forward
|
613 |
+
t.append(time_sync())
|
614 |
+
|
615 |
+
# Post-process
|
616 |
+
y = non_max_suppression(y if self.dmb else y[0],
|
617 |
+
self.conf,
|
618 |
+
self.iou,
|
619 |
+
self.classes,
|
620 |
+
self.agnostic,
|
621 |
+
self.multi_label,
|
622 |
+
max_det=self.max_det) # NMS
|
623 |
+
for i in range(n):
|
624 |
+
scale_coords(shape1, y[i][:, :4], shape0[i])
|
625 |
+
|
626 |
+
t.append(time_sync())
|
627 |
+
return Detections(imgs, y, files, t, self.names, x.shape)
|
628 |
+
|
629 |
+
|
630 |
+
class Detections:
|
631 |
+
# YOLOv5 detections class for inference results
|
632 |
+
def __init__(self, imgs, pred, files, times=(0, 0, 0, 0), names=None, shape=None):
|
633 |
+
super().__init__()
|
634 |
+
d = pred[0].device # device
|
635 |
+
gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device=d) for im in imgs] # normalizations
|
636 |
+
self.imgs = imgs # list of images as numpy arrays
|
637 |
+
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
|
638 |
+
self.names = names # class names
|
639 |
+
self.files = files # image filenames
|
640 |
+
self.times = times # profiling times
|
641 |
+
self.xyxy = pred # xyxy pixels
|
642 |
+
self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
|
643 |
+
self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized
|
644 |
+
self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized
|
645 |
+
self.n = len(self.pred) # number of images (batch size)
|
646 |
+
self.t = tuple((times[i + 1] - times[i]) * 1000 / self.n for i in range(3)) # timestamps (ms)
|
647 |
+
self.s = shape # inference BCHW shape
|
648 |
+
|
649 |
+
def display(self, pprint=False, show=False, save=False, crop=False, render=False, labels=True, save_dir=Path('')):
|
650 |
+
crops = []
|
651 |
+
for i, (im, pred) in enumerate(zip(self.imgs, self.pred)):
|
652 |
+
s = f'image {i + 1}/{len(self.pred)}: {im.shape[0]}x{im.shape[1]} ' # string
|
653 |
+
if pred.shape[0]:
|
654 |
+
for c in pred[:, -1].unique():
|
655 |
+
n = (pred[:, -1] == c).sum() # detections per class
|
656 |
+
s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
|
657 |
+
if show or save or render or crop:
|
658 |
+
annotator = Annotator(im, example=str(self.names))
|
659 |
+
for *box, conf, cls in reversed(pred): # xyxy, confidence, class
|
660 |
+
label = f'{self.names[int(cls)]} {conf:.2f}'
|
661 |
+
if crop:
|
662 |
+
file = save_dir / 'crops' / self.names[int(cls)] / self.files[i] if save else None
|
663 |
+
crops.append({
|
664 |
+
'box': box,
|
665 |
+
'conf': conf,
|
666 |
+
'cls': cls,
|
667 |
+
'label': label,
|
668 |
+
'im': save_one_box(box, im, file=file, save=save)})
|
669 |
+
else: # all others
|
670 |
+
annotator.box_label(box, label if labels else '', color=colors(cls))
|
671 |
+
im = annotator.im
|
672 |
+
else:
|
673 |
+
s += '(no detections)'
|
674 |
+
|
675 |
+
im = Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im # from np
|
676 |
+
if pprint:
|
677 |
+
print(s.rstrip(', '))
|
678 |
+
if show:
|
679 |
+
im.show(self.files[i]) # show
|
680 |
+
if save:
|
681 |
+
f = self.files[i]
|
682 |
+
im.save(save_dir / f) # save
|
683 |
+
if i == self.n - 1:
|
684 |
+
LOGGER.info(f"Saved {self.n} image{'s' * (self.n > 1)} to {colorstr('bold', save_dir)}")
|
685 |
+
if render:
|
686 |
+
self.imgs[i] = np.asarray(im)
|
687 |
+
if crop:
|
688 |
+
if save:
|
689 |
+
LOGGER.info(f'Saved results to {save_dir}\n')
|
690 |
+
return crops
|
691 |
+
|
692 |
+
def print(self):
|
693 |
+
self.display(pprint=True) # print results
|
694 |
+
print(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {tuple(self.s)}' % self.t)
|
695 |
+
|
696 |
+
def show(self, labels=True):
|
697 |
+
self.display(show=True, labels=labels) # show results
|
698 |
+
|
699 |
+
def save(self, labels=True, save_dir='runs/detect/exp'):
|
700 |
+
save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/detect/exp', mkdir=True) # increment save_dir
|
701 |
+
self.display(save=True, labels=labels, save_dir=save_dir) # save results
|
702 |
+
|
703 |
+
def crop(self, save=True, save_dir='runs/detect/exp'):
|
704 |
+
save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/detect/exp', mkdir=True) if save else None
|
705 |
+
return self.display(crop=True, save=save, save_dir=save_dir) # crop results
|
706 |
+
|
707 |
+
def render(self, labels=True):
|
708 |
+
self.display(render=True, labels=labels) # render results
|
709 |
+
return self.imgs
|
710 |
+
|
711 |
+
def pandas(self):
|
712 |
+
# return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0])
|
713 |
+
new = copy(self) # return copy
|
714 |
+
ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns
|
715 |
+
cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns
|
716 |
+
for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]):
|
717 |
+
a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update
|
718 |
+
setattr(new, k, [pd.DataFrame(x, columns=c) for x in a])
|
719 |
+
return new
|
720 |
+
|
721 |
+
def tolist(self):
|
722 |
+
# return a list of Detections objects, i.e. 'for result in results.tolist():'
|
723 |
+
r = range(self.n) # iterable
|
724 |
+
x = [Detections([self.imgs[i]], [self.pred[i]], [self.files[i]], self.times, self.names, self.s) for i in r]
|
725 |
+
# for d in x:
|
726 |
+
# for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']:
|
727 |
+
# setattr(d, k, getattr(d, k)[0]) # pop out of list
|
728 |
+
return x
|
729 |
+
|
730 |
+
def __len__(self):
|
731 |
+
return self.n # override len(results)
|
732 |
+
|
733 |
+
def __str__(self):
|
734 |
+
self.print() # override print(results)
|
735 |
+
return ''
|
736 |
+
|
737 |
+
|
738 |
+
class Classify(nn.Module):
|
739 |
+
# Classification head, i.e. x(b,c1,20,20) to x(b,c2)
|
740 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups
|
741 |
+
super().__init__()
|
742 |
+
self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1)
|
743 |
+
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g) # to x(b,c2,1,1)
|
744 |
+
self.flat = nn.Flatten()
|
745 |
+
|
746 |
+
def forward(self, x):
|
747 |
+
z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list
|
748 |
+
return self.flat(self.conv(z)) # flatten to x(b,c2)
|
models/custom_yolov5s.yaml
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# parameters
|
3 |
+
nc: 1 # number of classes
|
4 |
+
depth_multiple: 0.33 # model depth multiple
|
5 |
+
width_multiple: 0.50 # layer channel multiple
|
6 |
+
|
7 |
+
# anchors
|
8 |
+
anchors:
|
9 |
+
- [10,13, 16,30, 33,23] # P3/8
|
10 |
+
- [30,61, 62,45, 59,119] # P4/16
|
11 |
+
- [116,90, 156,198, 373,326] # P5/32
|
12 |
+
|
13 |
+
# YOLOv5 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
16 |
+
[[-1, 1, Focus, [64, 3]], # 0-P1/2
|
17 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
18 |
+
[-1, 3, BottleneckCSP, [128]],
|
19 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
20 |
+
[-1, 9, BottleneckCSP, [256]],
|
21 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
22 |
+
[-1, 9, BottleneckCSP, [512]],
|
23 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
24 |
+
[-1, 1, SPP, [1024, [5, 9, 13]]],
|
25 |
+
[-1, 3, BottleneckCSP, [1024, False]], # 9
|
26 |
+
]
|
27 |
+
|
28 |
+
# YOLOv5 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, BottleneckCSP, [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, BottleneckCSP, [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, BottleneckCSP, [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, BottleneckCSP, [1024, False]], # 23 (P5/32-large)
|
47 |
+
|
48 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
49 |
+
]
|
models/experimental.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
2 |
+
"""
|
3 |
+
Experimental modules
|
4 |
+
"""
|
5 |
+
import math
|
6 |
+
|
7 |
+
import numpy as np
|
8 |
+
import torch
|
9 |
+
import torch.nn as nn
|
10 |
+
|
11 |
+
from models.common import Conv
|
12 |
+
from utils.downloads import attempt_download
|
13 |
+
|
14 |
+
|
15 |
+
class Sum(nn.Module):
|
16 |
+
# Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
|
17 |
+
def __init__(self, n, weight=False): # n: number of inputs
|
18 |
+
super().__init__()
|
19 |
+
self.weight = weight # apply weights boolean
|
20 |
+
self.iter = range(n - 1) # iter object
|
21 |
+
if weight:
|
22 |
+
self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights
|
23 |
+
|
24 |
+
def forward(self, x):
|
25 |
+
y = x[0] # no weight
|
26 |
+
if self.weight:
|
27 |
+
w = torch.sigmoid(self.w) * 2
|
28 |
+
for i in self.iter:
|
29 |
+
y = y + x[i + 1] * w[i]
|
30 |
+
else:
|
31 |
+
for i in self.iter:
|
32 |
+
y = y + x[i + 1]
|
33 |
+
return y
|
34 |
+
|
35 |
+
|
36 |
+
class MixConv2d(nn.Module):
|
37 |
+
# Mixed Depth-wise Conv https://arxiv.org/abs/1907.09595
|
38 |
+
def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): # ch_in, ch_out, kernel, stride, ch_strategy
|
39 |
+
super().__init__()
|
40 |
+
n = len(k) # number of convolutions
|
41 |
+
if equal_ch: # equal c_ per group
|
42 |
+
i = torch.linspace(0, n - 1E-6, c2).floor() # c2 indices
|
43 |
+
c_ = [(i == g).sum() for g in range(n)] # intermediate channels
|
44 |
+
else: # equal weight.numel() per group
|
45 |
+
b = [c2] + [0] * n
|
46 |
+
a = np.eye(n + 1, n, k=-1)
|
47 |
+
a -= np.roll(a, 1, axis=1)
|
48 |
+
a *= np.array(k) ** 2
|
49 |
+
a[0] = 1
|
50 |
+
c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
|
51 |
+
|
52 |
+
self.m = nn.ModuleList([
|
53 |
+
nn.Conv2d(c1, int(c_), k, s, k // 2, groups=math.gcd(c1, int(c_)), bias=False) for k, c_ in zip(k, c_)])
|
54 |
+
self.bn = nn.BatchNorm2d(c2)
|
55 |
+
self.act = nn.SiLU()
|
56 |
+
|
57 |
+
def forward(self, x):
|
58 |
+
return self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
|
59 |
+
|
60 |
+
|
61 |
+
class Ensemble(nn.ModuleList):
|
62 |
+
# Ensemble of models
|
63 |
+
def __init__(self):
|
64 |
+
super().__init__()
|
65 |
+
|
66 |
+
def forward(self, x, augment=False, profile=False, visualize=False):
|
67 |
+
y = [module(x, augment, profile, visualize)[0] for module in self]
|
68 |
+
# y = torch.stack(y).max(0)[0] # max ensemble
|
69 |
+
# y = torch.stack(y).mean(0) # mean ensemble
|
70 |
+
y = torch.cat(y, 1) # nms ensemble
|
71 |
+
return y, None # inference, train output
|
72 |
+
|
73 |
+
|
74 |
+
def attempt_load(weights, device=None, inplace=True, fuse=True):
|
75 |
+
# Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
|
76 |
+
from models.yolo import Detect, Model
|
77 |
+
|
78 |
+
model = Ensemble()
|
79 |
+
for w in weights if isinstance(weights, list) else [weights]:
|
80 |
+
ckpt = torch.load(attempt_download(w), map_location='cpu') # load
|
81 |
+
ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model
|
82 |
+
model.append(ckpt.fuse().eval() if fuse else ckpt.eval()) # fused or un-fused model in eval mode
|
83 |
+
|
84 |
+
# Compatibility updates
|
85 |
+
for m in model.modules():
|
86 |
+
t = type(m)
|
87 |
+
if t in (nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model):
|
88 |
+
m.inplace = inplace # torch 1.7.0 compatibility
|
89 |
+
if t is Detect and not isinstance(m.anchor_grid, list):
|
90 |
+
delattr(m, 'anchor_grid')
|
91 |
+
setattr(m, 'anchor_grid', [torch.zeros(1)] * m.nl)
|
92 |
+
elif t is Conv:
|
93 |
+
m._non_persistent_buffers_set = set() # torch 1.6.0 compatibility
|
94 |
+
elif t is nn.Upsample and not hasattr(m, 'recompute_scale_factor'):
|
95 |
+
m.recompute_scale_factor = None # torch 1.11.0 compatibility
|
96 |
+
|
97 |
+
if len(model) == 1:
|
98 |
+
return model[-1] # return model
|
99 |
+
print(f'Ensemble created with {weights}\n')
|
100 |
+
for k in 'names', 'nc', 'yaml':
|
101 |
+
setattr(model, k, getattr(model[0], k))
|
102 |
+
model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride
|
103 |
+
assert all(model[0].nc == m.nc for m in model), f'Models have different class counts: {[m.nc for m in model]}'
|
104 |
+
return model # return ensemble
|
models/hub/anchors.yaml
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
2 |
+
# Default anchors for COCO data
|
3 |
+
|
4 |
+
|
5 |
+
# P5 -------------------------------------------------------------------------------------------------------------------
|
6 |
+
# P5-640:
|
7 |
+
anchors_p5_640:
|
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 |
+
|
13 |
+
# P6 -------------------------------------------------------------------------------------------------------------------
|
14 |
+
# P6-640: thr=0.25: 0.9964 BPR, 5.54 anchors past thr, n=12, img_size=640, metric_all=0.281/0.716-mean/best, past_thr=0.469-mean: 9,11, 21,19, 17,41, 43,32, 39,70, 86,64, 65,131, 134,130, 120,265, 282,180, 247,354, 512,387
|
15 |
+
anchors_p6_640:
|
16 |
+
- [9,11, 21,19, 17,41] # P3/8
|
17 |
+
- [43,32, 39,70, 86,64] # P4/16
|
18 |
+
- [65,131, 134,130, 120,265] # P5/32
|
19 |
+
- [282,180, 247,354, 512,387] # P6/64
|
20 |
+
|
21 |
+
# P6-1280: thr=0.25: 0.9950 BPR, 5.55 anchors past thr, n=12, img_size=1280, metric_all=0.281/0.714-mean/best, past_thr=0.468-mean: 19,27, 44,40, 38,94, 96,68, 86,152, 180,137, 140,301, 303,264, 238,542, 436,615, 739,380, 925,792
|
22 |
+
anchors_p6_1280:
|
23 |
+
- [19,27, 44,40, 38,94] # P3/8
|
24 |
+
- [96,68, 86,152, 180,137] # P4/16
|
25 |
+
- [140,301, 303,264, 238,542] # P5/32
|
26 |
+
- [436,615, 739,380, 925,792] # P6/64
|
27 |
+
|
28 |
+
# P6-1920: thr=0.25: 0.9950 BPR, 5.55 anchors past thr, n=12, img_size=1920, metric_all=0.281/0.714-mean/best, past_thr=0.468-mean: 28,41, 67,59, 57,141, 144,103, 129,227, 270,205, 209,452, 455,396, 358,812, 653,922, 1109,570, 1387,1187
|
29 |
+
anchors_p6_1920:
|
30 |
+
- [28,41, 67,59, 57,141] # P3/8
|
31 |
+
- [144,103, 129,227, 270,205] # P4/16
|
32 |
+
- [209,452, 455,396, 358,812] # P5/32
|
33 |
+
- [653,922, 1109,570, 1387,1187] # P6/64
|
34 |
+
|
35 |
+
|
36 |
+
# P7 -------------------------------------------------------------------------------------------------------------------
|
37 |
+
# P7-640: thr=0.25: 0.9962 BPR, 6.76 anchors past thr, n=15, img_size=640, metric_all=0.275/0.733-mean/best, past_thr=0.466-mean: 11,11, 13,30, 29,20, 30,46, 61,38, 39,92, 78,80, 146,66, 79,163, 149,150, 321,143, 157,303, 257,402, 359,290, 524,372
|
38 |
+
anchors_p7_640:
|
39 |
+
- [11,11, 13,30, 29,20] # P3/8
|
40 |
+
- [30,46, 61,38, 39,92] # P4/16
|
41 |
+
- [78,80, 146,66, 79,163] # P5/32
|
42 |
+
- [149,150, 321,143, 157,303] # P6/64
|
43 |
+
- [257,402, 359,290, 524,372] # P7/128
|
44 |
+
|
45 |
+
# P7-1280: thr=0.25: 0.9968 BPR, 6.71 anchors past thr, n=15, img_size=1280, metric_all=0.273/0.732-mean/best, past_thr=0.463-mean: 19,22, 54,36, 32,77, 70,83, 138,71, 75,173, 165,159, 148,334, 375,151, 334,317, 251,626, 499,474, 750,326, 534,814, 1079,818
|
46 |
+
anchors_p7_1280:
|
47 |
+
- [19,22, 54,36, 32,77] # P3/8
|
48 |
+
- [70,83, 138,71, 75,173] # P4/16
|
49 |
+
- [165,159, 148,334, 375,151] # P5/32
|
50 |
+
- [334,317, 251,626, 499,474] # P6/64
|
51 |
+
- [750,326, 534,814, 1079,818] # P7/128
|
52 |
+
|
53 |
+
# P7-1920: thr=0.25: 0.9968 BPR, 6.71 anchors past thr, n=15, img_size=1920, metric_all=0.273/0.732-mean/best, past_thr=0.463-mean: 29,34, 81,55, 47,115, 105,124, 207,107, 113,259, 247,238, 222,500, 563,227, 501,476, 376,939, 749,711, 1126,489, 801,1222, 1618,1227
|
54 |
+
anchors_p7_1920:
|
55 |
+
- [29,34, 81,55, 47,115] # P3/8
|
56 |
+
- [105,124, 207,107, 113,259] # P4/16
|
57 |
+
- [247,238, 222,500, 563,227] # P5/32
|
58 |
+
- [501,476, 376,939, 749,711] # P6/64
|
59 |
+
- [1126,489, 801,1222, 1618,1227] # P7/128
|
models/hub/yolov3-spp.yaml
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
# darknet53 backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Conv, [32, 3, 1]], # 0
|
16 |
+
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
|
17 |
+
[-1, 1, Bottleneck, [64]],
|
18 |
+
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
|
19 |
+
[-1, 2, Bottleneck, [128]],
|
20 |
+
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
|
21 |
+
[-1, 8, Bottleneck, [256]],
|
22 |
+
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
|
23 |
+
[-1, 8, Bottleneck, [512]],
|
24 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
|
25 |
+
[-1, 4, Bottleneck, [1024]], # 10
|
26 |
+
]
|
27 |
+
|
28 |
+
# YOLOv3-SPP head
|
29 |
+
head:
|
30 |
+
[[-1, 1, Bottleneck, [1024, False]],
|
31 |
+
[-1, 1, SPP, [512, [5, 9, 13]]],
|
32 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
33 |
+
[-1, 1, Conv, [512, 1, 1]],
|
34 |
+
[-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large)
|
35 |
+
|
36 |
+
[-2, 1, Conv, [256, 1, 1]],
|
37 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
38 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P4
|
39 |
+
[-1, 1, Bottleneck, [512, False]],
|
40 |
+
[-1, 1, Bottleneck, [512, False]],
|
41 |
+
[-1, 1, Conv, [256, 1, 1]],
|
42 |
+
[-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium)
|
43 |
+
|
44 |
+
[-2, 1, Conv, [128, 1, 1]],
|
45 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
46 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P3
|
47 |
+
[-1, 1, Bottleneck, [256, False]],
|
48 |
+
[-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small)
|
49 |
+
|
50 |
+
[[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
51 |
+
]
|
models/hub/yolov3-tiny.yaml
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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,14, 23,27, 37,58] # P4/16
|
9 |
+
- [81,82, 135,169, 344,319] # P5/32
|
10 |
+
|
11 |
+
# YOLOv3-tiny backbone
|
12 |
+
backbone:
|
13 |
+
# [from, number, module, args]
|
14 |
+
[[-1, 1, Conv, [16, 3, 1]], # 0
|
15 |
+
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 1-P1/2
|
16 |
+
[-1, 1, Conv, [32, 3, 1]],
|
17 |
+
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 3-P2/4
|
18 |
+
[-1, 1, Conv, [64, 3, 1]],
|
19 |
+
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 5-P3/8
|
20 |
+
[-1, 1, Conv, [128, 3, 1]],
|
21 |
+
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 7-P4/16
|
22 |
+
[-1, 1, Conv, [256, 3, 1]],
|
23 |
+
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 9-P5/32
|
24 |
+
[-1, 1, Conv, [512, 3, 1]],
|
25 |
+
[-1, 1, nn.ZeroPad2d, [[0, 1, 0, 1]]], # 11
|
26 |
+
[-1, 1, nn.MaxPool2d, [2, 1, 0]], # 12
|
27 |
+
]
|
28 |
+
|
29 |
+
# YOLOv3-tiny head
|
30 |
+
head:
|
31 |
+
[[-1, 1, Conv, [1024, 3, 1]],
|
32 |
+
[-1, 1, Conv, [256, 1, 1]],
|
33 |
+
[-1, 1, Conv, [512, 3, 1]], # 15 (P5/32-large)
|
34 |
+
|
35 |
+
[-2, 1, Conv, [128, 1, 1]],
|
36 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
37 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P4
|
38 |
+
[-1, 1, Conv, [256, 3, 1]], # 19 (P4/16-medium)
|
39 |
+
|
40 |
+
[[19, 15], 1, Detect, [nc, anchors]], # Detect(P4, P5)
|
41 |
+
]
|
models/hub/yolov3.yaml
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
# darknet53 backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Conv, [32, 3, 1]], # 0
|
16 |
+
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
|
17 |
+
[-1, 1, Bottleneck, [64]],
|
18 |
+
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
|
19 |
+
[-1, 2, Bottleneck, [128]],
|
20 |
+
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
|
21 |
+
[-1, 8, Bottleneck, [256]],
|
22 |
+
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
|
23 |
+
[-1, 8, Bottleneck, [512]],
|
24 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
|
25 |
+
[-1, 4, Bottleneck, [1024]], # 10
|
26 |
+
]
|
27 |
+
|
28 |
+
# YOLOv3 head
|
29 |
+
head:
|
30 |
+
[[-1, 1, Bottleneck, [1024, False]],
|
31 |
+
[-1, 1, Conv, [512, 1, 1]],
|
32 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
33 |
+
[-1, 1, Conv, [512, 1, 1]],
|
34 |
+
[-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large)
|
35 |
+
|
36 |
+
[-2, 1, Conv, [256, 1, 1]],
|
37 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
38 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P4
|
39 |
+
[-1, 1, Bottleneck, [512, False]],
|
40 |
+
[-1, 1, Bottleneck, [512, False]],
|
41 |
+
[-1, 1, Conv, [256, 1, 1]],
|
42 |
+
[-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium)
|
43 |
+
|
44 |
+
[-2, 1, Conv, [128, 1, 1]],
|
45 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
46 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P3
|
47 |
+
[-1, 1, Bottleneck, [256, False]],
|
48 |
+
[-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small)
|
49 |
+
|
50 |
+
[[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
51 |
+
]
|
models/hub/yolov5-bifpn.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 BiFPN head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14, 6], 1, Concat, [1]], # cat P4 <--- BiFPN change
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/hub/yolov5-fpn.yaml
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 FPN head
|
28 |
+
head:
|
29 |
+
[[-1, 3, C3, [1024, False]], # 10 (P5/32-large)
|
30 |
+
|
31 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
32 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
33 |
+
[-1, 1, Conv, [512, 1, 1]],
|
34 |
+
[-1, 3, C3, [512, False]], # 14 (P4/16-medium)
|
35 |
+
|
36 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
37 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
38 |
+
[-1, 1, Conv, [256, 1, 1]],
|
39 |
+
[-1, 3, C3, [256, False]], # 18 (P3/8-small)
|
40 |
+
|
41 |
+
[[18, 14, 10], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
42 |
+
]
|
models/hub/yolov5-p2.yaml
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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: 3 # AutoAnchor evolves 3 anchors per P output layer
|
8 |
+
|
9 |
+
# YOLOv5 v6.0 backbone
|
10 |
+
backbone:
|
11 |
+
# [from, number, module, args]
|
12 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
13 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
14 |
+
[-1, 3, C3, [128]],
|
15 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
16 |
+
[-1, 6, C3, [256]],
|
17 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
18 |
+
[-1, 9, C3, [512]],
|
19 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
20 |
+
[-1, 3, C3, [1024]],
|
21 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
22 |
+
]
|
23 |
+
|
24 |
+
# YOLOv5 v6.0 head with (P2, P3, P4, P5) outputs
|
25 |
+
head:
|
26 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
27 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
28 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
29 |
+
[-1, 3, C3, [512, False]], # 13
|
30 |
+
|
31 |
+
[-1, 1, Conv, [256, 1, 1]],
|
32 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
33 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
34 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
35 |
+
|
36 |
+
[-1, 1, Conv, [128, 1, 1]],
|
37 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
38 |
+
[[-1, 2], 1, Concat, [1]], # cat backbone P2
|
39 |
+
[-1, 1, C3, [128, False]], # 21 (P2/4-xsmall)
|
40 |
+
|
41 |
+
[-1, 1, Conv, [128, 3, 2]],
|
42 |
+
[[-1, 18], 1, Concat, [1]], # cat head P3
|
43 |
+
[-1, 3, C3, [256, False]], # 24 (P3/8-small)
|
44 |
+
|
45 |
+
[-1, 1, Conv, [256, 3, 2]],
|
46 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
47 |
+
[-1, 3, C3, [512, False]], # 27 (P4/16-medium)
|
48 |
+
|
49 |
+
[-1, 1, Conv, [512, 3, 2]],
|
50 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
51 |
+
[-1, 3, C3, [1024, False]], # 30 (P5/32-large)
|
52 |
+
|
53 |
+
[[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(P2, P3, P4, P5)
|
54 |
+
]
|
models/hub/yolov5-p34.yaml
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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: 3 # AutoAnchor evolves 3 anchors per P output layer
|
8 |
+
|
9 |
+
# YOLOv5 v6.0 backbone
|
10 |
+
backbone:
|
11 |
+
# [from, number, module, args]
|
12 |
+
[ [ -1, 1, Conv, [ 64, 6, 2, 2 ] ], # 0-P1/2
|
13 |
+
[ -1, 1, Conv, [ 128, 3, 2 ] ], # 1-P2/4
|
14 |
+
[ -1, 3, C3, [ 128 ] ],
|
15 |
+
[ -1, 1, Conv, [ 256, 3, 2 ] ], # 3-P3/8
|
16 |
+
[ -1, 6, C3, [ 256 ] ],
|
17 |
+
[ -1, 1, Conv, [ 512, 3, 2 ] ], # 5-P4/16
|
18 |
+
[ -1, 9, C3, [ 512 ] ],
|
19 |
+
[ -1, 1, Conv, [ 1024, 3, 2 ] ], # 7-P5/32
|
20 |
+
[ -1, 3, C3, [ 1024 ] ],
|
21 |
+
[ -1, 1, SPPF, [ 1024, 5 ] ], # 9
|
22 |
+
]
|
23 |
+
|
24 |
+
# YOLOv5 v6.0 head with (P3, P4) outputs
|
25 |
+
head:
|
26 |
+
[ [ -1, 1, Conv, [ 512, 1, 1 ] ],
|
27 |
+
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
|
28 |
+
[ [ -1, 6 ], 1, Concat, [ 1 ] ], # cat backbone P4
|
29 |
+
[ -1, 3, C3, [ 512, False ] ], # 13
|
30 |
+
|
31 |
+
[ -1, 1, Conv, [ 256, 1, 1 ] ],
|
32 |
+
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
|
33 |
+
[ [ -1, 4 ], 1, Concat, [ 1 ] ], # cat backbone P3
|
34 |
+
[ -1, 3, C3, [ 256, False ] ], # 17 (P3/8-small)
|
35 |
+
|
36 |
+
[ -1, 1, Conv, [ 256, 3, 2 ] ],
|
37 |
+
[ [ -1, 14 ], 1, Concat, [ 1 ] ], # cat head P4
|
38 |
+
[ -1, 3, C3, [ 512, False ] ], # 20 (P4/16-medium)
|
39 |
+
|
40 |
+
[ [ 17, 20 ], 1, Detect, [ nc, anchors ] ], # Detect(P3, P4)
|
41 |
+
]
|
models/hub/yolov5-p6.yaml
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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: 3 # AutoAnchor evolves 3 anchors per P output layer
|
8 |
+
|
9 |
+
# YOLOv5 v6.0 backbone
|
10 |
+
backbone:
|
11 |
+
# [from, number, module, args]
|
12 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
13 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
14 |
+
[-1, 3, C3, [128]],
|
15 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
16 |
+
[-1, 6, C3, [256]],
|
17 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
18 |
+
[-1, 9, C3, [512]],
|
19 |
+
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
20 |
+
[-1, 3, C3, [768]],
|
21 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
22 |
+
[-1, 3, C3, [1024]],
|
23 |
+
[-1, 1, SPPF, [1024, 5]], # 11
|
24 |
+
]
|
25 |
+
|
26 |
+
# YOLOv5 v6.0 head with (P3, P4, P5, P6) outputs
|
27 |
+
head:
|
28 |
+
[[-1, 1, Conv, [768, 1, 1]],
|
29 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
30 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
31 |
+
[-1, 3, C3, [768, False]], # 15
|
32 |
+
|
33 |
+
[-1, 1, Conv, [512, 1, 1]],
|
34 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
35 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
36 |
+
[-1, 3, C3, [512, False]], # 19
|
37 |
+
|
38 |
+
[-1, 1, Conv, [256, 1, 1]],
|
39 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
40 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
41 |
+
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [256, 3, 2]],
|
44 |
+
[[-1, 20], 1, Concat, [1]], # cat head P4
|
45 |
+
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
46 |
+
|
47 |
+
[-1, 1, Conv, [512, 3, 2]],
|
48 |
+
[[-1, 16], 1, Concat, [1]], # cat head P5
|
49 |
+
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
50 |
+
|
51 |
+
[-1, 1, Conv, [768, 3, 2]],
|
52 |
+
[[-1, 12], 1, Concat, [1]], # cat head P6
|
53 |
+
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
54 |
+
|
55 |
+
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
56 |
+
]
|
models/hub/yolov5-p7.yaml
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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: 3 # AutoAnchor evolves 3 anchors per P output layer
|
8 |
+
|
9 |
+
# YOLOv5 v6.0 backbone
|
10 |
+
backbone:
|
11 |
+
# [from, number, module, args]
|
12 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
13 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
14 |
+
[-1, 3, C3, [128]],
|
15 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
16 |
+
[-1, 6, C3, [256]],
|
17 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
18 |
+
[-1, 9, C3, [512]],
|
19 |
+
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
20 |
+
[-1, 3, C3, [768]],
|
21 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
22 |
+
[-1, 3, C3, [1024]],
|
23 |
+
[-1, 1, Conv, [1280, 3, 2]], # 11-P7/128
|
24 |
+
[-1, 3, C3, [1280]],
|
25 |
+
[-1, 1, SPPF, [1280, 5]], # 13
|
26 |
+
]
|
27 |
+
|
28 |
+
# YOLOv5 v6.0 head with (P3, P4, P5, P6, P7) outputs
|
29 |
+
head:
|
30 |
+
[[-1, 1, Conv, [1024, 1, 1]],
|
31 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
32 |
+
[[-1, 10], 1, Concat, [1]], # cat backbone P6
|
33 |
+
[-1, 3, C3, [1024, False]], # 17
|
34 |
+
|
35 |
+
[-1, 1, Conv, [768, 1, 1]],
|
36 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
37 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
38 |
+
[-1, 3, C3, [768, False]], # 21
|
39 |
+
|
40 |
+
[-1, 1, Conv, [512, 1, 1]],
|
41 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
42 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
43 |
+
[-1, 3, C3, [512, False]], # 25
|
44 |
+
|
45 |
+
[-1, 1, Conv, [256, 1, 1]],
|
46 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
47 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
48 |
+
[-1, 3, C3, [256, False]], # 29 (P3/8-small)
|
49 |
+
|
50 |
+
[-1, 1, Conv, [256, 3, 2]],
|
51 |
+
[[-1, 26], 1, Concat, [1]], # cat head P4
|
52 |
+
[-1, 3, C3, [512, False]], # 32 (P4/16-medium)
|
53 |
+
|
54 |
+
[-1, 1, Conv, [512, 3, 2]],
|
55 |
+
[[-1, 22], 1, Concat, [1]], # cat head P5
|
56 |
+
[-1, 3, C3, [768, False]], # 35 (P5/32-large)
|
57 |
+
|
58 |
+
[-1, 1, Conv, [768, 3, 2]],
|
59 |
+
[[-1, 18], 1, Concat, [1]], # cat head P6
|
60 |
+
[-1, 3, C3, [1024, False]], # 38 (P6/64-xlarge)
|
61 |
+
|
62 |
+
[-1, 1, Conv, [1024, 3, 2]],
|
63 |
+
[[-1, 14], 1, Concat, [1]], # cat head P7
|
64 |
+
[-1, 3, C3, [1280, False]], # 41 (P7/128-xxlarge)
|
65 |
+
|
66 |
+
[[29, 32, 35, 38, 41], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6, P7)
|
67 |
+
]
|
models/hub/yolov5-panet.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 PANet head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/hub/yolov5l6.yaml
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
- [19,27, 44,40, 38,94] # P3/8
|
9 |
+
- [96,68, 86,152, 180,137] # P4/16
|
10 |
+
- [140,301, 303,264, 238,542] # P5/32
|
11 |
+
- [436,615, 739,380, 925,792] # P6/64
|
12 |
+
|
13 |
+
# YOLOv5 v6.0 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
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, [768, 3, 2]], # 7-P5/32
|
24 |
+
[-1, 3, C3, [768]],
|
25 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
26 |
+
[-1, 3, C3, [1024]],
|
27 |
+
[-1, 1, SPPF, [1024, 5]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# YOLOv5 v6.0 head
|
31 |
+
head:
|
32 |
+
[[-1, 1, Conv, [768, 1, 1]],
|
33 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
34 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
35 |
+
[-1, 3, C3, [768, False]], # 15
|
36 |
+
|
37 |
+
[-1, 1, Conv, [512, 1, 1]],
|
38 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
39 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
40 |
+
[-1, 3, C3, [512, False]], # 19
|
41 |
+
|
42 |
+
[-1, 1, Conv, [256, 1, 1]],
|
43 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
44 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
45 |
+
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
46 |
+
|
47 |
+
[-1, 1, Conv, [256, 3, 2]],
|
48 |
+
[[-1, 20], 1, Concat, [1]], # cat head P4
|
49 |
+
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
50 |
+
|
51 |
+
[-1, 1, Conv, [512, 3, 2]],
|
52 |
+
[[-1, 16], 1, Concat, [1]], # cat head P5
|
53 |
+
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
54 |
+
|
55 |
+
[-1, 1, Conv, [768, 3, 2]],
|
56 |
+
[[-1, 12], 1, Concat, [1]], # cat head P6
|
57 |
+
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
58 |
+
|
59 |
+
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
60 |
+
]
|
models/hub/yolov5m6.yaml
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
- [19,27, 44,40, 38,94] # P3/8
|
9 |
+
- [96,68, 86,152, 180,137] # P4/16
|
10 |
+
- [140,301, 303,264, 238,542] # P5/32
|
11 |
+
- [436,615, 739,380, 925,792] # P6/64
|
12 |
+
|
13 |
+
# YOLOv5 v6.0 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
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, [768, 3, 2]], # 7-P5/32
|
24 |
+
[-1, 3, C3, [768]],
|
25 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
26 |
+
[-1, 3, C3, [1024]],
|
27 |
+
[-1, 1, SPPF, [1024, 5]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# YOLOv5 v6.0 head
|
31 |
+
head:
|
32 |
+
[[-1, 1, Conv, [768, 1, 1]],
|
33 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
34 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
35 |
+
[-1, 3, C3, [768, False]], # 15
|
36 |
+
|
37 |
+
[-1, 1, Conv, [512, 1, 1]],
|
38 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
39 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
40 |
+
[-1, 3, C3, [512, False]], # 19
|
41 |
+
|
42 |
+
[-1, 1, Conv, [256, 1, 1]],
|
43 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
44 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
45 |
+
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
46 |
+
|
47 |
+
[-1, 1, Conv, [256, 3, 2]],
|
48 |
+
[[-1, 20], 1, Concat, [1]], # cat head P4
|
49 |
+
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
50 |
+
|
51 |
+
[-1, 1, Conv, [512, 3, 2]],
|
52 |
+
[[-1, 16], 1, Concat, [1]], # cat head P5
|
53 |
+
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
54 |
+
|
55 |
+
[-1, 1, Conv, [768, 3, 2]],
|
56 |
+
[[-1, 12], 1, Concat, [1]], # cat head P6
|
57 |
+
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
58 |
+
|
59 |
+
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
60 |
+
]
|
models/hub/yolov5n6.yaml
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
- [19,27, 44,40, 38,94] # P3/8
|
9 |
+
- [96,68, 86,152, 180,137] # P4/16
|
10 |
+
- [140,301, 303,264, 238,542] # P5/32
|
11 |
+
- [436,615, 739,380, 925,792] # P6/64
|
12 |
+
|
13 |
+
# YOLOv5 v6.0 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
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, [768, 3, 2]], # 7-P5/32
|
24 |
+
[-1, 3, C3, [768]],
|
25 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
26 |
+
[-1, 3, C3, [1024]],
|
27 |
+
[-1, 1, SPPF, [1024, 5]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# YOLOv5 v6.0 head
|
31 |
+
head:
|
32 |
+
[[-1, 1, Conv, [768, 1, 1]],
|
33 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
34 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
35 |
+
[-1, 3, C3, [768, False]], # 15
|
36 |
+
|
37 |
+
[-1, 1, Conv, [512, 1, 1]],
|
38 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
39 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
40 |
+
[-1, 3, C3, [512, False]], # 19
|
41 |
+
|
42 |
+
[-1, 1, Conv, [256, 1, 1]],
|
43 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
44 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
45 |
+
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
46 |
+
|
47 |
+
[-1, 1, Conv, [256, 3, 2]],
|
48 |
+
[[-1, 20], 1, Concat, [1]], # cat head P4
|
49 |
+
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
50 |
+
|
51 |
+
[-1, 1, Conv, [512, 3, 2]],
|
52 |
+
[[-1, 16], 1, Concat, [1]], # cat head P5
|
53 |
+
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
54 |
+
|
55 |
+
[-1, 1, Conv, [768, 3, 2]],
|
56 |
+
[[-1, 12], 1, Concat, [1]], # cat head P6
|
57 |
+
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
58 |
+
|
59 |
+
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
60 |
+
]
|
models/hub/yolov5s-ghost.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, GhostConv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3Ghost, [128]],
|
18 |
+
[-1, 1, GhostConv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3Ghost, [256]],
|
20 |
+
[-1, 1, GhostConv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3Ghost, [512]],
|
22 |
+
[-1, 1, GhostConv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3Ghost, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 head
|
28 |
+
head:
|
29 |
+
[[-1, 1, GhostConv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3Ghost, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, GhostConv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3Ghost, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, GhostConv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3Ghost, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, GhostConv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3Ghost, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/hub/yolov5s-transformer.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3TR, [1024]], # 9 <--- C3TR() Transformer module
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/hub/yolov5s6.yaml
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
- [19,27, 44,40, 38,94] # P3/8
|
9 |
+
- [96,68, 86,152, 180,137] # P4/16
|
10 |
+
- [140,301, 303,264, 238,542] # P5/32
|
11 |
+
- [436,615, 739,380, 925,792] # P6/64
|
12 |
+
|
13 |
+
# YOLOv5 v6.0 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
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, [768, 3, 2]], # 7-P5/32
|
24 |
+
[-1, 3, C3, [768]],
|
25 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
26 |
+
[-1, 3, C3, [1024]],
|
27 |
+
[-1, 1, SPPF, [1024, 5]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# YOLOv5 v6.0 head
|
31 |
+
head:
|
32 |
+
[[-1, 1, Conv, [768, 1, 1]],
|
33 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
34 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
35 |
+
[-1, 3, C3, [768, False]], # 15
|
36 |
+
|
37 |
+
[-1, 1, Conv, [512, 1, 1]],
|
38 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
39 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
40 |
+
[-1, 3, C3, [512, False]], # 19
|
41 |
+
|
42 |
+
[-1, 1, Conv, [256, 1, 1]],
|
43 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
44 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
45 |
+
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
46 |
+
|
47 |
+
[-1, 1, Conv, [256, 3, 2]],
|
48 |
+
[[-1, 20], 1, Concat, [1]], # cat head P4
|
49 |
+
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
50 |
+
|
51 |
+
[-1, 1, Conv, [512, 3, 2]],
|
52 |
+
[[-1, 16], 1, Concat, [1]], # cat head P5
|
53 |
+
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
54 |
+
|
55 |
+
[-1, 1, Conv, [768, 3, 2]],
|
56 |
+
[[-1, 12], 1, Concat, [1]], # cat head P6
|
57 |
+
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
58 |
+
|
59 |
+
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
60 |
+
]
|
models/hub/yolov5x6.yaml
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
2 |
+
|
3 |
+
# Parameters
|
4 |
+
nc: 80 # number of classes
|
5 |
+
depth_multiple: 1.33 # model depth multiple
|
6 |
+
width_multiple: 1.25 # layer channel multiple
|
7 |
+
anchors:
|
8 |
+
- [19,27, 44,40, 38,94] # P3/8
|
9 |
+
- [96,68, 86,152, 180,137] # P4/16
|
10 |
+
- [140,301, 303,264, 238,542] # P5/32
|
11 |
+
- [436,615, 739,380, 925,792] # P6/64
|
12 |
+
|
13 |
+
# YOLOv5 v6.0 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
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, [768, 3, 2]], # 7-P5/32
|
24 |
+
[-1, 3, C3, [768]],
|
25 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
26 |
+
[-1, 3, C3, [1024]],
|
27 |
+
[-1, 1, SPPF, [1024, 5]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# YOLOv5 v6.0 head
|
31 |
+
head:
|
32 |
+
[[-1, 1, Conv, [768, 1, 1]],
|
33 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
34 |
+
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
35 |
+
[-1, 3, C3, [768, False]], # 15
|
36 |
+
|
37 |
+
[-1, 1, Conv, [512, 1, 1]],
|
38 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
39 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
40 |
+
[-1, 3, C3, [512, False]], # 19
|
41 |
+
|
42 |
+
[-1, 1, Conv, [256, 1, 1]],
|
43 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
44 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
45 |
+
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
46 |
+
|
47 |
+
[-1, 1, Conv, [256, 3, 2]],
|
48 |
+
[[-1, 20], 1, Concat, [1]], # cat head P4
|
49 |
+
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
50 |
+
|
51 |
+
[-1, 1, Conv, [512, 3, 2]],
|
52 |
+
[[-1, 16], 1, Concat, [1]], # cat head P5
|
53 |
+
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
54 |
+
|
55 |
+
[-1, 1, Conv, [768, 3, 2]],
|
56 |
+
[[-1, 12], 1, Concat, [1]], # cat head P6
|
57 |
+
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
58 |
+
|
59 |
+
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
60 |
+
]
|
models/tf.py
ADDED
@@ -0,0 +1,574 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
2 |
+
"""
|
3 |
+
TensorFlow, Keras and TFLite versions of YOLOv5
|
4 |
+
Authored by https://github.com/zldrobit in PR https://github.com/ultralytics/yolov5/pull/1127
|
5 |
+
|
6 |
+
Usage:
|
7 |
+
$ python models/tf.py --weights yolov5s.pt
|
8 |
+
|
9 |
+
Export:
|
10 |
+
$ python path/to/export.py --weights yolov5s.pt --include saved_model pb tflite tfjs
|
11 |
+
"""
|
12 |
+
|
13 |
+
import argparse
|
14 |
+
import sys
|
15 |
+
from copy import deepcopy
|
16 |
+
from pathlib import Path
|
17 |
+
|
18 |
+
FILE = Path(__file__).resolve()
|
19 |
+
ROOT = FILE.parents[1] # YOLOv5 root directory
|
20 |
+
if str(ROOT) not in sys.path:
|
21 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
22 |
+
# ROOT = ROOT.relative_to(Path.cwd()) # relative
|
23 |
+
|
24 |
+
import numpy as np
|
25 |
+
import tensorflow as tf
|
26 |
+
import torch
|
27 |
+
import torch.nn as nn
|
28 |
+
from tensorflow import keras
|
29 |
+
|
30 |
+
from models.common import (C3, SPP, SPPF, Bottleneck, BottleneckCSP, C3x, Concat, Conv, CrossConv, DWConv,
|
31 |
+
DWConvTranspose2d, Focus, autopad)
|
32 |
+
from models.experimental import MixConv2d, attempt_load
|
33 |
+
from models.yolo import Detect
|
34 |
+
from utils.activations import SiLU
|
35 |
+
from utils.general import LOGGER, make_divisible, print_args
|
36 |
+
|
37 |
+
|
38 |
+
class TFBN(keras.layers.Layer):
|
39 |
+
# TensorFlow BatchNormalization wrapper
|
40 |
+
def __init__(self, w=None):
|
41 |
+
super().__init__()
|
42 |
+
self.bn = keras.layers.BatchNormalization(
|
43 |
+
beta_initializer=keras.initializers.Constant(w.bias.numpy()),
|
44 |
+
gamma_initializer=keras.initializers.Constant(w.weight.numpy()),
|
45 |
+
moving_mean_initializer=keras.initializers.Constant(w.running_mean.numpy()),
|
46 |
+
moving_variance_initializer=keras.initializers.Constant(w.running_var.numpy()),
|
47 |
+
epsilon=w.eps)
|
48 |
+
|
49 |
+
def call(self, inputs):
|
50 |
+
return self.bn(inputs)
|
51 |
+
|
52 |
+
|
53 |
+
class TFPad(keras.layers.Layer):
|
54 |
+
# Pad inputs in spatial dimensions 1 and 2
|
55 |
+
def __init__(self, pad):
|
56 |
+
super().__init__()
|
57 |
+
if isinstance(pad, int):
|
58 |
+
self.pad = tf.constant([[0, 0], [pad, pad], [pad, pad], [0, 0]])
|
59 |
+
else: # tuple/list
|
60 |
+
self.pad = tf.constant([[0, 0], [pad[0], pad[0]], [pad[1], pad[1]], [0, 0]])
|
61 |
+
|
62 |
+
def call(self, inputs):
|
63 |
+
return tf.pad(inputs, self.pad, mode='constant', constant_values=0)
|
64 |
+
|
65 |
+
|
66 |
+
class TFConv(keras.layers.Layer):
|
67 |
+
# Standard convolution
|
68 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None):
|
69 |
+
# ch_in, ch_out, weights, kernel, stride, padding, groups
|
70 |
+
super().__init__()
|
71 |
+
assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument"
|
72 |
+
# TensorFlow convolution padding is inconsistent with PyTorch (e.g. k=3 s=2 'SAME' padding)
|
73 |
+
# see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch
|
74 |
+
conv = keras.layers.Conv2D(
|
75 |
+
filters=c2,
|
76 |
+
kernel_size=k,
|
77 |
+
strides=s,
|
78 |
+
padding='SAME' if s == 1 else 'VALID',
|
79 |
+
use_bias=not hasattr(w, 'bn'),
|
80 |
+
kernel_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()),
|
81 |
+
bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy()))
|
82 |
+
self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
|
83 |
+
self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
|
84 |
+
self.act = activations(w.act) if act else tf.identity
|
85 |
+
|
86 |
+
def call(self, inputs):
|
87 |
+
return self.act(self.bn(self.conv(inputs)))
|
88 |
+
|
89 |
+
|
90 |
+
class TFDWConv(keras.layers.Layer):
|
91 |
+
# Depthwise convolution
|
92 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, act=True, w=None):
|
93 |
+
# ch_in, ch_out, weights, kernel, stride, padding, groups
|
94 |
+
super().__init__()
|
95 |
+
assert c2 % c1 == 0, f'TFDWConv() output={c2} must be a multiple of input={c1} channels'
|
96 |
+
conv = keras.layers.DepthwiseConv2D(
|
97 |
+
kernel_size=k,
|
98 |
+
depth_multiplier=c2 // c1,
|
99 |
+
strides=s,
|
100 |
+
padding='SAME' if s == 1 else 'VALID',
|
101 |
+
use_bias=not hasattr(w, 'bn'),
|
102 |
+
depthwise_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()),
|
103 |
+
bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy()))
|
104 |
+
self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
|
105 |
+
self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
|
106 |
+
self.act = activations(w.act) if act else tf.identity
|
107 |
+
|
108 |
+
def call(self, inputs):
|
109 |
+
return self.act(self.bn(self.conv(inputs)))
|
110 |
+
|
111 |
+
|
112 |
+
class TFDWConvTranspose2d(keras.layers.Layer):
|
113 |
+
# Depthwise ConvTranspose2d
|
114 |
+
def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0, w=None):
|
115 |
+
# ch_in, ch_out, weights, kernel, stride, padding, groups
|
116 |
+
super().__init__()
|
117 |
+
assert c1 == c2, f'TFDWConv() output={c2} must be equal to input={c1} channels'
|
118 |
+
assert k == 4 and p1 == 1, 'TFDWConv() only valid for k=4 and p1=1'
|
119 |
+
weight, bias = w.weight.permute(2, 3, 1, 0).numpy(), w.bias.numpy()
|
120 |
+
self.c1 = c1
|
121 |
+
self.conv = [
|
122 |
+
keras.layers.Conv2DTranspose(filters=1,
|
123 |
+
kernel_size=k,
|
124 |
+
strides=s,
|
125 |
+
padding='VALID',
|
126 |
+
output_padding=p2,
|
127 |
+
use_bias=True,
|
128 |
+
kernel_initializer=keras.initializers.Constant(weight[..., i:i + 1]),
|
129 |
+
bias_initializer=keras.initializers.Constant(bias[i])) for i in range(c1)]
|
130 |
+
|
131 |
+
def call(self, inputs):
|
132 |
+
return tf.concat([m(x) for m, x in zip(self.conv, tf.split(inputs, self.c1, 3))], 3)[:, 1:-1, 1:-1]
|
133 |
+
|
134 |
+
|
135 |
+
class TFFocus(keras.layers.Layer):
|
136 |
+
# Focus wh information into c-space
|
137 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None):
|
138 |
+
# ch_in, ch_out, kernel, stride, padding, groups
|
139 |
+
super().__init__()
|
140 |
+
self.conv = TFConv(c1 * 4, c2, k, s, p, g, act, w.conv)
|
141 |
+
|
142 |
+
def call(self, inputs): # x(b,w,h,c) -> y(b,w/2,h/2,4c)
|
143 |
+
# inputs = inputs / 255 # normalize 0-255 to 0-1
|
144 |
+
inputs = [inputs[:, ::2, ::2, :], inputs[:, 1::2, ::2, :], inputs[:, ::2, 1::2, :], inputs[:, 1::2, 1::2, :]]
|
145 |
+
return self.conv(tf.concat(inputs, 3))
|
146 |
+
|
147 |
+
|
148 |
+
class TFBottleneck(keras.layers.Layer):
|
149 |
+
# Standard bottleneck
|
150 |
+
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5, w=None): # ch_in, ch_out, shortcut, groups, expansion
|
151 |
+
super().__init__()
|
152 |
+
c_ = int(c2 * e) # hidden channels
|
153 |
+
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
154 |
+
self.cv2 = TFConv(c_, c2, 3, 1, g=g, w=w.cv2)
|
155 |
+
self.add = shortcut and c1 == c2
|
156 |
+
|
157 |
+
def call(self, inputs):
|
158 |
+
return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs))
|
159 |
+
|
160 |
+
|
161 |
+
class TFCrossConv(keras.layers.Layer):
|
162 |
+
# Cross Convolution
|
163 |
+
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False, w=None):
|
164 |
+
super().__init__()
|
165 |
+
c_ = int(c2 * e) # hidden channels
|
166 |
+
self.cv1 = TFConv(c1, c_, (1, k), (1, s), w=w.cv1)
|
167 |
+
self.cv2 = TFConv(c_, c2, (k, 1), (s, 1), g=g, w=w.cv2)
|
168 |
+
self.add = shortcut and c1 == c2
|
169 |
+
|
170 |
+
def call(self, inputs):
|
171 |
+
return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs))
|
172 |
+
|
173 |
+
|
174 |
+
class TFConv2d(keras.layers.Layer):
|
175 |
+
# Substitution for PyTorch nn.Conv2D
|
176 |
+
def __init__(self, c1, c2, k, s=1, g=1, bias=True, w=None):
|
177 |
+
super().__init__()
|
178 |
+
assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument"
|
179 |
+
self.conv = keras.layers.Conv2D(filters=c2,
|
180 |
+
kernel_size=k,
|
181 |
+
strides=s,
|
182 |
+
padding='VALID',
|
183 |
+
use_bias=bias,
|
184 |
+
kernel_initializer=keras.initializers.Constant(
|
185 |
+
w.weight.permute(2, 3, 1, 0).numpy()),
|
186 |
+
bias_initializer=keras.initializers.Constant(w.bias.numpy()) if bias else None)
|
187 |
+
|
188 |
+
def call(self, inputs):
|
189 |
+
return self.conv(inputs)
|
190 |
+
|
191 |
+
|
192 |
+
class TFBottleneckCSP(keras.layers.Layer):
|
193 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
194 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None):
|
195 |
+
# ch_in, ch_out, number, shortcut, groups, expansion
|
196 |
+
super().__init__()
|
197 |
+
c_ = int(c2 * e) # hidden channels
|
198 |
+
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
199 |
+
self.cv2 = TFConv2d(c1, c_, 1, 1, bias=False, w=w.cv2)
|
200 |
+
self.cv3 = TFConv2d(c_, c_, 1, 1, bias=False, w=w.cv3)
|
201 |
+
self.cv4 = TFConv(2 * c_, c2, 1, 1, w=w.cv4)
|
202 |
+
self.bn = TFBN(w.bn)
|
203 |
+
self.act = lambda x: keras.activations.swish(x)
|
204 |
+
self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)])
|
205 |
+
|
206 |
+
def call(self, inputs):
|
207 |
+
y1 = self.cv3(self.m(self.cv1(inputs)))
|
208 |
+
y2 = self.cv2(inputs)
|
209 |
+
return self.cv4(self.act(self.bn(tf.concat((y1, y2), axis=3))))
|
210 |
+
|
211 |
+
|
212 |
+
class TFC3(keras.layers.Layer):
|
213 |
+
# CSP Bottleneck with 3 convolutions
|
214 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None):
|
215 |
+
# ch_in, ch_out, number, shortcut, groups, expansion
|
216 |
+
super().__init__()
|
217 |
+
c_ = int(c2 * e) # hidden channels
|
218 |
+
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
219 |
+
self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2)
|
220 |
+
self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3)
|
221 |
+
self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)])
|
222 |
+
|
223 |
+
def call(self, inputs):
|
224 |
+
return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3))
|
225 |
+
|
226 |
+
|
227 |
+
class TFC3x(keras.layers.Layer):
|
228 |
+
# 3 module with cross-convolutions
|
229 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None):
|
230 |
+
# ch_in, ch_out, number, shortcut, groups, expansion
|
231 |
+
super().__init__()
|
232 |
+
c_ = int(c2 * e) # hidden channels
|
233 |
+
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
234 |
+
self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2)
|
235 |
+
self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3)
|
236 |
+
self.m = keras.Sequential([
|
237 |
+
TFCrossConv(c_, c_, k=3, s=1, g=g, e=1.0, shortcut=shortcut, w=w.m[j]) for j in range(n)])
|
238 |
+
|
239 |
+
def call(self, inputs):
|
240 |
+
return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3))
|
241 |
+
|
242 |
+
|
243 |
+
class TFSPP(keras.layers.Layer):
|
244 |
+
# Spatial pyramid pooling layer used in YOLOv3-SPP
|
245 |
+
def __init__(self, c1, c2, k=(5, 9, 13), w=None):
|
246 |
+
super().__init__()
|
247 |
+
c_ = c1 // 2 # hidden channels
|
248 |
+
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
249 |
+
self.cv2 = TFConv(c_ * (len(k) + 1), c2, 1, 1, w=w.cv2)
|
250 |
+
self.m = [keras.layers.MaxPool2D(pool_size=x, strides=1, padding='SAME') for x in k]
|
251 |
+
|
252 |
+
def call(self, inputs):
|
253 |
+
x = self.cv1(inputs)
|
254 |
+
return self.cv2(tf.concat([x] + [m(x) for m in self.m], 3))
|
255 |
+
|
256 |
+
|
257 |
+
class TFSPPF(keras.layers.Layer):
|
258 |
+
# Spatial pyramid pooling-Fast layer
|
259 |
+
def __init__(self, c1, c2, k=5, w=None):
|
260 |
+
super().__init__()
|
261 |
+
c_ = c1 // 2 # hidden channels
|
262 |
+
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
263 |
+
self.cv2 = TFConv(c_ * 4, c2, 1, 1, w=w.cv2)
|
264 |
+
self.m = keras.layers.MaxPool2D(pool_size=k, strides=1, padding='SAME')
|
265 |
+
|
266 |
+
def call(self, inputs):
|
267 |
+
x = self.cv1(inputs)
|
268 |
+
y1 = self.m(x)
|
269 |
+
y2 = self.m(y1)
|
270 |
+
return self.cv2(tf.concat([x, y1, y2, self.m(y2)], 3))
|
271 |
+
|
272 |
+
|
273 |
+
class TFDetect(keras.layers.Layer):
|
274 |
+
# TF YOLOv5 Detect layer
|
275 |
+
def __init__(self, nc=80, anchors=(), ch=(), imgsz=(640, 640), w=None): # detection layer
|
276 |
+
super().__init__()
|
277 |
+
self.stride = tf.convert_to_tensor(w.stride.numpy(), dtype=tf.float32)
|
278 |
+
self.nc = nc # number of classes
|
279 |
+
self.no = nc + 5 # number of outputs per anchor
|
280 |
+
self.nl = len(anchors) # number of detection layers
|
281 |
+
self.na = len(anchors[0]) // 2 # number of anchors
|
282 |
+
self.grid = [tf.zeros(1)] * self.nl # init grid
|
283 |
+
self.anchors = tf.convert_to_tensor(w.anchors.numpy(), dtype=tf.float32)
|
284 |
+
self.anchor_grid = tf.reshape(self.anchors * tf.reshape(self.stride, [self.nl, 1, 1]), [self.nl, 1, -1, 1, 2])
|
285 |
+
self.m = [TFConv2d(x, self.no * self.na, 1, w=w.m[i]) for i, x in enumerate(ch)]
|
286 |
+
self.training = False # set to False after building model
|
287 |
+
self.imgsz = imgsz
|
288 |
+
for i in range(self.nl):
|
289 |
+
ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i]
|
290 |
+
self.grid[i] = self._make_grid(nx, ny)
|
291 |
+
|
292 |
+
def call(self, inputs):
|
293 |
+
z = [] # inference output
|
294 |
+
x = []
|
295 |
+
for i in range(self.nl):
|
296 |
+
x.append(self.m[i](inputs[i]))
|
297 |
+
# x(bs,20,20,255) to x(bs,3,20,20,85)
|
298 |
+
ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i]
|
299 |
+
x[i] = tf.reshape(x[i], [-1, ny * nx, self.na, self.no])
|
300 |
+
|
301 |
+
if not self.training: # inference
|
302 |
+
y = tf.sigmoid(x[i])
|
303 |
+
grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) - 0.5
|
304 |
+
anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) * 4
|
305 |
+
xy = (y[..., 0:2] * 2 + grid) * self.stride[i] # xy
|
306 |
+
wh = y[..., 2:4] ** 2 * anchor_grid
|
307 |
+
# Normalize xywh to 0-1 to reduce calibration error
|
308 |
+
xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
|
309 |
+
wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
|
310 |
+
y = tf.concat([xy, wh, y[..., 4:]], -1)
|
311 |
+
z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no]))
|
312 |
+
|
313 |
+
return tf.transpose(x, [0, 2, 1, 3]) if self.training else (tf.concat(z, 1), x)
|
314 |
+
|
315 |
+
@staticmethod
|
316 |
+
def _make_grid(nx=20, ny=20):
|
317 |
+
# yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
|
318 |
+
# return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
|
319 |
+
xv, yv = tf.meshgrid(tf.range(nx), tf.range(ny))
|
320 |
+
return tf.cast(tf.reshape(tf.stack([xv, yv], 2), [1, 1, ny * nx, 2]), dtype=tf.float32)
|
321 |
+
|
322 |
+
|
323 |
+
class TFUpsample(keras.layers.Layer):
|
324 |
+
# TF version of torch.nn.Upsample()
|
325 |
+
def __init__(self, size, scale_factor, mode, w=None): # warning: all arguments needed including 'w'
|
326 |
+
super().__init__()
|
327 |
+
assert scale_factor == 2, "scale_factor must be 2"
|
328 |
+
self.upsample = lambda x: tf.image.resize(x, (x.shape[1] * 2, x.shape[2] * 2), method=mode)
|
329 |
+
# self.upsample = keras.layers.UpSampling2D(size=scale_factor, interpolation=mode)
|
330 |
+
# with default arguments: align_corners=False, half_pixel_centers=False
|
331 |
+
# self.upsample = lambda x: tf.raw_ops.ResizeNearestNeighbor(images=x,
|
332 |
+
# size=(x.shape[1] * 2, x.shape[2] * 2))
|
333 |
+
|
334 |
+
def call(self, inputs):
|
335 |
+
return self.upsample(inputs)
|
336 |
+
|
337 |
+
|
338 |
+
class TFConcat(keras.layers.Layer):
|
339 |
+
# TF version of torch.concat()
|
340 |
+
def __init__(self, dimension=1, w=None):
|
341 |
+
super().__init__()
|
342 |
+
assert dimension == 1, "convert only NCHW to NHWC concat"
|
343 |
+
self.d = 3
|
344 |
+
|
345 |
+
def call(self, inputs):
|
346 |
+
return tf.concat(inputs, self.d)
|
347 |
+
|
348 |
+
|
349 |
+
def parse_model(d, ch, model, imgsz): # model_dict, input_channels(3)
|
350 |
+
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
|
351 |
+
anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple']
|
352 |
+
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
|
353 |
+
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
|
354 |
+
|
355 |
+
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
|
356 |
+
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args
|
357 |
+
m_str = m
|
358 |
+
m = eval(m) if isinstance(m, str) else m # eval strings
|
359 |
+
for j, a in enumerate(args):
|
360 |
+
try:
|
361 |
+
args[j] = eval(a) if isinstance(a, str) else a # eval strings
|
362 |
+
except NameError:
|
363 |
+
pass
|
364 |
+
|
365 |
+
n = max(round(n * gd), 1) if n > 1 else n # depth gain
|
366 |
+
if m in [
|
367 |
+
nn.Conv2d, Conv, DWConv, DWConvTranspose2d, Bottleneck, SPP, SPPF, MixConv2d, Focus, CrossConv,
|
368 |
+
BottleneckCSP, C3, C3x]:
|
369 |
+
c1, c2 = ch[f], args[0]
|
370 |
+
c2 = make_divisible(c2 * gw, 8) if c2 != no else c2
|
371 |
+
|
372 |
+
args = [c1, c2, *args[1:]]
|
373 |
+
if m in [BottleneckCSP, C3, C3x]:
|
374 |
+
args.insert(2, n)
|
375 |
+
n = 1
|
376 |
+
elif m is nn.BatchNorm2d:
|
377 |
+
args = [ch[f]]
|
378 |
+
elif m is Concat:
|
379 |
+
c2 = sum(ch[-1 if x == -1 else x + 1] for x in f)
|
380 |
+
elif m is Detect:
|
381 |
+
args.append([ch[x + 1] for x in f])
|
382 |
+
if isinstance(args[1], int): # number of anchors
|
383 |
+
args[1] = [list(range(args[1] * 2))] * len(f)
|
384 |
+
args.append(imgsz)
|
385 |
+
else:
|
386 |
+
c2 = ch[f]
|
387 |
+
|
388 |
+
tf_m = eval('TF' + m_str.replace('nn.', ''))
|
389 |
+
m_ = keras.Sequential([tf_m(*args, w=model.model[i][j]) for j in range(n)]) if n > 1 \
|
390 |
+
else tf_m(*args, w=model.model[i]) # module
|
391 |
+
|
392 |
+
torch_m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
|
393 |
+
t = str(m)[8:-2].replace('__main__.', '') # module type
|
394 |
+
np = sum(x.numel() for x in torch_m_.parameters()) # number params
|
395 |
+
m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
|
396 |
+
LOGGER.info(f'{i:>3}{str(f):>18}{str(n):>3}{np:>10} {t:<40}{str(args):<30}') # print
|
397 |
+
save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
|
398 |
+
layers.append(m_)
|
399 |
+
ch.append(c2)
|
400 |
+
return keras.Sequential(layers), sorted(save)
|
401 |
+
|
402 |
+
|
403 |
+
class TFModel:
|
404 |
+
# TF YOLOv5 model
|
405 |
+
def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, model=None, imgsz=(640, 640)): # model, channels, classes
|
406 |
+
super().__init__()
|
407 |
+
if isinstance(cfg, dict):
|
408 |
+
self.yaml = cfg # model dict
|
409 |
+
else: # is *.yaml
|
410 |
+
import yaml # for torch hub
|
411 |
+
self.yaml_file = Path(cfg).name
|
412 |
+
with open(cfg) as f:
|
413 |
+
self.yaml = yaml.load(f, Loader=yaml.FullLoader) # model dict
|
414 |
+
|
415 |
+
# Define model
|
416 |
+
if nc and nc != self.yaml['nc']:
|
417 |
+
LOGGER.info(f"Overriding {cfg} nc={self.yaml['nc']} with nc={nc}")
|
418 |
+
self.yaml['nc'] = nc # override yaml value
|
419 |
+
self.model, self.savelist = parse_model(deepcopy(self.yaml), ch=[ch], model=model, imgsz=imgsz)
|
420 |
+
|
421 |
+
def predict(self,
|
422 |
+
inputs,
|
423 |
+
tf_nms=False,
|
424 |
+
agnostic_nms=False,
|
425 |
+
topk_per_class=100,
|
426 |
+
topk_all=100,
|
427 |
+
iou_thres=0.45,
|
428 |
+
conf_thres=0.25):
|
429 |
+
y = [] # outputs
|
430 |
+
x = inputs
|
431 |
+
for m in self.model.layers:
|
432 |
+
if m.f != -1: # if not from previous layer
|
433 |
+
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
|
434 |
+
|
435 |
+
x = m(x) # run
|
436 |
+
y.append(x if m.i in self.savelist else None) # save output
|
437 |
+
|
438 |
+
# Add TensorFlow NMS
|
439 |
+
if tf_nms:
|
440 |
+
boxes = self._xywh2xyxy(x[0][..., :4])
|
441 |
+
probs = x[0][:, :, 4:5]
|
442 |
+
classes = x[0][:, :, 5:]
|
443 |
+
scores = probs * classes
|
444 |
+
if agnostic_nms:
|
445 |
+
nms = AgnosticNMS()((boxes, classes, scores), topk_all, iou_thres, conf_thres)
|
446 |
+
else:
|
447 |
+
boxes = tf.expand_dims(boxes, 2)
|
448 |
+
nms = tf.image.combined_non_max_suppression(boxes,
|
449 |
+
scores,
|
450 |
+
topk_per_class,
|
451 |
+
topk_all,
|
452 |
+
iou_thres,
|
453 |
+
conf_thres,
|
454 |
+
clip_boxes=False)
|
455 |
+
return nms, x[1]
|
456 |
+
return x[0] # output only first tensor [1,6300,85] = [xywh, conf, class0, class1, ...]
|
457 |
+
# x = x[0][0] # [x(1,6300,85), ...] to x(6300,85)
|
458 |
+
# xywh = x[..., :4] # x(6300,4) boxes
|
459 |
+
# conf = x[..., 4:5] # x(6300,1) confidences
|
460 |
+
# cls = tf.reshape(tf.cast(tf.argmax(x[..., 5:], axis=1), tf.float32), (-1, 1)) # x(6300,1) classes
|
461 |
+
# return tf.concat([conf, cls, xywh], 1)
|
462 |
+
|
463 |
+
@staticmethod
|
464 |
+
def _xywh2xyxy(xywh):
|
465 |
+
# Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
|
466 |
+
x, y, w, h = tf.split(xywh, num_or_size_splits=4, axis=-1)
|
467 |
+
return tf.concat([x - w / 2, y - h / 2, x + w / 2, y + h / 2], axis=-1)
|
468 |
+
|
469 |
+
|
470 |
+
class AgnosticNMS(keras.layers.Layer):
|
471 |
+
# TF Agnostic NMS
|
472 |
+
def call(self, input, topk_all, iou_thres, conf_thres):
|
473 |
+
# wrap map_fn to avoid TypeSpec related error https://stackoverflow.com/a/65809989/3036450
|
474 |
+
return tf.map_fn(lambda x: self._nms(x, topk_all, iou_thres, conf_thres),
|
475 |
+
input,
|
476 |
+
fn_output_signature=(tf.float32, tf.float32, tf.float32, tf.int32),
|
477 |
+
name='agnostic_nms')
|
478 |
+
|
479 |
+
@staticmethod
|
480 |
+
def _nms(x, topk_all=100, iou_thres=0.45, conf_thres=0.25): # agnostic NMS
|
481 |
+
boxes, classes, scores = x
|
482 |
+
class_inds = tf.cast(tf.argmax(classes, axis=-1), tf.float32)
|
483 |
+
scores_inp = tf.reduce_max(scores, -1)
|
484 |
+
selected_inds = tf.image.non_max_suppression(boxes,
|
485 |
+
scores_inp,
|
486 |
+
max_output_size=topk_all,
|
487 |
+
iou_threshold=iou_thres,
|
488 |
+
score_threshold=conf_thres)
|
489 |
+
selected_boxes = tf.gather(boxes, selected_inds)
|
490 |
+
padded_boxes = tf.pad(selected_boxes,
|
491 |
+
paddings=[[0, topk_all - tf.shape(selected_boxes)[0]], [0, 0]],
|
492 |
+
mode="CONSTANT",
|
493 |
+
constant_values=0.0)
|
494 |
+
selected_scores = tf.gather(scores_inp, selected_inds)
|
495 |
+
padded_scores = tf.pad(selected_scores,
|
496 |
+
paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]],
|
497 |
+
mode="CONSTANT",
|
498 |
+
constant_values=-1.0)
|
499 |
+
selected_classes = tf.gather(class_inds, selected_inds)
|
500 |
+
padded_classes = tf.pad(selected_classes,
|
501 |
+
paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]],
|
502 |
+
mode="CONSTANT",
|
503 |
+
constant_values=-1.0)
|
504 |
+
valid_detections = tf.shape(selected_inds)[0]
|
505 |
+
return padded_boxes, padded_scores, padded_classes, valid_detections
|
506 |
+
|
507 |
+
|
508 |
+
def activations(act=nn.SiLU):
|
509 |
+
# Returns TF activation from input PyTorch activation
|
510 |
+
if isinstance(act, nn.LeakyReLU):
|
511 |
+
return lambda x: keras.activations.relu(x, alpha=0.1)
|
512 |
+
elif isinstance(act, nn.Hardswish):
|
513 |
+
return lambda x: x * tf.nn.relu6(x + 3) * 0.166666667
|
514 |
+
elif isinstance(act, (nn.SiLU, SiLU)):
|
515 |
+
return lambda x: keras.activations.swish(x)
|
516 |
+
else:
|
517 |
+
raise Exception(f'no matching TensorFlow activation found for PyTorch activation {act}')
|
518 |
+
|
519 |
+
|
520 |
+
def representative_dataset_gen(dataset, ncalib=100):
|
521 |
+
# Representative dataset generator for use with converter.representative_dataset, returns a generator of np arrays
|
522 |
+
for n, (path, img, im0s, vid_cap, string) in enumerate(dataset):
|
523 |
+
im = np.transpose(img, [1, 2, 0])
|
524 |
+
im = np.expand_dims(im, axis=0).astype(np.float32)
|
525 |
+
im /= 255
|
526 |
+
yield [im]
|
527 |
+
if n >= ncalib:
|
528 |
+
break
|
529 |
+
|
530 |
+
|
531 |
+
def run(
|
532 |
+
weights=ROOT / 'yolov5s.pt', # weights path
|
533 |
+
imgsz=(640, 640), # inference size h,w
|
534 |
+
batch_size=1, # batch size
|
535 |
+
dynamic=False, # dynamic batch size
|
536 |
+
):
|
537 |
+
# PyTorch model
|
538 |
+
im = torch.zeros((batch_size, 3, *imgsz)) # BCHW image
|
539 |
+
model = attempt_load(weights, device=torch.device('cpu'), inplace=True, fuse=False)
|
540 |
+
_ = model(im) # inference
|
541 |
+
model.info()
|
542 |
+
|
543 |
+
# TensorFlow model
|
544 |
+
im = tf.zeros((batch_size, *imgsz, 3)) # BHWC image
|
545 |
+
tf_model = TFModel(cfg=model.yaml, model=model, nc=model.nc, imgsz=imgsz)
|
546 |
+
_ = tf_model.predict(im) # inference
|
547 |
+
|
548 |
+
# Keras model
|
549 |
+
im = keras.Input(shape=(*imgsz, 3), batch_size=None if dynamic else batch_size)
|
550 |
+
keras_model = keras.Model(inputs=im, outputs=tf_model.predict(im))
|
551 |
+
keras_model.summary()
|
552 |
+
|
553 |
+
LOGGER.info('PyTorch, TensorFlow and Keras models successfully verified.\nUse export.py for TF model export.')
|
554 |
+
|
555 |
+
|
556 |
+
def parse_opt():
|
557 |
+
parser = argparse.ArgumentParser()
|
558 |
+
parser.add_argument('--weights', type=str, default=ROOT / 'yolov5s.pt', help='weights path')
|
559 |
+
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
|
560 |
+
parser.add_argument('--batch-size', type=int, default=1, help='batch size')
|
561 |
+
parser.add_argument('--dynamic', action='store_true', help='dynamic batch size')
|
562 |
+
opt = parser.parse_args()
|
563 |
+
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
|
564 |
+
print_args(vars(opt))
|
565 |
+
return opt
|
566 |
+
|
567 |
+
|
568 |
+
def main(opt):
|
569 |
+
run(**vars(opt))
|
570 |
+
|
571 |
+
|
572 |
+
if __name__ == "__main__":
|
573 |
+
opt = parse_opt()
|
574 |
+
main(opt)
|
models/yolo.py
ADDED
@@ -0,0 +1,338 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
2 |
+
"""
|
3 |
+
YOLO-specific modules
|
4 |
+
|
5 |
+
Usage:
|
6 |
+
$ python path/to/models/yolo.py --cfg yolov5s.yaml
|
7 |
+
"""
|
8 |
+
|
9 |
+
import argparse
|
10 |
+
import os
|
11 |
+
import platform
|
12 |
+
import sys
|
13 |
+
from copy import deepcopy
|
14 |
+
from pathlib import Path
|
15 |
+
|
16 |
+
FILE = Path(__file__).resolve()
|
17 |
+
ROOT = FILE.parents[1] # YOLOv5 root directory
|
18 |
+
if str(ROOT) not in sys.path:
|
19 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
20 |
+
if platform.system() != 'Windows':
|
21 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
22 |
+
|
23 |
+
from models.common import *
|
24 |
+
from models.experimental import *
|
25 |
+
from utils.autoanchor import check_anchor_order
|
26 |
+
from utils.general import LOGGER, check_version, check_yaml, make_divisible, print_args
|
27 |
+
from utils.plots import feature_visualization
|
28 |
+
from utils.torch_utils import (fuse_conv_and_bn, initialize_weights, model_info, profile, scale_img, select_device,
|
29 |
+
time_sync)
|
30 |
+
|
31 |
+
try:
|
32 |
+
import thop # for FLOPs computation
|
33 |
+
except ImportError:
|
34 |
+
thop = None
|
35 |
+
|
36 |
+
|
37 |
+
class Detect(nn.Module):
|
38 |
+
stride = None # strides computed during build
|
39 |
+
onnx_dynamic = False # ONNX export parameter
|
40 |
+
export = False # export mode
|
41 |
+
|
42 |
+
def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer
|
43 |
+
super().__init__()
|
44 |
+
self.nc = nc # number of classes
|
45 |
+
self.no = nc + 5 # number of outputs per anchor
|
46 |
+
self.nl = len(anchors) # number of detection layers
|
47 |
+
self.na = len(anchors[0]) // 2 # number of anchors
|
48 |
+
self.grid = [torch.zeros(1)] * self.nl # init grid
|
49 |
+
self.anchor_grid = [torch.zeros(1)] * self.nl # init anchor grid
|
50 |
+
self.register_buffer('anchors', torch.tensor(anchors).float().view(self.nl, -1, 2)) # shape(nl,na,2)
|
51 |
+
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
|
52 |
+
self.inplace = inplace # use in-place ops (e.g. slice assignment)
|
53 |
+
|
54 |
+
def forward(self, x):
|
55 |
+
z = [] # inference output
|
56 |
+
for i in range(self.nl):
|
57 |
+
x[i] = self.m[i](x[i]) # conv
|
58 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
59 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
60 |
+
|
61 |
+
if not self.training: # inference
|
62 |
+
if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
63 |
+
self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
|
64 |
+
|
65 |
+
y = x[i].sigmoid()
|
66 |
+
if self.inplace:
|
67 |
+
y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i] # xy
|
68 |
+
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
69 |
+
else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
|
70 |
+
xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
|
71 |
+
xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
|
72 |
+
wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
|
73 |
+
y = torch.cat((xy, wh, conf), 4)
|
74 |
+
z.append(y.view(bs, -1, self.no))
|
75 |
+
|
76 |
+
return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)
|
77 |
+
|
78 |
+
def _make_grid(self, nx=20, ny=20, i=0):
|
79 |
+
d = self.anchors[i].device
|
80 |
+
t = self.anchors[i].dtype
|
81 |
+
shape = 1, self.na, ny, nx, 2 # grid shape
|
82 |
+
y, x = torch.arange(ny, device=d, dtype=t), torch.arange(nx, device=d, dtype=t)
|
83 |
+
if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
|
84 |
+
yv, xv = torch.meshgrid(y, x, indexing='ij')
|
85 |
+
else:
|
86 |
+
yv, xv = torch.meshgrid(y, x)
|
87 |
+
grid = torch.stack((xv, yv), 2).expand(shape) - 0.5 # add grid offset, i.e. y = 2.0 * x - 0.5
|
88 |
+
anchor_grid = (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape)
|
89 |
+
return grid, anchor_grid
|
90 |
+
|
91 |
+
|
92 |
+
class Model(nn.Module):
|
93 |
+
# YOLOv5 model
|
94 |
+
def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, anchors=None): # model, input channels, number of classes
|
95 |
+
super().__init__()
|
96 |
+
if isinstance(cfg, dict):
|
97 |
+
self.yaml = cfg # model dict
|
98 |
+
else: # is *.yaml
|
99 |
+
import yaml # for torch hub
|
100 |
+
self.yaml_file = Path(cfg).name
|
101 |
+
with open(cfg, encoding='ascii', errors='ignore') as f:
|
102 |
+
self.yaml = yaml.safe_load(f) # model dict
|
103 |
+
|
104 |
+
# Define model
|
105 |
+
ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels
|
106 |
+
if nc and nc != self.yaml['nc']:
|
107 |
+
LOGGER.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}")
|
108 |
+
self.yaml['nc'] = nc # override yaml value
|
109 |
+
if anchors:
|
110 |
+
LOGGER.info(f'Overriding model.yaml anchors with anchors={anchors}')
|
111 |
+
self.yaml['anchors'] = round(anchors) # override yaml value
|
112 |
+
self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist
|
113 |
+
self.names = [str(i) for i in range(self.yaml['nc'])] # default names
|
114 |
+
self.inplace = self.yaml.get('inplace', True)
|
115 |
+
|
116 |
+
# Build strides, anchors
|
117 |
+
m = self.model[-1] # Detect()
|
118 |
+
if isinstance(m, Detect):
|
119 |
+
s = 256 # 2x min stride
|
120 |
+
m.inplace = self.inplace
|
121 |
+
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
|
122 |
+
check_anchor_order(m) # must be in pixel-space (not grid-space)
|
123 |
+
m.anchors /= m.stride.view(-1, 1, 1)
|
124 |
+
self.stride = m.stride
|
125 |
+
self._initialize_biases() # only run once
|
126 |
+
|
127 |
+
# Init weights, biases
|
128 |
+
initialize_weights(self)
|
129 |
+
self.info()
|
130 |
+
LOGGER.info('')
|
131 |
+
|
132 |
+
def forward(self, x, augment=False, profile=False, visualize=False):
|
133 |
+
if augment:
|
134 |
+
return self._forward_augment(x) # augmented inference, None
|
135 |
+
return self._forward_once(x, profile, visualize) # single-scale inference, train
|
136 |
+
|
137 |
+
def _forward_augment(self, x):
|
138 |
+
img_size = x.shape[-2:] # height, width
|
139 |
+
s = [1, 0.83, 0.67] # scales
|
140 |
+
f = [None, 3, None] # flips (2-ud, 3-lr)
|
141 |
+
y = [] # outputs
|
142 |
+
for si, fi in zip(s, f):
|
143 |
+
xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max()))
|
144 |
+
yi = self._forward_once(xi)[0] # forward
|
145 |
+
# cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
|
146 |
+
yi = self._descale_pred(yi, fi, si, img_size)
|
147 |
+
y.append(yi)
|
148 |
+
y = self._clip_augmented(y) # clip augmented tails
|
149 |
+
return torch.cat(y, 1), None # augmented inference, train
|
150 |
+
|
151 |
+
def _forward_once(self, x, profile=False, visualize=False):
|
152 |
+
y, dt = [], [] # outputs
|
153 |
+
for m in self.model:
|
154 |
+
if m.f != -1: # if not from previous layer
|
155 |
+
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
|
156 |
+
if profile:
|
157 |
+
self._profile_one_layer(m, x, dt)
|
158 |
+
x = m(x) # run
|
159 |
+
y.append(x if m.i in self.save else None) # save output
|
160 |
+
if visualize:
|
161 |
+
feature_visualization(x, m.type, m.i, save_dir=visualize)
|
162 |
+
return x
|
163 |
+
|
164 |
+
def _descale_pred(self, p, flips, scale, img_size):
|
165 |
+
# de-scale predictions following augmented inference (inverse operation)
|
166 |
+
if self.inplace:
|
167 |
+
p[..., :4] /= scale # de-scale
|
168 |
+
if flips == 2:
|
169 |
+
p[..., 1] = img_size[0] - p[..., 1] # de-flip ud
|
170 |
+
elif flips == 3:
|
171 |
+
p[..., 0] = img_size[1] - p[..., 0] # de-flip lr
|
172 |
+
else:
|
173 |
+
x, y, wh = p[..., 0:1] / scale, p[..., 1:2] / scale, p[..., 2:4] / scale # de-scale
|
174 |
+
if flips == 2:
|
175 |
+
y = img_size[0] - y # de-flip ud
|
176 |
+
elif flips == 3:
|
177 |
+
x = img_size[1] - x # de-flip lr
|
178 |
+
p = torch.cat((x, y, wh, p[..., 4:]), -1)
|
179 |
+
return p
|
180 |
+
|
181 |
+
def _clip_augmented(self, y):
|
182 |
+
# Clip YOLOv5 augmented inference tails
|
183 |
+
nl = self.model[-1].nl # number of detection layers (P3-P5)
|
184 |
+
g = sum(4 ** x for x in range(nl)) # grid points
|
185 |
+
e = 1 # exclude layer count
|
186 |
+
i = (y[0].shape[1] // g) * sum(4 ** x for x in range(e)) # indices
|
187 |
+
y[0] = y[0][:, :-i] # large
|
188 |
+
i = (y[-1].shape[1] // g) * sum(4 ** (nl - 1 - x) for x in range(e)) # indices
|
189 |
+
y[-1] = y[-1][:, i:] # small
|
190 |
+
return y
|
191 |
+
|
192 |
+
def _profile_one_layer(self, m, x, dt):
|
193 |
+
c = isinstance(m, Detect) # is final layer, copy input as inplace fix
|
194 |
+
o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs
|
195 |
+
t = time_sync()
|
196 |
+
for _ in range(10):
|
197 |
+
m(x.copy() if c else x)
|
198 |
+
dt.append((time_sync() - t) * 100)
|
199 |
+
if m == self.model[0]:
|
200 |
+
LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} module")
|
201 |
+
LOGGER.info(f'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}')
|
202 |
+
if c:
|
203 |
+
LOGGER.info(f"{sum(dt):10.2f} {'-':>10s} {'-':>10s} Total")
|
204 |
+
|
205 |
+
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
|
206 |
+
# https://arxiv.org/abs/1708.02002 section 3.3
|
207 |
+
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
|
208 |
+
m = self.model[-1] # Detect() module
|
209 |
+
for mi, s in zip(m.m, m.stride): # from
|
210 |
+
b = mi.bias.view(m.na, -1).detach() # conv.bias(255) to (3,85)
|
211 |
+
b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
|
212 |
+
b[:, 5:] += math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # cls
|
213 |
+
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
|
214 |
+
|
215 |
+
def _print_biases(self):
|
216 |
+
m = self.model[-1] # Detect() module
|
217 |
+
for mi in m.m: # from
|
218 |
+
b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85)
|
219 |
+
LOGGER.info(
|
220 |
+
('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean()))
|
221 |
+
|
222 |
+
# def _print_weights(self):
|
223 |
+
# for m in self.model.modules():
|
224 |
+
# if type(m) is Bottleneck:
|
225 |
+
# LOGGER.info('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights
|
226 |
+
|
227 |
+
def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers
|
228 |
+
LOGGER.info('Fusing layers... ')
|
229 |
+
for m in self.model.modules():
|
230 |
+
if isinstance(m, (Conv, DWConv)) and hasattr(m, 'bn'):
|
231 |
+
m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
|
232 |
+
delattr(m, 'bn') # remove batchnorm
|
233 |
+
m.forward = m.forward_fuse # update forward
|
234 |
+
self.info()
|
235 |
+
return self
|
236 |
+
|
237 |
+
def info(self, verbose=False, img_size=640): # print model information
|
238 |
+
model_info(self, verbose, img_size)
|
239 |
+
|
240 |
+
def _apply(self, fn):
|
241 |
+
# Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers
|
242 |
+
self = super()._apply(fn)
|
243 |
+
m = self.model[-1] # Detect()
|
244 |
+
if isinstance(m, Detect):
|
245 |
+
m.stride = fn(m.stride)
|
246 |
+
m.grid = list(map(fn, m.grid))
|
247 |
+
if isinstance(m.anchor_grid, list):
|
248 |
+
m.anchor_grid = list(map(fn, m.anchor_grid))
|
249 |
+
return self
|
250 |
+
|
251 |
+
|
252 |
+
def parse_model(d, ch): # model_dict, input_channels(3)
|
253 |
+
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
|
254 |
+
anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple']
|
255 |
+
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
|
256 |
+
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
|
257 |
+
|
258 |
+
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
|
259 |
+
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args
|
260 |
+
m = eval(m) if isinstance(m, str) else m # eval strings
|
261 |
+
for j, a in enumerate(args):
|
262 |
+
try:
|
263 |
+
args[j] = eval(a) if isinstance(a, str) else a # eval strings
|
264 |
+
except NameError:
|
265 |
+
pass
|
266 |
+
|
267 |
+
n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain
|
268 |
+
if m in (Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
|
269 |
+
BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x):
|
270 |
+
c1, c2 = ch[f], args[0]
|
271 |
+
if c2 != no: # if not output
|
272 |
+
c2 = make_divisible(c2 * gw, 8)
|
273 |
+
|
274 |
+
args = [c1, c2, *args[1:]]
|
275 |
+
if m in [BottleneckCSP, C3, C3TR, C3Ghost, C3x]:
|
276 |
+
args.insert(2, n) # number of repeats
|
277 |
+
n = 1
|
278 |
+
elif m is nn.BatchNorm2d:
|
279 |
+
args = [ch[f]]
|
280 |
+
elif m is Concat:
|
281 |
+
c2 = sum(ch[x] for x in f)
|
282 |
+
elif m is Detect:
|
283 |
+
args.append([ch[x] for x in f])
|
284 |
+
if isinstance(args[1], int): # number of anchors
|
285 |
+
args[1] = [list(range(args[1] * 2))] * len(f)
|
286 |
+
elif m is Contract:
|
287 |
+
c2 = ch[f] * args[0] ** 2
|
288 |
+
elif m is Expand:
|
289 |
+
c2 = ch[f] // args[0] ** 2
|
290 |
+
else:
|
291 |
+
c2 = ch[f]
|
292 |
+
|
293 |
+
m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
|
294 |
+
t = str(m)[8:-2].replace('__main__.', '') # module type
|
295 |
+
np = sum(x.numel() for x in m_.parameters()) # number params
|
296 |
+
m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
|
297 |
+
LOGGER.info(f'{i:>3}{str(f):>18}{n_:>3}{np:10.0f} {t:<40}{str(args):<30}') # print
|
298 |
+
save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
|
299 |
+
layers.append(m_)
|
300 |
+
if i == 0:
|
301 |
+
ch = []
|
302 |
+
ch.append(c2)
|
303 |
+
return nn.Sequential(*layers), sorted(save)
|
304 |
+
|
305 |
+
|
306 |
+
if __name__ == '__main__':
|
307 |
+
parser = argparse.ArgumentParser()
|
308 |
+
parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml')
|
309 |
+
parser.add_argument('--batch-size', type=int, default=1, help='total batch size for all GPUs')
|
310 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
311 |
+
parser.add_argument('--profile', action='store_true', help='profile model speed')
|
312 |
+
parser.add_argument('--line-profile', action='store_true', help='profile model speed layer by layer')
|
313 |
+
parser.add_argument('--test', action='store_true', help='test all yolo*.yaml')
|
314 |
+
opt = parser.parse_args()
|
315 |
+
opt.cfg = check_yaml(opt.cfg) # check YAML
|
316 |
+
print_args(vars(opt))
|
317 |
+
device = select_device(opt.device)
|
318 |
+
|
319 |
+
# Create model
|
320 |
+
im = torch.rand(opt.batch_size, 3, 640, 640).to(device)
|
321 |
+
model = Model(opt.cfg).to(device)
|
322 |
+
|
323 |
+
# Options
|
324 |
+
if opt.line_profile: # profile layer by layer
|
325 |
+
_ = model(im, profile=True)
|
326 |
+
|
327 |
+
elif opt.profile: # profile forward-backward
|
328 |
+
results = profile(input=im, ops=[model], n=3)
|
329 |
+
|
330 |
+
elif opt.test: # test all models
|
331 |
+
for cfg in Path(ROOT / 'models').rglob('yolo*.yaml'):
|
332 |
+
try:
|
333 |
+
_ = Model(cfg)
|
334 |
+
except Exception as e:
|
335 |
+
print(f'Error in {cfg}: {e}')
|
336 |
+
|
337 |
+
else: # report fused model summary
|
338 |
+
model.fuse()
|
models/yolov5l.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/yolov5m.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/yolov5n.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/yolov5s.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|
models/yolov5x.yaml
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
2 |
+
|
3 |
+
# Parameters
|
4 |
+
nc: 80 # number of classes
|
5 |
+
depth_multiple: 1.33 # model depth multiple
|
6 |
+
width_multiple: 1.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 |
+
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
16 |
+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
17 |
+
[-1, 3, C3, [128]],
|
18 |
+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
19 |
+
[-1, 6, C3, [256]],
|
20 |
+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
21 |
+
[-1, 9, C3, [512]],
|
22 |
+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
23 |
+
[-1, 3, C3, [1024]],
|
24 |
+
[-1, 1, SPPF, [1024, 5]], # 9
|
25 |
+
]
|
26 |
+
|
27 |
+
# YOLOv5 v6.0 head
|
28 |
+
head:
|
29 |
+
[[-1, 1, Conv, [512, 1, 1]],
|
30 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
31 |
+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
32 |
+
[-1, 3, C3, [512, False]], # 13
|
33 |
+
|
34 |
+
[-1, 1, Conv, [256, 1, 1]],
|
35 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
36 |
+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
37 |
+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
38 |
+
|
39 |
+
[-1, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 14], 1, Concat, [1]], # cat head P4
|
41 |
+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
42 |
+
|
43 |
+
[-1, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 10], 1, Concat, [1]], # cat head P5
|
45 |
+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
46 |
+
|
47 |
+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
48 |
+
]
|