Implementation:Tencent Ncnn Blob
| Knowledge Sources | |
|---|---|
| Domains | Neural Network, Computation Graph |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Defines the Blob class, which represents a named data edge (tensor connection) between layers in the ncnn network computation graph.
Description
The Blob class (33 lines in blob.h) is a lightweight data structure that forms the edges of ncnn's computation graph, while Layer objects form the nodes. Each Blob represents a tensor that flows between layers during inference.
The class has four members:
- name (std::string, conditional on NCNN_STRING): The human-readable name of the blob, used for identifying inputs and outputs in the model param file.
- producer (int): The index of the layer that produces (outputs) this blob.
- consumer (int): The index of the layer that consumes (reads) this blob as input.
- shape (Mat): A shape hint Mat that stores the expected tensor dimensions, used for memory planning and validation.
The Net class maintains a vector of Blobs indexed by blob_index to track data flow through the network. During param file loading, blob indices are assigned to wire up the layer DAG (directed acyclic graph). At inference time, the Extractor uses blob names or indices to identify where to inject input data and where to extract output results.
The class is marked NCNN_EXPORT for shared library visibility and resides in the ncnn namespace.
Usage
Blobs are managed internally by the Net class during model loading and inference. Users interact with blobs indirectly through Extractor::input() and Extractor::extract() by specifying blob names or indices. Understanding the Blob class is helpful for debugging model loading issues and understanding ncnn's graph topology.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: src/blob.h
Signature
namespace ncnn {
class NCNN_EXPORT Blob
{
public:
// empty
Blob();
public:
#if NCNN_STRING
// blob name
std::string name;
#endif // NCNN_STRING
// layer index which produce this blob as output
int producer;
// layer index which need this blob as input
int consumer;
// shape hint
Mat shape;
};
} // namespace ncnn
Import
#include "blob.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (constructor) | N/A | N/A | Default constructor creates an empty Blob with uninitialized indices |
Outputs
| Name | Type | Description |
|---|---|---|
| name | std::string | Blob name (when NCNN_STRING is enabled) |
| producer | int | Index of the layer that outputs this blob |
| consumer | int | Index of the layer that reads this blob |
| shape | Mat | Shape hint for memory planning |
Usage Examples
Accessing Blob Information from a Loaded Net
#include "net.h"
ncnn::Net net;
net.load_param("model.param");
net.load_model("model.bin");
// Access blob information
const std::vector<ncnn::Blob>& blobs = net.blobs();
for (size_t i = 0; i < blobs.size(); i++)
{
#if NCNN_STRING
fprintf(stderr, "Blob %zu: name=%s producer=%d consumer=%d\n",
i, blobs[i].name.c_str(), blobs[i].producer, blobs[i].consumer);
#endif
}
Using Blob Names with Extractor
#include "net.h"
ncnn::Net net;
// ... load model ...
ncnn::Extractor ex = net.create_extractor();
// Blobs are referenced by name when feeding inputs and extracting outputs
ex.input("data", input_mat); // "data" is a blob name
ex.extract("output", output_mat); // "output" is a blob name