源码
https://github.com/qingzhouzhen/CenterNet,fork自https://github.com/xingyizhou/CenterNet, 2019/10/8
1 |
|
1 | python main.py ctdet --arch dlav0_34 --exp_id coco_dla_1x --batch_size 64 --master_batch 16 --lr 5e-4 --gpus 0,1,2,3 --num_workers 16 --resume |
之所以用dlav0_34是因为dla中含有1
2
3
4
5
6
训练完后在```path/CenterNet/exp/ctdet/coco_dla_1x```路径下生成```model_best.pth```和```model_last.pth```两个模型
#### 测试
使用作者提供的demo.py脚本进行测试
ctdet –arch dlav0_34 –demo /data0/hhq/project/CenterNet/images/ –load_model ../models/model_best.pth –gpus 31
2
3
#### 转onnx
使用脚本
ctdet –arch dlav0trans_34 –demo /data0/hhq/project/CenterNet/images/ –load_model ../models/model_best.pth –gpu 31
2
修改```dlav0.py DLASeg```类的```forward```函数,修改成,这样返回的就不是3个heatmap,而是4个,最后一个是第一个maxpool之后的结果
def forward(self, x):
x = self.base(x)
x = self.dla_up(x[self.first_level:])
# x = self.fc(x)
# y = self.softmax(self.up(x))
ret = []
for head in self.heads:
ret.append(self.__getattr__(head)(x))
sigmoid = ret[0].sigmoid_()
hmax = nn.functional.max_pool2d(
sigmoid, (3, 3), stride=1, padding=1)
ret[0] = hmax
ret.append(sigmoid)
return [ret]
1 |
|
import _init_paths
import os
import cv2
from opts import opts
from detectors.detector_factory import detector_factory
from torch.autograd import Variable
import torch
image_ext = [‘jpg’, ‘jpeg’, ‘png’, ‘webp’]
video_ext = [‘mp4’, ‘mov’, ‘avi’, ‘mkv’]
time_stats = [‘tot’, ‘load’, ‘pre’, ‘net’, ‘dec’, ‘post’, ‘merge’]
def demo(opt):
os.environ[‘CUDA_VISIBLE_DEVICES’] = opt.gpus_str
opt.debug = max(opt.debug, 1)
Detector = detector_factory[opt.task]
detector = Detector(opt)
torch_model = detector.model
c, h, w = (3, 512, 512)
dummy_input = Variable(torch.randn(1, c, h, w, device=’cuda’))
torch.onnx.export(torch_model, dummy_input, “/data0/hhq/project/CenterNet/models/dlav0_opt_80.onnx”,
verbose=True, export_params=True) #, operator_export_type=OperatorExportTypes.ONNX_ATEN)
if name == ‘main‘:
opt = opts().init()
demo(opt)`
转完后生成.onnx结尾的文件
onnx转TensorRT
TensorRT-5.1.2
加载RT模型并推理
自己写的ctdecode