U
    ­Ê¹j‡  ã                   @   sn   d Z ddlZddlmZ ddlmZmZmZmZm	Z	 ddl
Z
ddl
mZ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)ÚDictÚListÚOptionalÚTupleÚUnion)ÚnnÚTensoré   )Ú_log_api_usage_oncec                       sP   e Zd ZdZejejejejddœ‡ fdd„Zejj	dd„ ƒZ
d
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
    N)ÚbackboneÚrpnÚ	roi_headsÚ	transformÚreturnc                    s4   t ƒ  ¡  t| ƒ || _|| _|| _|| _d| _d S )NF)ÚsuperÚ__init__r   r   r   r   r   Ú_has_warned)Úselfr   r   r   r   ©Ú	__class__© úl/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/detection/generalized_rcnn.pyr      s    
zGeneralizedRCNN.__init__c                 C   s   | j r
|S |S )N)Útraining)r   ÚlossesÚ
detectionsr   r   r   Úeager_outputs&   s    zGeneralizedRCNN.eager_outputsc              	   C   s2  | j r‚|dkrt dd¡ nf|D ]`}|d }t|tjƒrht t|jƒdkoV|jd dkd|j› d	�¡ q t dd
t|ƒ› d	�¡ q g }|D ]L}|jdd… }t t|ƒdkd|jdd… › �¡ | |d |d f¡ qŠ|  	||¡\}}|dk	�r|t
|ƒD ]€\}}|d }|dd…dd…f |dd…dd…f k}	|	 ¡ rút |	jdd�¡d d }
||
  ¡ }t dd|› d|› d	�¡ qú|  |j¡}t|tjƒ�r¤td|fgƒ}|  |||¡\}}|  |||j|¡\}}| j	 ||j|¡}i }| |¡ | |¡ tj ¡ �r"| j�st d¡ d| _||fS |  ||¡S d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Ú
isinstancer	   ÚlenÚshapeÚtypeÚappendr   Ú	enumerateÚanyÚwhereÚtolistr   Ztensorsr   r   r   Zimage_sizesÚpostprocessÚupdateÚjitZis_scriptingr   ÚwarningsÚwarnr   )r   ZimagesÚtargetsÚ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   Úforward.   s\    þ
þ
(þ


zGeneralizedRCNN.forward)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚModuler   r'   r3   Zunusedr   r:   Ú__classcell__r   r   r   r   r      s
   "

r   )r>   r4   Úcollectionsr   Útypingr   r   r   r   r   r'   r   r	   Úutilsr   r?   r   r   r   r   r   Ú<module>   s   