0. 环境准备
-
1. 安装Docker: curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
或
sudo apt-get install docker.io
-
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:/
过程中出现的问题:
-
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
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'
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)])
img = Image.open(img_file)
img_t = transform(img)
batch_t = torch.unsqueeze(img_t, 0)
tc_out = model(batch_t).detach().cpu().numpy()
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)
运行程序:
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
|