# NeuralNetwork

该节点对输入数据执行神经网络推理。只要 VPU 支持所有所需层，即可运行任意模型。您可使用 .blob、superblob 或 NNArchive 格式，并面向多种平台（RVC2、RVC3、RVC4）。模型可来源于：

 * [HubAI 模型库](https://models.luxonis.com/)

仅限 RVC2

 * [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo)（200 多个预训练模型）
 * [DepthAI 模型库](https://github.com/luxonis/depthai-model-zoo)

请参考[模型转换指南](https://docs.luxonis.com/software-v3/ai-inference/conversion.md)，将您的网络编译为正确的格式（.blob、.superblob 或 NNArchive）。

## 在 Python 中构建 NeuralNetwork 节点

使用 build() 类方法之一，在一次调用中构建并链接节点：

```python
import depthai as dai

# 1. 从张量输入 + NNArchive
tensor_input = ...  # 例如其他节点的输出
nn_archive = dai.NNArchive('path/to/archive.tar.gz')
nn = dai.node.NeuralNetwork.build(tensor_input, nn_archive)

# 2. 从 Camera 节点 + NNModelDescription（+ 可选 fps）
cam = pipeline.create(dai.node.ColorCamera)
model_desc = dai.NNModelDescription(
    model='yolov6-nano',
    platform=''  # 留空以自动检测
)
nn = dai.node.NeuralNetwork.build(cam, model_desc, fps=30.0)

# 3. 从 ReplayVideo 节点 + NNArchive（+ 可选 fps）
replay = pipeline.create(dai.node.ReplayVideo)
replay.setSourcePath('video.mp4')
nn = dai.node.NeuralNetwork.build(replay, nn_archive, fps=15.0)
```

这些方法将：

 1. 下载或接受本地模型归档
 2. 验证归档是否为 NNArchive 格式
 3. 配置输入帧能力（分辨率、类型、帧率）
 4. 直接将相机或张量输出链接到 nn.input

## 手动实例化

如果您倾向于手动设置，请使用：

```python
pipeline = dai.Pipeline()

# 创建节点
nn = pipeline.create(dai.node.NeuralNetwork)

# 加载 NNArchive
nn_archive = dai.NNArchive('path/to/archive.tar.gz')

# 将 NNArchive 设置到 NN 节点
nn.setNNArchive('path/to/archive.tar.gz')
```

## 输入与输出

| 输入 | 类型 | 描述 |
| --- | --- | --- |
| `input` | 任意张量/ImgFrame | 用于推理的张量或 ImgFrame |
| `passthrough` | ImgFrame | 原始帧 |

| 输出 | 类型 | 描述 |
| --- | --- | --- |
| `out` | NNData | 推理结果（层 blob、输出） |

## 示例与实验

 * [神经网络](https://docs.luxonis.com/software-v3/depthai/examples/neural_network/neural_network.md) - 创建一个包含相机和神经网络节点的简单管线。
 * [神经网络多输入](https://docs.luxonis.com/software-v3/depthai/examples/neural_network/neural_network_multi_input.md) -
   运行一个将相机帧与静态图像拼接的神经网络模型，使用两个输入张量。
 * [神经网络多输入组合](https://docs.luxonis.com/software-v3/depthai/examples/neural_network/neural_network_multi_input_combined.md) -
   运行一个将两个输入图像合并为一个输出图像的神经网络模型。

## 参考

### dai::node::NeuralNetwork

Kind: class

NeuralNetwork node. Runs a neural inference on input data.

#### std::variant< NNModelDescription , NNArchive , std::string > Model

Kind: enum

#### Input input

Kind: variable

Input message with data to be inferred upon

#### Output out

Kind: variable

Outputs NNData message that carries inference results

#### Output passthrough

Kind: variable

Passthrough message on which the inference was performed. Suitable for when input queue is set to non-blocking behavior.

#### InputMap inputs

Kind: variable

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

#### OutputMap passthroughs

Kind: variable

Passthroughs which correspond to specified input

#### ~NeuralNetwork()

Kind: function

#### std::shared_ptr< NeuralNetwork > build(Node::Output & input, const NNArchive & nnArchive)

Kind: function

Build NeuralNetwork node. Connect output to this node's input and sets up the NNArchive .

parameters: output: Output to link; nnArchive: Neural network archive return: Shared pointer to NeuralNetwork node

#### std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< Camera > & input, const Model & model, std::optional< float >
fps, std::optional< dai::ImgResizeMode > resizeMode)

Kind: function

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 return: Shared pointer to NeuralNetwork node

#### std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< Camera > & input, const Model & model, const ImgFrameCapability
& capability)

Kind: function

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 return: Shared pointer to NeuralNetwork node

#### std::shared_ptr< NeuralNetwork > build(const std::shared_ptr< ReplayVideo > & input, const Model & model, std::optional<
float > fps)

Kind: function

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 return: Shared pointer to NeuralNetwork node

#### std::optional< std::reference_wrapper< const NNArchive > > getNNArchive()

Kind: function

Get the archive owned by this Node .

return: constant reference to this Nodes archive

#### void setNNArchive(const NNArchive & nnArchive)

Kind: function

Set NNArchive for this Node . If the archive's type is SUPERBLOB, use default number of shaves.

parameters: nnArchive: NNArchive to set

#### void setNNArchive(const NNArchive & nnArchive, int numShaves)

Kind: function

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

#### void setFromModelZoo(NNModelDescription description, bool useCached)

Kind: function

Download model from zoo and set it for this Node .

parameters: description: Model description to download; useCached: Use cached model if available

#### void setBlobPath(const std::filesystem::path & path)

Kind: function

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

#### void setBlob(OpenVINO::Blob blob)

Kind: function

Load network blob into assets and use once pipeline is started. parameters: blob: Network blob

#### void setBlob(const std::filesystem::path & path)

Kind: function

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

#### void setOtherModelFormat(std::vector< uint8_t > model)

Kind: function

Load network model into assets and use once pipeline is started. parameters: model: Network model

#### void setOtherModelFormat(const std::filesystem::path & path)

Kind: function

Load network model into assets and use once pipeline is started. parameters: Error: if file doesn't exist or isn't a valid network
model. parameters: path: Path to the network model

#### void setModelPath(const std::filesystem::path & modelPath)

Kind: function

Load network xml and bin files into assets. parameters: xmlModelPath: Path to the neural network model file.

#### void setNumPoolFrames(int numFrames)

Kind: function

Specifies how many frames will be available in the pool parameters: numFrames: How many frames will pool have

#### void setNumInferenceThreads(int numThreads)

Kind: function

How many threads should the node use to run the network. parameters: numThreads: Number of threads to dedicate to this node

#### void setNumNCEPerInferenceThread(int numNCEPerThread)

Kind: function

How many Neural Compute Engines should a single thread use for inference parameters: numNCEPerThread: Number of NCE per thread

#### void setNumShavesPerInferenceThread(int numShavesPerThread)

Kind: function

How many Shaves should a single thread use for inference parameters: numShavesPerThread: Number of shaves per thread

#### void setBackend(const std::string & backend)

Kind: function

Specifies backend to use parameters: backend: String specifying backend to use

#### void setBackendProperties(const std::map< std::string, std::string > & properties)

Kind: function

Set backend properties parameters: backendProperties: backend properties map

#### int getNumInferenceThreads()

Kind: function

How many inference threads will be used to run the network return: Number of threads, 0, 1 or 2. Zero means AUTO

#### void setModelFromDeviceZoo(DeviceModelZoo model)

Kind: function

Set model from Device Model Zoo parameters: model: DeviceModelZoo model enum note: Only applicable for RVC4 devices with OS 1.20.5
or higher

#### DeviceNodeCRTP()

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

Kind: function

### 需要帮助？

请前往 [OAKChina 官网](https://www.oakchina.cn/) 获取技术支持或解答您的任何疑问。
