# NeuralNetwork

该节点在输入数据上运行神经网络推理。只要 VPU [支持所有层](https://docs.luxonis.com/software/ai-inference/conversion.md#supported-layers)，任何 OpenVINO
神经网络都可以通过该节点运行。 这使您可以从 [Open Model Zoo](https://github.com/openvinotoolkit/open_model_zoo) 和 [DepthAI Model
Zoo](https://github.com/luxonis/depthai-model-zoo) 中选择 200多种预训练模型， 并直接在 OAK 设备上运行。

神经网络必须为 .blob 格式才能与 VPU 兼容。关于如何将神经网络（NN）编译为 .blob 的说明，请参见[此处](https://docs.luxonis.com/software/ai-inference/conversion.md)。

## 如何放置

#### Python

```python
pipeline = dai.Pipeline()
nn = pipeline.create(dai.node.NeuralNetwork)
```

#### C++

```cpp
dai::Pipeline pipeline;
auto nn = pipeline.create<dai::node::NeuralNetwork>();
```

## 输入和输出

## Passthrough 机制

Passthrough 机制非常有用，当节点的输入被指定为非阻塞时，消息可能会被覆盖。 此时我们无法知道节点对哪条消息执行了操作（例如 NN，是对第 25 帧进行推理，还是跳过了第 25 帧而对第 26 帧进行了推理）。 同时，这意味着：如果 xlink
和主机输入队列是阻塞的，并且我们同时收到 passthrough 和输出，我们可以对这两个队列进行阻塞获取，从而确保总是得到匹配的帧。它们可能不会同时到达， 但两者都会到达，并且会在队列中的正确位置，以便一起取出。

## 使用

#### Python

```python
pipeline = dai.Pipeline()
nn = pipeline.create(dai.node.NeuralNetwork)
nn.setBlobPath(bbBlobPath)
cam.out.link(nn.input)

# Send NN out to the host via XLink
nnXout = pipeline.create(dai.node.XLinkOut)
nnXout.setStreamName("nn")
nn.out.link(nnXout.input)

with dai.Device(pipeline) as device:
  qNn = device.getOutputQueue("nn")

  nnData = qNn.get() # Blocking

  # NN can output from multiple layers. Print all layer names:
  print(nnData.getAllLayerNames())

  # Get layer named "Layer1_FP16" as FP16
  layer1Data = nnData.getLayerFp16("Layer1_FP16")

  # You can now decode the output of your NN
```

#### C++

```cpp
dai::Pipeline pipeline;
auto nn = pipeline.create<dai::node::NeuralNetwork>();
nn->setBlobPath(bbBlobPath);
cam->out.link(nn->input);

// Send NN out to the host via XLink
auto nnXout = pipeline.create<dai::node::XLinkOut>();
nnXout->setStreamName("nn");
nn->out.link(nnXout->input);

dai::Device device(pipeline);
// Start the pipeline
device.startPipeline();

auto qNn = device.getOutputQueue("nn");

auto nnData = qNn->get<dai::NNData>(); // Blocking

// NN can output from multiple layers. Print all layer names:
cout << nnData->getAllLayerNames();

// Get layer named "Layer1_FP16" as FP16
auto layer1Data = nnData->getLayerFp16("Layer1_FP16");

// You can now decode the output of your NN
```

## 功能示例

 * [多输入帧拼接](https://docs.luxonis.com/software/depthai/examples/concat_multi_input.md)
 * [帧归一化](https://docs.luxonis.com/software/depthai/examples/normalization_multi_input.md)
 * [DeeplabV3 实验](https://github.com/luxonis/oak-examples/tree/master/gen2-deeplabv3_depth)
 * [年龄/性别实验](https://github.com/luxonis/oak-examples/blob/master/gen2-age-gender/main.py)
 * [EfficientDet 演示](https://github.com/luxonis/oak-examples/blob/master/gen2-efficientDet/main.py)

## 参考

### depthai.node.NeuralNetwork(depthai.Node)

Kind: Class

NeuralNetwork node. Runs a neural inference on input data.

#### getNumInferenceThreads(self) -> int: int

Kind: Method

How many inference threads will be used to run the network

Returns:
Number of threads, 0, 1 or 2. Zero means AUTO

#### setBlob()

Kind: Method

#### setBlobPath(self, path: Path)

Kind: Method

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

Throws:
Error if file doesn't exist or isn't a valid network blob.

Parameter ``path``:
Path to network blob

#### setNumInferenceThreads(self, numThreads: typing.SupportsInt)

Kind: Method

How many threads should the node use to run the network.

Parameter ``numThreads``:
Number of threads to dedicate to this node

#### setNumNCEPerInferenceThread(self, numNCEPerThread: typing.SupportsInt)

Kind: Method

How many Neural Compute Engines should a single thread use for inference

Parameter ``numNCEPerThread``:
Number of NCE per thread

#### setNumPoolFrames(self, numFrames: typing.SupportsInt)

Kind: Method

Specifies how many frames will be available in the pool

Parameter ``numFrames``:
How many frames will pool have

#### input

Kind: Property

Input message with data to be inferred upon Default queue is blocking with size
5

#### inputs

Kind: Property

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

#### out

Kind: Property

Outputs NNData message that carries inference results

#### passthrough

Kind: Property

Passthrough message on which the inference was performed.

Suitable for when input queue is set to non-blocking behavior.

#### passthroughs

Kind: Property

Passthroughs which correspond to specified input

### 需要帮助？

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