o
    V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de
de
fd	d
Zejjde
defddZ		dde
dededeeee
f  deeeef  dee
eeee
f  f fddZG dd de	jZde
dee dee de
fddZde
dee dee de
f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)
torch.onnxr   shape_as_tensor)r   r    r   Z/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/models/detection/transform.py_get_shape_onnx   s   r   vc                 C   s   | S Nr   )r   r   r   r   _fake_cast_onnx   s   r   self_min_sizeself_max_sizetarget
fixed_sizec                 C   s  t  r	t| }ntj rt| jdd  }n| jdd  }d }d }d }|d ur3|d |d g}nPtj s<t  rpt|j	tj
d}	t|j	tj
d}
t|}t|}t||	 ||
 }t  rkt|}n| }nt|}	t|}
t||	 ||
 }d}tjjj| d  ||d|ddd } |d u r| |fS d	|v r|d	 }tjjj|d d d f  |||d
d d df  }||d	< | |fS )Nr   r	   r   )dtypeTbilinearF)sizescale_factormoderecompute_scale_factoralign_cornersmasks)r   r   r!   )torchvision_is_tracingr   torchjitis_scriptingtensorshapemintofloat32maxfloatr   itemr   
functionalinterpolatebyte)r   r   r   r   r   im_shaper   r   r!   min_sizemax_sizeself_min_size_fself_max_size_fscalemaskr   r   r   _resize_image_and_masks   s\   



	
r;   c                       s  e Zd ZdZ		d)dededee dee ded	eeeef  d
e	f fddZ
	d*dee deeeeef   deeeeeeef   f fddZdedefddZdee defddZ	d*dedeeeef  deeeeeef  f fddZejjd+dee dedefddZdeee  dee fddZd+dee dedefd d!Zd"eeeef  d#eeeef  d$eeeef  deeeef  fd%d&Zdef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
        Nr5   r6   
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tupler5   r6   r>   r?   r@   r   poprB   )selfr5   r6   r>   r?   r@   r   rA   	__class__r   r   rD   b   s   

z!GeneralizedRCNNTransform.__init__imagestargetsr   c                 C   sB  dd |D }|d ur(g }|D ]}i }|  D ]\}}|||< q|| q|}tt|D ];}|| }	|d ur<|| nd }
|	 dkrLtd|	j | |	}	| |	|
\}	}
|	||< |d uri|
d uri|
||< q.dd |D }| j	|| j
d}g }|D ]}tt|dkd|  ||d	 |d
 f q}t||}||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*   rN   r   r   r   rQ      s    )r@      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_imagesr@   r&   _assertr
   )rI   rL   rM   targets_copytdatakr   ir   target_indeximage_sizesimage_sizes_list
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)is_floating_point	TypeErrorr   rk   r&   	as_tensorr>   r?   )rI   r   r   rk   meanstdr   r   r   r[      s   (z"GeneralizedRCNNTransform.normalizerb   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&   emptyuniform_r/   rX   r0   )rI   rb   indexr   r   r   torch_choice   s   "z%GeneralizedRCNNTransform.torch_choicer   c                 C   s   |j dd  \}}| jr| jr||fS | | j}n| jd }t||| j|| j\}}|d u r3||fS |d }t|||f|j dd  }||d< d|v ra|d }t	|||f|j dd  }||d< ||fS )Nr   boxes	keypoints)
r*   trainingrB   ru   r5   r;   r6   r   resize_boxesresize_keypoints)rI   r   r   hwr   bboxrx   r   r   r   r\      s"   
zGeneralizedRCNNTransform.resizec           
         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 ](}dd t|t
|jD }tjj|d|d d|d d|d f}	||	 q]t|S )Nr   c                    s   g | ]}|j   qS r   rS   rN   rc   r   r   rQ          z?GeneralizedRCNNTransform._onnx_batch_images.<locals>.<listcomp>r	   rT   c                 S   s   g | ]\}}|| qS r   r   )rO   s1s2r   r   r   rQ      s    )rW   rY   r&   r.   stackr,   r-   int64rV   ceilrG   zipr*   r   r1   pad)
rI   rL   r@   r6   
max_size_istridepadded_imgsrP   padding
padded_imgr   r   r   _onnx_batch_images   s   .**(
z+GeneralizedRCNNTransform._onnx_batch_imagesthe_listc                 C   sB   |d }|dd  D ]}t |D ]\}}t|| |||< qq
|S )Nr   r	   )	enumerater.   )rI   r   maxessublistrt   r0   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 | qT|S )Nc                 S   s   g | ]}t |jqS r   )rF   r*   rN   r   r   r   rQ      r   z9GeneralizedRCNNTransform.batch_images.<locals>.<listcomp>r	   rT   r   )r$   r%   r   r   r/   rF   rq   mathr   rX   new_fullrW   r*   copy_)	rI   rL   r@   r6   r   batch_shapebatched_imgsrc   rP   r   r   r   r]      s   ""6z%GeneralizedRCNNTransform.batch_imagesresultimage_shapesoriginal_image_sizesc                 C   s   | j r|S tt|||D ]?\}\}}}|d }t|||}||| d< d|v r8|d }	t|	||}	|	|| d< d|v rL|d }
t|
||}
|
|| d< q|S )Nrw   r#   rx   )ry   r   r   rz   r   r{   )rI   r   r   r   rc   predim_so_im_srw   r#   rx   r   r   r   postprocess  s    z$GeneralizedRCNNTransform.postprocessc                 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
))rK   __name__r>   r?   r5   r6   )rI   format_string_indentr   r   r   __repr__  s   z!GeneralizedRCNNTransform.__repr__)r=   Nr   )r=   )r   
__module____qualname____doc__rq   r   r/   r   r   r   rD   r   r   strr
   ri   r[   ru   r\   r&   r'   unusedr   r   r]   r   r   __classcell__r   r   rJ   r   r<   V   sf    
)

r<   rx   original_sizenew_sizec           	         s    fddt ||D }|\}}  }tj rH|d d d d df | }|d d d d df | }tj|||d d d d df fdd}|S |d  |9  < |d  |9  < |S )	Nc                    8   g | ]\}}t j|t j jd t j|t j jd  qS rj   r&   r)   r-   rk   rO   ss_origrx   r   r   rQ   !      z$resize_keypoints.<locals>.<listcomp>r   r	   rT   rY   ).r   ).r	   )r   cloner&   _C_get_tracing_stater   )	rx   r   r   ratiosratio_hratio_wresized_dataresized_data_0resized_data_1r   r   r   r{      s   

&r{   rw   c           
         sh    fddt ||D }|\}} d\}}}}	|| }|| }|| }|	| }	tj||||	fddS )Nc                    r   r   r   r   rw   r   r   rQ   3  r   z resize_boxes.<locals>.<listcomp>r	   r   )r   unbindr&   r   )
rw   r   r   r   ratio_heightratio_widthxminyminxmaxymaxr   r   r   rz   2  s   
rz   )NN)r   typingr   r   r   r   r   r&   r$   r   r   rh   r
   	roi_headsr   r'   r   r   r/   r   rq   r   r;   Moduler<   r{   rz   r   r   r   r   <module>   s<    	
= "K&