
    %`iA                     t    d Z ddlZddlmZ ddlmZmZ ddlZddlmZ ddl	m
Z
  G d d	ej                  ZdS )
z,
Implements the Generalized R-CNN framework
    N)OrderedDict)OptionalUnion)nn   )_log_api_usage_oncec                       e Zd ZdZdej        dej        dej        dej        ddf
 fdZej        j	        d	e
eej        f         d
ee
eej        f                  dee
eej        f         ee
eej        f                  f         fd            Z	 ddeej                 deee
eej        f                           dee
eej        f         ee
eej        f                  f         fdZ xZS )GeneralizedRCNNad  
    Main class for Generalized R-CNN.

    Args:
        backbone (nn.Module):
        rpn (nn.Module):
        roi_heads (nn.Module): takes the features + the proposals from the RPN and computes
            detections / masks from it.
        transform (nn.Module): performs the data transformation from the inputs to feed into
            the model
    backbonerpn	roi_heads	transformreturnNc                     t                                                       t          |            || _        || _        || _        || _        d| _        d S )NF)super__init__r   r   r   r   r   _has_warned)selfr   r   r   r   	__class__s        /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchvision/models/detection/generalized_rcnn.pyr   zGeneralizedRCNN.__init__   sS     	D!!!" "     losses
detectionsc                     | j         r|S |S N)training)r   r   r   s      r   eager_outputszGeneralizedRCNN.eager_outputs,   s     = 	Mr   imagestargetsc           	      :   | j         r|t          j        dd           n|D ]}|d         }t          |t          j                  rFt          j        t          |j                  dk    o|j        d         dk    d|j         d	           jt          j        dd
t          |           d	           g }|D ]j}|j        dd         }t          j        t          |          dk    d|j        dd                     |                    |d         |d         f           k| 	                    ||          \  }}|t          |          D ]\  }}|d         }|ddddf         |ddddf         k    }	|	                                rjt          j        |	                    d                    d         d         }
||
                                         }t          j        dd| d| d	           |                     |j                  }t          |t          j                  rt!          d|fg          }|                     |||          \  }}|                     |||j        |          \  }}| j	                            ||j        |          }i }|                    |           |                    |           t          j                                        r&| j        st3          j        d           d| _        ||fS |                     ||          S )a  
        Args:
            images (list[Tensor]): images to be processed
            targets (list[dict[str, tensor]]): ground-truth boxes present in the image (optional)

        Returns:
            result (list[BoxList] or dict[Tensor]): the output from the model.
                During training, it returns a dict[Tensor] which contains the losses.
                During testing, it returns list[BoxList] contains additional fields
                like `scores`, `labels` and `mask` (for Mask R-CNN models).

        NFz0targets should not be none when in training modeboxes      z:Expected target boxes to be a tensor of shape [N, 4], got .z0Expected target boxes to be of type Tensor, got zJexpecting the last two dimensions of the Tensor to be H and W instead got r      )dimzLAll bounding boxes should have positive height and width. Found invalid box z for target at index 0z=RCNN always returns a (Losses, Detections) tuple in scriptingT)r   torch_assert
isinstanceTensorlenshapetypeappendr   	enumerateanywheretolistr   tensorsr   r   r   image_sizespostprocessupdatejitis_scriptingr   warningswarnr   )r   r   r   targetr!   original_image_sizesimgval
target_idxdegenerate_boxesbb_idxdegen_bbfeatures	proposalsproposal_lossesr   detector_lossesr   s                     r   forwardzGeneralizedRCNN.forward5   s   " = 	e%WXXXX%  F"7OE!%66 	,,1Jek"o6JgY^Ydggg   
 !]tTY{{]]]   
 79 	: 	:C)BCC.CMCAm]`]fgigjgj]kmm   !''QQ(89999..99 &/&8&8  "
Fw#(ABB<5BQB<#? #'')) "[)9)=)=!)=)D)DEEaHKF,1&M,@,@,B,BHM[.6[ [MW[ [ [   ==00h-- 	6"S(O#455H%)XXfh%H%H"	?&*nnXy&J\^e&f&f#
O^//*,@
 

 o&&&o&&&9!!## 	:# (]^^^#' :%%%%fj999r   r   )__name__
__module____qualname____doc__r   Moduler   r*   r:   unuseddictstrr-   listr   r   r   tuplerJ   __classcell__)r   s   @r   r
   r
      s       
 
!)! Y! 9	!
 9! 
! ! ! ! ! !  Y3,-;?S%,EV@W;X	tC%&T#u|2C-D(EE	F    <@P: P:U\"P: $tC$5678P: 
tC%&T#u|2C-D(EE	F	P: P: P: P: P: P: P: P:r   r
   )rN   r<   collectionsr   typingr   r   r*   r   utilsr   rO   r
    r   r   <module>rZ      s      # # # # # # " " " " " " " "        ( ( ( ( ( (v: v: v: v: v:bi v: v: v: v: v:r   