
    ڧgU                     *   d dl mZmZ d dlZd dlmZ d dlmZ ddlmZ	 ddl
mZ dd	lmZmZmZ dd
lmZmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ g dZ G d de          Z  G d dej!                  Z" G d dej#                  Z$eeddZ% G d de          Z& e             edd fdej'        f          dd ddej'        dd!d"ee&         d#e(d$ee)         d%ee)         d&ee         d'ee)         d(ed)e fd*                        Z*dS )+    )AnyOptionalN)nn)MultiScaleRoIAlign   )misc)ObjectDetection   )register_modelWeightsWeightsEnum)_COCO_PERSON_CATEGORIES_COCO_PERSON_KEYPOINT_NAMES)_ovewrite_value_paramhandle_legacy_interface)resnet50ResNet50_Weights   )overwrite_eps)_resnet_fpn_extractor_validate_trainable_layers)
FasterRCNN)KeypointRCNN!KeypointRCNN_ResNet50_FPN_Weightskeypointrcnn_resnet50_fpnc                   d     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Z xZS )r   a"  
    Implements Keypoint R-CNN.

    The input to the model is expected to be a list of tensors, each of shape [C, H, W], one for each
    image, and should be in 0-1 range. Different images can have different sizes.

    The behavior of the model changes depending on if it is in training or evaluation mode.

    During training, the model expects both the input tensors and targets (list of dictionary),
    containing:

        - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with
            ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
        - labels (Int64Tensor[N]): the class label for each ground-truth box
        - keypoints (FloatTensor[N, K, 3]): the K keypoints location for each of the N instances, in the
          format [x, y, visibility], where visibility=0 means that the keypoint is not visible.

    The model returns a Dict[Tensor] during training, containing the classification and regression
    losses for both the RPN and the R-CNN, and the keypoint loss.

    During inference, the model requires only the input tensors, and returns the post-processed
    predictions as a List[Dict[Tensor]], one for each input image. The fields of the Dict are as
    follows:

        - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with
            ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
        - labels (Int64Tensor[N]): the predicted labels for each image
        - scores (Tensor[N]): the scores or each prediction
        - keypoints (FloatTensor[N, K, 3]): the locations of the predicted keypoints, in [x, y, v] format.

    Args:
        backbone (nn.Module): the network used to compute the features for the model.
            It should contain an out_channels attribute, which indicates the number of output
            channels that each feature map has (and it should be the same for all feature maps).
            The backbone should return a single Tensor or and OrderedDict[Tensor].
        num_classes (int): number of output classes of the model (including the background).
            If box_predictor is specified, num_classes should be None.
        min_size (int): Images are rescaled before feeding them to the backbone:
            we attempt to preserve the aspect ratio and scale the shorter edge
            to ``min_size``. If the resulting longer edge exceeds ``max_size``,
            then downscale so that the longer edge does not exceed ``max_size``.
            This may result in the shorter edge beeing lower than ``min_size``.
        max_size (int): See ``min_size``.
        image_mean (Tuple[float, float, float]): mean values used for input normalization.
            They are generally the mean values of the dataset on which the backbone has been trained
            on
        image_std (Tuple[float, float, float]): std values used for input normalization.
            They are generally the std values of the dataset on which the backbone has been trained on
        rpn_anchor_generator (AnchorGenerator): module that generates the anchors for a set of feature
            maps.
        rpn_head (nn.Module): module that computes the objectness and regression deltas from the RPN
        rpn_pre_nms_top_n_train (int): number of proposals to keep before applying NMS during training
        rpn_pre_nms_top_n_test (int): number of proposals to keep before applying NMS during testing
        rpn_post_nms_top_n_train (int): number of proposals to keep after applying NMS during training
        rpn_post_nms_top_n_test (int): number of proposals to keep after applying NMS during testing
        rpn_nms_thresh (float): NMS threshold used for postprocessing the RPN proposals
        rpn_fg_iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be
            considered as positive during training of the RPN.
        rpn_bg_iou_thresh (float): maximum IoU between the anchor and the GT box so that they can be
            considered as negative during training of the RPN.
        rpn_batch_size_per_image (int): number of anchors that are sampled during training of the RPN
            for computing the loss
        rpn_positive_fraction (float): proportion of positive anchors in a mini-batch during training
            of the RPN
        rpn_score_thresh (float): only return proposals with an objectness score greater than rpn_score_thresh
        box_roi_pool (MultiScaleRoIAlign): the module which crops and resizes the feature maps in
            the locations indicated by the bounding boxes
        box_head (nn.Module): module that takes the cropped feature maps as input
        box_predictor (nn.Module): module that takes the output of box_head and returns the
            classification logits and box regression deltas.
        box_score_thresh (float): during inference, only return proposals with a classification score
            greater than box_score_thresh
        box_nms_thresh (float): NMS threshold for the prediction head. Used during inference
        box_detections_per_img (int): maximum number of detections per image, for all classes.
        box_fg_iou_thresh (float): minimum IoU between the proposals and the GT box so that they can be
            considered as positive during training of the classification head
        box_bg_iou_thresh (float): maximum IoU between the proposals and the GT box so that they can be
            considered as negative during training of the classification head
        box_batch_size_per_image (int): number of proposals that are sampled during training of the
            classification head
        box_positive_fraction (float): proportion of positive proposals in a mini-batch during training
            of the classification head
        bbox_reg_weights (Tuple[float, float, float, float]): weights for the encoding/decoding of the
            bounding boxes
        keypoint_roi_pool (MultiScaleRoIAlign): the module which crops and resizes the feature maps in
             the locations indicated by the bounding boxes, which will be used for the keypoint head.
        keypoint_head (nn.Module): module that takes the cropped feature maps as input
        keypoint_predictor (nn.Module): module that takes the output of the keypoint_head and returns the
            heatmap logits

    Example::

        >>> import torch
        >>> import torchvision
        >>> from torchvision.models.detection import KeypointRCNN
        >>> from torchvision.models.detection.anchor_utils import AnchorGenerator
        >>>
        >>> # load a pre-trained model for classification and return
        >>> # only the features
        >>> backbone = torchvision.models.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).features
        >>> # KeypointRCNN needs to know the number of
        >>> # output channels in a backbone. For mobilenet_v2, it's 1280,
        >>> # so we need to add it here
        >>> backbone.out_channels = 1280
        >>>
        >>> # let's make the RPN generate 5 x 3 anchors per spatial
        >>> # location, with 5 different sizes and 3 different aspect
        >>> # ratios. We have a Tuple[Tuple[int]] because each feature
        >>> # map could potentially have different sizes and
        >>> # aspect ratios
        >>> anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),
        >>>                                    aspect_ratios=((0.5, 1.0, 2.0),))
        >>>
        >>> # let's define what are the feature maps that we will
        >>> # use to perform the region of interest cropping, as well as
        >>> # the size of the crop after rescaling.
        >>> # if your backbone returns a Tensor, featmap_names is expected to
        >>> # be ['0']. More generally, the backbone should return an
        >>> # OrderedDict[Tensor], and in featmap_names you can choose which
        >>> # feature maps to use.
        >>> roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=['0'],
        >>>                                                 output_size=7,
        >>>                                                 sampling_ratio=2)
        >>>
        >>> keypoint_roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=['0'],
        >>>                                                          output_size=14,
        >>>                                                          sampling_ratio=2)
        >>> # put the pieces together inside a KeypointRCNN model
        >>> model = KeypointRCNN(backbone,
        >>>                      num_classes=2,
        >>>                      rpn_anchor_generator=anchor_generator,
        >>>                      box_roi_pool=roi_pooler,
        >>>                      keypoint_roi_pool=keypoint_roi_pooler)
        >>> model.eval()
        >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
        >>> predictions = model(x)
    N5      ffffff?333333?         ?        皙?d            ?c"                 (   t          |t          t          d           f          st          d          |d}|!| t	          d          nd}!|j        }#|t          g ddd          }|6t          d	 t          d
          D                       }$t          |#|$          }| d}%t          |%|!          }  t                      j        |||||||||	|
|||||||||||||||||||fi |" || j        _        || j        _        | | j        _        d S )Nzckeypoint_roi_pool should be of type MultiScaleRoIAlign or None instead of {type(keypoint_roi_pool)})i  i  i  i  i   i   zAnum_keypoints should be None when keypoint_predictor is specified   )0123   r
   )featmap_namesoutput_sizesampling_ratioc              3      K   | ]}d V  dS )r'   N ).0_s     f/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/detection/keypoint_rcnn.py	<genexpr>z(KeypointRCNN.__init__.<locals>.<genexpr>   s"      #:#:AC#:#:#:#:#:#:       r'   )
isinstancer   type	TypeError
ValueErrorout_channelstuplerangeKeypointRCNNHeadsKeypointRCNNPredictorsuper__init__	roi_headskeypoint_roi_poolkeypoint_headkeypoint_predictor)'selfbackbonenum_classesmin_sizemax_size
image_mean	image_stdrpn_anchor_generatorrpn_headrpn_pre_nms_top_n_trainrpn_pre_nms_top_n_testrpn_post_nms_top_n_trainrpn_post_nms_top_n_testrpn_nms_threshrpn_fg_iou_threshrpn_bg_iou_threshrpn_batch_size_per_imagerpn_positive_fractionrpn_score_threshbox_roi_poolbox_headbox_predictorbox_score_threshbox_nms_threshbox_detections_per_imgbox_fg_iou_threshbox_bg_iou_threshbox_batch_size_per_imagebox_positive_fractionbbox_reg_weightsrG   rH   rI   num_keypointskwargsr?   keypoint_layerskeypoint_dim_reduced	__class__s'                                         r7   rE   zKeypointRCNN.__init__   s   T +.@$t**-MNN 	u   5H$!- !deee . M,$ 2AUAUAUcevw x x x ##:#:q#:#:#:::O-lOLLM%#& !67K]![![ #"$#$!"$!A"	
 "	
B C"	
 "	
 "	
H ,=('4$,>)))r9   ) NNr   NNNNr   r   r   r   r    r    r!   r"   r#   r$   NNNr%   r#   r&   r#   r#   r'   r(   NNNNN)__name__
__module____qualname____doc__rE   __classcell__rl   s   @r7   r   r      s        H HZ ! $#!% $!$!"!$"Mj? j? j? j? j? j? j? j? j? j?r9   r   c                        e Zd Z fdZ xZS )rB   c           
         g }|}|D ]X}|                     t          j        ||ddd                     |                     t          j        d                     |}Y t	                      j        |  |                                 D ]h}t          |t          j                  rLt          j        	                    |j
        dd           t          j                            |j        d	           id S )
Nr   r   stridepaddingT)inplacefan_outrelumodenonlinearityr   )appendr   Conv2dReLUrD   rE   childrenr;   initkaiming_normal_weight	constant_bias)rJ   in_channelslayersdnext_featurer?   mrl   s          r7   rE   zKeypointRCNNHeads.__init__  s    "" 	( 	(LHHRY|\1QPQRRRSSSHHRWT***+++'LL! 	- 	-A!RY'' -''yv'VVV!!!&!,,,	- 	-r9   )rm   rn   ro   rE   rq   rr   s   @r7   rB   rB     s8        - - - - - - - - -r9   rB   c                   $     e Zd Z fdZd Z xZS )rC   c                 `   t                                                       |}d}t          j        |||d|dz  dz
            | _        t          j                            | j        j        dd           t          j                            | j        j	        d           d| _
        || _        d S )	N   r
   r   ru   ry   rz   r{   r   )rD   rE   r   ConvTranspose2dkps_score_lowresr   r   r   r   r   up_scaler?   )rJ   r   rh   input_featuresdeconv_kernelrl   s        r7   rE   zKeypointRCNNPredictor.__init__!  s    $ " 2!Q&*!
 !
 !
 	 5 <9[abbb
$/4a888)r9   c                     |                      |          }t          j        j                            |t          | j                  ddd          S )NbilinearF)scale_factorr|   align_cornersrecompute_scale_factor)r   torchr   
functionalinterpolatefloatr   )rJ   xs     r7   forwardzKeypointRCNNPredictor.forward1  sN    !!!$$x"..E$-00zQVot / 
 
 	
r9   )rm   rn   ro   rE   r   rq   rr   s   @r7   rC   rC      sG        * * * * * 
 
 
 
 
 
 
r9   rC   )r   r   )
categorieskeypoint_namesrM   c                       e Zd Z edei eddddddidd	d
d          Z edei eddddddidd	dd          ZeZdS )r   zOhttps://download.pytorch.org/models/keypointrcnn_resnet50_fpn_coco-9f466800.pthi\z-https://github.com/pytorch/vision/issues/1606zCOCO-val2017gLI@ǧN@)box_mapkp_mapg!rh`@g}?5^Al@z
                These weights were produced by following a similar training recipe as on the paper but use a checkpoint
                from an early epoch.
            )
num_paramsrecipe_metrics_ops
_file_size_docs)url
transformsmetazOhttps://download.pytorch.org/models/keypointrcnn_resnet50_fpn_coco-fc266e95.pthzOhttps://github.com/pytorch/vision/tree/main/references/detection#keypoint-r-cnngLK@g     @P@g=
ףp-a@zSThese weights were produced by following a similar training recipe as on the paper.N)	rm   rn   ro   r   r	   _COMMON_METACOCO_LEGACYCOCO_V1DEFAULTr4   r9   r7   r   r   ?  s        ']"

"E#"! ! !
 
 
  K* g]"

"g#"! ! !n
 
 
  G$ GGGr9   r   
pretrainedc                 J    | d         dk    rt           j        nt           j        S )Nr   legacy)r   r   r   )ri   s    r7   <lambda>r   n  s(    ,8++ 9DD.6 r9   pretrained_backbone)weightsweights_backboneT)r   progressrL   rh   r   trainable_backbone_layersr   r   rL   rh   r   r   ri   returnc                    t                               |           } t          j        |          }| Ud}t          d|t	          | j        d                             }t          d|t	          | j        d                             }n|d}|d}| dup|du}t          ||dd	          }|rt          j        nt          j
        }t          |||
          }	t          |	|          }	t          |	|fd|i|}
| J|
                    |                     |d                     | t           j        k    rt#          |
d           |
S )a+  
    Constructs a Keypoint R-CNN model with a ResNet-50-FPN backbone.

    .. betastatus:: detection module

    Reference: `Mask R-CNN <https://arxiv.org/abs/1703.06870>`__.

    The input to the model is expected to be a list of tensors, each of shape ``[C, H, W]``, one for each
    image, and should be in ``0-1`` range. Different images can have different sizes.

    The behavior of the model changes depending on if it is in training or evaluation mode.

    During training, the model expects both the input tensors and targets (list of dictionary),
    containing:

        - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with
          ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
        - labels (``Int64Tensor[N]``): the class label for each ground-truth box
        - keypoints (``FloatTensor[N, K, 3]``): the ``K`` keypoints location for each of the ``N`` instances, in the
          format ``[x, y, visibility]``, where ``visibility=0`` means that the keypoint is not visible.

    The model returns a ``Dict[Tensor]`` during training, containing the classification and regression
    losses for both the RPN and the R-CNN, and the keypoint loss.

    During inference, the model requires only the input tensors, and returns the post-processed
    predictions as a ``List[Dict[Tensor]]``, one for each input image. The fields of the ``Dict`` are as
    follows, where ``N`` is the number of detected instances:

        - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with
          ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
        - labels (``Int64Tensor[N]``): the predicted labels for each instance
        - scores (``Tensor[N]``): the scores or each instance
        - keypoints (``FloatTensor[N, K, 3]``): the locations of the predicted keypoints, in ``[x, y, v]`` format.

    For more details on the output, you may refer to :ref:`instance_seg_output`.

    Keypoint R-CNN is exportable to ONNX for a fixed batch size with inputs images of fixed size.

    Example::

        >>> model = torchvision.models.detection.keypointrcnn_resnet50_fpn(weights=KeypointRCNN_ResNet50_FPN_Weights.DEFAULT)
        >>> model.eval()
        >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
        >>> predictions = model(x)
        >>>
        >>> # optionally, if you want to export the model to ONNX:
        >>> torch.onnx.export(model, x, "keypoint_rcnn.onnx", opset_version = 11)

    Args:
        weights (:class:`~torchvision.models.detection.KeypointRCNN_ResNet50_FPN_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.detection.KeypointRCNN_ResNet50_FPN_Weights`
            below for more details, and possible values. By default, no
            pre-trained weights are used.
        progress (bool): If True, displays a progress bar of the download to stderr
        num_classes (int, optional): number of output classes of the model (including the background)
        num_keypoints (int, optional): number of keypoints
        weights_backbone (:class:`~torchvision.models.ResNet50_Weights`, optional): The
            pretrained weights for the backbone.
        trainable_backbone_layers (int, optional): number of trainable (not frozen) layers starting from final block.
            Valid values are between 0 and 5, with 5 meaning all backbone layers are trainable. If ``None`` is
            passed (the default) this value is set to 3.

    .. autoclass:: torchvision.models.detection.KeypointRCNN_ResNet50_FPN_Weights
        :members:
    NrL   r   rh   r   r
   r*      r   )r   r   
norm_layerT)r   
check_hashr$   )r   verifyr   r   lenr   r   misc_nn_opsFrozenBatchNorm2dr   BatchNorm2dr   r   r   load_state_dictget_state_dictr   r   )r   r   rL   rh   r   r   ri   
is_trainedr   rK   models              r7   r   r   j  sf   l 066w??G'./?@@+M;GLYeLfHgHghh-o}cRYR^_oRpNqNqrrK M$D(8(DJ ::G`bcef g g2<P..".J 08PZ[[[H$X/HIIH;VVmVvVVEg44hSW4XXYYY7???%%%%Lr9   )+typingr   r   r   r   torchvision.opsr   opsr   r   transforms._presetsr	   _apir   r   r   _metar   r   _utilsr   r   resnetr   r   r   backbone_utilsr   r   faster_rcnnr   __all__r   
SequentialrB   ModulerC   r   r   IMAGENET1K_V1boolintr   r4   r9   r7   <module>r      s                          . . . . . . & & & & & & 2 2 2 2 2 2 7 7 7 7 7 7 7 7 7 7 H H H H H H H H C C C C C C C C / / / / / / / / ! ! ! ! ! ! M M M M M M M M # # # # # #  u? u? u? u? u?: u? u? u?p- - - - - - - -
 
 
 
 
BI 
 
 
2 *1 ( ( ( ( ( ( ( (V 	7 	7 ,-=-KL   <@!%#'3C3Q/3f f f78f f #	f
 C=f /0f  (}f f f f f  f f fr9   