注意!本篇首发于GiantPandaCV公众号,未经允许,不得转载!
需要的repo: 欢迎star
https://github.com/msnh2012/MsnhnetModelZoo
https://github.com/msnh2012/Msnhnet
https://github.com/msnh2012/CasiaLabeler
接上一篇使用yolov5训练自己的数据集。本篇将分windows和linux, pc和jetson nx平台分别给大家讲解如何使用Msnhnet部署yolov5.
在yolov5文件夹下打开终端。将best.pt拷贝至weights文件夹下。执行
python yolov5ToMsnhnet.py
yolov5ToMsnhnet.py文件内容:
from PytorchToMsnhnet import *
Msnhnet.Export = True
from models.experimental import attempt_load
import torch
weights = "weights/best.pt" # pt文件
msnhnetPath = "yolov5m.msnhnet" # 导出.msnhnet文件
msnhbinPath = "yolov5m.msnhbin" # 导出.msnhbin文件
model = attempt_load(weights, "cpu")
model.eval() # cpu模式,推理模式
img = torch.rand(512*512*3).reshape(1,3,512,512) #生成随机推理数据
tans(model,img,msnhnetPath,msnhbinPath) #模型转换
导出成功后会在文件夹下生成yolov5m.msnhnet和yolov5m.msnhbin文件。
1. 准备工作
(1) 安装Visual studio
(2) 安装cuda和cudnn, 此处请自行百度.
(3) 打开cmd, 输入nvcc.测试cuda是否安装完成。以下结果说明cuda配置完成。
(4) 安装cmake(建议3.17).
(5) clone Msnhnet
git clone https://github.com/msnh2012/Msnhnet.git
2. 编译OpenCV库
(1) 小编这里给大家准备好了OpenCV的源码文件,不用科学上网了。
链接:https://pan.baidu.com/s/1lpyNNdYqdKj8R-RCQwwCWg 提取码:6agk
(2) 打开cmake-gui.exe。
(3) 点击config选择安装的visual studio版本,选择x64(此处以VS2017为例),点击Finish,等待配置完成.
(4) 参数配置.
- CMAKE_INSTALL_PREFIX #指定安装位置,如: D:/libs/opencv
- CPU_BASELINE #选择AVX2(如果CPU支持AVX2加速)
- BUILD_TESTS #取消勾选
(5) 点击generate->Generating done.
(6) 点击Open Project.分别选择Debug右键生成。(此过程需要等待10min~60min不等,根据电脑配置)
(7) 右键安装。(会将编译好的可执行文件安装在指定安装位置,如:D:/libs/opencv)
(8) 重复6-7步选择Release版本进行编译安装。
(9) 指定OpenCV_DIR环境变量,用于CMakeList能使用FindPackage找到OpenCV.
(10) 指定Path环境变量.在Path环境变量下添加Opencv的bin文件夹位置,如: D:\libs\opencv\x64\vc15\bin
3. 编译Msnhnet库
(1) 打开cmake-gui.exe。
(2) 点击config选择安装的visual studio版本,选择x64(此处以VS2017为例),点击Finish,等待配置完成.
(3) 勾选以下参数。
- CMAKE_INSTALL_PREFIX #指定安装位置,如: D:/libs/Msnhnet
- BUILD_EXAMPLE #构建示例
- BUILD_SHARED_LIBS #构建动态链接库
- BUILD_USE_CUDNN #使用CUDNN
- BUILD_USE_GPU #使用GPU
- BUILD_USE_OPENCV #使用OPENCV
- ENABLE_OMP #使用OMP
- OMP_MAX_THREAD #使用最大核心数
(4) 点击generate->Generating done.
(5) 点击Open Project.分别选择Debug右键生成。
(6) 右键安装。(会将编译好的可执行文件安装在指定安装位置,如:D:/libs/Msnhnet)
(7) 重复6-7步选择Release版本进行编译安装.
(8) 指定Msnhnet_DIR环境变量,用于CMakeList能使用FindPackage找到Msnhnet.
(9)指定Path环境变量.在Path环境变量下添加Msnhnet的bin文件夹位置,如: D:\libs\Msnhnet\bin
(10) 测试。
yolov3_gpu D:/models
yolov3_gpu_fp16 D:/models #fp16推理
4. 使用C#部署Msnhnet
(1) clone MsnhnetSharp
git clone https://github.com/msnh2012/MsnhnetSharp
(2) 双击打开MsnhnetSharp.sln文件
MsnhnetSharp源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using static MsnhnetSharp.MsnhnetDef;
namespace MsnhnetSharp
{
public class Msnhnet
{
const string MsnhnetLib = "msnhnet.dll";
private const int MaxBBoxNum = 1024;
[DllImport(MsnhnetLib, EntryPoint = "initMsnhnet")]
static extern int _initMsnhnet();
[DllImport(MsnhnetLib, EntryPoint = "dispose")]
static extern int _dispose();
[DllImport(MsnhnetLib, EntryPoint = "withGPU")]
static extern int _withGPU(ref int GPU);
[DllImport(MsnhnetLib, EntryPoint = "withCUDNN")]
static extern int _withCUDNN(ref int CUDNN);
[DllImport(MsnhnetLib, EntryPoint = "getInputDim")]
static extern int _getInputDim(ref int width, ref int heigth, ref int channel);
[DllImport(MsnhnetLib, EntryPoint = "getCpuForwardTime")]
static extern int _getCpuForwardTime(ref float cpuForwardTime);
[DllImport(MsnhnetLib, EntryPoint = "getGpuForwardTime")]
static extern int _getGpuForwardTime(ref float getGpuForwardTime);
[DllImport(MsnhnetLib, EntryPoint = "buildMsnhnet")]
static extern int _buildMsnhnet(ref IntPtr msg, string msnhnet, string msnhbin, int useFp16, int useCudaOnly);
[DllImport(MsnhnetLib, EntryPoint = "runClassifyFile")]
static unsafe extern int _runClassifyFile(ref IntPtr msg, string imagePath, ref int bestIndex, PredDataType predDataType,
int runGPU, float* mean, float* std);
[DllImport(MsnhnetLib, EntryPoint = "runClassifyList")]
static unsafe extern int _runClassifyList(ref IntPtr msg, byte* data, int width, int height, int channel, ref int bestIndex, PredDataType predDataType,
int swapRGB, int runGPU, float* mean, float* std);
[DllImport(MsnhnetLib, EntryPoint = "runClassifyNoPred")]
static unsafe extern int _runClassifyNoPred(ref IntPtr msg, float* data, int len, ref int bestIndex, int runGPU);
[StructLayout(LayoutKind.Sequential)]
public struct BBox
{
public float x;
public float y;
public float w;
public float h;
public float conf;
public float bestClsConf;
public UInt32 bestClsIdx;
public float angle;
};
[StructLayout(LayoutKind.Sequential)]
public struct BBoxContainer
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxBBoxNum)]
public BBox[] boxes;
}
[DllImport(MsnhnetLib, EntryPoint = "runYoloFile")]
static extern int _runYoloFile(ref IntPtr msg, string imagePath, ref BBoxContainer bboxContainer, ref int detectedNum, int runGPU);
[DllImport(MsnhnetLib, EntryPoint = "runYoloList")]
static unsafe extern int _runYoloList(ref IntPtr msg, byte* data, int width, int height, int channel, ref BBoxContainer bboxContainer, ref int detectedNum, int swapRGB, int runGPU);
private bool netBuilt = false;
private bool netInited = false;
///
/// check GPU
///
///
static public bool WithGPU()
{
int GPU = 0;
_withGPU(ref GPU);
return (GPU == 1) ? true : false;
}
///
/// check CUDNN
///
///
static public bool WithCudnn()
{
int CUDNN = 0;
_withCUDNN(ref CUDNN);
return (CUDNN == 1) ? true : false;
}
///
/// Get input dim
///
///
public Dim GetInputDim()
{
if (netBuilt)
{
int width = 0;
int height = 0;
int channel = 0;
_getInputDim(ref width, ref height, ref channel);
Dim dim;
dim.width = width;
dim.height = height;
dim.channel = channel;
return dim;
}
else
{
throw new Exception("Net wasn't built yet");
}
}
///
/// Get cpu forward time
///
///
public float GetCpuForwardTime()
{
float time = 0;
_getCpuForwardTime(ref time);
return time;
}
///
/// Get gpu forward time
///
///
public float GetGpuForwardTime()
{
float time = 0;
if (_getGpuForwardTime(ref time) != 1)
{
throw new Exception("Msnhnet is not compiled with GPU mode!");
}
return time;
}
///
/// init net
///
public void InitNet()
{
if (_initMsnhnet() != 1)
{
throw new Exception("Init net Failed");
}
netInited = true;
}
///
/// build net
///
/// msnhnet file path
/// msnhbin file path
/// use fp16 or not
/// build with cudnn, but only use cuda
public void BuildNet(string msnhnet, string msnhbin, bool useFp16, bool useCudaOnly)
{
if (!netInited)
{
throw new Exception("Net wasn't inited yet");
}
IntPtr msg = new IntPtr();
if (_buildMsnhnet(ref msg, msnhnet, msnhbin, useFp16 ? 1 : 0, useCudaOnly ? 1 : 0) != 1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
netBuilt = true;
}
///
/// dispose net
///
public void Dispose()
{
_dispose();
}
///
/// forward net with image file, with preprocess
///
/// image file path
/// process function
/// run wit GPU
/// if normalize mean val
/// if normalize std val
///
public int RunClassifyFile(string imagePath, PredDataType predDataType, bool runGPU, float[] mean = null, float[] std = null)
{
if (!netBuilt)
{
throw new Exception("Net wasn't built yet");
}
IntPtr msg = new IntPtr();
int bestIndex = 0;
unsafe
{
if (predDataType == PredDataType.PRE_DATA_TRANSFORMED_FC3)
{
fixed (float* meanPtr = mean)
{
fixed (float* stdPtr = std)
{
if (_runClassifyFile(ref msg, imagePath, ref bestIndex, predDataType, runGPU ? 1 : 0, meanPtr, stdPtr) != 1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
}
}
}
else
{
if (_runClassifyFile(ref msg, imagePath, ref bestIndex, predDataType, runGPU ? 1 : 0, null, null) != 1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
}
}
return bestIndex;
}
///
/// forward net with image BitmapData, with preprocess
///
/// data
/// process function
/// net swap RGB or not
/// run wit GPU
/// if normalize mean val
/// if normalize std val
///
public int RunClassifyList(BitmapData bitmap, PredDataType predDataType, bool swapRGB, bool runGPU, float[] mean = null, float[] std = null)
{
if (!netBuilt)
{
throw new Exception("Net wasn't built yet");
}
IntPtr msg = new IntPtr();
int bestIndex = 0;
unsafe
{
if (predDataType == PredDataType.PRE_DATA_TRANSFORMED_FC3)
{
fixed (float* meanPtr = mean)
{
fixed (float* stdPtr = std)
{
if (_runClassifyList(ref msg, (byte*)bitmap.Scan0, bitmap.Width, bitmap.Height, bitmap.Stride / bitmap.Width, ref bestIndex, predDataType, swapRGB ? 1 : 0, runGPU ? 1 : 0, meanPtr, stdPtr) != 1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
}
}
}
else
{
if (_runClassifyList(ref msg, (byte*)bitmap.Scan0, bitmap.Width, bitmap.Height, bitmap.Stride / bitmap.Width, ref bestIndex, predDataType, swapRGB ? 1 : 0, runGPU ? 1 : 0, null, null) != 1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
}
}
return bestIndex;
}
///
/// forward net with float data, without preprocess
///
/// flaot data
/// run wit GPU
///
public int RunClassifyNoPred(float[] data, bool runGPU)
{
if (!netBuilt)
{
throw new Exception("Net wasn't built yet");
}
IntPtr msg = new IntPtr();
int bestIndex = 0;
unsafe
{
fixed (float* dataPtr = data)
{
if (_runClassifyNoPred(ref msg, dataPtr, data.Length, ref bestIndex, runGPU ? 1 : 0) != 1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
}
}
return bestIndex;
}
///
/// run yolo
///
/// image path
/// run with GPU
///
public List RunYoloFile(string imagePath, bool runGPU)
{
List bboxVec = new List();
IntPtr msg = new IntPtr();
BBoxContainer bboxContainer = new BBoxContainer();
int detectNum = 0;
if (!netBuilt)
{
throw new Exception("Net wasn't built yet");
}
if (_runYoloFile(ref msg, imagePath, ref bboxContainer, ref detectNum, runGPU?1:0)!=1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
for (int i = 0; i < detectNum; i++)
{
bboxVec.Add(bboxContainer.boxes[i]);
}
return bboxVec;
}
public List RunYoloList(BitmapData bitmap, bool swapRGB, bool runGPU)
{
if (!netBuilt)
{
throw new Exception("Net wasn't built yet");
}
List bboxVec = new List();
IntPtr msg = new IntPtr();
BBoxContainer bboxContainer = new BBoxContainer();
int detectNum = 0;
unsafe
{
if (_runYoloList(ref msg, (byte*)bitmap.Scan0, bitmap.Width, bitmap.Height, bitmap.Stride / bitmap.Width, ref bboxContainer, ref detectNum, swapRGB?1:0, runGPU?1:0) != 1)
{
string mstr = Marshal.PtrToStringAnsi(msg);
throw new Exception(mstr);
}
}
for (int i = 0; i < detectNum; i++)
{
bboxVec.Add(bboxContainer.boxes[i]);
}
return bboxVec;
}
}
}
(3) 选择x64平台和Release模式,右键生成MsnhnetSharp,再生成MsnhnetForm.
(4) 点击启动按钮。
(5) 在参数配置栏,分别指定msnhnetPath和msnhbinPath为之前导出的yolov5m的参数。然后将上一篇制作好的labels.txt文件,复制一份,重命名为labels.names.
(6) 点击初始化网络。等待初始化完成,init done.
(7) 点击读取图片, 选择那张bus.jpg.
(8) 点击yolo GPU(Yolo Detect GPU). 第一次推理时间较长。
(9) 点击重置图片。
(10) 再次点击yolo GPU(Yolo Detect GPU). 随后推理时间正常.
至此,使用C#部署Msnhnet完成,后续可以参考MsnhnetForm将MsnhnetSharp部署到你自己的工程中。
5. 使用CMake部署Msnhnet
工程文件源码:
链接:https://pan.baidu.com/s/1lpyNNdYqdKj8R-RCQwwCWg 提取码:6agk
(1) 新建MsnhnetPrj文件夹
(2) 将yolov5m.msnhnet,yolov5m.msnhbin,labels.txt拷贝到MsnhnetPrj文件夹内
(3) 新建CMakeLists.txt文件
cmake_minimum_required(VERSION 3.15)
project(yolov5m_msnhnet
LANGUAGES CXX C CUDA
VERSION 1.0)
find_package(OpenCV REQUIRED)
find_package(Msnhnet REQUIRED)
find_package(OpenMP REQUIRED)
add_executable(yolov5m_msnhnet yolov5m_msnhnet.cpp)
target_include_directories(yolov5m_msnhnet PUBLIC ${Msnhnet_INCLUDE_DIR})
target_link_libraries(yolov5m_msnhnet PUBLIC ${OpenCV_LIBS} Msnhnet)
(4) 新建yolov5m_msnhnet.cpp文件
#include
#include "Msnhnet/net/MsnhNetBuilder.h"
#include "Msnhnet/io/MsnhIO.h"
#include "Msnhnet/config/MsnhnetCfg.h"
#include "Msnhnet/utils/MsnhOpencvUtil.h"
void yolov5sGPUOpencv(const std::string& msnhnetPath, const std::string& msnhbinPath, const std::string& imgPath, const std::string& labelsPath)
{
try
{
Msnhnet::NetBuilder msnhNet;
Msnhnet::NetBuilder::setOnlyGpu(true);
//msnhNet.setUseFp16(true); //开启使用FP16推理
msnhNet.buildNetFromMsnhNet(msnhnetPath);
std::cout<<msnhNet.getLayerDetail();
msnhNet.loadWeightsFromMsnhBin(msnhbinPath);
std::vector<std::string> labels ;
Msnhnet::IO::readVectorStr(labels, labelsPath.data(), "\n");
Msnhnet::Point2I inSize = msnhNet.getInputSize();
std::vector<float> img;
std::vector<std::vector<Msnhnet::YoloBox>> result;
img = Msnhnet::OpencvUtil::getPaddingZeroF32C3(imgPath, cv::Size(inSize.x,inSize.y));
for (size_t i = 0; i < 10; i++)
{
auto st = Msnhnet::TimeUtil::startRecord();
result = msnhNet.runYoloGPU(img);
std::cout<<"time : " << Msnhnet::TimeUtil::getElapsedTime(st) <<"ms"<<std::endl<<std::flush;
}
cv::Mat org = cv::imread(imgPath);
Msnhnet::OpencvUtil::drawYoloBox(org,labels,result,inSize);
cv::imshow("test",org);
cv::waitKey();
}
catch (Msnhnet::Exception ex)
{
std::cout<<ex.what()<<" "<<ex.getErrFile() << " " <<ex.getErrLine()<< " "<<ex.getErrFun()<<std::endl;
}
}
int main(int argc, char** argv)
{
std::string msnhnetPath = "yolov5m.msnhnet";
std::string msnhbinPath = "yolov5m.msnhbin";
std::string labelsPath = "labels.txt";
std::string imgPath = "bus.jpg";
yolov5sGPUOpencv(msnhnetPath, msnhbinPath, imgPath,labelsPath);
getchar();
return 0;
}
(5) 配置CMake
打开cmake-gui.exe,按以下配置.点击Config.Generate
(6) 编译,点击Open Project,选择Release模式,参靠之前编译Msnhnet直接生成。
(7) 拷贝可执行文件。
从MsnhnetPrj/build/Release/yolov5m_msnhnet.exe拷贝到MsnhnetPrj目录。
(8) 部署结果
双击yolov5m_msnhnet.exe查看部署结果
1. 准备工作
一般来说,Jetson都已经自带了cuda和cudnn,故不用专门安装。
sudo apt-get install build-essential
sudo apt-get install libopencv
2. 编译Msnhnet库
(1) 终端打开cmake-gui。
(2) 点击config选择安装的visual studio版本,选择x64(此处以VS2017为例),点击Finish,等待配置完成.
(3) 勾选以下参数。
- CMAKE_INSTALL_PREFIX #指定安装位置,如: D:/libs/Msnhnet
- BUILD_EXAMPLE #构建示例
- BUILD_SHARED_LIBS #构建动态链接库
- BUILD_USE_CUDNN #使用CUDNN
- BUILD_USE_GPU #使用GPU
- BUILD_USE_NEON #使用neon加速
- BUILD_USE_OPENCV #使用OPENCV
- ENABLE_OMP #使用OMP
- OMP_MAX_THREAD #使用最大核心数
(4) 点击generate->Generating done.
(5) 在Msnhnet/build文件夹中打开终端。
make -j
sudo make install
(7) 配置系统环境变量
sudo gedit /etc/ld.so.conf.d/usr.confg
# 添加: /usr/local/lib
sudo ldconfig
(8) 测试。
cd /usr/local/bin
yolov3_gpu /home/xxx/models
yolov3_gpu_fp16 /home/xxx/models #fp16推理
3. 使用CMake部署Msnhnet
工程文件源码:
链接:https://pan.baidu.com/s/1lpyNNdYqdKj8R-RCQwwCWg 提取码:6agk
(1) 新建MsnhnetPrj文件夹
(2) 将yolov5m.msnhnet,yolov5m.msnhbin,labels.txt拷贝到MsnhnetPrj文件夹内
(3) 新建CMakeLists.txt文件
cmake_minimum_required(VERSION 3.15)
project(yolov5m_msnhnet
LANGUAGES CXX C CUDA
VERSION 1.0)
find_package(OpenCV REQUIRED)
find_package(Msnhnet REQUIRED)
find_package(OpenMP REQUIRED)
add_executable(yolov5m_msnhnet yolov5m_msnhnet.cpp)
target_include_directories(yolov5m_msnhnet PUBLIC ${Msnhnet_INCLUDE_DIR})
target_link_libraries(yolov5m_msnhnet PUBLIC ${OpenCV_LIBS} Msnhnet)
(4) 新建yolov5m_msnhnet.cpp文件
#include
#include "Msnhnet/net/MsnhNetBuilder.h"
#include "Msnhnet/io/MsnhIO.h"
#include "Msnhnet/config/MsnhnetCfg.h"
#include "Msnhnet/utils/MsnhOpencvUtil.h"
void yolov5sGPUOpencv(const std::string& msnhnetPath, const std::string& msnhbinPath, const std::string& imgPath, const std::string& labelsPath)
{
try
{
Msnhnet::NetBuilder msnhNet;
Msnhnet::NetBuilder::setOnlyGpu(true);
//msnhNet.setUseFp16(true); //开启使用FP16推理
msnhNet.buildNetFromMsnhNet(msnhnetPath);
std::cout<<msnhNet.getLayerDetail();
msnhNet.loadWeightsFromMsnhBin(msnhbinPath);
std::vector<std::string> labels ;
Msnhnet::IO::readVectorStr(labels, labelsPath.data(), "\n");
Msnhnet::Point2I inSize = msnhNet.getInputSize();
std::vector<float> img;
std::vector<std::vector<Msnhnet::YoloBox>> result;
img = Msnhnet::OpencvUtil::getPaddingZeroF32C3(imgPath, cv::Size(inSize.x,inSize.y));
for (size_t i = 0; i < 10; i++)
{
auto st = Msnhnet::TimeUtil::startRecord();
result = msnhNet.runYoloGPU(img);
std::cout<<"time : " << Msnhnet::TimeUtil::getElapsedTime(st) <<"ms"<<std::endl<<std::flush;
}
cv::Mat org = cv::imread(imgPath);
Msnhnet::OpencvUtil::drawYoloBox(org,labels,result,inSize);
cv::imshow("test",org);
cv::waitKey();
}
catch (Msnhnet::Exception ex)
{
std::cout<<ex.what()<<" "<<ex.getErrFile() << " " <<ex.getErrLine()<< " "<<ex.getErrFun()<<std::endl;
}
}
int main(int argc, char** argv)
{
std::string msnhnetPath = "../yolov5m.msnhnet";
std::string msnhbinPath = "../yolov5m.msnhbin";
std::string labelsPath = "../labels.txt";
std::string imgPath = "../bus.jpg";
yolov5sGPUOpencv(msnhnetPath, msnhbinPath, imgPath,labelsPath);
getchar();
return 0;
}
(5) 编译,在MsnhnetPrj文件夹下打开终端
mkdir build
cd build
make
./yolov5m_msnhnet
(6) 部署结果
和Jetson Nx部署类似, 主要区别是先要在Linux上配置好cuda和cudnn, 然后卸载CMake, 安装CMake 3.17版本. 其它的和Jestson NX一样.(ps. 在CMake参数配置里没有NEON项,此为ARM平台专有)
到此,使用Msnhnet从0到部署Yolov5网络完成。