glenn-jocher
commited on
Commit
·
ff02ae0
1
Parent(s):
d187459
module updates
Browse files- models/common.py +10 -36
- models/yolo.py +1 -3
- utils/utils.py +5 -4
models/common.py
CHANGED
@@ -6,11 +6,13 @@ import torch.nn.functional as F
|
|
6 |
from utils.utils import *
|
7 |
|
8 |
|
9 |
-
def DWConv(c1, c2, k=1, s=1, act=True):
|
|
|
10 |
return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
|
11 |
|
12 |
|
13 |
-
class Conv(nn.Module):
|
|
|
14 |
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
|
15 |
super(Conv, self).__init__()
|
16 |
self.conv = nn.Conv2d(c1, c2, k, s, k // 2, groups=g, bias=False)
|
@@ -25,6 +27,7 @@ class Conv(nn.Module): # standard convolution
|
|
25 |
|
26 |
|
27 |
class Bottleneck(nn.Module):
|
|
|
28 |
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
29 |
super(Bottleneck, self).__init__()
|
30 |
c_ = int(c2 * e) # hidden channels
|
@@ -36,21 +39,8 @@ class Bottleneck(nn.Module):
|
|
36 |
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
37 |
|
38 |
|
39 |
-
class BottleneckLight(nn.Module):
|
40 |
-
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
41 |
-
super(BottleneckLight, self).__init__()
|
42 |
-
c_ = int(c2 * e) # hidden channels
|
43 |
-
self.cv1 = Conv(c1, c_, 1, 1)
|
44 |
-
self.cv2 = nn.Conv2d(c_, c2, 3, 1, 3 // 2, groups=g, bias=False)
|
45 |
-
self.bn = nn.BatchNorm2d(c2)
|
46 |
-
self.act = nn.LeakyReLU(0.1, inplace=True)
|
47 |
-
self.add = shortcut and c1 == c2
|
48 |
-
|
49 |
-
def forward(self, x):
|
50 |
-
return self.act(self.bn(x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))))
|
51 |
-
|
52 |
-
|
53 |
class BottleneckCSP(nn.Module):
|
|
|
54 |
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
55 |
super(BottleneckCSP, self).__init__()
|
56 |
c_ = int(c2 * e) # hidden channels
|
@@ -68,25 +58,8 @@ class BottleneckCSP(nn.Module):
|
|
68 |
return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
|
69 |
|
70 |
|
71 |
-
class
|
72 |
-
|
73 |
-
super(Narrow, self).__init__()
|
74 |
-
c_ = c2 // 2 # hidden channels
|
75 |
-
self.cv1 = Conv(c1, c_, 1, 1)
|
76 |
-
self.cv2 = Conv(c_, c2, 3, 1, g=g)
|
77 |
-
self.add = shortcut and c1 == c2
|
78 |
-
|
79 |
-
def forward(self, x):
|
80 |
-
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
81 |
-
|
82 |
-
|
83 |
-
class Origami(nn.Module): # 5-side layering
|
84 |
-
def forward(self, x):
|
85 |
-
y = F.pad(x, [1, 1, 1, 1])
|
86 |
-
return torch.cat([x, y[..., :-2, 1:-1], y[..., 1:-1, :-2], y[..., 2:, 1:-1], y[..., 1:-1, 2:]], 1)
|
87 |
-
|
88 |
-
|
89 |
-
class ConvPlus(nn.Module): # standard convolution
|
90 |
def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): # ch_in, ch_out, kernel, stride, groups
|
91 |
super(ConvPlus, self).__init__()
|
92 |
self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias=bias)
|
@@ -96,7 +69,8 @@ class ConvPlus(nn.Module): # standard convolution
|
|
96 |
return self.cv1(x) + self.cv2(x)
|
97 |
|
98 |
|
99 |
-
class SPP(nn.Module):
|
|
|
100 |
def __init__(self, c1, c2, k=(5, 9, 13)):
|
101 |
super(SPP, self).__init__()
|
102 |
c_ = c1 // 2 # hidden channels
|
|
|
6 |
from utils.utils import *
|
7 |
|
8 |
|
9 |
+
def DWConv(c1, c2, k=1, s=1, act=True):
|
10 |
+
# Depthwise convolution
|
11 |
return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
|
12 |
|
13 |
|
14 |
+
class Conv(nn.Module):
|
15 |
+
# Standard convolution
|
16 |
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
|
17 |
super(Conv, self).__init__()
|
18 |
self.conv = nn.Conv2d(c1, c2, k, s, k // 2, groups=g, bias=False)
|
|
|
27 |
|
28 |
|
29 |
class Bottleneck(nn.Module):
|
30 |
+
# Standard bottleneck
|
31 |
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
32 |
super(Bottleneck, self).__init__()
|
33 |
c_ = int(c2 * e) # hidden channels
|
|
|
39 |
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
40 |
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
class BottleneckCSP(nn.Module):
|
43 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
44 |
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
45 |
super(BottleneckCSP, self).__init__()
|
46 |
c_ = int(c2 * e) # hidden channels
|
|
|
58 |
return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
|
59 |
|
60 |
|
61 |
+
class ConvPlus(nn.Module):
|
62 |
+
# Plus-shaped convolution
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): # ch_in, ch_out, kernel, stride, groups
|
64 |
super(ConvPlus, self).__init__()
|
65 |
self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias=bias)
|
|
|
69 |
return self.cv1(x) + self.cv2(x)
|
70 |
|
71 |
|
72 |
+
class SPP(nn.Module):
|
73 |
+
# Spatial pyramid pooling layer used in YOLOv3-SPP
|
74 |
def __init__(self, c1, c2, k=(5, 9, 13)):
|
75 |
super(SPP, self).__init__()
|
76 |
c_ = c1 // 2 # hidden channels
|
models/yolo.py
CHANGED
@@ -176,9 +176,7 @@ def parse_model(md, ch): # model_dict, input_channels(3)
|
|
176 |
elif m is nn.BatchNorm2d:
|
177 |
args = [ch[f]]
|
178 |
elif m is Concat:
|
179 |
-
c2 = sum([ch[x] for x in f])
|
180 |
-
elif m is Origami:
|
181 |
-
c2 = ch[f] * 5
|
182 |
elif m is Detect:
|
183 |
f = f or list(reversed([(-1 if j == i else j - 1) for j, x in enumerate(ch) if x == no]))
|
184 |
else:
|
|
|
176 |
elif m is nn.BatchNorm2d:
|
177 |
args = [ch[f]]
|
178 |
elif m is Concat:
|
179 |
+
c2 = sum([ch[-1 if x == -1 else x + 1] for x in f])
|
|
|
|
|
180 |
elif m is Detect:
|
181 |
f = f or list(reversed([(-1 if j == i else j - 1) for j, x in enumerate(ch) if x == no]))
|
182 |
else:
|
utils/utils.py
CHANGED
@@ -468,6 +468,7 @@ def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, fast=False, c
|
|
468 |
nx6 (x1, y1, x2, y2, conf, cls)
|
469 |
"""
|
470 |
nc = prediction[0].shape[1] - 5 # number of classes
|
|
|
471 |
|
472 |
# Settings
|
473 |
min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height
|
@@ -487,7 +488,7 @@ def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, fast=False, c
|
|
487 |
for xi, x in enumerate(prediction): # image index, image inference
|
488 |
# Apply constraints
|
489 |
# x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height
|
490 |
-
x = x[
|
491 |
|
492 |
# If none remain process next image
|
493 |
if not x.shape[0]:
|
@@ -1074,9 +1075,9 @@ def plot_results_overlay(start=0, stop=0): # from utils.utils import *; plot_re
|
|
1074 |
for i in range(5):
|
1075 |
for j in [i, i + 5]:
|
1076 |
y = results[j, x]
|
1077 |
-
|
1078 |
-
y_smooth = butter_lowpass_filtfilt(y)
|
1079 |
-
ax[i].plot(x, np.gradient(y_smooth), marker='.', label=s[j])
|
1080 |
|
1081 |
ax[i].set_title(t[i])
|
1082 |
ax[i].legend()
|
|
|
468 |
nx6 (x1, y1, x2, y2, conf, cls)
|
469 |
"""
|
470 |
nc = prediction[0].shape[1] - 5 # number of classes
|
471 |
+
xc = prediction[..., 4] > conf_thres # candidates
|
472 |
|
473 |
# Settings
|
474 |
min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height
|
|
|
488 |
for xi, x in enumerate(prediction): # image index, image inference
|
489 |
# Apply constraints
|
490 |
# x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height
|
491 |
+
x = x[xc[xi]] # confidence
|
492 |
|
493 |
# If none remain process next image
|
494 |
if not x.shape[0]:
|
|
|
1075 |
for i in range(5):
|
1076 |
for j in [i, i + 5]:
|
1077 |
y = results[j, x]
|
1078 |
+
ax[i].plot(x, y, marker='.', label=s[j])
|
1079 |
+
# y_smooth = butter_lowpass_filtfilt(y)
|
1080 |
+
# ax[i].plot(x, np.gradient(y_smooth), marker='.', label=s[j])
|
1081 |
|
1082 |
ax[i].set_title(t[i])
|
1083 |
ax[i].legend()
|