DepthAI
  • DepthAI Components
    • AprilTags
    • Benchmark
    • Camera
    • Calibration
    • DetectionNetwork
    • Events
    • FeatureTracker
    • Gate
    • HostNodes
    • ImageAlign
    • ImageManip
    • IMU
    • Misc
    • Model Zoo
    • NeuralDepth
    • NeuralNetwork
    • ObjectTracker
    • RecordReplay
    • RGBD
    • Script
    • SpatialDetectionNetwork
    • SpatialLocationCalculator
    • StereoDepth
    • Sync
    • VideoEncoder
    • Visualizer
    • Warp
    • RVC2-specific
  • Advanced Tutorials
  • API Reference
  • Tools
Software Stack

ON THIS PAGE

  • Demo
  • Pipeline
  • Source code

Benchmark NN

Supported on:RVC2RVC4
This example showcases how to use both the BenchmarkOut node and the BenchmarkIn node to measure the performance of a NN model.BenchmarkIn outputs messages as fast as possible, which is used to measure the performance of a NN model (by linking BenchmarkOut -> NeuralNetwork -> BenchmarkIn).

Demo

The yolov6-nano NN model should run at ~273 FPS on an OAK4 camera, and at ~67 FPS on an OAK camera.
Command Line
1Benchmark $ python3.9 benchmark_nn.py
2FPS is 273.2430114746094
3FPS is 273.161376953125
4FPS is 273.22802734375
This example requires the DepthAI v3 API, see installation instructions.

Pipeline

Source code

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}

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.