<jupyter_start><jupyter_text>FeatMap-size-aware heads and necks<jupyter_code>out_channels = 100#backbone.out_channels

from torchvision.models.detection.rpn import AnchorGenerator
anchor_sizes = ((32,), (64,), (128,), (256,), (512,))
aspect_ratios = ((1.0,),) * len(anchor_sizes)
rpn_anchor_generator = AnchorGenerator(
    anchor_sizes, aspect_ratios
)
# anchor_sizes = ((1,),(1,),(1,),(1,))#((50, 100), (25, 50), (40,15), (14,))#tuple(reversed())
# aspect_ratios = ((1.0,),) * len(anchor_sizes)
# rpn_anchor_generator = AnchorGenerator(
#     anchor_sizes, aspect_ratios
# )

# from torchvision.models.detection.rpn import RPNHead
# rpn_head = RPNHead(
#     out_channels, rpn_anchor_generator.num_anchors_per_location()[0]
# )

# rpn_pre_nms_top_n = dict(training=rpn_pre_nms_top_n_train, testing=rpn_pre_nms_top_n_test)
# rpn_post_nms_top_n = dict(training=rpn_post_nms_top_n_train, testing=rpn_post_nms_top_n_test)

# from torchvision.models.detection.rpn import RegionProposalNetwork
# rpn = RegionProposalNetwork(
#     rpn_anchor_generator, rpn_head,
#     rpn_fg_iou_thresh, rpn_bg_iou_thresh,
#     rpn_batch_size_per_image, rpn_positive_fraction,
#     rpn_pre_nms_top_n, rpn_post_nms_top_n, rpn_nms_thresh)
aspect_ratios
rpn_anchor_generator.num_anchors_per_location()<jupyter_output><empty_output><jupyter_text>Faster RCNN heads<jupyter_code>from torchvision.ops import MultiScaleRoIAlign
box_roi_pool = MultiScaleRoIAlign(
    featmap_names=[0, 1, 2, 3],
    output_size=7,
    sampling_ratio=2)

# from torchvision.models.detection.faster_rcnn import TwoMLPHead
# resolution = box_roi_pool.output_size[0]
# representation_size = 1024
# box_head = TwoMLPHead(
#     out_channels * resolution ** 2,
#     representation_size)

# from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
# representation_size = 1024
# box_predictor = FastRCNNPredictor(
#     representation_size,
#     num_classes)

# from torchvision.models.detection.roi_heads import RoIHeads
# roi_heads = RoIHeads(
#     # Box
#     box_roi_pool, box_head, box_predictor,
#     box_fg_iou_thresh, box_bg_iou_thresh,
#     box_batch_size_per_image, box_positive_fraction,
#     bbox_reg_weights,
#     box_score_thresh, box_nms_thresh, box_detections_per_img)<jupyter_output><empty_output><jupyter_text>MASK RCNN heads<jupyter_code>from torchvision.ops import MultiScaleRoIAlign
mask_roi_pool = MultiScaleRoIAlign(
    featmap_names=[0, 1, 2, 3],
    output_size=14,
    sampling_ratio=2)

# from torchvision.models.detection.mask_rcnn import MaskRCNNHeads
# mask_layers = (256, 256, 256, 256)
# mask_dilation = 1
# mask_head = MaskRCNNHeads(out_channels, mask_layers, mask_dilation)

# from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor
# mask_predictor_in_channels = 256  # == mask_layers[-1]
# mask_dim_reduced = 256
# mask_predictor = MaskRCNNPredictor(mask_predictor_in_channels,
#                                    mask_dim_reduced, num_classes)<jupyter_output><empty_output><jupyter_text>Keypoints RCNN heads  Putting everything together<jupyter_code>kwargs = {'mask_roi_pool' : mask_roi_pool,
            'box_roi_pool' : box_roi_pool}                      
#           'rpn_anchor_generator' : rpn_anchor_generator}
#           'rpn_head':rpn_head}
model = maskrcnn_mobileNetV3_fpn(num_classes=3, backbone_chkp=chkp_path, **kwargs)
model.eval()
inp = torch.ones(1,3,800,800)
with torch.no_grad():
    %time out = model.forward(inp)
out
with torch.no_grad():
    %time out = model.forward(inp)
model
from torchvision.models.detection.mask_rcnn import maskrcnn_resnet50_fpn
model_stock = maskrcnn_resnet50_fpn('resnet')
model_stock
from torchvision.models.detection.backbone_utils import resnet_fpn_backbone
resnet50_fpn = resnet_fpn_backbone('resnet50', False)
resnet50_fpn.eval()
inp = torch.ones(1,3,800,800)
with torch.no_grad():
    %time out = resnet50_fpn.forward(inp)

len(out)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10541021082042164, 'fraction_numerical': 0.028702057404114807, 'mean_word_length': 5.945326278659612, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '416218', 'license': 'Apache-2.0', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1493, 'n_words': 217, 'path': 'FPN-Test.ipynb', 'repo_name': 'IgorDavidyuk/pytorch-mobilenet-v3', 'size': '3937'}