用C++写一个部署在nvidia开发板上的yolov5模型推理代码
时间: 2023-05-24 14:03:32 浏览: 280
yolo5_class:yolov5的C ++类,方便其他C ++程序调用,懂的都懂
A: 首先,需要安装CUDA和CUDNN的相关库,并下载yolov5的代码仓库。然后,按照以下步骤来编写部署在nvidia开发板上的yolov5模型推理代码。
1. 导入必要的头文件
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <cuda_runtime_api.h>
#include <cublas_v2.h>
#include <curand.h>
#include <cudnn.h>
#include "darknet.h"
```
2. 定义模型类和相关参数
```c
typedef struct{
network net;
float *output;
float *input;
cudaStream_t stream;
} yolov5;
#define BATCH 1
#define DEVICE 0
#define NMS_THRESH 0.45
#define CONF_THRESH 0.25
#define CLASS_NUM 80
#define ANCHOR_NUM 3
#define INPUT_H 640
#define INPUT_W 640
#define CLASS_NAME_FILE "./coco.names"
#define WEIGHTS_FILE "./yolov5s.weights"
```
3. 加载模型
```c
void load_model(yolov5 *model, char *classes_path, char *weights_path){
// 加载类别名
char **names = get_labels(classes_path);
// 加载模型
model->net = parse_network_cfg(cfg_path);
if(weights_path){
load_weights(&model->net, weights_path);
}
// 设置输入层
model->input = (float *)calloc(model->net.batch * model->net.inputs, sizeof(float));
cudaMalloc((void **)&model->output, model->net.batch * model->net.outputs * sizeof(float));
cudaStreamCreate(&model->stream);
set_batch_network(&model->net, BATCH);
model->net.layers[model->net.n - 1].classes = CLASS_NUM;
model->net.layers[model->net.n - 1].anchor_num = ANCHOR_NUM;
model->net.layers[model->net.n - 1].confidence_thresh = CONF_THRESH;
model->net.layers[model->net.n - 1].nms_thresh = NMS_THRESH;
model->net.layers[model->net.n - 1].mask = (int *)calloc(ANCHOR_NUM, sizeof(int));
for(int i = 0; i < ANCHOR_NUM; i++) model->net.layers[model->net.n - 1].mask[i] = i;
srand(time(NULL));
}
```
4. 推理函数
```c
void yolov5_inference(yolov5 *model, char *img_path){
// 加载图片
image img = load_image_color(img_path, 0, 0);
// 缩放图片
image sized = resize_image(img, INPUT_W, INPUT_H);
// 将数据写入输入层
fill_cuda_data(sized.data, INPUT_H * INPUT_W * 3, model->input, INPUT_H * INPUT_W * 3 * BATCH, model->stream);
// 进行推理
forward_network(&model->net, model->input);
// 获取输出结果
get_network_boxes(&model->net, img.w, img.h, CONF_THRESH, model->net.layers[model->net.n - 1].mask, 0, 1, model->output, 1, &model->net.layers[model->net.n - 1], model->net.classes, model->net.outputs, 1);
// 进行非极大值抑制
do_nms_sort(model->net.hold_cpu, model->net.hold_gpu, model->net.batch, model->net.layers[model->net.n - 1].classes, model->net.layers[model->net.n - 1].w, model->net.layers[model->net.n - 1].h, model->output, model->net.layers[model->net.n - 1].nms, CLASS_NUM, NMS_THRESH);
// 输出结果
draw_detections(img, model->net.hold_cpu, model->net.darknet_gpu, model->net.layers[model->net.n - 1], CLASS_NUM, names, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
}
```
5. main函数
```c
int main(int argc, char **argv){
yolov5 model;
// 加载模型
load_model(&model, CLASS_NAME_FILE, WEIGHTS_FILE);
// 进行推理
yolov5_inference(&model, argv[1]);
// 释放资源
cudaFree(model.input);
cudaFree(model.output);
cudaStreamDestroy(model.stream);
free_network(model.net);
return 0;
}
```
以上就是用C语言编写的在nvidia开发板上部署yolov5模型推理代码。
阅读全文