U
    h/                     @   s   d dl Z d dlmZmZmZmZmZ d dlZd dlZd dlm	Z	m
Z
 ddlmZ ddlmZ ejje
e
ddd	Zejje
ed
ddZde
eeeeee
f  eeeef  ee
eeee
f  f dddZG dd de	jZe
ee ee e
dddZe
ee ee e
dddZdS )    N)AnyDictListOptionalTuple)nnTensor   )	ImageList)paste_masks_in_imageimagereturnc                 C   s   ddl m} || dd  S )Nr   )	operators)Z
torch.onnxr   Zshape_as_tensor)r   r    r   X/var/www/html/venv/lib/python3.8/site-packages/torchvision/models/detection/transform.py_get_shape_onnx   s    r   )vr   c                 C   s   | S )Nr   )r   r   r   r   _fake_cast_onnx   s    r   )r   self_min_sizeself_max_sizetarget
fixed_sizer   c                 C   s  t  rt| }n.tj r2t| jdd  }n| jdd  }d }d }d }|d k	rf|d |d g}ntj sxt  rt|j	tj
d}	t|j	tj
d}
t|}t|}t||	 ||
 }t  rt|}n| }n"t|}	t|}
t||	 ||
 }d}tjjj| d  ||d|ddd } |d kr:| |fS d	|kr|d	 }tjjj|d d d f  |||d
d d df  }||d	< | |fS )Nr   r	   r   )dtypeTZbilinearF)sizescale_factormoderecompute_scale_factorZalign_cornersmasks)r   r   r   )torchvision_is_tracingr   torchjitZis_scriptingtensorshapemintofloat32maxfloatr   itemr   
functionalZinterpolatebyte)r   r   r   r   r   Zim_shaper   r   r   min_sizemax_sizeZself_min_size_fZself_max_size_fscalemaskr   r   r   _resize_image_and_masks   s^    



	

   

r2   c                
       s  e Zd ZdZd!eeee ee eeeeef  e	d fddZ
d"ee eeeeef   eeeeeeef   f ddd	Zeed
ddZee edddZd#eeeeef  eeeeeef  f dddZejjd$ee eedddZeee  ee dddZd%ee eedddZeeeef  eeeef  eeeef  eeeef  dddZeddd Z  ZS )&GeneralizedRCNNTransformah  
    Performs input / target transformation before feeding the data to a GeneralizedRCNN
    model.

    The transformations it performs are:
        - input normalization (mean subtraction and std division)
        - input / target resizing to match min_size / max_size

    It returns a ImageList for the inputs, and a List[Dict[Tensor]] for the targets
        N)r.   r/   
image_mean	image_stdsize_divisibler   kwargsc                    sT   t    t|ttfs|f}|| _|| _|| _|| _|| _	|| _
|dd| _d S )N_skip_resizeF)super__init__
isinstancelisttupler.   r/   r5   r6   r7   r   popr9   )selfr.   r/   r5   r6   r7   r   r8   	__class__r   r   r;   b   s    

z!GeneralizedRCNNTransform.__init__)imagestargetsr   c                 C   sB  dd |D }|d k	rPg }|D ],}i }|  D ]\}}|||< q.|| q|}tt|D ]v}|| }	|d k	rx|| nd }
|	 dkrtd|	j | |	}	| |	|
\}	}
|	||< |d k	r\|
d k	r\|
||< q\dd |D }| j	|| j
d}g }|D ]4}tt|dkd|  ||d	 |d
 f qt||}||fS )Nc                 S   s   g | ]}|qS r   r   .0imgr   r   r   
<listcomp>z   s     z4GeneralizedRCNNTransform.forward.<locals>.<listcomp>   zFimages is expected to be a list of 3d tensors of shape [C, H, W], got c                 S   s   g | ]}|j d d qS )r   Nr%   rE   r   r   r   rH      s     )r7      zMInput tensors expected to have in the last two elements H and W, instead got r   r	   )itemsappendrangelendim
ValueErrorr%   	normalizeresizebatch_imagesr7   r"   Z_assertr
   )r@   rC   rD   Ztargets_copytdatakr   ir   Ztarget_indexZimage_sizesZimage_sizes_listZ
image_size
image_listr   r   r   forwardw   s<    




z GeneralizedRCNNTransform.forwardr   c                 C   st   |  std|j d|j|j }}tj| j||d}tj| j||d}||d d d d f  |d d d d f  S )NzOExpected input images to be of floating type (in range [0, 1]), but found type z insteadr   device)Zis_floating_point	TypeErrorr   r\   r"   Z	as_tensorr5   r6   )r@   r   r   r\   ZmeanZstdr   r   r   rR      s    z"GeneralizedRCNNTransform.normalize)rW   r   c                 C   s*   t tddtt| }|| S )z
        Implements `random.choice` via torch ops, so it can be compiled with
        TorchScript and we use PyTorch's RNG (not native RNG)
        r	   g        )intr"   emptyZuniform_r*   rO   r+   )r@   rW   indexr   r   r   torch_choice   s    "z%GeneralizedRCNNTransform.torch_choice)r   r   r   c                 C   s   |j dd  \}}| jr4| jr&||fS | | j}n
| jd }t||| j|| j\}}|d krf||fS |d }t|||f|j dd  }||d< d|kr|d }t	|||f|j dd  }||d< ||fS )Nr   boxes	keypoints)
r%   trainingr9   ra   r.   r2   r/   r   resize_boxesresize_keypoints)r@   r   r   hwr   Zbboxrd   r   r   r   rS      s"    
zGeneralizedRCNNTransform.resize)rC   r7   r   c           
         s  g }t |d  D ]< tt fdd|D tjtj}|| q|}t	|d tj| | tj|d< t	|d tj| | tj|d< t
|}g }|D ]P}dd t|t
|jD }tjj|d|d d|d d|d f}	||	 qt|S )Nr   c                    s   g | ]}|j   qS r   rJ   rE   rX   r   r   rH      s     z?GeneralizedRCNNTransform._onnx_batch_images.<locals>.<listcomp>r	   rK   c                 S   s   g | ]\}}|| qS r   r   )rF   s1s2r   r   r   rH      s     )rN   rP   r"   r)   stackr'   r(   Zint64rM   ceilr>   zipr%   r   r,   pad)
r@   rC   r7   r/   Z
max_size_istrideZpadded_imgsrG   paddingZ
padded_imgr   rj   r   _onnx_batch_images   s    .**(z+GeneralizedRCNNTransform._onnx_batch_images)the_listr   c                 C   sB   |d }|dd  D ](}t |D ]\}}t|| |||< q q|S )Nr   r	   )	enumerater)   )r@   rt   ZmaxesZsublistr`   r+   r   r   r   max_by_axis   s
    z$GeneralizedRCNNTransform.max_by_axisc           	      C   s   t  r| ||S | dd |D }t|}t|}ttt|d | | |d< ttt|d | | |d< t	|g| }|d 
|d}t|jd D ]@}|| }||d |jd d |jd d |jd f | q|S )Nc                 S   s   g | ]}t |jqS r   )r=   r%   rE   r   r   r   rH      s     z9GeneralizedRCNNTransform.batch_images.<locals>.<listcomp>r	   rK   r   )r    r!   rs   rv   r*   r=   r^   mathrn   rO   Znew_fullrN   r%   Zcopy_)	r@   rC   r7   r/   rq   Zbatch_shapeZbatched_imgsrX   rG   r   r   r   rT      s    ""6z%GeneralizedRCNNTransform.batch_images)resultimage_shapesoriginal_image_sizesr   c                 C   s   | j r
|S tt|||D ]~\}\}}}|d }t|||}||| d< d|krp|d }	t|	||}	|	|| d< d|kr|d }
t|
||}
|
|| d< q|S )Nrc   r   rd   )re   ru   ro   rf   r   rg   )r@   rx   ry   rz   rX   predZim_sZo_im_src   r   rd   r   r   r   postprocess  s    z$GeneralizedRCNNTransform.postprocess)r   c                 C   sZ   | j j d}d}|| d| j d| j d7 }|| d| j d| j d7 }|d	7 }|S )
N(z
    zNormalize(mean=z, std=)zResize(min_size=z, max_size=z, mode='bilinear')z
))rB   __name__r5   r6   r.   r/   )r@   format_string_indentr   r   r   __repr__  s    z!GeneralizedRCNNTransform.__repr__)r4   N)N)N)r4   )r4   )r   
__module____qualname____doc__r^   r   r*   r   r   r   r;   r   r   strr
   rZ   rR   ra   rS   r"   r#   unusedrs   rv   rT   r|   r   __classcell__r   r   rA   r   r3   V   sH       ) r3   )rd   original_sizenew_sizer   c           	         s    fddt ||D }|\}}  }tj r|d d d d df | }|d d d d df | }tj|||d d d d df fdd}n |d  |9  < |d  |9  < |S )	Nc                    s8   g | ]0\}}t j|t j jd t j|t j jd  qS r[   r"   r$   r(   r\   rF   sZs_origrd   r   r   rH   !  s   z$resize_keypoints.<locals>.<listcomp>r   r	   rK   rP   ).r   ).r	   )ro   cloner"   Z_CZ_get_tracing_staterm   )	rd   r   r   ratiosZratio_hZratio_wZresized_dataZresized_data_0Zresized_data_1r   r   r   rg      s    

(rg   )rc   r   r   r   c           
         sh    fddt ||D }|\}} d\}}}}	|| }|| }|| }|	| }	tj||||	fddS )Nc                    s8   g | ]0\}}t j|t j jd t j|t j jd  qS r   r   r   rc   r   r   rH   3  s   z resize_boxes.<locals>.<listcomp>r	   r   )ro   Zunbindr"   rm   )
rc   r   r   r   Zratio_heightZratio_widthZxminZyminZxmaxZymaxr   r   r   rf   2  s    
rf   )NN)rw   typingr   r   r   r   r   r"   r    r   r   rY   r
   Z	roi_headsr   r#   r   r   r*   r   r^   r   r2   Moduler3   rg   rf   r   r   r   r   <module>   s0   	  = K