YOLOV5转换libtorch(GPU)标准代码

YOLOV5转换libtorch(GPU)标准代码

最近整理了一些转模型经常碰到的问题让我很是苦恼,在次整理给大家,希望大家少走弯路,早日项目娄底。以下都是在c++中调用libtorch模型时出错。

问题总汇

我们要怎么确定是不是我们的模型出错了呢? 一般模型出错都是有窍门的。他一般都会伴随着C10这几个单词来的,要不就是torch::jit。

最常见也是最简单的报错

error while loading shared libraries: libtorch_cuda.so: cannot open shared object file: No such file or directory

这个保存原因就要看看你下载的libtorch是不是gpu版本的。在libtorch中有一个build-version。打开里面有libtorch+cu才是gpu版本的,才可以使用gpu。调用方法如下:

//m_modelPath为模型的路径
m_module = torch::jit::load(m_modelPath);
    //m_module.to(m_deviceType);
m_module.to(at::kCUDA);

找不到模型

terminate called after throwing an instance of ‘c10::Error’
what(): open file failed because of errno 2 on fopen: , file path:
这也是大家会经常碰到的错误,这里建议直接写绝对路径

模型转换gpu版本不对

terminate called after throwing an instance of ‘c10::Error’
what(): isTuple() INTERNAL ASSERT FAILED at “/home/ls/sdk/libtorch/include/ATen/core/ivalue_inl.h”:931, please report a bug to PyTorch. Expected Tuple but got GenericList

这个错误很是恼火,我把底层翻了个地朝天,终于找到了。这里大概意思就是模型中没有使用Gpu.
这里我把我的转换代码贴出来,大家可以参考。

import argparse
import sys
import time

sys.path.append('./')  # to run '$ python *.py' files in subdirectories

import torch
import torch.nn as nn
#这里添加这个是防止找不到model路径
sys.path.append('/media/ls/5f831db7-8671-43b0-8d53-0ad743c608e1/3Ddedict/axleDet_yolov5_dianyun5')
sys.path.append('/media/ls/5f831db7-8671-43b0-8d53-0ad743c608e1/3Ddedict/axleDet_yolov5_dianyun5/models')
import models
from models.experimental import attempt_load
from utils.activations import Hardswish, SiLU
from utils.general import set_logging, check_img_size
from utils.torch_utils import select_device
#输出torch是否支持cuda。输出1为支持,0为不支持。
print(torch.cuda.is_available())
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', type=str, default='需要转换的模型路径', help='weights path')  # from yolov5/models/
    #图片大小很重要 要和部署时的大小一致
    parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size')  # height, width
    #这里默认为1
    parser.add_argument('--batch-size', type=int, default=1, help='batch size')
    #生成onnx模型,默认生成。
    parser.add_argument('--dynamic', action='store_true', help='dynamic ONNX axes')
    parser.add_argument('--grid', action='store_true', help='export Detect() layer grid')
    #官方给给出的控制gpu,cpu的代码段。问题就出现在这里,所以为直接注释,具体怎么去调用gpu,大家往下看,我写了个最简单也是最不容易出错的。
    #parser.add_argument('--device', default='0', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    opt = parser.parse_args()
    opt.img_size *= 2 if len(opt.img_size) == 1 else 1  # expand
    print(opt)
    set_logging()
    t = time.time()
    #这里,如果检测到有cuda就使用cuda否则使用cpu
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print(device)
    # Load PyTorch model
    #device = select_device(opt.device)
    #map_location=torch.device('cuda') 这里直接使用cuda进行前向传播,这是一定不会出错的。
    model = attempt_load(opt.weights, map_location=torch.device('cuda'))  # load FP32 model
    labels = model.names

    # Checks
    gs = int(max(model.stride))  # grid size (max stride)
    opt.img_size = [check_img_size(x, gs) for x in opt.img_size]  # verify img_size are gs-multiples

    # Input
    #把图片直接放入cuda进行推理,减少犯错可能。
    img = torch.zeros(opt.batch_size, 3, *opt.img_size).to(device='cuda')  # image size(1,3,320,192) iDetection

    # Update model
    for k, m in model.named_modules():
        m._non_persistent_buffers_set = set()  # pytorch 1.6.0 compatibility
        if isinstance(m, models.common.Conv):  # assign export-friendly activations
            if isinstance(m.act, nn.Hardswish):
                m.act = Hardswish()
            elif isinstance(m.act, nn.SiLU):
                m.act = SiLU()
        # elif isinstance(m, models.yolo.Detect):
        #     m.forward = m.forward_export  # assign forward (optional)
    #model.model[-1].export = not opt.grid  # set Detect() layer grid export
    model.model[-1].export = False
    y = model(img)  # dry run

    # TorchScript export
    try:
    	
        print('\nStarting TorchScript export with torch %s...' % torch.__version__)
       #你要起的给生成模型起的名字。
        f = opt.weights.replace('.pt', '.torchscript.pt')  # filename
        ts = torch.jit.trace(model, img)
        ts.save(f)
        print('TorchScript export success, saved as %s' % f)
    except Exception as e:
        print('TorchScript export failure: %s' % e)

    # ONNX export
    try:
        import onnx

        print('\nStarting ONNX export with onnx %s...' % onnx.__version__)
        f = opt.weights.replace('.pt', '.onnx')  # filename
        torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
                          output_names=['classes', 'boxes'] if y is None else ['output'],
                          dynamic_axes={'images': {0: 'batch', 2: 'height', 3: 'width'},  # size(1,3,640,640)
                                        'output': {0: 'batch', 2: 'y', 3: 'x'}} if opt.dynamic else None)

        # Checks
        onnx_model = onnx.load(f)  # load onnx model
        onnx.checker.check_model(onnx_model)  # check onnx model
        # print(onnx.helper.printable_graph(onnx_model.graph))  # print a human readable model
        print('ONNX export success, saved as %s' % f)
    except Exception as e:
        print('ONNX export failure: %s' % e)

    # CoreML export
    try:
        import coremltools as ct

        print('\nStarting CoreML export with coremltools %s...' % ct.__version__)
        # convert model from torchscript and apply pixel scaling as per detect.py
        model = ct.convert(ts, inputs=[ct.ImageType(name='image', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])])
        f = opt.weights.replace('.pt', '.mlmodel')  # filename
        model.save(f)
        print('CoreML export success, saved as %s' % f)
    except Exception as e:
        print('CoreML export failure: %s' % e)

    # Finish
    print('\nExport complete (%.2fs). Visualize with https://github.com/lutzroeder/netron.' % (time.time() - t))

还有一些其他问题参考

https://blog.csdn.net/ll_cookie/article/details/88997254
这名博主也收集了一些错误,大家可以参考。
例如:
错误1:ValueError: substring not found
原因:forward 函数里不能有中文注释

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
以下是将YOLOv5转换为ONNX并在GPU上运行的示例代码: ```python import torch import onnx import onnxruntime from models.experimental import attempt_load from utils.general import non_max_suppression # Load YOLOv5 model weights = 'path/to/weights.pt' device = torch.device('cuda:0') # GPU model = attempt_load(weights, map_location=device) # load FP32 model model.to(device).eval() # Dummy input batch_size = 1 input_size = (640, 640) dummy_input = torch.randn(batch_size, 3, input_size[0], input_size[1], device=device) # Convert to ONNX input_names = ['input'] output_names = ['output'] onnx_path = 'path/to/model.onnx' dynamic_axes = {'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}} torch.onnx.export(model, dummy_input, onnx_path, opset_version=11, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes) # Load ONNX model sess_options = onnxruntime.SessionOptions() sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL sess = onnxruntime.InferenceSession(onnx_path, sess_options=sess_options) # Run inference on GPU input_data = dummy_input.cpu().numpy() # convert to numpy array outputs = sess.run(None, {input_names[0]: input_data}) # run inference detections = non_max_suppression(torch.from_numpy(outputs[0]), conf_thres=0.25, iou_thres=0.45) # post-process detections ``` 需要注意的是,这个示例代码假定您已经安装了YOLOv5代码,并将其放在了Python模块中,可以在`from models.experimental import attempt_load`中导入。如果您没有安装YOLOv5代码,您需要将其下载并放在正确的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值