Skip to content

Commit b406063

Browse files
committed
s -> nano
1 parent f85c633 commit b406063

File tree

7 files changed

+128
-57
lines changed

7 files changed

+128
-57
lines changed

yolox_ros_py/exps/yolox_nano.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
# Copyright (c) Megvii, Inc. and its affiliates.
4+
5+
import os
6+
7+
import torch.nn as nn
8+
9+
from yolox.exp import Exp as MyExp
10+
11+
12+
class Exp(MyExp):
13+
def __init__(self):
14+
super(Exp, self).__init__()
15+
self.depth = 0.33
16+
self.width = 0.25
17+
self.input_size = (416, 416)
18+
self.random_size = (10, 20)
19+
self.mosaic_scale = (0.5, 1.5)
20+
self.test_size = (416, 416)
21+
self.mosaic_prob = 0.5
22+
self.enable_mixup = False
23+
self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0]
24+
25+
def get_model(self, sublinear=False):
26+
27+
def init_yolo(M):
28+
for m in M.modules():
29+
if isinstance(m, nn.BatchNorm2d):
30+
m.eps = 1e-3
31+
m.momentum = 0.03
32+
if "model" not in self.__dict__:
33+
from yolox.models import YOLOX, YOLOPAFPN, YOLOXHead
34+
in_channels = [256, 512, 1024]
35+
# NANO model use depthwise = True, which is main difference.
36+
backbone = YOLOPAFPN(
37+
self.depth, self.width, in_channels=in_channels,
38+
act=self.act, depthwise=True,
39+
)
40+
head = YOLOXHead(
41+
self.num_classes, self.width, in_channels=in_channels,
42+
act=self.act, depthwise=True
43+
)
44+
self.model = YOLOX(backbone, head)
45+
46+
self.model.apply(init_yolo)
47+
self.model.head.initialize_biases(1e-2)
48+
return self.model
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def generate_launch_description():
1919
parameters=[
2020
{"image_size/width": 640},
2121
{"image_size/height": 480},
22-
{"yolox_exp_py" : yolox_ros_share_dir+'/yolox_s.py'},
22+
{"yolox_exp_py" : yolox_ros_share_dir+'/yolox_nano.py'},
2323
{"device" : 'gpu'},
2424
{"fp16" : True},
2525
{"fuse" : False},
2626
{"legacy" : False},
2727
{"trt" : False},
28-
{"ckpt" : yolox_ros_share_dir+"/yolox_s.pth"},
28+
{"ckpt" : yolox_ros_share_dir+"/yolox_nano.pth"},
2929
{"conf" : 0.3},
3030
{"threshold" : 0.65},
3131
{"resize" : 640},

yolox_ros_py/launch/yolox_s_cpu.launch.py renamed to yolox_ros_py/launch/yolox_nano_cpu.launch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def generate_launch_description():
1919
parameters=[
2020
{"image_size/width": 640},
2121
{"image_size/height": 480},
22-
{"yolox_exp_py" : yolox_ros_share_dir+'/yolox_s.py'},
22+
{"yolox_exp_py" : yolox_ros_share_dir+'/yolox_nano.py'},
2323
{"device" : 'cpu'},
2424
{"fp16" : True},
2525
{"fuse" : False},
2626
{"legacy" : False},
2727
{"trt" : False},
28-
{"ckpt" : yolox_ros_share_dir+"/yolox_s.pth"},
28+
{"ckpt" : yolox_ros_share_dir+"/yolox_nano.pth"},
2929
{"conf" : 0.3},
3030
{"threshold" : 0.65},
3131
{"resize" : 640},

yolox_ros_py/launch/yolox_s_openvino.launch.py renamed to yolox_ros_py/launch/yolox_nano_openvino.launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def generate_launch_description():
2222
{"image_size/width": 640},
2323
{"image_size/height": 480},
2424
{"device" : 'CPU'},
25-
{"model_path" : yolox_ros_share_dir+"/yolox_s.xml"},
25+
{"model_path" : yolox_ros_share_dir+"/yolox_nano.onnx"},
2626
{"conf" : 0.3},
2727
],
2828
)

yolox_ros_py/setup.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,62 @@
77
package_name = 'yolox_ros_py'
88

99

10-
YOLOX_S_WEIGHTS = 'yolox_s.pth'
11-
YOLOX_M_WEIGHTS = 'yolox_m.pth'
12-
YOLOX_L_WEIGHTS = 'yolox_l.pth'
13-
YOLOX_X_WEIGHTS = 'yolox_x.pth'
14-
YOLOX_DARKNET53_WEIGHTS = 'yolox_darknet53.pth'
15-
YOLOX_NANO_WEIGHTS = 'yolox_nano.pth'
16-
YOLOX_TINY_WEIGHTS = 'yolox_tiny.pth'
10+
YOLOX_S = 'yolox_s'
11+
YOLOX_M = 'yolox_m'
12+
YOLOX_L = 'yolox_l'
13+
YOLOX_X = 'yolox_x'
14+
YOLOX_DARKNET53 = 'yolox_darknet53'
15+
YOLOX_NANO = 'yolox_nano'
16+
YOLOX_TINY = 'yolox_tiny'
17+
18+
PTH = '.pth'
19+
ONNX = '.onnx'
1720

1821
YOLOX_VERSION = '0.1.1rc0'
1922

2023
BASE_LINK = 'https://github.com/Megvii-BaseDetection/YOLOX/releases/download/'+YOLOX_VERSION+'/'
21-
YOLOX_S_WEIGHTS_URL = BASE_LINK + YOLOX_S_WEIGHTS
22-
YOLOX_M_WEIGHTS_URL = BASE_LINK + YOLOX_M_WEIGHTS
23-
YOLOX_L_WEIGHTS_URL = BASE_LINK + YOLOX_L_WEIGHTS
24-
YOLOX_X_WEIGHTS_URL = BASE_LINK + YOLOX_X_WEIGHTS
25-
YOLOX_DARKNET53_WEIGHTS_URL = BASE_LINK + YOLOX_DARKNET53_WEIGHTS
26-
YOLOX_NANO_WEIGHTS_URL = BASE_LINK + YOLOX_NANO_WEIGHTS
27-
YOLOX_TINY_WEIGHTS_URL = BASE_LINK + YOLOX_TINY_WEIGHTS
24+
# .pth
25+
YOLOX_S_URL = BASE_LINK + YOLOX_S + PTH
26+
YOLOX_M_URL = BASE_LINK + YOLOX_M + PTH
27+
YOLOX_L_URL = BASE_LINK + YOLOX_L + PTH
28+
YOLOX_X_URL = BASE_LINK + YOLOX_X + PTH
29+
YOLOX_X_URL = BASE_LINK + YOLOX_DARKNET53 + PTH
30+
YOLOX_NANO_URL = BASE_LINK + YOLOX_NANO + PTH
31+
YOLOX_TINY_URL = BASE_LINK + YOLOX_TINY + PTH
32+
# .onnx
33+
YOLOX_S_ONNX_URL = BASE_LINK + YOLOX_S + ONNX
34+
YOLOX_M_ONNX_URL = BASE_LINK + YOLOX_M + ONNX
35+
YOLOX_L_ONNX_URL = BASE_LINK + YOLOX_L + ONNX
36+
YOLOX_X_ONNX_URL = BASE_LINK + YOLOX_X + ONNX
37+
YOLOX_X_ONNX_URL = BASE_LINK + YOLOX_DARKNET53 + ONNX
38+
YOLOX_NANO_ONNX_URL = BASE_LINK + YOLOX_NANO + ONNX
39+
YOLOX_TINY_ONNX_URL = BASE_LINK + YOLOX_TINY + ONNX
2840

2941
BASE_PATH = os.getcwd() + '/../weights/'
3042
os.makedirs(BASE_PATH, exist_ok=True)
31-
YOLOX_S_WEIGHTS_PATH = BASE_PATH + YOLOX_S_WEIGHTS
32-
YOLOX_M_WEIGHTS_PATH = BASE_PATH + YOLOX_M_WEIGHTS
33-
YOLOX_L_WEIGHTS_PATH = BASE_PATH + YOLOX_L_WEIGHTS
34-
YOLOX_X_WEIGHTS_PATH = BASE_PATH + YOLOX_X_WEIGHTS
35-
YOLOX_DARKNET53_WEIGHTS_PATH = BASE_PATH + YOLOX_DARKNET53_WEIGHTS
36-
YOLOX_NANO_WEIGHTS_PATH = BASE_PATH + YOLOX_NANO_WEIGHTS
37-
YOLOX_TINY_WEIGHTS_PATH = BASE_PATH + YOLOX_TINY_WEIGHTS
38-
39-
# Download YOLOX-S Weights
40-
if not os.path.exists(YOLOX_S_WEIGHTS_PATH):
41-
urlretrieve(YOLOX_S_WEIGHTS_URL, YOLOX_S_WEIGHTS_PATH)
43+
# .pth
44+
YOLOX_S_PATH = BASE_PATH + YOLOX_S + PTH
45+
YOLOX_M_PATH = BASE_PATH + YOLOX_M + PTH
46+
YOLOX_L_PATH = BASE_PATH + YOLOX_L + PTH
47+
YOLOX_X_PATH = BASE_PATH + YOLOX_X + PTH
48+
YOLOX_DARKNET53_PATH = BASE_PATH + YOLOX_DARKNET53 + PTH
49+
YOLOX_NANO_PATH = BASE_PATH + YOLOX_NANO + PTH
50+
YOLOX_TINY_PATH = BASE_PATH + YOLOX_TINY + PTH
51+
# .onnx
52+
YOLOX_S_ONNX_PATH = BASE_PATH + YOLOX_S + ONNX
53+
YOLOX_M_ONNX_PATH = BASE_PATH + YOLOX_M + ONNX
54+
YOLOX_L_ONNX_PATH = BASE_PATH + YOLOX_L + ONNX
55+
YOLOX_X_ONNX_PATH = BASE_PATH + YOLOX_X + ONNX
56+
YOLOX_DARKNET53_ONNX_PATH = BASE_PATH + YOLOX_DARKNET53 + ONNX
57+
YOLOX_NANO_ONNX_PATH = BASE_PATH + YOLOX_NANO + ONNX
58+
YOLOX_TINY_ONNX_PATH = BASE_PATH + YOLOX_TINY + ONNX
4259

43-
# Download examples
44-
# if not os.path.exists(YOLOX_TINY_WEIGHTS_PATH):
45-
# urlretrieve(YOLOX_TINY_WEIGHTS_URL, YOLOX_TINY_WEIGHTS_PATH)
60+
# Download YOLOX-NANO Weights
61+
if not os.path.exists(YOLOX_NANO_PATH):
62+
urlretrieve(YOLOX_NANO_URL, YOLOX_NANO_PATH)
63+
# Download YOLOX-NANO ONNX
64+
if not os.path.exists(YOLOX_NANO_ONNX_PATH):
65+
urlretrieve(YOLOX_NANO_ONNX_URL, YOLOX_NANO_ONNX_PATH)
4666

4767

4868
setup(
@@ -56,7 +76,8 @@
5676
(os.path.join('share', package_name), glob('./launch/*.launch.py')),
5777
(os.path.join('share', package_name), glob('../weights/*.pth')),
5878
(os.path.join('share', package_name), glob('../weights/openvino/*')),
59-
(os.path.join('share', package_name), glob('./YOLOX/exps/default/*.py')),
79+
(os.path.join('share', package_name), glob('../weights/onnx/*')),
80+
(os.path.join('share', package_name), glob('./exps/*.py')),
6081
],
6182
install_requires=['setuptools'],
6283
zip_safe=True,
@@ -71,6 +92,7 @@
7192
'console_scripts': [
7293
'yolox_ros = '+package_name+'.yolox_ros:ros_main',
7394
'yolox_openvino = '+package_name+'.yolox_openvino:ros_main',
95+
'yolox_onnx = '+package_name+'.yolox_onnx:ros_main',
7496
],
7597
},
7698
)

yolox_ros_py/yolox_ros_py/yolox_openvino.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
# ROS2 rclpy -- Ar-Ray-code 2021
66
import rclpy
77

8-
import argparse
9-
import logging as log
10-
118
import cv2
129
import numpy as np
1310

@@ -119,6 +116,8 @@ def imageflow_callback(self,msg:Image) -> None:
119116
start_time = cv2.getTickCount()
120117
bboxes = BoundingBoxes()
121118
img_rgb = self.bridge.imgmsg_to_cv2(msg,"bgr8")
119+
# resize
120+
img_rgb = cv2.resize(img_rgb, (self.input_width, self.input_height))
122121

123122
origin_img = img_rgb
124123
_, _, h, w = self.net.input_info[self.input_blob].input_data.shape
@@ -178,17 +177,17 @@ def imageflow_callback(self,msg:Image) -> None:
178177
self.get_logger().info(f'Error: {e}')
179178
pass
180179

181-
# origin_img is output
182-
183-
def ros_main(args = None) -> None:
180+
def ros_main(args = None):
184181
rclpy.init(args=args)
185-
186-
yolox_ros_class = yolox_ros()
187-
rclpy.spin(yolox_ros_class)
188-
189-
yolox_ros_class.destroy_node()
190-
rclpy.shutdown()
191-
cv2.destroyAllWindows()
182+
ros_class = yolox_ros()
183+
184+
try:
185+
rclpy.spin(ros_class)
186+
except KeyboardInterrupt:
187+
pass
188+
finally:
189+
ros_class.destroy_node()
190+
rclpy.shutdown()
192191

193192
if __name__ == "__main__":
194193
ros_main()

yolox_ros_py/yolox_ros_py/yolox_ros.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def setting_yolox_exp(self) -> None:
132132

133133
# ==============================================================
134134

135-
WEIGHTS_PATH = '../../weights/yolox_s.pth'
135+
WEIGHTS_PATH = '../../weights/yolox_nano.pth'
136136

137137
self.declare_parameter('imshow_isshow',True)
138138

@@ -280,15 +280,17 @@ def imageflow_callback(self,msg:Image) -> None:
280280
except:
281281
pass
282282

283-
def ros_main(args = None) -> None:
283+
def ros_main(args = None):
284284
rclpy.init(args=args)
285-
286-
yolox_ros_class = yolox_ros()
287-
rclpy.spin(yolox_ros_class)
288-
289-
yolox_ros_class.destroy_node()
290-
cv2.destroyAllWindows()
291-
rclpy.shutdown()
292-
285+
ros_class = yolox_ros()
286+
287+
try:
288+
rclpy.spin(ros_class)
289+
except KeyboardInterrupt:
290+
pass
291+
finally:
292+
ros_class.destroy_node()
293+
rclpy.shutdown()
294+
293295
if __name__ == "__main__":
294296
ros_main()

0 commit comments

Comments
 (0)