NVIDIA DeepStream SDK API Reference

9.2 Release
sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/postprocess/instance_bank.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef INSTANCE_BANK_H
19 #define INSTANCE_BANK_H
20 
21 #include <torch/torch.h>
22 
23 #include "../utils/log_utils.h"
24 #include "../utils/tensor.h"
25 
33 public:
34  virtual ~AnchorHandler() = default;
35 
44  virtual std::vector<torch::Tensor> anchor_projection(
45  const torch::Tensor& anchors,
46  const std::vector<torch::Tensor>& transforms,
47  const std::vector<torch::Tensor>& time_intervals) = 0;
48 };
49 
50 // Type definitions for metadata structures
51 // Use std::map for now for simplicity since it works
52 typedef std::map<std::string, torch::jit::IValue> MetaData;
53 typedef std::map<std::string, torch::Tensor> DNMetaData;
54 
61 class InstanceBank : public torch::nn::Module {
62 public:
80  int num_anchor,
81  int embed_dims,
82  const torch::Tensor& anchor,
83  AnchorHandler* anchor_handler = nullptr,
84  int num_temp_instances = 0,
85  float default_time_interval = 0.5,
86  float confidence_decay = 0.6,
87  bool anchor_grad = true,
88  bool feat_grad = true,
89  float max_time_interval = 2.0,
90  int reid_dims = -1,
91  int gpu_id = -1);
92 
96  void init_weight();
97 
101  void reset();
102 
111  std::tuple<torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor>
112  get(int batch_size, const MetaData& metas, const DNMetaData* dn_metas = nullptr);
113 
122  std::tuple<torch::Tensor, torch::Tensor>
123  update(torch::Tensor instance_feature, torch::Tensor anchor, torch::Tensor confidence);
124 
134  void cache(
135  torch::Tensor instance_feature,
136  torch::Tensor anchor,
137  torch::Tensor confidence,
138  const MetaData* metas = nullptr,
139  const std::vector<torch::Tensor>* feature_maps = nullptr);
140 
149  torch::Tensor get_instance_id(
150  torch::Tensor confidence,
151  torch::Tensor anchor = torch::Tensor(),
152  float threshold = -1.0);
153 
160  void update_instance_id(
161  torch::Tensor instance_id_tensor,
162  torch::Tensor confidence = torch::Tensor());
163 
169  torch::Tensor getMask() const {
170  return m_mask;
171  }
172 
173 private:
174  // Parameters
175  int m_embed_dims;
176  int m_reid_dims;
177  int m_num_anchor;
178  int m_num_temp_instances;
179  float m_default_time_interval;
180  float m_confidence_decay;
181  float m_max_time_interval;
182  AnchorHandler* m_anchor_handler;
183 
184  // Registered parameters
185  torch::Tensor m_anchor;
186  torch::Tensor m_anchor_init;
187  torch::Tensor m_instance_feature;
188  torch::Tensor m_reid_feature;
189 
190  // Internal state
191  torch::Tensor m_cached_feature;
192  torch::Tensor m_cached_anchor;
193  MetaData m_metas;
194  torch::Tensor m_mask;
195  torch::Tensor m_confidence;
196  torch::Tensor m_temp_confidence;
197  torch::Tensor m_instance_id;
198  int64_t m_prev_id;
199 
200  // Device to use for computations
201  torch::Device m_device;
202 };
203 
210 torch::Tensor load_anchor(const std::string& filename);
211 
212 #endif // INSTANCE_BANK_H
InstanceBank::update_instance_id
void update_instance_id(torch::Tensor instance_id_tensor, torch::Tensor confidence=torch::Tensor())
Update instance IDs based on confidence scores.
InstanceBank::get
std::tuple< torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor, torch::Tensor > get(int batch_size, const MetaData &metas, const DNMetaData *dn_metas=nullptr)
Get features, anchors and time intervals for the current batch.
InstanceBank::update
std::tuple< torch::Tensor, torch::Tensor > update(torch::Tensor instance_feature, torch::Tensor anchor, torch::Tensor confidence)
Update instance features and anchors.
InstanceBank
InstanceBank class for managing and tracking object instances across frames.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/postprocess/instance_bank.h:61
InstanceBank::getMask
torch::Tensor getMask() const
Get the mask tensor.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/postprocess/instance_bank.h:169
AnchorHandler
Interface class for anchor projection operations.
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/postprocess/instance_bank.h:32
DNMetaData
std::map< std::string, torch::Tensor > DNMetaData
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/postprocess/instance_bank.h:53
AnchorHandler::anchor_projection
virtual std::vector< torch::Tensor > anchor_projection(const torch::Tensor &anchors, const std::vector< torch::Tensor > &transforms, const std::vector< torch::Tensor > &time_intervals)=0
Project anchors using transformation matrices and time intervals.
load_anchor
torch::Tensor load_anchor(const std::string &filename)
Load anchor data from a NumPy .npy file.
InstanceBank::get_instance_id
torch::Tensor get_instance_id(torch::Tensor confidence, torch::Tensor anchor=torch::Tensor(), float threshold=-1.0)
Get instance IDs based on confidence scores.
InstanceBank::init_weight
void init_weight()
Initialize weights with proper initialization schemes.
InstanceBank::reset
void reset()
Reset internal state.
InstanceBank::InstanceBank
InstanceBank(int num_anchor, int embed_dims, const torch::Tensor &anchor, AnchorHandler *anchor_handler=nullptr, int num_temp_instances=0, float default_time_interval=0.5, float confidence_decay=0.6, bool anchor_grad=true, bool feat_grad=true, float max_time_interval=2.0, int reid_dims=-1, int gpu_id=-1)
Constructor for InstanceBank.
AnchorHandler::~AnchorHandler
virtual ~AnchorHandler()=default
InstanceBank::cache
void cache(torch::Tensor instance_feature, torch::Tensor anchor, torch::Tensor confidence, const MetaData *metas=nullptr, const std::vector< torch::Tensor > *feature_maps=nullptr)
Cache features, anchors and metadata for temporal association.
MetaData
std::map< std::string, torch::jit::IValue > MetaData
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/postprocess/instance_bank.h:52