此页面由 AI 自动翻译。查看英文原版
DepthAI
  • DepthAI 组件
    • AprilTags
    • 基准测试
    • Camera
    • 校准
    • DetectionNetwork
    • Events
    • FeatureTracker
    • Gate
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • 杂项
    • 模型动物园
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • 点云
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • VideoEncoder
    • Visualizer
    • VSLAM
    • Warp
    • RVC2-specific
  • 高级教程
  • API 参考
  • 工具
软件栈

本页目录

  • 演示
  • 管道
  • 源代码

基准测试 NN

Supported on:RVC2RVC4
本示例展示了如何使用 BenchmarkOut 节点 BenchmarkIn 节点 来测量 NN 模型的性能。BenchmarkIn 以尽可能快的速度输出消息,用于测量 NN 模型的性能 (通过链接 BenchmarkOut -> NeuralNetwork -> BenchmarkIn)。

演示

yolov6-nano NN 模型在 OAK4 摄像头上应运行约 273 FPS,在 OAK 摄像头上应运行约 67 FPS。
Command Line
1Benchmark $ python3.9 benchmark_nn.py
2FPS is 273.2430114746094
3FPS is 273.161376953125
4FPS is 273.22802734375
此示例需要 DepthAI v3 API,请参阅 安装说明

管道

源代码

Python

Python
GitHub
1import depthai as dai
2import numpy as np
3
4
5# First prepare the model for benchmarking
6device = dai.Device()
7modelPath = dai.getModelFromZoo(dai.NNModelDescription("yolov6-nano", platform=device.getPlatformAsString()))
8modelArhive = dai.NNArchive(modelPath)
9inputSize = modelArhive.getInputSize()
10type = modelArhive.getConfig().model.inputs[0].preprocessing.daiType
11
12if type:
13    try:
14        frameType = getattr(dai.ImgFrame.Type, type)
15    except AttributeError:
16        type = None
17
18if not type:
19    if device.getPlatform() == dai.Platform.RVC2:
20        frameType = dai.ImgFrame.Type.BGR888p
21    else:
22        frameType = dai.ImgFrame.Type.BGR888i
23
24
25# Construct the input (white) image for benchmarking
26img = np.ones((inputSize[1], inputSize[0], 3), np.uint8) * 255
27inputFrame = dai.ImgFrame()
28inputFrame.setCvFrame(img, frameType)
29
30with dai.Pipeline(device) as p:
31    benchmarkOut = p.create(dai.node.BenchmarkOut)
32    benchmarkOut.setRunOnHost(False) # The node can run on host or on device
33    benchmarkOut.setFps(-1) # As fast as possible
34
35    neuralNetwork = p.create(dai.node.NeuralNetwork).build(benchmarkOut.out, modelArhive)
36
37    benchmarkIn = p.create(dai.node.BenchmarkIn)
38    benchmarkIn.setRunOnHost(False) # The node can run on host or on device
39    benchmarkIn.sendReportEveryNMessages(100)
40    benchmarkIn.logReportsAsWarnings(False)
41    neuralNetwork.out.link(benchmarkIn.input)
42
43    outputQueue = benchmarkIn.report.createOutputQueue()
44    inputQueue = benchmarkOut.input.createInputQueue()
45
46    p.start()
47    inputQueue.send(inputFrame) # Send the input image only once
48    while p.isRunning():
49        benchmarkReport = outputQueue.get()
50        assert isinstance(benchmarkReport, dai.BenchmarkReport)
51        print(f"FPS is {benchmarkReport.fps}")

C++

1#include <atomic>
2#include <csignal>
3#include <depthai/depthai.hpp>
4#include <iostream>
5#include <opencv2/opencv.hpp>
6
7std::atomic<bool> quitEvent(false);
8
9void signalHandler(int) {
10    quitEvent = true;
11}
12
13int main() {
14    signal(SIGTERM, signalHandler);
15    signal(SIGINT, signalHandler);
16
17    // First prepare the model for benchmarking
18    std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();
19
20    dai::NNModelDescription modelDescription;
21    modelDescription.model = "yolov6-nano";
22    modelDescription.platform = device->getPlatformAsString();
23
24    auto modelPath = getModelFromZoo(modelDescription);
25    dai::NNArchive modelArchive(modelPath);
26    auto inputSize = modelArchive.getInputSize().value();
27
28    dai::ImgFrame::Type frameType;
29    if(device->getPlatform() == dai::Platform::RVC2) {
30        frameType = dai::ImgFrame::Type::BGR888p;
31    } else {
32        frameType = dai::ImgFrame::Type::BGR888i;
33    }
34
35    // Construct the input (white) image for benchmarking
36    cv::Mat img(std::get<1>(inputSize), std::get<0>(inputSize), CV_8UC3, cv::Scalar(255, 255, 255));
37    auto inputFrame = std::make_shared<dai::ImgFrame>();
38    inputFrame->setCvFrame(img, frameType);
39
40    dai::Pipeline pipeline(device);
41
42    auto benchmarkOut = pipeline.create<dai::node::BenchmarkOut>();
43    benchmarkOut->setRunOnHost(false);  // The node can run on host or on device
44    benchmarkOut->setFps(-1);           // As fast as possible
45
46    auto neuralNetwork = pipeline.create<dai::node::NeuralNetwork>();
47    neuralNetwork->setNNArchive(modelArchive);
48
49    auto benchmarkIn = pipeline.create<dai::node::BenchmarkIn>();
50    benchmarkIn->setRunOnHost(false);  // The node can run on host or on device
51    benchmarkIn->sendReportEveryNMessages(100);
52    benchmarkIn->logReportsAsWarnings(false);
53
54    // Linking
55    benchmarkOut->out.link(neuralNetwork->input);
56    neuralNetwork->out.link(benchmarkIn->input);
57
58    auto outputQueue = benchmarkIn->report.createOutputQueue();
59    auto inputQueue = benchmarkOut->input.createInputQueue();
60
61    pipeline.start();
62    inputQueue->send(inputFrame);
63
64    while(pipeline.isRunning() && !quitEvent) {
65        auto benchmarkReport = outputQueue->get<dai::BenchmarkReport>();
66        std::cout << "FPS is " << benchmarkReport->fps << std::endl;
67    }
68
69    pipeline.stop();
70    pipeline.wait();
71
72    return 0;
73}

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。