此页面由 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
    • 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 <depthai/depthai.hpp>
2#include <iostream>
3#include <opencv2/opencv.hpp>
4
5int main() {
6    // First prepare the model for benchmarking
7    std::shared_ptr<dai::Device> device = std::make_shared<dai::Device>();
8
9    dai::NNModelDescription modelDescription;
10    modelDescription.model = "yolov6-nano";
11    modelDescription.platform = device->getPlatformAsString();
12
13    auto modelPath = getModelFromZoo(modelDescription);
14    dai::NNArchive modelArchive(modelPath);
15    auto inputSize = modelArchive.getInputSize().value();
16
17    dai::ImgFrame::Type frameType;
18    if(device->getPlatform() == dai::Platform::RVC2) {
19        frameType = dai::ImgFrame::Type::BGR888p;
20    } else {
21        frameType = dai::ImgFrame::Type::BGR888i;
22    }
23
24    // Construct the input (white) image for benchmarking
25    cv::Mat img(std::get<1>(inputSize), std::get<0>(inputSize), CV_8UC3, cv::Scalar(255, 255, 255));
26    auto inputFrame = std::make_shared<dai::ImgFrame>();
27    inputFrame->setCvFrame(img, frameType);
28
29    dai::Pipeline pipeline(device);
30
31    auto benchmarkOut = pipeline.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    auto neuralNetwork = pipeline.create<dai::node::NeuralNetwork>();
36    neuralNetwork->setNNArchive(modelArchive);
37
38    auto benchmarkIn = pipeline.create<dai::node::BenchmarkIn>();
39    benchmarkIn->setRunOnHost(false);  // The node can run on host or on device
40    benchmarkIn->sendReportEveryNMessages(100);
41    benchmarkIn->logReportsAsWarnings(false);
42
43    // Linking
44    benchmarkOut->out.link(neuralNetwork->input);
45    neuralNetwork->out.link(benchmarkIn->input);
46
47    auto outputQueue = benchmarkIn->report.createOutputQueue();
48    auto inputQueue = benchmarkOut->input.createInputQueue();
49
50    pipeline.start();
51    inputQueue->send(inputFrame);
52
53    while(pipeline.isRunning()) {
54        auto benchmarkReport = outputQueue->get<dai::BenchmarkReport>();
55        std::cout << "FPS is " << benchmarkReport->fps << std::endl;
56    }
57
58    return 0;
59}

需要帮助?

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