此页面由 AI 自动翻译。查看英文原版
最后更新

本页目录

  • 在 Python 中构建 NeuralNetwork 节点
  • 手动实例化
  • 输入和输出
  • 示例和实验
  • 参考
Claude CodeCodexCursor
Luxonis 智能体工具包为你常用的编程智能体加载 Luxonis 上下文与技能,更快地基于 OAK 开发应用。
安装工具包

NeuralNetwork

最后更新
此节点对输入数据运行神经网络推理。只要 VPU 支持所有必需的层,就可以执行任何模型。您可以使用 .blob、superblob 或 NNArchive 格式,并针对各种平台(RVC2、RVC3、RVC4)。模型可以来自:仅限 RVC2使用模型转换指南将您的网络编译为正确的格式(.blob、.superblob 或 NNArchive)。

在 Python 中构建 NeuralNetwork 节点

使用 build() 类方法之一,通过一次调用即可构建并链接节点:
Python
1import depthai as dai
2
3# 1. 从张量输入 + NNArchive
4tensor_input = ...  # 例如,来自另一个节点的输出
5nn_archive = dai.NNArchive('path/to/archive.tar.gz')
6nn = dai.node.NeuralNetwork.build(tensor_input, nn_archive)
7
8# 2. 从 Camera 节点 + NNModelDescription(+ 可选 fps)
9cam = pipeline.create(dai.node.ColorCamera)
10model_desc = dai.NNModelDescription(
11    model='yolov6-nano',
12    platform=''  # 空表示自动检测
13)
14nn = dai.node.NeuralNetwork.build(cam, model_desc, fps=30.0)
15
16# 3. 从 ReplayVideo 节点 + NNArchive(+ 可选 fps)
17replay = pipeline.create(dai.node.ReplayVideo)
18replay.setSourcePath('video.mp4')
19nn = dai.node.NeuralNetwork.build(replay, nn_archive, fps=15.0)
这些方法将:
  1. 下载或接受本地模型存档
  2. 验证存档是否为 NNArchive 格式
  3. 配置输入帧功能(分辨率、类型、fps)
  4. 将相机或张量输出直接链接到 nn.input

手动实例化

如果您喜欢手动设置,请使用:
Python
1pipeline = dai.Pipeline()
2
3# 创建节点
4nn = pipeline.create(dai.node.NeuralNetwork)
5
6# 加载 NNArchive
7nn_archive = dai.NNArchive('path/to/archive.tar.gz')
8
9# 将 NNArchive 设置到 NN 节点
10nn.setNNArchive('path/to/archive.tar.gz')

输入和输出

输入类型描述
input任何张量/ImgFrame用于推理的张量或 ImgFrame
passthroughImgFrame原始帧
输出类型描述
outNNData推理结果(层 blob、输出)

示例和实验

  • 神经网络 - 创建一个包含相机和神经网络节点的简单管道。
  • 神经网络多输入 - 运行一个神经网络模型,该模型使用两个输入张量将相机帧与静态图像连接起来。
  • 神经网络多输入组合 - 运行一个神经网络模型,该模型将两个输入图像组合成一个输出图像。

参考

class

dai::node::NeuralNetwork

#include NeuralNetwork.hpp
using
Model = std::variant< NNModelDescription, NNArchive, std::string >
variable
Input input
Input message with data to be inferred upon
variable
Output out
Outputs NNData message that carries inference results
variable
Output passthrough
Passthrough message on which the inference was performed.Suitable for when input queue is set to non-blocking behavior.
variable
InputMap inputs
Inputs mapped to network inputs. Useful for inferring from separate data sources Default input is non-blocking with queue size 1 and waits for messages
variable
OutputMap passthroughs
Passthroughs which correspond to specified input
function
~NeuralNetwork()
function
std::shared_ptr< NeuralNetwork > build(Node::Output & input, const NNArchive & nnArchive)
Build NeuralNetwork node. Connect output to this node's input and sets up the NNArchive.
Parameters
  • output: Output to link
  • nnArchive: Neural network archive
Returns
Shared pointer to NeuralNetwork node
function
std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< Camera > & input, const Model & model, std::optional< float > fps, std::optional< dai::ImgResizeMode > resizeMode)
Build NeuralNetwork node. Connect Camera output to this node's input and configure the inference model.
Parameters
  • input: Camera node
  • model: Neural network model description, NNArchive or HubAI model id string
  • fps: Desired frames per second
  • resizeMode: Resize mode for input frames
Returns
Shared pointer to NeuralNetwork node
function
std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< Camera > & input, const Model & model, const ImgFrameCapability & capability)
Build NeuralNetwork node. Connect Camera output to this node's input and configure the inference model.
Parameters
  • input: Camera node
  • model: Neural network model description, NNArchive or HubAI model id string
  • capability: Camera capabilities
Returns
Shared pointer to NeuralNetwork node
function
std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< ReplayVideo > & input, const Model & model, std::optional< float > fps)
Build NeuralNetwork node. Connect ReplayVideo output to this node's input and configure the inference model.
Parameters
  • input: ReplayVideo node
  • model: Neural network model description, NNArchive or HubAI model id string
  • fps: Desired frames per second
Returns
Shared pointer to NeuralNetwork node
function
std::optional< std::reference_wrapper< const NNArchive > > getNNArchive()
Get the archive owned by this Node.
Returns
constant reference to this Nodes archive
function
void setNNArchive(const NNArchive & nnArchive)
Set NNArchive for this Node. If the archive's type is SUPERBLOB, use default number of shaves.
Parameters
function
void setNNArchive(const NNArchive & nnArchive, int numShaves)
Set NNArchive for this Node, throws if the archive's type is not SUPERBLOB.
Parameters
  • nnArchive: NNArchive to set
  • numShaves: Number of shaves to use
function
void setFromModelZoo(NNModelDescription description, bool useCached)
Download model from zoo and set it for this Node.
Parameters
  • description: Model description to download
  • useCached: Use cached model if available
function
void setBlobPath(const std::filesystem::path & path)
Load network blob into assets and use once pipeline is started.
Parameters
  • Error: if file doesn't exist or isn't a valid network blob.
Parameters
  • path: Path to network blob
function
void setBlob(OpenVINO::Blob blob)
Load network blob into assets and use once pipeline is started.
Parameters
  • blob: Network blob
function
void setBlob(const std::filesystem::path & path)
Same functionality as the setBlobPath(). Load network blob into assets and use once pipeline is started.
Parameters
  • Error: if file doesn't exist or isn't a valid network blob.
Parameters
  • path: Path to network blob
function
void setOtherModelFormat(std::vector< uint8_t > model)
Load network model into assets and use once pipeline is started.
Parameters
  • model: Network model
function
void setOtherModelFormat(const std::filesystem::path & path)
Register a network model for lazy asset loading and use it once the pipeline is started. The file must remain available and unchanged until the pipeline has been built.
Parameters
  • Error: if file doesn't exist or isn't a valid network model.
Parameters
  • path: Path to the network model
function
void setModelPath(const std::filesystem::path & modelPath)
Load a network model into assets. DLC and other custom model files are loaded lazily and must remain available and unchanged until the pipeline has been built.
Parameters
  • modelPath: Path to the neural network model file.
function
void setNumPoolFrames(int numFrames)
Specifies how many frames will be available in the pool
Parameters
  • numFrames: How many frames will pool have
function
void setNumInferenceThreads(int numThreads)
How many threads should the node use to run the network.
Parameters
  • numThreads: Number of threads to dedicate to this node
function
void setNumNCEPerInferenceThread(int numNCEPerThread)
How many Neural Compute Engines should a single thread use for inference
Parameters
  • numNCEPerThread: Number of NCE per thread
function
void setNumShavesPerInferenceThread(int numShavesPerThread)
How many Shaves should a single thread use for inference
Parameters
  • numShavesPerThread: Number of shaves per thread
function
void setBackend(const std::string & backend)
Specifies backend to use
Parameters
  • backend: String specifying backend to use
function
void setBackendProperties(const std::map< std::string, std::string > & properties)
Set backend properties
Parameters
  • backendProperties: backend properties map
function
int getNumInferenceThreads()
How many inference threads will be used to run the network
Returns
Number of threads, 0, 1 or 2. Zero means AUTO
function
void setModelFromDeviceZoo(DeviceModelZoo model)
Set model from Device Model Zoo
Parameters
  • model: DeviceModelZoo model enum
Note
Only applicable for RVC4 devices with OS 1.20.5 or higher
inline function
DeviceNodeCRTP()
inline function
DeviceNodeCRTP(const std::shared_ptr< Device > & device)
inline function
DeviceNodeCRTP(std::unique_ptr< Properties > props)
inline function
DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)
inline function
DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

需要帮助?

请访问讨论论坛以获取技术支持或您可能有的任何其他问题。