o
    hA                     @   s^   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	 d	ejZdS )
z,
Implements the Generalized R-CNN framework
    N)OrderedDict)OptionalUnion)nn   )_log_api_usage_oncec                       s   e Zd ZdZdejdejdejdejddf
 fd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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dZ  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                    s4   t    t|  || _|| _|| _|| _d| _d S )NF)super__init__r   r   r	   r
   r   _has_warned)selfr	   r
   r   r   	__class__ s/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torchvision/models/detection/generalized_rcnn.pyr      s   

zGeneralizedRCNN.__init__losses
detectionsc                 C   s   | j r|S |S N)training)r   r   r   r   r   r   eager_outputs,   s   zGeneralizedRCNN.eager_outputsimagestargetsc              	   C   s*  | j rA|du rtdd n3|D ]0}|d }t|tjr4tt|jdko+|jd dkd|j d	 qtdd
t| d	 qg }|D ]&}|jdd }tt|dkd|jdd   ||d |d f qE| 	||\}}|durt
|D ]@\}}|d }|ddddf |ddddf k}	|	 rt|	jddd d }
||
  }tdd| d| d	 q|| |j}t|tjrtd|fg}| |||\}}| |||j|\}}| j	||j|}i }|| || tj r| jst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   torchZ_assert
isinstanceTensorlenshapetypeappendr   	enumerateanywheretolistr	   Ztensorsr   r
   r   Zimage_sizespostprocessupdatejitZis_scriptingr   warningswarnr   )r   r   r   targetr   Zoriginal_image_sizesZimgvalZ
target_idxZdegenerate_boxesZbb_idxZdegen_bbfeaturesZ	proposalsZproposal_lossesr   Zdetector_lossesr   r   r   r   forward5   sp   
(


zGeneralizedRCNN.forwardr   )__name__
__module____qualname____doc__r   Moduler   r&   r3   Zunuseddictstrr(   listr   r   r   tupler9   __classcell__r   r   r   r   r      s:    ""r   )r=   r4   collectionsr   typingr   r   r&   r   utilsr   r>   r   r   r   r   r   <module>   s    