Postprocess
cosine_similarity(a, b)
¶
Compute the cosine similarity between two vectors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
ndarray
|
Vector A. |
required |
b
|
ndarray
|
Vector B. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
Union[number, ndarray]
|
Cosine similarity between vectors A and B. |
Source code in inference/core/utils/postprocess.py
14 15 16 17 18 19 20 21 22 23 24 25 |
|
crop_mask(masks, boxes)
¶
"Crop" predicted masks by zeroing out everything not in the predicted bbox. Vectorized by Chong (thanks Chong).
Source code in inference/core/utils/postprocess.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
|
get_static_crop_dimensions(orig_shape, preproc, disable_preproc_static_crop=False)
¶
Generates a transformation based on preprocessing configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
orig_shape
|
tuple
|
The original shape of the object (e.g., image) - (height, width). |
required |
preproc
|
dict
|
Preprocessing configuration dictionary, containing information such as static cropping. |
required |
disable_preproc_static_crop
|
bool
|
If true, the static crop preprocessing step is disabled for this call. Default is False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
Tuple[Tuple[int, int], Tuple[int, int]]
|
A tuple containing the shift in the x and y directions, and the updated original shape after cropping. |
Source code in inference/core/utils/postprocess.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|
mask2multipoly(mask)
¶
Find all contours in the mask and return them as a float32 array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
ndarray
|
A binary mask. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Contours represented as a float32 array. |
Source code in inference/core/utils/postprocess.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
mask2poly(mask)
¶
Find contours in the mask and return them as a float32 array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
ndarray
|
A binary mask. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Contours represented as a float32 array. |
Source code in inference/core/utils/postprocess.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
masks2multipoly(masks)
¶
Converts binary masks to polygonal segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
ndarray
|
A set of binary masks, where masks are multiplied by 255 and converted to uint8 type. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List[ndarray]
|
A list of segments, where each segment is obtained by converting the corresponding mask. |
Source code in inference/core/utils/postprocess.py
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 |
|
masks2poly(masks)
¶
Converts binary masks to polygonal segments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
ndarray
|
A set of binary masks, where masks are multiplied by 255 and converted to uint8 type. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List[ndarray]
|
A list of segments, where each segment is obtained by converting the corresponding mask. |
Source code in inference/core/utils/postprocess.py
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 53 54 55 56 57 58 59 60 61 |
|
post_process_bboxes(predictions, infer_shape, img_dims, preproc, disable_preproc_static_crop=False, resize_method='Stretch to')
¶
Postprocesses each patch of detections by scaling them to the original image coordinates and by shifting them based on a static crop preproc (if applied).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions
|
List[List[List[float]]]
|
The predictions output from NMS, indices are: batch x prediction x [x1, y1, x2, y2, ...]. |
required |
infer_shape
|
Tuple[int, int]
|
The shape of the inference image. |
required |
img_dims
|
List[Tuple[int, int]]
|
The dimensions of the original image for each batch, indices are: batch x [height, width]. |
required |
preproc
|
dict
|
Preprocessing configuration dictionary. |
required |
disable_preproc_static_crop
|
bool
|
If true, the static crop preprocessing step is disabled for this call. Default is False. |
False
|
resize_method
|
str
|
Resize method for image. Defaults to "Stretch to". |
'Stretch to'
|
Returns:
Type | Description |
---|---|
List[List[List[float]]]
|
List[List[List[float]]]: The scaled and shifted predictions, indices are: batch x prediction x [x1, y1, x2, y2, ...]. |
Source code in inference/core/utils/postprocess.py
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 169 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 |
|
post_process_keypoints(predictions, keypoints_start_index, infer_shape, img_dims, preproc, disable_preproc_static_crop=False, resize_method='Stretch to')
¶
Scales and shifts keypoints based on the given image shapes and preprocessing method.
This function performs polygon scaling and shifting based on the specified resizing method and pre-processing steps. The polygons are transformed according to the ratio and padding between two images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions
|
List[List[List[float]]]
|
predictions from model |
required |
keypoints_start_index
|
int
|
offset in the 3rd dimension pointing where in the prediction start keypoints [(x, y, cfg), ...] for each keypoint class |
required |
img_dims list of
|
tuple of int
|
Shape of the source image (height, width). |
required |
infer_shape
|
tuple of int
|
Shape of the target image (height, width). |
required |
preproc
|
object
|
Preprocessing details used for generating the transformation. |
required |
resize_method
|
str
|
Resizing method, either "Stretch to", "Fit (black edges) in", "Fit (white edges) in", or "Fit (grey edges) in". Defaults to "Stretch to". |
'Stretch to'
|
disable_preproc_static_crop
|
bool
|
flag to disable static crop |
False
|
Returns: list of list of list: predictions with post-processed keypoints
Source code in inference/core/utils/postprocess.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
|
post_process_polygons(origin_shape, polys, infer_shape, preproc, resize_method='Stretch to')
¶
Scales and shifts polygons based on the given image shapes and preprocessing method.
This function performs polygon scaling and shifting based on the specified resizing method and pre-processing steps. The polygons are transformed according to the ratio and padding between two images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
origin_shape
|
tuple of int
|
Shape of the source image (height, width). |
required |
infer_shape
|
tuple of int
|
Shape of the target image (height, width). |
required |
polys
|
list of list of tuple
|
List of polygons, where each polygon is represented by a list of (x, y) coordinates. |
required |
preproc
|
object
|
Preprocessing details used for generating the transformation. |
required |
resize_method
|
str
|
Resizing method, either "Stretch to", "Fit (black edges) in", "Fit (white edges) in", or "Fit (grey edges) in". Defaults to "Stretch to". |
'Stretch to'
|
Returns:
Type | Description |
---|---|
List[List[Tuple[float, float]]]
|
list of list of tuple: A list of shifted and scaled polygons. |
Source code in inference/core/utils/postprocess.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
|
process_mask_accurate(protos, masks_in, bboxes, shape)
¶
Returns masks that are the size of the original image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protos
|
ndarray
|
Prototype masks. |
required |
masks_in
|
ndarray
|
Input masks. |
required |
bboxes
|
ndarray
|
Bounding boxes. |
required |
shape
|
tuple
|
Target shape. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
numpy.ndarray: Processed masks. |
Source code in inference/core/utils/postprocess.py
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 |
|
process_mask_fast(protos, masks_in, bboxes, shape)
¶
Returns masks in their original size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protos
|
ndarray
|
Prototype masks. |
required |
masks_in
|
ndarray
|
Input masks. |
required |
bboxes
|
ndarray
|
Bounding boxes. |
required |
shape
|
tuple
|
Target shape. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
numpy.ndarray: Processed masks. |
Source code in inference/core/utils/postprocess.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
process_mask_tradeoff(protos, masks_in, bboxes, shape, tradeoff_factor)
¶
Returns masks that are the size of the original image with a tradeoff factor applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protos
|
ndarray
|
Prototype masks. |
required |
masks_in
|
ndarray
|
Input masks. |
required |
bboxes
|
ndarray
|
Bounding boxes. |
required |
shape
|
tuple
|
Target shape. |
required |
tradeoff_factor
|
float
|
Tradeoff factor for resizing masks. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
numpy.ndarray: Processed masks. |
Source code in inference/core/utils/postprocess.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
sigmoid(x)
¶
Computes the sigmoid function for the given input.
The sigmoid function is defined as: f(x) = 1 / (1 + exp(-x))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float or ndarray
|
Input value or array for which the sigmoid function is to be computed. |
required |
Returns:
Type | Description |
---|---|
Union[float, number, ndarray]
|
float or numpy.ndarray: The computed sigmoid value(s). |
Source code in inference/core/utils/postprocess.py
685 686 687 688 689 690 691 692 693 694 695 696 697 |
|