unitorch.models.grounding_dino¤
GroundingDinoProcessor¤
Bases: HfTextClassificationProcessor, HfImageClassificationProcessor
Initializes the GroundingDinoProcessor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab_path
|
str
|
Path to the BERT vocabulary file. |
required |
vision_config_path
|
str
|
Path to the GroundingDINO image processor configuration file. |
required |
max_seq_length
|
int
|
Maximum sequence length. Defaults to 128. |
128
|
position_start_id
|
int
|
Starting position ID. Defaults to 0. |
0
|
Source code in src/unitorch/models/grounding_dino/processing.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
bert_tokenizer
instance-attribute
¤
bert_tokenizer = BertTokenizer(
vocab_path, do_basic_tokenize=True, do_lower_case=True
)
detection ¤
detection(
text: str,
image: Union[str, Image],
bboxes: List[List[float]],
classes: List[str],
)
Processes image and text for training with ground-truth detections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text describing objects. |
required |
image
|
str or Image
|
Input image or path. |
required |
bboxes
|
List[List[float]]
|
Ground-truth bounding boxes in [x1, y1, x2, y2] format. |
required |
classes
|
List[str]
|
Class name for each bounding box. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GenericOutputs |
Processed inputs including pixel values, text tokens, boxes, and class IDs. |
Source code in src/unitorch/models/grounding_dino/processing.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
detection_inputs ¤
detection_inputs(text: str, image: Union[str, Image])
Processes image and text for inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text describing objects. |
required |
image
|
str or Image
|
Input image or path. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GenericOutputs |
Processed pixel values and text tokens. |
Source code in src/unitorch/models/grounding_dino/processing.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
GroundingDinoForDetection¤
Bases: GenericModel
Initializes the GroundingDinoForDetection model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to the GroundingDINO configuration file. |
required |
Source code in src/unitorch/models/grounding_dino/modeling.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
bbox_embed
instance-attribute
¤
bbox_embed = ModuleList(
[_bbox_embed for _ in (range(decoder_layers))]
)
class_embed
instance-attribute
¤
class_embed = ModuleList(
[_class_embed for _ in (range(decoder_layers))]
)
matcher
instance-attribute
¤
matcher = DeformableDetrHungarianMatcher(
class_cost=class_cost,
bbox_cost=bbox_cost,
giou_cost=giou_cost,
)
criterion
instance-attribute
¤
criterion = DeformableDetrImageLoss(
matcher=matcher,
num_classes=num_labels,
focal_alpha=focal_alpha,
losses=losses,
)
_set_aux_loss ¤
_set_aux_loss(
outputs_class: Tensor, outputs_coord: Tensor
) -> List[Dict]
Source code in src/unitorch/models/grounding_dino/modeling.py
116 117 118 119 120 121 122 123 124 | |
_decode_outputs ¤
_decode_outputs(
hidden_states: Tensor,
enc_text_hidden_state: Tensor,
init_reference_points: Tensor,
inter_references_points: Tensor,
attention_mask: Tensor,
)
Decodes model outputs into class and coordinate predictions.
Source code in src/unitorch/models/grounding_dino/modeling.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
forward ¤
forward(
pixel_values: Tensor,
input_ids: Tensor,
token_type_ids: Tensor,
attention_mask: Tensor,
bboxes: Union[List[Tensor], Tensor],
classes: Union[List[Tensor], Tensor],
)
Forward pass computing detection loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Tensor
|
Input image tensor. |
required |
input_ids
|
Tensor
|
Text token IDs. |
required |
token_type_ids
|
Tensor
|
Token type IDs. |
required |
attention_mask
|
Tensor
|
Attention mask. |
required |
bboxes
|
List[Tensor] or Tensor
|
Ground-truth boxes in xyxy format. |
required |
classes
|
List[Tensor] or Tensor
|
Ground-truth class IDs. |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Total detection loss. |
Source code in src/unitorch/models/grounding_dino/modeling.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
detect ¤
detect(
pixel_values: Tensor,
input_ids: Tensor,
token_type_ids: Tensor,
attention_mask: Tensor,
norm_bboxes: Optional[bool] = False,
text_threshold: Optional[float] = 0.25,
box_threshold: Optional[float] = 0.25,
)
Runs detection inference and returns filtered predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pixel_values
|
Tensor
|
Input image tensor. |
required |
input_ids
|
Tensor
|
Text token IDs. |
required |
token_type_ids
|
Tensor
|
Token type IDs. |
required |
attention_mask
|
Tensor
|
Attention mask. |
required |
norm_bboxes
|
bool
|
Whether to return normalized boxes. Defaults to False. |
False
|
text_threshold
|
float
|
Threshold for text token scores. Defaults to 0.25. |
0.25
|
box_threshold
|
float
|
Threshold for box confidence scores. Defaults to 0.25. |
0.25
|
Returns:
| Name | Type | Description |
|---|---|---|
GenericOutputs |
Detected bounding boxes, scores, and class IDs. |
Source code in src/unitorch/models/grounding_dino/modeling.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |