栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Docker实现Pytorch网络模型转Onnx格式,多种方法(opencv、onnxruntime、c++)调用Onnx

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Docker实现Pytorch网络模型转Onnx格式,多种方法(opencv、onnxruntime、c++)调用Onnx

文章目录
  • 0. 环境准备
  • 1. 数据准备
  • 2. Torchvison模型推理
  • 3. Pytorch -> Onnx
  • 4. Opencv 调用 Onnx
  • 5. onnxruntime 调用 Onnx

0. 环境准备
  • 1. 安装Docker:

    curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    或
    sudo apt-get install docker.io          # 上面由于curl问题  采用的该命令
    
  • 2. 添加库

    创建.sh文件

    touch script.sh
    

    编辑.sh文件

    gedit script.sh
    

    添加如下内容:

    curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | 
      sudo apt-key add -
    distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
    curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | 
      sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
    sudo apt-get update
    

    运行.sh文件 sh/bash

    sh script.sh
    
  • 将当前用户加入到docker用户组:
    1、创建docker组(安装docker时会自动创建,一般无需重新创建)

    $ sudo groupadd docker
    

    2、将当前用户加入到docker用户组

    $ sudo gpasswd -a  ${USER} docker
    

    3 、重新启动docker

     sudo service docker restart
     #或者执行以下命令,无须重新登录
     newgrp docker    # 建议用这个
    
  • 3. 创建镜像并同时创建容器:

    nldy@xiaoxie-Z10PE-D8-WS:~$ docker pull python:3.6
    nldy@xiaoxie-Z10PE-D8-WS:~$ docker images
    REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
    python       3.6       1498723101b5   9 days ago   902MB
    
  • 4.测试:

    nldy@xiaoxie-Z10PE-D8-WS:~$ docker run -it --gpus=all python:3.6 /bin/bash
    root@bda3e7f60958:/# nvidia-smi
    

过程中出现的问题:

  • curl: (1) Protocol “https” not supported or disabled in libcurl
    解决: https://blog.csdn.net/TommyXu8023/article/details/113446056?

  • openssl: relocation error: openssl: symbol EVP_mdc2 version
    OPENSSL_1_1_0 not defined in file libcrypto.so.1.1 with link time
    reference
    解决:https://blog.csdn.net/lc11535/article/details/111769295?

  • Job for docker.service failed because the control process exited with
    error code. See “systemctl status docker.service” and “journalctl
    -xe” for details.
    造成原因:

    nldy@xiaoxie-Z10PE-D8-WS:~$ sudo service docker restart
    

    改:

    nldy@xiaoxie-Z10PE-D8-WS:~$ newgrp docker
    
1. 数据准备

classification_classes_ILSVRC2012.txt: 下载链接
dog.jpg:

VGG16模型: https://download.pytorch.org/models/vgg16-397923af.pth

2. Torchvison模型推理

Opencv-Onnx.py

#-*- codeing = utf-8 -*-
#@Function: pytorch->onnx  opencv.dnn 调用 onnx   onnxruntime 调用 Onnx
#@Time : 2021/10/14 12:34
#@Author : yx
#@File : Opencv-Onnx.py
#@Software : PyCharm

import torch
import torchvision
import cv2
import onnx
import numpy as np
import timm
import os
from PIL import Image
from torchvision import transforms
import onnxruntime
from onnxsim import simplify
import torchvision.models as models

print(torch.__version__)
print(cv2.__version__)
print(np.__version__)
print(onnx.__version__)
classes = None
class_file = './classification_classes_ILSVRC2012.txt'
with open(class_file, 'rt') as f:
    classes = f.read().rstrip('n').split('n')

def init_model(model_name):
    if model_name == 'vgg16':
        path = "./vgg16.pth"
        vgg16 = models.vgg16(pretrained=False).eval()
        vgg16.load_state_dict(torch.load(path))
    model = vgg16
    dummy = torch.randn(1, 3, 224, 224)
    
    return model, dummy

model, dummy = init_model('vgg16')

img_file = './dog.jpg'

################################torchvison模型推理#####################################
transform = transforms.Compose([  # [1]
    transforms.Resize(256),  # [2]
    transforms.CenterCrop(224),  # [3]
    transforms.ToTensor(),  # [4]
    transforms.Normalize(  # [5]
        mean=[0.485, 0.456, 0.406],  # [6]
        std=[0.229, 0.224, 0.225]  # [7]
    )])

img = Image.open(img_file)
img_t = transform(img)            # 输入给模型的图像数据要先进行转换
batch_t = torch.unsqueeze(img_t, 0)
tc_out = model(batch_t).detach().cpu().numpy()
# Get a class with a highest score.
tc_out = tc_out.flatten()
classId = np.argmax(tc_out)
confidence = tc_out[classId]
print(confidence)

label = '%s: %.4f' % (classes[classId] if classes else 'Class #%d' % classId, confidence)
print(label)
################################ torchvison模型推理 ######################################

运行程序:

xiaoyuanzi@xiaoyuanzi-virtual-machine:~$ docker run  -v $PWD/python/onnx:/usr/src/python/onnx  -w /usr/src/python/onnx new_python:3.6 python Opencv-Onnx.py
1.9.1+cu102
4.5.3
1.19.5
1.10.1
19.722366
French bulldog: 19.7224
3. Pytorch -> onnx 4. Opencv 调用 onnx 5. onnxruntime 调用 onnx
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/342279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号