NeuralNetwork
最后更新
- Open Model Zoo(200 多个预训练模型)
- DepthAI 模型库
.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)- 下载或接受本地模型存档
- 验证存档是否为 NNArchive 格式
- 配置输入帧功能(分辨率、类型、fps)
- 将相机或张量输出直接链接到
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 |
passthrough | ImgFrame | 原始帧 |
| 输出 | 类型 | 描述 |
|---|---|---|
out | NNData | 推理结果(层 blob、输出) |
示例和实验
- 神经网络 - 创建一个包含相机和神经网络节点的简单管道。
- 神经网络多输入 - 运行一个神经网络模型,该模型使用两个输入张量将相机帧与静态图像连接起来。
- 神经网络多输入组合 - 运行一个神经网络模型,该模型将两个输入图像组合成一个输出图像。
参考
class
dai::node::NeuralNetwork
using
Model = std::variant< NNModelDescription, NNArchive, std::string >
variable
Input input
Input message with data to be inferred upon
variable
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
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
Returns
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
Returns
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
function
std::optional< std::reference_wrapper< const NNArchive > > getNNArchive()function
void setNNArchive(const NNArchive & nnArchive)function
void setNNArchive(const NNArchive & nnArchive, int numShaves)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)需要帮助?
请访问讨论论坛以获取技术支持或您可能有的任何其他问题。