Spaces:
Sleeping
Sleeping
yan123yan
commited on
Commit
•
58ff559
1
Parent(s):
abf96d3
v1.1 update yolov7 & yolov8
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitignore +17 -1
- app.py +119 -6
- infer/__init__.py +2 -0
- infer/yolov5/get_results.py +32 -0
- infer/yolov7/__init__.py +2 -0
- infer/yolov7/cfg/baseline/r50-csp.yaml +49 -0
- infer/yolov7/cfg/baseline/x50-csp.yaml +49 -0
- infer/yolov7/cfg/baseline/yolor-csp-x.yaml +52 -0
- infer/yolov7/cfg/baseline/yolor-csp.yaml +52 -0
- infer/yolov7/cfg/baseline/yolor-d6.yaml +63 -0
- infer/yolov7/cfg/baseline/yolor-e6.yaml +63 -0
- infer/yolov7/cfg/baseline/yolor-p6.yaml +63 -0
- infer/yolov7/cfg/baseline/yolor-w6.yaml +63 -0
- infer/yolov7/cfg/baseline/yolov3-spp.yaml +51 -0
- infer/yolov7/cfg/baseline/yolov3.yaml +51 -0
- infer/yolov7/cfg/baseline/yolov4-csp.yaml +52 -0
- infer/yolov7/cfg/deploy/yolov7-d6.yaml +202 -0
- infer/yolov7/cfg/deploy/yolov7-e6.yaml +180 -0
- infer/yolov7/cfg/deploy/yolov7-e6e.yaml +301 -0
- infer/yolov7/cfg/deploy/yolov7-tiny-silu.yaml +112 -0
- infer/yolov7/cfg/deploy/yolov7-tiny.yaml +112 -0
- infer/yolov7/cfg/deploy/yolov7-w6.yaml +158 -0
- infer/yolov7/cfg/deploy/yolov7.yaml +140 -0
- infer/yolov7/cfg/deploy/yolov7x.yaml +156 -0
- infer/yolov7/cfg/training/yolov7-d6.yaml +207 -0
- infer/yolov7/cfg/training/yolov7-e6.yaml +185 -0
- infer/yolov7/cfg/training/yolov7-e6e.yaml +306 -0
- infer/yolov7/cfg/training/yolov7-tiny.yaml +112 -0
- infer/yolov7/cfg/training/yolov7-w6.yaml +163 -0
- infer/yolov7/cfg/training/yolov7.yaml +140 -0
- infer/yolov7/cfg/training/yolov7x.yaml +156 -0
- infer/yolov7/data/coco.yaml +23 -0
- infer/yolov7/data/hyp.scratch.custom.yaml +31 -0
- infer/yolov7/data/hyp.scratch.p5.yaml +31 -0
- infer/yolov7/data/hyp.scratch.p6.yaml +31 -0
- infer/yolov7/data/hyp.scratch.tiny.yaml +31 -0
- infer/yolov7/get_results.py +43 -0
- infer/yolov7/models/__init__.py +2 -0
- infer/yolov7/models/common.py +2019 -0
- infer/yolov7/models/experimental.py +277 -0
- infer/yolov7/models/yolo.py +843 -0
- infer/yolov7/utils/__init__.py +1 -0
- infer/yolov7/utils/activations.py +72 -0
- infer/yolov7/utils/add_nms.py +155 -0
- infer/yolov7/utils/autoanchor.py +160 -0
- infer/yolov7/utils/aws/__init__.py +1 -0
- infer/yolov7/utils/aws/mime.sh +26 -0
- infer/yolov7/utils/aws/resume.py +37 -0
- infer/yolov7/utils/aws/userdata.sh +27 -0
- infer/yolov7/utils/datasets.py +1320 -0
.gitignore
CHANGED
@@ -1 +1,17 @@
|
|
1 |
-
.idea
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.idea
|
2 |
+
__pycache__
|
3 |
+
*.pyc
|
4 |
+
*.pyo
|
5 |
+
*.pyd
|
6 |
+
*.log
|
7 |
+
*.bak
|
8 |
+
*.swp
|
9 |
+
*.tmp
|
10 |
+
*.db
|
11 |
+
*.sqlite
|
12 |
+
*.sqlite3
|
13 |
+
*.db-journal
|
14 |
+
*.db-shm
|
15 |
+
*.db-wal
|
16 |
+
my_model/v7-tiny-cbam.pt
|
17 |
+
my_model/v7-x.pt
|
app.py
CHANGED
@@ -1,17 +1,130 @@
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
image_path = "image/"
|
|
|
|
|
6 |
each_image_name = os.listdir(image_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
st.set_page_config(layout="wide")
|
9 |
-
body1 = st.container()
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
with body1_col1:
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from PIL import Image
|
4 |
+
import random
|
5 |
+
import cv2
|
6 |
+
|
7 |
+
from infer.yolov7.get_results import get_yolov7_result
|
8 |
+
from infer.yolov5.get_results import get_yolov5_result
|
9 |
+
from infer.yolov8.get_results import get_yolov8_result
|
10 |
|
11 |
image_path = "image/"
|
12 |
+
txt_path = "label/"
|
13 |
+
random.seed(0)
|
14 |
each_image_name = os.listdir(image_path)
|
15 |
+
random.shuffle(each_image_name)
|
16 |
+
|
17 |
+
label_names = ['epiglottis', 'vocal cord', 'trachea', 'carina', 'right main bronchus', 'intermediate bronchus',
|
18 |
+
'right upper lobar bronchus', 'right middle lobar bronchus', 'right lower lobar bronchus', 'right superior segment bronchus',
|
19 |
+
'right basal bronchus', 'left main bronchus', 'left upper lobar bronchus', 'left division bronchus',
|
20 |
+
'left lingular bronchus', 'left lower bronchus', 'left superior segment', 'left basal bronchus']
|
21 |
+
|
22 |
+
model_list = ['YOLO-V8',
|
23 |
+
'YOLO-V7',
|
24 |
+
'YOLO-V5']
|
25 |
|
26 |
st.set_page_config(layout="wide")
|
|
|
27 |
|
28 |
+
def inference(image, model_name, conf_threshold, iou_threshold):
|
29 |
+
if model_name == "YOLO-V7":
|
30 |
+
return get_yolov7_result(image, conf_threshold, iou_threshold, label_names)
|
31 |
+
elif model_name == "YOLO-V5":
|
32 |
+
#return get_yolov5_result(image, conf_threshold, iou_threshold, label_names)
|
33 |
+
return None, None
|
34 |
+
elif model_name == "YOLO-V8":
|
35 |
+
return get_yolov8_result(image, conf_threshold, iou_threshold, label_names)
|
36 |
+
else:
|
37 |
+
return None, None
|
38 |
+
|
39 |
+
def image_on_click(image_index):
|
40 |
+
with body1_col2:
|
41 |
+
st.header("Image Information")
|
42 |
+
image_name = each_image_name[image_index]
|
43 |
+
image = Image.open(os.path.join(image_path, image_name))
|
44 |
+
cv2_image = cv2.imread(os.path.join(image_path, image_name))
|
45 |
+
cv2_image_copy = cv2_image.copy()
|
46 |
+
cv2_h, cv2_w, _ = cv2_image.shape
|
47 |
+
st.write("Image Width: " ,image.width)
|
48 |
+
st.write("Image Height: " ,image.height)
|
49 |
+
temp_label_list = []
|
50 |
+
with open(os.path.join(txt_path, image_name.replace(".png",".txt")), "r") as f:
|
51 |
+
lines = f.readlines()
|
52 |
+
for line in lines:
|
53 |
+
line = line.split(" ")
|
54 |
+
label_index = int(line[0]) - 1
|
55 |
+
label_name = label_names[label_index]
|
56 |
+
x_center = float(line[1])
|
57 |
+
y_center = float(line[2])
|
58 |
+
width = float(line[3])
|
59 |
+
height = float(line[4])
|
60 |
+
x_center, y_center, width, height = [x_center * cv2_w, y_center * cv2_h, width * cv2_w, height * cv2_h]
|
61 |
+
|
62 |
+
x_min = int(x_center - width / 2)
|
63 |
+
y_min = int(y_center - height / 2)
|
64 |
+
x_max = int(x_center + width / 2)
|
65 |
+
y_max = int(y_center + height / 2)
|
66 |
+
|
67 |
+
cv2.rectangle(cv2_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
68 |
+
|
69 |
+
label_size, _ = cv2.getTextSize(label_name, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
|
70 |
+
label_x_min = x_min
|
71 |
+
label_y_min = y_min - label_size[1] - 10
|
72 |
+
label_x_max = x_min + label_size[0]
|
73 |
+
label_y_max = y_min
|
74 |
+
cv2.rectangle(cv2_image, (label_x_min, label_y_min), (label_x_max, label_y_max), (0, 255, 0), cv2.FILLED)
|
75 |
+
cv2.putText(cv2_image, label_name, (label_x_min, label_y_min + label_size[1] + 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
|
76 |
+
(0, 0, 0), 1)
|
77 |
|
78 |
+
temp_label_list.append(label_name)
|
79 |
+
st.write("Label:" + str(temp_label_list))
|
80 |
+
cv2_image = cv2_image[...,::-1]
|
81 |
+
|
82 |
+
st.image(cv2_image, image_name.replace(".png","") + " label image")
|
83 |
+
|
84 |
+
with body1_col3:
|
85 |
+
st.header("Inference Result")
|
86 |
+
result_image, result_list = inference(cv2_image_copy, selected_model, conf_threshold, iou_threshold)
|
87 |
+
if result_list is not None:
|
88 |
+
for each_list in result_list:
|
89 |
+
st.markdown(f'Label: <span style="color:rgb{each_list[1][::-1]}">{each_list[0]}</span> Conf: <span style="color:red">{"{:.3f}".format(each_list[2])}</span>', unsafe_allow_html=True)
|
90 |
+
if result_image is not None:
|
91 |
+
st.image(result_image, image_name.replace(".png","") + " result image")
|
92 |
+
else:
|
93 |
+
st.warning("Not implemented yet")
|
94 |
+
|
95 |
+
body1 = st.container()
|
96 |
+
with body1:
|
97 |
+
body1_col1, body1_col2, body1_col3 = st.columns([2,1,1])
|
98 |
with body1_col1:
|
99 |
+
st.header("Select an image")
|
100 |
+
image_cols = st.columns(5)
|
101 |
+
for i, col in enumerate(image_cols):
|
102 |
+
with col:
|
103 |
+
image = Image.open(os.path.join(image_path, each_image_name[i]))
|
104 |
+
st.image(image, each_image_name[i].replace(".png",""))
|
105 |
+
button_cols = st.columns(5)
|
106 |
+
for i, col in enumerate(button_cols):
|
107 |
+
with col:
|
108 |
+
st.button('Select', key=i, use_container_width=True, on_click=image_on_click, args=(i,))
|
109 |
+
|
110 |
+
selected_model = st.selectbox('Select the inference model', model_list)
|
111 |
+
conf_threshold = st.slider('Select the confidence threshold', 0.0, 1.0, 0.60)
|
112 |
+
iou_threshold = st.slider('Select the IOU threshold', 0.0, 1.0, 0.45)
|
113 |
+
|
114 |
+
|
115 |
+
st.markdown("""
|
116 |
+
<style>
|
117 |
+
.footer {
|
118 |
+
position: fixed;
|
119 |
+
left: 0;
|
120 |
+
bottom: 0;
|
121 |
+
width: 100%;
|
122 |
+
text-align: center;
|
123 |
+
}
|
124 |
+
</style>
|
125 |
+
<div class="footer">
|
126 |
+
<p>Our paper: <a href="#">Enhanced Object Detection in Pediatric Bronchoscopy Images using YOLO-based Algorithms with CBAM Attention Mechanism</a></p>
|
127 |
+
<p>Author: Jianqi Yan</p>
|
128 |
+
<p>Copyright © 2024, Quanbao Technologies Co. Ltd </p>
|
129 |
+
</div>
|
130 |
+
""", unsafe_allow_html=True)
|
infer/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# init
|
2 |
+
from .yolov7 import *
|
infer/yolov5/get_results.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
colors = [(np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) for _ in range(20)]
|
7 |
+
|
8 |
+
def get_yolov5_result(image, conf_threshold, iou_threshold, class_names):
|
9 |
+
weights = 'my_model/v5-n.pt'
|
10 |
+
device = 'cpu'
|
11 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path=weights, force_reload=True)
|
12 |
+
model.to(device)
|
13 |
+
model.eval()
|
14 |
+
|
15 |
+
results = model(image, size=320)
|
16 |
+
st.write(results.pandas().xyxy[0])
|
17 |
+
|
18 |
+
img0 = image.copy()
|
19 |
+
result_list = []
|
20 |
+
|
21 |
+
for i, det in enumerate(results.xyxy[0]):
|
22 |
+
if det[4] > conf_threshold:
|
23 |
+
label = class_names[int(det[5]) - 1]
|
24 |
+
color = colors[int(det[5]) % len(colors)]
|
25 |
+
result_list.append([label, color, det[4]])
|
26 |
+
cv2.rectangle(img0, (int(det[0]), int(det[1])), (int(det[2]), int(det[3])), color, 2)
|
27 |
+
cv2.putText(img0, label, (int(det[0]), int(det[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
28 |
+
|
29 |
+
img0 = img0[...,::-1]
|
30 |
+
return img0, result_list
|
31 |
+
|
32 |
+
|
infer/yolov7/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# init
|
2 |
+
from .models import *
|
infer/yolov7/cfg/baseline/r50-csp.yaml
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# CSP-ResNet backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Stem, [128]], # 0-P1/2
|
16 |
+
[-1, 3, ResCSPC, [128]],
|
17 |
+
[-1, 1, Conv, [256, 3, 2]], # 2-P3/8
|
18 |
+
[-1, 4, ResCSPC, [256]],
|
19 |
+
[-1, 1, Conv, [512, 3, 2]], # 4-P3/8
|
20 |
+
[-1, 6, ResCSPC, [512]],
|
21 |
+
[-1, 1, Conv, [1024, 3, 2]], # 6-P3/8
|
22 |
+
[-1, 3, ResCSPC, [1024]], # 7
|
23 |
+
]
|
24 |
+
|
25 |
+
# CSP-Res-PAN head
|
26 |
+
head:
|
27 |
+
[[-1, 1, SPPCSPC, [512]], # 8
|
28 |
+
[-1, 1, Conv, [256, 1, 1]],
|
29 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
30 |
+
[5, 1, Conv, [256, 1, 1]], # route backbone P4
|
31 |
+
[[-1, -2], 1, Concat, [1]],
|
32 |
+
[-1, 2, ResCSPB, [256]], # 13
|
33 |
+
[-1, 1, Conv, [128, 1, 1]],
|
34 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
35 |
+
[3, 1, Conv, [128, 1, 1]], # route backbone P3
|
36 |
+
[[-1, -2], 1, Concat, [1]],
|
37 |
+
[-1, 2, ResCSPB, [128]], # 18
|
38 |
+
[-1, 1, Conv, [256, 3, 1]],
|
39 |
+
[-2, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 13], 1, Concat, [1]], # cat
|
41 |
+
[-1, 2, ResCSPB, [256]], # 22
|
42 |
+
[-1, 1, Conv, [512, 3, 1]],
|
43 |
+
[-2, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 8], 1, Concat, [1]], # cat
|
45 |
+
[-1, 2, ResCSPB, [512]], # 26
|
46 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
47 |
+
|
48 |
+
[[19,23,27], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
|
49 |
+
]
|
infer/yolov7/cfg/baseline/x50-csp.yaml
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# CSP-ResNeXt backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Stem, [128]], # 0-P1/2
|
16 |
+
[-1, 3, ResXCSPC, [128]],
|
17 |
+
[-1, 1, Conv, [256, 3, 2]], # 2-P3/8
|
18 |
+
[-1, 4, ResXCSPC, [256]],
|
19 |
+
[-1, 1, Conv, [512, 3, 2]], # 4-P3/8
|
20 |
+
[-1, 6, ResXCSPC, [512]],
|
21 |
+
[-1, 1, Conv, [1024, 3, 2]], # 6-P3/8
|
22 |
+
[-1, 3, ResXCSPC, [1024]], # 7
|
23 |
+
]
|
24 |
+
|
25 |
+
# CSP-ResX-PAN head
|
26 |
+
head:
|
27 |
+
[[-1, 1, SPPCSPC, [512]], # 8
|
28 |
+
[-1, 1, Conv, [256, 1, 1]],
|
29 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
30 |
+
[5, 1, Conv, [256, 1, 1]], # route backbone P4
|
31 |
+
[[-1, -2], 1, Concat, [1]],
|
32 |
+
[-1, 2, ResXCSPB, [256]], # 13
|
33 |
+
[-1, 1, Conv, [128, 1, 1]],
|
34 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
35 |
+
[3, 1, Conv, [128, 1, 1]], # route backbone P3
|
36 |
+
[[-1, -2], 1, Concat, [1]],
|
37 |
+
[-1, 2, ResXCSPB, [128]], # 18
|
38 |
+
[-1, 1, Conv, [256, 3, 1]],
|
39 |
+
[-2, 1, Conv, [256, 3, 2]],
|
40 |
+
[[-1, 13], 1, Concat, [1]], # cat
|
41 |
+
[-1, 2, ResXCSPB, [256]], # 22
|
42 |
+
[-1, 1, Conv, [512, 3, 1]],
|
43 |
+
[-2, 1, Conv, [512, 3, 2]],
|
44 |
+
[[-1, 8], 1, Concat, [1]], # cat
|
45 |
+
[-1, 2, ResXCSPB, [512]], # 26
|
46 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
47 |
+
|
48 |
+
[[19,23,27], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
|
49 |
+
]
|
infer/yolov7/cfg/baseline/yolor-csp-x.yaml
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.33 # model depth multiple
|
4 |
+
width_multiple: 1.25 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# CSP-Darknet 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, BottleneckCSPC, [128]],
|
20 |
+
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
|
21 |
+
[-1, 8, BottleneckCSPC, [256]],
|
22 |
+
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
|
23 |
+
[-1, 8, BottleneckCSPC, [512]],
|
24 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
|
25 |
+
[-1, 4, BottleneckCSPC, [1024]], # 10
|
26 |
+
]
|
27 |
+
|
28 |
+
# CSP-Dark-PAN head
|
29 |
+
head:
|
30 |
+
[[-1, 1, SPPCSPC, [512]], # 11
|
31 |
+
[-1, 1, Conv, [256, 1, 1]],
|
32 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
33 |
+
[8, 1, Conv, [256, 1, 1]], # route backbone P4
|
34 |
+
[[-1, -2], 1, Concat, [1]],
|
35 |
+
[-1, 2, BottleneckCSPB, [256]], # 16
|
36 |
+
[-1, 1, Conv, [128, 1, 1]],
|
37 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
38 |
+
[6, 1, Conv, [128, 1, 1]], # route backbone P3
|
39 |
+
[[-1, -2], 1, Concat, [1]],
|
40 |
+
[-1, 2, BottleneckCSPB, [128]], # 21
|
41 |
+
[-1, 1, Conv, [256, 3, 1]],
|
42 |
+
[-2, 1, Conv, [256, 3, 2]],
|
43 |
+
[[-1, 16], 1, Concat, [1]], # cat
|
44 |
+
[-1, 2, BottleneckCSPB, [256]], # 25
|
45 |
+
[-1, 1, Conv, [512, 3, 1]],
|
46 |
+
[-2, 1, Conv, [512, 3, 2]],
|
47 |
+
[[-1, 11], 1, Concat, [1]], # cat
|
48 |
+
[-1, 2, BottleneckCSPB, [512]], # 29
|
49 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
50 |
+
|
51 |
+
[[22,26,30], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
|
52 |
+
]
|
infer/yolov7/cfg/baseline/yolor-csp.yaml
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# CSP-Darknet 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, BottleneckCSPC, [128]],
|
20 |
+
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
|
21 |
+
[-1, 8, BottleneckCSPC, [256]],
|
22 |
+
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
|
23 |
+
[-1, 8, BottleneckCSPC, [512]],
|
24 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
|
25 |
+
[-1, 4, BottleneckCSPC, [1024]], # 10
|
26 |
+
]
|
27 |
+
|
28 |
+
# CSP-Dark-PAN head
|
29 |
+
head:
|
30 |
+
[[-1, 1, SPPCSPC, [512]], # 11
|
31 |
+
[-1, 1, Conv, [256, 1, 1]],
|
32 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
33 |
+
[8, 1, Conv, [256, 1, 1]], # route backbone P4
|
34 |
+
[[-1, -2], 1, Concat, [1]],
|
35 |
+
[-1, 2, BottleneckCSPB, [256]], # 16
|
36 |
+
[-1, 1, Conv, [128, 1, 1]],
|
37 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
38 |
+
[6, 1, Conv, [128, 1, 1]], # route backbone P3
|
39 |
+
[[-1, -2], 1, Concat, [1]],
|
40 |
+
[-1, 2, BottleneckCSPB, [128]], # 21
|
41 |
+
[-1, 1, Conv, [256, 3, 1]],
|
42 |
+
[-2, 1, Conv, [256, 3, 2]],
|
43 |
+
[[-1, 16], 1, Concat, [1]], # cat
|
44 |
+
[-1, 2, BottleneckCSPB, [256]], # 25
|
45 |
+
[-1, 1, Conv, [512, 3, 1]],
|
46 |
+
[-2, 1, Conv, [512, 3, 2]],
|
47 |
+
[[-1, 11], 1, Concat, [1]], # cat
|
48 |
+
[-1, 2, BottleneckCSPB, [512]], # 29
|
49 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
50 |
+
|
51 |
+
[[22,26,30], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
|
52 |
+
]
|
infer/yolov7/cfg/baseline/yolor-d6.yaml
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # expand model depth
|
4 |
+
width_multiple: 1.25 # expand layer channels
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# CSP-Darknet backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [64, 3, 1]], # 1-P1/2
|
18 |
+
[-1, 1, DownC, [128]], # 2-P2/4
|
19 |
+
[-1, 3, BottleneckCSPA, [128]],
|
20 |
+
[-1, 1, DownC, [256]], # 4-P3/8
|
21 |
+
[-1, 15, BottleneckCSPA, [256]],
|
22 |
+
[-1, 1, DownC, [512]], # 6-P4/16
|
23 |
+
[-1, 15, BottleneckCSPA, [512]],
|
24 |
+
[-1, 1, DownC, [768]], # 8-P5/32
|
25 |
+
[-1, 7, BottleneckCSPA, [768]],
|
26 |
+
[-1, 1, DownC, [1024]], # 10-P6/64
|
27 |
+
[-1, 7, BottleneckCSPA, [1024]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# CSP-Dark-PAN head
|
31 |
+
head:
|
32 |
+
[[-1, 1, SPPCSPC, [512]], # 12
|
33 |
+
[-1, 1, Conv, [384, 1, 1]],
|
34 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
35 |
+
[-6, 1, Conv, [384, 1, 1]], # route backbone P5
|
36 |
+
[[-1, -2], 1, Concat, [1]],
|
37 |
+
[-1, 3, BottleneckCSPB, [384]], # 17
|
38 |
+
[-1, 1, Conv, [256, 1, 1]],
|
39 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
40 |
+
[-13, 1, Conv, [256, 1, 1]], # route backbone P4
|
41 |
+
[[-1, -2], 1, Concat, [1]],
|
42 |
+
[-1, 3, BottleneckCSPB, [256]], # 22
|
43 |
+
[-1, 1, Conv, [128, 1, 1]],
|
44 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
45 |
+
[-20, 1, Conv, [128, 1, 1]], # route backbone P3
|
46 |
+
[[-1, -2], 1, Concat, [1]],
|
47 |
+
[-1, 3, BottleneckCSPB, [128]], # 27
|
48 |
+
[-1, 1, Conv, [256, 3, 1]],
|
49 |
+
[-2, 1, DownC, [256]],
|
50 |
+
[[-1, 22], 1, Concat, [1]], # cat
|
51 |
+
[-1, 3, BottleneckCSPB, [256]], # 31
|
52 |
+
[-1, 1, Conv, [512, 3, 1]],
|
53 |
+
[-2, 1, DownC, [384]],
|
54 |
+
[[-1, 17], 1, Concat, [1]], # cat
|
55 |
+
[-1, 3, BottleneckCSPB, [384]], # 35
|
56 |
+
[-1, 1, Conv, [768, 3, 1]],
|
57 |
+
[-2, 1, DownC, [512]],
|
58 |
+
[[-1, 12], 1, Concat, [1]], # cat
|
59 |
+
[-1, 3, BottleneckCSPB, [512]], # 39
|
60 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
61 |
+
|
62 |
+
[[28,32,36,40], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
63 |
+
]
|
infer/yolov7/cfg/baseline/yolor-e6.yaml
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # expand model depth
|
4 |
+
width_multiple: 1.25 # expand layer channels
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# CSP-Darknet backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [64, 3, 1]], # 1-P1/2
|
18 |
+
[-1, 1, DownC, [128]], # 2-P2/4
|
19 |
+
[-1, 3, BottleneckCSPA, [128]],
|
20 |
+
[-1, 1, DownC, [256]], # 4-P3/8
|
21 |
+
[-1, 7, BottleneckCSPA, [256]],
|
22 |
+
[-1, 1, DownC, [512]], # 6-P4/16
|
23 |
+
[-1, 7, BottleneckCSPA, [512]],
|
24 |
+
[-1, 1, DownC, [768]], # 8-P5/32
|
25 |
+
[-1, 3, BottleneckCSPA, [768]],
|
26 |
+
[-1, 1, DownC, [1024]], # 10-P6/64
|
27 |
+
[-1, 3, BottleneckCSPA, [1024]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# CSP-Dark-PAN head
|
31 |
+
head:
|
32 |
+
[[-1, 1, SPPCSPC, [512]], # 12
|
33 |
+
[-1, 1, Conv, [384, 1, 1]],
|
34 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
35 |
+
[-6, 1, Conv, [384, 1, 1]], # route backbone P5
|
36 |
+
[[-1, -2], 1, Concat, [1]],
|
37 |
+
[-1, 3, BottleneckCSPB, [384]], # 17
|
38 |
+
[-1, 1, Conv, [256, 1, 1]],
|
39 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
40 |
+
[-13, 1, Conv, [256, 1, 1]], # route backbone P4
|
41 |
+
[[-1, -2], 1, Concat, [1]],
|
42 |
+
[-1, 3, BottleneckCSPB, [256]], # 22
|
43 |
+
[-1, 1, Conv, [128, 1, 1]],
|
44 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
45 |
+
[-20, 1, Conv, [128, 1, 1]], # route backbone P3
|
46 |
+
[[-1, -2], 1, Concat, [1]],
|
47 |
+
[-1, 3, BottleneckCSPB, [128]], # 27
|
48 |
+
[-1, 1, Conv, [256, 3, 1]],
|
49 |
+
[-2, 1, DownC, [256]],
|
50 |
+
[[-1, 22], 1, Concat, [1]], # cat
|
51 |
+
[-1, 3, BottleneckCSPB, [256]], # 31
|
52 |
+
[-1, 1, Conv, [512, 3, 1]],
|
53 |
+
[-2, 1, DownC, [384]],
|
54 |
+
[[-1, 17], 1, Concat, [1]], # cat
|
55 |
+
[-1, 3, BottleneckCSPB, [384]], # 35
|
56 |
+
[-1, 1, Conv, [768, 3, 1]],
|
57 |
+
[-2, 1, DownC, [512]],
|
58 |
+
[[-1, 12], 1, Concat, [1]], # cat
|
59 |
+
[-1, 3, BottleneckCSPB, [512]], # 39
|
60 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
61 |
+
|
62 |
+
[[28,32,36,40], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
63 |
+
]
|
infer/yolov7/cfg/baseline/yolor-p6.yaml
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # expand model depth
|
4 |
+
width_multiple: 1.0 # expand layer channels
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# CSP-Darknet backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [64, 3, 1]], # 1-P1/2
|
18 |
+
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4
|
19 |
+
[-1, 3, BottleneckCSPA, [128]],
|
20 |
+
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8
|
21 |
+
[-1, 7, BottleneckCSPA, [256]],
|
22 |
+
[-1, 1, Conv, [384, 3, 2]], # 6-P4/16
|
23 |
+
[-1, 7, BottleneckCSPA, [384]],
|
24 |
+
[-1, 1, Conv, [512, 3, 2]], # 8-P5/32
|
25 |
+
[-1, 3, BottleneckCSPA, [512]],
|
26 |
+
[-1, 1, Conv, [640, 3, 2]], # 10-P6/64
|
27 |
+
[-1, 3, BottleneckCSPA, [640]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# CSP-Dark-PAN head
|
31 |
+
head:
|
32 |
+
[[-1, 1, SPPCSPC, [320]], # 12
|
33 |
+
[-1, 1, Conv, [256, 1, 1]],
|
34 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
35 |
+
[-6, 1, Conv, [256, 1, 1]], # route backbone P5
|
36 |
+
[[-1, -2], 1, Concat, [1]],
|
37 |
+
[-1, 3, BottleneckCSPB, [256]], # 17
|
38 |
+
[-1, 1, Conv, [192, 1, 1]],
|
39 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
40 |
+
[-13, 1, Conv, [192, 1, 1]], # route backbone P4
|
41 |
+
[[-1, -2], 1, Concat, [1]],
|
42 |
+
[-1, 3, BottleneckCSPB, [192]], # 22
|
43 |
+
[-1, 1, Conv, [128, 1, 1]],
|
44 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
45 |
+
[-20, 1, Conv, [128, 1, 1]], # route backbone P3
|
46 |
+
[[-1, -2], 1, Concat, [1]],
|
47 |
+
[-1, 3, BottleneckCSPB, [128]], # 27
|
48 |
+
[-1, 1, Conv, [256, 3, 1]],
|
49 |
+
[-2, 1, Conv, [192, 3, 2]],
|
50 |
+
[[-1, 22], 1, Concat, [1]], # cat
|
51 |
+
[-1, 3, BottleneckCSPB, [192]], # 31
|
52 |
+
[-1, 1, Conv, [384, 3, 1]],
|
53 |
+
[-2, 1, Conv, [256, 3, 2]],
|
54 |
+
[[-1, 17], 1, Concat, [1]], # cat
|
55 |
+
[-1, 3, BottleneckCSPB, [256]], # 35
|
56 |
+
[-1, 1, Conv, [512, 3, 1]],
|
57 |
+
[-2, 1, Conv, [320, 3, 2]],
|
58 |
+
[[-1, 12], 1, Concat, [1]], # cat
|
59 |
+
[-1, 3, BottleneckCSPB, [320]], # 39
|
60 |
+
[-1, 1, Conv, [640, 3, 1]],
|
61 |
+
|
62 |
+
[[28,32,36,40], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
63 |
+
]
|
infer/yolov7/cfg/baseline/yolor-w6.yaml
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # expand model depth
|
4 |
+
width_multiple: 1.0 # expand layer channels
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# CSP-Darknet backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [64, 3, 1]], # 1-P1/2
|
18 |
+
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4
|
19 |
+
[-1, 3, BottleneckCSPA, [128]],
|
20 |
+
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8
|
21 |
+
[-1, 7, BottleneckCSPA, [256]],
|
22 |
+
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16
|
23 |
+
[-1, 7, BottleneckCSPA, [512]],
|
24 |
+
[-1, 1, Conv, [768, 3, 2]], # 8-P5/32
|
25 |
+
[-1, 3, BottleneckCSPA, [768]],
|
26 |
+
[-1, 1, Conv, [1024, 3, 2]], # 10-P6/64
|
27 |
+
[-1, 3, BottleneckCSPA, [1024]], # 11
|
28 |
+
]
|
29 |
+
|
30 |
+
# CSP-Dark-PAN head
|
31 |
+
head:
|
32 |
+
[[-1, 1, SPPCSPC, [512]], # 12
|
33 |
+
[-1, 1, Conv, [384, 1, 1]],
|
34 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
35 |
+
[-6, 1, Conv, [384, 1, 1]], # route backbone P5
|
36 |
+
[[-1, -2], 1, Concat, [1]],
|
37 |
+
[-1, 3, BottleneckCSPB, [384]], # 17
|
38 |
+
[-1, 1, Conv, [256, 1, 1]],
|
39 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
40 |
+
[-13, 1, Conv, [256, 1, 1]], # route backbone P4
|
41 |
+
[[-1, -2], 1, Concat, [1]],
|
42 |
+
[-1, 3, BottleneckCSPB, [256]], # 22
|
43 |
+
[-1, 1, Conv, [128, 1, 1]],
|
44 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
45 |
+
[-20, 1, Conv, [128, 1, 1]], # route backbone P3
|
46 |
+
[[-1, -2], 1, Concat, [1]],
|
47 |
+
[-1, 3, BottleneckCSPB, [128]], # 27
|
48 |
+
[-1, 1, Conv, [256, 3, 1]],
|
49 |
+
[-2, 1, Conv, [256, 3, 2]],
|
50 |
+
[[-1, 22], 1, Concat, [1]], # cat
|
51 |
+
[-1, 3, BottleneckCSPB, [256]], # 31
|
52 |
+
[-1, 1, Conv, [512, 3, 1]],
|
53 |
+
[-2, 1, Conv, [384, 3, 2]],
|
54 |
+
[[-1, 17], 1, Concat, [1]], # cat
|
55 |
+
[-1, 3, BottleneckCSPB, [384]], # 35
|
56 |
+
[-1, 1, Conv, [768, 3, 1]],
|
57 |
+
[-2, 1, Conv, [512, 3, 2]],
|
58 |
+
[[-1, 12], 1, Concat, [1]], # cat
|
59 |
+
[-1, 3, BottleneckCSPB, [512]], # 39
|
60 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
61 |
+
|
62 |
+
[[28,32,36,40], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
63 |
+
]
|
infer/yolov7/cfg/baseline/yolov3-spp.yaml
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
]
|
infer/yolov7/cfg/baseline/yolov3.yaml
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
]
|
infer/yolov7/cfg/baseline/yolov4-csp.yaml
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# CSP-Darknet 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, BottleneckCSPC, [128]],
|
20 |
+
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
|
21 |
+
[-1, 8, BottleneckCSPC, [256]],
|
22 |
+
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
|
23 |
+
[-1, 8, BottleneckCSPC, [512]],
|
24 |
+
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
|
25 |
+
[-1, 4, BottleneckCSPC, [1024]], # 10
|
26 |
+
]
|
27 |
+
|
28 |
+
# CSP-Dark-PAN head
|
29 |
+
head:
|
30 |
+
[[-1, 1, SPPCSPC, [512]], # 11
|
31 |
+
[-1, 1, Conv, [256, 1, 1]],
|
32 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
33 |
+
[8, 1, Conv, [256, 1, 1]], # route backbone P4
|
34 |
+
[[-1, -2], 1, Concat, [1]],
|
35 |
+
[-1, 2, BottleneckCSPB, [256]], # 16
|
36 |
+
[-1, 1, Conv, [128, 1, 1]],
|
37 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
38 |
+
[6, 1, Conv, [128, 1, 1]], # route backbone P3
|
39 |
+
[[-1, -2], 1, Concat, [1]],
|
40 |
+
[-1, 2, BottleneckCSPB, [128]], # 21
|
41 |
+
[-1, 1, Conv, [256, 3, 1]],
|
42 |
+
[-2, 1, Conv, [256, 3, 2]],
|
43 |
+
[[-1, 16], 1, Concat, [1]], # cat
|
44 |
+
[-1, 2, BottleneckCSPB, [256]], # 25
|
45 |
+
[-1, 1, Conv, [512, 3, 1]],
|
46 |
+
[-2, 1, Conv, [512, 3, 2]],
|
47 |
+
[[-1, 11], 1, Concat, [1]], # cat
|
48 |
+
[-1, 2, BottleneckCSPB, [512]], # 29
|
49 |
+
[-1, 1, Conv, [1024, 3, 1]],
|
50 |
+
|
51 |
+
[[22,26,30], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
52 |
+
]
|
infer/yolov7/cfg/deploy/yolov7-d6.yaml
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7-d6 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args],
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [96, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, DownC, [192]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[-1, 1, Conv, [64, 3, 1]],
|
29 |
+
[-1, 1, Conv, [64, 3, 1]],
|
30 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
31 |
+
[-1, 1, Conv, [192, 1, 1]], # 14
|
32 |
+
|
33 |
+
[-1, 1, DownC, [384]], # 15-P3/8
|
34 |
+
[-1, 1, Conv, [128, 1, 1]],
|
35 |
+
[-2, 1, Conv, [128, 1, 1]],
|
36 |
+
[-1, 1, Conv, [128, 3, 1]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[-1, 1, Conv, [128, 3, 1]],
|
41 |
+
[-1, 1, Conv, [128, 3, 1]],
|
42 |
+
[-1, 1, Conv, [128, 3, 1]],
|
43 |
+
[-1, 1, Conv, [128, 3, 1]],
|
44 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
45 |
+
[-1, 1, Conv, [384, 1, 1]], # 27
|
46 |
+
|
47 |
+
[-1, 1, DownC, [768]], # 28-P4/16
|
48 |
+
[-1, 1, Conv, [256, 1, 1]],
|
49 |
+
[-2, 1, Conv, [256, 1, 1]],
|
50 |
+
[-1, 1, Conv, [256, 3, 1]],
|
51 |
+
[-1, 1, Conv, [256, 3, 1]],
|
52 |
+
[-1, 1, Conv, [256, 3, 1]],
|
53 |
+
[-1, 1, Conv, [256, 3, 1]],
|
54 |
+
[-1, 1, Conv, [256, 3, 1]],
|
55 |
+
[-1, 1, Conv, [256, 3, 1]],
|
56 |
+
[-1, 1, Conv, [256, 3, 1]],
|
57 |
+
[-1, 1, Conv, [256, 3, 1]],
|
58 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
59 |
+
[-1, 1, Conv, [768, 1, 1]], # 40
|
60 |
+
|
61 |
+
[-1, 1, DownC, [1152]], # 41-P5/32
|
62 |
+
[-1, 1, Conv, [384, 1, 1]],
|
63 |
+
[-2, 1, Conv, [384, 1, 1]],
|
64 |
+
[-1, 1, Conv, [384, 3, 1]],
|
65 |
+
[-1, 1, Conv, [384, 3, 1]],
|
66 |
+
[-1, 1, Conv, [384, 3, 1]],
|
67 |
+
[-1, 1, Conv, [384, 3, 1]],
|
68 |
+
[-1, 1, Conv, [384, 3, 1]],
|
69 |
+
[-1, 1, Conv, [384, 3, 1]],
|
70 |
+
[-1, 1, Conv, [384, 3, 1]],
|
71 |
+
[-1, 1, Conv, [384, 3, 1]],
|
72 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
73 |
+
[-1, 1, Conv, [1152, 1, 1]], # 53
|
74 |
+
|
75 |
+
[-1, 1, DownC, [1536]], # 54-P6/64
|
76 |
+
[-1, 1, Conv, [512, 1, 1]],
|
77 |
+
[-2, 1, Conv, [512, 1, 1]],
|
78 |
+
[-1, 1, Conv, [512, 3, 1]],
|
79 |
+
[-1, 1, Conv, [512, 3, 1]],
|
80 |
+
[-1, 1, Conv, [512, 3, 1]],
|
81 |
+
[-1, 1, Conv, [512, 3, 1]],
|
82 |
+
[-1, 1, Conv, [512, 3, 1]],
|
83 |
+
[-1, 1, Conv, [512, 3, 1]],
|
84 |
+
[-1, 1, Conv, [512, 3, 1]],
|
85 |
+
[-1, 1, Conv, [512, 3, 1]],
|
86 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
87 |
+
[-1, 1, Conv, [1536, 1, 1]], # 66
|
88 |
+
]
|
89 |
+
|
90 |
+
# yolov7-d6 head
|
91 |
+
head:
|
92 |
+
[[-1, 1, SPPCSPC, [768]], # 67
|
93 |
+
|
94 |
+
[-1, 1, Conv, [576, 1, 1]],
|
95 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
96 |
+
[53, 1, Conv, [576, 1, 1]], # route backbone P5
|
97 |
+
[[-1, -2], 1, Concat, [1]],
|
98 |
+
|
99 |
+
[-1, 1, Conv, [384, 1, 1]],
|
100 |
+
[-2, 1, Conv, [384, 1, 1]],
|
101 |
+
[-1, 1, Conv, [192, 3, 1]],
|
102 |
+
[-1, 1, Conv, [192, 3, 1]],
|
103 |
+
[-1, 1, Conv, [192, 3, 1]],
|
104 |
+
[-1, 1, Conv, [192, 3, 1]],
|
105 |
+
[-1, 1, Conv, [192, 3, 1]],
|
106 |
+
[-1, 1, Conv, [192, 3, 1]],
|
107 |
+
[-1, 1, Conv, [192, 3, 1]],
|
108 |
+
[-1, 1, Conv, [192, 3, 1]],
|
109 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
110 |
+
[-1, 1, Conv, [576, 1, 1]], # 83
|
111 |
+
|
112 |
+
[-1, 1, Conv, [384, 1, 1]],
|
113 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
114 |
+
[40, 1, Conv, [384, 1, 1]], # route backbone P4
|
115 |
+
[[-1, -2], 1, Concat, [1]],
|
116 |
+
|
117 |
+
[-1, 1, Conv, [256, 1, 1]],
|
118 |
+
[-2, 1, Conv, [256, 1, 1]],
|
119 |
+
[-1, 1, Conv, [128, 3, 1]],
|
120 |
+
[-1, 1, Conv, [128, 3, 1]],
|
121 |
+
[-1, 1, Conv, [128, 3, 1]],
|
122 |
+
[-1, 1, Conv, [128, 3, 1]],
|
123 |
+
[-1, 1, Conv, [128, 3, 1]],
|
124 |
+
[-1, 1, Conv, [128, 3, 1]],
|
125 |
+
[-1, 1, Conv, [128, 3, 1]],
|
126 |
+
[-1, 1, Conv, [128, 3, 1]],
|
127 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
128 |
+
[-1, 1, Conv, [384, 1, 1]], # 99
|
129 |
+
|
130 |
+
[-1, 1, Conv, [192, 1, 1]],
|
131 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
132 |
+
[27, 1, Conv, [192, 1, 1]], # route backbone P3
|
133 |
+
[[-1, -2], 1, Concat, [1]],
|
134 |
+
|
135 |
+
[-1, 1, Conv, [128, 1, 1]],
|
136 |
+
[-2, 1, Conv, [128, 1, 1]],
|
137 |
+
[-1, 1, Conv, [64, 3, 1]],
|
138 |
+
[-1, 1, Conv, [64, 3, 1]],
|
139 |
+
[-1, 1, Conv, [64, 3, 1]],
|
140 |
+
[-1, 1, Conv, [64, 3, 1]],
|
141 |
+
[-1, 1, Conv, [64, 3, 1]],
|
142 |
+
[-1, 1, Conv, [64, 3, 1]],
|
143 |
+
[-1, 1, Conv, [64, 3, 1]],
|
144 |
+
[-1, 1, Conv, [64, 3, 1]],
|
145 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
146 |
+
[-1, 1, Conv, [192, 1, 1]], # 115
|
147 |
+
|
148 |
+
[-1, 1, DownC, [384]],
|
149 |
+
[[-1, 99], 1, Concat, [1]],
|
150 |
+
|
151 |
+
[-1, 1, Conv, [256, 1, 1]],
|
152 |
+
[-2, 1, Conv, [256, 1, 1]],
|
153 |
+
[-1, 1, Conv, [128, 3, 1]],
|
154 |
+
[-1, 1, Conv, [128, 3, 1]],
|
155 |
+
[-1, 1, Conv, [128, 3, 1]],
|
156 |
+
[-1, 1, Conv, [128, 3, 1]],
|
157 |
+
[-1, 1, Conv, [128, 3, 1]],
|
158 |
+
[-1, 1, Conv, [128, 3, 1]],
|
159 |
+
[-1, 1, Conv, [128, 3, 1]],
|
160 |
+
[-1, 1, Conv, [128, 3, 1]],
|
161 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
162 |
+
[-1, 1, Conv, [384, 1, 1]], # 129
|
163 |
+
|
164 |
+
[-1, 1, DownC, [576]],
|
165 |
+
[[-1, 83], 1, Concat, [1]],
|
166 |
+
|
167 |
+
[-1, 1, Conv, [384, 1, 1]],
|
168 |
+
[-2, 1, Conv, [384, 1, 1]],
|
169 |
+
[-1, 1, Conv, [192, 3, 1]],
|
170 |
+
[-1, 1, Conv, [192, 3, 1]],
|
171 |
+
[-1, 1, Conv, [192, 3, 1]],
|
172 |
+
[-1, 1, Conv, [192, 3, 1]],
|
173 |
+
[-1, 1, Conv, [192, 3, 1]],
|
174 |
+
[-1, 1, Conv, [192, 3, 1]],
|
175 |
+
[-1, 1, Conv, [192, 3, 1]],
|
176 |
+
[-1, 1, Conv, [192, 3, 1]],
|
177 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
178 |
+
[-1, 1, Conv, [576, 1, 1]], # 143
|
179 |
+
|
180 |
+
[-1, 1, DownC, [768]],
|
181 |
+
[[-1, 67], 1, Concat, [1]],
|
182 |
+
|
183 |
+
[-1, 1, Conv, [512, 1, 1]],
|
184 |
+
[-2, 1, Conv, [512, 1, 1]],
|
185 |
+
[-1, 1, Conv, [256, 3, 1]],
|
186 |
+
[-1, 1, Conv, [256, 3, 1]],
|
187 |
+
[-1, 1, Conv, [256, 3, 1]],
|
188 |
+
[-1, 1, Conv, [256, 3, 1]],
|
189 |
+
[-1, 1, Conv, [256, 3, 1]],
|
190 |
+
[-1, 1, Conv, [256, 3, 1]],
|
191 |
+
[-1, 1, Conv, [256, 3, 1]],
|
192 |
+
[-1, 1, Conv, [256, 3, 1]],
|
193 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
194 |
+
[-1, 1, Conv, [768, 1, 1]], # 157
|
195 |
+
|
196 |
+
[115, 1, Conv, [384, 3, 1]],
|
197 |
+
[129, 1, Conv, [768, 3, 1]],
|
198 |
+
[143, 1, Conv, [1152, 3, 1]],
|
199 |
+
[157, 1, Conv, [1536, 3, 1]],
|
200 |
+
|
201 |
+
[[158,159,160,161], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
202 |
+
]
|
infer/yolov7/cfg/deploy/yolov7-e6.yaml
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7-e6 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args],
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [80, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, DownC, [160]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
29 |
+
[-1, 1, Conv, [160, 1, 1]], # 12
|
30 |
+
|
31 |
+
[-1, 1, DownC, [320]], # 13-P3/8
|
32 |
+
[-1, 1, Conv, [128, 1, 1]],
|
33 |
+
[-2, 1, Conv, [128, 1, 1]],
|
34 |
+
[-1, 1, Conv, [128, 3, 1]],
|
35 |
+
[-1, 1, Conv, [128, 3, 1]],
|
36 |
+
[-1, 1, Conv, [128, 3, 1]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
41 |
+
[-1, 1, Conv, [320, 1, 1]], # 23
|
42 |
+
|
43 |
+
[-1, 1, DownC, [640]], # 24-P4/16
|
44 |
+
[-1, 1, Conv, [256, 1, 1]],
|
45 |
+
[-2, 1, Conv, [256, 1, 1]],
|
46 |
+
[-1, 1, Conv, [256, 3, 1]],
|
47 |
+
[-1, 1, Conv, [256, 3, 1]],
|
48 |
+
[-1, 1, Conv, [256, 3, 1]],
|
49 |
+
[-1, 1, Conv, [256, 3, 1]],
|
50 |
+
[-1, 1, Conv, [256, 3, 1]],
|
51 |
+
[-1, 1, Conv, [256, 3, 1]],
|
52 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
53 |
+
[-1, 1, Conv, [640, 1, 1]], # 34
|
54 |
+
|
55 |
+
[-1, 1, DownC, [960]], # 35-P5/32
|
56 |
+
[-1, 1, Conv, [384, 1, 1]],
|
57 |
+
[-2, 1, Conv, [384, 1, 1]],
|
58 |
+
[-1, 1, Conv, [384, 3, 1]],
|
59 |
+
[-1, 1, Conv, [384, 3, 1]],
|
60 |
+
[-1, 1, Conv, [384, 3, 1]],
|
61 |
+
[-1, 1, Conv, [384, 3, 1]],
|
62 |
+
[-1, 1, Conv, [384, 3, 1]],
|
63 |
+
[-1, 1, Conv, [384, 3, 1]],
|
64 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
65 |
+
[-1, 1, Conv, [960, 1, 1]], # 45
|
66 |
+
|
67 |
+
[-1, 1, DownC, [1280]], # 46-P6/64
|
68 |
+
[-1, 1, Conv, [512, 1, 1]],
|
69 |
+
[-2, 1, Conv, [512, 1, 1]],
|
70 |
+
[-1, 1, Conv, [512, 3, 1]],
|
71 |
+
[-1, 1, Conv, [512, 3, 1]],
|
72 |
+
[-1, 1, Conv, [512, 3, 1]],
|
73 |
+
[-1, 1, Conv, [512, 3, 1]],
|
74 |
+
[-1, 1, Conv, [512, 3, 1]],
|
75 |
+
[-1, 1, Conv, [512, 3, 1]],
|
76 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
77 |
+
[-1, 1, Conv, [1280, 1, 1]], # 56
|
78 |
+
]
|
79 |
+
|
80 |
+
# yolov7-e6 head
|
81 |
+
head:
|
82 |
+
[[-1, 1, SPPCSPC, [640]], # 57
|
83 |
+
|
84 |
+
[-1, 1, Conv, [480, 1, 1]],
|
85 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
86 |
+
[45, 1, Conv, [480, 1, 1]], # route backbone P5
|
87 |
+
[[-1, -2], 1, Concat, [1]],
|
88 |
+
|
89 |
+
[-1, 1, Conv, [384, 1, 1]],
|
90 |
+
[-2, 1, Conv, [384, 1, 1]],
|
91 |
+
[-1, 1, Conv, [192, 3, 1]],
|
92 |
+
[-1, 1, Conv, [192, 3, 1]],
|
93 |
+
[-1, 1, Conv, [192, 3, 1]],
|
94 |
+
[-1, 1, Conv, [192, 3, 1]],
|
95 |
+
[-1, 1, Conv, [192, 3, 1]],
|
96 |
+
[-1, 1, Conv, [192, 3, 1]],
|
97 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
98 |
+
[-1, 1, Conv, [480, 1, 1]], # 71
|
99 |
+
|
100 |
+
[-1, 1, Conv, [320, 1, 1]],
|
101 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
102 |
+
[34, 1, Conv, [320, 1, 1]], # route backbone P4
|
103 |
+
[[-1, -2], 1, Concat, [1]],
|
104 |
+
|
105 |
+
[-1, 1, Conv, [256, 1, 1]],
|
106 |
+
[-2, 1, Conv, [256, 1, 1]],
|
107 |
+
[-1, 1, Conv, [128, 3, 1]],
|
108 |
+
[-1, 1, Conv, [128, 3, 1]],
|
109 |
+
[-1, 1, Conv, [128, 3, 1]],
|
110 |
+
[-1, 1, Conv, [128, 3, 1]],
|
111 |
+
[-1, 1, Conv, [128, 3, 1]],
|
112 |
+
[-1, 1, Conv, [128, 3, 1]],
|
113 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
114 |
+
[-1, 1, Conv, [320, 1, 1]], # 85
|
115 |
+
|
116 |
+
[-1, 1, Conv, [160, 1, 1]],
|
117 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
118 |
+
[23, 1, Conv, [160, 1, 1]], # route backbone P3
|
119 |
+
[[-1, -2], 1, Concat, [1]],
|
120 |
+
|
121 |
+
[-1, 1, Conv, [128, 1, 1]],
|
122 |
+
[-2, 1, Conv, [128, 1, 1]],
|
123 |
+
[-1, 1, Conv, [64, 3, 1]],
|
124 |
+
[-1, 1, Conv, [64, 3, 1]],
|
125 |
+
[-1, 1, Conv, [64, 3, 1]],
|
126 |
+
[-1, 1, Conv, [64, 3, 1]],
|
127 |
+
[-1, 1, Conv, [64, 3, 1]],
|
128 |
+
[-1, 1, Conv, [64, 3, 1]],
|
129 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
130 |
+
[-1, 1, Conv, [160, 1, 1]], # 99
|
131 |
+
|
132 |
+
[-1, 1, DownC, [320]],
|
133 |
+
[[-1, 85], 1, Concat, [1]],
|
134 |
+
|
135 |
+
[-1, 1, Conv, [256, 1, 1]],
|
136 |
+
[-2, 1, Conv, [256, 1, 1]],
|
137 |
+
[-1, 1, Conv, [128, 3, 1]],
|
138 |
+
[-1, 1, Conv, [128, 3, 1]],
|
139 |
+
[-1, 1, Conv, [128, 3, 1]],
|
140 |
+
[-1, 1, Conv, [128, 3, 1]],
|
141 |
+
[-1, 1, Conv, [128, 3, 1]],
|
142 |
+
[-1, 1, Conv, [128, 3, 1]],
|
143 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
144 |
+
[-1, 1, Conv, [320, 1, 1]], # 111
|
145 |
+
|
146 |
+
[-1, 1, DownC, [480]],
|
147 |
+
[[-1, 71], 1, Concat, [1]],
|
148 |
+
|
149 |
+
[-1, 1, Conv, [384, 1, 1]],
|
150 |
+
[-2, 1, Conv, [384, 1, 1]],
|
151 |
+
[-1, 1, Conv, [192, 3, 1]],
|
152 |
+
[-1, 1, Conv, [192, 3, 1]],
|
153 |
+
[-1, 1, Conv, [192, 3, 1]],
|
154 |
+
[-1, 1, Conv, [192, 3, 1]],
|
155 |
+
[-1, 1, Conv, [192, 3, 1]],
|
156 |
+
[-1, 1, Conv, [192, 3, 1]],
|
157 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
158 |
+
[-1, 1, Conv, [480, 1, 1]], # 123
|
159 |
+
|
160 |
+
[-1, 1, DownC, [640]],
|
161 |
+
[[-1, 57], 1, Concat, [1]],
|
162 |
+
|
163 |
+
[-1, 1, Conv, [512, 1, 1]],
|
164 |
+
[-2, 1, Conv, [512, 1, 1]],
|
165 |
+
[-1, 1, Conv, [256, 3, 1]],
|
166 |
+
[-1, 1, Conv, [256, 3, 1]],
|
167 |
+
[-1, 1, Conv, [256, 3, 1]],
|
168 |
+
[-1, 1, Conv, [256, 3, 1]],
|
169 |
+
[-1, 1, Conv, [256, 3, 1]],
|
170 |
+
[-1, 1, Conv, [256, 3, 1]],
|
171 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
172 |
+
[-1, 1, Conv, [640, 1, 1]], # 135
|
173 |
+
|
174 |
+
[99, 1, Conv, [320, 3, 1]],
|
175 |
+
[111, 1, Conv, [640, 3, 1]],
|
176 |
+
[123, 1, Conv, [960, 3, 1]],
|
177 |
+
[135, 1, Conv, [1280, 3, 1]],
|
178 |
+
|
179 |
+
[[136,137,138,139], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
180 |
+
]
|
infer/yolov7/cfg/deploy/yolov7-e6e.yaml
ADDED
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7-e6e backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args],
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [80, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, DownC, [160]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
29 |
+
[-1, 1, Conv, [160, 1, 1]], # 12
|
30 |
+
[-11, 1, Conv, [64, 1, 1]],
|
31 |
+
[-12, 1, Conv, [64, 1, 1]],
|
32 |
+
[-1, 1, Conv, [64, 3, 1]],
|
33 |
+
[-1, 1, Conv, [64, 3, 1]],
|
34 |
+
[-1, 1, Conv, [64, 3, 1]],
|
35 |
+
[-1, 1, Conv, [64, 3, 1]],
|
36 |
+
[-1, 1, Conv, [64, 3, 1]],
|
37 |
+
[-1, 1, Conv, [64, 3, 1]],
|
38 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
39 |
+
[-1, 1, Conv, [160, 1, 1]], # 22
|
40 |
+
[[-1, -11], 1, Shortcut, [1]], # 23
|
41 |
+
|
42 |
+
[-1, 1, DownC, [320]], # 24-P3/8
|
43 |
+
[-1, 1, Conv, [128, 1, 1]],
|
44 |
+
[-2, 1, Conv, [128, 1, 1]],
|
45 |
+
[-1, 1, Conv, [128, 3, 1]],
|
46 |
+
[-1, 1, Conv, [128, 3, 1]],
|
47 |
+
[-1, 1, Conv, [128, 3, 1]],
|
48 |
+
[-1, 1, Conv, [128, 3, 1]],
|
49 |
+
[-1, 1, Conv, [128, 3, 1]],
|
50 |
+
[-1, 1, Conv, [128, 3, 1]],
|
51 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
52 |
+
[-1, 1, Conv, [320, 1, 1]], # 34
|
53 |
+
[-11, 1, Conv, [128, 1, 1]],
|
54 |
+
[-12, 1, Conv, [128, 1, 1]],
|
55 |
+
[-1, 1, Conv, [128, 3, 1]],
|
56 |
+
[-1, 1, Conv, [128, 3, 1]],
|
57 |
+
[-1, 1, Conv, [128, 3, 1]],
|
58 |
+
[-1, 1, Conv, [128, 3, 1]],
|
59 |
+
[-1, 1, Conv, [128, 3, 1]],
|
60 |
+
[-1, 1, Conv, [128, 3, 1]],
|
61 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
62 |
+
[-1, 1, Conv, [320, 1, 1]], # 44
|
63 |
+
[[-1, -11], 1, Shortcut, [1]], # 45
|
64 |
+
|
65 |
+
[-1, 1, DownC, [640]], # 46-P4/16
|
66 |
+
[-1, 1, Conv, [256, 1, 1]],
|
67 |
+
[-2, 1, Conv, [256, 1, 1]],
|
68 |
+
[-1, 1, Conv, [256, 3, 1]],
|
69 |
+
[-1, 1, Conv, [256, 3, 1]],
|
70 |
+
[-1, 1, Conv, [256, 3, 1]],
|
71 |
+
[-1, 1, Conv, [256, 3, 1]],
|
72 |
+
[-1, 1, Conv, [256, 3, 1]],
|
73 |
+
[-1, 1, Conv, [256, 3, 1]],
|
74 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
75 |
+
[-1, 1, Conv, [640, 1, 1]], # 56
|
76 |
+
[-11, 1, Conv, [256, 1, 1]],
|
77 |
+
[-12, 1, Conv, [256, 1, 1]],
|
78 |
+
[-1, 1, Conv, [256, 3, 1]],
|
79 |
+
[-1, 1, Conv, [256, 3, 1]],
|
80 |
+
[-1, 1, Conv, [256, 3, 1]],
|
81 |
+
[-1, 1, Conv, [256, 3, 1]],
|
82 |
+
[-1, 1, Conv, [256, 3, 1]],
|
83 |
+
[-1, 1, Conv, [256, 3, 1]],
|
84 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
85 |
+
[-1, 1, Conv, [640, 1, 1]], # 66
|
86 |
+
[[-1, -11], 1, Shortcut, [1]], # 67
|
87 |
+
|
88 |
+
[-1, 1, DownC, [960]], # 68-P5/32
|
89 |
+
[-1, 1, Conv, [384, 1, 1]],
|
90 |
+
[-2, 1, Conv, [384, 1, 1]],
|
91 |
+
[-1, 1, Conv, [384, 3, 1]],
|
92 |
+
[-1, 1, Conv, [384, 3, 1]],
|
93 |
+
[-1, 1, Conv, [384, 3, 1]],
|
94 |
+
[-1, 1, Conv, [384, 3, 1]],
|
95 |
+
[-1, 1, Conv, [384, 3, 1]],
|
96 |
+
[-1, 1, Conv, [384, 3, 1]],
|
97 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
98 |
+
[-1, 1, Conv, [960, 1, 1]], # 78
|
99 |
+
[-11, 1, Conv, [384, 1, 1]],
|
100 |
+
[-12, 1, Conv, [384, 1, 1]],
|
101 |
+
[-1, 1, Conv, [384, 3, 1]],
|
102 |
+
[-1, 1, Conv, [384, 3, 1]],
|
103 |
+
[-1, 1, Conv, [384, 3, 1]],
|
104 |
+
[-1, 1, Conv, [384, 3, 1]],
|
105 |
+
[-1, 1, Conv, [384, 3, 1]],
|
106 |
+
[-1, 1, Conv, [384, 3, 1]],
|
107 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
108 |
+
[-1, 1, Conv, [960, 1, 1]], # 88
|
109 |
+
[[-1, -11], 1, Shortcut, [1]], # 89
|
110 |
+
|
111 |
+
[-1, 1, DownC, [1280]], # 90-P6/64
|
112 |
+
[-1, 1, Conv, [512, 1, 1]],
|
113 |
+
[-2, 1, Conv, [512, 1, 1]],
|
114 |
+
[-1, 1, Conv, [512, 3, 1]],
|
115 |
+
[-1, 1, Conv, [512, 3, 1]],
|
116 |
+
[-1, 1, Conv, [512, 3, 1]],
|
117 |
+
[-1, 1, Conv, [512, 3, 1]],
|
118 |
+
[-1, 1, Conv, [512, 3, 1]],
|
119 |
+
[-1, 1, Conv, [512, 3, 1]],
|
120 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
121 |
+
[-1, 1, Conv, [1280, 1, 1]], # 100
|
122 |
+
[-11, 1, Conv, [512, 1, 1]],
|
123 |
+
[-12, 1, Conv, [512, 1, 1]],
|
124 |
+
[-1, 1, Conv, [512, 3, 1]],
|
125 |
+
[-1, 1, Conv, [512, 3, 1]],
|
126 |
+
[-1, 1, Conv, [512, 3, 1]],
|
127 |
+
[-1, 1, Conv, [512, 3, 1]],
|
128 |
+
[-1, 1, Conv, [512, 3, 1]],
|
129 |
+
[-1, 1, Conv, [512, 3, 1]],
|
130 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
131 |
+
[-1, 1, Conv, [1280, 1, 1]], # 110
|
132 |
+
[[-1, -11], 1, Shortcut, [1]], # 111
|
133 |
+
]
|
134 |
+
|
135 |
+
# yolov7-e6e head
|
136 |
+
head:
|
137 |
+
[[-1, 1, SPPCSPC, [640]], # 112
|
138 |
+
|
139 |
+
[-1, 1, Conv, [480, 1, 1]],
|
140 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
141 |
+
[89, 1, Conv, [480, 1, 1]], # route backbone P5
|
142 |
+
[[-1, -2], 1, Concat, [1]],
|
143 |
+
|
144 |
+
[-1, 1, Conv, [384, 1, 1]],
|
145 |
+
[-2, 1, Conv, [384, 1, 1]],
|
146 |
+
[-1, 1, Conv, [192, 3, 1]],
|
147 |
+
[-1, 1, Conv, [192, 3, 1]],
|
148 |
+
[-1, 1, Conv, [192, 3, 1]],
|
149 |
+
[-1, 1, Conv, [192, 3, 1]],
|
150 |
+
[-1, 1, Conv, [192, 3, 1]],
|
151 |
+
[-1, 1, Conv, [192, 3, 1]],
|
152 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
153 |
+
[-1, 1, Conv, [480, 1, 1]], # 126
|
154 |
+
[-11, 1, Conv, [384, 1, 1]],
|
155 |
+
[-12, 1, Conv, [384, 1, 1]],
|
156 |
+
[-1, 1, Conv, [192, 3, 1]],
|
157 |
+
[-1, 1, Conv, [192, 3, 1]],
|
158 |
+
[-1, 1, Conv, [192, 3, 1]],
|
159 |
+
[-1, 1, Conv, [192, 3, 1]],
|
160 |
+
[-1, 1, Conv, [192, 3, 1]],
|
161 |
+
[-1, 1, Conv, [192, 3, 1]],
|
162 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
163 |
+
[-1, 1, Conv, [480, 1, 1]], # 136
|
164 |
+
[[-1, -11], 1, Shortcut, [1]], # 137
|
165 |
+
|
166 |
+
[-1, 1, Conv, [320, 1, 1]],
|
167 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
168 |
+
[67, 1, Conv, [320, 1, 1]], # route backbone P4
|
169 |
+
[[-1, -2], 1, Concat, [1]],
|
170 |
+
|
171 |
+
[-1, 1, Conv, [256, 1, 1]],
|
172 |
+
[-2, 1, Conv, [256, 1, 1]],
|
173 |
+
[-1, 1, Conv, [128, 3, 1]],
|
174 |
+
[-1, 1, Conv, [128, 3, 1]],
|
175 |
+
[-1, 1, Conv, [128, 3, 1]],
|
176 |
+
[-1, 1, Conv, [128, 3, 1]],
|
177 |
+
[-1, 1, Conv, [128, 3, 1]],
|
178 |
+
[-1, 1, Conv, [128, 3, 1]],
|
179 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
180 |
+
[-1, 1, Conv, [320, 1, 1]], # 151
|
181 |
+
[-11, 1, Conv, [256, 1, 1]],
|
182 |
+
[-12, 1, Conv, [256, 1, 1]],
|
183 |
+
[-1, 1, Conv, [128, 3, 1]],
|
184 |
+
[-1, 1, Conv, [128, 3, 1]],
|
185 |
+
[-1, 1, Conv, [128, 3, 1]],
|
186 |
+
[-1, 1, Conv, [128, 3, 1]],
|
187 |
+
[-1, 1, Conv, [128, 3, 1]],
|
188 |
+
[-1, 1, Conv, [128, 3, 1]],
|
189 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
190 |
+
[-1, 1, Conv, [320, 1, 1]], # 161
|
191 |
+
[[-1, -11], 1, Shortcut, [1]], # 162
|
192 |
+
|
193 |
+
[-1, 1, Conv, [160, 1, 1]],
|
194 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
195 |
+
[45, 1, Conv, [160, 1, 1]], # route backbone P3
|
196 |
+
[[-1, -2], 1, Concat, [1]],
|
197 |
+
|
198 |
+
[-1, 1, Conv, [128, 1, 1]],
|
199 |
+
[-2, 1, Conv, [128, 1, 1]],
|
200 |
+
[-1, 1, Conv, [64, 3, 1]],
|
201 |
+
[-1, 1, Conv, [64, 3, 1]],
|
202 |
+
[-1, 1, Conv, [64, 3, 1]],
|
203 |
+
[-1, 1, Conv, [64, 3, 1]],
|
204 |
+
[-1, 1, Conv, [64, 3, 1]],
|
205 |
+
[-1, 1, Conv, [64, 3, 1]],
|
206 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
207 |
+
[-1, 1, Conv, [160, 1, 1]], # 176
|
208 |
+
[-11, 1, Conv, [128, 1, 1]],
|
209 |
+
[-12, 1, Conv, [128, 1, 1]],
|
210 |
+
[-1, 1, Conv, [64, 3, 1]],
|
211 |
+
[-1, 1, Conv, [64, 3, 1]],
|
212 |
+
[-1, 1, Conv, [64, 3, 1]],
|
213 |
+
[-1, 1, Conv, [64, 3, 1]],
|
214 |
+
[-1, 1, Conv, [64, 3, 1]],
|
215 |
+
[-1, 1, Conv, [64, 3, 1]],
|
216 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
217 |
+
[-1, 1, Conv, [160, 1, 1]], # 186
|
218 |
+
[[-1, -11], 1, Shortcut, [1]], # 187
|
219 |
+
|
220 |
+
[-1, 1, DownC, [320]],
|
221 |
+
[[-1, 162], 1, Concat, [1]],
|
222 |
+
|
223 |
+
[-1, 1, Conv, [256, 1, 1]],
|
224 |
+
[-2, 1, Conv, [256, 1, 1]],
|
225 |
+
[-1, 1, Conv, [128, 3, 1]],
|
226 |
+
[-1, 1, Conv, [128, 3, 1]],
|
227 |
+
[-1, 1, Conv, [128, 3, 1]],
|
228 |
+
[-1, 1, Conv, [128, 3, 1]],
|
229 |
+
[-1, 1, Conv, [128, 3, 1]],
|
230 |
+
[-1, 1, Conv, [128, 3, 1]],
|
231 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
232 |
+
[-1, 1, Conv, [320, 1, 1]], # 199
|
233 |
+
[-11, 1, Conv, [256, 1, 1]],
|
234 |
+
[-12, 1, Conv, [256, 1, 1]],
|
235 |
+
[-1, 1, Conv, [128, 3, 1]],
|
236 |
+
[-1, 1, Conv, [128, 3, 1]],
|
237 |
+
[-1, 1, Conv, [128, 3, 1]],
|
238 |
+
[-1, 1, Conv, [128, 3, 1]],
|
239 |
+
[-1, 1, Conv, [128, 3, 1]],
|
240 |
+
[-1, 1, Conv, [128, 3, 1]],
|
241 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
242 |
+
[-1, 1, Conv, [320, 1, 1]], # 209
|
243 |
+
[[-1, -11], 1, Shortcut, [1]], # 210
|
244 |
+
|
245 |
+
[-1, 1, DownC, [480]],
|
246 |
+
[[-1, 137], 1, Concat, [1]],
|
247 |
+
|
248 |
+
[-1, 1, Conv, [384, 1, 1]],
|
249 |
+
[-2, 1, Conv, [384, 1, 1]],
|
250 |
+
[-1, 1, Conv, [192, 3, 1]],
|
251 |
+
[-1, 1, Conv, [192, 3, 1]],
|
252 |
+
[-1, 1, Conv, [192, 3, 1]],
|
253 |
+
[-1, 1, Conv, [192, 3, 1]],
|
254 |
+
[-1, 1, Conv, [192, 3, 1]],
|
255 |
+
[-1, 1, Conv, [192, 3, 1]],
|
256 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
257 |
+
[-1, 1, Conv, [480, 1, 1]], # 222
|
258 |
+
[-11, 1, Conv, [384, 1, 1]],
|
259 |
+
[-12, 1, Conv, [384, 1, 1]],
|
260 |
+
[-1, 1, Conv, [192, 3, 1]],
|
261 |
+
[-1, 1, Conv, [192, 3, 1]],
|
262 |
+
[-1, 1, Conv, [192, 3, 1]],
|
263 |
+
[-1, 1, Conv, [192, 3, 1]],
|
264 |
+
[-1, 1, Conv, [192, 3, 1]],
|
265 |
+
[-1, 1, Conv, [192, 3, 1]],
|
266 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
267 |
+
[-1, 1, Conv, [480, 1, 1]], # 232
|
268 |
+
[[-1, -11], 1, Shortcut, [1]], # 233
|
269 |
+
|
270 |
+
[-1, 1, DownC, [640]],
|
271 |
+
[[-1, 112], 1, Concat, [1]],
|
272 |
+
|
273 |
+
[-1, 1, Conv, [512, 1, 1]],
|
274 |
+
[-2, 1, Conv, [512, 1, 1]],
|
275 |
+
[-1, 1, Conv, [256, 3, 1]],
|
276 |
+
[-1, 1, Conv, [256, 3, 1]],
|
277 |
+
[-1, 1, Conv, [256, 3, 1]],
|
278 |
+
[-1, 1, Conv, [256, 3, 1]],
|
279 |
+
[-1, 1, Conv, [256, 3, 1]],
|
280 |
+
[-1, 1, Conv, [256, 3, 1]],
|
281 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
282 |
+
[-1, 1, Conv, [640, 1, 1]], # 245
|
283 |
+
[-11, 1, Conv, [512, 1, 1]],
|
284 |
+
[-12, 1, Conv, [512, 1, 1]],
|
285 |
+
[-1, 1, Conv, [256, 3, 1]],
|
286 |
+
[-1, 1, Conv, [256, 3, 1]],
|
287 |
+
[-1, 1, Conv, [256, 3, 1]],
|
288 |
+
[-1, 1, Conv, [256, 3, 1]],
|
289 |
+
[-1, 1, Conv, [256, 3, 1]],
|
290 |
+
[-1, 1, Conv, [256, 3, 1]],
|
291 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
292 |
+
[-1, 1, Conv, [640, 1, 1]], # 255
|
293 |
+
[[-1, -11], 1, Shortcut, [1]], # 256
|
294 |
+
|
295 |
+
[187, 1, Conv, [320, 3, 1]],
|
296 |
+
[210, 1, Conv, [640, 3, 1]],
|
297 |
+
[233, 1, Conv, [960, 3, 1]],
|
298 |
+
[256, 1, Conv, [1280, 3, 1]],
|
299 |
+
|
300 |
+
[[257,258,259,260], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
301 |
+
]
|
infer/yolov7/cfg/deploy/yolov7-tiny-silu.yaml
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# YOLOv7-tiny backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Conv, [32, 3, 2]], # 0-P1/2
|
16 |
+
|
17 |
+
[-1, 1, Conv, [64, 3, 2]], # 1-P2/4
|
18 |
+
|
19 |
+
[-1, 1, Conv, [32, 1, 1]],
|
20 |
+
[-2, 1, Conv, [32, 1, 1]],
|
21 |
+
[-1, 1, Conv, [32, 3, 1]],
|
22 |
+
[-1, 1, Conv, [32, 3, 1]],
|
23 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
24 |
+
[-1, 1, Conv, [64, 1, 1]], # 7
|
25 |
+
|
26 |
+
[-1, 1, MP, []], # 8-P3/8
|
27 |
+
[-1, 1, Conv, [64, 1, 1]],
|
28 |
+
[-2, 1, Conv, [64, 1, 1]],
|
29 |
+
[-1, 1, Conv, [64, 3, 1]],
|
30 |
+
[-1, 1, Conv, [64, 3, 1]],
|
31 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
32 |
+
[-1, 1, Conv, [128, 1, 1]], # 14
|
33 |
+
|
34 |
+
[-1, 1, MP, []], # 15-P4/16
|
35 |
+
[-1, 1, Conv, [128, 1, 1]],
|
36 |
+
[-2, 1, Conv, [128, 1, 1]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1]],
|
39 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
40 |
+
[-1, 1, Conv, [256, 1, 1]], # 21
|
41 |
+
|
42 |
+
[-1, 1, MP, []], # 22-P5/32
|
43 |
+
[-1, 1, Conv, [256, 1, 1]],
|
44 |
+
[-2, 1, Conv, [256, 1, 1]],
|
45 |
+
[-1, 1, Conv, [256, 3, 1]],
|
46 |
+
[-1, 1, Conv, [256, 3, 1]],
|
47 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
48 |
+
[-1, 1, Conv, [512, 1, 1]], # 28
|
49 |
+
]
|
50 |
+
|
51 |
+
# YOLOv7-tiny head
|
52 |
+
head:
|
53 |
+
[[-1, 1, Conv, [256, 1, 1]],
|
54 |
+
[-2, 1, Conv, [256, 1, 1]],
|
55 |
+
[-1, 1, SP, [5]],
|
56 |
+
[-2, 1, SP, [9]],
|
57 |
+
[-3, 1, SP, [13]],
|
58 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
59 |
+
[-1, 1, Conv, [256, 1, 1]],
|
60 |
+
[[-1, -7], 1, Concat, [1]],
|
61 |
+
[-1, 1, Conv, [256, 1, 1]], # 37
|
62 |
+
|
63 |
+
[-1, 1, Conv, [128, 1, 1]],
|
64 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
65 |
+
[21, 1, Conv, [128, 1, 1]], # route backbone P4
|
66 |
+
[[-1, -2], 1, Concat, [1]],
|
67 |
+
|
68 |
+
[-1, 1, Conv, [64, 1, 1]],
|
69 |
+
[-2, 1, Conv, [64, 1, 1]],
|
70 |
+
[-1, 1, Conv, [64, 3, 1]],
|
71 |
+
[-1, 1, Conv, [64, 3, 1]],
|
72 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
73 |
+
[-1, 1, Conv, [128, 1, 1]], # 47
|
74 |
+
|
75 |
+
[-1, 1, Conv, [64, 1, 1]],
|
76 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
77 |
+
[14, 1, Conv, [64, 1, 1]], # route backbone P3
|
78 |
+
[[-1, -2], 1, Concat, [1]],
|
79 |
+
|
80 |
+
[-1, 1, Conv, [32, 1, 1]],
|
81 |
+
[-2, 1, Conv, [32, 1, 1]],
|
82 |
+
[-1, 1, Conv, [32, 3, 1]],
|
83 |
+
[-1, 1, Conv, [32, 3, 1]],
|
84 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
85 |
+
[-1, 1, Conv, [64, 1, 1]], # 57
|
86 |
+
|
87 |
+
[-1, 1, Conv, [128, 3, 2]],
|
88 |
+
[[-1, 47], 1, Concat, [1]],
|
89 |
+
|
90 |
+
[-1, 1, Conv, [64, 1, 1]],
|
91 |
+
[-2, 1, Conv, [64, 1, 1]],
|
92 |
+
[-1, 1, Conv, [64, 3, 1]],
|
93 |
+
[-1, 1, Conv, [64, 3, 1]],
|
94 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
95 |
+
[-1, 1, Conv, [128, 1, 1]], # 65
|
96 |
+
|
97 |
+
[-1, 1, Conv, [256, 3, 2]],
|
98 |
+
[[-1, 37], 1, Concat, [1]],
|
99 |
+
|
100 |
+
[-1, 1, Conv, [128, 1, 1]],
|
101 |
+
[-2, 1, Conv, [128, 1, 1]],
|
102 |
+
[-1, 1, Conv, [128, 3, 1]],
|
103 |
+
[-1, 1, Conv, [128, 3, 1]],
|
104 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
105 |
+
[-1, 1, Conv, [256, 1, 1]], # 73
|
106 |
+
|
107 |
+
[57, 1, Conv, [128, 3, 1]],
|
108 |
+
[65, 1, Conv, [256, 3, 1]],
|
109 |
+
[73, 1, Conv, [512, 3, 1]],
|
110 |
+
|
111 |
+
[[74,75,76], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
112 |
+
]
|
infer/yolov7/cfg/deploy/yolov7-tiny.yaml
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7-tiny backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args] c2, k=1, s=1, p=None, g=1, act=True
|
15 |
+
[[-1, 1, Conv, [32, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 0-P1/2
|
16 |
+
|
17 |
+
[-1, 1, Conv, [64, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 1-P2/4
|
18 |
+
|
19 |
+
[-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
20 |
+
[-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
21 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
22 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
23 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
24 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 7
|
25 |
+
|
26 |
+
[-1, 1, MP, []], # 8-P3/8
|
27 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
28 |
+
[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
29 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
30 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
31 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
32 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 14
|
33 |
+
|
34 |
+
[-1, 1, MP, []], # 15-P4/16
|
35 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
36 |
+
[-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
39 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
40 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 21
|
41 |
+
|
42 |
+
[-1, 1, MP, []], # 22-P5/32
|
43 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
44 |
+
[-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
45 |
+
[-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
46 |
+
[-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
47 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
48 |
+
[-1, 1, Conv, [512, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 28
|
49 |
+
]
|
50 |
+
|
51 |
+
# yolov7-tiny head
|
52 |
+
head:
|
53 |
+
[[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
54 |
+
[-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
55 |
+
[-1, 1, SP, [5]],
|
56 |
+
[-2, 1, SP, [9]],
|
57 |
+
[-3, 1, SP, [13]],
|
58 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
59 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
60 |
+
[[-1, -7], 1, Concat, [1]],
|
61 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 37
|
62 |
+
|
63 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
64 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
65 |
+
[21, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P4
|
66 |
+
[[-1, -2], 1, Concat, [1]],
|
67 |
+
|
68 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
69 |
+
[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
70 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
71 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
72 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
73 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 47
|
74 |
+
|
75 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
76 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
77 |
+
[14, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P3
|
78 |
+
[[-1, -2], 1, Concat, [1]],
|
79 |
+
|
80 |
+
[-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
81 |
+
[-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
82 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
83 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
84 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
85 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 57
|
86 |
+
|
87 |
+
[-1, 1, Conv, [128, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
|
88 |
+
[[-1, 47], 1, Concat, [1]],
|
89 |
+
|
90 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
91 |
+
[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
92 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
93 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
94 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
95 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 65
|
96 |
+
|
97 |
+
[-1, 1, Conv, [256, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
|
98 |
+
[[-1, 37], 1, Concat, [1]],
|
99 |
+
|
100 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
101 |
+
[-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
102 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
103 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
104 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
105 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 73
|
106 |
+
|
107 |
+
[57, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
108 |
+
[65, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
109 |
+
[73, 1, Conv, [512, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
110 |
+
|
111 |
+
[[74,75,76], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
112 |
+
]
|
infer/yolov7/cfg/deploy/yolov7-w6.yaml
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7-w6 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [64, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
27 |
+
[-1, 1, Conv, [128, 1, 1]], # 10
|
28 |
+
|
29 |
+
[-1, 1, Conv, [256, 3, 2]], # 11-P3/8
|
30 |
+
[-1, 1, Conv, [128, 1, 1]],
|
31 |
+
[-2, 1, Conv, [128, 1, 1]],
|
32 |
+
[-1, 1, Conv, [128, 3, 1]],
|
33 |
+
[-1, 1, Conv, [128, 3, 1]],
|
34 |
+
[-1, 1, Conv, [128, 3, 1]],
|
35 |
+
[-1, 1, Conv, [128, 3, 1]],
|
36 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
37 |
+
[-1, 1, Conv, [256, 1, 1]], # 19
|
38 |
+
|
39 |
+
[-1, 1, Conv, [512, 3, 2]], # 20-P4/16
|
40 |
+
[-1, 1, Conv, [256, 1, 1]],
|
41 |
+
[-2, 1, Conv, [256, 1, 1]],
|
42 |
+
[-1, 1, Conv, [256, 3, 1]],
|
43 |
+
[-1, 1, Conv, [256, 3, 1]],
|
44 |
+
[-1, 1, Conv, [256, 3, 1]],
|
45 |
+
[-1, 1, Conv, [256, 3, 1]],
|
46 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
47 |
+
[-1, 1, Conv, [512, 1, 1]], # 28
|
48 |
+
|
49 |
+
[-1, 1, Conv, [768, 3, 2]], # 29-P5/32
|
50 |
+
[-1, 1, Conv, [384, 1, 1]],
|
51 |
+
[-2, 1, Conv, [384, 1, 1]],
|
52 |
+
[-1, 1, Conv, [384, 3, 1]],
|
53 |
+
[-1, 1, Conv, [384, 3, 1]],
|
54 |
+
[-1, 1, Conv, [384, 3, 1]],
|
55 |
+
[-1, 1, Conv, [384, 3, 1]],
|
56 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
57 |
+
[-1, 1, Conv, [768, 1, 1]], # 37
|
58 |
+
|
59 |
+
[-1, 1, Conv, [1024, 3, 2]], # 38-P6/64
|
60 |
+
[-1, 1, Conv, [512, 1, 1]],
|
61 |
+
[-2, 1, Conv, [512, 1, 1]],
|
62 |
+
[-1, 1, Conv, [512, 3, 1]],
|
63 |
+
[-1, 1, Conv, [512, 3, 1]],
|
64 |
+
[-1, 1, Conv, [512, 3, 1]],
|
65 |
+
[-1, 1, Conv, [512, 3, 1]],
|
66 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
67 |
+
[-1, 1, Conv, [1024, 1, 1]], # 46
|
68 |
+
]
|
69 |
+
|
70 |
+
# yolov7-w6 head
|
71 |
+
head:
|
72 |
+
[[-1, 1, SPPCSPC, [512]], # 47
|
73 |
+
|
74 |
+
[-1, 1, Conv, [384, 1, 1]],
|
75 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
76 |
+
[37, 1, Conv, [384, 1, 1]], # route backbone P5
|
77 |
+
[[-1, -2], 1, Concat, [1]],
|
78 |
+
|
79 |
+
[-1, 1, Conv, [384, 1, 1]],
|
80 |
+
[-2, 1, Conv, [384, 1, 1]],
|
81 |
+
[-1, 1, Conv, [192, 3, 1]],
|
82 |
+
[-1, 1, Conv, [192, 3, 1]],
|
83 |
+
[-1, 1, Conv, [192, 3, 1]],
|
84 |
+
[-1, 1, Conv, [192, 3, 1]],
|
85 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
86 |
+
[-1, 1, Conv, [384, 1, 1]], # 59
|
87 |
+
|
88 |
+
[-1, 1, Conv, [256, 1, 1]],
|
89 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
90 |
+
[28, 1, Conv, [256, 1, 1]], # route backbone P4
|
91 |
+
[[-1, -2], 1, Concat, [1]],
|
92 |
+
|
93 |
+
[-1, 1, Conv, [256, 1, 1]],
|
94 |
+
[-2, 1, Conv, [256, 1, 1]],
|
95 |
+
[-1, 1, Conv, [128, 3, 1]],
|
96 |
+
[-1, 1, Conv, [128, 3, 1]],
|
97 |
+
[-1, 1, Conv, [128, 3, 1]],
|
98 |
+
[-1, 1, Conv, [128, 3, 1]],
|
99 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
100 |
+
[-1, 1, Conv, [256, 1, 1]], # 71
|
101 |
+
|
102 |
+
[-1, 1, Conv, [128, 1, 1]],
|
103 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
104 |
+
[19, 1, Conv, [128, 1, 1]], # route backbone P3
|
105 |
+
[[-1, -2], 1, Concat, [1]],
|
106 |
+
|
107 |
+
[-1, 1, Conv, [128, 1, 1]],
|
108 |
+
[-2, 1, Conv, [128, 1, 1]],
|
109 |
+
[-1, 1, Conv, [64, 3, 1]],
|
110 |
+
[-1, 1, Conv, [64, 3, 1]],
|
111 |
+
[-1, 1, Conv, [64, 3, 1]],
|
112 |
+
[-1, 1, Conv, [64, 3, 1]],
|
113 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
114 |
+
[-1, 1, Conv, [128, 1, 1]], # 83
|
115 |
+
|
116 |
+
[-1, 1, Conv, [256, 3, 2]],
|
117 |
+
[[-1, 71], 1, Concat, [1]], # cat
|
118 |
+
|
119 |
+
[-1, 1, Conv, [256, 1, 1]],
|
120 |
+
[-2, 1, Conv, [256, 1, 1]],
|
121 |
+
[-1, 1, Conv, [128, 3, 1]],
|
122 |
+
[-1, 1, Conv, [128, 3, 1]],
|
123 |
+
[-1, 1, Conv, [128, 3, 1]],
|
124 |
+
[-1, 1, Conv, [128, 3, 1]],
|
125 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
126 |
+
[-1, 1, Conv, [256, 1, 1]], # 93
|
127 |
+
|
128 |
+
[-1, 1, Conv, [384, 3, 2]],
|
129 |
+
[[-1, 59], 1, Concat, [1]], # cat
|
130 |
+
|
131 |
+
[-1, 1, Conv, [384, 1, 1]],
|
132 |
+
[-2, 1, Conv, [384, 1, 1]],
|
133 |
+
[-1, 1, Conv, [192, 3, 1]],
|
134 |
+
[-1, 1, Conv, [192, 3, 1]],
|
135 |
+
[-1, 1, Conv, [192, 3, 1]],
|
136 |
+
[-1, 1, Conv, [192, 3, 1]],
|
137 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
138 |
+
[-1, 1, Conv, [384, 1, 1]], # 103
|
139 |
+
|
140 |
+
[-1, 1, Conv, [512, 3, 2]],
|
141 |
+
[[-1, 47], 1, Concat, [1]], # cat
|
142 |
+
|
143 |
+
[-1, 1, Conv, [512, 1, 1]],
|
144 |
+
[-2, 1, Conv, [512, 1, 1]],
|
145 |
+
[-1, 1, Conv, [256, 3, 1]],
|
146 |
+
[-1, 1, Conv, [256, 3, 1]],
|
147 |
+
[-1, 1, Conv, [256, 3, 1]],
|
148 |
+
[-1, 1, Conv, [256, 3, 1]],
|
149 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
150 |
+
[-1, 1, Conv, [512, 1, 1]], # 113
|
151 |
+
|
152 |
+
[83, 1, Conv, [256, 3, 1]],
|
153 |
+
[93, 1, Conv, [512, 3, 1]],
|
154 |
+
[103, 1, Conv, [768, 3, 1]],
|
155 |
+
[113, 1, Conv, [1024, 3, 1]],
|
156 |
+
|
157 |
+
[[114,115,116,117], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
158 |
+
]
|
infer/yolov7/cfg/deploy/yolov7.yaml
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# yolov7 backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Conv, [32, 3, 1]], # 0
|
16 |
+
|
17 |
+
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
|
18 |
+
[-1, 1, Conv, [64, 3, 1]],
|
19 |
+
|
20 |
+
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
|
21 |
+
[-1, 1, Conv, [64, 1, 1]],
|
22 |
+
[-2, 1, Conv, [64, 1, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
28 |
+
[-1, 1, Conv, [256, 1, 1]], # 11
|
29 |
+
|
30 |
+
[-1, 1, MP, []],
|
31 |
+
[-1, 1, Conv, [128, 1, 1]],
|
32 |
+
[-3, 1, Conv, [128, 1, 1]],
|
33 |
+
[-1, 1, Conv, [128, 3, 2]],
|
34 |
+
[[-1, -3], 1, Concat, [1]], # 16-P3/8
|
35 |
+
[-1, 1, Conv, [128, 1, 1]],
|
36 |
+
[-2, 1, Conv, [128, 1, 1]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[-1, 1, Conv, [128, 3, 1]],
|
41 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
42 |
+
[-1, 1, Conv, [512, 1, 1]], # 24
|
43 |
+
|
44 |
+
[-1, 1, MP, []],
|
45 |
+
[-1, 1, Conv, [256, 1, 1]],
|
46 |
+
[-3, 1, Conv, [256, 1, 1]],
|
47 |
+
[-1, 1, Conv, [256, 3, 2]],
|
48 |
+
[[-1, -3], 1, Concat, [1]], # 29-P4/16
|
49 |
+
[-1, 1, Conv, [256, 1, 1]],
|
50 |
+
[-2, 1, Conv, [256, 1, 1]],
|
51 |
+
[-1, 1, Conv, [256, 3, 1]],
|
52 |
+
[-1, 1, Conv, [256, 3, 1]],
|
53 |
+
[-1, 1, Conv, [256, 3, 1]],
|
54 |
+
[-1, 1, Conv, [256, 3, 1]],
|
55 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
56 |
+
[-1, 1, Conv, [1024, 1, 1]], # 37
|
57 |
+
|
58 |
+
[-1, 1, MP, []],
|
59 |
+
[-1, 1, Conv, [512, 1, 1]],
|
60 |
+
[-3, 1, Conv, [512, 1, 1]],
|
61 |
+
[-1, 1, Conv, [512, 3, 2]],
|
62 |
+
[[-1, -3], 1, Concat, [1]], # 42-P5/32
|
63 |
+
[-1, 1, Conv, [256, 1, 1]],
|
64 |
+
[-2, 1, Conv, [256, 1, 1]],
|
65 |
+
[-1, 1, Conv, [256, 3, 1]],
|
66 |
+
[-1, 1, Conv, [256, 3, 1]],
|
67 |
+
[-1, 1, Conv, [256, 3, 1]],
|
68 |
+
[-1, 1, Conv, [256, 3, 1]],
|
69 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
70 |
+
[-1, 1, Conv, [1024, 1, 1]], # 50
|
71 |
+
]
|
72 |
+
|
73 |
+
# yolov7 head
|
74 |
+
head:
|
75 |
+
[[-1, 1, SPPCSPC, [512]], # 51
|
76 |
+
|
77 |
+
[-1, 1, Conv, [256, 1, 1]],
|
78 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
79 |
+
[37, 1, Conv, [256, 1, 1]], # route backbone P4
|
80 |
+
[[-1, -2], 1, Concat, [1]],
|
81 |
+
|
82 |
+
[-1, 1, Conv, [256, 1, 1]],
|
83 |
+
[-2, 1, Conv, [256, 1, 1]],
|
84 |
+
[-1, 1, Conv, [128, 3, 1]],
|
85 |
+
[-1, 1, Conv, [128, 3, 1]],
|
86 |
+
[-1, 1, Conv, [128, 3, 1]],
|
87 |
+
[-1, 1, Conv, [128, 3, 1]],
|
88 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
89 |
+
[-1, 1, Conv, [256, 1, 1]], # 63
|
90 |
+
|
91 |
+
[-1, 1, Conv, [128, 1, 1]],
|
92 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
93 |
+
[24, 1, Conv, [128, 1, 1]], # route backbone P3
|
94 |
+
[[-1, -2], 1, Concat, [1]],
|
95 |
+
|
96 |
+
[-1, 1, Conv, [128, 1, 1]],
|
97 |
+
[-2, 1, Conv, [128, 1, 1]],
|
98 |
+
[-1, 1, Conv, [64, 3, 1]],
|
99 |
+
[-1, 1, Conv, [64, 3, 1]],
|
100 |
+
[-1, 1, Conv, [64, 3, 1]],
|
101 |
+
[-1, 1, Conv, [64, 3, 1]],
|
102 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
103 |
+
[-1, 1, Conv, [128, 1, 1]], # 75
|
104 |
+
|
105 |
+
[-1, 1, MP, []],
|
106 |
+
[-1, 1, Conv, [128, 1, 1]],
|
107 |
+
[-3, 1, Conv, [128, 1, 1]],
|
108 |
+
[-1, 1, Conv, [128, 3, 2]],
|
109 |
+
[[-1, -3, 63], 1, Concat, [1]],
|
110 |
+
|
111 |
+
[-1, 1, Conv, [256, 1, 1]],
|
112 |
+
[-2, 1, Conv, [256, 1, 1]],
|
113 |
+
[-1, 1, Conv, [128, 3, 1]],
|
114 |
+
[-1, 1, Conv, [128, 3, 1]],
|
115 |
+
[-1, 1, Conv, [128, 3, 1]],
|
116 |
+
[-1, 1, Conv, [128, 3, 1]],
|
117 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
118 |
+
[-1, 1, Conv, [256, 1, 1]], # 88
|
119 |
+
|
120 |
+
[-1, 1, MP, []],
|
121 |
+
[-1, 1, Conv, [256, 1, 1]],
|
122 |
+
[-3, 1, Conv, [256, 1, 1]],
|
123 |
+
[-1, 1, Conv, [256, 3, 2]],
|
124 |
+
[[-1, -3, 51], 1, Concat, [1]],
|
125 |
+
|
126 |
+
[-1, 1, Conv, [512, 1, 1]],
|
127 |
+
[-2, 1, Conv, [512, 1, 1]],
|
128 |
+
[-1, 1, Conv, [256, 3, 1]],
|
129 |
+
[-1, 1, Conv, [256, 3, 1]],
|
130 |
+
[-1, 1, Conv, [256, 3, 1]],
|
131 |
+
[-1, 1, Conv, [256, 3, 1]],
|
132 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
133 |
+
[-1, 1, Conv, [512, 1, 1]], # 101
|
134 |
+
|
135 |
+
[75, 1, RepConv, [256, 3, 1]],
|
136 |
+
[88, 1, RepConv, [512, 3, 1]],
|
137 |
+
[101, 1, RepConv, [1024, 3, 1]],
|
138 |
+
|
139 |
+
[[102,103,104], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
140 |
+
]
|
infer/yolov7/cfg/deploy/yolov7x.yaml
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# yolov7x backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Conv, [40, 3, 1]], # 0
|
16 |
+
|
17 |
+
[-1, 1, Conv, [80, 3, 2]], # 1-P1/2
|
18 |
+
[-1, 1, Conv, [80, 3, 1]],
|
19 |
+
|
20 |
+
[-1, 1, Conv, [160, 3, 2]], # 3-P2/4
|
21 |
+
[-1, 1, Conv, [64, 1, 1]],
|
22 |
+
[-2, 1, Conv, [64, 1, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[-1, 1, Conv, [64, 3, 1]],
|
29 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
30 |
+
[-1, 1, Conv, [320, 1, 1]], # 13
|
31 |
+
|
32 |
+
[-1, 1, MP, []],
|
33 |
+
[-1, 1, Conv, [160, 1, 1]],
|
34 |
+
[-3, 1, Conv, [160, 1, 1]],
|
35 |
+
[-1, 1, Conv, [160, 3, 2]],
|
36 |
+
[[-1, -3], 1, Concat, [1]], # 18-P3/8
|
37 |
+
[-1, 1, Conv, [128, 1, 1]],
|
38 |
+
[-2, 1, Conv, [128, 1, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[-1, 1, Conv, [128, 3, 1]],
|
41 |
+
[-1, 1, Conv, [128, 3, 1]],
|
42 |
+
[-1, 1, Conv, [128, 3, 1]],
|
43 |
+
[-1, 1, Conv, [128, 3, 1]],
|
44 |
+
[-1, 1, Conv, [128, 3, 1]],
|
45 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
46 |
+
[-1, 1, Conv, [640, 1, 1]], # 28
|
47 |
+
|
48 |
+
[-1, 1, MP, []],
|
49 |
+
[-1, 1, Conv, [320, 1, 1]],
|
50 |
+
[-3, 1, Conv, [320, 1, 1]],
|
51 |
+
[-1, 1, Conv, [320, 3, 2]],
|
52 |
+
[[-1, -3], 1, Concat, [1]], # 33-P4/16
|
53 |
+
[-1, 1, Conv, [256, 1, 1]],
|
54 |
+
[-2, 1, Conv, [256, 1, 1]],
|
55 |
+
[-1, 1, Conv, [256, 3, 1]],
|
56 |
+
[-1, 1, Conv, [256, 3, 1]],
|
57 |
+
[-1, 1, Conv, [256, 3, 1]],
|
58 |
+
[-1, 1, Conv, [256, 3, 1]],
|
59 |
+
[-1, 1, Conv, [256, 3, 1]],
|
60 |
+
[-1, 1, Conv, [256, 3, 1]],
|
61 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
62 |
+
[-1, 1, Conv, [1280, 1, 1]], # 43
|
63 |
+
|
64 |
+
[-1, 1, MP, []],
|
65 |
+
[-1, 1, Conv, [640, 1, 1]],
|
66 |
+
[-3, 1, Conv, [640, 1, 1]],
|
67 |
+
[-1, 1, Conv, [640, 3, 2]],
|
68 |
+
[[-1, -3], 1, Concat, [1]], # 48-P5/32
|
69 |
+
[-1, 1, Conv, [256, 1, 1]],
|
70 |
+
[-2, 1, Conv, [256, 1, 1]],
|
71 |
+
[-1, 1, Conv, [256, 3, 1]],
|
72 |
+
[-1, 1, Conv, [256, 3, 1]],
|
73 |
+
[-1, 1, Conv, [256, 3, 1]],
|
74 |
+
[-1, 1, Conv, [256, 3, 1]],
|
75 |
+
[-1, 1, Conv, [256, 3, 1]],
|
76 |
+
[-1, 1, Conv, [256, 3, 1]],
|
77 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
78 |
+
[-1, 1, Conv, [1280, 1, 1]], # 58
|
79 |
+
]
|
80 |
+
|
81 |
+
# yolov7x head
|
82 |
+
head:
|
83 |
+
[[-1, 1, SPPCSPC, [640]], # 59
|
84 |
+
|
85 |
+
[-1, 1, Conv, [320, 1, 1]],
|
86 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
87 |
+
[43, 1, Conv, [320, 1, 1]], # route backbone P4
|
88 |
+
[[-1, -2], 1, Concat, [1]],
|
89 |
+
|
90 |
+
[-1, 1, Conv, [256, 1, 1]],
|
91 |
+
[-2, 1, Conv, [256, 1, 1]],
|
92 |
+
[-1, 1, Conv, [256, 3, 1]],
|
93 |
+
[-1, 1, Conv, [256, 3, 1]],
|
94 |
+
[-1, 1, Conv, [256, 3, 1]],
|
95 |
+
[-1, 1, Conv, [256, 3, 1]],
|
96 |
+
[-1, 1, Conv, [256, 3, 1]],
|
97 |
+
[-1, 1, Conv, [256, 3, 1]],
|
98 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
99 |
+
[-1, 1, Conv, [320, 1, 1]], # 73
|
100 |
+
|
101 |
+
[-1, 1, Conv, [160, 1, 1]],
|
102 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
103 |
+
[28, 1, Conv, [160, 1, 1]], # route backbone P3
|
104 |
+
[[-1, -2], 1, Concat, [1]],
|
105 |
+
|
106 |
+
[-1, 1, Conv, [128, 1, 1]],
|
107 |
+
[-2, 1, Conv, [128, 1, 1]],
|
108 |
+
[-1, 1, Conv, [128, 3, 1]],
|
109 |
+
[-1, 1, Conv, [128, 3, 1]],
|
110 |
+
[-1, 1, Conv, [128, 3, 1]],
|
111 |
+
[-1, 1, Conv, [128, 3, 1]],
|
112 |
+
[-1, 1, Conv, [128, 3, 1]],
|
113 |
+
[-1, 1, Conv, [128, 3, 1]],
|
114 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
115 |
+
[-1, 1, Conv, [160, 1, 1]], # 87
|
116 |
+
|
117 |
+
[-1, 1, MP, []],
|
118 |
+
[-1, 1, Conv, [160, 1, 1]],
|
119 |
+
[-3, 1, Conv, [160, 1, 1]],
|
120 |
+
[-1, 1, Conv, [160, 3, 2]],
|
121 |
+
[[-1, -3, 73], 1, Concat, [1]],
|
122 |
+
|
123 |
+
[-1, 1, Conv, [256, 1, 1]],
|
124 |
+
[-2, 1, Conv, [256, 1, 1]],
|
125 |
+
[-1, 1, Conv, [256, 3, 1]],
|
126 |
+
[-1, 1, Conv, [256, 3, 1]],
|
127 |
+
[-1, 1, Conv, [256, 3, 1]],
|
128 |
+
[-1, 1, Conv, [256, 3, 1]],
|
129 |
+
[-1, 1, Conv, [256, 3, 1]],
|
130 |
+
[-1, 1, Conv, [256, 3, 1]],
|
131 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
132 |
+
[-1, 1, Conv, [320, 1, 1]], # 102
|
133 |
+
|
134 |
+
[-1, 1, MP, []],
|
135 |
+
[-1, 1, Conv, [320, 1, 1]],
|
136 |
+
[-3, 1, Conv, [320, 1, 1]],
|
137 |
+
[-1, 1, Conv, [320, 3, 2]],
|
138 |
+
[[-1, -3, 59], 1, Concat, [1]],
|
139 |
+
|
140 |
+
[-1, 1, Conv, [512, 1, 1]],
|
141 |
+
[-2, 1, Conv, [512, 1, 1]],
|
142 |
+
[-1, 1, Conv, [512, 3, 1]],
|
143 |
+
[-1, 1, Conv, [512, 3, 1]],
|
144 |
+
[-1, 1, Conv, [512, 3, 1]],
|
145 |
+
[-1, 1, Conv, [512, 3, 1]],
|
146 |
+
[-1, 1, Conv, [512, 3, 1]],
|
147 |
+
[-1, 1, Conv, [512, 3, 1]],
|
148 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
149 |
+
[-1, 1, Conv, [640, 1, 1]], # 117
|
150 |
+
|
151 |
+
[87, 1, Conv, [320, 3, 1]],
|
152 |
+
[102, 1, Conv, [640, 3, 1]],
|
153 |
+
[117, 1, Conv, [1280, 3, 1]],
|
154 |
+
|
155 |
+
[[118,119,120], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
156 |
+
]
|
infer/yolov7/cfg/training/yolov7-d6.yaml
ADDED
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args],
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [96, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, DownC, [192]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[-1, 1, Conv, [64, 3, 1]],
|
29 |
+
[-1, 1, Conv, [64, 3, 1]],
|
30 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
31 |
+
[-1, 1, Conv, [192, 1, 1]], # 14
|
32 |
+
|
33 |
+
[-1, 1, DownC, [384]], # 15-P3/8
|
34 |
+
[-1, 1, Conv, [128, 1, 1]],
|
35 |
+
[-2, 1, Conv, [128, 1, 1]],
|
36 |
+
[-1, 1, Conv, [128, 3, 1]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[-1, 1, Conv, [128, 3, 1]],
|
41 |
+
[-1, 1, Conv, [128, 3, 1]],
|
42 |
+
[-1, 1, Conv, [128, 3, 1]],
|
43 |
+
[-1, 1, Conv, [128, 3, 1]],
|
44 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
45 |
+
[-1, 1, Conv, [384, 1, 1]], # 27
|
46 |
+
|
47 |
+
[-1, 1, DownC, [768]], # 28-P4/16
|
48 |
+
[-1, 1, Conv, [256, 1, 1]],
|
49 |
+
[-2, 1, Conv, [256, 1, 1]],
|
50 |
+
[-1, 1, Conv, [256, 3, 1]],
|
51 |
+
[-1, 1, Conv, [256, 3, 1]],
|
52 |
+
[-1, 1, Conv, [256, 3, 1]],
|
53 |
+
[-1, 1, Conv, [256, 3, 1]],
|
54 |
+
[-1, 1, Conv, [256, 3, 1]],
|
55 |
+
[-1, 1, Conv, [256, 3, 1]],
|
56 |
+
[-1, 1, Conv, [256, 3, 1]],
|
57 |
+
[-1, 1, Conv, [256, 3, 1]],
|
58 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
59 |
+
[-1, 1, Conv, [768, 1, 1]], # 40
|
60 |
+
|
61 |
+
[-1, 1, DownC, [1152]], # 41-P5/32
|
62 |
+
[-1, 1, Conv, [384, 1, 1]],
|
63 |
+
[-2, 1, Conv, [384, 1, 1]],
|
64 |
+
[-1, 1, Conv, [384, 3, 1]],
|
65 |
+
[-1, 1, Conv, [384, 3, 1]],
|
66 |
+
[-1, 1, Conv, [384, 3, 1]],
|
67 |
+
[-1, 1, Conv, [384, 3, 1]],
|
68 |
+
[-1, 1, Conv, [384, 3, 1]],
|
69 |
+
[-1, 1, Conv, [384, 3, 1]],
|
70 |
+
[-1, 1, Conv, [384, 3, 1]],
|
71 |
+
[-1, 1, Conv, [384, 3, 1]],
|
72 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
73 |
+
[-1, 1, Conv, [1152, 1, 1]], # 53
|
74 |
+
|
75 |
+
[-1, 1, DownC, [1536]], # 54-P6/64
|
76 |
+
[-1, 1, Conv, [512, 1, 1]],
|
77 |
+
[-2, 1, Conv, [512, 1, 1]],
|
78 |
+
[-1, 1, Conv, [512, 3, 1]],
|
79 |
+
[-1, 1, Conv, [512, 3, 1]],
|
80 |
+
[-1, 1, Conv, [512, 3, 1]],
|
81 |
+
[-1, 1, Conv, [512, 3, 1]],
|
82 |
+
[-1, 1, Conv, [512, 3, 1]],
|
83 |
+
[-1, 1, Conv, [512, 3, 1]],
|
84 |
+
[-1, 1, Conv, [512, 3, 1]],
|
85 |
+
[-1, 1, Conv, [512, 3, 1]],
|
86 |
+
[[-1, -3, -5, -7, -9, -10], 1, Concat, [1]],
|
87 |
+
[-1, 1, Conv, [1536, 1, 1]], # 66
|
88 |
+
]
|
89 |
+
|
90 |
+
# yolov7 head
|
91 |
+
head:
|
92 |
+
[[-1, 1, SPPCSPC, [768]], # 67
|
93 |
+
|
94 |
+
[-1, 1, Conv, [576, 1, 1]],
|
95 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
96 |
+
[53, 1, Conv, [576, 1, 1]], # route backbone P5
|
97 |
+
[[-1, -2], 1, Concat, [1]],
|
98 |
+
|
99 |
+
[-1, 1, Conv, [384, 1, 1]],
|
100 |
+
[-2, 1, Conv, [384, 1, 1]],
|
101 |
+
[-1, 1, Conv, [192, 3, 1]],
|
102 |
+
[-1, 1, Conv, [192, 3, 1]],
|
103 |
+
[-1, 1, Conv, [192, 3, 1]],
|
104 |
+
[-1, 1, Conv, [192, 3, 1]],
|
105 |
+
[-1, 1, Conv, [192, 3, 1]],
|
106 |
+
[-1, 1, Conv, [192, 3, 1]],
|
107 |
+
[-1, 1, Conv, [192, 3, 1]],
|
108 |
+
[-1, 1, Conv, [192, 3, 1]],
|
109 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
110 |
+
[-1, 1, Conv, [576, 1, 1]], # 83
|
111 |
+
|
112 |
+
[-1, 1, Conv, [384, 1, 1]],
|
113 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
114 |
+
[40, 1, Conv, [384, 1, 1]], # route backbone P4
|
115 |
+
[[-1, -2], 1, Concat, [1]],
|
116 |
+
|
117 |
+
[-1, 1, Conv, [256, 1, 1]],
|
118 |
+
[-2, 1, Conv, [256, 1, 1]],
|
119 |
+
[-1, 1, Conv, [128, 3, 1]],
|
120 |
+
[-1, 1, Conv, [128, 3, 1]],
|
121 |
+
[-1, 1, Conv, [128, 3, 1]],
|
122 |
+
[-1, 1, Conv, [128, 3, 1]],
|
123 |
+
[-1, 1, Conv, [128, 3, 1]],
|
124 |
+
[-1, 1, Conv, [128, 3, 1]],
|
125 |
+
[-1, 1, Conv, [128, 3, 1]],
|
126 |
+
[-1, 1, Conv, [128, 3, 1]],
|
127 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
128 |
+
[-1, 1, Conv, [384, 1, 1]], # 99
|
129 |
+
|
130 |
+
[-1, 1, Conv, [192, 1, 1]],
|
131 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
132 |
+
[27, 1, Conv, [192, 1, 1]], # route backbone P3
|
133 |
+
[[-1, -2], 1, Concat, [1]],
|
134 |
+
|
135 |
+
[-1, 1, Conv, [128, 1, 1]],
|
136 |
+
[-2, 1, Conv, [128, 1, 1]],
|
137 |
+
[-1, 1, Conv, [64, 3, 1]],
|
138 |
+
[-1, 1, Conv, [64, 3, 1]],
|
139 |
+
[-1, 1, Conv, [64, 3, 1]],
|
140 |
+
[-1, 1, Conv, [64, 3, 1]],
|
141 |
+
[-1, 1, Conv, [64, 3, 1]],
|
142 |
+
[-1, 1, Conv, [64, 3, 1]],
|
143 |
+
[-1, 1, Conv, [64, 3, 1]],
|
144 |
+
[-1, 1, Conv, [64, 3, 1]],
|
145 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
146 |
+
[-1, 1, Conv, [192, 1, 1]], # 115
|
147 |
+
|
148 |
+
[-1, 1, DownC, [384]],
|
149 |
+
[[-1, 99], 1, Concat, [1]],
|
150 |
+
|
151 |
+
[-1, 1, Conv, [256, 1, 1]],
|
152 |
+
[-2, 1, Conv, [256, 1, 1]],
|
153 |
+
[-1, 1, Conv, [128, 3, 1]],
|
154 |
+
[-1, 1, Conv, [128, 3, 1]],
|
155 |
+
[-1, 1, Conv, [128, 3, 1]],
|
156 |
+
[-1, 1, Conv, [128, 3, 1]],
|
157 |
+
[-1, 1, Conv, [128, 3, 1]],
|
158 |
+
[-1, 1, Conv, [128, 3, 1]],
|
159 |
+
[-1, 1, Conv, [128, 3, 1]],
|
160 |
+
[-1, 1, Conv, [128, 3, 1]],
|
161 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
162 |
+
[-1, 1, Conv, [384, 1, 1]], # 129
|
163 |
+
|
164 |
+
[-1, 1, DownC, [576]],
|
165 |
+
[[-1, 83], 1, Concat, [1]],
|
166 |
+
|
167 |
+
[-1, 1, Conv, [384, 1, 1]],
|
168 |
+
[-2, 1, Conv, [384, 1, 1]],
|
169 |
+
[-1, 1, Conv, [192, 3, 1]],
|
170 |
+
[-1, 1, Conv, [192, 3, 1]],
|
171 |
+
[-1, 1, Conv, [192, 3, 1]],
|
172 |
+
[-1, 1, Conv, [192, 3, 1]],
|
173 |
+
[-1, 1, Conv, [192, 3, 1]],
|
174 |
+
[-1, 1, Conv, [192, 3, 1]],
|
175 |
+
[-1, 1, Conv, [192, 3, 1]],
|
176 |
+
[-1, 1, Conv, [192, 3, 1]],
|
177 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
178 |
+
[-1, 1, Conv, [576, 1, 1]], # 143
|
179 |
+
|
180 |
+
[-1, 1, DownC, [768]],
|
181 |
+
[[-1, 67], 1, Concat, [1]],
|
182 |
+
|
183 |
+
[-1, 1, Conv, [512, 1, 1]],
|
184 |
+
[-2, 1, Conv, [512, 1, 1]],
|
185 |
+
[-1, 1, Conv, [256, 3, 1]],
|
186 |
+
[-1, 1, Conv, [256, 3, 1]],
|
187 |
+
[-1, 1, Conv, [256, 3, 1]],
|
188 |
+
[-1, 1, Conv, [256, 3, 1]],
|
189 |
+
[-1, 1, Conv, [256, 3, 1]],
|
190 |
+
[-1, 1, Conv, [256, 3, 1]],
|
191 |
+
[-1, 1, Conv, [256, 3, 1]],
|
192 |
+
[-1, 1, Conv, [256, 3, 1]],
|
193 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10], 1, Concat, [1]],
|
194 |
+
[-1, 1, Conv, [768, 1, 1]], # 157
|
195 |
+
|
196 |
+
[115, 1, Conv, [384, 3, 1]],
|
197 |
+
[129, 1, Conv, [768, 3, 1]],
|
198 |
+
[143, 1, Conv, [1152, 3, 1]],
|
199 |
+
[157, 1, Conv, [1536, 3, 1]],
|
200 |
+
|
201 |
+
[115, 1, Conv, [384, 3, 1]],
|
202 |
+
[99, 1, Conv, [768, 3, 1]],
|
203 |
+
[83, 1, Conv, [1152, 3, 1]],
|
204 |
+
[67, 1, Conv, [1536, 3, 1]],
|
205 |
+
|
206 |
+
[[158,159,160,161,162,163,164,165], 1, IAuxDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
207 |
+
]
|
infer/yolov7/cfg/training/yolov7-e6.yaml
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args],
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [80, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, DownC, [160]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
29 |
+
[-1, 1, Conv, [160, 1, 1]], # 12
|
30 |
+
|
31 |
+
[-1, 1, DownC, [320]], # 13-P3/8
|
32 |
+
[-1, 1, Conv, [128, 1, 1]],
|
33 |
+
[-2, 1, Conv, [128, 1, 1]],
|
34 |
+
[-1, 1, Conv, [128, 3, 1]],
|
35 |
+
[-1, 1, Conv, [128, 3, 1]],
|
36 |
+
[-1, 1, Conv, [128, 3, 1]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
41 |
+
[-1, 1, Conv, [320, 1, 1]], # 23
|
42 |
+
|
43 |
+
[-1, 1, DownC, [640]], # 24-P4/16
|
44 |
+
[-1, 1, Conv, [256, 1, 1]],
|
45 |
+
[-2, 1, Conv, [256, 1, 1]],
|
46 |
+
[-1, 1, Conv, [256, 3, 1]],
|
47 |
+
[-1, 1, Conv, [256, 3, 1]],
|
48 |
+
[-1, 1, Conv, [256, 3, 1]],
|
49 |
+
[-1, 1, Conv, [256, 3, 1]],
|
50 |
+
[-1, 1, Conv, [256, 3, 1]],
|
51 |
+
[-1, 1, Conv, [256, 3, 1]],
|
52 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
53 |
+
[-1, 1, Conv, [640, 1, 1]], # 34
|
54 |
+
|
55 |
+
[-1, 1, DownC, [960]], # 35-P5/32
|
56 |
+
[-1, 1, Conv, [384, 1, 1]],
|
57 |
+
[-2, 1, Conv, [384, 1, 1]],
|
58 |
+
[-1, 1, Conv, [384, 3, 1]],
|
59 |
+
[-1, 1, Conv, [384, 3, 1]],
|
60 |
+
[-1, 1, Conv, [384, 3, 1]],
|
61 |
+
[-1, 1, Conv, [384, 3, 1]],
|
62 |
+
[-1, 1, Conv, [384, 3, 1]],
|
63 |
+
[-1, 1, Conv, [384, 3, 1]],
|
64 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
65 |
+
[-1, 1, Conv, [960, 1, 1]], # 45
|
66 |
+
|
67 |
+
[-1, 1, DownC, [1280]], # 46-P6/64
|
68 |
+
[-1, 1, Conv, [512, 1, 1]],
|
69 |
+
[-2, 1, Conv, [512, 1, 1]],
|
70 |
+
[-1, 1, Conv, [512, 3, 1]],
|
71 |
+
[-1, 1, Conv, [512, 3, 1]],
|
72 |
+
[-1, 1, Conv, [512, 3, 1]],
|
73 |
+
[-1, 1, Conv, [512, 3, 1]],
|
74 |
+
[-1, 1, Conv, [512, 3, 1]],
|
75 |
+
[-1, 1, Conv, [512, 3, 1]],
|
76 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
77 |
+
[-1, 1, Conv, [1280, 1, 1]], # 56
|
78 |
+
]
|
79 |
+
|
80 |
+
# yolov7 head
|
81 |
+
head:
|
82 |
+
[[-1, 1, SPPCSPC, [640]], # 57
|
83 |
+
|
84 |
+
[-1, 1, Conv, [480, 1, 1]],
|
85 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
86 |
+
[45, 1, Conv, [480, 1, 1]], # route backbone P5
|
87 |
+
[[-1, -2], 1, Concat, [1]],
|
88 |
+
|
89 |
+
[-1, 1, Conv, [384, 1, 1]],
|
90 |
+
[-2, 1, Conv, [384, 1, 1]],
|
91 |
+
[-1, 1, Conv, [192, 3, 1]],
|
92 |
+
[-1, 1, Conv, [192, 3, 1]],
|
93 |
+
[-1, 1, Conv, [192, 3, 1]],
|
94 |
+
[-1, 1, Conv, [192, 3, 1]],
|
95 |
+
[-1, 1, Conv, [192, 3, 1]],
|
96 |
+
[-1, 1, Conv, [192, 3, 1]],
|
97 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
98 |
+
[-1, 1, Conv, [480, 1, 1]], # 71
|
99 |
+
|
100 |
+
[-1, 1, Conv, [320, 1, 1]],
|
101 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
102 |
+
[34, 1, Conv, [320, 1, 1]], # route backbone P4
|
103 |
+
[[-1, -2], 1, Concat, [1]],
|
104 |
+
|
105 |
+
[-1, 1, Conv, [256, 1, 1]],
|
106 |
+
[-2, 1, Conv, [256, 1, 1]],
|
107 |
+
[-1, 1, Conv, [128, 3, 1]],
|
108 |
+
[-1, 1, Conv, [128, 3, 1]],
|
109 |
+
[-1, 1, Conv, [128, 3, 1]],
|
110 |
+
[-1, 1, Conv, [128, 3, 1]],
|
111 |
+
[-1, 1, Conv, [128, 3, 1]],
|
112 |
+
[-1, 1, Conv, [128, 3, 1]],
|
113 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
114 |
+
[-1, 1, Conv, [320, 1, 1]], # 85
|
115 |
+
|
116 |
+
[-1, 1, Conv, [160, 1, 1]],
|
117 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
118 |
+
[23, 1, Conv, [160, 1, 1]], # route backbone P3
|
119 |
+
[[-1, -2], 1, Concat, [1]],
|
120 |
+
|
121 |
+
[-1, 1, Conv, [128, 1, 1]],
|
122 |
+
[-2, 1, Conv, [128, 1, 1]],
|
123 |
+
[-1, 1, Conv, [64, 3, 1]],
|
124 |
+
[-1, 1, Conv, [64, 3, 1]],
|
125 |
+
[-1, 1, Conv, [64, 3, 1]],
|
126 |
+
[-1, 1, Conv, [64, 3, 1]],
|
127 |
+
[-1, 1, Conv, [64, 3, 1]],
|
128 |
+
[-1, 1, Conv, [64, 3, 1]],
|
129 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
130 |
+
[-1, 1, Conv, [160, 1, 1]], # 99
|
131 |
+
|
132 |
+
[-1, 1, DownC, [320]],
|
133 |
+
[[-1, 85], 1, Concat, [1]],
|
134 |
+
|
135 |
+
[-1, 1, Conv, [256, 1, 1]],
|
136 |
+
[-2, 1, Conv, [256, 1, 1]],
|
137 |
+
[-1, 1, Conv, [128, 3, 1]],
|
138 |
+
[-1, 1, Conv, [128, 3, 1]],
|
139 |
+
[-1, 1, Conv, [128, 3, 1]],
|
140 |
+
[-1, 1, Conv, [128, 3, 1]],
|
141 |
+
[-1, 1, Conv, [128, 3, 1]],
|
142 |
+
[-1, 1, Conv, [128, 3, 1]],
|
143 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
144 |
+
[-1, 1, Conv, [320, 1, 1]], # 111
|
145 |
+
|
146 |
+
[-1, 1, DownC, [480]],
|
147 |
+
[[-1, 71], 1, Concat, [1]],
|
148 |
+
|
149 |
+
[-1, 1, Conv, [384, 1, 1]],
|
150 |
+
[-2, 1, Conv, [384, 1, 1]],
|
151 |
+
[-1, 1, Conv, [192, 3, 1]],
|
152 |
+
[-1, 1, Conv, [192, 3, 1]],
|
153 |
+
[-1, 1, Conv, [192, 3, 1]],
|
154 |
+
[-1, 1, Conv, [192, 3, 1]],
|
155 |
+
[-1, 1, Conv, [192, 3, 1]],
|
156 |
+
[-1, 1, Conv, [192, 3, 1]],
|
157 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
158 |
+
[-1, 1, Conv, [480, 1, 1]], # 123
|
159 |
+
|
160 |
+
[-1, 1, DownC, [640]],
|
161 |
+
[[-1, 57], 1, Concat, [1]],
|
162 |
+
|
163 |
+
[-1, 1, Conv, [512, 1, 1]],
|
164 |
+
[-2, 1, Conv, [512, 1, 1]],
|
165 |
+
[-1, 1, Conv, [256, 3, 1]],
|
166 |
+
[-1, 1, Conv, [256, 3, 1]],
|
167 |
+
[-1, 1, Conv, [256, 3, 1]],
|
168 |
+
[-1, 1, Conv, [256, 3, 1]],
|
169 |
+
[-1, 1, Conv, [256, 3, 1]],
|
170 |
+
[-1, 1, Conv, [256, 3, 1]],
|
171 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
172 |
+
[-1, 1, Conv, [640, 1, 1]], # 135
|
173 |
+
|
174 |
+
[99, 1, Conv, [320, 3, 1]],
|
175 |
+
[111, 1, Conv, [640, 3, 1]],
|
176 |
+
[123, 1, Conv, [960, 3, 1]],
|
177 |
+
[135, 1, Conv, [1280, 3, 1]],
|
178 |
+
|
179 |
+
[99, 1, Conv, [320, 3, 1]],
|
180 |
+
[85, 1, Conv, [640, 3, 1]],
|
181 |
+
[71, 1, Conv, [960, 3, 1]],
|
182 |
+
[57, 1, Conv, [1280, 3, 1]],
|
183 |
+
|
184 |
+
[[136,137,138,139,140,141,142,143], 1, IAuxDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
185 |
+
]
|
infer/yolov7/cfg/training/yolov7-e6e.yaml
ADDED
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args],
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [80, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, DownC, [160]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
29 |
+
[-1, 1, Conv, [160, 1, 1]], # 12
|
30 |
+
[-11, 1, Conv, [64, 1, 1]],
|
31 |
+
[-12, 1, Conv, [64, 1, 1]],
|
32 |
+
[-1, 1, Conv, [64, 3, 1]],
|
33 |
+
[-1, 1, Conv, [64, 3, 1]],
|
34 |
+
[-1, 1, Conv, [64, 3, 1]],
|
35 |
+
[-1, 1, Conv, [64, 3, 1]],
|
36 |
+
[-1, 1, Conv, [64, 3, 1]],
|
37 |
+
[-1, 1, Conv, [64, 3, 1]],
|
38 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
39 |
+
[-1, 1, Conv, [160, 1, 1]], # 22
|
40 |
+
[[-1, -11], 1, Shortcut, [1]], # 23
|
41 |
+
|
42 |
+
[-1, 1, DownC, [320]], # 24-P3/8
|
43 |
+
[-1, 1, Conv, [128, 1, 1]],
|
44 |
+
[-2, 1, Conv, [128, 1, 1]],
|
45 |
+
[-1, 1, Conv, [128, 3, 1]],
|
46 |
+
[-1, 1, Conv, [128, 3, 1]],
|
47 |
+
[-1, 1, Conv, [128, 3, 1]],
|
48 |
+
[-1, 1, Conv, [128, 3, 1]],
|
49 |
+
[-1, 1, Conv, [128, 3, 1]],
|
50 |
+
[-1, 1, Conv, [128, 3, 1]],
|
51 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
52 |
+
[-1, 1, Conv, [320, 1, 1]], # 34
|
53 |
+
[-11, 1, Conv, [128, 1, 1]],
|
54 |
+
[-12, 1, Conv, [128, 1, 1]],
|
55 |
+
[-1, 1, Conv, [128, 3, 1]],
|
56 |
+
[-1, 1, Conv, [128, 3, 1]],
|
57 |
+
[-1, 1, Conv, [128, 3, 1]],
|
58 |
+
[-1, 1, Conv, [128, 3, 1]],
|
59 |
+
[-1, 1, Conv, [128, 3, 1]],
|
60 |
+
[-1, 1, Conv, [128, 3, 1]],
|
61 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
62 |
+
[-1, 1, Conv, [320, 1, 1]], # 44
|
63 |
+
[[-1, -11], 1, Shortcut, [1]], # 45
|
64 |
+
|
65 |
+
[-1, 1, DownC, [640]], # 46-P4/16
|
66 |
+
[-1, 1, Conv, [256, 1, 1]],
|
67 |
+
[-2, 1, Conv, [256, 1, 1]],
|
68 |
+
[-1, 1, Conv, [256, 3, 1]],
|
69 |
+
[-1, 1, Conv, [256, 3, 1]],
|
70 |
+
[-1, 1, Conv, [256, 3, 1]],
|
71 |
+
[-1, 1, Conv, [256, 3, 1]],
|
72 |
+
[-1, 1, Conv, [256, 3, 1]],
|
73 |
+
[-1, 1, Conv, [256, 3, 1]],
|
74 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
75 |
+
[-1, 1, Conv, [640, 1, 1]], # 56
|
76 |
+
[-11, 1, Conv, [256, 1, 1]],
|
77 |
+
[-12, 1, Conv, [256, 1, 1]],
|
78 |
+
[-1, 1, Conv, [256, 3, 1]],
|
79 |
+
[-1, 1, Conv, [256, 3, 1]],
|
80 |
+
[-1, 1, Conv, [256, 3, 1]],
|
81 |
+
[-1, 1, Conv, [256, 3, 1]],
|
82 |
+
[-1, 1, Conv, [256, 3, 1]],
|
83 |
+
[-1, 1, Conv, [256, 3, 1]],
|
84 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
85 |
+
[-1, 1, Conv, [640, 1, 1]], # 66
|
86 |
+
[[-1, -11], 1, Shortcut, [1]], # 67
|
87 |
+
|
88 |
+
[-1, 1, DownC, [960]], # 68-P5/32
|
89 |
+
[-1, 1, Conv, [384, 1, 1]],
|
90 |
+
[-2, 1, Conv, [384, 1, 1]],
|
91 |
+
[-1, 1, Conv, [384, 3, 1]],
|
92 |
+
[-1, 1, Conv, [384, 3, 1]],
|
93 |
+
[-1, 1, Conv, [384, 3, 1]],
|
94 |
+
[-1, 1, Conv, [384, 3, 1]],
|
95 |
+
[-1, 1, Conv, [384, 3, 1]],
|
96 |
+
[-1, 1, Conv, [384, 3, 1]],
|
97 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
98 |
+
[-1, 1, Conv, [960, 1, 1]], # 78
|
99 |
+
[-11, 1, Conv, [384, 1, 1]],
|
100 |
+
[-12, 1, Conv, [384, 1, 1]],
|
101 |
+
[-1, 1, Conv, [384, 3, 1]],
|
102 |
+
[-1, 1, Conv, [384, 3, 1]],
|
103 |
+
[-1, 1, Conv, [384, 3, 1]],
|
104 |
+
[-1, 1, Conv, [384, 3, 1]],
|
105 |
+
[-1, 1, Conv, [384, 3, 1]],
|
106 |
+
[-1, 1, Conv, [384, 3, 1]],
|
107 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
108 |
+
[-1, 1, Conv, [960, 1, 1]], # 88
|
109 |
+
[[-1, -11], 1, Shortcut, [1]], # 89
|
110 |
+
|
111 |
+
[-1, 1, DownC, [1280]], # 90-P6/64
|
112 |
+
[-1, 1, Conv, [512, 1, 1]],
|
113 |
+
[-2, 1, Conv, [512, 1, 1]],
|
114 |
+
[-1, 1, Conv, [512, 3, 1]],
|
115 |
+
[-1, 1, Conv, [512, 3, 1]],
|
116 |
+
[-1, 1, Conv, [512, 3, 1]],
|
117 |
+
[-1, 1, Conv, [512, 3, 1]],
|
118 |
+
[-1, 1, Conv, [512, 3, 1]],
|
119 |
+
[-1, 1, Conv, [512, 3, 1]],
|
120 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
121 |
+
[-1, 1, Conv, [1280, 1, 1]], # 100
|
122 |
+
[-11, 1, Conv, [512, 1, 1]],
|
123 |
+
[-12, 1, Conv, [512, 1, 1]],
|
124 |
+
[-1, 1, Conv, [512, 3, 1]],
|
125 |
+
[-1, 1, Conv, [512, 3, 1]],
|
126 |
+
[-1, 1, Conv, [512, 3, 1]],
|
127 |
+
[-1, 1, Conv, [512, 3, 1]],
|
128 |
+
[-1, 1, Conv, [512, 3, 1]],
|
129 |
+
[-1, 1, Conv, [512, 3, 1]],
|
130 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
131 |
+
[-1, 1, Conv, [1280, 1, 1]], # 110
|
132 |
+
[[-1, -11], 1, Shortcut, [1]], # 111
|
133 |
+
]
|
134 |
+
|
135 |
+
# yolov7 head
|
136 |
+
head:
|
137 |
+
[[-1, 1, SPPCSPC, [640]], # 112
|
138 |
+
|
139 |
+
[-1, 1, Conv, [480, 1, 1]],
|
140 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
141 |
+
[89, 1, Conv, [480, 1, 1]], # route backbone P5
|
142 |
+
[[-1, -2], 1, Concat, [1]],
|
143 |
+
|
144 |
+
[-1, 1, Conv, [384, 1, 1]],
|
145 |
+
[-2, 1, Conv, [384, 1, 1]],
|
146 |
+
[-1, 1, Conv, [192, 3, 1]],
|
147 |
+
[-1, 1, Conv, [192, 3, 1]],
|
148 |
+
[-1, 1, Conv, [192, 3, 1]],
|
149 |
+
[-1, 1, Conv, [192, 3, 1]],
|
150 |
+
[-1, 1, Conv, [192, 3, 1]],
|
151 |
+
[-1, 1, Conv, [192, 3, 1]],
|
152 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
153 |
+
[-1, 1, Conv, [480, 1, 1]], # 126
|
154 |
+
[-11, 1, Conv, [384, 1, 1]],
|
155 |
+
[-12, 1, Conv, [384, 1, 1]],
|
156 |
+
[-1, 1, Conv, [192, 3, 1]],
|
157 |
+
[-1, 1, Conv, [192, 3, 1]],
|
158 |
+
[-1, 1, Conv, [192, 3, 1]],
|
159 |
+
[-1, 1, Conv, [192, 3, 1]],
|
160 |
+
[-1, 1, Conv, [192, 3, 1]],
|
161 |
+
[-1, 1, Conv, [192, 3, 1]],
|
162 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
163 |
+
[-1, 1, Conv, [480, 1, 1]], # 136
|
164 |
+
[[-1, -11], 1, Shortcut, [1]], # 137
|
165 |
+
|
166 |
+
[-1, 1, Conv, [320, 1, 1]],
|
167 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
168 |
+
[67, 1, Conv, [320, 1, 1]], # route backbone P4
|
169 |
+
[[-1, -2], 1, Concat, [1]],
|
170 |
+
|
171 |
+
[-1, 1, Conv, [256, 1, 1]],
|
172 |
+
[-2, 1, Conv, [256, 1, 1]],
|
173 |
+
[-1, 1, Conv, [128, 3, 1]],
|
174 |
+
[-1, 1, Conv, [128, 3, 1]],
|
175 |
+
[-1, 1, Conv, [128, 3, 1]],
|
176 |
+
[-1, 1, Conv, [128, 3, 1]],
|
177 |
+
[-1, 1, Conv, [128, 3, 1]],
|
178 |
+
[-1, 1, Conv, [128, 3, 1]],
|
179 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
180 |
+
[-1, 1, Conv, [320, 1, 1]], # 151
|
181 |
+
[-11, 1, Conv, [256, 1, 1]],
|
182 |
+
[-12, 1, Conv, [256, 1, 1]],
|
183 |
+
[-1, 1, Conv, [128, 3, 1]],
|
184 |
+
[-1, 1, Conv, [128, 3, 1]],
|
185 |
+
[-1, 1, Conv, [128, 3, 1]],
|
186 |
+
[-1, 1, Conv, [128, 3, 1]],
|
187 |
+
[-1, 1, Conv, [128, 3, 1]],
|
188 |
+
[-1, 1, Conv, [128, 3, 1]],
|
189 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
190 |
+
[-1, 1, Conv, [320, 1, 1]], # 161
|
191 |
+
[[-1, -11], 1, Shortcut, [1]], # 162
|
192 |
+
|
193 |
+
[-1, 1, Conv, [160, 1, 1]],
|
194 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
195 |
+
[45, 1, Conv, [160, 1, 1]], # route backbone P3
|
196 |
+
[[-1, -2], 1, Concat, [1]],
|
197 |
+
|
198 |
+
[-1, 1, Conv, [128, 1, 1]],
|
199 |
+
[-2, 1, Conv, [128, 1, 1]],
|
200 |
+
[-1, 1, Conv, [64, 3, 1]],
|
201 |
+
[-1, 1, Conv, [64, 3, 1]],
|
202 |
+
[-1, 1, Conv, [64, 3, 1]],
|
203 |
+
[-1, 1, Conv, [64, 3, 1]],
|
204 |
+
[-1, 1, Conv, [64, 3, 1]],
|
205 |
+
[-1, 1, Conv, [64, 3, 1]],
|
206 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
207 |
+
[-1, 1, Conv, [160, 1, 1]], # 176
|
208 |
+
[-11, 1, Conv, [128, 1, 1]],
|
209 |
+
[-12, 1, Conv, [128, 1, 1]],
|
210 |
+
[-1, 1, Conv, [64, 3, 1]],
|
211 |
+
[-1, 1, Conv, [64, 3, 1]],
|
212 |
+
[-1, 1, Conv, [64, 3, 1]],
|
213 |
+
[-1, 1, Conv, [64, 3, 1]],
|
214 |
+
[-1, 1, Conv, [64, 3, 1]],
|
215 |
+
[-1, 1, Conv, [64, 3, 1]],
|
216 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
217 |
+
[-1, 1, Conv, [160, 1, 1]], # 186
|
218 |
+
[[-1, -11], 1, Shortcut, [1]], # 187
|
219 |
+
|
220 |
+
[-1, 1, DownC, [320]],
|
221 |
+
[[-1, 162], 1, Concat, [1]],
|
222 |
+
|
223 |
+
[-1, 1, Conv, [256, 1, 1]],
|
224 |
+
[-2, 1, Conv, [256, 1, 1]],
|
225 |
+
[-1, 1, Conv, [128, 3, 1]],
|
226 |
+
[-1, 1, Conv, [128, 3, 1]],
|
227 |
+
[-1, 1, Conv, [128, 3, 1]],
|
228 |
+
[-1, 1, Conv, [128, 3, 1]],
|
229 |
+
[-1, 1, Conv, [128, 3, 1]],
|
230 |
+
[-1, 1, Conv, [128, 3, 1]],
|
231 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
232 |
+
[-1, 1, Conv, [320, 1, 1]], # 199
|
233 |
+
[-11, 1, Conv, [256, 1, 1]],
|
234 |
+
[-12, 1, Conv, [256, 1, 1]],
|
235 |
+
[-1, 1, Conv, [128, 3, 1]],
|
236 |
+
[-1, 1, Conv, [128, 3, 1]],
|
237 |
+
[-1, 1, Conv, [128, 3, 1]],
|
238 |
+
[-1, 1, Conv, [128, 3, 1]],
|
239 |
+
[-1, 1, Conv, [128, 3, 1]],
|
240 |
+
[-1, 1, Conv, [128, 3, 1]],
|
241 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
242 |
+
[-1, 1, Conv, [320, 1, 1]], # 209
|
243 |
+
[[-1, -11], 1, Shortcut, [1]], # 210
|
244 |
+
|
245 |
+
[-1, 1, DownC, [480]],
|
246 |
+
[[-1, 137], 1, Concat, [1]],
|
247 |
+
|
248 |
+
[-1, 1, Conv, [384, 1, 1]],
|
249 |
+
[-2, 1, Conv, [384, 1, 1]],
|
250 |
+
[-1, 1, Conv, [192, 3, 1]],
|
251 |
+
[-1, 1, Conv, [192, 3, 1]],
|
252 |
+
[-1, 1, Conv, [192, 3, 1]],
|
253 |
+
[-1, 1, Conv, [192, 3, 1]],
|
254 |
+
[-1, 1, Conv, [192, 3, 1]],
|
255 |
+
[-1, 1, Conv, [192, 3, 1]],
|
256 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
257 |
+
[-1, 1, Conv, [480, 1, 1]], # 222
|
258 |
+
[-11, 1, Conv, [384, 1, 1]],
|
259 |
+
[-12, 1, Conv, [384, 1, 1]],
|
260 |
+
[-1, 1, Conv, [192, 3, 1]],
|
261 |
+
[-1, 1, Conv, [192, 3, 1]],
|
262 |
+
[-1, 1, Conv, [192, 3, 1]],
|
263 |
+
[-1, 1, Conv, [192, 3, 1]],
|
264 |
+
[-1, 1, Conv, [192, 3, 1]],
|
265 |
+
[-1, 1, Conv, [192, 3, 1]],
|
266 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
267 |
+
[-1, 1, Conv, [480, 1, 1]], # 232
|
268 |
+
[[-1, -11], 1, Shortcut, [1]], # 233
|
269 |
+
|
270 |
+
[-1, 1, DownC, [640]],
|
271 |
+
[[-1, 112], 1, Concat, [1]],
|
272 |
+
|
273 |
+
[-1, 1, Conv, [512, 1, 1]],
|
274 |
+
[-2, 1, Conv, [512, 1, 1]],
|
275 |
+
[-1, 1, Conv, [256, 3, 1]],
|
276 |
+
[-1, 1, Conv, [256, 3, 1]],
|
277 |
+
[-1, 1, Conv, [256, 3, 1]],
|
278 |
+
[-1, 1, Conv, [256, 3, 1]],
|
279 |
+
[-1, 1, Conv, [256, 3, 1]],
|
280 |
+
[-1, 1, Conv, [256, 3, 1]],
|
281 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
282 |
+
[-1, 1, Conv, [640, 1, 1]], # 245
|
283 |
+
[-11, 1, Conv, [512, 1, 1]],
|
284 |
+
[-12, 1, Conv, [512, 1, 1]],
|
285 |
+
[-1, 1, Conv, [256, 3, 1]],
|
286 |
+
[-1, 1, Conv, [256, 3, 1]],
|
287 |
+
[-1, 1, Conv, [256, 3, 1]],
|
288 |
+
[-1, 1, Conv, [256, 3, 1]],
|
289 |
+
[-1, 1, Conv, [256, 3, 1]],
|
290 |
+
[-1, 1, Conv, [256, 3, 1]],
|
291 |
+
[[-1, -2, -3, -4, -5, -6, -7, -8], 1, Concat, [1]],
|
292 |
+
[-1, 1, Conv, [640, 1, 1]], # 255
|
293 |
+
[[-1, -11], 1, Shortcut, [1]], # 256
|
294 |
+
|
295 |
+
[187, 1, Conv, [320, 3, 1]],
|
296 |
+
[210, 1, Conv, [640, 3, 1]],
|
297 |
+
[233, 1, Conv, [960, 3, 1]],
|
298 |
+
[256, 1, Conv, [1280, 3, 1]],
|
299 |
+
|
300 |
+
[186, 1, Conv, [320, 3, 1]],
|
301 |
+
[161, 1, Conv, [640, 3, 1]],
|
302 |
+
[136, 1, Conv, [960, 3, 1]],
|
303 |
+
[112, 1, Conv, [1280, 3, 1]],
|
304 |
+
|
305 |
+
[[257,258,259,260,261,262,263,264], 1, IAuxDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
306 |
+
]
|
infer/yolov7/cfg/training/yolov7-tiny.yaml
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7-tiny backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args] c2, k=1, s=1, p=None, g=1, act=True
|
15 |
+
[[-1, 1, Conv, [32, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 0-P1/2
|
16 |
+
|
17 |
+
[-1, 1, Conv, [64, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 1-P2/4
|
18 |
+
|
19 |
+
[-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
20 |
+
[-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
21 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
22 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
23 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
24 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 7
|
25 |
+
|
26 |
+
[-1, 1, MP, []], # 8-P3/8
|
27 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
28 |
+
[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
29 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
30 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
31 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
32 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 14
|
33 |
+
|
34 |
+
[-1, 1, MP, []], # 15-P4/16
|
35 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
36 |
+
[-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
39 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
40 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 21
|
41 |
+
|
42 |
+
[-1, 1, MP, []], # 22-P5/32
|
43 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
44 |
+
[-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
45 |
+
[-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
46 |
+
[-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
47 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
48 |
+
[-1, 1, Conv, [512, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 28
|
49 |
+
]
|
50 |
+
|
51 |
+
# yolov7-tiny head
|
52 |
+
head:
|
53 |
+
[[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
54 |
+
[-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
55 |
+
[-1, 1, SP, [5]],
|
56 |
+
[-2, 1, SP, [9]],
|
57 |
+
[-3, 1, SP, [13]],
|
58 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
59 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
60 |
+
[[-1, -7], 1, Concat, [1]],
|
61 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 37
|
62 |
+
|
63 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
64 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
65 |
+
[21, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P4
|
66 |
+
[[-1, -2], 1, Concat, [1]],
|
67 |
+
|
68 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
69 |
+
[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
70 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
71 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
72 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
73 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 47
|
74 |
+
|
75 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
76 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
77 |
+
[14, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P3
|
78 |
+
[[-1, -2], 1, Concat, [1]],
|
79 |
+
|
80 |
+
[-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
81 |
+
[-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
82 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
83 |
+
[-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
84 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
85 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 57
|
86 |
+
|
87 |
+
[-1, 1, Conv, [128, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
|
88 |
+
[[-1, 47], 1, Concat, [1]],
|
89 |
+
|
90 |
+
[-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
91 |
+
[-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
92 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
93 |
+
[-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
94 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
95 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 65
|
96 |
+
|
97 |
+
[-1, 1, Conv, [256, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
|
98 |
+
[[-1, 37], 1, Concat, [1]],
|
99 |
+
|
100 |
+
[-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
101 |
+
[-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
|
102 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
103 |
+
[-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
104 |
+
[[-1, -2, -3, -4], 1, Concat, [1]],
|
105 |
+
[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 73
|
106 |
+
|
107 |
+
[57, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
108 |
+
[65, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
109 |
+
[73, 1, Conv, [512, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
|
110 |
+
|
111 |
+
[[74,75,76], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
|
112 |
+
]
|
infer/yolov7/cfg/training/yolov7-w6.yaml
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
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 |
+
# yolov7 backbone
|
14 |
+
backbone:
|
15 |
+
# [from, number, module, args]
|
16 |
+
[[-1, 1, ReOrg, []], # 0
|
17 |
+
[-1, 1, Conv, [64, 3, 1]], # 1-P1/2
|
18 |
+
|
19 |
+
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4
|
20 |
+
[-1, 1, Conv, [64, 1, 1]],
|
21 |
+
[-2, 1, Conv, [64, 1, 1]],
|
22 |
+
[-1, 1, Conv, [64, 3, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
27 |
+
[-1, 1, Conv, [128, 1, 1]], # 10
|
28 |
+
|
29 |
+
[-1, 1, Conv, [256, 3, 2]], # 11-P3/8
|
30 |
+
[-1, 1, Conv, [128, 1, 1]],
|
31 |
+
[-2, 1, Conv, [128, 1, 1]],
|
32 |
+
[-1, 1, Conv, [128, 3, 1]],
|
33 |
+
[-1, 1, Conv, [128, 3, 1]],
|
34 |
+
[-1, 1, Conv, [128, 3, 1]],
|
35 |
+
[-1, 1, Conv, [128, 3, 1]],
|
36 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
37 |
+
[-1, 1, Conv, [256, 1, 1]], # 19
|
38 |
+
|
39 |
+
[-1, 1, Conv, [512, 3, 2]], # 20-P4/16
|
40 |
+
[-1, 1, Conv, [256, 1, 1]],
|
41 |
+
[-2, 1, Conv, [256, 1, 1]],
|
42 |
+
[-1, 1, Conv, [256, 3, 1]],
|
43 |
+
[-1, 1, Conv, [256, 3, 1]],
|
44 |
+
[-1, 1, Conv, [256, 3, 1]],
|
45 |
+
[-1, 1, Conv, [256, 3, 1]],
|
46 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
47 |
+
[-1, 1, Conv, [512, 1, 1]], # 28
|
48 |
+
|
49 |
+
[-1, 1, Conv, [768, 3, 2]], # 29-P5/32
|
50 |
+
[-1, 1, Conv, [384, 1, 1]],
|
51 |
+
[-2, 1, Conv, [384, 1, 1]],
|
52 |
+
[-1, 1, Conv, [384, 3, 1]],
|
53 |
+
[-1, 1, Conv, [384, 3, 1]],
|
54 |
+
[-1, 1, Conv, [384, 3, 1]],
|
55 |
+
[-1, 1, Conv, [384, 3, 1]],
|
56 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
57 |
+
[-1, 1, Conv, [768, 1, 1]], # 37
|
58 |
+
|
59 |
+
[-1, 1, Conv, [1024, 3, 2]], # 38-P6/64
|
60 |
+
[-1, 1, Conv, [512, 1, 1]],
|
61 |
+
[-2, 1, Conv, [512, 1, 1]],
|
62 |
+
[-1, 1, Conv, [512, 3, 1]],
|
63 |
+
[-1, 1, Conv, [512, 3, 1]],
|
64 |
+
[-1, 1, Conv, [512, 3, 1]],
|
65 |
+
[-1, 1, Conv, [512, 3, 1]],
|
66 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
67 |
+
[-1, 1, Conv, [1024, 1, 1]], # 46
|
68 |
+
]
|
69 |
+
|
70 |
+
# yolov7 head
|
71 |
+
head:
|
72 |
+
[[-1, 1, SPPCSPC, [512]], # 47
|
73 |
+
|
74 |
+
[-1, 1, Conv, [384, 1, 1]],
|
75 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
76 |
+
[37, 1, Conv, [384, 1, 1]], # route backbone P5
|
77 |
+
[[-1, -2], 1, Concat, [1]],
|
78 |
+
|
79 |
+
[-1, 1, Conv, [384, 1, 1]],
|
80 |
+
[-2, 1, Conv, [384, 1, 1]],
|
81 |
+
[-1, 1, Conv, [192, 3, 1]],
|
82 |
+
[-1, 1, Conv, [192, 3, 1]],
|
83 |
+
[-1, 1, Conv, [192, 3, 1]],
|
84 |
+
[-1, 1, Conv, [192, 3, 1]],
|
85 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
86 |
+
[-1, 1, Conv, [384, 1, 1]], # 59
|
87 |
+
|
88 |
+
[-1, 1, Conv, [256, 1, 1]],
|
89 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
90 |
+
[28, 1, Conv, [256, 1, 1]], # route backbone P4
|
91 |
+
[[-1, -2], 1, Concat, [1]],
|
92 |
+
|
93 |
+
[-1, 1, Conv, [256, 1, 1]],
|
94 |
+
[-2, 1, Conv, [256, 1, 1]],
|
95 |
+
[-1, 1, Conv, [128, 3, 1]],
|
96 |
+
[-1, 1, Conv, [128, 3, 1]],
|
97 |
+
[-1, 1, Conv, [128, 3, 1]],
|
98 |
+
[-1, 1, Conv, [128, 3, 1]],
|
99 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
100 |
+
[-1, 1, Conv, [256, 1, 1]], # 71
|
101 |
+
|
102 |
+
[-1, 1, Conv, [128, 1, 1]],
|
103 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
104 |
+
[19, 1, Conv, [128, 1, 1]], # route backbone P3
|
105 |
+
[[-1, -2], 1, Concat, [1]],
|
106 |
+
|
107 |
+
[-1, 1, Conv, [128, 1, 1]],
|
108 |
+
[-2, 1, Conv, [128, 1, 1]],
|
109 |
+
[-1, 1, Conv, [64, 3, 1]],
|
110 |
+
[-1, 1, Conv, [64, 3, 1]],
|
111 |
+
[-1, 1, Conv, [64, 3, 1]],
|
112 |
+
[-1, 1, Conv, [64, 3, 1]],
|
113 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
114 |
+
[-1, 1, Conv, [128, 1, 1]], # 83
|
115 |
+
|
116 |
+
[-1, 1, Conv, [256, 3, 2]],
|
117 |
+
[[-1, 71], 1, Concat, [1]], # cat
|
118 |
+
|
119 |
+
[-1, 1, Conv, [256, 1, 1]],
|
120 |
+
[-2, 1, Conv, [256, 1, 1]],
|
121 |
+
[-1, 1, Conv, [128, 3, 1]],
|
122 |
+
[-1, 1, Conv, [128, 3, 1]],
|
123 |
+
[-1, 1, Conv, [128, 3, 1]],
|
124 |
+
[-1, 1, Conv, [128, 3, 1]],
|
125 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
126 |
+
[-1, 1, Conv, [256, 1, 1]], # 93
|
127 |
+
|
128 |
+
[-1, 1, Conv, [384, 3, 2]],
|
129 |
+
[[-1, 59], 1, Concat, [1]], # cat
|
130 |
+
|
131 |
+
[-1, 1, Conv, [384, 1, 1]],
|
132 |
+
[-2, 1, Conv, [384, 1, 1]],
|
133 |
+
[-1, 1, Conv, [192, 3, 1]],
|
134 |
+
[-1, 1, Conv, [192, 3, 1]],
|
135 |
+
[-1, 1, Conv, [192, 3, 1]],
|
136 |
+
[-1, 1, Conv, [192, 3, 1]],
|
137 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
138 |
+
[-1, 1, Conv, [384, 1, 1]], # 103
|
139 |
+
|
140 |
+
[-1, 1, Conv, [512, 3, 2]],
|
141 |
+
[[-1, 47], 1, Concat, [1]], # cat
|
142 |
+
|
143 |
+
[-1, 1, Conv, [512, 1, 1]],
|
144 |
+
[-2, 1, Conv, [512, 1, 1]],
|
145 |
+
[-1, 1, Conv, [256, 3, 1]],
|
146 |
+
[-1, 1, Conv, [256, 3, 1]],
|
147 |
+
[-1, 1, Conv, [256, 3, 1]],
|
148 |
+
[-1, 1, Conv, [256, 3, 1]],
|
149 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
150 |
+
[-1, 1, Conv, [512, 1, 1]], # 113
|
151 |
+
|
152 |
+
[83, 1, Conv, [256, 3, 1]],
|
153 |
+
[93, 1, Conv, [512, 3, 1]],
|
154 |
+
[103, 1, Conv, [768, 3, 1]],
|
155 |
+
[113, 1, Conv, [1024, 3, 1]],
|
156 |
+
|
157 |
+
[83, 1, Conv, [320, 3, 1]],
|
158 |
+
[71, 1, Conv, [640, 3, 1]],
|
159 |
+
[59, 1, Conv, [960, 3, 1]],
|
160 |
+
[47, 1, Conv, [1280, 3, 1]],
|
161 |
+
|
162 |
+
[[114,115,116,117,118,119,120,121], 1, IAuxDetect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
163 |
+
]
|
infer/yolov7/cfg/training/yolov7.yaml
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# yolov7 backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Conv, [32, 3, 1]], # 0
|
16 |
+
|
17 |
+
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
|
18 |
+
[-1, 1, Conv, [64, 3, 1]],
|
19 |
+
|
20 |
+
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
|
21 |
+
[-1, 1, Conv, [64, 1, 1]],
|
22 |
+
[-2, 1, Conv, [64, 1, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
28 |
+
[-1, 1, Conv, [256, 1, 1]], # 11
|
29 |
+
|
30 |
+
[-1, 1, MP, []],
|
31 |
+
[-1, 1, Conv, [128, 1, 1]],
|
32 |
+
[-3, 1, Conv, [128, 1, 1]],
|
33 |
+
[-1, 1, Conv, [128, 3, 2]],
|
34 |
+
[[-1, -3], 1, Concat, [1]], # 16-P3/8
|
35 |
+
[-1, 1, Conv, [128, 1, 1]],
|
36 |
+
[-2, 1, Conv, [128, 1, 1]],
|
37 |
+
[-1, 1, Conv, [128, 3, 1]],
|
38 |
+
[-1, 1, Conv, [128, 3, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[-1, 1, Conv, [128, 3, 1]],
|
41 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
42 |
+
[-1, 1, Conv, [512, 1, 1]], # 24
|
43 |
+
|
44 |
+
[-1, 1, MP, []],
|
45 |
+
[-1, 1, Conv, [256, 1, 1]],
|
46 |
+
[-3, 1, Conv, [256, 1, 1]],
|
47 |
+
[-1, 1, Conv, [256, 3, 2]],
|
48 |
+
[[-1, -3], 1, Concat, [1]], # 29-P4/16
|
49 |
+
[-1, 1, Conv, [256, 1, 1]],
|
50 |
+
[-2, 1, Conv, [256, 1, 1]],
|
51 |
+
[-1, 1, Conv, [256, 3, 1]],
|
52 |
+
[-1, 1, Conv, [256, 3, 1]],
|
53 |
+
[-1, 1, Conv, [256, 3, 1]],
|
54 |
+
[-1, 1, Conv, [256, 3, 1]],
|
55 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
56 |
+
[-1, 1, Conv, [1024, 1, 1]], # 37
|
57 |
+
|
58 |
+
[-1, 1, MP, []],
|
59 |
+
[-1, 1, Conv, [512, 1, 1]],
|
60 |
+
[-3, 1, Conv, [512, 1, 1]],
|
61 |
+
[-1, 1, Conv, [512, 3, 2]],
|
62 |
+
[[-1, -3], 1, Concat, [1]], # 42-P5/32
|
63 |
+
[-1, 1, Conv, [256, 1, 1]],
|
64 |
+
[-2, 1, Conv, [256, 1, 1]],
|
65 |
+
[-1, 1, Conv, [256, 3, 1]],
|
66 |
+
[-1, 1, Conv, [256, 3, 1]],
|
67 |
+
[-1, 1, Conv, [256, 3, 1]],
|
68 |
+
[-1, 1, Conv, [256, 3, 1]],
|
69 |
+
[[-1, -3, -5, -6], 1, Concat, [1]],
|
70 |
+
[-1, 1, Conv, [1024, 1, 1]], # 50
|
71 |
+
]
|
72 |
+
|
73 |
+
# yolov7 head
|
74 |
+
head:
|
75 |
+
[[-1, 1, SPPCSPC, [512]], # 51
|
76 |
+
|
77 |
+
[-1, 1, Conv, [256, 1, 1]],
|
78 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
79 |
+
[37, 1, Conv, [256, 1, 1]], # route backbone P4
|
80 |
+
[[-1, -2], 1, Concat, [1]],
|
81 |
+
|
82 |
+
[-1, 1, Conv, [256, 1, 1]],
|
83 |
+
[-2, 1, Conv, [256, 1, 1]],
|
84 |
+
[-1, 1, Conv, [128, 3, 1]],
|
85 |
+
[-1, 1, Conv, [128, 3, 1]],
|
86 |
+
[-1, 1, Conv, [128, 3, 1]],
|
87 |
+
[-1, 1, Conv, [128, 3, 1]],
|
88 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
89 |
+
[-1, 1, Conv, [256, 1, 1]], # 63
|
90 |
+
|
91 |
+
[-1, 1, Conv, [128, 1, 1]],
|
92 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
93 |
+
[24, 1, Conv, [128, 1, 1]], # route backbone P3
|
94 |
+
[[-1, -2], 1, Concat, [1]],
|
95 |
+
|
96 |
+
[-1, 1, Conv, [128, 1, 1]],
|
97 |
+
[-2, 1, Conv, [128, 1, 1]],
|
98 |
+
[-1, 1, Conv, [64, 3, 1]],
|
99 |
+
[-1, 1, Conv, [64, 3, 1]],
|
100 |
+
[-1, 1, Conv, [64, 3, 1]],
|
101 |
+
[-1, 1, Conv, [64, 3, 1]],
|
102 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
103 |
+
[-1, 1, Conv, [128, 1, 1]], # 75
|
104 |
+
|
105 |
+
[-1, 1, MP, []],
|
106 |
+
[-1, 1, Conv, [128, 1, 1]],
|
107 |
+
[-3, 1, Conv, [128, 1, 1]],
|
108 |
+
[-1, 1, Conv, [128, 3, 2]],
|
109 |
+
[[-1, -3, 63], 1, Concat, [1]],
|
110 |
+
|
111 |
+
[-1, 1, Conv, [256, 1, 1]],
|
112 |
+
[-2, 1, Conv, [256, 1, 1]],
|
113 |
+
[-1, 1, Conv, [128, 3, 1]],
|
114 |
+
[-1, 1, Conv, [128, 3, 1]],
|
115 |
+
[-1, 1, Conv, [128, 3, 1]],
|
116 |
+
[-1, 1, Conv, [128, 3, 1]],
|
117 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
118 |
+
[-1, 1, Conv, [256, 1, 1]], # 88
|
119 |
+
|
120 |
+
[-1, 1, MP, []],
|
121 |
+
[-1, 1, Conv, [256, 1, 1]],
|
122 |
+
[-3, 1, Conv, [256, 1, 1]],
|
123 |
+
[-1, 1, Conv, [256, 3, 2]],
|
124 |
+
[[-1, -3, 51], 1, Concat, [1]],
|
125 |
+
|
126 |
+
[-1, 1, Conv, [512, 1, 1]],
|
127 |
+
[-2, 1, Conv, [512, 1, 1]],
|
128 |
+
[-1, 1, Conv, [256, 3, 1]],
|
129 |
+
[-1, 1, Conv, [256, 3, 1]],
|
130 |
+
[-1, 1, Conv, [256, 3, 1]],
|
131 |
+
[-1, 1, Conv, [256, 3, 1]],
|
132 |
+
[[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
|
133 |
+
[-1, 1, Conv, [512, 1, 1]], # 101
|
134 |
+
|
135 |
+
[75, 1, RepConv, [256, 3, 1]],
|
136 |
+
[88, 1, RepConv, [512, 3, 1]],
|
137 |
+
[101, 1, RepConv, [1024, 3, 1]],
|
138 |
+
|
139 |
+
[[102,103,104], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
|
140 |
+
]
|
infer/yolov7/cfg/training/yolov7x.yaml
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# parameters
|
2 |
+
nc: 80 # number of classes
|
3 |
+
depth_multiple: 1.0 # model depth multiple
|
4 |
+
width_multiple: 1.0 # layer channel multiple
|
5 |
+
|
6 |
+
# anchors
|
7 |
+
anchors:
|
8 |
+
- [12,16, 19,36, 40,28] # P3/8
|
9 |
+
- [36,75, 76,55, 72,146] # P4/16
|
10 |
+
- [142,110, 192,243, 459,401] # P5/32
|
11 |
+
|
12 |
+
# yolov7 backbone
|
13 |
+
backbone:
|
14 |
+
# [from, number, module, args]
|
15 |
+
[[-1, 1, Conv, [40, 3, 1]], # 0
|
16 |
+
|
17 |
+
[-1, 1, Conv, [80, 3, 2]], # 1-P1/2
|
18 |
+
[-1, 1, Conv, [80, 3, 1]],
|
19 |
+
|
20 |
+
[-1, 1, Conv, [160, 3, 2]], # 3-P2/4
|
21 |
+
[-1, 1, Conv, [64, 1, 1]],
|
22 |
+
[-2, 1, Conv, [64, 1, 1]],
|
23 |
+
[-1, 1, Conv, [64, 3, 1]],
|
24 |
+
[-1, 1, Conv, [64, 3, 1]],
|
25 |
+
[-1, 1, Conv, [64, 3, 1]],
|
26 |
+
[-1, 1, Conv, [64, 3, 1]],
|
27 |
+
[-1, 1, Conv, [64, 3, 1]],
|
28 |
+
[-1, 1, Conv, [64, 3, 1]],
|
29 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
30 |
+
[-1, 1, Conv, [320, 1, 1]], # 13
|
31 |
+
|
32 |
+
[-1, 1, MP, []],
|
33 |
+
[-1, 1, Conv, [160, 1, 1]],
|
34 |
+
[-3, 1, Conv, [160, 1, 1]],
|
35 |
+
[-1, 1, Conv, [160, 3, 2]],
|
36 |
+
[[-1, -3], 1, Concat, [1]], # 18-P3/8
|
37 |
+
[-1, 1, Conv, [128, 1, 1]],
|
38 |
+
[-2, 1, Conv, [128, 1, 1]],
|
39 |
+
[-1, 1, Conv, [128, 3, 1]],
|
40 |
+
[-1, 1, Conv, [128, 3, 1]],
|
41 |
+
[-1, 1, Conv, [128, 3, 1]],
|
42 |
+
[-1, 1, Conv, [128, 3, 1]],
|
43 |
+
[-1, 1, Conv, [128, 3, 1]],
|
44 |
+
[-1, 1, Conv, [128, 3, 1]],
|
45 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
46 |
+
[-1, 1, Conv, [640, 1, 1]], # 28
|
47 |
+
|
48 |
+
[-1, 1, MP, []],
|
49 |
+
[-1, 1, Conv, [320, 1, 1]],
|
50 |
+
[-3, 1, Conv, [320, 1, 1]],
|
51 |
+
[-1, 1, Conv, [320, 3, 2]],
|
52 |
+
[[-1, -3], 1, Concat, [1]], # 33-P4/16
|
53 |
+
[-1, 1, Conv, [256, 1, 1]],
|
54 |
+
[-2, 1, Conv, [256, 1, 1]],
|
55 |
+
[-1, 1, Conv, [256, 3, 1]],
|
56 |
+
[-1, 1, Conv, [256, 3, 1]],
|
57 |
+
[-1, 1, Conv, [256, 3, 1]],
|
58 |
+
[-1, 1, Conv, [256, 3, 1]],
|
59 |
+
[-1, 1, Conv, [256, 3, 1]],
|
60 |
+
[-1, 1, Conv, [256, 3, 1]],
|
61 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
62 |
+
[-1, 1, Conv, [1280, 1, 1]], # 43
|
63 |
+
|
64 |
+
[-1, 1, MP, []],
|
65 |
+
[-1, 1, Conv, [640, 1, 1]],
|
66 |
+
[-3, 1, Conv, [640, 1, 1]],
|
67 |
+
[-1, 1, Conv, [640, 3, 2]],
|
68 |
+
[[-1, -3], 1, Concat, [1]], # 48-P5/32
|
69 |
+
[-1, 1, Conv, [256, 1, 1]],
|
70 |
+
[-2, 1, Conv, [256, 1, 1]],
|
71 |
+
[-1, 1, Conv, [256, 3, 1]],
|
72 |
+
[-1, 1, Conv, [256, 3, 1]],
|
73 |
+
[-1, 1, Conv, [256, 3, 1]],
|
74 |
+
[-1, 1, Conv, [256, 3, 1]],
|
75 |
+
[-1, 1, Conv, [256, 3, 1]],
|
76 |
+
[-1, 1, Conv, [256, 3, 1]],
|
77 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
78 |
+
[-1, 1, Conv, [1280, 1, 1]], # 58
|
79 |
+
]
|
80 |
+
|
81 |
+
# yolov7 head
|
82 |
+
head:
|
83 |
+
[[-1, 1, SPPCSPC, [640]], # 59
|
84 |
+
|
85 |
+
[-1, 1, Conv, [320, 1, 1]],
|
86 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
87 |
+
[43, 1, Conv, [320, 1, 1]], # route backbone P4
|
88 |
+
[[-1, -2], 1, Concat, [1]],
|
89 |
+
|
90 |
+
[-1, 1, Conv, [256, 1, 1]],
|
91 |
+
[-2, 1, Conv, [256, 1, 1]],
|
92 |
+
[-1, 1, Conv, [256, 3, 1]],
|
93 |
+
[-1, 1, Conv, [256, 3, 1]],
|
94 |
+
[-1, 1, Conv, [256, 3, 1]],
|
95 |
+
[-1, 1, Conv, [256, 3, 1]],
|
96 |
+
[-1, 1, Conv, [256, 3, 1]],
|
97 |
+
[-1, 1, Conv, [256, 3, 1]],
|
98 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
99 |
+
[-1, 1, Conv, [320, 1, 1]], # 73
|
100 |
+
|
101 |
+
[-1, 1, Conv, [160, 1, 1]],
|
102 |
+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
103 |
+
[28, 1, Conv, [160, 1, 1]], # route backbone P3
|
104 |
+
[[-1, -2], 1, Concat, [1]],
|
105 |
+
|
106 |
+
[-1, 1, Conv, [128, 1, 1]],
|
107 |
+
[-2, 1, Conv, [128, 1, 1]],
|
108 |
+
[-1, 1, Conv, [128, 3, 1]],
|
109 |
+
[-1, 1, Conv, [128, 3, 1]],
|
110 |
+
[-1, 1, Conv, [128, 3, 1]],
|
111 |
+
[-1, 1, Conv, [128, 3, 1]],
|
112 |
+
[-1, 1, Conv, [128, 3, 1]],
|
113 |
+
[-1, 1, Conv, [128, 3, 1]],
|
114 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
115 |
+
[-1, 1, Conv, [160, 1, 1]], # 87
|
116 |
+
|
117 |
+
[-1, 1, MP, []],
|
118 |
+
[-1, 1, Conv, [160, 1, 1]],
|
119 |
+
[-3, 1, Conv, [160, 1, 1]],
|
120 |
+
[-1, 1, Conv, [160, 3, 2]],
|
121 |
+
[[-1, -3, 73], 1, Concat, [1]],
|
122 |
+
|
123 |
+
[-1, 1, Conv, [256, 1, 1]],
|
124 |
+
[-2, 1, Conv, [256, 1, 1]],
|
125 |
+
[-1, 1, Conv, [256, 3, 1]],
|
126 |
+
[-1, 1, Conv, [256, 3, 1]],
|
127 |
+
[-1, 1, Conv, [256, 3, 1]],
|
128 |
+
[-1, 1, Conv, [256, 3, 1]],
|
129 |
+
[-1, 1, Conv, [256, 3, 1]],
|
130 |
+
[-1, 1, Conv, [256, 3, 1]],
|
131 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
132 |
+
[-1, 1, Conv, [320, 1, 1]], # 102
|
133 |
+
|
134 |
+
[-1, 1, MP, []],
|
135 |
+
[-1, 1, Conv, [320, 1, 1]],
|
136 |
+
[-3, 1, Conv, [320, 1, 1]],
|
137 |
+
[-1, 1, Conv, [320, 3, 2]],
|
138 |
+
[[-1, -3, 59], 1, Concat, [1]],
|
139 |
+
|
140 |
+
[-1, 1, Conv, [512, 1, 1]],
|
141 |
+
[-2, 1, Conv, [512, 1, 1]],
|
142 |
+
[-1, 1, Conv, [512, 3, 1]],
|
143 |
+
[-1, 1, Conv, [512, 3, 1]],
|
144 |
+
[-1, 1, Conv, [512, 3, 1]],
|
145 |
+
[-1, 1, Conv, [512, 3, 1]],
|
146 |
+
[-1, 1, Conv, [512, 3, 1]],
|
147 |
+
[-1, 1, Conv, [512, 3, 1]],
|
148 |
+
[[-1, -3, -5, -7, -8], 1, Concat, [1]],
|
149 |
+
[-1, 1, Conv, [640, 1, 1]], # 117
|
150 |
+
|
151 |
+
[87, 1, Conv, [320, 3, 1]],
|
152 |
+
[102, 1, Conv, [640, 3, 1]],
|
153 |
+
[117, 1, Conv, [1280, 3, 1]],
|
154 |
+
|
155 |
+
[[118,119,120], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
|
156 |
+
]
|
infer/yolov7/data/coco.yaml
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# COCO 2017 dataset http://cocodataset.org
|
2 |
+
|
3 |
+
# download command/URL (optional)
|
4 |
+
download: bash ./scripts/get_coco.sh
|
5 |
+
|
6 |
+
# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
|
7 |
+
train: ./coco/train2017.txt # 118287 images
|
8 |
+
val: ./coco/val2017.txt # 5000 images
|
9 |
+
test: ./coco/test-dev2017.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
|
10 |
+
|
11 |
+
# number of classes
|
12 |
+
nc: 80
|
13 |
+
|
14 |
+
# class names
|
15 |
+
names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
|
16 |
+
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
|
17 |
+
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
|
18 |
+
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
|
19 |
+
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
|
20 |
+
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
|
21 |
+
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
|
22 |
+
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
|
23 |
+
'hair drier', 'toothbrush' ]
|
infer/yolov7/data/hyp.scratch.custom.yaml
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
|
2 |
+
lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf)
|
3 |
+
momentum: 0.937 # SGD momentum/Adam beta1
|
4 |
+
weight_decay: 0.0005 # optimizer weight decay 5e-4
|
5 |
+
warmup_epochs: 3.0 # warmup epochs (fractions ok)
|
6 |
+
warmup_momentum: 0.8 # warmup initial momentum
|
7 |
+
warmup_bias_lr: 0.1 # warmup initial bias lr
|
8 |
+
box: 0.05 # box loss gain
|
9 |
+
cls: 0.3 # cls loss gain
|
10 |
+
cls_pw: 1.0 # cls BCELoss positive_weight
|
11 |
+
obj: 0.7 # obj loss gain (scale with pixels)
|
12 |
+
obj_pw: 1.0 # obj BCELoss positive_weight
|
13 |
+
iou_t: 0.20 # IoU training threshold
|
14 |
+
anchor_t: 4.0 # anchor-multiple threshold
|
15 |
+
# anchors: 3 # anchors per output layer (0 to ignore)
|
16 |
+
fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
|
17 |
+
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
|
18 |
+
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
|
19 |
+
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
|
20 |
+
degrees: 0.0 # image rotation (+/- deg)
|
21 |
+
translate: 0.2 # image translation (+/- fraction)
|
22 |
+
scale: 0.5 # image scale (+/- gain)
|
23 |
+
shear: 0.0 # image shear (+/- deg)
|
24 |
+
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
|
25 |
+
flipud: 0.0 # image flip up-down (probability)
|
26 |
+
fliplr: 0.5 # image flip left-right (probability)
|
27 |
+
mosaic: 1.0 # image mosaic (probability)
|
28 |
+
mixup: 0.0 # image mixup (probability)
|
29 |
+
copy_paste: 0.0 # image copy paste (probability)
|
30 |
+
paste_in: 0.0 # image copy paste (probability), use 0 for faster training
|
31 |
+
loss_ota: 1 # use ComputeLossOTA, use 0 for faster training
|
infer/yolov7/data/hyp.scratch.p5.yaml
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
|
2 |
+
lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf)
|
3 |
+
momentum: 0.937 # SGD momentum/Adam beta1
|
4 |
+
weight_decay: 0.0005 # optimizer weight decay 5e-4
|
5 |
+
warmup_epochs: 3.0 # warmup epochs (fractions ok)
|
6 |
+
warmup_momentum: 0.8 # warmup initial momentum
|
7 |
+
warmup_bias_lr: 0.1 # warmup initial bias lr
|
8 |
+
box: 0.05 # box loss gain
|
9 |
+
cls: 0.3 # cls loss gain
|
10 |
+
cls_pw: 1.0 # cls BCELoss positive_weight
|
11 |
+
obj: 0.7 # obj loss gain (scale with pixels)
|
12 |
+
obj_pw: 1.0 # obj BCELoss positive_weight
|
13 |
+
iou_t: 0.20 # IoU training threshold
|
14 |
+
anchor_t: 4.0 # anchor-multiple threshold
|
15 |
+
# anchors: 3 # anchors per output layer (0 to ignore)
|
16 |
+
fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
|
17 |
+
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
|
18 |
+
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
|
19 |
+
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
|
20 |
+
degrees: 0.0 # image rotation (+/- deg)
|
21 |
+
translate: 0.2 # image translation (+/- fraction)
|
22 |
+
scale: 0.9 # image scale (+/- gain)
|
23 |
+
shear: 0.0 # image shear (+/- deg)
|
24 |
+
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
|
25 |
+
flipud: 0.0 # image flip up-down (probability)
|
26 |
+
fliplr: 0.5 # image flip left-right (probability)
|
27 |
+
mosaic: 1.0 # image mosaic (probability)
|
28 |
+
mixup: 0.15 # image mixup (probability)
|
29 |
+
copy_paste: 0.0 # image copy paste (probability)
|
30 |
+
paste_in: 0.15 # image copy paste (probability), use 0 for faster training
|
31 |
+
loss_ota: 1 # use ComputeLossOTA, use 0 for faster training
|
infer/yolov7/data/hyp.scratch.p6.yaml
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
|
2 |
+
lrf: 0.2 # final OneCycleLR learning rate (lr0 * lrf)
|
3 |
+
momentum: 0.937 # SGD momentum/Adam beta1
|
4 |
+
weight_decay: 0.0005 # optimizer weight decay 5e-4
|
5 |
+
warmup_epochs: 3.0 # warmup epochs (fractions ok)
|
6 |
+
warmup_momentum: 0.8 # warmup initial momentum
|
7 |
+
warmup_bias_lr: 0.1 # warmup initial bias lr
|
8 |
+
box: 0.05 # box loss gain
|
9 |
+
cls: 0.3 # cls loss gain
|
10 |
+
cls_pw: 1.0 # cls BCELoss positive_weight
|
11 |
+
obj: 0.7 # obj loss gain (scale with pixels)
|
12 |
+
obj_pw: 1.0 # obj BCELoss positive_weight
|
13 |
+
iou_t: 0.20 # IoU training threshold
|
14 |
+
anchor_t: 4.0 # anchor-multiple threshold
|
15 |
+
# anchors: 3 # anchors per output layer (0 to ignore)
|
16 |
+
fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
|
17 |
+
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
|
18 |
+
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
|
19 |
+
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
|
20 |
+
degrees: 0.0 # image rotation (+/- deg)
|
21 |
+
translate: 0.2 # image translation (+/- fraction)
|
22 |
+
scale: 0.9 # image scale (+/- gain)
|
23 |
+
shear: 0.0 # image shear (+/- deg)
|
24 |
+
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
|
25 |
+
flipud: 0.0 # image flip up-down (probability)
|
26 |
+
fliplr: 0.5 # image flip left-right (probability)
|
27 |
+
mosaic: 1.0 # image mosaic (probability)
|
28 |
+
mixup: 0.15 # image mixup (probability)
|
29 |
+
copy_paste: 0.0 # image copy paste (probability)
|
30 |
+
paste_in: 0.15 # image copy paste (probability), use 0 for faster training
|
31 |
+
loss_ota: 1 # use ComputeLossOTA, use 0 for faster training
|
infer/yolov7/data/hyp.scratch.tiny.yaml
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
|
2 |
+
lrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf)
|
3 |
+
momentum: 0.937 # SGD momentum/Adam beta1
|
4 |
+
weight_decay: 0.0005 # optimizer weight decay 5e-4
|
5 |
+
warmup_epochs: 3.0 # warmup epochs (fractions ok)
|
6 |
+
warmup_momentum: 0.8 # warmup initial momentum
|
7 |
+
warmup_bias_lr: 0.1 # warmup initial bias lr
|
8 |
+
box: 0.05 # box loss gain
|
9 |
+
cls: 0.5 # cls loss gain
|
10 |
+
cls_pw: 1.0 # cls BCELoss positive_weight
|
11 |
+
obj: 1.0 # obj loss gain (scale with pixels)
|
12 |
+
obj_pw: 1.0 # obj BCELoss positive_weight
|
13 |
+
iou_t: 0.20 # IoU training threshold
|
14 |
+
anchor_t: 4.0 # anchor-multiple threshold
|
15 |
+
# anchors: 3 # anchors per output layer (0 to ignore)
|
16 |
+
fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
|
17 |
+
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
|
18 |
+
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
|
19 |
+
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
|
20 |
+
degrees: 0.0 # image rotation (+/- deg)
|
21 |
+
translate: 0.1 # image translation (+/- fraction)
|
22 |
+
scale: 0.5 # image scale (+/- gain)
|
23 |
+
shear: 0.0 # image shear (+/- deg)
|
24 |
+
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
|
25 |
+
flipud: 0.0 # image flip up-down (probability)
|
26 |
+
fliplr: 0.5 # image flip left-right (probability)
|
27 |
+
mosaic: 1.0 # image mosaic (probability)
|
28 |
+
mixup: 0.05 # image mixup (probability)
|
29 |
+
copy_paste: 0.0 # image copy paste (probability)
|
30 |
+
paste_in: 0.05 # image copy paste (probability), use 0 for faster training
|
31 |
+
loss_ota: 1 # use ComputeLossOTA, use 0 for faster training
|
infer/yolov7/get_results.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import cv2
|
3 |
+
from infer.yolov7.models.experimental import attempt_load
|
4 |
+
from infer.yolov7.utils.datasets import letterbox
|
5 |
+
from infer.yolov7.utils.general import non_max_suppression, scale_coords
|
6 |
+
from infer.yolov7.utils.torch_utils import select_device
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
colors = [(np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255)) for _ in range(20)]
|
10 |
+
|
11 |
+
def get_yolov7_result(image, conf_threshold, iou_threshold, class_names):
|
12 |
+
weights = 'my_model/v7-tiny.pt'
|
13 |
+
device = select_device('cpu')
|
14 |
+
model = attempt_load(weights, map_location=device)
|
15 |
+
model.eval()
|
16 |
+
|
17 |
+
img = letterbox(image, new_shape=320)[0]
|
18 |
+
img = img[:, :, ::-1].transpose(2, 0, 1)
|
19 |
+
img = torch.from_numpy(img.copy()).to(device)
|
20 |
+
img = img.float()
|
21 |
+
img /= 255.0
|
22 |
+
if img.ndimension() == 3:
|
23 |
+
img = img.unsqueeze(0)
|
24 |
+
|
25 |
+
with torch.no_grad():
|
26 |
+
pred = model(img, augment=False)[0]
|
27 |
+
pred = non_max_suppression(pred, conf_threshold, iou_threshold, classes=None, agnostic=False)
|
28 |
+
|
29 |
+
img0 = image.copy()
|
30 |
+
result_list = []
|
31 |
+
|
32 |
+
for i, det in enumerate(pred):
|
33 |
+
if len(det):
|
34 |
+
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
|
35 |
+
for *xyxy, conf, cls in reversed(det):
|
36 |
+
x1, y1, x2, y2 = map(int, xyxy)
|
37 |
+
label = f'{class_names[int(cls) - 1]} {conf:.2f}'
|
38 |
+
color = colors[int(cls) % len(colors)]
|
39 |
+
result_list.append([class_names[int(cls) - 1], color, conf])
|
40 |
+
cv2.rectangle(img0, (x1, y1), (x2, y2), color, 2)
|
41 |
+
cv2.putText(img0, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
42 |
+
img0 = img0[...,::-1]
|
43 |
+
return img0, result_list
|
infer/yolov7/models/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# init
|
2 |
+
from .yolo import *
|
infer/yolov7/models/common.py
ADDED
@@ -0,0 +1,2019 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
from copy import copy
|
3 |
+
from pathlib import Path
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
import requests
|
8 |
+
import torch
|
9 |
+
import torch.nn as nn
|
10 |
+
import torch.nn.functional as F
|
11 |
+
from torchvision.ops import DeformConv2d
|
12 |
+
from PIL import Image
|
13 |
+
from torch.cuda import amp
|
14 |
+
|
15 |
+
from infer.yolov7.utils.datasets import letterbox
|
16 |
+
from infer.yolov7.utils.general import non_max_suppression, make_divisible, scale_coords, increment_path, xyxy2xywh
|
17 |
+
from infer.yolov7.utils.plots import color_list, plot_one_box
|
18 |
+
from infer.yolov7.utils.torch_utils import time_synchronized
|
19 |
+
|
20 |
+
|
21 |
+
##### basic ####
|
22 |
+
|
23 |
+
def autopad(k, p=None): # kernel, padding
|
24 |
+
# Pad to 'same'
|
25 |
+
if p is None:
|
26 |
+
p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
|
27 |
+
return p
|
28 |
+
|
29 |
+
|
30 |
+
class MP(nn.Module):
|
31 |
+
def __init__(self, k=2):
|
32 |
+
super(MP, self).__init__()
|
33 |
+
self.m = nn.MaxPool2d(kernel_size=k, stride=k)
|
34 |
+
|
35 |
+
def forward(self, x):
|
36 |
+
return self.m(x)
|
37 |
+
|
38 |
+
|
39 |
+
class SP(nn.Module):
|
40 |
+
def __init__(self, k=3, s=1):
|
41 |
+
super(SP, self).__init__()
|
42 |
+
self.m = nn.MaxPool2d(kernel_size=k, stride=s, padding=k // 2)
|
43 |
+
|
44 |
+
def forward(self, x):
|
45 |
+
return self.m(x)
|
46 |
+
|
47 |
+
|
48 |
+
class ReOrg(nn.Module):
|
49 |
+
def __init__(self):
|
50 |
+
super(ReOrg, self).__init__()
|
51 |
+
|
52 |
+
def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
|
53 |
+
return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)
|
54 |
+
|
55 |
+
|
56 |
+
class Concat(nn.Module):
|
57 |
+
def __init__(self, dimension=1):
|
58 |
+
super(Concat, self).__init__()
|
59 |
+
self.d = dimension
|
60 |
+
|
61 |
+
def forward(self, x):
|
62 |
+
return torch.cat(x, self.d)
|
63 |
+
|
64 |
+
|
65 |
+
class Chuncat(nn.Module):
|
66 |
+
def __init__(self, dimension=1):
|
67 |
+
super(Chuncat, self).__init__()
|
68 |
+
self.d = dimension
|
69 |
+
|
70 |
+
def forward(self, x):
|
71 |
+
x1 = []
|
72 |
+
x2 = []
|
73 |
+
for xi in x:
|
74 |
+
xi1, xi2 = xi.chunk(2, self.d)
|
75 |
+
x1.append(xi1)
|
76 |
+
x2.append(xi2)
|
77 |
+
return torch.cat(x1+x2, self.d)
|
78 |
+
|
79 |
+
|
80 |
+
class Shortcut(nn.Module):
|
81 |
+
def __init__(self, dimension=0):
|
82 |
+
super(Shortcut, self).__init__()
|
83 |
+
self.d = dimension
|
84 |
+
|
85 |
+
def forward(self, x):
|
86 |
+
return x[0]+x[1]
|
87 |
+
|
88 |
+
|
89 |
+
class Foldcut(nn.Module):
|
90 |
+
def __init__(self, dimension=0):
|
91 |
+
super(Foldcut, self).__init__()
|
92 |
+
self.d = dimension
|
93 |
+
|
94 |
+
def forward(self, x):
|
95 |
+
x1, x2 = x.chunk(2, self.d)
|
96 |
+
return x1+x2
|
97 |
+
|
98 |
+
|
99 |
+
class Conv(nn.Module):
|
100 |
+
# Standard convolution
|
101 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
102 |
+
super(Conv, self).__init__()
|
103 |
+
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
|
104 |
+
self.bn = nn.BatchNorm2d(c2)
|
105 |
+
self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
|
106 |
+
|
107 |
+
def forward(self, x):
|
108 |
+
return self.act(self.bn(self.conv(x)))
|
109 |
+
|
110 |
+
def fuseforward(self, x):
|
111 |
+
return self.act(self.conv(x))
|
112 |
+
|
113 |
+
|
114 |
+
class RobustConv(nn.Module):
|
115 |
+
# Robust convolution (use high kernel size 7-11 for: downsampling and other layers). Train for 300 - 450 epochs.
|
116 |
+
def __init__(self, c1, c2, k=7, s=1, p=None, g=1, act=True, layer_scale_init_value=1e-6): # ch_in, ch_out, kernel, stride, padding, groups
|
117 |
+
super(RobustConv, self).__init__()
|
118 |
+
self.conv_dw = Conv(c1, c1, k=k, s=s, p=p, g=c1, act=act)
|
119 |
+
self.conv1x1 = nn.Conv2d(c1, c2, 1, 1, 0, groups=1, bias=True)
|
120 |
+
self.gamma = nn.Parameter(layer_scale_init_value * torch.ones(c2)) if layer_scale_init_value > 0 else None
|
121 |
+
|
122 |
+
def forward(self, x):
|
123 |
+
x = x.to(memory_format=torch.channels_last)
|
124 |
+
x = self.conv1x1(self.conv_dw(x))
|
125 |
+
if self.gamma is not None:
|
126 |
+
x = x.mul(self.gamma.reshape(1, -1, 1, 1))
|
127 |
+
return x
|
128 |
+
|
129 |
+
|
130 |
+
class RobustConv2(nn.Module):
|
131 |
+
# Robust convolution 2 (use [32, 5, 2] or [32, 7, 4] or [32, 11, 8] for one of the paths in CSP).
|
132 |
+
def __init__(self, c1, c2, k=7, s=4, p=None, g=1, act=True, layer_scale_init_value=1e-6): # ch_in, ch_out, kernel, stride, padding, groups
|
133 |
+
super(RobustConv2, self).__init__()
|
134 |
+
self.conv_strided = Conv(c1, c1, k=k, s=s, p=p, g=c1, act=act)
|
135 |
+
self.conv_deconv = nn.ConvTranspose2d(in_channels=c1, out_channels=c2, kernel_size=s, stride=s,
|
136 |
+
padding=0, bias=True, dilation=1, groups=1
|
137 |
+
)
|
138 |
+
self.gamma = nn.Parameter(layer_scale_init_value * torch.ones(c2)) if layer_scale_init_value > 0 else None
|
139 |
+
|
140 |
+
def forward(self, x):
|
141 |
+
x = self.conv_deconv(self.conv_strided(x))
|
142 |
+
if self.gamma is not None:
|
143 |
+
x = x.mul(self.gamma.reshape(1, -1, 1, 1))
|
144 |
+
return x
|
145 |
+
|
146 |
+
|
147 |
+
def DWConv(c1, c2, k=1, s=1, act=True):
|
148 |
+
# Depthwise convolution
|
149 |
+
return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
|
150 |
+
|
151 |
+
|
152 |
+
class GhostConv(nn.Module):
|
153 |
+
# Ghost Convolution https://github.com/huawei-noah/ghostnet
|
154 |
+
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
|
155 |
+
super(GhostConv, self).__init__()
|
156 |
+
c_ = c2 // 2 # hidden channels
|
157 |
+
self.cv1 = Conv(c1, c_, k, s, None, g, act)
|
158 |
+
self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)
|
159 |
+
|
160 |
+
def forward(self, x):
|
161 |
+
y = self.cv1(x)
|
162 |
+
return torch.cat([y, self.cv2(y)], 1)
|
163 |
+
|
164 |
+
|
165 |
+
class Stem(nn.Module):
|
166 |
+
# Stem
|
167 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
168 |
+
super(Stem, self).__init__()
|
169 |
+
c_ = int(c2/2) # hidden channels
|
170 |
+
self.cv1 = Conv(c1, c_, 3, 2)
|
171 |
+
self.cv2 = Conv(c_, c_, 1, 1)
|
172 |
+
self.cv3 = Conv(c_, c_, 3, 2)
|
173 |
+
self.pool = torch.nn.MaxPool2d(2, stride=2)
|
174 |
+
self.cv4 = Conv(2 * c_, c2, 1, 1)
|
175 |
+
|
176 |
+
def forward(self, x):
|
177 |
+
x = self.cv1(x)
|
178 |
+
return self.cv4(torch.cat((self.cv3(self.cv2(x)), self.pool(x)), dim=1))
|
179 |
+
|
180 |
+
|
181 |
+
class DownC(nn.Module):
|
182 |
+
# Spatial pyramid pooling layer used in YOLOv3-SPP
|
183 |
+
def __init__(self, c1, c2, n=1, k=2):
|
184 |
+
super(DownC, self).__init__()
|
185 |
+
c_ = int(c1) # hidden channels
|
186 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
187 |
+
self.cv2 = Conv(c_, c2//2, 3, k)
|
188 |
+
self.cv3 = Conv(c1, c2//2, 1, 1)
|
189 |
+
self.mp = nn.MaxPool2d(kernel_size=k, stride=k)
|
190 |
+
|
191 |
+
def forward(self, x):
|
192 |
+
return torch.cat((self.cv2(self.cv1(x)), self.cv3(self.mp(x))), dim=1)
|
193 |
+
|
194 |
+
|
195 |
+
class SPP(nn.Module):
|
196 |
+
# Spatial pyramid pooling layer used in YOLOv3-SPP
|
197 |
+
def __init__(self, c1, c2, k=(5, 9, 13)):
|
198 |
+
super(SPP, self).__init__()
|
199 |
+
c_ = c1 // 2 # hidden channels
|
200 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
201 |
+
self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
|
202 |
+
self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
|
203 |
+
|
204 |
+
def forward(self, x):
|
205 |
+
x = self.cv1(x)
|
206 |
+
return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
|
207 |
+
|
208 |
+
|
209 |
+
class Bottleneck(nn.Module):
|
210 |
+
# Darknet bottleneck
|
211 |
+
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
212 |
+
super(Bottleneck, self).__init__()
|
213 |
+
c_ = int(c2 * e) # hidden channels
|
214 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
215 |
+
self.cv2 = Conv(c_, c2, 3, 1, g=g)
|
216 |
+
self.add = shortcut and c1 == c2
|
217 |
+
|
218 |
+
def forward(self, x):
|
219 |
+
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
220 |
+
|
221 |
+
|
222 |
+
class Res(nn.Module):
|
223 |
+
# ResNet bottleneck
|
224 |
+
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
225 |
+
super(Res, self).__init__()
|
226 |
+
c_ = int(c2 * e) # hidden channels
|
227 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
228 |
+
self.cv2 = Conv(c_, c_, 3, 1, g=g)
|
229 |
+
self.cv3 = Conv(c_, c2, 1, 1)
|
230 |
+
self.add = shortcut and c1 == c2
|
231 |
+
|
232 |
+
def forward(self, x):
|
233 |
+
return x + self.cv3(self.cv2(self.cv1(x))) if self.add else self.cv3(self.cv2(self.cv1(x)))
|
234 |
+
|
235 |
+
|
236 |
+
class ResX(Res):
|
237 |
+
# ResNet bottleneck
|
238 |
+
def __init__(self, c1, c2, shortcut=True, g=32, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
239 |
+
super().__init__(c1, c2, shortcut, g, e)
|
240 |
+
c_ = int(c2 * e) # hidden channels
|
241 |
+
|
242 |
+
|
243 |
+
class Ghost(nn.Module):
|
244 |
+
# Ghost Bottleneck https://github.com/huawei-noah/ghostnet
|
245 |
+
def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride
|
246 |
+
super(Ghost, self).__init__()
|
247 |
+
c_ = c2 // 2
|
248 |
+
self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw
|
249 |
+
DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw
|
250 |
+
GhostConv(c_, c2, 1, 1, act=False)) # pw-linear
|
251 |
+
self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False),
|
252 |
+
Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity()
|
253 |
+
|
254 |
+
def forward(self, x):
|
255 |
+
return self.conv(x) + self.shortcut(x)
|
256 |
+
|
257 |
+
##### end of basic #####
|
258 |
+
|
259 |
+
|
260 |
+
##### cspnet #####
|
261 |
+
|
262 |
+
class SPPCSPC(nn.Module):
|
263 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
264 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):
|
265 |
+
super(SPPCSPC, self).__init__()
|
266 |
+
c_ = int(2 * c2 * e) # hidden channels
|
267 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
268 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
269 |
+
self.cv3 = Conv(c_, c_, 3, 1)
|
270 |
+
self.cv4 = Conv(c_, c_, 1, 1)
|
271 |
+
self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
|
272 |
+
self.cv5 = Conv(4 * c_, c_, 1, 1)
|
273 |
+
self.cv6 = Conv(c_, c_, 3, 1)
|
274 |
+
self.cv7 = Conv(2 * c_, c2, 1, 1)
|
275 |
+
|
276 |
+
def forward(self, x):
|
277 |
+
x1 = self.cv4(self.cv3(self.cv1(x)))
|
278 |
+
y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1)))
|
279 |
+
y2 = self.cv2(x)
|
280 |
+
return self.cv7(torch.cat((y1, y2), dim=1))
|
281 |
+
|
282 |
+
class GhostSPPCSPC(SPPCSPC):
|
283 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
284 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)):
|
285 |
+
super().__init__(c1, c2, n, shortcut, g, e, k)
|
286 |
+
c_ = int(2 * c2 * e) # hidden channels
|
287 |
+
self.cv1 = GhostConv(c1, c_, 1, 1)
|
288 |
+
self.cv2 = GhostConv(c1, c_, 1, 1)
|
289 |
+
self.cv3 = GhostConv(c_, c_, 3, 1)
|
290 |
+
self.cv4 = GhostConv(c_, c_, 1, 1)
|
291 |
+
self.cv5 = GhostConv(4 * c_, c_, 1, 1)
|
292 |
+
self.cv6 = GhostConv(c_, c_, 3, 1)
|
293 |
+
self.cv7 = GhostConv(2 * c_, c2, 1, 1)
|
294 |
+
|
295 |
+
|
296 |
+
class GhostStem(Stem):
|
297 |
+
# Stem
|
298 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
299 |
+
super().__init__(c1, c2, k, s, p, g, act)
|
300 |
+
c_ = int(c2/2) # hidden channels
|
301 |
+
self.cv1 = GhostConv(c1, c_, 3, 2)
|
302 |
+
self.cv2 = GhostConv(c_, c_, 1, 1)
|
303 |
+
self.cv3 = GhostConv(c_, c_, 3, 2)
|
304 |
+
self.cv4 = GhostConv(2 * c_, c2, 1, 1)
|
305 |
+
|
306 |
+
|
307 |
+
class BottleneckCSPA(nn.Module):
|
308 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
309 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
310 |
+
super(BottleneckCSPA, self).__init__()
|
311 |
+
c_ = int(c2 * e) # hidden channels
|
312 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
313 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
314 |
+
self.cv3 = Conv(2 * c_, c2, 1, 1)
|
315 |
+
self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
316 |
+
|
317 |
+
def forward(self, x):
|
318 |
+
y1 = self.m(self.cv1(x))
|
319 |
+
y2 = self.cv2(x)
|
320 |
+
return self.cv3(torch.cat((y1, y2), dim=1))
|
321 |
+
|
322 |
+
|
323 |
+
class BottleneckCSPB(nn.Module):
|
324 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
325 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
326 |
+
super(BottleneckCSPB, self).__init__()
|
327 |
+
c_ = int(c2) # hidden channels
|
328 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
329 |
+
self.cv2 = Conv(c_, c_, 1, 1)
|
330 |
+
self.cv3 = Conv(2 * c_, c2, 1, 1)
|
331 |
+
self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
332 |
+
|
333 |
+
def forward(self, x):
|
334 |
+
x1 = self.cv1(x)
|
335 |
+
y1 = self.m(x1)
|
336 |
+
y2 = self.cv2(x1)
|
337 |
+
return self.cv3(torch.cat((y1, y2), dim=1))
|
338 |
+
|
339 |
+
|
340 |
+
class BottleneckCSPC(nn.Module):
|
341 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
342 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
343 |
+
super(BottleneckCSPC, self).__init__()
|
344 |
+
c_ = int(c2 * e) # hidden channels
|
345 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
346 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
347 |
+
self.cv3 = Conv(c_, c_, 1, 1)
|
348 |
+
self.cv4 = Conv(2 * c_, c2, 1, 1)
|
349 |
+
self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
350 |
+
|
351 |
+
def forward(self, x):
|
352 |
+
y1 = self.cv3(self.m(self.cv1(x)))
|
353 |
+
y2 = self.cv2(x)
|
354 |
+
return self.cv4(torch.cat((y1, y2), dim=1))
|
355 |
+
|
356 |
+
|
357 |
+
class ResCSPA(BottleneckCSPA):
|
358 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
359 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
360 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
361 |
+
c_ = int(c2 * e) # hidden channels
|
362 |
+
self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
363 |
+
|
364 |
+
|
365 |
+
class ResCSPB(BottleneckCSPB):
|
366 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
367 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
368 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
369 |
+
c_ = int(c2) # hidden channels
|
370 |
+
self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
371 |
+
|
372 |
+
|
373 |
+
class ResCSPC(BottleneckCSPC):
|
374 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
375 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
376 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
377 |
+
c_ = int(c2 * e) # hidden channels
|
378 |
+
self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
379 |
+
|
380 |
+
|
381 |
+
class ResXCSPA(ResCSPA):
|
382 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
383 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
384 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
385 |
+
c_ = int(c2 * e) # hidden channels
|
386 |
+
self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
387 |
+
|
388 |
+
|
389 |
+
class ResXCSPB(ResCSPB):
|
390 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
391 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
392 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
393 |
+
c_ = int(c2) # hidden channels
|
394 |
+
self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
395 |
+
|
396 |
+
|
397 |
+
class ResXCSPC(ResCSPC):
|
398 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
399 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
400 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
401 |
+
c_ = int(c2 * e) # hidden channels
|
402 |
+
self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
403 |
+
|
404 |
+
|
405 |
+
class GhostCSPA(BottleneckCSPA):
|
406 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
407 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
408 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
409 |
+
c_ = int(c2 * e) # hidden channels
|
410 |
+
self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)])
|
411 |
+
|
412 |
+
|
413 |
+
class GhostCSPB(BottleneckCSPB):
|
414 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
415 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
416 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
417 |
+
c_ = int(c2) # hidden channels
|
418 |
+
self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)])
|
419 |
+
|
420 |
+
|
421 |
+
class GhostCSPC(BottleneckCSPC):
|
422 |
+
# CSP https://github.com/WongKinYiu/CrossStagePartialNetworks
|
423 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
424 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
425 |
+
c_ = int(c2 * e) # hidden channels
|
426 |
+
self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)])
|
427 |
+
|
428 |
+
##### end of cspnet #####
|
429 |
+
|
430 |
+
|
431 |
+
##### yolor #####
|
432 |
+
|
433 |
+
class ImplicitA(nn.Module):
|
434 |
+
def __init__(self, channel, mean=0., std=.02):
|
435 |
+
super(ImplicitA, self).__init__()
|
436 |
+
self.channel = channel
|
437 |
+
self.mean = mean
|
438 |
+
self.std = std
|
439 |
+
self.implicit = nn.Parameter(torch.zeros(1, channel, 1, 1))
|
440 |
+
nn.init.normal_(self.implicit, mean=self.mean, std=self.std)
|
441 |
+
|
442 |
+
def forward(self, x):
|
443 |
+
return self.implicit + x
|
444 |
+
|
445 |
+
|
446 |
+
class ImplicitM(nn.Module):
|
447 |
+
def __init__(self, channel, mean=1., std=.02):
|
448 |
+
super(ImplicitM, self).__init__()
|
449 |
+
self.channel = channel
|
450 |
+
self.mean = mean
|
451 |
+
self.std = std
|
452 |
+
self.implicit = nn.Parameter(torch.ones(1, channel, 1, 1))
|
453 |
+
nn.init.normal_(self.implicit, mean=self.mean, std=self.std)
|
454 |
+
|
455 |
+
def forward(self, x):
|
456 |
+
return self.implicit * x
|
457 |
+
|
458 |
+
##### end of yolor #####
|
459 |
+
|
460 |
+
|
461 |
+
##### repvgg #####
|
462 |
+
|
463 |
+
class RepConv(nn.Module):
|
464 |
+
# Represented convolution
|
465 |
+
# https://arxiv.org/abs/2101.03697
|
466 |
+
|
467 |
+
def __init__(self, c1, c2, k=3, s=1, p=None, g=1, act=True, deploy=False):
|
468 |
+
super(RepConv, self).__init__()
|
469 |
+
|
470 |
+
self.deploy = deploy
|
471 |
+
self.groups = g
|
472 |
+
self.in_channels = c1
|
473 |
+
self.out_channels = c2
|
474 |
+
|
475 |
+
assert k == 3
|
476 |
+
assert autopad(k, p) == 1
|
477 |
+
|
478 |
+
padding_11 = autopad(k, p) - k // 2
|
479 |
+
|
480 |
+
self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
|
481 |
+
|
482 |
+
if deploy:
|
483 |
+
self.rbr_reparam = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=True)
|
484 |
+
|
485 |
+
else:
|
486 |
+
self.rbr_identity = (nn.BatchNorm2d(num_features=c1) if c2 == c1 and s == 1 else None)
|
487 |
+
|
488 |
+
self.rbr_dense = nn.Sequential(
|
489 |
+
nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False),
|
490 |
+
nn.BatchNorm2d(num_features=c2),
|
491 |
+
)
|
492 |
+
|
493 |
+
self.rbr_1x1 = nn.Sequential(
|
494 |
+
nn.Conv2d( c1, c2, 1, s, padding_11, groups=g, bias=False),
|
495 |
+
nn.BatchNorm2d(num_features=c2),
|
496 |
+
)
|
497 |
+
|
498 |
+
def forward(self, inputs):
|
499 |
+
if hasattr(self, "rbr_reparam"):
|
500 |
+
return self.act(self.rbr_reparam(inputs))
|
501 |
+
|
502 |
+
if self.rbr_identity is None:
|
503 |
+
id_out = 0
|
504 |
+
else:
|
505 |
+
id_out = self.rbr_identity(inputs)
|
506 |
+
|
507 |
+
return self.act(self.rbr_dense(inputs) + self.rbr_1x1(inputs) + id_out)
|
508 |
+
|
509 |
+
def get_equivalent_kernel_bias(self):
|
510 |
+
kernel3x3, bias3x3 = self._fuse_bn_tensor(self.rbr_dense)
|
511 |
+
kernel1x1, bias1x1 = self._fuse_bn_tensor(self.rbr_1x1)
|
512 |
+
kernelid, biasid = self._fuse_bn_tensor(self.rbr_identity)
|
513 |
+
return (
|
514 |
+
kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid,
|
515 |
+
bias3x3 + bias1x1 + biasid,
|
516 |
+
)
|
517 |
+
|
518 |
+
def _pad_1x1_to_3x3_tensor(self, kernel1x1):
|
519 |
+
if kernel1x1 is None:
|
520 |
+
return 0
|
521 |
+
else:
|
522 |
+
return nn.functional.pad(kernel1x1, [1, 1, 1, 1])
|
523 |
+
|
524 |
+
def _fuse_bn_tensor(self, branch):
|
525 |
+
if branch is None:
|
526 |
+
return 0, 0
|
527 |
+
if isinstance(branch, nn.Sequential):
|
528 |
+
kernel = branch[0].weight
|
529 |
+
running_mean = branch[1].running_mean
|
530 |
+
running_var = branch[1].running_var
|
531 |
+
gamma = branch[1].weight
|
532 |
+
beta = branch[1].bias
|
533 |
+
eps = branch[1].eps
|
534 |
+
else:
|
535 |
+
assert isinstance(branch, nn.BatchNorm2d)
|
536 |
+
if not hasattr(self, "id_tensor"):
|
537 |
+
input_dim = self.in_channels // self.groups
|
538 |
+
kernel_value = np.zeros(
|
539 |
+
(self.in_channels, input_dim, 3, 3), dtype=np.float32
|
540 |
+
)
|
541 |
+
for i in range(self.in_channels):
|
542 |
+
kernel_value[i, i % input_dim, 1, 1] = 1
|
543 |
+
self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device)
|
544 |
+
kernel = self.id_tensor
|
545 |
+
running_mean = branch.running_mean
|
546 |
+
running_var = branch.running_var
|
547 |
+
gamma = branch.weight
|
548 |
+
beta = branch.bias
|
549 |
+
eps = branch.eps
|
550 |
+
std = (running_var + eps).sqrt()
|
551 |
+
t = (gamma / std).reshape(-1, 1, 1, 1)
|
552 |
+
return kernel * t, beta - running_mean * gamma / std
|
553 |
+
|
554 |
+
def repvgg_convert(self):
|
555 |
+
kernel, bias = self.get_equivalent_kernel_bias()
|
556 |
+
return (
|
557 |
+
kernel.detach().cpu().numpy(),
|
558 |
+
bias.detach().cpu().numpy(),
|
559 |
+
)
|
560 |
+
|
561 |
+
def fuse_conv_bn(self, conv, bn):
|
562 |
+
|
563 |
+
std = (bn.running_var + bn.eps).sqrt()
|
564 |
+
bias = bn.bias - bn.running_mean * bn.weight / std
|
565 |
+
|
566 |
+
t = (bn.weight / std).reshape(-1, 1, 1, 1)
|
567 |
+
weights = conv.weight * t
|
568 |
+
|
569 |
+
bn = nn.Identity()
|
570 |
+
conv = nn.Conv2d(in_channels = conv.in_channels,
|
571 |
+
out_channels = conv.out_channels,
|
572 |
+
kernel_size = conv.kernel_size,
|
573 |
+
stride=conv.stride,
|
574 |
+
padding = conv.padding,
|
575 |
+
dilation = conv.dilation,
|
576 |
+
groups = conv.groups,
|
577 |
+
bias = True,
|
578 |
+
padding_mode = conv.padding_mode)
|
579 |
+
|
580 |
+
conv.weight = torch.nn.Parameter(weights)
|
581 |
+
conv.bias = torch.nn.Parameter(bias)
|
582 |
+
return conv
|
583 |
+
|
584 |
+
def fuse_repvgg_block(self):
|
585 |
+
if self.deploy:
|
586 |
+
return
|
587 |
+
print(f"RepConv.fuse_repvgg_block")
|
588 |
+
|
589 |
+
self.rbr_dense = self.fuse_conv_bn(self.rbr_dense[0], self.rbr_dense[1])
|
590 |
+
|
591 |
+
self.rbr_1x1 = self.fuse_conv_bn(self.rbr_1x1[0], self.rbr_1x1[1])
|
592 |
+
rbr_1x1_bias = self.rbr_1x1.bias
|
593 |
+
weight_1x1_expanded = torch.nn.functional.pad(self.rbr_1x1.weight, [1, 1, 1, 1])
|
594 |
+
|
595 |
+
# Fuse self.rbr_identity
|
596 |
+
if (isinstance(self.rbr_identity, nn.BatchNorm2d) or isinstance(self.rbr_identity, nn.modules.batchnorm.SyncBatchNorm)):
|
597 |
+
# print(f"fuse: rbr_identity == BatchNorm2d or SyncBatchNorm")
|
598 |
+
identity_conv_1x1 = nn.Conv2d(
|
599 |
+
in_channels=self.in_channels,
|
600 |
+
out_channels=self.out_channels,
|
601 |
+
kernel_size=1,
|
602 |
+
stride=1,
|
603 |
+
padding=0,
|
604 |
+
groups=self.groups,
|
605 |
+
bias=False)
|
606 |
+
identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.to(self.rbr_1x1.weight.data.device)
|
607 |
+
identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.squeeze().squeeze()
|
608 |
+
# print(f" identity_conv_1x1.weight = {identity_conv_1x1.weight.shape}")
|
609 |
+
identity_conv_1x1.weight.data.fill_(0.0)
|
610 |
+
identity_conv_1x1.weight.data.fill_diagonal_(1.0)
|
611 |
+
identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.unsqueeze(2).unsqueeze(3)
|
612 |
+
# print(f" identity_conv_1x1.weight = {identity_conv_1x1.weight.shape}")
|
613 |
+
|
614 |
+
identity_conv_1x1 = self.fuse_conv_bn(identity_conv_1x1, self.rbr_identity)
|
615 |
+
bias_identity_expanded = identity_conv_1x1.bias
|
616 |
+
weight_identity_expanded = torch.nn.functional.pad(identity_conv_1x1.weight, [1, 1, 1, 1])
|
617 |
+
else:
|
618 |
+
# print(f"fuse: rbr_identity != BatchNorm2d, rbr_identity = {self.rbr_identity}")
|
619 |
+
bias_identity_expanded = torch.nn.Parameter( torch.zeros_like(rbr_1x1_bias) )
|
620 |
+
weight_identity_expanded = torch.nn.Parameter( torch.zeros_like(weight_1x1_expanded) )
|
621 |
+
|
622 |
+
|
623 |
+
#print(f"self.rbr_1x1.weight = {self.rbr_1x1.weight.shape}, ")
|
624 |
+
#print(f"weight_1x1_expanded = {weight_1x1_expanded.shape}, ")
|
625 |
+
#print(f"self.rbr_dense.weight = {self.rbr_dense.weight.shape}, ")
|
626 |
+
|
627 |
+
self.rbr_dense.weight = torch.nn.Parameter(self.rbr_dense.weight + weight_1x1_expanded + weight_identity_expanded)
|
628 |
+
self.rbr_dense.bias = torch.nn.Parameter(self.rbr_dense.bias + rbr_1x1_bias + bias_identity_expanded)
|
629 |
+
|
630 |
+
self.rbr_reparam = self.rbr_dense
|
631 |
+
self.deploy = True
|
632 |
+
|
633 |
+
if self.rbr_identity is not None:
|
634 |
+
del self.rbr_identity
|
635 |
+
self.rbr_identity = None
|
636 |
+
|
637 |
+
if self.rbr_1x1 is not None:
|
638 |
+
del self.rbr_1x1
|
639 |
+
self.rbr_1x1 = None
|
640 |
+
|
641 |
+
if self.rbr_dense is not None:
|
642 |
+
del self.rbr_dense
|
643 |
+
self.rbr_dense = None
|
644 |
+
|
645 |
+
|
646 |
+
class RepBottleneck(Bottleneck):
|
647 |
+
# Standard bottleneck
|
648 |
+
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
649 |
+
super().__init__(c1, c2, shortcut=True, g=1, e=0.5)
|
650 |
+
c_ = int(c2 * e) # hidden channels
|
651 |
+
self.cv2 = RepConv(c_, c2, 3, 1, g=g)
|
652 |
+
|
653 |
+
|
654 |
+
class RepBottleneckCSPA(BottleneckCSPA):
|
655 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
656 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
657 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
658 |
+
c_ = int(c2 * e) # hidden channels
|
659 |
+
self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
660 |
+
|
661 |
+
|
662 |
+
class RepBottleneckCSPB(BottleneckCSPB):
|
663 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
664 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
665 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
666 |
+
c_ = int(c2) # hidden channels
|
667 |
+
self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
668 |
+
|
669 |
+
|
670 |
+
class RepBottleneckCSPC(BottleneckCSPC):
|
671 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
672 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
673 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
674 |
+
c_ = int(c2 * e) # hidden channels
|
675 |
+
self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
676 |
+
|
677 |
+
|
678 |
+
class RepRes(Res):
|
679 |
+
# Standard bottleneck
|
680 |
+
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
681 |
+
super().__init__(c1, c2, shortcut, g, e)
|
682 |
+
c_ = int(c2 * e) # hidden channels
|
683 |
+
self.cv2 = RepConv(c_, c_, 3, 1, g=g)
|
684 |
+
|
685 |
+
|
686 |
+
class RepResCSPA(ResCSPA):
|
687 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
688 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
689 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
690 |
+
c_ = int(c2 * e) # hidden channels
|
691 |
+
self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
692 |
+
|
693 |
+
|
694 |
+
class RepResCSPB(ResCSPB):
|
695 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
696 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
697 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
698 |
+
c_ = int(c2) # hidden channels
|
699 |
+
self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
700 |
+
|
701 |
+
|
702 |
+
class RepResCSPC(ResCSPC):
|
703 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
704 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
705 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
706 |
+
c_ = int(c2 * e) # hidden channels
|
707 |
+
self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
708 |
+
|
709 |
+
|
710 |
+
class RepResX(ResX):
|
711 |
+
# Standard bottleneck
|
712 |
+
def __init__(self, c1, c2, shortcut=True, g=32, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
713 |
+
super().__init__(c1, c2, shortcut, g, e)
|
714 |
+
c_ = int(c2 * e) # hidden channels
|
715 |
+
self.cv2 = RepConv(c_, c_, 3, 1, g=g)
|
716 |
+
|
717 |
+
|
718 |
+
class RepResXCSPA(ResXCSPA):
|
719 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
720 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
721 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
722 |
+
c_ = int(c2 * e) # hidden channels
|
723 |
+
self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
724 |
+
|
725 |
+
|
726 |
+
class RepResXCSPB(ResXCSPB):
|
727 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
728 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
729 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
730 |
+
c_ = int(c2) # hidden channels
|
731 |
+
self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
732 |
+
|
733 |
+
|
734 |
+
class RepResXCSPC(ResXCSPC):
|
735 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
736 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
737 |
+
super().__init__(c1, c2, n, shortcut, g, e)
|
738 |
+
c_ = int(c2 * e) # hidden channels
|
739 |
+
self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)])
|
740 |
+
|
741 |
+
##### end of repvgg #####
|
742 |
+
|
743 |
+
|
744 |
+
##### transformer #####
|
745 |
+
|
746 |
+
class TransformerLayer(nn.Module):
|
747 |
+
# Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance)
|
748 |
+
def __init__(self, c, num_heads):
|
749 |
+
super().__init__()
|
750 |
+
self.q = nn.Linear(c, c, bias=False)
|
751 |
+
self.k = nn.Linear(c, c, bias=False)
|
752 |
+
self.v = nn.Linear(c, c, bias=False)
|
753 |
+
self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads)
|
754 |
+
self.fc1 = nn.Linear(c, c, bias=False)
|
755 |
+
self.fc2 = nn.Linear(c, c, bias=False)
|
756 |
+
|
757 |
+
def forward(self, x):
|
758 |
+
x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x
|
759 |
+
x = self.fc2(self.fc1(x)) + x
|
760 |
+
return x
|
761 |
+
|
762 |
+
|
763 |
+
class TransformerBlock(nn.Module):
|
764 |
+
# Vision Transformer https://arxiv.org/abs/2010.11929
|
765 |
+
def __init__(self, c1, c2, num_heads, num_layers):
|
766 |
+
super().__init__()
|
767 |
+
self.conv = None
|
768 |
+
if c1 != c2:
|
769 |
+
self.conv = Conv(c1, c2)
|
770 |
+
self.linear = nn.Linear(c2, c2) # learnable position embedding
|
771 |
+
self.tr = nn.Sequential(*[TransformerLayer(c2, num_heads) for _ in range(num_layers)])
|
772 |
+
self.c2 = c2
|
773 |
+
|
774 |
+
def forward(self, x):
|
775 |
+
if self.conv is not None:
|
776 |
+
x = self.conv(x)
|
777 |
+
b, _, w, h = x.shape
|
778 |
+
p = x.flatten(2)
|
779 |
+
p = p.unsqueeze(0)
|
780 |
+
p = p.transpose(0, 3)
|
781 |
+
p = p.squeeze(3)
|
782 |
+
e = self.linear(p)
|
783 |
+
x = p + e
|
784 |
+
|
785 |
+
x = self.tr(x)
|
786 |
+
x = x.unsqueeze(3)
|
787 |
+
x = x.transpose(0, 3)
|
788 |
+
x = x.reshape(b, self.c2, w, h)
|
789 |
+
return x
|
790 |
+
|
791 |
+
##### end of transformer #####
|
792 |
+
|
793 |
+
|
794 |
+
##### yolov5 #####
|
795 |
+
|
796 |
+
class Focus(nn.Module):
|
797 |
+
# Focus wh information into c-space
|
798 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
799 |
+
super(Focus, self).__init__()
|
800 |
+
self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
|
801 |
+
# self.contract = Contract(gain=2)
|
802 |
+
|
803 |
+
def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
|
804 |
+
return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
|
805 |
+
# return self.conv(self.contract(x))
|
806 |
+
|
807 |
+
|
808 |
+
class SPPF(nn.Module):
|
809 |
+
# Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
|
810 |
+
def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13))
|
811 |
+
super().__init__()
|
812 |
+
c_ = c1 // 2 # hidden channels
|
813 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
814 |
+
self.cv2 = Conv(c_ * 4, c2, 1, 1)
|
815 |
+
self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
|
816 |
+
|
817 |
+
def forward(self, x):
|
818 |
+
x = self.cv1(x)
|
819 |
+
y1 = self.m(x)
|
820 |
+
y2 = self.m(y1)
|
821 |
+
return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))
|
822 |
+
|
823 |
+
|
824 |
+
class Contract(nn.Module):
|
825 |
+
# Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40)
|
826 |
+
def __init__(self, gain=2):
|
827 |
+
super().__init__()
|
828 |
+
self.gain = gain
|
829 |
+
|
830 |
+
def forward(self, x):
|
831 |
+
N, C, H, W = x.size() # assert (H / s == 0) and (W / s == 0), 'Indivisible gain'
|
832 |
+
s = self.gain
|
833 |
+
x = x.view(N, C, H // s, s, W // s, s) # x(1,64,40,2,40,2)
|
834 |
+
x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40)
|
835 |
+
return x.view(N, C * s * s, H // s, W // s) # x(1,256,40,40)
|
836 |
+
|
837 |
+
|
838 |
+
class Expand(nn.Module):
|
839 |
+
# Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160)
|
840 |
+
def __init__(self, gain=2):
|
841 |
+
super().__init__()
|
842 |
+
self.gain = gain
|
843 |
+
|
844 |
+
def forward(self, x):
|
845 |
+
N, C, H, W = x.size() # assert C / s ** 2 == 0, 'Indivisible gain'
|
846 |
+
s = self.gain
|
847 |
+
x = x.view(N, s, s, C // s ** 2, H, W) # x(1,2,2,16,80,80)
|
848 |
+
x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2)
|
849 |
+
return x.view(N, C // s ** 2, H * s, W * s) # x(1,16,160,160)
|
850 |
+
|
851 |
+
|
852 |
+
class NMS(nn.Module):
|
853 |
+
# Non-Maximum Suppression (NMS) module
|
854 |
+
conf = 0.25 # confidence threshold
|
855 |
+
iou = 0.45 # IoU threshold
|
856 |
+
classes = None # (optional list) filter by class
|
857 |
+
|
858 |
+
def __init__(self):
|
859 |
+
super(NMS, self).__init__()
|
860 |
+
|
861 |
+
def forward(self, x):
|
862 |
+
return non_max_suppression(x[0], conf_thres=self.conf, iou_thres=self.iou, classes=self.classes)
|
863 |
+
|
864 |
+
|
865 |
+
class autoShape(nn.Module):
|
866 |
+
# input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS
|
867 |
+
conf = 0.25 # NMS confidence threshold
|
868 |
+
iou = 0.45 # NMS IoU threshold
|
869 |
+
classes = None # (optional list) filter by class
|
870 |
+
|
871 |
+
def __init__(self, model):
|
872 |
+
super(autoShape, self).__init__()
|
873 |
+
self.model = model.eval()
|
874 |
+
|
875 |
+
def autoshape(self):
|
876 |
+
print('autoShape already enabled, skipping... ') # model already converted to model.autoshape()
|
877 |
+
return self
|
878 |
+
|
879 |
+
@torch.no_grad()
|
880 |
+
def forward(self, imgs, size=640, augment=False, profile=False):
|
881 |
+
# Inference from various sources. For height=640, width=1280, RGB images example inputs are:
|
882 |
+
# filename: imgs = 'data/samples/zidane.jpg'
|
883 |
+
# URI: = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/zidane.jpg'
|
884 |
+
# OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3)
|
885 |
+
# PIL: = Image.open('image.jpg') # HWC x(640,1280,3)
|
886 |
+
# numpy: = np.zeros((640,1280,3)) # HWC
|
887 |
+
# torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values)
|
888 |
+
# multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images
|
889 |
+
|
890 |
+
t = [time_synchronized()]
|
891 |
+
p = next(self.model.parameters()) # for device and type
|
892 |
+
if isinstance(imgs, torch.Tensor): # torch
|
893 |
+
with amp.autocast(enabled=p.device.type != 'cpu'):
|
894 |
+
return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference
|
895 |
+
|
896 |
+
# Pre-process
|
897 |
+
n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images
|
898 |
+
shape0, shape1, files = [], [], [] # image and inference shapes, filenames
|
899 |
+
for i, im in enumerate(imgs):
|
900 |
+
f = f'image{i}' # filename
|
901 |
+
if isinstance(im, str): # filename or uri
|
902 |
+
im, f = np.asarray(Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im)), im
|
903 |
+
elif isinstance(im, Image.Image): # PIL Image
|
904 |
+
im, f = np.asarray(im), getattr(im, 'filename', f) or f
|
905 |
+
files.append(Path(f).with_suffix('.jpg').name)
|
906 |
+
if im.shape[0] < 5: # image in CHW
|
907 |
+
im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
|
908 |
+
im = im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3) # enforce 3ch input
|
909 |
+
s = im.shape[:2] # HWC
|
910 |
+
shape0.append(s) # image shape
|
911 |
+
g = (size / max(s)) # gain
|
912 |
+
shape1.append([y * g for y in s])
|
913 |
+
imgs[i] = im # update
|
914 |
+
shape1 = [make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)] # inference shape
|
915 |
+
x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs] # pad
|
916 |
+
x = np.stack(x, 0) if n > 1 else x[0][None] # stack
|
917 |
+
x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW
|
918 |
+
x = torch.from_numpy(x).to(p.device).type_as(p) / 255. # uint8 to fp16/32
|
919 |
+
t.append(time_synchronized())
|
920 |
+
|
921 |
+
with amp.autocast(enabled=p.device.type != 'cpu'):
|
922 |
+
# Inference
|
923 |
+
y = self.model(x, augment, profile)[0] # forward
|
924 |
+
t.append(time_synchronized())
|
925 |
+
|
926 |
+
# Post-process
|
927 |
+
y = non_max_suppression(y, conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) # NMS
|
928 |
+
for i in range(n):
|
929 |
+
scale_coords(shape1, y[i][:, :4], shape0[i])
|
930 |
+
|
931 |
+
t.append(time_synchronized())
|
932 |
+
return Detections(imgs, y, files, t, self.names, x.shape)
|
933 |
+
|
934 |
+
|
935 |
+
class Detections:
|
936 |
+
# detections class for YOLOv5 inference results
|
937 |
+
def __init__(self, imgs, pred, files, times=None, names=None, shape=None):
|
938 |
+
super(Detections, self).__init__()
|
939 |
+
d = pred[0].device # device
|
940 |
+
gn = [torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.], device=d) for im in imgs] # normalizations
|
941 |
+
self.imgs = imgs # list of images as numpy arrays
|
942 |
+
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
|
943 |
+
self.names = names # class names
|
944 |
+
self.files = files # image filenames
|
945 |
+
self.xyxy = pred # xyxy pixels
|
946 |
+
self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
|
947 |
+
self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized
|
948 |
+
self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized
|
949 |
+
self.n = len(self.pred) # number of images (batch size)
|
950 |
+
self.t = tuple((times[i + 1] - times[i]) * 1000 / self.n for i in range(3)) # timestamps (ms)
|
951 |
+
self.s = shape # inference BCHW shape
|
952 |
+
|
953 |
+
def display(self, pprint=False, show=False, save=False, render=False, save_dir=''):
|
954 |
+
colors = color_list()
|
955 |
+
for i, (img, pred) in enumerate(zip(self.imgs, self.pred)):
|
956 |
+
str = f'image {i + 1}/{len(self.pred)}: {img.shape[0]}x{img.shape[1]} '
|
957 |
+
if pred is not None:
|
958 |
+
for c in pred[:, -1].unique():
|
959 |
+
n = (pred[:, -1] == c).sum() # detections per class
|
960 |
+
str += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
|
961 |
+
if show or save or render:
|
962 |
+
for *box, conf, cls in pred: # xyxy, confidence, class
|
963 |
+
label = f'{self.names[int(cls)]} {conf:.2f}'
|
964 |
+
plot_one_box(box, img, label=label, color=colors[int(cls) % 10])
|
965 |
+
img = Image.fromarray(img.astype(np.uint8)) if isinstance(img, np.ndarray) else img # from np
|
966 |
+
if pprint:
|
967 |
+
print(str.rstrip(', '))
|
968 |
+
if show:
|
969 |
+
img.show(self.files[i]) # show
|
970 |
+
if save:
|
971 |
+
f = self.files[i]
|
972 |
+
img.save(Path(save_dir) / f) # save
|
973 |
+
print(f"{'Saved' * (i == 0)} {f}", end=',' if i < self.n - 1 else f' to {save_dir}\n')
|
974 |
+
if render:
|
975 |
+
self.imgs[i] = np.asarray(img)
|
976 |
+
|
977 |
+
def print(self):
|
978 |
+
self.display(pprint=True) # print results
|
979 |
+
print(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {tuple(self.s)}' % self.t)
|
980 |
+
|
981 |
+
def show(self):
|
982 |
+
self.display(show=True) # show results
|
983 |
+
|
984 |
+
def save(self, save_dir='runs/hub/exp'):
|
985 |
+
save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/hub/exp') # increment save_dir
|
986 |
+
Path(save_dir).mkdir(parents=True, exist_ok=True)
|
987 |
+
self.display(save=True, save_dir=save_dir) # save results
|
988 |
+
|
989 |
+
def render(self):
|
990 |
+
self.display(render=True) # render results
|
991 |
+
return self.imgs
|
992 |
+
|
993 |
+
def pandas(self):
|
994 |
+
# return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0])
|
995 |
+
new = copy(self) # return copy
|
996 |
+
ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns
|
997 |
+
cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns
|
998 |
+
for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]):
|
999 |
+
a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update
|
1000 |
+
setattr(new, k, [pd.DataFrame(x, columns=c) for x in a])
|
1001 |
+
return new
|
1002 |
+
|
1003 |
+
def tolist(self):
|
1004 |
+
# return a list of Detections objects, i.e. 'for result in results.tolist():'
|
1005 |
+
x = [Detections([self.imgs[i]], [self.pred[i]], self.names, self.s) for i in range(self.n)]
|
1006 |
+
for d in x:
|
1007 |
+
for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']:
|
1008 |
+
setattr(d, k, getattr(d, k)[0]) # pop out of list
|
1009 |
+
return x
|
1010 |
+
|
1011 |
+
def __len__(self):
|
1012 |
+
return self.n
|
1013 |
+
|
1014 |
+
|
1015 |
+
class Classify(nn.Module):
|
1016 |
+
# Classification head, i.e. x(b,c1,20,20) to x(b,c2)
|
1017 |
+
def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups
|
1018 |
+
super(Classify, self).__init__()
|
1019 |
+
self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1)
|
1020 |
+
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g) # to x(b,c2,1,1)
|
1021 |
+
self.flat = nn.Flatten()
|
1022 |
+
|
1023 |
+
def forward(self, x):
|
1024 |
+
z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list
|
1025 |
+
return self.flat(self.conv(z)) # flatten to x(b,c2)
|
1026 |
+
|
1027 |
+
##### end of yolov5 ######
|
1028 |
+
|
1029 |
+
|
1030 |
+
##### orepa #####
|
1031 |
+
|
1032 |
+
def transI_fusebn(kernel, bn):
|
1033 |
+
gamma = bn.weight
|
1034 |
+
std = (bn.running_var + bn.eps).sqrt()
|
1035 |
+
return kernel * ((gamma / std).reshape(-1, 1, 1, 1)), bn.bias - bn.running_mean * gamma / std
|
1036 |
+
|
1037 |
+
|
1038 |
+
class ConvBN(nn.Module):
|
1039 |
+
def __init__(self, in_channels, out_channels, kernel_size,
|
1040 |
+
stride=1, padding=0, dilation=1, groups=1, deploy=False, nonlinear=None):
|
1041 |
+
super().__init__()
|
1042 |
+
if nonlinear is None:
|
1043 |
+
self.nonlinear = nn.Identity()
|
1044 |
+
else:
|
1045 |
+
self.nonlinear = nonlinear
|
1046 |
+
if deploy:
|
1047 |
+
self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,
|
1048 |
+
stride=stride, padding=padding, dilation=dilation, groups=groups, bias=True)
|
1049 |
+
else:
|
1050 |
+
self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,
|
1051 |
+
stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False)
|
1052 |
+
self.bn = nn.BatchNorm2d(num_features=out_channels)
|
1053 |
+
|
1054 |
+
def forward(self, x):
|
1055 |
+
if hasattr(self, 'bn'):
|
1056 |
+
return self.nonlinear(self.bn(self.conv(x)))
|
1057 |
+
else:
|
1058 |
+
return self.nonlinear(self.conv(x))
|
1059 |
+
|
1060 |
+
def switch_to_deploy(self):
|
1061 |
+
kernel, bias = transI_fusebn(self.conv.weight, self.bn)
|
1062 |
+
conv = nn.Conv2d(in_channels=self.conv.in_channels, out_channels=self.conv.out_channels, kernel_size=self.conv.kernel_size,
|
1063 |
+
stride=self.conv.stride, padding=self.conv.padding, dilation=self.conv.dilation, groups=self.conv.groups, bias=True)
|
1064 |
+
conv.weight.data = kernel
|
1065 |
+
conv.bias.data = bias
|
1066 |
+
for para in self.parameters():
|
1067 |
+
para.detach_()
|
1068 |
+
self.__delattr__('conv')
|
1069 |
+
self.__delattr__('bn')
|
1070 |
+
self.conv = conv
|
1071 |
+
|
1072 |
+
class OREPA_3x3_RepConv(nn.Module):
|
1073 |
+
|
1074 |
+
def __init__(self, in_channels, out_channels, kernel_size,
|
1075 |
+
stride=1, padding=0, dilation=1, groups=1,
|
1076 |
+
internal_channels_1x1_3x3=None,
|
1077 |
+
deploy=False, nonlinear=None, single_init=False):
|
1078 |
+
super(OREPA_3x3_RepConv, self).__init__()
|
1079 |
+
self.deploy = deploy
|
1080 |
+
|
1081 |
+
if nonlinear is None:
|
1082 |
+
self.nonlinear = nn.Identity()
|
1083 |
+
else:
|
1084 |
+
self.nonlinear = nonlinear
|
1085 |
+
|
1086 |
+
self.kernel_size = kernel_size
|
1087 |
+
self.in_channels = in_channels
|
1088 |
+
self.out_channels = out_channels
|
1089 |
+
self.groups = groups
|
1090 |
+
assert padding == kernel_size // 2
|
1091 |
+
|
1092 |
+
self.stride = stride
|
1093 |
+
self.padding = padding
|
1094 |
+
self.dilation = dilation
|
1095 |
+
|
1096 |
+
self.branch_counter = 0
|
1097 |
+
|
1098 |
+
self.weight_rbr_origin = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), kernel_size, kernel_size))
|
1099 |
+
nn.init.kaiming_uniform_(self.weight_rbr_origin, a=math.sqrt(1.0))
|
1100 |
+
self.branch_counter += 1
|
1101 |
+
|
1102 |
+
|
1103 |
+
if groups < out_channels:
|
1104 |
+
self.weight_rbr_avg_conv = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), 1, 1))
|
1105 |
+
self.weight_rbr_pfir_conv = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), 1, 1))
|
1106 |
+
nn.init.kaiming_uniform_(self.weight_rbr_avg_conv, a=1.0)
|
1107 |
+
nn.init.kaiming_uniform_(self.weight_rbr_pfir_conv, a=1.0)
|
1108 |
+
self.weight_rbr_avg_conv.data
|
1109 |
+
self.weight_rbr_pfir_conv.data
|
1110 |
+
self.register_buffer('weight_rbr_avg_avg', torch.ones(kernel_size, kernel_size).mul(1.0/kernel_size/kernel_size))
|
1111 |
+
self.branch_counter += 1
|
1112 |
+
|
1113 |
+
else:
|
1114 |
+
raise NotImplementedError
|
1115 |
+
self.branch_counter += 1
|
1116 |
+
|
1117 |
+
if internal_channels_1x1_3x3 is None:
|
1118 |
+
internal_channels_1x1_3x3 = in_channels if groups < out_channels else 2 * in_channels # For mobilenet, it is better to have 2X internal channels
|
1119 |
+
|
1120 |
+
if internal_channels_1x1_3x3 == in_channels:
|
1121 |
+
self.weight_rbr_1x1_kxk_idconv1 = nn.Parameter(torch.zeros(in_channels, int(in_channels/self.groups), 1, 1))
|
1122 |
+
id_value = np.zeros((in_channels, int(in_channels/self.groups), 1, 1))
|
1123 |
+
for i in range(in_channels):
|
1124 |
+
id_value[i, i % int(in_channels/self.groups), 0, 0] = 1
|
1125 |
+
id_tensor = torch.from_numpy(id_value).type_as(self.weight_rbr_1x1_kxk_idconv1)
|
1126 |
+
self.register_buffer('id_tensor', id_tensor)
|
1127 |
+
|
1128 |
+
else:
|
1129 |
+
self.weight_rbr_1x1_kxk_conv1 = nn.Parameter(torch.Tensor(internal_channels_1x1_3x3, int(in_channels/self.groups), 1, 1))
|
1130 |
+
nn.init.kaiming_uniform_(self.weight_rbr_1x1_kxk_conv1, a=math.sqrt(1.0))
|
1131 |
+
self.weight_rbr_1x1_kxk_conv2 = nn.Parameter(torch.Tensor(out_channels, int(internal_channels_1x1_3x3/self.groups), kernel_size, kernel_size))
|
1132 |
+
nn.init.kaiming_uniform_(self.weight_rbr_1x1_kxk_conv2, a=math.sqrt(1.0))
|
1133 |
+
self.branch_counter += 1
|
1134 |
+
|
1135 |
+
expand_ratio = 8
|
1136 |
+
self.weight_rbr_gconv_dw = nn.Parameter(torch.Tensor(in_channels*expand_ratio, 1, kernel_size, kernel_size))
|
1137 |
+
self.weight_rbr_gconv_pw = nn.Parameter(torch.Tensor(out_channels, in_channels*expand_ratio, 1, 1))
|
1138 |
+
nn.init.kaiming_uniform_(self.weight_rbr_gconv_dw, a=math.sqrt(1.0))
|
1139 |
+
nn.init.kaiming_uniform_(self.weight_rbr_gconv_pw, a=math.sqrt(1.0))
|
1140 |
+
self.branch_counter += 1
|
1141 |
+
|
1142 |
+
if out_channels == in_channels and stride == 1:
|
1143 |
+
self.branch_counter += 1
|
1144 |
+
|
1145 |
+
self.vector = nn.Parameter(torch.Tensor(self.branch_counter, self.out_channels))
|
1146 |
+
self.bn = nn.BatchNorm2d(out_channels)
|
1147 |
+
|
1148 |
+
self.fre_init()
|
1149 |
+
|
1150 |
+
nn.init.constant_(self.vector[0, :], 0.25) #origin
|
1151 |
+
nn.init.constant_(self.vector[1, :], 0.25) #avg
|
1152 |
+
nn.init.constant_(self.vector[2, :], 0.0) #prior
|
1153 |
+
nn.init.constant_(self.vector[3, :], 0.5) #1x1_kxk
|
1154 |
+
nn.init.constant_(self.vector[4, :], 0.5) #dws_conv
|
1155 |
+
|
1156 |
+
|
1157 |
+
def fre_init(self):
|
1158 |
+
prior_tensor = torch.Tensor(self.out_channels, self.kernel_size, self.kernel_size)
|
1159 |
+
half_fg = self.out_channels/2
|
1160 |
+
for i in range(self.out_channels):
|
1161 |
+
for h in range(3):
|
1162 |
+
for w in range(3):
|
1163 |
+
if i < half_fg:
|
1164 |
+
prior_tensor[i, h, w] = math.cos(math.pi*(h+0.5)*(i+1)/3)
|
1165 |
+
else:
|
1166 |
+
prior_tensor[i, h, w] = math.cos(math.pi*(w+0.5)*(i+1-half_fg)/3)
|
1167 |
+
|
1168 |
+
self.register_buffer('weight_rbr_prior', prior_tensor)
|
1169 |
+
|
1170 |
+
def weight_gen(self):
|
1171 |
+
|
1172 |
+
weight_rbr_origin = torch.einsum('oihw,o->oihw', self.weight_rbr_origin, self.vector[0, :])
|
1173 |
+
|
1174 |
+
weight_rbr_avg = torch.einsum('oihw,o->oihw', torch.einsum('oihw,hw->oihw', self.weight_rbr_avg_conv, self.weight_rbr_avg_avg), self.vector[1, :])
|
1175 |
+
|
1176 |
+
weight_rbr_pfir = torch.einsum('oihw,o->oihw', torch.einsum('oihw,ohw->oihw', self.weight_rbr_pfir_conv, self.weight_rbr_prior), self.vector[2, :])
|
1177 |
+
|
1178 |
+
weight_rbr_1x1_kxk_conv1 = None
|
1179 |
+
if hasattr(self, 'weight_rbr_1x1_kxk_idconv1'):
|
1180 |
+
weight_rbr_1x1_kxk_conv1 = (self.weight_rbr_1x1_kxk_idconv1 + self.id_tensor).squeeze()
|
1181 |
+
elif hasattr(self, 'weight_rbr_1x1_kxk_conv1'):
|
1182 |
+
weight_rbr_1x1_kxk_conv1 = self.weight_rbr_1x1_kxk_conv1.squeeze()
|
1183 |
+
else:
|
1184 |
+
raise NotImplementedError
|
1185 |
+
weight_rbr_1x1_kxk_conv2 = self.weight_rbr_1x1_kxk_conv2
|
1186 |
+
|
1187 |
+
if self.groups > 1:
|
1188 |
+
g = self.groups
|
1189 |
+
t, ig = weight_rbr_1x1_kxk_conv1.size()
|
1190 |
+
o, tg, h, w = weight_rbr_1x1_kxk_conv2.size()
|
1191 |
+
weight_rbr_1x1_kxk_conv1 = weight_rbr_1x1_kxk_conv1.view(g, int(t/g), ig)
|
1192 |
+
weight_rbr_1x1_kxk_conv2 = weight_rbr_1x1_kxk_conv2.view(g, int(o/g), tg, h, w)
|
1193 |
+
weight_rbr_1x1_kxk = torch.einsum('gti,gothw->goihw', weight_rbr_1x1_kxk_conv1, weight_rbr_1x1_kxk_conv2).view(o, ig, h, w)
|
1194 |
+
else:
|
1195 |
+
weight_rbr_1x1_kxk = torch.einsum('ti,othw->oihw', weight_rbr_1x1_kxk_conv1, weight_rbr_1x1_kxk_conv2)
|
1196 |
+
|
1197 |
+
weight_rbr_1x1_kxk = torch.einsum('oihw,o->oihw', weight_rbr_1x1_kxk, self.vector[3, :])
|
1198 |
+
|
1199 |
+
weight_rbr_gconv = self.dwsc2full(self.weight_rbr_gconv_dw, self.weight_rbr_gconv_pw, self.in_channels)
|
1200 |
+
weight_rbr_gconv = torch.einsum('oihw,o->oihw', weight_rbr_gconv, self.vector[4, :])
|
1201 |
+
|
1202 |
+
weight = weight_rbr_origin + weight_rbr_avg + weight_rbr_1x1_kxk + weight_rbr_pfir + weight_rbr_gconv
|
1203 |
+
|
1204 |
+
return weight
|
1205 |
+
|
1206 |
+
def dwsc2full(self, weight_dw, weight_pw, groups):
|
1207 |
+
|
1208 |
+
t, ig, h, w = weight_dw.size()
|
1209 |
+
o, _, _, _ = weight_pw.size()
|
1210 |
+
tg = int(t/groups)
|
1211 |
+
i = int(ig*groups)
|
1212 |
+
weight_dw = weight_dw.view(groups, tg, ig, h, w)
|
1213 |
+
weight_pw = weight_pw.squeeze().view(o, groups, tg)
|
1214 |
+
|
1215 |
+
weight_dsc = torch.einsum('gtihw,ogt->ogihw', weight_dw, weight_pw)
|
1216 |
+
return weight_dsc.view(o, i, h, w)
|
1217 |
+
|
1218 |
+
def forward(self, inputs):
|
1219 |
+
weight = self.weight_gen()
|
1220 |
+
out = F.conv2d(inputs, weight, bias=None, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups)
|
1221 |
+
|
1222 |
+
return self.nonlinear(self.bn(out))
|
1223 |
+
|
1224 |
+
class RepConv_OREPA(nn.Module):
|
1225 |
+
|
1226 |
+
def __init__(self, c1, c2, k=3, s=1, padding=1, dilation=1, groups=1, padding_mode='zeros', deploy=False, use_se=False, nonlinear=nn.SiLU()):
|
1227 |
+
super(RepConv_OREPA, self).__init__()
|
1228 |
+
self.deploy = deploy
|
1229 |
+
self.groups = groups
|
1230 |
+
self.in_channels = c1
|
1231 |
+
self.out_channels = c2
|
1232 |
+
|
1233 |
+
self.padding = padding
|
1234 |
+
self.dilation = dilation
|
1235 |
+
self.groups = groups
|
1236 |
+
|
1237 |
+
assert k == 3
|
1238 |
+
assert padding == 1
|
1239 |
+
|
1240 |
+
padding_11 = padding - k // 2
|
1241 |
+
|
1242 |
+
if nonlinear is None:
|
1243 |
+
self.nonlinearity = nn.Identity()
|
1244 |
+
else:
|
1245 |
+
self.nonlinearity = nonlinear
|
1246 |
+
|
1247 |
+
if use_se:
|
1248 |
+
self.se = SEBlock(self.out_channels, internal_neurons=self.out_channels // 16)
|
1249 |
+
else:
|
1250 |
+
self.se = nn.Identity()
|
1251 |
+
|
1252 |
+
if deploy:
|
1253 |
+
self.rbr_reparam = nn.Conv2d(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=k, stride=s,
|
1254 |
+
padding=padding, dilation=dilation, groups=groups, bias=True, padding_mode=padding_mode)
|
1255 |
+
|
1256 |
+
else:
|
1257 |
+
self.rbr_identity = nn.BatchNorm2d(num_features=self.in_channels) if self.out_channels == self.in_channels and s == 1 else None
|
1258 |
+
self.rbr_dense = OREPA_3x3_RepConv(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=k, stride=s, padding=padding, groups=groups, dilation=1)
|
1259 |
+
self.rbr_1x1 = ConvBN(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=1, stride=s, padding=padding_11, groups=groups, dilation=1)
|
1260 |
+
print('RepVGG Block, identity = ', self.rbr_identity)
|
1261 |
+
|
1262 |
+
|
1263 |
+
def forward(self, inputs):
|
1264 |
+
if hasattr(self, 'rbr_reparam'):
|
1265 |
+
return self.nonlinearity(self.se(self.rbr_reparam(inputs)))
|
1266 |
+
|
1267 |
+
if self.rbr_identity is None:
|
1268 |
+
id_out = 0
|
1269 |
+
else:
|
1270 |
+
id_out = self.rbr_identity(inputs)
|
1271 |
+
|
1272 |
+
out1 = self.rbr_dense(inputs)
|
1273 |
+
out2 = self.rbr_1x1(inputs)
|
1274 |
+
out3 = id_out
|
1275 |
+
out = out1 + out2 + out3
|
1276 |
+
|
1277 |
+
return self.nonlinearity(self.se(out))
|
1278 |
+
|
1279 |
+
|
1280 |
+
# Optional. This improves the accuracy and facilitates quantization.
|
1281 |
+
# 1. Cancel the original weight decay on rbr_dense.conv.weight and rbr_1x1.conv.weight.
|
1282 |
+
# 2. Use like this.
|
1283 |
+
# loss = criterion(....)
|
1284 |
+
# for every RepVGGBlock blk:
|
1285 |
+
# loss += weight_decay_coefficient * 0.5 * blk.get_cust_L2()
|
1286 |
+
# optimizer.zero_grad()
|
1287 |
+
# loss.backward()
|
1288 |
+
|
1289 |
+
# Not used for OREPA
|
1290 |
+
def get_custom_L2(self):
|
1291 |
+
K3 = self.rbr_dense.weight_gen()
|
1292 |
+
K1 = self.rbr_1x1.conv.weight
|
1293 |
+
t3 = (self.rbr_dense.bn.weight / ((self.rbr_dense.bn.running_var + self.rbr_dense.bn.eps).sqrt())).reshape(-1, 1, 1, 1).detach()
|
1294 |
+
t1 = (self.rbr_1x1.bn.weight / ((self.rbr_1x1.bn.running_var + self.rbr_1x1.bn.eps).sqrt())).reshape(-1, 1, 1, 1).detach()
|
1295 |
+
|
1296 |
+
l2_loss_circle = (K3 ** 2).sum() - (K3[:, :, 1:2, 1:2] ** 2).sum() # The L2 loss of the "circle" of weights in 3x3 kernel. Use regular L2 on them.
|
1297 |
+
eq_kernel = K3[:, :, 1:2, 1:2] * t3 + K1 * t1 # The equivalent resultant central point of 3x3 kernel.
|
1298 |
+
l2_loss_eq_kernel = (eq_kernel ** 2 / (t3 ** 2 + t1 ** 2)).sum() # Normalize for an L2 coefficient comparable to regular L2.
|
1299 |
+
return l2_loss_eq_kernel + l2_loss_circle
|
1300 |
+
|
1301 |
+
def get_equivalent_kernel_bias(self):
|
1302 |
+
kernel3x3, bias3x3 = self._fuse_bn_tensor(self.rbr_dense)
|
1303 |
+
kernel1x1, bias1x1 = self._fuse_bn_tensor(self.rbr_1x1)
|
1304 |
+
kernelid, biasid = self._fuse_bn_tensor(self.rbr_identity)
|
1305 |
+
return kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid
|
1306 |
+
|
1307 |
+
def _pad_1x1_to_3x3_tensor(self, kernel1x1):
|
1308 |
+
if kernel1x1 is None:
|
1309 |
+
return 0
|
1310 |
+
else:
|
1311 |
+
return torch.nn.functional.pad(kernel1x1, [1,1,1,1])
|
1312 |
+
|
1313 |
+
def _fuse_bn_tensor(self, branch):
|
1314 |
+
if branch is None:
|
1315 |
+
return 0, 0
|
1316 |
+
if not isinstance(branch, nn.BatchNorm2d):
|
1317 |
+
if isinstance(branch, OREPA_3x3_RepConv):
|
1318 |
+
kernel = branch.weight_gen()
|
1319 |
+
elif isinstance(branch, ConvBN):
|
1320 |
+
kernel = branch.conv.weight
|
1321 |
+
else:
|
1322 |
+
raise NotImplementedError
|
1323 |
+
running_mean = branch.bn.running_mean
|
1324 |
+
running_var = branch.bn.running_var
|
1325 |
+
gamma = branch.bn.weight
|
1326 |
+
beta = branch.bn.bias
|
1327 |
+
eps = branch.bn.eps
|
1328 |
+
else:
|
1329 |
+
if not hasattr(self, 'id_tensor'):
|
1330 |
+
input_dim = self.in_channels // self.groups
|
1331 |
+
kernel_value = np.zeros((self.in_channels, input_dim, 3, 3), dtype=np.float32)
|
1332 |
+
for i in range(self.in_channels):
|
1333 |
+
kernel_value[i, i % input_dim, 1, 1] = 1
|
1334 |
+
self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device)
|
1335 |
+
kernel = self.id_tensor
|
1336 |
+
running_mean = branch.running_mean
|
1337 |
+
running_var = branch.running_var
|
1338 |
+
gamma = branch.weight
|
1339 |
+
beta = branch.bias
|
1340 |
+
eps = branch.eps
|
1341 |
+
std = (running_var + eps).sqrt()
|
1342 |
+
t = (gamma / std).reshape(-1, 1, 1, 1)
|
1343 |
+
return kernel * t, beta - running_mean * gamma / std
|
1344 |
+
|
1345 |
+
def switch_to_deploy(self):
|
1346 |
+
if hasattr(self, 'rbr_reparam'):
|
1347 |
+
return
|
1348 |
+
print(f"RepConv_OREPA.switch_to_deploy")
|
1349 |
+
kernel, bias = self.get_equivalent_kernel_bias()
|
1350 |
+
self.rbr_reparam = nn.Conv2d(in_channels=self.rbr_dense.in_channels, out_channels=self.rbr_dense.out_channels,
|
1351 |
+
kernel_size=self.rbr_dense.kernel_size, stride=self.rbr_dense.stride,
|
1352 |
+
padding=self.rbr_dense.padding, dilation=self.rbr_dense.dilation, groups=self.rbr_dense.groups, bias=True)
|
1353 |
+
self.rbr_reparam.weight.data = kernel
|
1354 |
+
self.rbr_reparam.bias.data = bias
|
1355 |
+
for para in self.parameters():
|
1356 |
+
para.detach_()
|
1357 |
+
self.__delattr__('rbr_dense')
|
1358 |
+
self.__delattr__('rbr_1x1')
|
1359 |
+
if hasattr(self, 'rbr_identity'):
|
1360 |
+
self.__delattr__('rbr_identity')
|
1361 |
+
|
1362 |
+
##### end of orepa #####
|
1363 |
+
|
1364 |
+
|
1365 |
+
##### swin transformer #####
|
1366 |
+
|
1367 |
+
class WindowAttention(nn.Module):
|
1368 |
+
|
1369 |
+
def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.):
|
1370 |
+
|
1371 |
+
super().__init__()
|
1372 |
+
self.dim = dim
|
1373 |
+
self.window_size = window_size # Wh, Ww
|
1374 |
+
self.num_heads = num_heads
|
1375 |
+
head_dim = dim // num_heads
|
1376 |
+
self.scale = qk_scale or head_dim ** -0.5
|
1377 |
+
|
1378 |
+
# define a parameter table of relative position bias
|
1379 |
+
self.relative_position_bias_table = nn.Parameter(
|
1380 |
+
torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads)) # 2*Wh-1 * 2*Ww-1, nH
|
1381 |
+
|
1382 |
+
# get pair-wise relative position index for each token inside the window
|
1383 |
+
coords_h = torch.arange(self.window_size[0])
|
1384 |
+
coords_w = torch.arange(self.window_size[1])
|
1385 |
+
coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
|
1386 |
+
coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
|
1387 |
+
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
|
1388 |
+
relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2
|
1389 |
+
relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0
|
1390 |
+
relative_coords[:, :, 1] += self.window_size[1] - 1
|
1391 |
+
relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
|
1392 |
+
relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww
|
1393 |
+
self.register_buffer("relative_position_index", relative_position_index)
|
1394 |
+
|
1395 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
1396 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
1397 |
+
self.proj = nn.Linear(dim, dim)
|
1398 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
1399 |
+
|
1400 |
+
nn.init.normal_(self.relative_position_bias_table, std=.02)
|
1401 |
+
self.softmax = nn.Softmax(dim=-1)
|
1402 |
+
|
1403 |
+
def forward(self, x, mask=None):
|
1404 |
+
|
1405 |
+
B_, N, C = x.shape
|
1406 |
+
qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
|
1407 |
+
q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
|
1408 |
+
|
1409 |
+
q = q * self.scale
|
1410 |
+
attn = (q @ k.transpose(-2, -1))
|
1411 |
+
|
1412 |
+
relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view(
|
1413 |
+
self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH
|
1414 |
+
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
1415 |
+
attn = attn + relative_position_bias.unsqueeze(0)
|
1416 |
+
|
1417 |
+
if mask is not None:
|
1418 |
+
nW = mask.shape[0]
|
1419 |
+
attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0)
|
1420 |
+
attn = attn.view(-1, self.num_heads, N, N)
|
1421 |
+
attn = self.softmax(attn)
|
1422 |
+
else:
|
1423 |
+
attn = self.softmax(attn)
|
1424 |
+
|
1425 |
+
attn = self.attn_drop(attn)
|
1426 |
+
|
1427 |
+
# print(attn.dtype, v.dtype)
|
1428 |
+
try:
|
1429 |
+
x = (attn @ v).transpose(1, 2).reshape(B_, N, C)
|
1430 |
+
except:
|
1431 |
+
#print(attn.dtype, v.dtype)
|
1432 |
+
x = (attn.half() @ v).transpose(1, 2).reshape(B_, N, C)
|
1433 |
+
x = self.proj(x)
|
1434 |
+
x = self.proj_drop(x)
|
1435 |
+
return x
|
1436 |
+
|
1437 |
+
class Mlp(nn.Module):
|
1438 |
+
|
1439 |
+
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.):
|
1440 |
+
super().__init__()
|
1441 |
+
out_features = out_features or in_features
|
1442 |
+
hidden_features = hidden_features or in_features
|
1443 |
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
1444 |
+
self.act = act_layer()
|
1445 |
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
1446 |
+
self.drop = nn.Dropout(drop)
|
1447 |
+
|
1448 |
+
def forward(self, x):
|
1449 |
+
x = self.fc1(x)
|
1450 |
+
x = self.act(x)
|
1451 |
+
x = self.drop(x)
|
1452 |
+
x = self.fc2(x)
|
1453 |
+
x = self.drop(x)
|
1454 |
+
return x
|
1455 |
+
|
1456 |
+
def window_partition(x, window_size):
|
1457 |
+
|
1458 |
+
B, H, W, C = x.shape
|
1459 |
+
assert H % window_size == 0, 'feature map h and w can not divide by window size'
|
1460 |
+
x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
|
1461 |
+
windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
|
1462 |
+
return windows
|
1463 |
+
|
1464 |
+
def window_reverse(windows, window_size, H, W):
|
1465 |
+
|
1466 |
+
B = int(windows.shape[0] / (H * W / window_size / window_size))
|
1467 |
+
x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
|
1468 |
+
x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
|
1469 |
+
return x
|
1470 |
+
|
1471 |
+
|
1472 |
+
class SwinTransformerLayer(nn.Module):
|
1473 |
+
|
1474 |
+
def __init__(self, dim, num_heads, window_size=8, shift_size=0,
|
1475 |
+
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0.,
|
1476 |
+
act_layer=nn.SiLU, norm_layer=nn.LayerNorm):
|
1477 |
+
super().__init__()
|
1478 |
+
self.dim = dim
|
1479 |
+
self.num_heads = num_heads
|
1480 |
+
self.window_size = window_size
|
1481 |
+
self.shift_size = shift_size
|
1482 |
+
self.mlp_ratio = mlp_ratio
|
1483 |
+
# if min(self.input_resolution) <= self.window_size:
|
1484 |
+
# # if window size is larger than input resolution, we don't partition windows
|
1485 |
+
# self.shift_size = 0
|
1486 |
+
# self.window_size = min(self.input_resolution)
|
1487 |
+
assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size"
|
1488 |
+
|
1489 |
+
self.norm1 = norm_layer(dim)
|
1490 |
+
self.attn = WindowAttention(
|
1491 |
+
dim, window_size=(self.window_size, self.window_size), num_heads=num_heads,
|
1492 |
+
qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
|
1493 |
+
|
1494 |
+
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
1495 |
+
self.norm2 = norm_layer(dim)
|
1496 |
+
mlp_hidden_dim = int(dim * mlp_ratio)
|
1497 |
+
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
|
1498 |
+
|
1499 |
+
def create_mask(self, H, W):
|
1500 |
+
# calculate attention mask for SW-MSA
|
1501 |
+
img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1
|
1502 |
+
h_slices = (slice(0, -self.window_size),
|
1503 |
+
slice(-self.window_size, -self.shift_size),
|
1504 |
+
slice(-self.shift_size, None))
|
1505 |
+
w_slices = (slice(0, -self.window_size),
|
1506 |
+
slice(-self.window_size, -self.shift_size),
|
1507 |
+
slice(-self.shift_size, None))
|
1508 |
+
cnt = 0
|
1509 |
+
for h in h_slices:
|
1510 |
+
for w in w_slices:
|
1511 |
+
img_mask[:, h, w, :] = cnt
|
1512 |
+
cnt += 1
|
1513 |
+
|
1514 |
+
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
|
1515 |
+
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
|
1516 |
+
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
|
1517 |
+
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
|
1518 |
+
|
1519 |
+
return attn_mask
|
1520 |
+
|
1521 |
+
def forward(self, x):
|
1522 |
+
# reshape x[b c h w] to x[b l c]
|
1523 |
+
_, _, H_, W_ = x.shape
|
1524 |
+
|
1525 |
+
Padding = False
|
1526 |
+
if min(H_, W_) < self.window_size or H_ % self.window_size!=0 or W_ % self.window_size!=0:
|
1527 |
+
Padding = True
|
1528 |
+
# print(f'img_size {min(H_, W_)} is less than (or not divided by) window_size {self.window_size}, Padding.')
|
1529 |
+
pad_r = (self.window_size - W_ % self.window_size) % self.window_size
|
1530 |
+
pad_b = (self.window_size - H_ % self.window_size) % self.window_size
|
1531 |
+
x = F.pad(x, (0, pad_r, 0, pad_b))
|
1532 |
+
|
1533 |
+
# print('2', x.shape)
|
1534 |
+
B, C, H, W = x.shape
|
1535 |
+
L = H * W
|
1536 |
+
x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) # b, L, c
|
1537 |
+
|
1538 |
+
# create mask from init to forward
|
1539 |
+
if self.shift_size > 0:
|
1540 |
+
attn_mask = self.create_mask(H, W).to(x.device)
|
1541 |
+
else:
|
1542 |
+
attn_mask = None
|
1543 |
+
|
1544 |
+
shortcut = x
|
1545 |
+
x = self.norm1(x)
|
1546 |
+
x = x.view(B, H, W, C)
|
1547 |
+
|
1548 |
+
# cyclic shift
|
1549 |
+
if self.shift_size > 0:
|
1550 |
+
shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2))
|
1551 |
+
else:
|
1552 |
+
shifted_x = x
|
1553 |
+
|
1554 |
+
# partition windows
|
1555 |
+
x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C
|
1556 |
+
x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C
|
1557 |
+
|
1558 |
+
# W-MSA/SW-MSA
|
1559 |
+
attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C
|
1560 |
+
|
1561 |
+
# merge windows
|
1562 |
+
attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C)
|
1563 |
+
shifted_x = window_reverse(attn_windows, self.window_size, H, W) # B H' W' C
|
1564 |
+
|
1565 |
+
# reverse cyclic shift
|
1566 |
+
if self.shift_size > 0:
|
1567 |
+
x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2))
|
1568 |
+
else:
|
1569 |
+
x = shifted_x
|
1570 |
+
x = x.view(B, H * W, C)
|
1571 |
+
|
1572 |
+
# FFN
|
1573 |
+
x = shortcut + self.drop_path(x)
|
1574 |
+
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
1575 |
+
|
1576 |
+
x = x.permute(0, 2, 1).contiguous().view(-1, C, H, W) # b c h w
|
1577 |
+
|
1578 |
+
if Padding:
|
1579 |
+
x = x[:, :, :H_, :W_] # reverse padding
|
1580 |
+
|
1581 |
+
return x
|
1582 |
+
|
1583 |
+
|
1584 |
+
class SwinTransformerBlock(nn.Module):
|
1585 |
+
def __init__(self, c1, c2, num_heads, num_layers, window_size=8):
|
1586 |
+
super().__init__()
|
1587 |
+
self.conv = None
|
1588 |
+
if c1 != c2:
|
1589 |
+
self.conv = Conv(c1, c2)
|
1590 |
+
|
1591 |
+
# remove input_resolution
|
1592 |
+
self.blocks = nn.Sequential(*[SwinTransformerLayer(dim=c2, num_heads=num_heads, window_size=window_size,
|
1593 |
+
shift_size=0 if (i % 2 == 0) else window_size // 2) for i in range(num_layers)])
|
1594 |
+
|
1595 |
+
def forward(self, x):
|
1596 |
+
if self.conv is not None:
|
1597 |
+
x = self.conv(x)
|
1598 |
+
x = self.blocks(x)
|
1599 |
+
return x
|
1600 |
+
|
1601 |
+
|
1602 |
+
class STCSPA(nn.Module):
|
1603 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
1604 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
1605 |
+
super(STCSPA, self).__init__()
|
1606 |
+
c_ = int(c2 * e) # hidden channels
|
1607 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
1608 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
1609 |
+
self.cv3 = Conv(2 * c_, c2, 1, 1)
|
1610 |
+
num_heads = c_ // 32
|
1611 |
+
self.m = SwinTransformerBlock(c_, c_, num_heads, n)
|
1612 |
+
#self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
1613 |
+
|
1614 |
+
def forward(self, x):
|
1615 |
+
y1 = self.m(self.cv1(x))
|
1616 |
+
y2 = self.cv2(x)
|
1617 |
+
return self.cv3(torch.cat((y1, y2), dim=1))
|
1618 |
+
|
1619 |
+
|
1620 |
+
class STCSPB(nn.Module):
|
1621 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
1622 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
1623 |
+
super(STCSPB, self).__init__()
|
1624 |
+
c_ = int(c2) # hidden channels
|
1625 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
1626 |
+
self.cv2 = Conv(c_, c_, 1, 1)
|
1627 |
+
self.cv3 = Conv(2 * c_, c2, 1, 1)
|
1628 |
+
num_heads = c_ // 32
|
1629 |
+
self.m = SwinTransformerBlock(c_, c_, num_heads, n)
|
1630 |
+
#self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
1631 |
+
|
1632 |
+
def forward(self, x):
|
1633 |
+
x1 = self.cv1(x)
|
1634 |
+
y1 = self.m(x1)
|
1635 |
+
y2 = self.cv2(x1)
|
1636 |
+
return self.cv3(torch.cat((y1, y2), dim=1))
|
1637 |
+
|
1638 |
+
|
1639 |
+
class STCSPC(nn.Module):
|
1640 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
1641 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
1642 |
+
super(STCSPC, self).__init__()
|
1643 |
+
c_ = int(c2 * e) # hidden channels
|
1644 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
1645 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
1646 |
+
self.cv3 = Conv(c_, c_, 1, 1)
|
1647 |
+
self.cv4 = Conv(2 * c_, c2, 1, 1)
|
1648 |
+
num_heads = c_ // 32
|
1649 |
+
self.m = SwinTransformerBlock(c_, c_, num_heads, n)
|
1650 |
+
#self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
1651 |
+
|
1652 |
+
def forward(self, x):
|
1653 |
+
y1 = self.cv3(self.m(self.cv1(x)))
|
1654 |
+
y2 = self.cv2(x)
|
1655 |
+
return self.cv4(torch.cat((y1, y2), dim=1))
|
1656 |
+
|
1657 |
+
##### end of swin transformer #####
|
1658 |
+
|
1659 |
+
|
1660 |
+
##### swin transformer v2 #####
|
1661 |
+
|
1662 |
+
class WindowAttention_v2(nn.Module):
|
1663 |
+
|
1664 |
+
def __init__(self, dim, window_size, num_heads, qkv_bias=True, attn_drop=0., proj_drop=0.,
|
1665 |
+
pretrained_window_size=[0, 0]):
|
1666 |
+
|
1667 |
+
super().__init__()
|
1668 |
+
self.dim = dim
|
1669 |
+
self.window_size = window_size # Wh, Ww
|
1670 |
+
self.pretrained_window_size = pretrained_window_size
|
1671 |
+
self.num_heads = num_heads
|
1672 |
+
|
1673 |
+
self.logit_scale = nn.Parameter(torch.log(10 * torch.ones((num_heads, 1, 1))), requires_grad=True)
|
1674 |
+
|
1675 |
+
# mlp to generate continuous relative position bias
|
1676 |
+
self.cpb_mlp = nn.Sequential(nn.Linear(2, 512, bias=True),
|
1677 |
+
nn.ReLU(inplace=True),
|
1678 |
+
nn.Linear(512, num_heads, bias=False))
|
1679 |
+
|
1680 |
+
# get relative_coords_table
|
1681 |
+
relative_coords_h = torch.arange(-(self.window_size[0] - 1), self.window_size[0], dtype=torch.float32)
|
1682 |
+
relative_coords_w = torch.arange(-(self.window_size[1] - 1), self.window_size[1], dtype=torch.float32)
|
1683 |
+
relative_coords_table = torch.stack(
|
1684 |
+
torch.meshgrid([relative_coords_h,
|
1685 |
+
relative_coords_w])).permute(1, 2, 0).contiguous().unsqueeze(0) # 1, 2*Wh-1, 2*Ww-1, 2
|
1686 |
+
if pretrained_window_size[0] > 0:
|
1687 |
+
relative_coords_table[:, :, :, 0] /= (pretrained_window_size[0] - 1)
|
1688 |
+
relative_coords_table[:, :, :, 1] /= (pretrained_window_size[1] - 1)
|
1689 |
+
else:
|
1690 |
+
relative_coords_table[:, :, :, 0] /= (self.window_size[0] - 1)
|
1691 |
+
relative_coords_table[:, :, :, 1] /= (self.window_size[1] - 1)
|
1692 |
+
relative_coords_table *= 8 # normalize to -8, 8
|
1693 |
+
relative_coords_table = torch.sign(relative_coords_table) * torch.log2(
|
1694 |
+
torch.abs(relative_coords_table) + 1.0) / np.log2(8)
|
1695 |
+
|
1696 |
+
self.register_buffer("relative_coords_table", relative_coords_table)
|
1697 |
+
|
1698 |
+
# get pair-wise relative position index for each token inside the window
|
1699 |
+
coords_h = torch.arange(self.window_size[0])
|
1700 |
+
coords_w = torch.arange(self.window_size[1])
|
1701 |
+
coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
|
1702 |
+
coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
|
1703 |
+
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
|
1704 |
+
relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2
|
1705 |
+
relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0
|
1706 |
+
relative_coords[:, :, 1] += self.window_size[1] - 1
|
1707 |
+
relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
|
1708 |
+
relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww
|
1709 |
+
self.register_buffer("relative_position_index", relative_position_index)
|
1710 |
+
|
1711 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=False)
|
1712 |
+
if qkv_bias:
|
1713 |
+
self.q_bias = nn.Parameter(torch.zeros(dim))
|
1714 |
+
self.v_bias = nn.Parameter(torch.zeros(dim))
|
1715 |
+
else:
|
1716 |
+
self.q_bias = None
|
1717 |
+
self.v_bias = None
|
1718 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
1719 |
+
self.proj = nn.Linear(dim, dim)
|
1720 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
1721 |
+
self.softmax = nn.Softmax(dim=-1)
|
1722 |
+
|
1723 |
+
def forward(self, x, mask=None):
|
1724 |
+
|
1725 |
+
B_, N, C = x.shape
|
1726 |
+
qkv_bias = None
|
1727 |
+
if self.q_bias is not None:
|
1728 |
+
qkv_bias = torch.cat((self.q_bias, torch.zeros_like(self.v_bias, requires_grad=False), self.v_bias))
|
1729 |
+
qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias)
|
1730 |
+
qkv = qkv.reshape(B_, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4)
|
1731 |
+
q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
|
1732 |
+
|
1733 |
+
# cosine attention
|
1734 |
+
attn = (F.normalize(q, dim=-1) @ F.normalize(k, dim=-1).transpose(-2, -1))
|
1735 |
+
logit_scale = torch.clamp(self.logit_scale, max=torch.log(torch.tensor(1. / 0.01))).exp()
|
1736 |
+
attn = attn * logit_scale
|
1737 |
+
|
1738 |
+
relative_position_bias_table = self.cpb_mlp(self.relative_coords_table).view(-1, self.num_heads)
|
1739 |
+
relative_position_bias = relative_position_bias_table[self.relative_position_index.view(-1)].view(
|
1740 |
+
self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH
|
1741 |
+
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
1742 |
+
relative_position_bias = 16 * torch.sigmoid(relative_position_bias)
|
1743 |
+
attn = attn + relative_position_bias.unsqueeze(0)
|
1744 |
+
|
1745 |
+
if mask is not None:
|
1746 |
+
nW = mask.shape[0]
|
1747 |
+
attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0)
|
1748 |
+
attn = attn.view(-1, self.num_heads, N, N)
|
1749 |
+
attn = self.softmax(attn)
|
1750 |
+
else:
|
1751 |
+
attn = self.softmax(attn)
|
1752 |
+
|
1753 |
+
attn = self.attn_drop(attn)
|
1754 |
+
|
1755 |
+
try:
|
1756 |
+
x = (attn @ v).transpose(1, 2).reshape(B_, N, C)
|
1757 |
+
except:
|
1758 |
+
x = (attn.half() @ v).transpose(1, 2).reshape(B_, N, C)
|
1759 |
+
|
1760 |
+
x = self.proj(x)
|
1761 |
+
x = self.proj_drop(x)
|
1762 |
+
return x
|
1763 |
+
|
1764 |
+
def extra_repr(self) -> str:
|
1765 |
+
return f'dim={self.dim}, window_size={self.window_size}, ' \
|
1766 |
+
f'pretrained_window_size={self.pretrained_window_size}, num_heads={self.num_heads}'
|
1767 |
+
|
1768 |
+
def flops(self, N):
|
1769 |
+
# calculate flops for 1 window with token length of N
|
1770 |
+
flops = 0
|
1771 |
+
# qkv = self.qkv(x)
|
1772 |
+
flops += N * self.dim * 3 * self.dim
|
1773 |
+
# attn = (q @ k.transpose(-2, -1))
|
1774 |
+
flops += self.num_heads * N * (self.dim // self.num_heads) * N
|
1775 |
+
# x = (attn @ v)
|
1776 |
+
flops += self.num_heads * N * N * (self.dim // self.num_heads)
|
1777 |
+
# x = self.proj(x)
|
1778 |
+
flops += N * self.dim * self.dim
|
1779 |
+
return flops
|
1780 |
+
|
1781 |
+
class Mlp_v2(nn.Module):
|
1782 |
+
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.):
|
1783 |
+
super().__init__()
|
1784 |
+
out_features = out_features or in_features
|
1785 |
+
hidden_features = hidden_features or in_features
|
1786 |
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
1787 |
+
self.act = act_layer()
|
1788 |
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
1789 |
+
self.drop = nn.Dropout(drop)
|
1790 |
+
|
1791 |
+
def forward(self, x):
|
1792 |
+
x = self.fc1(x)
|
1793 |
+
x = self.act(x)
|
1794 |
+
x = self.drop(x)
|
1795 |
+
x = self.fc2(x)
|
1796 |
+
x = self.drop(x)
|
1797 |
+
return x
|
1798 |
+
|
1799 |
+
|
1800 |
+
def window_partition_v2(x, window_size):
|
1801 |
+
|
1802 |
+
B, H, W, C = x.shape
|
1803 |
+
x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
|
1804 |
+
windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
|
1805 |
+
return windows
|
1806 |
+
|
1807 |
+
|
1808 |
+
def window_reverse_v2(windows, window_size, H, W):
|
1809 |
+
|
1810 |
+
B = int(windows.shape[0] / (H * W / window_size / window_size))
|
1811 |
+
x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
|
1812 |
+
x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
|
1813 |
+
return x
|
1814 |
+
|
1815 |
+
|
1816 |
+
class SwinTransformerLayer_v2(nn.Module):
|
1817 |
+
|
1818 |
+
def __init__(self, dim, num_heads, window_size=7, shift_size=0,
|
1819 |
+
mlp_ratio=4., qkv_bias=True, drop=0., attn_drop=0., drop_path=0.,
|
1820 |
+
act_layer=nn.SiLU, norm_layer=nn.LayerNorm, pretrained_window_size=0):
|
1821 |
+
super().__init__()
|
1822 |
+
self.dim = dim
|
1823 |
+
#self.input_resolution = input_resolution
|
1824 |
+
self.num_heads = num_heads
|
1825 |
+
self.window_size = window_size
|
1826 |
+
self.shift_size = shift_size
|
1827 |
+
self.mlp_ratio = mlp_ratio
|
1828 |
+
#if min(self.input_resolution) <= self.window_size:
|
1829 |
+
# # if window size is larger than input resolution, we don't partition windows
|
1830 |
+
# self.shift_size = 0
|
1831 |
+
# self.window_size = min(self.input_resolution)
|
1832 |
+
assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size"
|
1833 |
+
|
1834 |
+
self.norm1 = norm_layer(dim)
|
1835 |
+
self.attn = WindowAttention_v2(
|
1836 |
+
dim, window_size=(self.window_size, self.window_size), num_heads=num_heads,
|
1837 |
+
qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=drop,
|
1838 |
+
pretrained_window_size=(pretrained_window_size, pretrained_window_size))
|
1839 |
+
|
1840 |
+
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
1841 |
+
self.norm2 = norm_layer(dim)
|
1842 |
+
mlp_hidden_dim = int(dim * mlp_ratio)
|
1843 |
+
self.mlp = Mlp_v2(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
|
1844 |
+
|
1845 |
+
def create_mask(self, H, W):
|
1846 |
+
# calculate attention mask for SW-MSA
|
1847 |
+
img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1
|
1848 |
+
h_slices = (slice(0, -self.window_size),
|
1849 |
+
slice(-self.window_size, -self.shift_size),
|
1850 |
+
slice(-self.shift_size, None))
|
1851 |
+
w_slices = (slice(0, -self.window_size),
|
1852 |
+
slice(-self.window_size, -self.shift_size),
|
1853 |
+
slice(-self.shift_size, None))
|
1854 |
+
cnt = 0
|
1855 |
+
for h in h_slices:
|
1856 |
+
for w in w_slices:
|
1857 |
+
img_mask[:, h, w, :] = cnt
|
1858 |
+
cnt += 1
|
1859 |
+
|
1860 |
+
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
|
1861 |
+
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
|
1862 |
+
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
|
1863 |
+
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
|
1864 |
+
|
1865 |
+
return attn_mask
|
1866 |
+
|
1867 |
+
def forward(self, x):
|
1868 |
+
# reshape x[b c h w] to x[b l c]
|
1869 |
+
_, _, H_, W_ = x.shape
|
1870 |
+
|
1871 |
+
Padding = False
|
1872 |
+
if min(H_, W_) < self.window_size or H_ % self.window_size!=0 or W_ % self.window_size!=0:
|
1873 |
+
Padding = True
|
1874 |
+
# print(f'img_size {min(H_, W_)} is less than (or not divided by) window_size {self.window_size}, Padding.')
|
1875 |
+
pad_r = (self.window_size - W_ % self.window_size) % self.window_size
|
1876 |
+
pad_b = (self.window_size - H_ % self.window_size) % self.window_size
|
1877 |
+
x = F.pad(x, (0, pad_r, 0, pad_b))
|
1878 |
+
|
1879 |
+
# print('2', x.shape)
|
1880 |
+
B, C, H, W = x.shape
|
1881 |
+
L = H * W
|
1882 |
+
x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) # b, L, c
|
1883 |
+
|
1884 |
+
# create mask from init to forward
|
1885 |
+
if self.shift_size > 0:
|
1886 |
+
attn_mask = self.create_mask(H, W).to(x.device)
|
1887 |
+
else:
|
1888 |
+
attn_mask = None
|
1889 |
+
|
1890 |
+
shortcut = x
|
1891 |
+
x = x.view(B, H, W, C)
|
1892 |
+
|
1893 |
+
# cyclic shift
|
1894 |
+
if self.shift_size > 0:
|
1895 |
+
shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2))
|
1896 |
+
else:
|
1897 |
+
shifted_x = x
|
1898 |
+
|
1899 |
+
# partition windows
|
1900 |
+
x_windows = window_partition_v2(shifted_x, self.window_size) # nW*B, window_size, window_size, C
|
1901 |
+
x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C
|
1902 |
+
|
1903 |
+
# W-MSA/SW-MSA
|
1904 |
+
attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C
|
1905 |
+
|
1906 |
+
# merge windows
|
1907 |
+
attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C)
|
1908 |
+
shifted_x = window_reverse_v2(attn_windows, self.window_size, H, W) # B H' W' C
|
1909 |
+
|
1910 |
+
# reverse cyclic shift
|
1911 |
+
if self.shift_size > 0:
|
1912 |
+
x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2))
|
1913 |
+
else:
|
1914 |
+
x = shifted_x
|
1915 |
+
x = x.view(B, H * W, C)
|
1916 |
+
x = shortcut + self.drop_path(self.norm1(x))
|
1917 |
+
|
1918 |
+
# FFN
|
1919 |
+
x = x + self.drop_path(self.norm2(self.mlp(x)))
|
1920 |
+
x = x.permute(0, 2, 1).contiguous().view(-1, C, H, W) # b c h w
|
1921 |
+
|
1922 |
+
if Padding:
|
1923 |
+
x = x[:, :, :H_, :W_] # reverse padding
|
1924 |
+
|
1925 |
+
return x
|
1926 |
+
|
1927 |
+
def extra_repr(self) -> str:
|
1928 |
+
return f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " \
|
1929 |
+
f"window_size={self.window_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}"
|
1930 |
+
|
1931 |
+
def flops(self):
|
1932 |
+
flops = 0
|
1933 |
+
H, W = self.input_resolution
|
1934 |
+
# norm1
|
1935 |
+
flops += self.dim * H * W
|
1936 |
+
# W-MSA/SW-MSA
|
1937 |
+
nW = H * W / self.window_size / self.window_size
|
1938 |
+
flops += nW * self.attn.flops(self.window_size * self.window_size)
|
1939 |
+
# mlp
|
1940 |
+
flops += 2 * H * W * self.dim * self.dim * self.mlp_ratio
|
1941 |
+
# norm2
|
1942 |
+
flops += self.dim * H * W
|
1943 |
+
return flops
|
1944 |
+
|
1945 |
+
|
1946 |
+
class SwinTransformer2Block(nn.Module):
|
1947 |
+
def __init__(self, c1, c2, num_heads, num_layers, window_size=7):
|
1948 |
+
super().__init__()
|
1949 |
+
self.conv = None
|
1950 |
+
if c1 != c2:
|
1951 |
+
self.conv = Conv(c1, c2)
|
1952 |
+
|
1953 |
+
# remove input_resolution
|
1954 |
+
self.blocks = nn.Sequential(*[SwinTransformerLayer_v2(dim=c2, num_heads=num_heads, window_size=window_size,
|
1955 |
+
shift_size=0 if (i % 2 == 0) else window_size // 2) for i in range(num_layers)])
|
1956 |
+
|
1957 |
+
def forward(self, x):
|
1958 |
+
if self.conv is not None:
|
1959 |
+
x = self.conv(x)
|
1960 |
+
x = self.blocks(x)
|
1961 |
+
return x
|
1962 |
+
|
1963 |
+
|
1964 |
+
class ST2CSPA(nn.Module):
|
1965 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
1966 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
1967 |
+
super(ST2CSPA, self).__init__()
|
1968 |
+
c_ = int(c2 * e) # hidden channels
|
1969 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
1970 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
1971 |
+
self.cv3 = Conv(2 * c_, c2, 1, 1)
|
1972 |
+
num_heads = c_ // 32
|
1973 |
+
self.m = SwinTransformer2Block(c_, c_, num_heads, n)
|
1974 |
+
#self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
1975 |
+
|
1976 |
+
def forward(self, x):
|
1977 |
+
y1 = self.m(self.cv1(x))
|
1978 |
+
y2 = self.cv2(x)
|
1979 |
+
return self.cv3(torch.cat((y1, y2), dim=1))
|
1980 |
+
|
1981 |
+
|
1982 |
+
class ST2CSPB(nn.Module):
|
1983 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
1984 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
1985 |
+
super(ST2CSPB, self).__init__()
|
1986 |
+
c_ = int(c2) # hidden channels
|
1987 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
1988 |
+
self.cv2 = Conv(c_, c_, 1, 1)
|
1989 |
+
self.cv3 = Conv(2 * c_, c2, 1, 1)
|
1990 |
+
num_heads = c_ // 32
|
1991 |
+
self.m = SwinTransformer2Block(c_, c_, num_heads, n)
|
1992 |
+
#self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
1993 |
+
|
1994 |
+
def forward(self, x):
|
1995 |
+
x1 = self.cv1(x)
|
1996 |
+
y1 = self.m(x1)
|
1997 |
+
y2 = self.cv2(x1)
|
1998 |
+
return self.cv3(torch.cat((y1, y2), dim=1))
|
1999 |
+
|
2000 |
+
|
2001 |
+
class ST2CSPC(nn.Module):
|
2002 |
+
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
2003 |
+
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
2004 |
+
super(ST2CSPC, self).__init__()
|
2005 |
+
c_ = int(c2 * e) # hidden channels
|
2006 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
2007 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
2008 |
+
self.cv3 = Conv(c_, c_, 1, 1)
|
2009 |
+
self.cv4 = Conv(2 * c_, c2, 1, 1)
|
2010 |
+
num_heads = c_ // 32
|
2011 |
+
self.m = SwinTransformer2Block(c_, c_, num_heads, n)
|
2012 |
+
#self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
|
2013 |
+
|
2014 |
+
def forward(self, x):
|
2015 |
+
y1 = self.cv3(self.m(self.cv1(x)))
|
2016 |
+
y2 = self.cv2(x)
|
2017 |
+
return self.cv4(torch.cat((y1, y2), dim=1))
|
2018 |
+
|
2019 |
+
##### end of swin transformer v2 #####
|
infer/yolov7/models/experimental.py
ADDED
@@ -0,0 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: UTF-8 -*-
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import random
|
5 |
+
import torch
|
6 |
+
import torch.nn as nn
|
7 |
+
|
8 |
+
from infer.yolov7.models.common import Conv, DWConv
|
9 |
+
from infer.yolov7.utils.google_utils import attempt_download
|
10 |
+
|
11 |
+
|
12 |
+
class CrossConv(nn.Module):
|
13 |
+
# Cross Convolution Downsample
|
14 |
+
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False):
|
15 |
+
# ch_in, ch_out, kernel, stride, groups, expansion, shortcut
|
16 |
+
super(CrossConv, self).__init__()
|
17 |
+
c_ = int(c2 * e) # hidden channels
|
18 |
+
self.cv1 = Conv(c1, c_, (1, k), (1, s))
|
19 |
+
self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g)
|
20 |
+
self.add = shortcut and c1 == c2
|
21 |
+
|
22 |
+
def forward(self, x):
|
23 |
+
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
24 |
+
|
25 |
+
|
26 |
+
class Sum(nn.Module):
|
27 |
+
# Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
|
28 |
+
def __init__(self, n, weight=False): # n: number of inputs
|
29 |
+
super(Sum, self).__init__()
|
30 |
+
self.weight = weight # apply weights boolean
|
31 |
+
self.iter = range(n - 1) # iter object
|
32 |
+
if weight:
|
33 |
+
self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) # layer weights
|
34 |
+
|
35 |
+
def forward(self, x):
|
36 |
+
y = x[0] # no weight
|
37 |
+
if self.weight:
|
38 |
+
w = torch.sigmoid(self.w) * 2
|
39 |
+
for i in self.iter:
|
40 |
+
y = y + x[i + 1] * w[i]
|
41 |
+
else:
|
42 |
+
for i in self.iter:
|
43 |
+
y = y + x[i + 1]
|
44 |
+
return y
|
45 |
+
|
46 |
+
|
47 |
+
class MixConv2d(nn.Module):
|
48 |
+
# Mixed Depthwise Conv https://arxiv.org/abs/1907.09595
|
49 |
+
def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True):
|
50 |
+
super(MixConv2d, self).__init__()
|
51 |
+
groups = len(k)
|
52 |
+
if equal_ch: # equal c_ per group
|
53 |
+
i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices
|
54 |
+
c_ = [(i == g).sum() for g in range(groups)] # intermediate channels
|
55 |
+
else: # equal weight.numel() per group
|
56 |
+
b = [c2] + [0] * groups
|
57 |
+
a = np.eye(groups + 1, groups, k=-1)
|
58 |
+
a -= np.roll(a, 1, axis=1)
|
59 |
+
a *= np.array(k) ** 2
|
60 |
+
a[0] = 1
|
61 |
+
c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
|
62 |
+
|
63 |
+
self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)])
|
64 |
+
self.bn = nn.BatchNorm2d(c2)
|
65 |
+
self.act = nn.LeakyReLU(0.1, inplace=True)
|
66 |
+
|
67 |
+
def forward(self, x):
|
68 |
+
return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
|
69 |
+
|
70 |
+
|
71 |
+
class Ensemble(nn.ModuleList):
|
72 |
+
# Ensemble of models
|
73 |
+
def __init__(self):
|
74 |
+
super(Ensemble, self).__init__()
|
75 |
+
|
76 |
+
def forward(self, x, augment=False):
|
77 |
+
y = []
|
78 |
+
for module in self:
|
79 |
+
y.append(module(x, augment)[0])
|
80 |
+
# y = torch.stack(y).max(0)[0] # max ensemble
|
81 |
+
# y = torch.stack(y).mean(0) # mean ensemble
|
82 |
+
y = torch.cat(y, 1) # nms ensemble
|
83 |
+
return y, None # inference, train output
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
class ORT_NMS(torch.autograd.Function):
|
90 |
+
'''ONNX-Runtime NMS operation'''
|
91 |
+
@staticmethod
|
92 |
+
def forward(ctx,
|
93 |
+
boxes,
|
94 |
+
scores,
|
95 |
+
max_output_boxes_per_class=torch.tensor([100]),
|
96 |
+
iou_threshold=torch.tensor([0.45]),
|
97 |
+
score_threshold=torch.tensor([0.25])):
|
98 |
+
device = boxes.device
|
99 |
+
batch = scores.shape[0]
|
100 |
+
num_det = random.randint(0, 100)
|
101 |
+
batches = torch.randint(0, batch, (num_det,)).sort()[0].to(device)
|
102 |
+
idxs = torch.arange(100, 100 + num_det).to(device)
|
103 |
+
zeros = torch.zeros((num_det,), dtype=torch.int64).to(device)
|
104 |
+
selected_indices = torch.cat([batches[None], zeros[None], idxs[None]], 0).T.contiguous()
|
105 |
+
selected_indices = selected_indices.to(torch.int64)
|
106 |
+
return selected_indices
|
107 |
+
|
108 |
+
@staticmethod
|
109 |
+
def symbolic(g, boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold):
|
110 |
+
return g.op("NonMaxSuppression", boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold)
|
111 |
+
|
112 |
+
|
113 |
+
class TRT_NMS(torch.autograd.Function):
|
114 |
+
'''TensorRT NMS operation'''
|
115 |
+
@staticmethod
|
116 |
+
def forward(
|
117 |
+
ctx,
|
118 |
+
boxes,
|
119 |
+
scores,
|
120 |
+
background_class=-1,
|
121 |
+
box_coding=1,
|
122 |
+
iou_threshold=0.45,
|
123 |
+
max_output_boxes=100,
|
124 |
+
plugin_version="1",
|
125 |
+
score_activation=0,
|
126 |
+
score_threshold=0.25,
|
127 |
+
):
|
128 |
+
batch_size, num_boxes, num_classes = scores.shape
|
129 |
+
num_det = torch.randint(0, max_output_boxes, (batch_size, 1), dtype=torch.int32)
|
130 |
+
det_boxes = torch.randn(batch_size, max_output_boxes, 4)
|
131 |
+
det_scores = torch.randn(batch_size, max_output_boxes)
|
132 |
+
det_classes = torch.randint(0, num_classes, (batch_size, max_output_boxes), dtype=torch.int32)
|
133 |
+
return num_det, det_boxes, det_scores, det_classes
|
134 |
+
|
135 |
+
@staticmethod
|
136 |
+
def symbolic(g,
|
137 |
+
boxes,
|
138 |
+
scores,
|
139 |
+
background_class=-1,
|
140 |
+
box_coding=1,
|
141 |
+
iou_threshold=0.45,
|
142 |
+
max_output_boxes=100,
|
143 |
+
plugin_version="1",
|
144 |
+
score_activation=0,
|
145 |
+
score_threshold=0.25):
|
146 |
+
out = g.op("TRT::EfficientNMS_TRT",
|
147 |
+
boxes,
|
148 |
+
scores,
|
149 |
+
background_class_i=background_class,
|
150 |
+
box_coding_i=box_coding,
|
151 |
+
iou_threshold_f=iou_threshold,
|
152 |
+
max_output_boxes_i=max_output_boxes,
|
153 |
+
plugin_version_s=plugin_version,
|
154 |
+
score_activation_i=score_activation,
|
155 |
+
score_threshold_f=score_threshold,
|
156 |
+
outputs=4)
|
157 |
+
nums, boxes, scores, classes = out
|
158 |
+
return nums, boxes, scores, classes
|
159 |
+
|
160 |
+
|
161 |
+
class ONNX_ORT(nn.Module):
|
162 |
+
'''onnx module with ONNX-Runtime NMS operation.'''
|
163 |
+
def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=640, device=None, n_classes=80):
|
164 |
+
super().__init__()
|
165 |
+
self.device = device if device else torch.device("cpu")
|
166 |
+
self.max_obj = torch.tensor([max_obj]).to(device)
|
167 |
+
self.iou_threshold = torch.tensor([iou_thres]).to(device)
|
168 |
+
self.score_threshold = torch.tensor([score_thres]).to(device)
|
169 |
+
self.max_wh = max_wh # if max_wh != 0 : non-agnostic else : agnostic
|
170 |
+
self.convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]],
|
171 |
+
dtype=torch.float32,
|
172 |
+
device=self.device)
|
173 |
+
self.n_classes=n_classes
|
174 |
+
|
175 |
+
def forward(self, x):
|
176 |
+
boxes = x[:, :, :4]
|
177 |
+
conf = x[:, :, 4:5]
|
178 |
+
scores = x[:, :, 5:]
|
179 |
+
if self.n_classes == 1:
|
180 |
+
scores = conf # for models with one class, cls_loss is 0 and cls_conf is always 0.5,
|
181 |
+
# so there is no need to multiplicate.
|
182 |
+
else:
|
183 |
+
scores *= conf # conf = obj_conf * cls_conf
|
184 |
+
boxes @= self.convert_matrix
|
185 |
+
max_score, category_id = scores.max(2, keepdim=True)
|
186 |
+
dis = category_id.float() * self.max_wh
|
187 |
+
nmsbox = boxes + dis
|
188 |
+
max_score_tp = max_score.transpose(1, 2).contiguous()
|
189 |
+
selected_indices = ORT_NMS.apply(nmsbox, max_score_tp, self.max_obj, self.iou_threshold, self.score_threshold)
|
190 |
+
X, Y = selected_indices[:, 0], selected_indices[:, 2]
|
191 |
+
selected_boxes = boxes[X, Y, :]
|
192 |
+
selected_categories = category_id[X, Y, :].float()
|
193 |
+
selected_scores = max_score[X, Y, :]
|
194 |
+
X = X.unsqueeze(1).float()
|
195 |
+
return torch.cat([X, selected_boxes, selected_categories, selected_scores], 1)
|
196 |
+
|
197 |
+
class ONNX_TRT(nn.Module):
|
198 |
+
'''onnx module with TensorRT NMS operation.'''
|
199 |
+
def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None ,device=None, n_classes=80):
|
200 |
+
super().__init__()
|
201 |
+
assert max_wh is None
|
202 |
+
self.device = device if device else torch.device('cpu')
|
203 |
+
self.background_class = -1,
|
204 |
+
self.box_coding = 1,
|
205 |
+
self.iou_threshold = iou_thres
|
206 |
+
self.max_obj = max_obj
|
207 |
+
self.plugin_version = '1'
|
208 |
+
self.score_activation = 0
|
209 |
+
self.score_threshold = score_thres
|
210 |
+
self.n_classes=n_classes
|
211 |
+
|
212 |
+
def forward(self, x):
|
213 |
+
boxes = x[:, :, :4]
|
214 |
+
conf = x[:, :, 4:5]
|
215 |
+
scores = x[:, :, 5:]
|
216 |
+
if self.n_classes == 1:
|
217 |
+
scores = conf # for models with one class, cls_loss is 0 and cls_conf is always 0.5,
|
218 |
+
# so there is no need to multiplicate.
|
219 |
+
else:
|
220 |
+
scores *= conf # conf = obj_conf * cls_conf
|
221 |
+
num_det, det_boxes, det_scores, det_classes = TRT_NMS.apply(boxes, scores, self.background_class, self.box_coding,
|
222 |
+
self.iou_threshold, self.max_obj,
|
223 |
+
self.plugin_version, self.score_activation,
|
224 |
+
self.score_threshold)
|
225 |
+
return num_det, det_boxes, det_scores, det_classes
|
226 |
+
|
227 |
+
|
228 |
+
class End2End(nn.Module):
|
229 |
+
'''export onnx or tensorrt model with NMS operation.'''
|
230 |
+
def __init__(self, model, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None, device=None, n_classes=80):
|
231 |
+
super().__init__()
|
232 |
+
device = device if device else torch.device('cpu')
|
233 |
+
assert isinstance(max_wh,(int)) or max_wh is None
|
234 |
+
self.model = model.to(device)
|
235 |
+
self.model.model[-1].end2end = True
|
236 |
+
self.patch_model = ONNX_TRT if max_wh is None else ONNX_ORT
|
237 |
+
self.end2end = self.patch_model(max_obj, iou_thres, score_thres, max_wh, device, n_classes)
|
238 |
+
self.end2end.eval()
|
239 |
+
|
240 |
+
def forward(self, x):
|
241 |
+
x = self.model(x)
|
242 |
+
x = self.end2end(x)
|
243 |
+
return x
|
244 |
+
|
245 |
+
|
246 |
+
|
247 |
+
|
248 |
+
|
249 |
+
def attempt_load(weights, map_location=None):
|
250 |
+
# Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
|
251 |
+
import sys
|
252 |
+
sys.path.insert(0, "infer\yolov7")
|
253 |
+
|
254 |
+
model = Ensemble()
|
255 |
+
for w in weights if isinstance(weights, list) else [weights]:
|
256 |
+
#attempt_download(w)
|
257 |
+
ckpt = torch.load(w, map_location=map_location) # load
|
258 |
+
model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model
|
259 |
+
|
260 |
+
# Compatibility updates
|
261 |
+
for m in model.modules():
|
262 |
+
if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]:
|
263 |
+
m.inplace = True # pytorch 1.7.0 compatibility
|
264 |
+
elif type(m) is nn.Upsample:
|
265 |
+
m.recompute_scale_factor = None # torch 1.11.0 compatibility
|
266 |
+
elif type(m) is Conv:
|
267 |
+
m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility
|
268 |
+
|
269 |
+
if len(model) == 1:
|
270 |
+
return model[-1] # return model
|
271 |
+
else:
|
272 |
+
print('Ensemble created with %s\n' % weights)
|
273 |
+
for k in ['names', 'stride']:
|
274 |
+
setattr(model, k, getattr(model[-1], k))
|
275 |
+
return model # return ensemble
|
276 |
+
|
277 |
+
|
infer/yolov7/models/yolo.py
ADDED
@@ -0,0 +1,843 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import logging
|
3 |
+
import sys
|
4 |
+
from copy import deepcopy
|
5 |
+
|
6 |
+
sys.path.append('./') # to run '$ python *.py' files in subdirectories
|
7 |
+
logger = logging.getLogger(__name__)
|
8 |
+
import torch
|
9 |
+
from infer.yolov7.models.common import *
|
10 |
+
from infer.yolov7.models.experimental import *
|
11 |
+
from infer.yolov7.utils.autoanchor import check_anchor_order
|
12 |
+
from infer.yolov7.utils.general import make_divisible, check_file, set_logging
|
13 |
+
from infer.yolov7.utils.torch_utils import time_synchronized, fuse_conv_and_bn, model_info, scale_img, initialize_weights, \
|
14 |
+
select_device, copy_attr
|
15 |
+
from infer.yolov7.utils.loss import SigmoidBin
|
16 |
+
|
17 |
+
try:
|
18 |
+
import thop # for FLOPS computation
|
19 |
+
except ImportError:
|
20 |
+
thop = None
|
21 |
+
|
22 |
+
|
23 |
+
class Detect(nn.Module):
|
24 |
+
stride = None # strides computed during build
|
25 |
+
export = False # onnx export
|
26 |
+
end2end = False
|
27 |
+
include_nms = False
|
28 |
+
concat = False
|
29 |
+
|
30 |
+
def __init__(self, nc=80, anchors=(), ch=()): # detection layer
|
31 |
+
super(Detect, self).__init__()
|
32 |
+
self.nc = nc # number of classes
|
33 |
+
self.no = nc + 5 # number of outputs per anchor
|
34 |
+
self.nl = len(anchors) # number of detection layers
|
35 |
+
self.na = len(anchors[0]) // 2 # number of anchors
|
36 |
+
self.grid = [torch.zeros(1)] * self.nl # init grid
|
37 |
+
a = torch.tensor(anchors).float().view(self.nl, -1, 2)
|
38 |
+
self.register_buffer('anchors', a) # shape(nl,na,2)
|
39 |
+
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)
|
40 |
+
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
|
41 |
+
|
42 |
+
def forward(self, x):
|
43 |
+
# x = x.copy() # for profiling
|
44 |
+
z = [] # inference output
|
45 |
+
self.training |= self.export
|
46 |
+
for i in range(self.nl):
|
47 |
+
x[i] = self.m[i](x[i]) # conv
|
48 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
49 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
50 |
+
|
51 |
+
if not self.training: # inference
|
52 |
+
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
53 |
+
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
|
54 |
+
y = x[i].sigmoid()
|
55 |
+
if not torch.onnx.is_in_onnx_export():
|
56 |
+
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
57 |
+
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
58 |
+
else:
|
59 |
+
xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
|
60 |
+
xy = xy * (2. * self.stride[i]) + (self.stride[i] * (self.grid[i] - 0.5)) # new xy
|
61 |
+
wh = wh ** 2 * (4 * self.anchor_grid[i].data) # new wh
|
62 |
+
y = torch.cat((xy, wh, conf), 4)
|
63 |
+
z.append(y.view(bs, -1, self.no))
|
64 |
+
|
65 |
+
if self.training:
|
66 |
+
out = x
|
67 |
+
elif self.end2end:
|
68 |
+
out = torch.cat(z, 1)
|
69 |
+
elif self.include_nms:
|
70 |
+
z = self.convert(z)
|
71 |
+
out = (z, )
|
72 |
+
elif self.concat:
|
73 |
+
out = torch.cat(z, 1)
|
74 |
+
else:
|
75 |
+
out = (torch.cat(z, 1), x)
|
76 |
+
|
77 |
+
return out
|
78 |
+
|
79 |
+
@staticmethod
|
80 |
+
def _make_grid(nx=20, ny=20):
|
81 |
+
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
|
82 |
+
return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
|
83 |
+
|
84 |
+
def convert(self, z):
|
85 |
+
z = torch.cat(z, 1)
|
86 |
+
box = z[:, :, :4]
|
87 |
+
conf = z[:, :, 4:5]
|
88 |
+
score = z[:, :, 5:]
|
89 |
+
score *= conf
|
90 |
+
convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]],
|
91 |
+
dtype=torch.float32,
|
92 |
+
device=z.device)
|
93 |
+
box @= convert_matrix
|
94 |
+
return (box, score)
|
95 |
+
|
96 |
+
|
97 |
+
class IDetect(nn.Module):
|
98 |
+
stride = None # strides computed during build
|
99 |
+
export = False # onnx export
|
100 |
+
end2end = False
|
101 |
+
include_nms = False
|
102 |
+
concat = False
|
103 |
+
|
104 |
+
def __init__(self, nc=80, anchors=(), ch=()): # detection layer
|
105 |
+
super(IDetect, self).__init__()
|
106 |
+
self.nc = nc # number of classes
|
107 |
+
self.no = nc + 5 # number of outputs per anchor
|
108 |
+
self.nl = len(anchors) # number of detection layers
|
109 |
+
self.na = len(anchors[0]) // 2 # number of anchors
|
110 |
+
self.grid = [torch.zeros(1)] * self.nl # init grid
|
111 |
+
a = torch.tensor(anchors).float().view(self.nl, -1, 2)
|
112 |
+
self.register_buffer('anchors', a) # shape(nl,na,2)
|
113 |
+
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)
|
114 |
+
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
|
115 |
+
|
116 |
+
self.ia = nn.ModuleList(ImplicitA(x) for x in ch)
|
117 |
+
self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch)
|
118 |
+
|
119 |
+
def forward(self, x):
|
120 |
+
# x = x.copy() # for profiling
|
121 |
+
z = [] # inference output
|
122 |
+
self.training |= self.export
|
123 |
+
for i in range(self.nl):
|
124 |
+
x[i] = self.m[i](self.ia[i](x[i])) # conv
|
125 |
+
x[i] = self.im[i](x[i])
|
126 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
127 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
128 |
+
|
129 |
+
if not self.training: # inference
|
130 |
+
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
131 |
+
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
|
132 |
+
|
133 |
+
y = x[i].sigmoid()
|
134 |
+
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
135 |
+
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
136 |
+
z.append(y.view(bs, -1, self.no))
|
137 |
+
|
138 |
+
return x if self.training else (torch.cat(z, 1), x)
|
139 |
+
|
140 |
+
def fuseforward(self, x):
|
141 |
+
# x = x.copy() # for profiling
|
142 |
+
z = [] # inference output
|
143 |
+
self.training |= self.export
|
144 |
+
for i in range(self.nl):
|
145 |
+
x[i] = self.m[i](x[i]) # conv
|
146 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
147 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
148 |
+
|
149 |
+
if not self.training: # inference
|
150 |
+
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
151 |
+
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
|
152 |
+
|
153 |
+
y = x[i].sigmoid()
|
154 |
+
if not torch.onnx.is_in_onnx_export():
|
155 |
+
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
156 |
+
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
157 |
+
else:
|
158 |
+
xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
|
159 |
+
xy = xy * (2. * self.stride[i]) + (self.stride[i] * (self.grid[i] - 0.5)) # new xy
|
160 |
+
wh = wh ** 2 * (4 * self.anchor_grid[i].data) # new wh
|
161 |
+
y = torch.cat((xy, wh, conf), 4)
|
162 |
+
z.append(y.view(bs, -1, self.no))
|
163 |
+
|
164 |
+
if self.training:
|
165 |
+
out = x
|
166 |
+
elif self.end2end:
|
167 |
+
out = torch.cat(z, 1)
|
168 |
+
elif self.include_nms:
|
169 |
+
z = self.convert(z)
|
170 |
+
out = (z, )
|
171 |
+
elif self.concat:
|
172 |
+
out = torch.cat(z, 1)
|
173 |
+
else:
|
174 |
+
out = (torch.cat(z, 1), x)
|
175 |
+
|
176 |
+
return out
|
177 |
+
|
178 |
+
def fuse(self):
|
179 |
+
print("IDetect.fuse")
|
180 |
+
# fuse ImplicitA and Convolution
|
181 |
+
for i in range(len(self.m)):
|
182 |
+
c1,c2,_,_ = self.m[i].weight.shape
|
183 |
+
c1_,c2_, _,_ = self.ia[i].implicit.shape
|
184 |
+
self.m[i].bias += torch.matmul(self.m[i].weight.reshape(c1,c2),self.ia[i].implicit.reshape(c2_,c1_)).squeeze(1)
|
185 |
+
|
186 |
+
# fuse ImplicitM and Convolution
|
187 |
+
for i in range(len(self.m)):
|
188 |
+
c1,c2, _,_ = self.im[i].implicit.shape
|
189 |
+
self.m[i].bias *= self.im[i].implicit.reshape(c2)
|
190 |
+
self.m[i].weight *= self.im[i].implicit.transpose(0,1)
|
191 |
+
|
192 |
+
@staticmethod
|
193 |
+
def _make_grid(nx=20, ny=20):
|
194 |
+
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
|
195 |
+
return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
|
196 |
+
|
197 |
+
def convert(self, z):
|
198 |
+
z = torch.cat(z, 1)
|
199 |
+
box = z[:, :, :4]
|
200 |
+
conf = z[:, :, 4:5]
|
201 |
+
score = z[:, :, 5:]
|
202 |
+
score *= conf
|
203 |
+
convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]],
|
204 |
+
dtype=torch.float32,
|
205 |
+
device=z.device)
|
206 |
+
box @= convert_matrix
|
207 |
+
return (box, score)
|
208 |
+
|
209 |
+
|
210 |
+
class IKeypoint(nn.Module):
|
211 |
+
stride = None # strides computed during build
|
212 |
+
export = False # onnx export
|
213 |
+
|
214 |
+
def __init__(self, nc=80, anchors=(), nkpt=17, ch=(), inplace=True, dw_conv_kpt=False): # detection layer
|
215 |
+
super(IKeypoint, self).__init__()
|
216 |
+
self.nc = nc # number of classes
|
217 |
+
self.nkpt = nkpt
|
218 |
+
self.dw_conv_kpt = dw_conv_kpt
|
219 |
+
self.no_det=(nc + 5) # number of outputs per anchor for box and class
|
220 |
+
self.no_kpt = 3*self.nkpt ## number of outputs per anchor for keypoints
|
221 |
+
self.no = self.no_det+self.no_kpt
|
222 |
+
self.nl = len(anchors) # number of detection layers
|
223 |
+
self.na = len(anchors[0]) // 2 # number of anchors
|
224 |
+
self.grid = [torch.zeros(1)] * self.nl # init grid
|
225 |
+
self.flip_test = False
|
226 |
+
a = torch.tensor(anchors).float().view(self.nl, -1, 2)
|
227 |
+
self.register_buffer('anchors', a) # shape(nl,na,2)
|
228 |
+
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)
|
229 |
+
self.m = nn.ModuleList(nn.Conv2d(x, self.no_det * self.na, 1) for x in ch) # output conv
|
230 |
+
|
231 |
+
self.ia = nn.ModuleList(ImplicitA(x) for x in ch)
|
232 |
+
self.im = nn.ModuleList(ImplicitM(self.no_det * self.na) for _ in ch)
|
233 |
+
|
234 |
+
if self.nkpt is not None:
|
235 |
+
if self.dw_conv_kpt: #keypoint head is slightly more complex
|
236 |
+
self.m_kpt = nn.ModuleList(
|
237 |
+
nn.Sequential(DWConv(x, x, k=3), Conv(x,x),
|
238 |
+
DWConv(x, x, k=3), Conv(x, x),
|
239 |
+
DWConv(x, x, k=3), Conv(x,x),
|
240 |
+
DWConv(x, x, k=3), Conv(x, x),
|
241 |
+
DWConv(x, x, k=3), Conv(x, x),
|
242 |
+
DWConv(x, x, k=3), nn.Conv2d(x, self.no_kpt * self.na, 1)) for x in ch)
|
243 |
+
else: #keypoint head is a single convolution
|
244 |
+
self.m_kpt = nn.ModuleList(nn.Conv2d(x, self.no_kpt * self.na, 1) for x in ch)
|
245 |
+
|
246 |
+
self.inplace = inplace # use in-place ops (e.g. slice assignment)
|
247 |
+
|
248 |
+
def forward(self, x):
|
249 |
+
# x = x.copy() # for profiling
|
250 |
+
z = [] # inference output
|
251 |
+
self.training |= self.export
|
252 |
+
for i in range(self.nl):
|
253 |
+
if self.nkpt is None or self.nkpt==0:
|
254 |
+
x[i] = self.im[i](self.m[i](self.ia[i](x[i]))) # conv
|
255 |
+
else :
|
256 |
+
x[i] = torch.cat((self.im[i](self.m[i](self.ia[i](x[i]))), self.m_kpt[i](x[i])), axis=1)
|
257 |
+
|
258 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
259 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
260 |
+
x_det = x[i][..., :6]
|
261 |
+
x_kpt = x[i][..., 6:]
|
262 |
+
|
263 |
+
if not self.training: # inference
|
264 |
+
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
265 |
+
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
|
266 |
+
kpt_grid_x = self.grid[i][..., 0:1]
|
267 |
+
kpt_grid_y = self.grid[i][..., 1:2]
|
268 |
+
|
269 |
+
if self.nkpt == 0:
|
270 |
+
y = x[i].sigmoid()
|
271 |
+
else:
|
272 |
+
y = x_det.sigmoid()
|
273 |
+
|
274 |
+
if self.inplace:
|
275 |
+
xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
276 |
+
wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i].view(1, self.na, 1, 1, 2) # wh
|
277 |
+
if self.nkpt != 0:
|
278 |
+
x_kpt[..., 0::3] = (x_kpt[..., ::3] * 2. - 0.5 + kpt_grid_x.repeat(1,1,1,1,17)) * self.stride[i] # xy
|
279 |
+
x_kpt[..., 1::3] = (x_kpt[..., 1::3] * 2. - 0.5 + kpt_grid_y.repeat(1,1,1,1,17)) * self.stride[i] # xy
|
280 |
+
#x_kpt[..., 0::3] = (x_kpt[..., ::3] + kpt_grid_x.repeat(1,1,1,1,17)) * self.stride[i] # xy
|
281 |
+
#x_kpt[..., 1::3] = (x_kpt[..., 1::3] + kpt_grid_y.repeat(1,1,1,1,17)) * self.stride[i] # xy
|
282 |
+
#print('=============')
|
283 |
+
#print(self.anchor_grid[i].shape)
|
284 |
+
#print(self.anchor_grid[i][...,0].unsqueeze(4).shape)
|
285 |
+
#print(x_kpt[..., 0::3].shape)
|
286 |
+
#x_kpt[..., 0::3] = ((x_kpt[..., 0::3].tanh() * 2.) ** 3 * self.anchor_grid[i][...,0].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_x.repeat(1,1,1,1,17) * self.stride[i] # xy
|
287 |
+
#x_kpt[..., 1::3] = ((x_kpt[..., 1::3].tanh() * 2.) ** 3 * self.anchor_grid[i][...,1].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_y.repeat(1,1,1,1,17) * self.stride[i] # xy
|
288 |
+
#x_kpt[..., 0::3] = (((x_kpt[..., 0::3].sigmoid() * 4.) ** 2 - 8.) * self.anchor_grid[i][...,0].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_x.repeat(1,1,1,1,17) * self.stride[i] # xy
|
289 |
+
#x_kpt[..., 1::3] = (((x_kpt[..., 1::3].sigmoid() * 4.) ** 2 - 8.) * self.anchor_grid[i][...,1].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_y.repeat(1,1,1,1,17) * self.stride[i] # xy
|
290 |
+
x_kpt[..., 2::3] = x_kpt[..., 2::3].sigmoid()
|
291 |
+
|
292 |
+
y = torch.cat((xy, wh, y[..., 4:], x_kpt), dim = -1)
|
293 |
+
|
294 |
+
else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
|
295 |
+
xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
296 |
+
wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
297 |
+
if self.nkpt != 0:
|
298 |
+
y[..., 6:] = (y[..., 6:] * 2. - 0.5 + self.grid[i].repeat((1,1,1,1,self.nkpt))) * self.stride[i] # xy
|
299 |
+
y = torch.cat((xy, wh, y[..., 4:]), -1)
|
300 |
+
|
301 |
+
z.append(y.view(bs, -1, self.no))
|
302 |
+
|
303 |
+
return x if self.training else (torch.cat(z, 1), x)
|
304 |
+
|
305 |
+
@staticmethod
|
306 |
+
def _make_grid(nx=20, ny=20):
|
307 |
+
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
|
308 |
+
return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
|
309 |
+
|
310 |
+
|
311 |
+
class IAuxDetect(nn.Module):
|
312 |
+
stride = None # strides computed during build
|
313 |
+
export = False # onnx export
|
314 |
+
end2end = False
|
315 |
+
include_nms = False
|
316 |
+
concat = False
|
317 |
+
|
318 |
+
def __init__(self, nc=80, anchors=(), ch=()): # detection layer
|
319 |
+
super(IAuxDetect, self).__init__()
|
320 |
+
self.nc = nc # number of classes
|
321 |
+
self.no = nc + 5 # number of outputs per anchor
|
322 |
+
self.nl = len(anchors) # number of detection layers
|
323 |
+
self.na = len(anchors[0]) // 2 # number of anchors
|
324 |
+
self.grid = [torch.zeros(1)] * self.nl # init grid
|
325 |
+
a = torch.tensor(anchors).float().view(self.nl, -1, 2)
|
326 |
+
self.register_buffer('anchors', a) # shape(nl,na,2)
|
327 |
+
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)
|
328 |
+
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch[:self.nl]) # output conv
|
329 |
+
self.m2 = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch[self.nl:]) # output conv
|
330 |
+
|
331 |
+
self.ia = nn.ModuleList(ImplicitA(x) for x in ch[:self.nl])
|
332 |
+
self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch[:self.nl])
|
333 |
+
|
334 |
+
def forward(self, x):
|
335 |
+
# x = x.copy() # for profiling
|
336 |
+
z = [] # inference output
|
337 |
+
self.training |= self.export
|
338 |
+
for i in range(self.nl):
|
339 |
+
x[i] = self.m[i](self.ia[i](x[i])) # conv
|
340 |
+
x[i] = self.im[i](x[i])
|
341 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
342 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
343 |
+
|
344 |
+
x[i+self.nl] = self.m2[i](x[i+self.nl])
|
345 |
+
x[i+self.nl] = x[i+self.nl].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
346 |
+
|
347 |
+
if not self.training: # inference
|
348 |
+
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
349 |
+
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
|
350 |
+
|
351 |
+
y = x[i].sigmoid()
|
352 |
+
if not torch.onnx.is_in_onnx_export():
|
353 |
+
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
354 |
+
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
355 |
+
else:
|
356 |
+
xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
|
357 |
+
xy = xy * (2. * self.stride[i]) + (self.stride[i] * (self.grid[i] - 0.5)) # new xy
|
358 |
+
wh = wh ** 2 * (4 * self.anchor_grid[i].data) # new wh
|
359 |
+
y = torch.cat((xy, wh, conf), 4)
|
360 |
+
z.append(y.view(bs, -1, self.no))
|
361 |
+
|
362 |
+
return x if self.training else (torch.cat(z, 1), x[:self.nl])
|
363 |
+
|
364 |
+
def fuseforward(self, x):
|
365 |
+
# x = x.copy() # for profiling
|
366 |
+
z = [] # inference output
|
367 |
+
self.training |= self.export
|
368 |
+
for i in range(self.nl):
|
369 |
+
x[i] = self.m[i](x[i]) # conv
|
370 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
371 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
372 |
+
|
373 |
+
if not self.training: # inference
|
374 |
+
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
375 |
+
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
|
376 |
+
|
377 |
+
y = x[i].sigmoid()
|
378 |
+
if not torch.onnx.is_in_onnx_export():
|
379 |
+
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
380 |
+
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
381 |
+
else:
|
382 |
+
xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
383 |
+
wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i].data # wh
|
384 |
+
y = torch.cat((xy, wh, y[..., 4:]), -1)
|
385 |
+
z.append(y.view(bs, -1, self.no))
|
386 |
+
|
387 |
+
if self.training:
|
388 |
+
out = x
|
389 |
+
elif self.end2end:
|
390 |
+
out = torch.cat(z, 1)
|
391 |
+
elif self.include_nms:
|
392 |
+
z = self.convert(z)
|
393 |
+
out = (z, )
|
394 |
+
elif self.concat:
|
395 |
+
out = torch.cat(z, 1)
|
396 |
+
else:
|
397 |
+
out = (torch.cat(z, 1), x)
|
398 |
+
|
399 |
+
return out
|
400 |
+
|
401 |
+
def fuse(self):
|
402 |
+
print("IAuxDetect.fuse")
|
403 |
+
# fuse ImplicitA and Convolution
|
404 |
+
for i in range(len(self.m)):
|
405 |
+
c1,c2,_,_ = self.m[i].weight.shape
|
406 |
+
c1_,c2_, _,_ = self.ia[i].implicit.shape
|
407 |
+
self.m[i].bias += torch.matmul(self.m[i].weight.reshape(c1,c2),self.ia[i].implicit.reshape(c2_,c1_)).squeeze(1)
|
408 |
+
|
409 |
+
# fuse ImplicitM and Convolution
|
410 |
+
for i in range(len(self.m)):
|
411 |
+
c1,c2, _,_ = self.im[i].implicit.shape
|
412 |
+
self.m[i].bias *= self.im[i].implicit.reshape(c2)
|
413 |
+
self.m[i].weight *= self.im[i].implicit.transpose(0,1)
|
414 |
+
|
415 |
+
@staticmethod
|
416 |
+
def _make_grid(nx=20, ny=20):
|
417 |
+
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
|
418 |
+
return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
|
419 |
+
|
420 |
+
def convert(self, z):
|
421 |
+
z = torch.cat(z, 1)
|
422 |
+
box = z[:, :, :4]
|
423 |
+
conf = z[:, :, 4:5]
|
424 |
+
score = z[:, :, 5:]
|
425 |
+
score *= conf
|
426 |
+
convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]],
|
427 |
+
dtype=torch.float32,
|
428 |
+
device=z.device)
|
429 |
+
box @= convert_matrix
|
430 |
+
return (box, score)
|
431 |
+
|
432 |
+
|
433 |
+
class IBin(nn.Module):
|
434 |
+
stride = None # strides computed during build
|
435 |
+
export = False # onnx export
|
436 |
+
|
437 |
+
def __init__(self, nc=80, anchors=(), ch=(), bin_count=21): # detection layer
|
438 |
+
super(IBin, self).__init__()
|
439 |
+
self.nc = nc # number of classes
|
440 |
+
self.bin_count = bin_count
|
441 |
+
|
442 |
+
self.w_bin_sigmoid = SigmoidBin(bin_count=self.bin_count, min=0.0, max=4.0)
|
443 |
+
self.h_bin_sigmoid = SigmoidBin(bin_count=self.bin_count, min=0.0, max=4.0)
|
444 |
+
# classes, x,y,obj
|
445 |
+
self.no = nc + 3 + \
|
446 |
+
self.w_bin_sigmoid.get_length() + self.h_bin_sigmoid.get_length() # w-bce, h-bce
|
447 |
+
# + self.x_bin_sigmoid.get_length() + self.y_bin_sigmoid.get_length()
|
448 |
+
|
449 |
+
self.nl = len(anchors) # number of detection layers
|
450 |
+
self.na = len(anchors[0]) // 2 # number of anchors
|
451 |
+
self.grid = [torch.zeros(1)] * self.nl # init grid
|
452 |
+
a = torch.tensor(anchors).float().view(self.nl, -1, 2)
|
453 |
+
self.register_buffer('anchors', a) # shape(nl,na,2)
|
454 |
+
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2)
|
455 |
+
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
|
456 |
+
|
457 |
+
self.ia = nn.ModuleList(ImplicitA(x) for x in ch)
|
458 |
+
self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch)
|
459 |
+
|
460 |
+
def forward(self, x):
|
461 |
+
|
462 |
+
#self.x_bin_sigmoid.use_fw_regression = True
|
463 |
+
#self.y_bin_sigmoid.use_fw_regression = True
|
464 |
+
self.w_bin_sigmoid.use_fw_regression = True
|
465 |
+
self.h_bin_sigmoid.use_fw_regression = True
|
466 |
+
|
467 |
+
# x = x.copy() # for profiling
|
468 |
+
z = [] # inference output
|
469 |
+
self.training |= self.export
|
470 |
+
for i in range(self.nl):
|
471 |
+
x[i] = self.m[i](self.ia[i](x[i])) # conv
|
472 |
+
x[i] = self.im[i](x[i])
|
473 |
+
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
474 |
+
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
475 |
+
|
476 |
+
if not self.training: # inference
|
477 |
+
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
478 |
+
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
|
479 |
+
|
480 |
+
y = x[i].sigmoid()
|
481 |
+
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy
|
482 |
+
#y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
|
483 |
+
|
484 |
+
|
485 |
+
#px = (self.x_bin_sigmoid.forward(y[..., 0:12]) + self.grid[i][..., 0]) * self.stride[i]
|
486 |
+
#py = (self.y_bin_sigmoid.forward(y[..., 12:24]) + self.grid[i][..., 1]) * self.stride[i]
|
487 |
+
|
488 |
+
pw = self.w_bin_sigmoid.forward(y[..., 2:24]) * self.anchor_grid[i][..., 0]
|
489 |
+
ph = self.h_bin_sigmoid.forward(y[..., 24:46]) * self.anchor_grid[i][..., 1]
|
490 |
+
|
491 |
+
#y[..., 0] = px
|
492 |
+
#y[..., 1] = py
|
493 |
+
y[..., 2] = pw
|
494 |
+
y[..., 3] = ph
|
495 |
+
|
496 |
+
y = torch.cat((y[..., 0:4], y[..., 46:]), dim=-1)
|
497 |
+
|
498 |
+
z.append(y.view(bs, -1, y.shape[-1]))
|
499 |
+
|
500 |
+
return x if self.training else (torch.cat(z, 1), x)
|
501 |
+
|
502 |
+
@staticmethod
|
503 |
+
def _make_grid(nx=20, ny=20):
|
504 |
+
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
|
505 |
+
return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
|
506 |
+
|
507 |
+
|
508 |
+
class Model(nn.Module):
|
509 |
+
def __init__(self, cfg='yolor-csp-c.yaml', ch=3, nc=None, anchors=None): # model, input channels, number of classes
|
510 |
+
super(Model, self).__init__()
|
511 |
+
self.traced = False
|
512 |
+
if isinstance(cfg, dict):
|
513 |
+
self.yaml = cfg # model dict
|
514 |
+
else: # is *.yaml
|
515 |
+
import yaml # for torch hub
|
516 |
+
self.yaml_file = Path(cfg).name
|
517 |
+
with open(cfg) as f:
|
518 |
+
self.yaml = yaml.load(f, Loader=yaml.SafeLoader) # model dict
|
519 |
+
|
520 |
+
# Define model
|
521 |
+
ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels
|
522 |
+
if nc and nc != self.yaml['nc']:
|
523 |
+
logger.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}")
|
524 |
+
self.yaml['nc'] = nc # override yaml value
|
525 |
+
if anchors:
|
526 |
+
logger.info(f'Overriding model.yaml anchors with anchors={anchors}')
|
527 |
+
self.yaml['anchors'] = round(anchors) # override yaml value
|
528 |
+
self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist
|
529 |
+
self.names = [str(i) for i in range(self.yaml['nc'])] # default names
|
530 |
+
# print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))])
|
531 |
+
|
532 |
+
# Build strides, anchors
|
533 |
+
m = self.model[-1] # Detect()
|
534 |
+
if isinstance(m, Detect):
|
535 |
+
s = 256 # 2x min stride
|
536 |
+
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
|
537 |
+
check_anchor_order(m)
|
538 |
+
m.anchors /= m.stride.view(-1, 1, 1)
|
539 |
+
self.stride = m.stride
|
540 |
+
self._initialize_biases() # only run once
|
541 |
+
# print('Strides: %s' % m.stride.tolist())
|
542 |
+
if isinstance(m, IDetect):
|
543 |
+
s = 256 # 2x min stride
|
544 |
+
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
|
545 |
+
check_anchor_order(m)
|
546 |
+
m.anchors /= m.stride.view(-1, 1, 1)
|
547 |
+
self.stride = m.stride
|
548 |
+
self._initialize_biases() # only run once
|
549 |
+
# print('Strides: %s' % m.stride.tolist())
|
550 |
+
if isinstance(m, IAuxDetect):
|
551 |
+
s = 256 # 2x min stride
|
552 |
+
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))[:4]]) # forward
|
553 |
+
#print(m.stride)
|
554 |
+
check_anchor_order(m)
|
555 |
+
m.anchors /= m.stride.view(-1, 1, 1)
|
556 |
+
self.stride = m.stride
|
557 |
+
self._initialize_aux_biases() # only run once
|
558 |
+
# print('Strides: %s' % m.stride.tolist())
|
559 |
+
if isinstance(m, IBin):
|
560 |
+
s = 256 # 2x min stride
|
561 |
+
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
|
562 |
+
check_anchor_order(m)
|
563 |
+
m.anchors /= m.stride.view(-1, 1, 1)
|
564 |
+
self.stride = m.stride
|
565 |
+
self._initialize_biases_bin() # only run once
|
566 |
+
# print('Strides: %s' % m.stride.tolist())
|
567 |
+
if isinstance(m, IKeypoint):
|
568 |
+
s = 256 # 2x min stride
|
569 |
+
m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward
|
570 |
+
check_anchor_order(m)
|
571 |
+
m.anchors /= m.stride.view(-1, 1, 1)
|
572 |
+
self.stride = m.stride
|
573 |
+
self._initialize_biases_kpt() # only run once
|
574 |
+
# print('Strides: %s' % m.stride.tolist())
|
575 |
+
|
576 |
+
# Init weights, biases
|
577 |
+
initialize_weights(self)
|
578 |
+
self.info()
|
579 |
+
logger.info('')
|
580 |
+
|
581 |
+
def forward(self, x, augment=False, profile=False):
|
582 |
+
if augment:
|
583 |
+
img_size = x.shape[-2:] # height, width
|
584 |
+
s = [1, 0.83, 0.67] # scales
|
585 |
+
f = [None, 3, None] # flips (2-ud, 3-lr)
|
586 |
+
y = [] # outputs
|
587 |
+
for si, fi in zip(s, f):
|
588 |
+
xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max()))
|
589 |
+
yi = self.forward_once(xi)[0] # forward
|
590 |
+
# cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
|
591 |
+
yi[..., :4] /= si # de-scale
|
592 |
+
if fi == 2:
|
593 |
+
yi[..., 1] = img_size[0] - yi[..., 1] # de-flip ud
|
594 |
+
elif fi == 3:
|
595 |
+
yi[..., 0] = img_size[1] - yi[..., 0] # de-flip lr
|
596 |
+
y.append(yi)
|
597 |
+
return torch.cat(y, 1), None # augmented inference, train
|
598 |
+
else:
|
599 |
+
return self.forward_once(x, profile) # single-scale inference, train
|
600 |
+
|
601 |
+
def forward_once(self, x, profile=False):
|
602 |
+
y, dt = [], [] # outputs
|
603 |
+
for m in self.model:
|
604 |
+
if m.f != -1: # if not from previous layer
|
605 |
+
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
|
606 |
+
|
607 |
+
if not hasattr(self, 'traced'):
|
608 |
+
self.traced=False
|
609 |
+
|
610 |
+
if self.traced:
|
611 |
+
if isinstance(m, Detect) or isinstance(m, IDetect) or isinstance(m, IAuxDetect) or isinstance(m, IKeypoint):
|
612 |
+
break
|
613 |
+
|
614 |
+
if profile:
|
615 |
+
c = isinstance(m, (Detect, IDetect, IAuxDetect, IBin))
|
616 |
+
o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPS
|
617 |
+
for _ in range(10):
|
618 |
+
m(x.copy() if c else x)
|
619 |
+
t = time_synchronized()
|
620 |
+
for _ in range(10):
|
621 |
+
m(x.copy() if c else x)
|
622 |
+
dt.append((time_synchronized() - t) * 100)
|
623 |
+
print('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt[-1], m.type))
|
624 |
+
|
625 |
+
x = m(x) # run
|
626 |
+
|
627 |
+
y.append(x if m.i in self.save else None) # save output
|
628 |
+
|
629 |
+
if profile:
|
630 |
+
print('%.1fms total' % sum(dt))
|
631 |
+
return x
|
632 |
+
|
633 |
+
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
|
634 |
+
# https://arxiv.org/abs/1708.02002 section 3.3
|
635 |
+
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
|
636 |
+
m = self.model[-1] # Detect() module
|
637 |
+
for mi, s in zip(m.m, m.stride): # from
|
638 |
+
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
|
639 |
+
b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
|
640 |
+
b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
|
641 |
+
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
|
642 |
+
|
643 |
+
def _initialize_aux_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
|
644 |
+
# https://arxiv.org/abs/1708.02002 section 3.3
|
645 |
+
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
|
646 |
+
m = self.model[-1] # Detect() module
|
647 |
+
for mi, mi2, s in zip(m.m, m.m2, m.stride): # from
|
648 |
+
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
|
649 |
+
b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
|
650 |
+
b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
|
651 |
+
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
|
652 |
+
b2 = mi2.bias.view(m.na, -1) # conv.bias(255) to (3,85)
|
653 |
+
b2.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
|
654 |
+
b2.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
|
655 |
+
mi2.bias = torch.nn.Parameter(b2.view(-1), requires_grad=True)
|
656 |
+
|
657 |
+
def _initialize_biases_bin(self, cf=None): # initialize biases into Detect(), cf is class frequency
|
658 |
+
# https://arxiv.org/abs/1708.02002 section 3.3
|
659 |
+
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
|
660 |
+
m = self.model[-1] # Bin() module
|
661 |
+
bc = m.bin_count
|
662 |
+
for mi, s in zip(m.m, m.stride): # from
|
663 |
+
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
|
664 |
+
old = b[:, (0,1,2,bc+3)].data
|
665 |
+
obj_idx = 2*bc+4
|
666 |
+
b[:, :obj_idx].data += math.log(0.6 / (bc + 1 - 0.99))
|
667 |
+
b[:, obj_idx].data += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
|
668 |
+
b[:, (obj_idx+1):].data += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
|
669 |
+
b[:, (0,1,2,bc+3)].data = old
|
670 |
+
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
|
671 |
+
|
672 |
+
def _initialize_biases_kpt(self, cf=None): # initialize biases into Detect(), cf is class frequency
|
673 |
+
# https://arxiv.org/abs/1708.02002 section 3.3
|
674 |
+
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
|
675 |
+
m = self.model[-1] # Detect() module
|
676 |
+
for mi, s in zip(m.m, m.stride): # from
|
677 |
+
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
|
678 |
+
b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
|
679 |
+
b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
|
680 |
+
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
|
681 |
+
|
682 |
+
def _print_biases(self):
|
683 |
+
m = self.model[-1] # Detect() module
|
684 |
+
for mi in m.m: # from
|
685 |
+
b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85)
|
686 |
+
print(('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean()))
|
687 |
+
|
688 |
+
# def _print_weights(self):
|
689 |
+
# for m in self.model.modules():
|
690 |
+
# if type(m) is Bottleneck:
|
691 |
+
# print('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights
|
692 |
+
|
693 |
+
def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers
|
694 |
+
print('Fusing layers... ')
|
695 |
+
for m in self.model.modules():
|
696 |
+
if isinstance(m, RepConv):
|
697 |
+
#print(f" fuse_repvgg_block")
|
698 |
+
m.fuse_repvgg_block()
|
699 |
+
elif isinstance(m, RepConv_OREPA):
|
700 |
+
#print(f" switch_to_deploy")
|
701 |
+
m.switch_to_deploy()
|
702 |
+
elif type(m) is Conv and hasattr(m, 'bn'):
|
703 |
+
m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
|
704 |
+
delattr(m, 'bn') # remove batchnorm
|
705 |
+
m.forward = m.fuseforward # update forward
|
706 |
+
elif isinstance(m, (IDetect, IAuxDetect)):
|
707 |
+
m.fuse()
|
708 |
+
m.forward = m.fuseforward
|
709 |
+
self.info()
|
710 |
+
return self
|
711 |
+
|
712 |
+
def nms(self, mode=True): # add or remove NMS module
|
713 |
+
present = type(self.model[-1]) is NMS # last layer is NMS
|
714 |
+
if mode and not present:
|
715 |
+
print('Adding NMS... ')
|
716 |
+
m = NMS() # module
|
717 |
+
m.f = -1 # from
|
718 |
+
m.i = self.model[-1].i + 1 # index
|
719 |
+
self.model.add_module(name='%s' % m.i, module=m) # add
|
720 |
+
self.eval()
|
721 |
+
elif not mode and present:
|
722 |
+
print('Removing NMS... ')
|
723 |
+
self.model = self.model[:-1] # remove
|
724 |
+
return self
|
725 |
+
|
726 |
+
def autoshape(self): # add autoShape module
|
727 |
+
print('Adding autoShape... ')
|
728 |
+
m = autoShape(self) # wrap model
|
729 |
+
copy_attr(m, self, include=('yaml', 'nc', 'hyp', 'names', 'stride'), exclude=()) # copy attributes
|
730 |
+
return m
|
731 |
+
|
732 |
+
def info(self, verbose=False, img_size=640): # print model information
|
733 |
+
model_info(self, verbose, img_size)
|
734 |
+
|
735 |
+
|
736 |
+
def parse_model(d, ch): # model_dict, input_channels(3)
|
737 |
+
logger.info('\n%3s%18s%3s%10s %-40s%-30s' % ('', 'from', 'n', 'params', 'module', 'arguments'))
|
738 |
+
anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple']
|
739 |
+
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
|
740 |
+
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
|
741 |
+
|
742 |
+
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
|
743 |
+
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args
|
744 |
+
m = eval(m) if isinstance(m, str) else m # eval strings
|
745 |
+
for j, a in enumerate(args):
|
746 |
+
try:
|
747 |
+
args[j] = eval(a) if isinstance(a, str) else a # eval strings
|
748 |
+
except:
|
749 |
+
pass
|
750 |
+
|
751 |
+
n = max(round(n * gd), 1) if n > 1 else n # depth gain
|
752 |
+
if m in [nn.Conv2d, Conv, RobustConv, RobustConv2, DWConv, GhostConv, RepConv, RepConv_OREPA, DownC,
|
753 |
+
SPP, SPPF, SPPCSPC, GhostSPPCSPC, MixConv2d, Focus, Stem, GhostStem, CrossConv,
|
754 |
+
Bottleneck, BottleneckCSPA, BottleneckCSPB, BottleneckCSPC,
|
755 |
+
RepBottleneck, RepBottleneckCSPA, RepBottleneckCSPB, RepBottleneckCSPC,
|
756 |
+
Res, ResCSPA, ResCSPB, ResCSPC,
|
757 |
+
RepRes, RepResCSPA, RepResCSPB, RepResCSPC,
|
758 |
+
ResX, ResXCSPA, ResXCSPB, ResXCSPC,
|
759 |
+
RepResX, RepResXCSPA, RepResXCSPB, RepResXCSPC,
|
760 |
+
Ghost, GhostCSPA, GhostCSPB, GhostCSPC,
|
761 |
+
SwinTransformerBlock, STCSPA, STCSPB, STCSPC,
|
762 |
+
SwinTransformer2Block, ST2CSPA, ST2CSPB, ST2CSPC]:
|
763 |
+
c1, c2 = ch[f], args[0]
|
764 |
+
if c2 != no: # if not output
|
765 |
+
c2 = make_divisible(c2 * gw, 8)
|
766 |
+
|
767 |
+
args = [c1, c2, *args[1:]]
|
768 |
+
if m in [DownC, SPPCSPC, GhostSPPCSPC,
|
769 |
+
BottleneckCSPA, BottleneckCSPB, BottleneckCSPC,
|
770 |
+
RepBottleneckCSPA, RepBottleneckCSPB, RepBottleneckCSPC,
|
771 |
+
ResCSPA, ResCSPB, ResCSPC,
|
772 |
+
RepResCSPA, RepResCSPB, RepResCSPC,
|
773 |
+
ResXCSPA, ResXCSPB, ResXCSPC,
|
774 |
+
RepResXCSPA, RepResXCSPB, RepResXCSPC,
|
775 |
+
GhostCSPA, GhostCSPB, GhostCSPC,
|
776 |
+
STCSPA, STCSPB, STCSPC,
|
777 |
+
ST2CSPA, ST2CSPB, ST2CSPC]:
|
778 |
+
args.insert(2, n) # number of repeats
|
779 |
+
n = 1
|
780 |
+
elif m is nn.BatchNorm2d:
|
781 |
+
args = [ch[f]]
|
782 |
+
elif m is Concat:
|
783 |
+
c2 = sum([ch[x] for x in f])
|
784 |
+
elif m is Chuncat:
|
785 |
+
c2 = sum([ch[x] for x in f])
|
786 |
+
elif m is Shortcut:
|
787 |
+
c2 = ch[f[0]]
|
788 |
+
elif m is Foldcut:
|
789 |
+
c2 = ch[f] // 2
|
790 |
+
elif m in [Detect, IDetect, IAuxDetect, IBin, IKeypoint]:
|
791 |
+
args.append([ch[x] for x in f])
|
792 |
+
if isinstance(args[1], int): # number of anchors
|
793 |
+
args[1] = [list(range(args[1] * 2))] * len(f)
|
794 |
+
elif m is ReOrg:
|
795 |
+
c2 = ch[f] * 4
|
796 |
+
elif m is Contract:
|
797 |
+
c2 = ch[f] * args[0] ** 2
|
798 |
+
elif m is Expand:
|
799 |
+
c2 = ch[f] // args[0] ** 2
|
800 |
+
else:
|
801 |
+
c2 = ch[f]
|
802 |
+
|
803 |
+
m_ = nn.Sequential(*[m(*args) for _ in range(n)]) if n > 1 else m(*args) # module
|
804 |
+
t = str(m)[8:-2].replace('__main__.', '') # module type
|
805 |
+
np = sum([x.numel() for x in m_.parameters()]) # number params
|
806 |
+
m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
|
807 |
+
logger.info('%3s%18s%3s%10.0f %-40s%-30s' % (i, f, n, np, t, args)) # print
|
808 |
+
save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
|
809 |
+
layers.append(m_)
|
810 |
+
if i == 0:
|
811 |
+
ch = []
|
812 |
+
ch.append(c2)
|
813 |
+
return nn.Sequential(*layers), sorted(save)
|
814 |
+
|
815 |
+
|
816 |
+
if __name__ == '__main__':
|
817 |
+
parser = argparse.ArgumentParser()
|
818 |
+
parser.add_argument('--cfg', type=str, default='yolor-csp-c.yaml', help='model.yaml')
|
819 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
820 |
+
parser.add_argument('--profile', action='store_true', help='profile model speed')
|
821 |
+
opt = parser.parse_args()
|
822 |
+
opt.cfg = check_file(opt.cfg) # check file
|
823 |
+
set_logging()
|
824 |
+
device = select_device(opt.device)
|
825 |
+
|
826 |
+
# Create model
|
827 |
+
model = Model(opt.cfg).to(device)
|
828 |
+
model.train()
|
829 |
+
|
830 |
+
if opt.profile:
|
831 |
+
img = torch.rand(1, 3, 640, 640).to(device)
|
832 |
+
y = model(img, profile=True)
|
833 |
+
|
834 |
+
# Profile
|
835 |
+
# img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 640, 640).to(device)
|
836 |
+
# y = model(img, profile=True)
|
837 |
+
|
838 |
+
# Tensorboard
|
839 |
+
# from torch.utils.tensorboard import SummaryWriter
|
840 |
+
# tb_writer = SummaryWriter()
|
841 |
+
# print("Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/")
|
842 |
+
# tb_writer.add_graph(model.model, img) # add model to tensorboard
|
843 |
+
# tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard
|
infer/yolov7/utils/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# init
|
infer/yolov7/utils/activations.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Activation functions
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
import torch.nn.functional as F
|
6 |
+
|
7 |
+
|
8 |
+
# SiLU https://arxiv.org/pdf/1606.08415.pdf ----------------------------------------------------------------------------
|
9 |
+
class SiLU(nn.Module): # export-friendly version of nn.SiLU()
|
10 |
+
@staticmethod
|
11 |
+
def forward(x):
|
12 |
+
return x * torch.sigmoid(x)
|
13 |
+
|
14 |
+
|
15 |
+
class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
|
16 |
+
@staticmethod
|
17 |
+
def forward(x):
|
18 |
+
# return x * F.hardsigmoid(x) # for torchscript and CoreML
|
19 |
+
return x * F.hardtanh(x + 3, 0., 6.) / 6. # for torchscript, CoreML and ONNX
|
20 |
+
|
21 |
+
|
22 |
+
class MemoryEfficientSwish(nn.Module):
|
23 |
+
class F(torch.autograd.Function):
|
24 |
+
@staticmethod
|
25 |
+
def forward(ctx, x):
|
26 |
+
ctx.save_for_backward(x)
|
27 |
+
return x * torch.sigmoid(x)
|
28 |
+
|
29 |
+
@staticmethod
|
30 |
+
def backward(ctx, grad_output):
|
31 |
+
x = ctx.saved_tensors[0]
|
32 |
+
sx = torch.sigmoid(x)
|
33 |
+
return grad_output * (sx * (1 + x * (1 - sx)))
|
34 |
+
|
35 |
+
def forward(self, x):
|
36 |
+
return self.F.apply(x)
|
37 |
+
|
38 |
+
|
39 |
+
# Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
|
40 |
+
class Mish(nn.Module):
|
41 |
+
@staticmethod
|
42 |
+
def forward(x):
|
43 |
+
return x * F.softplus(x).tanh()
|
44 |
+
|
45 |
+
|
46 |
+
class MemoryEfficientMish(nn.Module):
|
47 |
+
class F(torch.autograd.Function):
|
48 |
+
@staticmethod
|
49 |
+
def forward(ctx, x):
|
50 |
+
ctx.save_for_backward(x)
|
51 |
+
return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
|
52 |
+
|
53 |
+
@staticmethod
|
54 |
+
def backward(ctx, grad_output):
|
55 |
+
x = ctx.saved_tensors[0]
|
56 |
+
sx = torch.sigmoid(x)
|
57 |
+
fx = F.softplus(x).tanh()
|
58 |
+
return grad_output * (fx + x * sx * (1 - fx * fx))
|
59 |
+
|
60 |
+
def forward(self, x):
|
61 |
+
return self.F.apply(x)
|
62 |
+
|
63 |
+
|
64 |
+
# FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------
|
65 |
+
class FReLU(nn.Module):
|
66 |
+
def __init__(self, c1, k=3): # ch_in, kernel
|
67 |
+
super().__init__()
|
68 |
+
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)
|
69 |
+
self.bn = nn.BatchNorm2d(c1)
|
70 |
+
|
71 |
+
def forward(self, x):
|
72 |
+
return torch.max(x, self.bn(self.conv(x)))
|
infer/yolov7/utils/add_nms.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import onnx
|
3 |
+
from onnx import shape_inference
|
4 |
+
try:
|
5 |
+
import onnx_graphsurgeon as gs
|
6 |
+
except Exception as e:
|
7 |
+
print('Import onnx_graphsurgeon failure: %s' % e)
|
8 |
+
|
9 |
+
import logging
|
10 |
+
|
11 |
+
LOGGER = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
class RegisterNMS(object):
|
14 |
+
def __init__(
|
15 |
+
self,
|
16 |
+
onnx_model_path: str,
|
17 |
+
precision: str = "fp32",
|
18 |
+
):
|
19 |
+
|
20 |
+
self.graph = gs.import_onnx(onnx.load(onnx_model_path))
|
21 |
+
assert self.graph
|
22 |
+
LOGGER.info("ONNX graph created successfully")
|
23 |
+
# Fold constants via ONNX-GS that PyTorch2ONNX may have missed
|
24 |
+
self.graph.fold_constants()
|
25 |
+
self.precision = precision
|
26 |
+
self.batch_size = 1
|
27 |
+
def infer(self):
|
28 |
+
"""
|
29 |
+
Sanitize the graph by cleaning any unconnected nodes, do a topological resort,
|
30 |
+
and fold constant inputs values. When possible, run shape inference on the
|
31 |
+
ONNX graph to determine tensor shapes.
|
32 |
+
"""
|
33 |
+
for _ in range(3):
|
34 |
+
count_before = len(self.graph.nodes)
|
35 |
+
|
36 |
+
self.graph.cleanup().toposort()
|
37 |
+
try:
|
38 |
+
for node in self.graph.nodes:
|
39 |
+
for o in node.outputs:
|
40 |
+
o.shape = None
|
41 |
+
model = gs.export_onnx(self.graph)
|
42 |
+
model = shape_inference.infer_shapes(model)
|
43 |
+
self.graph = gs.import_onnx(model)
|
44 |
+
except Exception as e:
|
45 |
+
LOGGER.info(f"Shape inference could not be performed at this time:\n{e}")
|
46 |
+
try:
|
47 |
+
self.graph.fold_constants(fold_shapes=True)
|
48 |
+
except TypeError as e:
|
49 |
+
LOGGER.error(
|
50 |
+
"This version of ONNX GraphSurgeon does not support folding shapes, "
|
51 |
+
f"please upgrade your onnx_graphsurgeon module. Error:\n{e}"
|
52 |
+
)
|
53 |
+
raise
|
54 |
+
|
55 |
+
count_after = len(self.graph.nodes)
|
56 |
+
if count_before == count_after:
|
57 |
+
# No new folding occurred in this iteration, so we can stop for now.
|
58 |
+
break
|
59 |
+
|
60 |
+
def save(self, output_path):
|
61 |
+
"""
|
62 |
+
Save the ONNX model to the given location.
|
63 |
+
Args:
|
64 |
+
output_path: Path pointing to the location where to write
|
65 |
+
out the updated ONNX model.
|
66 |
+
"""
|
67 |
+
self.graph.cleanup().toposort()
|
68 |
+
model = gs.export_onnx(self.graph)
|
69 |
+
onnx.save(model, output_path)
|
70 |
+
LOGGER.info(f"Saved ONNX model to {output_path}")
|
71 |
+
|
72 |
+
def register_nms(
|
73 |
+
self,
|
74 |
+
*,
|
75 |
+
score_thresh: float = 0.25,
|
76 |
+
nms_thresh: float = 0.45,
|
77 |
+
detections_per_img: int = 100,
|
78 |
+
):
|
79 |
+
"""
|
80 |
+
Register the ``EfficientNMS_TRT`` plugin node.
|
81 |
+
NMS expects these shapes for its input tensors:
|
82 |
+
- box_net: [batch_size, number_boxes, 4]
|
83 |
+
- class_net: [batch_size, number_boxes, number_labels]
|
84 |
+
Args:
|
85 |
+
score_thresh (float): The scalar threshold for score (low scoring boxes are removed).
|
86 |
+
nms_thresh (float): The scalar threshold for IOU (new boxes that have high IOU
|
87 |
+
overlap with previously selected boxes are removed).
|
88 |
+
detections_per_img (int): Number of best detections to keep after NMS.
|
89 |
+
"""
|
90 |
+
|
91 |
+
self.infer()
|
92 |
+
# Find the concat node at the end of the network
|
93 |
+
op_inputs = self.graph.outputs
|
94 |
+
op = "EfficientNMS_TRT"
|
95 |
+
attrs = {
|
96 |
+
"plugin_version": "1",
|
97 |
+
"background_class": -1, # no background class
|
98 |
+
"max_output_boxes": detections_per_img,
|
99 |
+
"score_threshold": score_thresh,
|
100 |
+
"iou_threshold": nms_thresh,
|
101 |
+
"score_activation": False,
|
102 |
+
"box_coding": 0,
|
103 |
+
}
|
104 |
+
|
105 |
+
if self.precision == "fp32":
|
106 |
+
dtype_output = np.float32
|
107 |
+
elif self.precision == "fp16":
|
108 |
+
dtype_output = np.float16
|
109 |
+
else:
|
110 |
+
raise NotImplementedError(f"Currently not supports precision: {self.precision}")
|
111 |
+
|
112 |
+
# NMS Outputs
|
113 |
+
output_num_detections = gs.Variable(
|
114 |
+
name="num_dets",
|
115 |
+
dtype=np.int32,
|
116 |
+
shape=[self.batch_size, 1],
|
117 |
+
) # A scalar indicating the number of valid detections per batch image.
|
118 |
+
output_boxes = gs.Variable(
|
119 |
+
name="det_boxes",
|
120 |
+
dtype=dtype_output,
|
121 |
+
shape=[self.batch_size, detections_per_img, 4],
|
122 |
+
)
|
123 |
+
output_scores = gs.Variable(
|
124 |
+
name="det_scores",
|
125 |
+
dtype=dtype_output,
|
126 |
+
shape=[self.batch_size, detections_per_img],
|
127 |
+
)
|
128 |
+
output_labels = gs.Variable(
|
129 |
+
name="det_classes",
|
130 |
+
dtype=np.int32,
|
131 |
+
shape=[self.batch_size, detections_per_img],
|
132 |
+
)
|
133 |
+
|
134 |
+
op_outputs = [output_num_detections, output_boxes, output_scores, output_labels]
|
135 |
+
|
136 |
+
# Create the NMS Plugin node with the selected inputs. The outputs of the node will also
|
137 |
+
# become the final outputs of the graph.
|
138 |
+
self.graph.layer(op=op, name="batched_nms", inputs=op_inputs, outputs=op_outputs, attrs=attrs)
|
139 |
+
LOGGER.info(f"Created NMS plugin '{op}' with attributes: {attrs}")
|
140 |
+
|
141 |
+
self.graph.outputs = op_outputs
|
142 |
+
|
143 |
+
self.infer()
|
144 |
+
|
145 |
+
def save(self, output_path):
|
146 |
+
"""
|
147 |
+
Save the ONNX model to the given location.
|
148 |
+
Args:
|
149 |
+
output_path: Path pointing to the location where to write
|
150 |
+
out the updated ONNX model.
|
151 |
+
"""
|
152 |
+
self.graph.cleanup().toposort()
|
153 |
+
model = gs.export_onnx(self.graph)
|
154 |
+
onnx.save(model, output_path)
|
155 |
+
LOGGER.info(f"Saved ONNX model to {output_path}")
|
infer/yolov7/utils/autoanchor.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Auto-anchor utils
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import yaml
|
6 |
+
from scipy.cluster.vq import kmeans
|
7 |
+
from tqdm import tqdm
|
8 |
+
|
9 |
+
from infer.yolov7.utils.general import colorstr
|
10 |
+
|
11 |
+
|
12 |
+
def check_anchor_order(m):
|
13 |
+
# Check anchor order against stride order for YOLO Detect() module m, and correct if necessary
|
14 |
+
a = m.anchor_grid.prod(-1).view(-1) # anchor area
|
15 |
+
da = a[-1] - a[0] # delta a
|
16 |
+
ds = m.stride[-1] - m.stride[0] # delta s
|
17 |
+
if da.sign() != ds.sign(): # same order
|
18 |
+
print('Reversing anchor order')
|
19 |
+
m.anchors[:] = m.anchors.flip(0)
|
20 |
+
m.anchor_grid[:] = m.anchor_grid.flip(0)
|
21 |
+
|
22 |
+
|
23 |
+
def check_anchors(dataset, model, thr=4.0, imgsz=640):
|
24 |
+
# Check anchor fit to data, recompute if necessary
|
25 |
+
prefix = colorstr('autoanchor: ')
|
26 |
+
print(f'\n{prefix}Analyzing anchors... ', end='')
|
27 |
+
m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect()
|
28 |
+
shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
29 |
+
scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale
|
30 |
+
wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh
|
31 |
+
|
32 |
+
def metric(k): # compute metric
|
33 |
+
r = wh[:, None] / k[None]
|
34 |
+
x = torch.min(r, 1. / r).min(2)[0] # ratio metric
|
35 |
+
best = x.max(1)[0] # best_x
|
36 |
+
aat = (x > 1. / thr).float().sum(1).mean() # anchors above threshold
|
37 |
+
bpr = (best > 1. / thr).float().mean() # best possible recall
|
38 |
+
return bpr, aat
|
39 |
+
|
40 |
+
anchors = m.anchor_grid.clone().cpu().view(-1, 2) # current anchors
|
41 |
+
bpr, aat = metric(anchors)
|
42 |
+
print(f'anchors/target = {aat:.2f}, Best Possible Recall (BPR) = {bpr:.4f}', end='')
|
43 |
+
if bpr < 0.98: # threshold to recompute
|
44 |
+
print('. Attempting to improve anchors, please wait...')
|
45 |
+
na = m.anchor_grid.numel() // 2 # number of anchors
|
46 |
+
try:
|
47 |
+
anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False)
|
48 |
+
except Exception as e:
|
49 |
+
print(f'{prefix}ERROR: {e}')
|
50 |
+
new_bpr = metric(anchors)[0]
|
51 |
+
if new_bpr > bpr: # replace anchors
|
52 |
+
anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors)
|
53 |
+
m.anchor_grid[:] = anchors.clone().view_as(m.anchor_grid) # for inference
|
54 |
+
check_anchor_order(m)
|
55 |
+
m.anchors[:] = anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1) # loss
|
56 |
+
print(f'{prefix}New anchors saved to model. Update model *.yaml to use these anchors in the future.')
|
57 |
+
else:
|
58 |
+
print(f'{prefix}Original anchors better than new anchors. Proceeding with original anchors.')
|
59 |
+
print('') # newline
|
60 |
+
|
61 |
+
|
62 |
+
def kmean_anchors(path='./data/coco.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True):
|
63 |
+
""" Creates kmeans-evolved anchors from training dataset
|
64 |
+
|
65 |
+
Arguments:
|
66 |
+
path: path to dataset *.yaml, or a loaded dataset
|
67 |
+
n: number of anchors
|
68 |
+
img_size: image size used for training
|
69 |
+
thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
|
70 |
+
gen: generations to evolve anchors using genetic algorithm
|
71 |
+
verbose: print all results
|
72 |
+
|
73 |
+
Return:
|
74 |
+
k: kmeans evolved anchors
|
75 |
+
|
76 |
+
Usage:
|
77 |
+
from utils.autoanchor import *; _ = kmean_anchors()
|
78 |
+
"""
|
79 |
+
thr = 1. / thr
|
80 |
+
prefix = colorstr('autoanchor: ')
|
81 |
+
|
82 |
+
def metric(k, wh): # compute metrics
|
83 |
+
r = wh[:, None] / k[None]
|
84 |
+
x = torch.min(r, 1. / r).min(2)[0] # ratio metric
|
85 |
+
# x = wh_iou(wh, torch.tensor(k)) # iou metric
|
86 |
+
return x, x.max(1)[0] # x, best_x
|
87 |
+
|
88 |
+
def anchor_fitness(k): # mutation fitness
|
89 |
+
_, best = metric(torch.tensor(k, dtype=torch.float32), wh)
|
90 |
+
return (best * (best > thr).float()).mean() # fitness
|
91 |
+
|
92 |
+
def print_results(k):
|
93 |
+
k = k[np.argsort(k.prod(1))] # sort small to large
|
94 |
+
x, best = metric(k, wh0)
|
95 |
+
bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
|
96 |
+
print(f'{prefix}thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr')
|
97 |
+
print(f'{prefix}n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, '
|
98 |
+
f'past_thr={x[x > thr].mean():.3f}-mean: ', end='')
|
99 |
+
for i, x in enumerate(k):
|
100 |
+
print('%i,%i' % (round(x[0]), round(x[1])), end=', ' if i < len(k) - 1 else '\n') # use in *.cfg
|
101 |
+
return k
|
102 |
+
|
103 |
+
if isinstance(path, str): # *.yaml file
|
104 |
+
with open(path) as f:
|
105 |
+
data_dict = yaml.load(f, Loader=yaml.SafeLoader) # model dict
|
106 |
+
from utils.datasets import LoadImagesAndLabels
|
107 |
+
dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
|
108 |
+
else:
|
109 |
+
dataset = path # dataset
|
110 |
+
|
111 |
+
# Get label wh
|
112 |
+
shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
113 |
+
wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh
|
114 |
+
|
115 |
+
# Filter
|
116 |
+
i = (wh0 < 3.0).any(1).sum()
|
117 |
+
if i:
|
118 |
+
print(f'{prefix}WARNING: Extremely small objects found. {i} of {len(wh0)} labels are < 3 pixels in size.')
|
119 |
+
wh = wh0[(wh0 >= 2.0).any(1)] # filter > 2 pixels
|
120 |
+
# wh = wh * (np.random.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1
|
121 |
+
|
122 |
+
# Kmeans calculation
|
123 |
+
print(f'{prefix}Running kmeans for {n} anchors on {len(wh)} points...')
|
124 |
+
s = wh.std(0) # sigmas for whitening
|
125 |
+
k, dist = kmeans(wh / s, n, iter=30) # points, mean distance
|
126 |
+
assert len(k) == n, print(f'{prefix}ERROR: scipy.cluster.vq.kmeans requested {n} points but returned only {len(k)}')
|
127 |
+
k *= s
|
128 |
+
wh = torch.tensor(wh, dtype=torch.float32) # filtered
|
129 |
+
wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered
|
130 |
+
k = print_results(k)
|
131 |
+
|
132 |
+
# Plot
|
133 |
+
# k, d = [None] * 20, [None] * 20
|
134 |
+
# for i in tqdm(range(1, 21)):
|
135 |
+
# k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
|
136 |
+
# fig, ax = plt.subplots(1, 2, figsize=(14, 7), tight_layout=True)
|
137 |
+
# ax = ax.ravel()
|
138 |
+
# ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.')
|
139 |
+
# fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh
|
140 |
+
# ax[0].hist(wh[wh[:, 0]<100, 0],400)
|
141 |
+
# ax[1].hist(wh[wh[:, 1]<100, 1],400)
|
142 |
+
# fig.savefig('wh.png', dpi=200)
|
143 |
+
|
144 |
+
# Evolve
|
145 |
+
npr = np.random
|
146 |
+
f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma
|
147 |
+
pbar = tqdm(range(gen), desc=f'{prefix}Evolving anchors with Genetic Algorithm:') # progress bar
|
148 |
+
for _ in pbar:
|
149 |
+
v = np.ones(sh)
|
150 |
+
while (v == 1).all(): # mutate until a change occurs (prevent duplicates)
|
151 |
+
v = ((npr.random(sh) < mp) * npr.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0)
|
152 |
+
kg = (k.copy() * v).clip(min=2.0)
|
153 |
+
fg = anchor_fitness(kg)
|
154 |
+
if fg > f:
|
155 |
+
f, k = fg, kg.copy()
|
156 |
+
pbar.desc = f'{prefix}Evolving anchors with Genetic Algorithm: fitness = {f:.4f}'
|
157 |
+
if verbose:
|
158 |
+
print_results(k)
|
159 |
+
|
160 |
+
return print_results(k)
|
infer/yolov7/utils/aws/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
#init
|
infer/yolov7/utils/aws/mime.sh
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AWS EC2 instance startup 'MIME' script https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/
|
2 |
+
# This script will run on every instance restart, not only on first start
|
3 |
+
# --- DO NOT COPY ABOVE COMMENTS WHEN PASTING INTO USERDATA ---
|
4 |
+
|
5 |
+
Content-Type: multipart/mixed; boundary="//"
|
6 |
+
MIME-Version: 1.0
|
7 |
+
|
8 |
+
--//
|
9 |
+
Content-Type: text/cloud-config; charset="us-ascii"
|
10 |
+
MIME-Version: 1.0
|
11 |
+
Content-Transfer-Encoding: 7bit
|
12 |
+
Content-Disposition: attachment; filename="cloud-config.txt"
|
13 |
+
|
14 |
+
#cloud-config
|
15 |
+
cloud_final_modules:
|
16 |
+
- [scripts-user, always]
|
17 |
+
|
18 |
+
--//
|
19 |
+
Content-Type: text/x-shellscript; charset="us-ascii"
|
20 |
+
MIME-Version: 1.0
|
21 |
+
Content-Transfer-Encoding: 7bit
|
22 |
+
Content-Disposition: attachment; filename="userdata.txt"
|
23 |
+
|
24 |
+
#!/bin/bash
|
25 |
+
# --- paste contents of userdata.sh here ---
|
26 |
+
--//
|
infer/yolov7/utils/aws/resume.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Resume all interrupted trainings in yolor/ dir including DDP trainings
|
2 |
+
# Usage: $ python utils/aws/resume.py
|
3 |
+
|
4 |
+
import os
|
5 |
+
import sys
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
import torch
|
9 |
+
import yaml
|
10 |
+
|
11 |
+
sys.path.append('./') # to run '$ python *.py' files in subdirectories
|
12 |
+
|
13 |
+
port = 0 # --master_port
|
14 |
+
path = Path('').resolve()
|
15 |
+
for last in path.rglob('*/**/last.pt'):
|
16 |
+
ckpt = torch.load(last)
|
17 |
+
if ckpt['optimizer'] is None:
|
18 |
+
continue
|
19 |
+
|
20 |
+
# Load opt.yaml
|
21 |
+
with open(last.parent.parent / 'opt.yaml') as f:
|
22 |
+
opt = yaml.load(f, Loader=yaml.SafeLoader)
|
23 |
+
|
24 |
+
# Get device count
|
25 |
+
d = opt['device'].split(',') # devices
|
26 |
+
nd = len(d) # number of devices
|
27 |
+
ddp = nd > 1 or (nd == 0 and torch.cuda.device_count() > 1) # distributed data parallel
|
28 |
+
|
29 |
+
if ddp: # multi-GPU
|
30 |
+
port += 1
|
31 |
+
cmd = f'python -m torch.distributed.launch --nproc_per_node {nd} --master_port {port} train.py --resume {last}'
|
32 |
+
else: # single-GPU
|
33 |
+
cmd = f'python train.py --resume {last}'
|
34 |
+
|
35 |
+
cmd += ' > /dev/null 2>&1 &' # redirect output to dev/null and run in daemon thread
|
36 |
+
print(cmd)
|
37 |
+
os.system(cmd)
|
infer/yolov7/utils/aws/userdata.sh
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
# AWS EC2 instance startup script https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
|
3 |
+
# This script will run only once on first instance start (for a re-start script see mime.sh)
|
4 |
+
# /home/ubuntu (ubuntu) or /home/ec2-user (amazon-linux) is working dir
|
5 |
+
# Use >300 GB SSD
|
6 |
+
|
7 |
+
cd home/ubuntu
|
8 |
+
if [ ! -d yolor ]; then
|
9 |
+
echo "Running first-time script." # install dependencies, download COCO, pull Docker
|
10 |
+
git clone -b main https://github.com/WongKinYiu/yolov7 && sudo chmod -R 777 yolov7
|
11 |
+
cd yolov7
|
12 |
+
bash data/scripts/get_coco.sh && echo "Data done." &
|
13 |
+
sudo docker pull nvcr.io/nvidia/pytorch:21.08-py3 && echo "Docker done." &
|
14 |
+
python -m pip install --upgrade pip && pip install -r requirements.txt && python detect.py && echo "Requirements done." &
|
15 |
+
wait && echo "All tasks done." # finish background tasks
|
16 |
+
else
|
17 |
+
echo "Running re-start script." # resume interrupted runs
|
18 |
+
i=0
|
19 |
+
list=$(sudo docker ps -qa) # container list i.e. $'one\ntwo\nthree\nfour'
|
20 |
+
while IFS= read -r id; do
|
21 |
+
((i++))
|
22 |
+
echo "restarting container $i: $id"
|
23 |
+
sudo docker start $id
|
24 |
+
# sudo docker exec -it $id python train.py --resume # single-GPU
|
25 |
+
sudo docker exec -d $id python utils/aws/resume.py # multi-scenario
|
26 |
+
done <<<"$list"
|
27 |
+
fi
|
infer/yolov7/utils/datasets.py
ADDED
@@ -0,0 +1,1320 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Dataset utils and dataloaders
|
2 |
+
|
3 |
+
import glob
|
4 |
+
import logging
|
5 |
+
import math
|
6 |
+
import os
|
7 |
+
import random
|
8 |
+
import shutil
|
9 |
+
import time
|
10 |
+
from itertools import repeat
|
11 |
+
from multiprocessing.pool import ThreadPool
|
12 |
+
from pathlib import Path
|
13 |
+
from threading import Thread
|
14 |
+
|
15 |
+
import cv2
|
16 |
+
import numpy as np
|
17 |
+
import torch
|
18 |
+
import torch.nn.functional as F
|
19 |
+
from PIL import Image, ExifTags
|
20 |
+
from torch.utils.data import Dataset
|
21 |
+
from tqdm import tqdm
|
22 |
+
|
23 |
+
import pickle
|
24 |
+
from copy import deepcopy
|
25 |
+
#from pycocotools import mask as maskUtils
|
26 |
+
from torchvision.utils import save_image
|
27 |
+
from torchvision.ops import roi_pool, roi_align, ps_roi_pool, ps_roi_align
|
28 |
+
|
29 |
+
from infer.yolov7.utils.general import check_requirements, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, segment2box, segments2boxes, \
|
30 |
+
resample_segments, clean_str
|
31 |
+
from infer.yolov7.utils.torch_utils import torch_distributed_zero_first
|
32 |
+
|
33 |
+
# Parameters
|
34 |
+
help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
|
35 |
+
img_formats = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng', 'webp', 'mpo'] # acceptable image suffixes
|
36 |
+
vid_formats = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv'] # acceptable video suffixes
|
37 |
+
logger = logging.getLogger(__name__)
|
38 |
+
|
39 |
+
# Get orientation exif tag
|
40 |
+
for orientation in ExifTags.TAGS.keys():
|
41 |
+
if ExifTags.TAGS[orientation] == 'Orientation':
|
42 |
+
break
|
43 |
+
|
44 |
+
|
45 |
+
def get_hash(files):
|
46 |
+
# Returns a single hash value of a list of files
|
47 |
+
return sum(os.path.getsize(f) for f in files if os.path.isfile(f))
|
48 |
+
|
49 |
+
|
50 |
+
def exif_size(img):
|
51 |
+
# Returns exif-corrected PIL size
|
52 |
+
s = img.size # (width, height)
|
53 |
+
try:
|
54 |
+
rotation = dict(img._getexif().items())[orientation]
|
55 |
+
if rotation == 6: # rotation 270
|
56 |
+
s = (s[1], s[0])
|
57 |
+
elif rotation == 8: # rotation 90
|
58 |
+
s = (s[1], s[0])
|
59 |
+
except:
|
60 |
+
pass
|
61 |
+
|
62 |
+
return s
|
63 |
+
|
64 |
+
|
65 |
+
def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False,
|
66 |
+
rank=-1, world_size=1, workers=8, image_weights=False, quad=False, prefix=''):
|
67 |
+
# Make sure only the first process in DDP process the dataset first, and the following others can use the cache
|
68 |
+
with torch_distributed_zero_first(rank):
|
69 |
+
dataset = LoadImagesAndLabels(path, imgsz, batch_size,
|
70 |
+
augment=augment, # augment images
|
71 |
+
hyp=hyp, # augmentation hyperparameters
|
72 |
+
rect=rect, # rectangular training
|
73 |
+
cache_images=cache,
|
74 |
+
single_cls=opt.single_cls,
|
75 |
+
stride=int(stride),
|
76 |
+
pad=pad,
|
77 |
+
image_weights=image_weights,
|
78 |
+
prefix=prefix)
|
79 |
+
|
80 |
+
batch_size = min(batch_size, len(dataset))
|
81 |
+
nw = min([os.cpu_count() // world_size, batch_size if batch_size > 1 else 0, workers]) # number of workers
|
82 |
+
sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None
|
83 |
+
loader = torch.utils.data.DataLoader if image_weights else InfiniteDataLoader
|
84 |
+
# Use torch.utils.data.DataLoader() if dataset.properties will update during training else InfiniteDataLoader()
|
85 |
+
dataloader = loader(dataset,
|
86 |
+
batch_size=batch_size,
|
87 |
+
num_workers=nw,
|
88 |
+
sampler=sampler,
|
89 |
+
pin_memory=True,
|
90 |
+
collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn)
|
91 |
+
return dataloader, dataset
|
92 |
+
|
93 |
+
|
94 |
+
class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
|
95 |
+
""" Dataloader that reuses workers
|
96 |
+
|
97 |
+
Uses same syntax as vanilla DataLoader
|
98 |
+
"""
|
99 |
+
|
100 |
+
def __init__(self, *args, **kwargs):
|
101 |
+
super().__init__(*args, **kwargs)
|
102 |
+
object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler))
|
103 |
+
self.iterator = super().__iter__()
|
104 |
+
|
105 |
+
def __len__(self):
|
106 |
+
return len(self.batch_sampler.sampler)
|
107 |
+
|
108 |
+
def __iter__(self):
|
109 |
+
for i in range(len(self)):
|
110 |
+
yield next(self.iterator)
|
111 |
+
|
112 |
+
|
113 |
+
class _RepeatSampler(object):
|
114 |
+
""" Sampler that repeats forever
|
115 |
+
|
116 |
+
Args:
|
117 |
+
sampler (Sampler)
|
118 |
+
"""
|
119 |
+
|
120 |
+
def __init__(self, sampler):
|
121 |
+
self.sampler = sampler
|
122 |
+
|
123 |
+
def __iter__(self):
|
124 |
+
while True:
|
125 |
+
yield from iter(self.sampler)
|
126 |
+
|
127 |
+
|
128 |
+
class LoadImages: # for inference
|
129 |
+
def __init__(self, path, img_size=640, stride=32):
|
130 |
+
p = str(Path(path).absolute()) # os-agnostic absolute path
|
131 |
+
if '*' in p:
|
132 |
+
files = sorted(glob.glob(p, recursive=True)) # glob
|
133 |
+
elif os.path.isdir(p):
|
134 |
+
files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir
|
135 |
+
elif os.path.isfile(p):
|
136 |
+
files = [p] # files
|
137 |
+
else:
|
138 |
+
raise Exception(f'ERROR: {p} does not exist')
|
139 |
+
|
140 |
+
images = [x for x in files if x.split('.')[-1].lower() in img_formats]
|
141 |
+
videos = [x for x in files if x.split('.')[-1].lower() in vid_formats]
|
142 |
+
ni, nv = len(images), len(videos)
|
143 |
+
|
144 |
+
self.img_size = img_size
|
145 |
+
self.stride = stride
|
146 |
+
self.files = images + videos
|
147 |
+
self.nf = ni + nv # number of files
|
148 |
+
self.video_flag = [False] * ni + [True] * nv
|
149 |
+
self.mode = 'image'
|
150 |
+
if any(videos):
|
151 |
+
self.new_video(videos[0]) # new video
|
152 |
+
else:
|
153 |
+
self.cap = None
|
154 |
+
assert self.nf > 0, f'No images or videos found in {p}. ' \
|
155 |
+
f'Supported formats are:\nimages: {img_formats}\nvideos: {vid_formats}'
|
156 |
+
|
157 |
+
def __iter__(self):
|
158 |
+
self.count = 0
|
159 |
+
return self
|
160 |
+
|
161 |
+
def __next__(self):
|
162 |
+
if self.count == self.nf:
|
163 |
+
raise StopIteration
|
164 |
+
path = self.files[self.count]
|
165 |
+
|
166 |
+
if self.video_flag[self.count]:
|
167 |
+
# Read video
|
168 |
+
self.mode = 'video'
|
169 |
+
ret_val, img0 = self.cap.read()
|
170 |
+
if not ret_val:
|
171 |
+
self.count += 1
|
172 |
+
self.cap.release()
|
173 |
+
if self.count == self.nf: # last video
|
174 |
+
raise StopIteration
|
175 |
+
else:
|
176 |
+
path = self.files[self.count]
|
177 |
+
self.new_video(path)
|
178 |
+
ret_val, img0 = self.cap.read()
|
179 |
+
|
180 |
+
self.frame += 1
|
181 |
+
print(f'video {self.count + 1}/{self.nf} ({self.frame}/{self.nframes}) {path}: ', end='')
|
182 |
+
|
183 |
+
else:
|
184 |
+
# Read image
|
185 |
+
self.count += 1
|
186 |
+
img0 = cv2.imread(path) # BGR
|
187 |
+
assert img0 is not None, 'Image Not Found ' + path
|
188 |
+
#print(f'image {self.count}/{self.nf} {path}: ', end='')
|
189 |
+
|
190 |
+
# Padded resize
|
191 |
+
img = letterbox(img0, self.img_size, stride=self.stride)[0]
|
192 |
+
|
193 |
+
# Convert
|
194 |
+
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
|
195 |
+
img = np.ascontiguousarray(img)
|
196 |
+
|
197 |
+
return path, img, img0, self.cap
|
198 |
+
|
199 |
+
def new_video(self, path):
|
200 |
+
self.frame = 0
|
201 |
+
self.cap = cv2.VideoCapture(path)
|
202 |
+
self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
203 |
+
|
204 |
+
def __len__(self):
|
205 |
+
return self.nf # number of files
|
206 |
+
|
207 |
+
|
208 |
+
class LoadWebcam: # for inference
|
209 |
+
def __init__(self, pipe='0', img_size=640, stride=32):
|
210 |
+
self.img_size = img_size
|
211 |
+
self.stride = stride
|
212 |
+
|
213 |
+
if pipe.isnumeric():
|
214 |
+
pipe = eval(pipe) # local camera
|
215 |
+
# pipe = 'rtsp://192.168.1.64/1' # IP camera
|
216 |
+
# pipe = 'rtsp://username:password@192.168.1.64/1' # IP camera with login
|
217 |
+
# pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera
|
218 |
+
|
219 |
+
self.pipe = pipe
|
220 |
+
self.cap = cv2.VideoCapture(pipe) # video capture object
|
221 |
+
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size
|
222 |
+
|
223 |
+
def __iter__(self):
|
224 |
+
self.count = -1
|
225 |
+
return self
|
226 |
+
|
227 |
+
def __next__(self):
|
228 |
+
self.count += 1
|
229 |
+
if cv2.waitKey(1) == ord('q'): # q to quit
|
230 |
+
self.cap.release()
|
231 |
+
cv2.destroyAllWindows()
|
232 |
+
raise StopIteration
|
233 |
+
|
234 |
+
# Read frame
|
235 |
+
if self.pipe == 0: # local camera
|
236 |
+
ret_val, img0 = self.cap.read()
|
237 |
+
img0 = cv2.flip(img0, 1) # flip left-right
|
238 |
+
else: # IP camera
|
239 |
+
n = 0
|
240 |
+
while True:
|
241 |
+
n += 1
|
242 |
+
self.cap.grab()
|
243 |
+
if n % 30 == 0: # skip frames
|
244 |
+
ret_val, img0 = self.cap.retrieve()
|
245 |
+
if ret_val:
|
246 |
+
break
|
247 |
+
|
248 |
+
# Print
|
249 |
+
assert ret_val, f'Camera Error {self.pipe}'
|
250 |
+
img_path = 'webcam.jpg'
|
251 |
+
print(f'webcam {self.count}: ', end='')
|
252 |
+
|
253 |
+
# Padded resize
|
254 |
+
img = letterbox(img0, self.img_size, stride=self.stride)[0]
|
255 |
+
|
256 |
+
# Convert
|
257 |
+
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
|
258 |
+
img = np.ascontiguousarray(img)
|
259 |
+
|
260 |
+
return img_path, img, img0, None
|
261 |
+
|
262 |
+
def __len__(self):
|
263 |
+
return 0
|
264 |
+
|
265 |
+
|
266 |
+
class LoadStreams: # multiple IP or RTSP cameras
|
267 |
+
def __init__(self, sources='streams.txt', img_size=640, stride=32):
|
268 |
+
self.mode = 'stream'
|
269 |
+
self.img_size = img_size
|
270 |
+
self.stride = stride
|
271 |
+
|
272 |
+
if os.path.isfile(sources):
|
273 |
+
with open(sources, 'r') as f:
|
274 |
+
sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
|
275 |
+
else:
|
276 |
+
sources = [sources]
|
277 |
+
|
278 |
+
n = len(sources)
|
279 |
+
self.imgs = [None] * n
|
280 |
+
self.sources = [clean_str(x) for x in sources] # clean source names for later
|
281 |
+
for i, s in enumerate(sources):
|
282 |
+
# Start the thread to read frames from the video stream
|
283 |
+
print(f'{i + 1}/{n}: {s}... ', end='')
|
284 |
+
url = eval(s) if s.isnumeric() else s
|
285 |
+
if 'youtube.com/' in str(url) or 'youtu.be/' in str(url): # if source is YouTube video
|
286 |
+
check_requirements(('pafy', 'youtube_dl'))
|
287 |
+
import pafy
|
288 |
+
url = pafy.new(url).getbest(preftype="mp4").url
|
289 |
+
cap = cv2.VideoCapture(url)
|
290 |
+
assert cap.isOpened(), f'Failed to open {s}'
|
291 |
+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
292 |
+
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
293 |
+
self.fps = cap.get(cv2.CAP_PROP_FPS) % 100
|
294 |
+
|
295 |
+
_, self.imgs[i] = cap.read() # guarantee first frame
|
296 |
+
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
|
297 |
+
print(f' success ({w}x{h} at {self.fps:.2f} FPS).')
|
298 |
+
thread.start()
|
299 |
+
print('') # newline
|
300 |
+
|
301 |
+
# check for common shapes
|
302 |
+
s = np.stack([letterbox(x, self.img_size, stride=self.stride)[0].shape for x in self.imgs], 0) # shapes
|
303 |
+
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
|
304 |
+
if not self.rect:
|
305 |
+
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
|
306 |
+
|
307 |
+
def update(self, index, cap):
|
308 |
+
# Read next stream frame in a daemon thread
|
309 |
+
n = 0
|
310 |
+
while cap.isOpened():
|
311 |
+
n += 1
|
312 |
+
# _, self.imgs[index] = cap.read()
|
313 |
+
cap.grab()
|
314 |
+
if n == 4: # read every 4th frame
|
315 |
+
success, im = cap.retrieve()
|
316 |
+
self.imgs[index] = im if success else self.imgs[index] * 0
|
317 |
+
n = 0
|
318 |
+
time.sleep(1 / self.fps) # wait time
|
319 |
+
|
320 |
+
def __iter__(self):
|
321 |
+
self.count = -1
|
322 |
+
return self
|
323 |
+
|
324 |
+
def __next__(self):
|
325 |
+
self.count += 1
|
326 |
+
img0 = self.imgs.copy()
|
327 |
+
if cv2.waitKey(1) == ord('q'): # q to quit
|
328 |
+
cv2.destroyAllWindows()
|
329 |
+
raise StopIteration
|
330 |
+
|
331 |
+
# Letterbox
|
332 |
+
img = [letterbox(x, self.img_size, auto=self.rect, stride=self.stride)[0] for x in img0]
|
333 |
+
|
334 |
+
# Stack
|
335 |
+
img = np.stack(img, 0)
|
336 |
+
|
337 |
+
# Convert
|
338 |
+
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
|
339 |
+
img = np.ascontiguousarray(img)
|
340 |
+
|
341 |
+
return self.sources, img, img0, None
|
342 |
+
|
343 |
+
def __len__(self):
|
344 |
+
return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years
|
345 |
+
|
346 |
+
|
347 |
+
def img2label_paths(img_paths):
|
348 |
+
# Define label paths as a function of image paths
|
349 |
+
sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings
|
350 |
+
return ['txt'.join(x.replace(sa, sb, 1).rsplit(x.split('.')[-1], 1)) for x in img_paths]
|
351 |
+
|
352 |
+
|
353 |
+
class LoadImagesAndLabels(Dataset): # for training/testing
|
354 |
+
def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False,
|
355 |
+
cache_images=False, single_cls=False, stride=32, pad=0.0, prefix=''):
|
356 |
+
self.img_size = img_size
|
357 |
+
self.augment = augment
|
358 |
+
self.hyp = hyp
|
359 |
+
self.image_weights = image_weights
|
360 |
+
self.rect = False if image_weights else rect
|
361 |
+
self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training)
|
362 |
+
self.mosaic_border = [-img_size // 2, -img_size // 2]
|
363 |
+
self.stride = stride
|
364 |
+
self.path = path
|
365 |
+
#self.albumentations = Albumentations() if augment else None
|
366 |
+
|
367 |
+
try:
|
368 |
+
f = [] # image files
|
369 |
+
for p in path if isinstance(path, list) else [path]:
|
370 |
+
p = Path(p) # os-agnostic
|
371 |
+
if p.is_dir(): # dir
|
372 |
+
f += glob.glob(str(p / '**' / '*.*'), recursive=True)
|
373 |
+
# f = list(p.rglob('**/*.*')) # pathlib
|
374 |
+
elif p.is_file(): # file
|
375 |
+
with open(p, 'r') as t:
|
376 |
+
t = t.read().strip().splitlines()
|
377 |
+
parent = str(p.parent) + os.sep
|
378 |
+
f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
|
379 |
+
# f += [p.parent / x.lstrip(os.sep) for x in t] # local to global path (pathlib)
|
380 |
+
else:
|
381 |
+
raise Exception(f'{prefix}{p} does not exist')
|
382 |
+
self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats])
|
383 |
+
# self.img_files = sorted([x for x in f if x.suffix[1:].lower() in img_formats]) # pathlib
|
384 |
+
assert self.img_files, f'{prefix}No images found'
|
385 |
+
except Exception as e:
|
386 |
+
raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}')
|
387 |
+
|
388 |
+
# Check cache
|
389 |
+
self.label_files = img2label_paths(self.img_files) # labels
|
390 |
+
cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache') # cached labels
|
391 |
+
if cache_path.is_file():
|
392 |
+
cache, exists = torch.load(cache_path), True # load
|
393 |
+
#if cache['hash'] != get_hash(self.label_files + self.img_files) or 'version' not in cache: # changed
|
394 |
+
# cache, exists = self.cache_labels(cache_path, prefix), False # re-cache
|
395 |
+
else:
|
396 |
+
cache, exists = self.cache_labels(cache_path, prefix), False # cache
|
397 |
+
|
398 |
+
# Display cache
|
399 |
+
nf, nm, ne, nc, n = cache.pop('results') # found, missing, empty, corrupted, total
|
400 |
+
if exists:
|
401 |
+
d = f"Scanning '{cache_path}' images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupted"
|
402 |
+
tqdm(None, desc=prefix + d, total=n, initial=n) # display cache results
|
403 |
+
assert nf > 0 or not augment, f'{prefix}No labels in {cache_path}. Can not train without labels. See {help_url}'
|
404 |
+
|
405 |
+
# Read cache
|
406 |
+
cache.pop('hash') # remove hash
|
407 |
+
cache.pop('version') # remove version
|
408 |
+
labels, shapes, self.segments = zip(*cache.values())
|
409 |
+
self.labels = list(labels)
|
410 |
+
self.shapes = np.array(shapes, dtype=np.float64)
|
411 |
+
self.img_files = list(cache.keys()) # update
|
412 |
+
self.label_files = img2label_paths(cache.keys()) # update
|
413 |
+
if single_cls:
|
414 |
+
for x in self.labels:
|
415 |
+
x[:, 0] = 0
|
416 |
+
|
417 |
+
n = len(shapes) # number of images
|
418 |
+
bi = np.floor(np.arange(n) / batch_size).astype(int) # batch index
|
419 |
+
nb = bi[-1] + 1 # number of batches
|
420 |
+
self.batch = bi # batch index of image
|
421 |
+
self.n = n
|
422 |
+
self.indices = range(n)
|
423 |
+
|
424 |
+
# Rectangular Training
|
425 |
+
if self.rect:
|
426 |
+
# Sort by aspect ratio
|
427 |
+
s = self.shapes # wh
|
428 |
+
ar = s[:, 1] / s[:, 0] # aspect ratio
|
429 |
+
irect = ar.argsort()
|
430 |
+
self.img_files = [self.img_files[i] for i in irect]
|
431 |
+
self.label_files = [self.label_files[i] for i in irect]
|
432 |
+
self.labels = [self.labels[i] for i in irect]
|
433 |
+
self.shapes = s[irect] # wh
|
434 |
+
ar = ar[irect]
|
435 |
+
|
436 |
+
# Set training image shapes
|
437 |
+
shapes = [[1, 1]] * nb
|
438 |
+
for i in range(nb):
|
439 |
+
ari = ar[bi == i]
|
440 |
+
mini, maxi = ari.min(), ari.max()
|
441 |
+
if maxi < 1:
|
442 |
+
shapes[i] = [maxi, 1]
|
443 |
+
elif mini > 1:
|
444 |
+
shapes[i] = [1, 1 / mini]
|
445 |
+
|
446 |
+
self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(int) * stride
|
447 |
+
|
448 |
+
# Cache images into memory for faster training (WARNING: large datasets may exceed system RAM)
|
449 |
+
self.imgs = [None] * n
|
450 |
+
if cache_images:
|
451 |
+
if cache_images == 'disk':
|
452 |
+
self.im_cache_dir = Path(Path(self.img_files[0]).parent.as_posix() + '_npy')
|
453 |
+
self.img_npy = [self.im_cache_dir / Path(f).with_suffix('.npy').name for f in self.img_files]
|
454 |
+
self.im_cache_dir.mkdir(parents=True, exist_ok=True)
|
455 |
+
gb = 0 # Gigabytes of cached images
|
456 |
+
self.img_hw0, self.img_hw = [None] * n, [None] * n
|
457 |
+
results = ThreadPool(8).imap(lambda x: load_image(*x), zip(repeat(self), range(n)))
|
458 |
+
pbar = tqdm(enumerate(results), total=n)
|
459 |
+
for i, x in pbar:
|
460 |
+
if cache_images == 'disk':
|
461 |
+
if not self.img_npy[i].exists():
|
462 |
+
np.save(self.img_npy[i].as_posix(), x[0])
|
463 |
+
gb += self.img_npy[i].stat().st_size
|
464 |
+
else:
|
465 |
+
self.imgs[i], self.img_hw0[i], self.img_hw[i] = x
|
466 |
+
gb += self.imgs[i].nbytes
|
467 |
+
pbar.desc = f'{prefix}Caching images ({gb / 1E9:.1f}GB)'
|
468 |
+
pbar.close()
|
469 |
+
|
470 |
+
def cache_labels(self, path=Path('./labels.cache'), prefix=''):
|
471 |
+
# Cache dataset labels, check images and read shapes
|
472 |
+
x = {} # dict
|
473 |
+
nm, nf, ne, nc = 0, 0, 0, 0 # number missing, found, empty, duplicate
|
474 |
+
pbar = tqdm(zip(self.img_files, self.label_files), desc='Scanning images', total=len(self.img_files))
|
475 |
+
for i, (im_file, lb_file) in enumerate(pbar):
|
476 |
+
try:
|
477 |
+
# verify images
|
478 |
+
im = Image.open(im_file)
|
479 |
+
im.verify() # PIL verify
|
480 |
+
shape = exif_size(im) # image size
|
481 |
+
segments = [] # instance segments
|
482 |
+
assert (shape[0] > 9) & (shape[1] > 9), f'image size {shape} <10 pixels'
|
483 |
+
assert im.format.lower() in img_formats, f'invalid image format {im.format}'
|
484 |
+
|
485 |
+
# verify labels
|
486 |
+
if os.path.isfile(lb_file):
|
487 |
+
nf += 1 # label found
|
488 |
+
with open(lb_file, 'r') as f:
|
489 |
+
l = [x.split() for x in f.read().strip().splitlines()]
|
490 |
+
if any([len(x) > 8 for x in l]): # is segment
|
491 |
+
classes = np.array([x[0] for x in l], dtype=np.float32)
|
492 |
+
segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in l] # (cls, xy1...)
|
493 |
+
l = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh)
|
494 |
+
l = np.array(l, dtype=np.float32)
|
495 |
+
if len(l):
|
496 |
+
assert l.shape[1] == 5, 'labels require 5 columns each'
|
497 |
+
assert (l >= 0).all(), 'negative labels'
|
498 |
+
assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels'
|
499 |
+
assert np.unique(l, axis=0).shape[0] == l.shape[0], 'duplicate labels'
|
500 |
+
else:
|
501 |
+
ne += 1 # label empty
|
502 |
+
l = np.zeros((0, 5), dtype=np.float32)
|
503 |
+
else:
|
504 |
+
nm += 1 # label missing
|
505 |
+
l = np.zeros((0, 5), dtype=np.float32)
|
506 |
+
x[im_file] = [l, shape, segments]
|
507 |
+
except Exception as e:
|
508 |
+
nc += 1
|
509 |
+
print(f'{prefix}WARNING: Ignoring corrupted image and/or label {im_file}: {e}')
|
510 |
+
|
511 |
+
pbar.desc = f"{prefix}Scanning '{path.parent / path.stem}' images and labels... " \
|
512 |
+
f"{nf} found, {nm} missing, {ne} empty, {nc} corrupted"
|
513 |
+
pbar.close()
|
514 |
+
|
515 |
+
if nf == 0:
|
516 |
+
print(f'{prefix}WARNING: No labels found in {path}. See {help_url}')
|
517 |
+
|
518 |
+
x['hash'] = get_hash(self.label_files + self.img_files)
|
519 |
+
x['results'] = nf, nm, ne, nc, i + 1
|
520 |
+
x['version'] = 0.1 # cache version
|
521 |
+
torch.save(x, path) # save for next time
|
522 |
+
logging.info(f'{prefix}New cache created: {path}')
|
523 |
+
return x
|
524 |
+
|
525 |
+
def __len__(self):
|
526 |
+
return len(self.img_files)
|
527 |
+
|
528 |
+
# def __iter__(self):
|
529 |
+
# self.count = -1
|
530 |
+
# print('ran dataset iter')
|
531 |
+
# #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF)
|
532 |
+
# return self
|
533 |
+
|
534 |
+
def __getitem__(self, index):
|
535 |
+
index = self.indices[index] # linear, shuffled, or image_weights
|
536 |
+
|
537 |
+
hyp = self.hyp
|
538 |
+
mosaic = self.mosaic and random.random() < hyp['mosaic']
|
539 |
+
if mosaic:
|
540 |
+
# Load mosaic
|
541 |
+
if random.random() < 0.8:
|
542 |
+
img, labels = load_mosaic(self, index)
|
543 |
+
else:
|
544 |
+
img, labels = load_mosaic9(self, index)
|
545 |
+
shapes = None
|
546 |
+
|
547 |
+
# MixUp https://arxiv.org/pdf/1710.09412.pdf
|
548 |
+
if random.random() < hyp['mixup']:
|
549 |
+
if random.random() < 0.8:
|
550 |
+
img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1))
|
551 |
+
else:
|
552 |
+
img2, labels2 = load_mosaic9(self, random.randint(0, len(self.labels) - 1))
|
553 |
+
r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0
|
554 |
+
img = (img * r + img2 * (1 - r)).astype(np.uint8)
|
555 |
+
labels = np.concatenate((labels, labels2), 0)
|
556 |
+
|
557 |
+
else:
|
558 |
+
# Load image
|
559 |
+
img, (h0, w0), (h, w) = load_image(self, index)
|
560 |
+
|
561 |
+
# Letterbox
|
562 |
+
shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
|
563 |
+
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
|
564 |
+
shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
|
565 |
+
|
566 |
+
labels = self.labels[index].copy()
|
567 |
+
if labels.size: # normalized xywh to pixel xyxy format
|
568 |
+
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1])
|
569 |
+
|
570 |
+
if self.augment:
|
571 |
+
# Augment imagespace
|
572 |
+
if not mosaic:
|
573 |
+
img, labels = random_perspective(img, labels,
|
574 |
+
degrees=hyp['degrees'],
|
575 |
+
translate=hyp['translate'],
|
576 |
+
scale=hyp['scale'],
|
577 |
+
shear=hyp['shear'],
|
578 |
+
perspective=hyp['perspective'])
|
579 |
+
|
580 |
+
|
581 |
+
#img, labels = self.albumentations(img, labels)
|
582 |
+
|
583 |
+
# Augment colorspace
|
584 |
+
augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
|
585 |
+
|
586 |
+
# Apply cutouts
|
587 |
+
# if random.random() < 0.9:
|
588 |
+
# labels = cutout(img, labels)
|
589 |
+
|
590 |
+
if random.random() < hyp['paste_in']:
|
591 |
+
sample_labels, sample_images, sample_masks = [], [], []
|
592 |
+
while len(sample_labels) < 30:
|
593 |
+
sample_labels_, sample_images_, sample_masks_ = load_samples(self, random.randint(0, len(self.labels) - 1))
|
594 |
+
sample_labels += sample_labels_
|
595 |
+
sample_images += sample_images_
|
596 |
+
sample_masks += sample_masks_
|
597 |
+
#print(len(sample_labels))
|
598 |
+
if len(sample_labels) == 0:
|
599 |
+
break
|
600 |
+
labels = pastein(img, labels, sample_labels, sample_images, sample_masks)
|
601 |
+
|
602 |
+
nL = len(labels) # number of labels
|
603 |
+
if nL:
|
604 |
+
labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh
|
605 |
+
labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1
|
606 |
+
labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1
|
607 |
+
|
608 |
+
if self.augment:
|
609 |
+
# flip up-down
|
610 |
+
if random.random() < hyp['flipud']:
|
611 |
+
img = np.flipud(img)
|
612 |
+
if nL:
|
613 |
+
labels[:, 2] = 1 - labels[:, 2]
|
614 |
+
|
615 |
+
# flip left-right
|
616 |
+
if random.random() < hyp['fliplr']:
|
617 |
+
img = np.fliplr(img)
|
618 |
+
if nL:
|
619 |
+
labels[:, 1] = 1 - labels[:, 1]
|
620 |
+
|
621 |
+
labels_out = torch.zeros((nL, 6))
|
622 |
+
if nL:
|
623 |
+
labels_out[:, 1:] = torch.from_numpy(labels)
|
624 |
+
|
625 |
+
# Convert
|
626 |
+
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
|
627 |
+
img = np.ascontiguousarray(img)
|
628 |
+
|
629 |
+
return torch.from_numpy(img), labels_out, self.img_files[index], shapes
|
630 |
+
|
631 |
+
@staticmethod
|
632 |
+
def collate_fn(batch):
|
633 |
+
img, label, path, shapes = zip(*batch) # transposed
|
634 |
+
for i, l in enumerate(label):
|
635 |
+
l[:, 0] = i # add target image index for build_targets()
|
636 |
+
return torch.stack(img, 0), torch.cat(label, 0), path, shapes
|
637 |
+
|
638 |
+
@staticmethod
|
639 |
+
def collate_fn4(batch):
|
640 |
+
img, label, path, shapes = zip(*batch) # transposed
|
641 |
+
n = len(shapes) // 4
|
642 |
+
img4, label4, path4, shapes4 = [], [], path[:n], shapes[:n]
|
643 |
+
|
644 |
+
ho = torch.tensor([[0., 0, 0, 1, 0, 0]])
|
645 |
+
wo = torch.tensor([[0., 0, 1, 0, 0, 0]])
|
646 |
+
s = torch.tensor([[1, 1, .5, .5, .5, .5]]) # scale
|
647 |
+
for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW
|
648 |
+
i *= 4
|
649 |
+
if random.random() < 0.5:
|
650 |
+
im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2., mode='bilinear', align_corners=False)[
|
651 |
+
0].type(img[i].type())
|
652 |
+
l = label[i]
|
653 |
+
else:
|
654 |
+
im = torch.cat((torch.cat((img[i], img[i + 1]), 1), torch.cat((img[i + 2], img[i + 3]), 1)), 2)
|
655 |
+
l = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s
|
656 |
+
img4.append(im)
|
657 |
+
label4.append(l)
|
658 |
+
|
659 |
+
for i, l in enumerate(label4):
|
660 |
+
l[:, 0] = i # add target image index for build_targets()
|
661 |
+
|
662 |
+
return torch.stack(img4, 0), torch.cat(label4, 0), path4, shapes4
|
663 |
+
|
664 |
+
|
665 |
+
# Ancillary functions --------------------------------------------------------------------------------------------------
|
666 |
+
def load_image(self, index):
|
667 |
+
# loads 1 image from dataset, returns img, original hw, resized hw
|
668 |
+
img = self.imgs[index]
|
669 |
+
if img is None: # not cached
|
670 |
+
path = self.img_files[index]
|
671 |
+
img = cv2.imread(path) # BGR
|
672 |
+
assert img is not None, 'Image Not Found ' + path
|
673 |
+
h0, w0 = img.shape[:2] # orig hw
|
674 |
+
r = self.img_size / max(h0, w0) # resize image to img_size
|
675 |
+
if r != 1: # always resize down, only resize up if training with augmentation
|
676 |
+
interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR
|
677 |
+
img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp)
|
678 |
+
return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized
|
679 |
+
else:
|
680 |
+
return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized
|
681 |
+
|
682 |
+
|
683 |
+
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
|
684 |
+
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
|
685 |
+
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
|
686 |
+
dtype = img.dtype # uint8
|
687 |
+
|
688 |
+
x = np.arange(0, 256, dtype=np.int16)
|
689 |
+
lut_hue = ((x * r[0]) % 180).astype(dtype)
|
690 |
+
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
|
691 |
+
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
|
692 |
+
|
693 |
+
img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
|
694 |
+
cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
|
695 |
+
|
696 |
+
|
697 |
+
def hist_equalize(img, clahe=True, bgr=False):
|
698 |
+
# Equalize histogram on BGR image 'img' with img.shape(n,m,3) and range 0-255
|
699 |
+
yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV)
|
700 |
+
if clahe:
|
701 |
+
c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
702 |
+
yuv[:, :, 0] = c.apply(yuv[:, :, 0])
|
703 |
+
else:
|
704 |
+
yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram
|
705 |
+
return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB
|
706 |
+
|
707 |
+
|
708 |
+
def load_mosaic(self, index):
|
709 |
+
# loads images in a 4-mosaic
|
710 |
+
|
711 |
+
labels4, segments4 = [], []
|
712 |
+
s = self.img_size
|
713 |
+
yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y
|
714 |
+
indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices
|
715 |
+
for i, index in enumerate(indices):
|
716 |
+
# Load image
|
717 |
+
img, _, (h, w) = load_image(self, index)
|
718 |
+
|
719 |
+
# place img in img4
|
720 |
+
if i == 0: # top left
|
721 |
+
img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
|
722 |
+
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
|
723 |
+
x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
|
724 |
+
elif i == 1: # top right
|
725 |
+
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
|
726 |
+
x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
|
727 |
+
elif i == 2: # bottom left
|
728 |
+
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
|
729 |
+
x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
|
730 |
+
elif i == 3: # bottom right
|
731 |
+
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
|
732 |
+
x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
|
733 |
+
|
734 |
+
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
|
735 |
+
padw = x1a - x1b
|
736 |
+
padh = y1a - y1b
|
737 |
+
|
738 |
+
# Labels
|
739 |
+
labels, segments = self.labels[index].copy(), self.segments[index].copy()
|
740 |
+
if labels.size:
|
741 |
+
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format
|
742 |
+
segments = [xyn2xy(x, w, h, padw, padh) for x in segments]
|
743 |
+
labels4.append(labels)
|
744 |
+
segments4.extend(segments)
|
745 |
+
|
746 |
+
# Concat/clip labels
|
747 |
+
labels4 = np.concatenate(labels4, 0)
|
748 |
+
for x in (labels4[:, 1:], *segments4):
|
749 |
+
np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
|
750 |
+
# img4, labels4 = replicate(img4, labels4) # replicate
|
751 |
+
|
752 |
+
# Augment
|
753 |
+
#img4, labels4, segments4 = remove_background(img4, labels4, segments4)
|
754 |
+
#sample_segments(img4, labels4, segments4, probability=self.hyp['copy_paste'])
|
755 |
+
img4, labels4, segments4 = copy_paste(img4, labels4, segments4, probability=self.hyp['copy_paste'])
|
756 |
+
img4, labels4 = random_perspective(img4, labels4, segments4,
|
757 |
+
degrees=self.hyp['degrees'],
|
758 |
+
translate=self.hyp['translate'],
|
759 |
+
scale=self.hyp['scale'],
|
760 |
+
shear=self.hyp['shear'],
|
761 |
+
perspective=self.hyp['perspective'],
|
762 |
+
border=self.mosaic_border) # border to remove
|
763 |
+
|
764 |
+
return img4, labels4
|
765 |
+
|
766 |
+
|
767 |
+
def load_mosaic9(self, index):
|
768 |
+
# loads images in a 9-mosaic
|
769 |
+
|
770 |
+
labels9, segments9 = [], []
|
771 |
+
s = self.img_size
|
772 |
+
indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices
|
773 |
+
for i, index in enumerate(indices):
|
774 |
+
# Load image
|
775 |
+
img, _, (h, w) = load_image(self, index)
|
776 |
+
|
777 |
+
# place img in img9
|
778 |
+
if i == 0: # center
|
779 |
+
img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
|
780 |
+
h0, w0 = h, w
|
781 |
+
c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates
|
782 |
+
elif i == 1: # top
|
783 |
+
c = s, s - h, s + w, s
|
784 |
+
elif i == 2: # top right
|
785 |
+
c = s + wp, s - h, s + wp + w, s
|
786 |
+
elif i == 3: # right
|
787 |
+
c = s + w0, s, s + w0 + w, s + h
|
788 |
+
elif i == 4: # bottom right
|
789 |
+
c = s + w0, s + hp, s + w0 + w, s + hp + h
|
790 |
+
elif i == 5: # bottom
|
791 |
+
c = s + w0 - w, s + h0, s + w0, s + h0 + h
|
792 |
+
elif i == 6: # bottom left
|
793 |
+
c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h
|
794 |
+
elif i == 7: # left
|
795 |
+
c = s - w, s + h0 - h, s, s + h0
|
796 |
+
elif i == 8: # top left
|
797 |
+
c = s - w, s + h0 - hp - h, s, s + h0 - hp
|
798 |
+
|
799 |
+
padx, pady = c[:2]
|
800 |
+
x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords
|
801 |
+
|
802 |
+
# Labels
|
803 |
+
labels, segments = self.labels[index].copy(), self.segments[index].copy()
|
804 |
+
if labels.size:
|
805 |
+
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format
|
806 |
+
segments = [xyn2xy(x, w, h, padx, pady) for x in segments]
|
807 |
+
labels9.append(labels)
|
808 |
+
segments9.extend(segments)
|
809 |
+
|
810 |
+
# Image
|
811 |
+
img9[y1:y2, x1:x2] = img[y1 - pady:, x1 - padx:] # img9[ymin:ymax, xmin:xmax]
|
812 |
+
hp, wp = h, w # height, width previous
|
813 |
+
|
814 |
+
# Offset
|
815 |
+
yc, xc = [int(random.uniform(0, s)) for _ in self.mosaic_border] # mosaic center x, y
|
816 |
+
img9 = img9[yc:yc + 2 * s, xc:xc + 2 * s]
|
817 |
+
|
818 |
+
# Concat/clip labels
|
819 |
+
labels9 = np.concatenate(labels9, 0)
|
820 |
+
labels9[:, [1, 3]] -= xc
|
821 |
+
labels9[:, [2, 4]] -= yc
|
822 |
+
c = np.array([xc, yc]) # centers
|
823 |
+
segments9 = [x - c for x in segments9]
|
824 |
+
|
825 |
+
for x in (labels9[:, 1:], *segments9):
|
826 |
+
np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
|
827 |
+
# img9, labels9 = replicate(img9, labels9) # replicate
|
828 |
+
|
829 |
+
# Augment
|
830 |
+
#img9, labels9, segments9 = remove_background(img9, labels9, segments9)
|
831 |
+
img9, labels9, segments9 = copy_paste(img9, labels9, segments9, probability=self.hyp['copy_paste'])
|
832 |
+
img9, labels9 = random_perspective(img9, labels9, segments9,
|
833 |
+
degrees=self.hyp['degrees'],
|
834 |
+
translate=self.hyp['translate'],
|
835 |
+
scale=self.hyp['scale'],
|
836 |
+
shear=self.hyp['shear'],
|
837 |
+
perspective=self.hyp['perspective'],
|
838 |
+
border=self.mosaic_border) # border to remove
|
839 |
+
|
840 |
+
return img9, labels9
|
841 |
+
|
842 |
+
|
843 |
+
def load_samples(self, index):
|
844 |
+
# loads images in a 4-mosaic
|
845 |
+
|
846 |
+
labels4, segments4 = [], []
|
847 |
+
s = self.img_size
|
848 |
+
yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y
|
849 |
+
indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices
|
850 |
+
for i, index in enumerate(indices):
|
851 |
+
# Load image
|
852 |
+
img, _, (h, w) = load_image(self, index)
|
853 |
+
|
854 |
+
# place img in img4
|
855 |
+
if i == 0: # top left
|
856 |
+
img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
|
857 |
+
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
|
858 |
+
x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
|
859 |
+
elif i == 1: # top right
|
860 |
+
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
|
861 |
+
x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
|
862 |
+
elif i == 2: # bottom left
|
863 |
+
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
|
864 |
+
x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
|
865 |
+
elif i == 3: # bottom right
|
866 |
+
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
|
867 |
+
x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
|
868 |
+
|
869 |
+
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
|
870 |
+
padw = x1a - x1b
|
871 |
+
padh = y1a - y1b
|
872 |
+
|
873 |
+
# Labels
|
874 |
+
labels, segments = self.labels[index].copy(), self.segments[index].copy()
|
875 |
+
if labels.size:
|
876 |
+
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format
|
877 |
+
segments = [xyn2xy(x, w, h, padw, padh) for x in segments]
|
878 |
+
labels4.append(labels)
|
879 |
+
segments4.extend(segments)
|
880 |
+
|
881 |
+
# Concat/clip labels
|
882 |
+
labels4 = np.concatenate(labels4, 0)
|
883 |
+
for x in (labels4[:, 1:], *segments4):
|
884 |
+
np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
|
885 |
+
# img4, labels4 = replicate(img4, labels4) # replicate
|
886 |
+
|
887 |
+
# Augment
|
888 |
+
#img4, labels4, segments4 = remove_background(img4, labels4, segments4)
|
889 |
+
sample_labels, sample_images, sample_masks = sample_segments(img4, labels4, segments4, probability=0.5)
|
890 |
+
|
891 |
+
return sample_labels, sample_images, sample_masks
|
892 |
+
|
893 |
+
|
894 |
+
def copy_paste(img, labels, segments, probability=0.5):
|
895 |
+
# Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
|
896 |
+
n = len(segments)
|
897 |
+
if probability and n:
|
898 |
+
h, w, c = img.shape # height, width, channels
|
899 |
+
im_new = np.zeros(img.shape, np.uint8)
|
900 |
+
for j in random.sample(range(n), k=round(probability * n)):
|
901 |
+
l, s = labels[j], segments[j]
|
902 |
+
box = w - l[3], l[2], w - l[1], l[4]
|
903 |
+
ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
|
904 |
+
if (ioa < 0.30).all(): # allow 30% obscuration of existing labels
|
905 |
+
labels = np.concatenate((labels, [[l[0], *box]]), 0)
|
906 |
+
segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1))
|
907 |
+
cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
|
908 |
+
|
909 |
+
result = cv2.bitwise_and(src1=img, src2=im_new)
|
910 |
+
result = cv2.flip(result, 1) # augment segments (flip left-right)
|
911 |
+
i = result > 0 # pixels to replace
|
912 |
+
# i[:, :] = result.max(2).reshape(h, w, 1) # act over ch
|
913 |
+
img[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug
|
914 |
+
|
915 |
+
return img, labels, segments
|
916 |
+
|
917 |
+
|
918 |
+
def remove_background(img, labels, segments):
|
919 |
+
# Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
|
920 |
+
n = len(segments)
|
921 |
+
h, w, c = img.shape # height, width, channels
|
922 |
+
im_new = np.zeros(img.shape, np.uint8)
|
923 |
+
img_new = np.ones(img.shape, np.uint8) * 114
|
924 |
+
for j in range(n):
|
925 |
+
cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
|
926 |
+
|
927 |
+
result = cv2.bitwise_and(src1=img, src2=im_new)
|
928 |
+
|
929 |
+
i = result > 0 # pixels to replace
|
930 |
+
img_new[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug
|
931 |
+
|
932 |
+
return img_new, labels, segments
|
933 |
+
|
934 |
+
|
935 |
+
def sample_segments(img, labels, segments, probability=0.5):
|
936 |
+
# Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
|
937 |
+
n = len(segments)
|
938 |
+
sample_labels = []
|
939 |
+
sample_images = []
|
940 |
+
sample_masks = []
|
941 |
+
if probability and n:
|
942 |
+
h, w, c = img.shape # height, width, channels
|
943 |
+
for j in random.sample(range(n), k=round(probability * n)):
|
944 |
+
l, s = labels[j], segments[j]
|
945 |
+
box = l[1].astype(int).clip(0,w-1), l[2].astype(int).clip(0,h-1), l[3].astype(int).clip(0,w-1), l[4].astype(int).clip(0,h-1)
|
946 |
+
|
947 |
+
#print(box)
|
948 |
+
if (box[2] <= box[0]) or (box[3] <= box[1]):
|
949 |
+
continue
|
950 |
+
|
951 |
+
sample_labels.append(l[0])
|
952 |
+
|
953 |
+
mask = np.zeros(img.shape, np.uint8)
|
954 |
+
|
955 |
+
cv2.drawContours(mask, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
|
956 |
+
sample_masks.append(mask[box[1]:box[3],box[0]:box[2],:])
|
957 |
+
|
958 |
+
result = cv2.bitwise_and(src1=img, src2=mask)
|
959 |
+
i = result > 0 # pixels to replace
|
960 |
+
mask[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug
|
961 |
+
#print(box)
|
962 |
+
sample_images.append(mask[box[1]:box[3],box[0]:box[2],:])
|
963 |
+
|
964 |
+
return sample_labels, sample_images, sample_masks
|
965 |
+
|
966 |
+
|
967 |
+
def replicate(img, labels):
|
968 |
+
# Replicate labels
|
969 |
+
h, w = img.shape[:2]
|
970 |
+
boxes = labels[:, 1:].astype(int)
|
971 |
+
x1, y1, x2, y2 = boxes.T
|
972 |
+
s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
|
973 |
+
for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
|
974 |
+
x1b, y1b, x2b, y2b = boxes[i]
|
975 |
+
bh, bw = y2b - y1b, x2b - x1b
|
976 |
+
yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
|
977 |
+
x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
|
978 |
+
img[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
|
979 |
+
labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
|
980 |
+
|
981 |
+
return img, labels
|
982 |
+
|
983 |
+
|
984 |
+
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
|
985 |
+
# Resize and pad image while meeting stride-multiple constraints
|
986 |
+
shape = img.shape[:2] # current shape [height, width]
|
987 |
+
if isinstance(new_shape, int):
|
988 |
+
new_shape = (new_shape, new_shape)
|
989 |
+
|
990 |
+
# Scale ratio (new / old)
|
991 |
+
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
|
992 |
+
if not scaleup: # only scale down, do not scale up (for better test mAP)
|
993 |
+
r = min(r, 1.0)
|
994 |
+
|
995 |
+
# Compute padding
|
996 |
+
ratio = r, r # width, height ratios
|
997 |
+
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
|
998 |
+
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
|
999 |
+
if auto: # minimum rectangle
|
1000 |
+
dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
|
1001 |
+
elif scaleFill: # stretch
|
1002 |
+
dw, dh = 0.0, 0.0
|
1003 |
+
new_unpad = (new_shape[1], new_shape[0])
|
1004 |
+
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
|
1005 |
+
|
1006 |
+
dw /= 2 # divide padding into 2 sides
|
1007 |
+
dh /= 2
|
1008 |
+
|
1009 |
+
if shape[::-1] != new_unpad: # resize
|
1010 |
+
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
|
1011 |
+
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
|
1012 |
+
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
|
1013 |
+
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
|
1014 |
+
return img, ratio, (dw, dh)
|
1015 |
+
|
1016 |
+
|
1017 |
+
def random_perspective(img, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
|
1018 |
+
border=(0, 0)):
|
1019 |
+
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
|
1020 |
+
# targets = [cls, xyxy]
|
1021 |
+
|
1022 |
+
height = img.shape[0] + border[0] * 2 # shape(h,w,c)
|
1023 |
+
width = img.shape[1] + border[1] * 2
|
1024 |
+
|
1025 |
+
# Center
|
1026 |
+
C = np.eye(3)
|
1027 |
+
C[0, 2] = -img.shape[1] / 2 # x translation (pixels)
|
1028 |
+
C[1, 2] = -img.shape[0] / 2 # y translation (pixels)
|
1029 |
+
|
1030 |
+
# Perspective
|
1031 |
+
P = np.eye(3)
|
1032 |
+
P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y)
|
1033 |
+
P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x)
|
1034 |
+
|
1035 |
+
# Rotation and Scale
|
1036 |
+
R = np.eye(3)
|
1037 |
+
a = random.uniform(-degrees, degrees)
|
1038 |
+
# a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
|
1039 |
+
s = random.uniform(1 - scale, 1.1 + scale)
|
1040 |
+
# s = 2 ** random.uniform(-scale, scale)
|
1041 |
+
R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)
|
1042 |
+
|
1043 |
+
# Shear
|
1044 |
+
S = np.eye(3)
|
1045 |
+
S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg)
|
1046 |
+
S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg)
|
1047 |
+
|
1048 |
+
# Translation
|
1049 |
+
T = np.eye(3)
|
1050 |
+
T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels)
|
1051 |
+
T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels)
|
1052 |
+
|
1053 |
+
# Combined rotation matrix
|
1054 |
+
M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT
|
1055 |
+
if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed
|
1056 |
+
if perspective:
|
1057 |
+
img = cv2.warpPerspective(img, M, dsize=(width, height), borderValue=(114, 114, 114))
|
1058 |
+
else: # affine
|
1059 |
+
img = cv2.warpAffine(img, M[:2], dsize=(width, height), borderValue=(114, 114, 114))
|
1060 |
+
|
1061 |
+
# Visualize
|
1062 |
+
# import matplotlib.pyplot as plt
|
1063 |
+
# ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
|
1064 |
+
# ax[0].imshow(img[:, :, ::-1]) # base
|
1065 |
+
# ax[1].imshow(img2[:, :, ::-1]) # warped
|
1066 |
+
|
1067 |
+
# Transform label coordinates
|
1068 |
+
n = len(targets)
|
1069 |
+
if n:
|
1070 |
+
use_segments = any(x.any() for x in segments)
|
1071 |
+
new = np.zeros((n, 4))
|
1072 |
+
if use_segments: # warp segments
|
1073 |
+
segments = resample_segments(segments) # upsample
|
1074 |
+
for i, segment in enumerate(segments):
|
1075 |
+
xy = np.ones((len(segment), 3))
|
1076 |
+
xy[:, :2] = segment
|
1077 |
+
xy = xy @ M.T # transform
|
1078 |
+
xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine
|
1079 |
+
|
1080 |
+
# clip
|
1081 |
+
new[i] = segment2box(xy, width, height)
|
1082 |
+
|
1083 |
+
else: # warp boxes
|
1084 |
+
xy = np.ones((n * 4, 3))
|
1085 |
+
xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1
|
1086 |
+
xy = xy @ M.T # transform
|
1087 |
+
xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine
|
1088 |
+
|
1089 |
+
# create new boxes
|
1090 |
+
x = xy[:, [0, 2, 4, 6]]
|
1091 |
+
y = xy[:, [1, 3, 5, 7]]
|
1092 |
+
new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T
|
1093 |
+
|
1094 |
+
# clip
|
1095 |
+
new[:, [0, 2]] = new[:, [0, 2]].clip(0, width)
|
1096 |
+
new[:, [1, 3]] = new[:, [1, 3]].clip(0, height)
|
1097 |
+
|
1098 |
+
# filter candidates
|
1099 |
+
i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10)
|
1100 |
+
targets = targets[i]
|
1101 |
+
targets[:, 1:5] = new[i]
|
1102 |
+
|
1103 |
+
return img, targets
|
1104 |
+
|
1105 |
+
|
1106 |
+
def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)
|
1107 |
+
# Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
|
1108 |
+
w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
|
1109 |
+
w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
|
1110 |
+
ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio
|
1111 |
+
return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates
|
1112 |
+
|
1113 |
+
|
1114 |
+
def bbox_ioa(box1, box2):
|
1115 |
+
# Returns the intersection over box2 area given box1, box2. box1 is 4, box2 is nx4. boxes are x1y1x2y2
|
1116 |
+
box2 = box2.transpose()
|
1117 |
+
|
1118 |
+
# Get the coordinates of bounding boxes
|
1119 |
+
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
|
1120 |
+
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
|
1121 |
+
|
1122 |
+
# Intersection area
|
1123 |
+
inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \
|
1124 |
+
(np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0)
|
1125 |
+
|
1126 |
+
# box2 area
|
1127 |
+
box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + 1e-16
|
1128 |
+
|
1129 |
+
# Intersection over box2 area
|
1130 |
+
return inter_area / box2_area
|
1131 |
+
|
1132 |
+
|
1133 |
+
def cutout(image, labels):
|
1134 |
+
# Applies image cutout augmentation https://arxiv.org/abs/1708.04552
|
1135 |
+
h, w = image.shape[:2]
|
1136 |
+
|
1137 |
+
# create random masks
|
1138 |
+
scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction
|
1139 |
+
for s in scales:
|
1140 |
+
mask_h = random.randint(1, int(h * s))
|
1141 |
+
mask_w = random.randint(1, int(w * s))
|
1142 |
+
|
1143 |
+
# box
|
1144 |
+
xmin = max(0, random.randint(0, w) - mask_w // 2)
|
1145 |
+
ymin = max(0, random.randint(0, h) - mask_h // 2)
|
1146 |
+
xmax = min(w, xmin + mask_w)
|
1147 |
+
ymax = min(h, ymin + mask_h)
|
1148 |
+
|
1149 |
+
# apply random color mask
|
1150 |
+
image[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)]
|
1151 |
+
|
1152 |
+
# return unobscured labels
|
1153 |
+
if len(labels) and s > 0.03:
|
1154 |
+
box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
|
1155 |
+
ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
|
1156 |
+
labels = labels[ioa < 0.60] # remove >60% obscured labels
|
1157 |
+
|
1158 |
+
return labels
|
1159 |
+
|
1160 |
+
|
1161 |
+
def pastein(image, labels, sample_labels, sample_images, sample_masks):
|
1162 |
+
# Applies image cutout augmentation https://arxiv.org/abs/1708.04552
|
1163 |
+
h, w = image.shape[:2]
|
1164 |
+
|
1165 |
+
# create random masks
|
1166 |
+
scales = [0.75] * 2 + [0.5] * 4 + [0.25] * 4 + [0.125] * 4 + [0.0625] * 6 # image size fraction
|
1167 |
+
for s in scales:
|
1168 |
+
if random.random() < 0.2:
|
1169 |
+
continue
|
1170 |
+
mask_h = random.randint(1, int(h * s))
|
1171 |
+
mask_w = random.randint(1, int(w * s))
|
1172 |
+
|
1173 |
+
# box
|
1174 |
+
xmin = max(0, random.randint(0, w) - mask_w // 2)
|
1175 |
+
ymin = max(0, random.randint(0, h) - mask_h // 2)
|
1176 |
+
xmax = min(w, xmin + mask_w)
|
1177 |
+
ymax = min(h, ymin + mask_h)
|
1178 |
+
|
1179 |
+
box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
|
1180 |
+
if len(labels):
|
1181 |
+
ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
|
1182 |
+
else:
|
1183 |
+
ioa = np.zeros(1)
|
1184 |
+
|
1185 |
+
if (ioa < 0.30).all() and len(sample_labels) and (xmax > xmin+20) and (ymax > ymin+20): # allow 30% obscuration of existing labels
|
1186 |
+
sel_ind = random.randint(0, len(sample_labels)-1)
|
1187 |
+
#print(len(sample_labels))
|
1188 |
+
#print(sel_ind)
|
1189 |
+
#print((xmax-xmin, ymax-ymin))
|
1190 |
+
#print(image[ymin:ymax, xmin:xmax].shape)
|
1191 |
+
#print([[sample_labels[sel_ind], *box]])
|
1192 |
+
#print(labels.shape)
|
1193 |
+
hs, ws, cs = sample_images[sel_ind].shape
|
1194 |
+
r_scale = min((ymax-ymin)/hs, (xmax-xmin)/ws)
|
1195 |
+
r_w = int(ws*r_scale)
|
1196 |
+
r_h = int(hs*r_scale)
|
1197 |
+
|
1198 |
+
if (r_w > 10) and (r_h > 10):
|
1199 |
+
r_mask = cv2.resize(sample_masks[sel_ind], (r_w, r_h))
|
1200 |
+
r_image = cv2.resize(sample_images[sel_ind], (r_w, r_h))
|
1201 |
+
temp_crop = image[ymin:ymin+r_h, xmin:xmin+r_w]
|
1202 |
+
m_ind = r_mask > 0
|
1203 |
+
if m_ind.astype(np.int32).sum() > 60:
|
1204 |
+
temp_crop[m_ind] = r_image[m_ind]
|
1205 |
+
#print(sample_labels[sel_ind])
|
1206 |
+
#print(sample_images[sel_ind].shape)
|
1207 |
+
#print(temp_crop.shape)
|
1208 |
+
box = np.array([xmin, ymin, xmin+r_w, ymin+r_h], dtype=np.float32)
|
1209 |
+
if len(labels):
|
1210 |
+
labels = np.concatenate((labels, [[sample_labels[sel_ind], *box]]), 0)
|
1211 |
+
else:
|
1212 |
+
labels = np.array([[sample_labels[sel_ind], *box]])
|
1213 |
+
|
1214 |
+
image[ymin:ymin+r_h, xmin:xmin+r_w] = temp_crop
|
1215 |
+
|
1216 |
+
return labels
|
1217 |
+
|
1218 |
+
class Albumentations:
|
1219 |
+
# YOLOv5 Albumentations class (optional, only used if package is installed)
|
1220 |
+
def __init__(self):
|
1221 |
+
self.transform = None
|
1222 |
+
import albumentations as A
|
1223 |
+
|
1224 |
+
self.transform = A.Compose([
|
1225 |
+
A.CLAHE(p=0.01),
|
1226 |
+
A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.01),
|
1227 |
+
A.RandomGamma(gamma_limit=[80, 120], p=0.01),
|
1228 |
+
A.Blur(p=0.01),
|
1229 |
+
A.MedianBlur(p=0.01),
|
1230 |
+
A.ToGray(p=0.01),
|
1231 |
+
A.ImageCompression(quality_lower=75, p=0.01),],
|
1232 |
+
bbox_params=A.BboxParams(format='pascal_voc', label_fields=['class_labels']))
|
1233 |
+
|
1234 |
+
#logging.info(colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms if x.p))
|
1235 |
+
|
1236 |
+
def __call__(self, im, labels, p=1.0):
|
1237 |
+
if self.transform and random.random() < p:
|
1238 |
+
new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed
|
1239 |
+
im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
|
1240 |
+
return im, labels
|
1241 |
+
|
1242 |
+
|
1243 |
+
def create_folder(path='./new'):
|
1244 |
+
# Create folder
|
1245 |
+
if os.path.exists(path):
|
1246 |
+
shutil.rmtree(path) # delete output folder
|
1247 |
+
os.makedirs(path) # make new output folder
|
1248 |
+
|
1249 |
+
|
1250 |
+
def flatten_recursive(path='../coco'):
|
1251 |
+
# Flatten a recursive directory by bringing all files to top level
|
1252 |
+
new_path = Path(path + '_flat')
|
1253 |
+
create_folder(new_path)
|
1254 |
+
for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)):
|
1255 |
+
shutil.copyfile(file, new_path / Path(file).name)
|
1256 |
+
|
1257 |
+
|
1258 |
+
def extract_boxes(path='../coco/'): # from utils.datasets import *; extract_boxes('../coco128')
|
1259 |
+
# Convert detection dataset into classification dataset, with one directory per class
|
1260 |
+
|
1261 |
+
path = Path(path) # images dir
|
1262 |
+
shutil.rmtree(path / 'classifier') if (path / 'classifier').is_dir() else None # remove existing
|
1263 |
+
files = list(path.rglob('*.*'))
|
1264 |
+
n = len(files) # number of files
|
1265 |
+
for im_file in tqdm(files, total=n):
|
1266 |
+
if im_file.suffix[1:] in img_formats:
|
1267 |
+
# image
|
1268 |
+
im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB
|
1269 |
+
h, w = im.shape[:2]
|
1270 |
+
|
1271 |
+
# labels
|
1272 |
+
lb_file = Path(img2label_paths([str(im_file)])[0])
|
1273 |
+
if Path(lb_file).exists():
|
1274 |
+
with open(lb_file, 'r') as f:
|
1275 |
+
lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels
|
1276 |
+
|
1277 |
+
for j, x in enumerate(lb):
|
1278 |
+
c = int(x[0]) # class
|
1279 |
+
f = (path / 'classifier') / f'{c}' / f'{path.stem}_{im_file.stem}_{j}.jpg' # new filename
|
1280 |
+
if not f.parent.is_dir():
|
1281 |
+
f.parent.mkdir(parents=True)
|
1282 |
+
|
1283 |
+
b = x[1:] * [w, h, w, h] # box
|
1284 |
+
# b[2:] = b[2:].max() # rectangle to square
|
1285 |
+
b[2:] = b[2:] * 1.2 + 3 # pad
|
1286 |
+
b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int)
|
1287 |
+
|
1288 |
+
b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image
|
1289 |
+
b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
|
1290 |
+
assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}'
|
1291 |
+
|
1292 |
+
|
1293 |
+
def autosplit(path='../coco', weights=(0.9, 0.1, 0.0), annotated_only=False):
|
1294 |
+
""" Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
|
1295 |
+
Usage: from utils.datasets import *; autosplit('../coco')
|
1296 |
+
Arguments
|
1297 |
+
path: Path to images directory
|
1298 |
+
weights: Train, val, test weights (list)
|
1299 |
+
annotated_only: Only use images with an annotated txt file
|
1300 |
+
"""
|
1301 |
+
path = Path(path) # images dir
|
1302 |
+
files = sum([list(path.rglob(f"*.{img_ext}")) for img_ext in img_formats], []) # image files only
|
1303 |
+
n = len(files) # number of files
|
1304 |
+
indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split
|
1305 |
+
|
1306 |
+
txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files
|
1307 |
+
[(path / x).unlink() for x in txt if (path / x).exists()] # remove existing
|
1308 |
+
|
1309 |
+
print(f'Autosplitting images from {path}' + ', using *.txt labeled images only' * annotated_only)
|
1310 |
+
for i, img in tqdm(zip(indices, files), total=n):
|
1311 |
+
if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label
|
1312 |
+
with open(path / txt[i], 'a') as f:
|
1313 |
+
f.write(str(img) + '\n') # add image to txt file
|
1314 |
+
|
1315 |
+
|
1316 |
+
def load_segmentations(self, index):
|
1317 |
+
key = '/work/handsomejw66/coco17/' + self.img_files[index]
|
1318 |
+
#print(key)
|
1319 |
+
# /work/handsomejw66/coco17/
|
1320 |
+
return self.segs[key]
|