neural_network_test.cpp
Go to the documentation of this file.
1 #include <catch2/catch_all.hpp>
2 
3 // std
4 #include <atomic>
5 #include <iostream>
6 
7 // Include depthai library
8 #include <depthai/depthai.hpp>
9 
10 const auto MOBILENET_BLOB_PATH = BLOB_PATH;
11 const auto MOBILENET_WIDTH = 300;
12 const auto MOBILENET_HEIGHT = 300;
13 const auto MOBILENET_CHANNEL = 3;
15 const auto MOBILENET_INPUT_TENSOR = "data";
16 const auto MOBILENET_OUTPUT_TENSOR = "detection_out";
17 
19  dai::Pipeline p;
20  auto x_in = p.create<dai::node::XLinkIn>();
21  auto x_out = p.create<dai::node::XLinkOut>();
22  auto nn = p.create<dai::node::NeuralNetwork>();
23 
24  // Load blob
25  if(manualBlob) {
26  dai::OpenVINO::Blob blob(BLOB_PATH);
27  nn->setBlob(std::move(blob));
28  } else {
29  nn->setBlobPath(BLOB_PATH);
30  }
31  // Set input stream
32  x_in->setStreamName("input");
33  // Set output stream
34  x_out->setStreamName("output");
35 
36  // Link nodes
37  x_in->out.link(nn->input);
38  nn->out.link(x_out->input);
39 
40  return p;
41 }
42 
43 void test(bool manualBlob) {
44  using namespace std::chrono;
45  using namespace std::chrono_literals;
46 
47  auto pipeline = createNeuralNetworkPipeline(manualBlob);
48 
49  dai::Device device(pipeline.getOpenVINOVersion());
50 
51  std::atomic<bool> receivedLogMessage{false};
52 
53  // no warnings should appear
55  device.addLogCallback([&receivedLogMessage](dai::LogMessage msg) {
56  if(msg.level >= dai::LogLevel::WARN) {
57  receivedLogMessage = true;
58  }
59  });
60 
61  // Start pipeline and feed correct sized data in various forms (Planar BGR 300*300*3 for mobilenet)
62  device.startPipeline(pipeline);
63 
64  // Iterate 10 times
65  for(int i = 0; i < 10; i++) {
66  // Setup messages
68  buffer.setData(std::vector<uint8_t>(MOBILENET_DATA_SIZE + i * 1024 * 10));
69 
70  dai::NNData nndata1, nndata2;
71  // Specify tensor by name
72  nndata1.setLayer(MOBILENET_INPUT_TENSOR, std::vector<uint8_t>(MOBILENET_DATA_SIZE + i * 1024 * 10));
73  // Specify tensor by index
74  nndata2.setLayer("", std::vector<uint8_t>(MOBILENET_DATA_SIZE + i * 1024 * 10));
75 
76  dai::ImgFrame frame;
80  frame.setData(std::vector<uint8_t>(MOBILENET_DATA_SIZE + i * 1024 * 10));
81 
82  int msgIndex = 0;
83  for(const dai::ADatatype& msg : std::initializer_list<std::reference_wrapper<dai::ADatatype>>{buffer, nndata1, nndata2, frame}) {
84  std::cout << msgIndex << "\n";
85  device.getInputQueue("input")->send(msg);
86  bool timedOut = false;
87  auto inference = device.getOutputQueue("output")->get<dai::NNData>(1s, timedOut);
88  REQUIRE(inference != nullptr);
89  REQUIRE(timedOut == false);
90  REQUIRE(inference->hasLayer(MOBILENET_OUTPUT_TENSOR));
91  msgIndex++;
92  }
93  }
94 
95  // At the end test if any error messages appeared
96  REQUIRE(receivedLogMessage == false);
97 }
98 
99 TEST_CASE("Neural network node data checks - setBlobPath") {
100  test(false);
101 }
102 TEST_CASE("Neural network node data checks - setBlob") {
103  test(true);
104 }
dai::DeviceBase::startPipeline
bool startPipeline()
Definition: DeviceBase.cpp:1511
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
createNeuralNetworkPipeline
dai::Pipeline createNeuralNetworkPipeline(bool manualBlob)
Definition: neural_network_test.cpp:18
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
dai::NNData
Definition: NNData.hpp:16
dai::OpenVINO::Blob
OpenVINO Blob.
Definition: OpenVINO.hpp:23
test
void test(bool manualBlob)
Definition: neural_network_test.cpp:43
MOBILENET_INPUT_TENSOR
const auto MOBILENET_INPUT_TENSOR
Definition: neural_network_test.cpp:15
dai::NNData::setLayer
NNData & setLayer(const std::string &name, std::vector< std::uint8_t > data)
Definition: NNData.cpp:110
dai::ADatatype
Abstract message.
Definition: ADatatype.hpp:11
MOBILENET_CHANNEL
const auto MOBILENET_CHANNEL
Definition: neural_network_test.cpp:13
dai::ImgFrame::setWidth
ImgFrame & setWidth(unsigned int width)
Definition: pipeline/datatype/ImgFrame.cpp:99
dai::node::NeuralNetwork
NeuralNetwork node. Runs a neural inference on input data.
Definition: NeuralNetwork.hpp:18
TEST_CASE
TEST_CASE("Neural network node data checks - setBlobPath")
Definition: neural_network_test.cpp:99
dai::DeviceBase::setLogLevel
void setLogLevel(LogLevel level)
Definition: DeviceBase.cpp:1196
MOBILENET_HEIGHT
const auto MOBILENET_HEIGHT
Definition: neural_network_test.cpp:12
nanorpc::core::type::buffer
std::vector< std::uint8_t > buffer
Definition: type.h:28
dai::RawImgFrame::Type::BGR888p
@ BGR888p
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::XLinkIn::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkIn.cpp:12
MOBILENET_DATA_SIZE
const size_t MOBILENET_DATA_SIZE
Definition: neural_network_test.cpp:14
dai::LogMessage
Definition: LogMessage.hpp:13
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::LogLevel::WARN
@ WARN
dai::Buffer
Base message - buffer of binary data.
Definition: Buffer.hpp:13
dai::Device
Definition: Device.hpp:21
dai::ImgFrame::setType
ImgFrame & setType(Type type)
Definition: pipeline/datatype/ImgFrame.cpp:118
dai::ImgFrame::setHeight
ImgFrame & setHeight(unsigned int height)
Definition: pipeline/datatype/ImgFrame.cpp:105
MOBILENET_BLOB_PATH
const auto MOBILENET_BLOB_PATH
Definition: neural_network_test.cpp:10
dai::LogMessage::level
LogLevel level
Definition: LogMessage.hpp:15
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
dai::Buffer::setData
void setData(const std::vector< std::uint8_t > &data)
Definition: Buffer.cpp:17
dai::DeviceBase::addLogCallback
int addLogCallback(std::function< void(LogMessage)> callback)
Definition: DeviceBase.cpp:1272
MOBILENET_WIDTH
const auto MOBILENET_WIDTH
Definition: neural_network_test.cpp:11
MOBILENET_OUTPUT_TENSOR
const auto MOBILENET_OUTPUT_TENSOR
Definition: neural_network_test.cpp:16


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19