遇到的问题
训练好的模型是pt格式,需要转成onnx进行部署和推理,转换脚本:
CALL activate pytorch
python export.py --weights ./runs/train/exp25/weights/best.pt --data ./data/tianjin_zuoyi_color.yaml --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 800 800 --include onnx --device 0
pause
运行后代码报错(两处异常):
解决方法
源代码中有两处问题,修改export.py,方式如下(注释掉的为源代码,下面为修正后代码):
# Update model
model.eval()
for k, m in model.named_modules():
# if isinstance(m, (Detect, V6Detect)):
if isinstance(m, Detect):
m.inplace = inplace
m.dynamic = dynamic
m.export = True
for _ in range(2):
y = model(im) # dry runs
if half and not coreml:
im, model = im.half(), model.half() # to FP16
# shape = tuple((y[0] if isinstance(y, tuple) else y).shape) # model output shape
shape = tuple((y[0][0] if isinstance(y, tuple) else y).shape) # model output shape
metadata = {'stride': int(max(model.stride)), 'names': model.names} # model metadata
LOGGER.info(f"\n{colorstr('PyTorch:')} starting from {file} with output shape {shape} ({file_size(file):.1f} MB)")
运行结果
成功导出onnx文件,onnx模型结构可通过网站:神经网络模型可视化工具 可视化查询