Operations API Reference¶
oriented_det.ops
¶
Ops module exposing oriented IoU and NMS helpers.
batch_rbox_iou(boxes_a, boxes_b, *, device=None, intersection_backend='auto', use_aabb_prefilter=True)
¶
Produce an IoU matrix for two RBox collections.
This is a CPU Python implementation. For GPU-accelerated batch IoU, use:
from oriented_det.ops.gpu_ops import oriented_box_iou_gpu
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes_a
|
Sequence[RBox | Sequence[float]]
|
First collection of RBoxes |
required |
boxes_b
|
Sequence[RBox | Sequence[float]]
|
Second collection of RBoxes |
required |
device
|
Optional['torch.device']
|
Unused, kept for API compatibility |
None
|
intersection_backend
|
str
|
Backend for intersection calculation ('auto', 'python', or 'shapely') |
'auto'
|
use_aabb_prefilter
|
bool
|
If True (default), use fast AABB pre-filtering to skip expensive polygon intersections for non-overlapping boxes. |
True
|
Returns:
| Type | Description |
|---|---|
list[list[float]]
|
IoU matrix as a list of lists [len(boxes_a), len(boxes_b)] |
Source code in oriented_det/ops/iou.py
generate_oriented_anchors_gpu(image_size, feature_map_sizes, anchor_scales, anchor_ratios, anchor_angles, stride_per_level, device=torch.device('cpu'), octave_base_scale=None, scales_per_octave=None)
¶
Generate oriented anchors using vectorized GPU operations.
Fully vectorized, no Python loops for anchor generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_size
|
Tuple[int, int]
|
(height, width) of input image |
required |
feature_map_sizes
|
List[Tuple[int, int]]
|
List of (height, width) for each FPN level |
required |
anchor_scales
|
List[float]
|
List of anchor scales (one per level or shared) |
required |
anchor_ratios
|
List[float]
|
List of aspect ratios |
required |
anchor_angles
|
List[float]
|
List of anchor angles in radians |
required |
stride_per_level
|
List[int]
|
List of strides for each FPN level |
required |
device
|
device
|
Device to create tensors on |
device('cpu')
|
Returns:
| Type | Description |
|---|---|
List[Tensor]
|
List of anchor tensors, each [HWA, 5] in format [cx, cy, w, h, angle] |
Source code in oriented_det/ops/gpu_ops.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
hbb_iou_gpu(boxes1_xyxy, boxes2_xyxy)
¶
Compute axis-aligned (horizontal) IoU matrix between two sets of xyxy boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes1_xyxy
|
Tensor
|
[N, 4] (x1, y1, x2, y2) |
required |
boxes2_xyxy
|
Tensor
|
[M, 4] (x1, y1, x2, y2) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
[N, M] IoU matrix |
Source code in oriented_det/ops/gpu_ops.py
kfiou_loss(pred_decode, targets_decode, *, pred=None, target=None, fun=None, beta=1.0 / 9.0, eps=1e-06, reduction='mean', weight=None)
¶
Reduced KFIoU loss (scalar unless reduction="none").
Source code in oriented_det/ops/kfiou.py
kfiou_loss_per_box(pred_decode, targets_decode, *, pred=None, target=None, fun=None, beta=1.0 / 9.0, eps=1e-06)
¶
KFIoU loss terms per box (MMRotate kfiou_loss without reduction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred_decode
|
Tensor
|
|
required |
targets_decode
|
Tensor
|
|
required |
pred
|
Optional[Tensor]
|
Optional |
None
|
target
|
Optional[Tensor]
|
Optional |
None
|
fun
|
Optional[str]
|
|
None
|
beta
|
float
|
Smooth L1 beta for the center term. |
1.0 / 9.0
|
eps
|
float
|
Numerical stabilizer. |
1e-06
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in oriented_det/ops/kfiou.py
kfiou_overlap_ratio(pred_decode, targets_decode, *, eps=1e-06)
¶
Per-row KFIoU overlap ratio Vb / (Vb_p + Vb_t - Vb + eps) in [0, 1] (typically).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred_decode
|
Tensor
|
|
required |
targets_decode
|
Tensor
|
|
required |
eps
|
float
|
Small constant for numerical stability in the denominator. |
1e-06
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor |
Tensor
|
downstream |
Tensor
|
stable under AMP ( |
Tensor
|
casts gradients back to the input dtype for backward. |
Source code in oriented_det/ops/kfiou.py
match_anchors_to_gt_gpu(anchors, gt_boxes, positive_iou_threshold=0.7, negative_iou_threshold=0.3, use_hbb_for_assignment=False, min_pos_iou=0.3, match_low_quality=True)
¶
Match anchors to GT using GPU-accelerated IoU.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchors
|
Tensor
|
[N, 5] anchors |
required |
gt_boxes
|
Tensor
|
[M, 5] ground truth boxes |
required |
positive_iou_threshold
|
float
|
IoU threshold for positive |
0.7
|
negative_iou_threshold
|
float
|
IoU threshold for negative |
0.3
|
use_hbb_for_assignment
|
bool
|
If True, use axis-aligned (HBB) overlap for assignment instead of oriented IoU. Each box is converted to its horizontal bounding box (min/max of corners) then standard IoU is computed. Can yield more positive anchors when GT is rotated. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
labels |
Tensor
|
[N] with -1=ignore, 0=background, 1=foreground |
matched_gt_indices |
Tensor
|
[N] index of matched GT (-1 if none) |
Source code in oriented_det/ops/gpu_ops.py
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 | |
mean_auxiliary_box_reg_loss(decoded_boxes, matched_gt, *, loss_type='riou', kfiou_fun=None, probiou_mode=None)
¶
Scalar auxiliary loss on decoded positives (mean over boxes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decoded_boxes
|
Tensor
|
|
required |
matched_gt
|
Tensor
|
|
required |
loss_type
|
str
|
|
'riou'
|
kfiou_fun
|
Optional[str]
|
When |
None
|
probiou_mode
|
Optional[str]
|
When |
None
|
Source code in oriented_det/ops/kfiou.py
obb_to_xyxy_gpu(boxes)
¶
Convert oriented boxes [N, 5] (cx, cy, w, h, angle) to axis-aligned xyxy [N, 4].
Uses the 4 corner vertices of each OBB and takes min/max to get the HBB. Fully vectorized and GPU-compatible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
Tensor
|
[N, 5] in format [cx, cy, w, h, angle] |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
[N, 4] in format [x1, y1, x2, y2] |
Source code in oriented_det/ops/gpu_ops.py
oriented_box_hbb_iou_gpu(anchors, gt_boxes)
¶
Compute HBB (axis-aligned) IoU matrix between oriented boxes.
Each OBB is converted to its axis-aligned bounding box (min/max of corners), then standard xyxy IoU is computed. Fully GPU-compatible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchors
|
Tensor
|
[N, 5] (cx, cy, w, h, angle) |
required |
gt_boxes
|
Tensor
|
[M, 5] (cx, cy, w, h, angle) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
[N, M] IoU matrix |
Source code in oriented_det/ops/gpu_ops.py
oriented_box_iou_gpu(boxes1, boxes2, num_samples=None)
¶
Compute oriented IoU matrix using sampling-based approximation.
This is a fully vectorized GPU implementation that approximates IoU by sampling points inside boxes and checking containment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes1
|
Tensor
|
[N, 5] boxes in format [cx, cy, w, h, angle] |
required |
boxes2
|
Tensor
|
[M, 5] boxes in format [cx, cy, w, h, angle] |
required |
num_samples
|
Optional[int]
|
Samples per box (perfect square, e.g. 100 = 10×10).
If |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
[N, M] IoU matrix |
Source code in oriented_det/ops/gpu_ops.py
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 553 554 555 556 557 558 559 560 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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | |
oriented_nms(boxes, scores, iou_threshold=0.5, max_detections=None, labels=None, backend=None)
¶
Perform oriented NMS and return kept indices.
This is a CPU Python implementation. For GPU-accelerated NMS, use:
from oriented_det.ops.gpu_ops import oriented_nms_gpu
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
Sequence[RBox | Sequence[float]]
|
Sequence of RBoxes or 5-tuples |
required |
scores
|
Sequence[float]
|
Confidence scores aligned with |
required |
iou_threshold
|
float
|
Overlap threshold to suppress boxes. |
0.5
|
max_detections
|
int | None
|
Optional cap on number of returns. |
None
|
labels
|
Optional[Sequence[int]]
|
Optional sequence of class labels aligned with |
None
|
backend
|
Optional[str]
|
Optional backend selector ('python' or 'torch'); for API compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
List[int]
|
List of indices of boxes to keep after NMS. |
Examples:
>>> boxes = [RBox(0, 0, 2, 2, 0), RBox(0.5, 0.5, 2, 2, 0)]
>>> scores = [0.9, 0.8]
>>> keep = oriented_nms(boxes, scores, iou_threshold=0.3)
>>> # Class-aware NMS
>>> labels = [1, 2] # Different classes
>>> keep = oriented_nms(boxes, scores, labels=labels, iou_threshold=0.3)
>>> # Both boxes kept since they're different classes
Source code in oriented_det/ops/nms.py
oriented_nms_gpu(boxes, scores, iou_threshold=0.5, max_detections=None)
¶
GPU-accelerated oriented NMS using sampling-based IoU.
Uses ORIENTED_DET_GPU_NMS_IOU_SAMPLES as an optional floor when geometry
sizing is enabled; otherwise a flat count (default 100). Geometry uses the
same rules as oriented IoU (see ops/README.md).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
Tensor
|
[N, 5] boxes |
required |
scores
|
Tensor
|
[N] confidence scores |
required |
iou_threshold
|
float
|
Suppression threshold |
0.5
|
max_detections
|
Optional[int]
|
Maximum detections to keep |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Indices of kept boxes |
Source code in oriented_det/ops/gpu_ops.py
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 | |
polygon_iou(poly_a, poly_b, *, intersection_backend='auto')
¶
Compute IoU between two polygon-like objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
poly_a
|
First polygon-like object |
required | |
poly_b
|
Second polygon-like object |
required | |
intersection_backend
|
str
|
Backend for intersection calculation ('auto', 'python', or 'shapely') |
'auto'
|
Returns:
| Type | Description |
|---|---|
float
|
IoU value as a float |
Source code in oriented_det/ops/iou.py
probiou_loss(pred, target, *, eps=0.001, mode='l1', reduction='mean', weight=None)
¶
Reduced ProbIoU loss (scalar unless reduction="none").
Source code in oriented_det/ops/probiou.py
probiou_loss_per_box(pred, target, *, eps=0.001, mode='l1')
¶
Per-instance ProbIoU loss [N] (lower is better overlap).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
Tensor
|
|
required |
target
|
Tensor
|
|
required |
eps
|
float
|
Stabilizer for divisions and logarithms. |
0.001
|
mode
|
ProbIoUMode
|
|
'l1'
|
Source code in oriented_det/ops/probiou.py
qbox_iou(box_a, box_b, *, intersection_backend='auto')
¶
Compute IoU between two quadrilateral boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
box_a
|
QBox | Iterable[Sequence[float]]
|
First quadrilateral box |
required |
box_b
|
QBox | Iterable[Sequence[float]]
|
Second quadrilateral box |
required |
intersection_backend
|
str
|
Backend for intersection calculation ('auto', 'python', or 'shapely') |
'auto'
|
Returns:
| Type | Description |
|---|---|
float
|
IoU value as a float |
Source code in oriented_det/ops/iou.py
rbox_iou(box_a, box_b, *, intersection_backend='auto', use_aabb_prefilter=True, backend=None)
¶
Compute IoU between two rotated boxes.
This is a CPU Python implementation. For GPU-accelerated IoU, use:
from oriented_det.ops.gpu_ops import oriented_box_iou_gpu
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
box_a
|
RBox | Sequence[float]
|
First rotated box |
required |
box_b
|
RBox | Sequence[float]
|
Second rotated box |
required |
intersection_backend
|
str
|
Backend for intersection calculation ('auto', 'python', or 'shapely') |
'auto'
|
use_aabb_prefilter
|
bool
|
If True (default), use fast AABB pre-filtering to skip expensive polygon intersection for non-overlapping boxes. |
True
|
backend
|
Optional[str]
|
Alias for intersection_backend (for API compatibility). |
None
|
Returns:
| Type | Description |
|---|---|
float
|
IoU value as a float |
Source code in oriented_det/ops/iou.py
xy_wh_r_to_xy_sigma(xywhr)
¶
Map oriented boxes to Gaussian mean xy and covariance sigma.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xywhr
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Source code in oriented_det/ops/kfiou.py
Important Notes¶
NMS Performance¶
Important: torchvision.ops.nms_rotated does not exist in current torchvision versions (tested with 0.22.0+).
Current Implementation: - The framework uses a Python-based NMS implementation with optimizations: - AABB (axis-aligned bounding box) pre-filtering before expensive rotated IoU computation - Batch IoU computation for better performance - Class-aware NMS support (boxes of different classes don't suppress each other) - Performance: ~2.6x faster than naive implementation (11.9s for 2000 boxes vs 31.5s) - Future: GPU-accelerated kernels via custom CUDA implementation planned
Optimization Details: - AABB pre-filtering eliminates ~80-90% of rotated IoU computations - Pre-computed AABBs avoid redundant calculations - Batch processing reduces Python overhead
Backend Selection¶
For CPU-based IoU, use iou.rbox_iou() and nms.oriented_nms() with intersection_backend ("auto", "python", or "shapely"). For GPU-accelerated operations, use oriented_det.ops.gpu_ops (e.g., oriented_box_iou_gpu, oriented_nms_gpu). Models automatically use GPU kernels when available.
Examples¶
Basic IoU Computation¶
from oriented_det.geometry import RBox
from oriented_det.ops import iou
boxes = [
RBox(0, 0, 2, 1, 0),
RBox(0.5, 0.1, 2, 1, 0),
RBox(4, 0, 2, 1, 0),
]
# Compute IoU between two boxes
overlap = iou.rbox_iou(boxes[0], boxes[1])
# Batch IoU computation (more efficient)
iou_matrix = iou.batch_rbox_iou(boxes, boxes)
Basic NMS¶
from oriented_det.geometry import RBox
from oriented_det.ops import nms
boxes = [
RBox(0, 0, 2, 1, 0),
RBox(0.5, 0.1, 2, 1, 0), # Overlaps with first
RBox(4, 0, 2, 1, 0), # No overlap
]
scores = [0.9, 0.8, 0.6]
# Apply NMS
keep = nms.oriented_nms(boxes, scores, iou_threshold=0.3)
Class-Aware NMS¶
# Boxes of different classes don't suppress each other
labels = [0, 0, 1] # First two boxes are class 0, third is class 1
keep = nms.oriented_nms(boxes, scores, iou_threshold=0.3, labels=labels)
GPU Operations¶
For maximum performance with large batches, use the GPU-accelerated operations:
GPU IoU Computation¶
import torch
from oriented_det.ops.gpu_ops import oriented_box_iou_gpu
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Input format: [N, 5] tensors with [cx, cy, w, h, angle]
boxes1 = torch.tensor([
[100, 100, 50, 30, 0.0],
[200, 150, 40, 60, 0.5],
], device=device)
boxes2 = torch.tensor([
[105, 105, 50, 30, 0.0],
[300, 300, 50, 50, 0.0],
], device=device)
# Compute IoU matrix: [N, M]
iou_matrix = oriented_box_iou_gpu(boxes1, boxes2, num_samples=100)
GPU Anchor Generation¶
import torch
from oriented_det.ops.gpu_ops import generate_oriented_anchors_gpu
import math
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Generate anchors for FPN levels
anchors_per_level = generate_oriented_anchors_gpu(
image_size=(800, 800),
feature_map_sizes=[(200, 200), (100, 100), (50, 50)],
anchor_scales=[8],
anchor_ratios=[0.5, 1.0, 2.0],
anchor_angles=[-math.pi/2, 0, math.pi/2],
stride_per_level=[4, 8, 16],
device=device
)
# Returns list of [H*W*A, 5] anchor tensors
GPU Anchor Matching¶
import torch
from oriented_det.ops.gpu_ops import match_anchors_to_gt_gpu
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
anchors = torch.randn(1000, 5, device=device) # [N, 5]
gt_boxes = torch.randn(10, 5, device=device) # [M, 5]
labels, matched_gt_indices = match_anchors_to_gt_gpu(
anchors=anchors,
gt_boxes=gt_boxes,
positive_iou_threshold=0.7,
negative_iou_threshold=0.3,
)
# labels: [N] with -1=ignore, 0=background, 1=foreground
# matched_gt_indices: [N] index of matched GT (-1 if none)
GPU NMS¶
import torch
from oriented_det.ops.gpu_ops import oriented_nms_gpu
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
boxes = torch.randn(1000, 5, device=device) # [N, 5]
scores = torch.rand(1000, device=device) # [N]
keep_indices = oriented_nms_gpu(
boxes=boxes,
scores=scores,
iou_threshold=0.5,
max_detections=100,
)
# Returns tensor of kept box indices
See Also: - GPU Operations Guide - Detailed usage guide