Models API Reference¶
oriented_det.models
¶
Model registry for oriented detection baselines.
ClassWeightsMixin
¶
Mixin class for models that support class weights.
Provides common functionality for managing class weights in two-stage detectors (RotatedFasterRCNN, OrientedRCNN).
Source code in oriented_det/models/utils.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |
roi_class_weights
property
¶
Get the current class weights tensor.
Returns the class weights tensor if it has been set (via set_class_weights or
passed directly as a tensor during initialization). Returns None if no class
weights are configured.
Returns:
| Type | Description |
|---|---|
Optional[Tensor]
|
Class weights tensor of shape [num_classes + 1] (including background), |
Optional[Tensor]
|
or None if not configured. |
Example
model = OrientedRCNN(num_classes=15) weights = model.roi_class_weights # Returns None if not set model.set_class_weights(class_map) weights = model.roi_class_weights # Returns tensor after setting
__init__(num_classes, class_weights=None)
¶
Initialize class weights mixin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of object classes (excluding background) |
required |
class_weights
|
Optional[Union[Dict[str, float], Tensor]]
|
Optional class weights: - Dict[str, float]: Mapping from class name to weight - torch.Tensor: Tensor of shape [num_classes + 1] (including background) - None: Equal weights for all classes |
None
|
Source code in oriented_det/models/utils.py
set_class_weights(class_map, device=None)
¶
Set class weights tensor from class mapping.
Converts class weights dictionary (class_name -> weight) to tensor format using the provided class mapping. Must be called before training if using class weights with a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_map
|
Dict[str, int]
|
Mapping from class names to class IDs (1-indexed, 0 is background) |
required |
device
|
Optional[device]
|
Device to create tensor on (defaults to model's device) |
None
|
Example
class_map = {"plane": 1, "ship": 2, "small-vehicle": 3} class_weights = {"plane": 2.0, "ship": 1.5, "small-vehicle": 1.0} model = OrientedRCNN(num_classes=3, roi_class_weights=class_weights) model.set_class_weights(class_map)
Source code in oriented_det/models/utils.py
set_class_weights_tensor(weights)
¶
Set ROI class weights directly as a tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
'torch.Tensor'
|
Tensor of shape [num_classes + 1] (including background). |
required |
Source code in oriented_det/models/utils.py
DeltaXYWHAHBBoxCoder
¶
5 params: dx, dy, dw, dh, da. norm_factor, edge_swap. MMRotate ROI head.
Source code in oriented_det/models/bbox_coder.py
decode(anchors, deltas, normalize_le90=True)
¶
Decode deltas [N, 5] to boxes [N, 5].
Source code in oriented_det/models/bbox_coder.py
encode(anchors, gt_boxes)
¶
Encode gt boxes relative to anchors. anchors/gt: [N, 5] (cx, cy, w, h, angle).
Source code in oriented_det/models/bbox_coder.py
DeltaXYWHBBoxCoder
¶
4 params: dx, dy, dw, dh. Angle from anchor. MMRotate Rotated Faster R-CNN RPN.
Same as encode_rpn_boxes / decode_rpn_boxes.
Source code in oriented_det/models/bbox_coder.py
MidpointOffsetCoder
¶
6 params: dx, dy, dw, dh, da, db. MMRotate Oriented R-CNN RPN (MidpointOffsetCoder).
Encodes/decodes axis-aligned proposals (xyxy) relative to oriented GT using midpoint offsets. Proposals must be axis-aligned xyxy [N, 4] (x1, y1, x2, y2) so that center (px, py) and size (pw, ph) are uniquely defined; the coder then predicts deltas to transform each horizontal box into an oriented box (da, db = offsets of the oriented box's top/right edge midpoints relative to the horizontal box). Used when the RPN outputs horizontal proposals and a second stage predicts orientation (e.g. Oriented R-CNN).
Source code in oriented_det/models/bbox_coder.py
decode(proposals, deltas, wh_ratio_clip=16 / 1000)
¶
Decode deltas to oriented boxes.
proposals: [N, 4] xyxy (axis-aligned); deltas: [N, 6]. Returns [N, 5] obb (cx, cy, w, h, angle).
Source code in oriented_det/models/bbox_coder.py
encode(proposals, gt_bboxes)
¶
Encode GT obb relative to axis-aligned proposals.
proposals: [N, 4] xyxy (x1, y1, x2, y2); must be axis-aligned so (px, py, pw, ph) are well-defined. gt_bboxes: [N, 5] (cx, cy, w, h, angle). Returns [N, 6] deltas.
Source code in oriented_det/models/bbox_coder.py
OrientedRCNN
¶
Bases: ClassWeightsMixin, GroupedCeMixin, Module
Oriented R-CNN (MMRotate-style): horizontal RPN + MidpointOffset (6 params) -> oriented ROI head.
RPN anchor_angles is optional on the constructor only (not in training JSON); default is horizontal [0.0].
Source code in oriented_det/models/oriented_rcnn.py
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 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 951 952 953 954 955 956 957 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 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 | |
set_final_nms_iou_for_epoch(epoch)
¶
Update final detection NMS IoU threshold from schedule. Lower = more aggressive suppression.
Source code in oriented_det/models/oriented_rcnn.py
set_roi_box_reg_angle_weight_for_epoch(epoch)
¶
Update ROI angle (5th dim) SmoothL1 weight from schedule (0-based epoch index).
Source code in oriented_det/models/oriented_rcnn.py
set_roi_box_reg_iou_weight_for_epoch(epoch)
¶
Update ROI auxiliary IoU loss weight from schedule (0-based epoch index).
Source code in oriented_det/models/oriented_rcnn.py
RotatedFasterRCNN
¶
Bases: ClassWeightsMixin, GroupedCeMixin, Module
Rotated Faster R-CNN (MMRotate-style): horizontal RPN + horizontal RoIAlign + rotated ROI head.
Mimics MMRotate's Rotated Faster R-CNN: - Horizontal RPN with 4 reg params (DeltaXYWHBBoxCoder) - Horizontal RoIAlign - ROI head with 5 params (DeltaXYWHTHBBoxCoder)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of object classes (excluding background) |
required |
backbone
|
Optional backbone module (if None, creates ResNet+FPN) |
None
|
|
backbone_name
|
str
|
Name of backbone to create ("resnet18", "resnet50", etc.) |
'resnet50'
|
pretrained_backbone
|
bool
|
Whether to use pretrained backbone weights |
False
|
trainable_layers
|
int
|
Number of backbone layers to keep trainable. For ResNet50, typically 5 layers (conv1 + layer1-4). Set to 5 to train all layers. Default is 5 (all layers trainable). |
5
|
anchor_scales
|
Optional[List[float]]
|
List of anchor scales for RPN |
None
|
anchor_ratios
|
Optional[List[float]]
|
List of anchor aspect ratios for RPN |
None
|
rpn_pre_nms_top_n
|
int
|
Number of top proposals before NMS in RPN |
2000
|
rpn_post_nms_top_n
|
int
|
Number of top proposals after NMS in RPN |
2000
|
rpn_positive_iou_threshold
|
float
|
IoU threshold for positive RPN anchors |
0.7
|
rpn_negative_iou_threshold
|
float
|
IoU threshold for negative RPN anchors |
0.3
|
roi_positive_iou_threshold
|
float
|
IoU threshold for positive ROI proposals |
0.5
|
roi_negative_iou_threshold
|
float
|
IoU threshold for negative ROI proposals |
0.5
|
roi_output_size
|
Tuple[int, int]
|
Output size for ROI align (height, width) |
(7, 7)
|
roi_spatial_scales
|
Optional[List[float]]
|
Spatial scales for each FPN level in ROI align (defaults from |
None
|
fpn_strides
|
Optional[List[int]]
|
Nominal strides per FPN level for ROI module init; forward pass overrides with strides derived from image size and feature map shapes so RPN anchors and ROI align match the grid. |
None
|
use_checkpoint
|
bool
|
Whether to use gradient checkpointing (trades compute for memory) |
False
|
roi_chunk_size
|
int
|
Number of ROIs to process in parallel during ROI align. Lower values use less memory but are slower. Recommended: 16-64 for typical GPU memory (8-24GB). |
32
|
roi_use_checkpoint
|
bool
|
If True, use gradient checkpointing in ROI align to trade compute for memory (~2x less memory, ~30% slower). |
False
|
roi_loss_type
|
str
|
Type of classification loss: - "cross_entropy": Standard cross-entropy loss (default) - "focal": Focal loss for handling class imbalance and hard examples |
'cross_entropy'
|
roi_class_weights
|
Optional[Union[Dict[str, float], Tensor]]
|
Optional class weights for loss computation: - Dict[str, float]: Mapping from class name to weight (requires calling set_class_weights() with class_map) - torch.Tensor: Tensor of shape [num_classes + 1] (including background) - None: Equal weights for all classes (default) |
None
|
roi_focal_alpha
|
float
|
Alpha parameter for focal loss (default: 1.0) |
1.0
|
roi_focal_gamma
|
float
|
Gamma parameter for focal loss (default: 2.0) - gamma=0: equivalent to cross-entropy - gamma>0: focuses on hard examples - gamma=2: standard value used in literature |
2.0
|
Example
Standard cross-entropy loss¶
model = OrientedRCNN(num_classes=15)
Class-weighted cross-entropy loss¶
class_weights = {"small-vehicle": 0.5, "plane": 2.0, ...} model = OrientedRCNN(num_classes=15, roi_class_weights=class_weights) model.set_class_weights(class_map) # Must call before training
Focal loss¶
model = OrientedRCNN(num_classes=15, roi_loss_type="focal", roi_focal_gamma=2.0)
Focal loss with class weights¶
model = OrientedRCNN( ... num_classes=15, ... roi_loss_type="focal", ... roi_class_weights=class_weights, ... roi_focal_gamma=2.0 ... ) model.set_class_weights(class_map)
Source code in oriented_det/models/oriented_rcnn.py
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 110 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 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 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 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 243 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 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 351 352 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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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 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 670 671 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 757 758 759 760 761 | |
__init__(num_classes, *, backbone=None, backbone_name='resnet50', pretrained_backbone=False, trainable_layers=5, anchor_scales=None, anchor_ratios=None, anchor_angles=None, rpn_pre_nms_top_n=2000, rpn_post_nms_top_n=2000, rpn_min_size=0.0, max_detections_per_image=2000, rpn_nms_threshold=0.7, final_nms_iou_threshold=0.1, rpn_positive_iou_threshold=0.7, rpn_negative_iou_threshold=0.3, rpn_fg_bg_sampling_ratio=0.5, rpn_batch_size_per_image=256, rpn_min_pos_iou=0.3, rpn_match_low_quality=True, roi_match_low_quality=False, roi_min_pos_iou=0.5, add_gt_as_proposals=True, roi_positive_iou_threshold=0.5, roi_negative_iou_threshold=0.5, roi_fg_bg_sampling_ratio=0.25, roi_batch_size_per_image=512, roi_output_size=(7, 7), roi_spatial_scales=None, fpn_strides=None, returned_layers=None, use_checkpoint=False, roi_chunk_size=32, roi_use_checkpoint=False, roi_loss_type='cross_entropy', roi_class_weights=None, roi_focal_alpha=1.0, roi_focal_gamma=2.0, roi_label_smoothing=0.0, target_means=None, target_stds=None, roi_norm_factor=2.0, roi_edge_swap=True, roi_proj_xy=False, roi_box_reg_angle_weight=1.0, roi_box_reg_angle_schedule_epochs=None, roi_box_reg_angle_schedule_values=None, roi_box_reg_iou_weight=0.0, roi_box_reg_iou_loss_type='riou', roi_box_reg_kfiou_fun=None, roi_box_reg_probiou_mode=None, roi_box_reg_main_loss_type='smooth_l1', roi_box_reg_norm='sampled_all', roi_box_reg_smooth_l1_aux_weight=0.0, use_hbb_for_matching=False, inference_pre_nms_score_threshold=0.05, final_nms_iou_schedule_epochs=None, final_nms_iou_schedule_values=None, roi_box_reg_iou_schedule_epochs=None, roi_box_reg_iou_schedule_values=None, nms_class_agnostic=False, final_nms_use_cpu=False, roi_inference_top_class_only=False)
¶
Initialize Rotated Faster R-CNN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of object classes (excluding background) |
required |
backbone
|
Optional backbone module (if None, creates ResNet+FPN) |
None
|
|
backbone_name
|
str
|
Name of backbone to create ("resnet18", "resnet50", etc.) |
'resnet50'
|
pretrained_backbone
|
bool
|
Whether to use pretrained backbone weights |
False
|
trainable_layers
|
int
|
Number of backbone layers to keep trainable. For ResNet50, typically 5 layers (conv1 + layer1-4). Set to 5 to train all layers. Default is 5 (all layers trainable). |
5
|
anchor_scales
|
Optional[List[float]]
|
List of anchor scales for RPN |
None
|
anchor_ratios
|
Optional[List[float]]
|
List of anchor aspect ratios for RPN |
None
|
anchor_angles
|
Optional[List[float]]
|
Optional RPN angles in radians (Python API only; not in JSON). |
None
|
rpn_pre_nms_top_n
|
int
|
Number of top proposals before NMS in RPN |
2000
|
rpn_post_nms_top_n
|
int
|
Number of top proposals after NMS in RPN |
2000
|
rpn_positive_iou_threshold
|
float
|
IoU threshold for positive RPN anchors |
0.7
|
rpn_negative_iou_threshold
|
float
|
IoU threshold for negative RPN anchors |
0.3
|
rpn_fg_bg_sampling_ratio
|
float
|
Ratio of foreground to background in sampled RPN anchors (default: 0.5) |
0.5
|
roi_positive_iou_threshold
|
float
|
IoU threshold for positive ROI proposals |
0.5
|
roi_negative_iou_threshold
|
float
|
IoU threshold for negative ROI proposals |
0.5
|
roi_fg_bg_sampling_ratio
|
float
|
Ratio of foreground to background in sampled ROI proposals (default: 0.25) |
0.25
|
roi_output_size
|
Tuple[int, int]
|
Output size for ROI align (height, width) |
(7, 7)
|
roi_spatial_scales
|
Optional[List[float]]
|
Spatial scales for each FPN level in ROI align (defaults from |
None
|
fpn_strides
|
Optional[List[int]]
|
Nominal strides for ROI module init; forward uses strides derived from the feature grid. |
None
|
use_checkpoint
|
bool
|
Whether to use gradient checkpointing (trades compute for memory) |
False
|
roi_chunk_size
|
int
|
Number of ROIs to process in parallel during ROI align |
32
|
roi_use_checkpoint
|
bool
|
If True, use gradient checkpointing in ROI align |
False
|
roi_loss_type
|
str
|
Type of classification loss: - "cross_entropy": Standard cross-entropy loss (default) - "focal": Focal loss for handling class imbalance |
'cross_entropy'
|
roi_class_weights
|
Optional[Union[Dict[str, float], Tensor]]
|
Optional class weights for loss computation. Can be: - Dict[str, float]: Mapping from class name to weight - torch.Tensor: Tensor of shape [num_classes + 1] (including background) - None: Equal weights for all classes (default) Note: If dict is provided, it will be converted to tensor using the model's class mapping. Background (index 0) defaults to weight 1.0 if not specified. |
None
|
roi_focal_alpha
|
float
|
Alpha parameter for focal loss (default: 1.0) |
1.0
|
roi_focal_gamma
|
float
|
Gamma parameter for focal loss (default: 2.0) |
2.0
|
roi_label_smoothing
|
float
|
Label smoothing for ROI classification (default: 0.0). Use e.g. 0.1 to reduce overconfidence. |
0.0
|
target_means
|
Optional[Tuple[float, float, float, float, float]]
|
Optional means for regression target normalization [dx, dy, dw, dh, dangle]. Default: (0.0, 0.0, 0.0, 0.0, 0.0) - no centering (like MMRotate default). Can be set to empirical means computed from dataset for better normalization. |
None
|
target_stds
|
Optional[Tuple[float, float, float, float, float]]
|
Optional stds for regression target normalization [dx, dy, dw, dh, dangle]. Default: (0.1, 0.1, 0.2, 0.2, 0.1) (MMRotate DeltaXYWHTHBBoxCoder). Can be set to empirical stds computed from dataset for better normalization. |
None
|
roi_norm_factor
|
float
|
Angle scaling for ROI encode/decode (MMRotate uses 2.0). |
2.0
|
roi_edge_swap
|
bool
|
Whether to use edge_swap for ROI (MMRotate uses True). |
True
|
roi_proj_xy
|
bool
|
If True, encode/decode ROI dx/dy in proposal local frame. For horizontal xyxy RoIs (angle 0) this is equivalent to global offsets. |
False
|
roi_box_reg_angle_weight
|
float
|
Weight for the angle (5th) component in ROI box regression loss. Use > 1.0 (e.g. 2.0) to improve orientation alignment when predictions stick to anchor angles. |
1.0
|
roi_box_reg_iou_weight
|
float
|
Weight for auxiliary decoded-box loss when main is Smooth L1. |
0.0
|
roi_box_reg_iou_loss_type
|
str
|
|
'riou'
|
roi_box_reg_kfiou_fun
|
Optional[str]
|
Optional KFIoU overlap mapping when using |
None
|
roi_box_reg_probiou_mode
|
Optional[str]
|
|
None
|
roi_box_reg_main_loss_type
|
str
|
Primary ROI reg loss: |
'smooth_l1'
|
roi_box_reg_norm
|
str
|
|
'sampled_all'
|
roi_box_reg_smooth_l1_aux_weight
|
float
|
Encoded Smooth L1 aux when main is decoded. |
0.0
|
roi_min_pos_iou
|
float
|
Min IoU for low-quality ROI matches when |
0.5
|
roi_inference_top_class_only
|
bool
|
If True, ROI inference uses argmax fg class per proposal
(see |
False
|
Source code in oriented_det/models/oriented_rcnn.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 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 243 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 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 351 352 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 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
forward(images, targets=None)
¶
Forward pass through oriented R-CNN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Sequence[Tensor]
|
List of image tensors (C, H, W) in [0, 1] range |
required |
targets
|
Optional[Sequence[Dict[str, Any]]]
|
Optional list of target dicts for training. Each dict must contain: - "rboxes" (List[RBox] or tensor [N, 5] with format [cx, cy, w, h, angle]) - "labels" (tensor [N]) |
None
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, Tensor], List[Dict[str, Any]]]
|
|
Union[Dict[str, Tensor], List[Dict[str, Any]]]
|
|
Source code in oriented_det/models/oriented_rcnn.py
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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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 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 670 671 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 757 758 759 760 761 | |
set_final_nms_iou_for_epoch(epoch)
¶
Update final detection NMS IoU threshold from schedule. Lower = more aggressive suppression.
Source code in oriented_det/models/oriented_rcnn.py
set_roi_box_reg_angle_weight_for_epoch(epoch)
¶
Update ROI angle (5th dim) SmoothL1 weight from schedule (0-based epoch index).
Source code in oriented_det/models/oriented_rcnn.py
set_roi_box_reg_iou_weight_for_epoch(epoch)
¶
Update ROI auxiliary IoU loss weight from schedule (0-based epoch index).
Source code in oriented_det/models/oriented_rcnn.py
RotatedRetinaNet
¶
Bases: Module
Complete Rotated RetinaNet model for true oriented object detection.
This model implements a full single-stage oriented detector that: - Uses MMRotate-style anchor priors: horizontal boxes as (cx, cy, w, h, theta) with fixed theta=0 at init - Predicts oriented bounding boxes with 5 parameters (cx, cy, w, h, angle) - Uses oriented IoU for matching and oriented NMS for post-processing - Preserves angle information throughout training and inference - Uses sigmoid focal loss for classification (MMRotate FocalLoss use_sigmoid=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of object classes (excluding background) |
required |
backbone
|
Optional backbone module (if None, creates ResNet+FPN) |
None
|
|
backbone_name
|
str
|
Name of backbone to create ("resnet18", "resnet50", etc.) |
'resnet50'
|
pretrained_backbone
|
bool
|
Whether to use pretrained backbone weights |
False
|
trainable_layers
|
int
|
Number of backbone layers to keep trainable |
5
|
anchor_scales
|
Optional[List[float]]
|
List of anchor scales for Rotated RetinaNet |
None
|
anchor_ratios
|
Optional[List[float]]
|
List of anchor aspect ratios for Rotated RetinaNet |
None
|
anchor_angles
|
Optional[List[float]]
|
Optional RPN anchor angles in radians (advanced, not in |
None
|
positive_iou_threshold
|
float
|
IoU threshold for positive anchors |
0.5
|
negative_iou_threshold
|
float
|
IoU threshold for negative anchors |
0.4
|
focal_alpha
|
float
|
Alpha parameter for focal loss (default: 0.25, Rotated RetinaNet standard) |
0.25
|
focal_gamma
|
float
|
Gamma parameter for focal loss (default: 2.0, Rotated RetinaNet standard) |
2.0
|
box_reg_weight
|
float
|
Weight for box regression loss (default: 1.0) |
1.0
|
score_threshold
|
float
|
Score threshold for inference (default: 0.05) |
0.05
|
final_nms_iou_threshold
|
float
|
IoU threshold for final oriented NMS (default: 0.5); not RPN NMS |
0.5
|
max_detections_per_image
|
int
|
Maximum number of detections per image (default: 100) |
100
|
final_nms_iou_schedule_epochs
|
Optional[List[int]]
|
Optional epoch boundaries for final NMS IoU schedule (e.g. [50, 150, 250]) |
None
|
final_nms_iou_schedule_values
|
Optional[List[float]]
|
Final NMS IoU per segment (e.g. [0.6, 0.45, 0.35, 0.25]); lower = more suppression |
None
|
target_means
|
Optional[Tuple[float, float, float, float, float]]
|
Optional means for target normalization (MMRotate compatibility) |
None
|
target_stds
|
Optional[Tuple[float, float, float, float, float]]
|
Optional stds for target normalization (MMRotate compatibility) |
None
|
norm_factor
|
Optional[float]
|
Optional angle scaling for encode/decode (MMRotate Rotated RetinaNet uses None) |
None
|
edge_swap
|
bool
|
Whether to use edge_swap in bbox coder (MMRotate uses True) |
True
|
use_hbb_for_matching
|
bool
|
If True, use HBB IoU for anchor-GT matching (optional; priors use a single reference angle). |
False
|
final_nms_use_cpu
|
bool
|
If True, skip GPU sampling NMS and run final class-aware NMS with exact polygon IoU on CPU. |
False
|
Example
model = RotatedRetinaNet(num_classes=15, backbone_name="resnet50") model.train() losses = model(images, targets) model.eval() predictions = model(images)
Source code in oriented_det/models/rotated_retinanet.py
426 427 428 429 430 431 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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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 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 670 671 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 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 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 951 952 953 954 955 956 957 958 | |
forward(images, targets=None)
¶
Forward pass through Rotated RetinaNet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Sequence[Tensor]
|
List of image tensors (C, H, W) in [0, 1] range |
required |
targets
|
Optional[Sequence[Dict[str, Any]]]
|
Optional list of target dicts for training. Each dict must contain: - "rboxes" (List[RBox] or tensor [N, 5] with format [cx, cy, w, h, angle]) - "labels" (tensor [N], 1-indexed) |
None
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, Tensor], List[Dict[str, Any]]]
|
|
Union[Dict[str, Tensor], List[Dict[str, Any]]]
|
|
Source code in oriented_det/models/rotated_retinanet.py
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 670 671 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 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 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 951 952 953 954 955 956 957 958 | |
set_box_reg_iou_weight_for_epoch(epoch)
¶
Update anchor auxiliary IoU loss weight from schedule (0-based epoch index).
Source code in oriented_det/models/rotated_retinanet.py
set_final_nms_iou_for_epoch(epoch)
¶
Update final detection NMS IoU threshold from schedule for the given epoch. Lower threshold = more aggressive suppression of overlapping boxes.
Source code in oriented_det/models/rotated_retinanet.py
build_resnet_fpn_backbone(backbone_name='resnet50', *, pretrained=False, trainable_layers=3, norm_layer=None, returned_layers=None, use_p6p7_extra_levels=False)
¶
Create a ResNet+FPN backbone or a minimal fallback if torchvision is missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone_name
|
str
|
ResNet variant ("resnet50", etc.). |
'resnet50'
|
pretrained
|
bool
|
Whether to load ImageNet backbone weights. |
False
|
trainable_layers
|
int
|
Number of backbone stages to train. |
3
|
norm_layer
|
Norm layer override. Default None keeps torchvision's
|
None
|
|
returned_layers
|
Optional[List[int]]
|
ResNet stage indices to use for FPN (1–4). Default None uses torchvision default [1,2,3,4] (C2–C5, 5 levels with P6). Use [2,3,4] for MMRotate-style FPN (C3–C5 only, 4 levels with P6). |
None
|
use_p6p7_extra_levels
|
bool
|
If True, attach |
False
|
Source code in oriented_det/models/backbones/resnet_fpn.py
derive_fpn_strides_from_grid(image_size, feature_map_sizes)
¶
Compute FPN strides from the real input size and backbone feature map shapes.
For each level, stride is image_size / feature_map_size (H and W must agree).
This keeps anchors, RPN, and ROI align consistent with the actual grid even if
fpn_strides in config is wrong or stale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_size
|
Tuple[int, int]
|
|
required |
feature_map_sizes
|
Sequence[Tuple[int, int]]
|
|
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
One integer stride per level. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If sizes are invalid or H/W imply different strides. |
Source code in oriented_det/models/utils.py
extract_backbone_features(backbone, images, use_checkpoint=False, training=False, include_pool_level=False)
¶
Extract features from backbone and convert to list format.
Handles: - Stacking images into batch - Optional gradient checkpointing - Converting OrderedDict/dict outputs to list format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone
|
Module
|
Backbone module |
required |
images
|
Sequence[Tensor]
|
Sequence of image tensors (C, H, W) in [0, 1] range |
required |
use_checkpoint
|
bool
|
Whether to use gradient checkpointing |
False
|
training
|
bool
|
Whether model is in training mode |
False
|
include_pool_level
|
bool
|
If True, also keep torchvision FPN's |
False
|
Returns:
| Type | Description |
|---|---|
List[Tensor]
|
List of feature maps from FPN, each of shape [B, C, H, W] |
Source code in oriented_det/models/utils.py
prepare_targets(targets, device=None)
¶
Prepare targets for training, preserving oriented boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
Sequence[Dict[str, Any]]
|
List of target dicts, each containing: - "rboxes" (List[RBox] or tensor [N, 5] with format [cx, cy, w, h, angle]) - "labels" (tensor [N] or list, 1-indexed) |
required |
device
|
Optional[device]
|
Optional device to move tensors to |
None
|
Returns:
| Type | Description |
|---|---|
List[Tensor]
|
Tuple of (gt_boxes_list, gt_labels_list, gt_boxes_ignore_list): |
List[Tensor]
|
|
List[Tensor]
|
|
Tuple[List[Tensor], List[Tensor], List[Tensor]]
|
|
Source code in oriented_det/models/utils.py
rboxes_to_tensor(rboxes, device=None)
¶
Convert RBoxes to tensor format [N, 5] with [cx, cy, w, h, angle].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rboxes
|
Sequence[RBox]
|
Sequence of RBox objects |
required |
device
|
Optional[device]
|
Optional device to create tensor on |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of shape [N, 5] with format [cx, cy, w, h, angle] |
Source code in oriented_det/models/utils.py
setup_anchors(anchor_scales, anchor_ratios, anchor_angles, default_angles, octave_base_scale=None, scales_per_octave=None)
¶
Setup anchor configuration and return scales, ratios, angles, and num_anchors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor_scales
|
Optional[List[float]]
|
Optional list of anchor scales (default: [8]) |
required |
anchor_ratios
|
Optional[List[float]]
|
Optional list of anchor ratios (default: [0.5, 1.0, 2.0]) |
required |
anchor_angles
|
Optional[List[float]]
|
Optional list of anchor angles (default: provided) |
required |
default_angles
|
List[float]
|
Default angles to use if anchor_angles is None |
required |
octave_base_scale
|
Optional[float]
|
MMRotate RetinaNet |
None
|
scales_per_octave
|
Optional[int]
|
MMRotate RetinaNet |
None
|
Returns:
| Type | Description |
|---|---|
List[float]
|
Tuple of (scales, ratios, angles, num_anchors): |
List[float]
|
|
List[float]
|
|
int
|
|
Tuple[List[float], List[float], List[float], int]
|
|
Source code in oriented_det/models/utils.py
setup_backbone(backbone, backbone_name='resnet50', pretrained_backbone=False, trainable_layers=5, returned_layers=None, use_p6p7_extra_levels=False)
¶
Setup backbone and return backbone module and output channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone
|
Optional[Module]
|
Optional backbone module (if None, creates ResNet+FPN) |
required |
backbone_name
|
str
|
Name of backbone to create ("resnet18", "resnet50", etc.) |
'resnet50'
|
pretrained_backbone
|
bool
|
Whether to use pretrained backbone weights |
False
|
trainable_layers
|
int
|
Number of backbone layers to keep trainable |
5
|
returned_layers
|
Optional[List[int]]
|
ResNet stages for FPN (e.g. [2,3,4] for MMRotate C3–C5 only) |
None
|
use_p6p7_extra_levels
|
bool
|
RetinaNet-style P6/P7 convs on C5 (MMRotate on_input). |
False
|
Returns:
| Type | Description |
|---|---|
Module
|
Tuple of (backbone, output_channels): |
int
|
|
Tuple[Module, int]
|
|
Source code in oriented_det/models/utils.py
tensor_to_rboxes(boxes)
¶
Convert tensor [N, 5] with [cx, cy, w, h, angle] to RBoxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
Tensor
|
Tensor of shape [N, 5] with format [cx, cy, w, h, angle] |
required |
Returns:
| Type | Description |
|---|---|
List[RBox]
|
List of RBox objects |
Source code in oriented_det/models/utils.py
warn_if_fpn_strides_mismatch(configured, derived)
¶
Emit a single warning if config strides disagree with grid-derived strides.