CoolFace
Apppublic

Mjolnir65/FasterRCNN

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
transform.cpython-37.pyc98 linesDownload Raw Back to __pycache__
1B

2��hp/�@sddlZddlmZmZmZmZmZddlZddlZddlm	Z	m3Z4ddlmZej
je5e6d�dd��Zej
je7ed	�d8d��Zde9eeeeee10feeeefee11eeee12ffd�d
d�ZGdd�de	j�Ze13eeeee14d�dd�Ze15eeeee16d�dd�ZdS)�N)�Any�Dict�List�Optional�Tuple)�nn�Tensor�)�	ImageList)�image�returncCsddlm}|�|�dd�S)Nr)�	operators�����)�17torch.onnxr
�shape_as_tensor)rr
�r�7/home/rohit/Desktop/Sem6/CV/q1.2/detection/transform.py�_get_shape_onnxsr)�vrcCs|S)Nr)rrrr�_fake_cast_onnxsr)r�
self_min_size�
self_max_size�target�18fixed_sizercCs�t��rt|�}n.tj��r2t�|jdd��}n|jdd�}d}d}d}|dk	rf|d|dg}n�tj��sxt��r�t�|�j	tj19d�}	t�|�j	tj20d�}21t|�}t|�}t�||	||22�}
t��r�t
|
�}n|
��}n"t|�}	t|�}23t||	||24�}d}tjjj|d||d|dd�d}|dk�r:||fSd	|k�r�|d	}tjjj|dd�df��|||d25�dd�df��}||d	<||fS)Nrr	r)�dtypeT�bilinearF)�size�scale_factor�mode�recompute_scale_factor�
align_corners�masks)rrr)�torchvision�_is_tracingr�torch�jit�is_scripting�tensor�shape�min�to�float32�max�floatr�itemr�26functional�interpolate�byte)rrrrr�im_shaperrr�min_size�max_sizeZself_min_size_fZself_max_size_f�scale�maskrrr�_resize_image_and_maskssP272829303132"r7c33s�eZdZdZd!eeeeeeeeeeefe	d��fdd�
Z34d"eeeeee
efeeeeee
effd�dd	�Zeed35�dd�Zeeed
�dd�Zd#eeee
efeeeee
effd�dd�Zejjd$eeeed�dd��Zeeeeed�dd�Zd%eeeed�dd�Zeee
efeeeefeeeefeee
efd�dd�Ze
d�dd �Z�ZS)&�GeneralizedRCNNTransformah36    Performs input / target transformation before feeding the data to a GeneralizedRCNN37    model.38 39    The transformations it performs are:40        - input normalization (mean subtraction and std division)41        - input / target resizing to match min_size / max_size42 43    It returns a ImageList for the inputs, and a List[Dict[Tensor]] for the targets44    � N)r3r4�45image_mean�	image_std�size_divisibler�kwargscsTt���t|ttf�s|f}||_||_||_||_||_	||_46|�dd�|_dS)N�_skip_resizeF)
�super�__init__�47isinstance�list�tupler3r4r:r;r<r�popr>)�selfr3r4r:r;r<rr=)�	__class__rrr@as48 49z!GeneralizedRCNNTransform.__init__)�images�targetsrcCsTdd�|D�}|dk	rXg}x8|D]0}i}x|��D]\}}|||<q2W|�|�q W|}x�tt|��D]v}||}	|dk	r�||nd}50|	��dkr�td|	j����|�|	�}	|�|	|51�\}	}52|	||<|dk	rf|53dk	rf|54||<qfWdd�|D�}|j	||j55d�}g}x>|D]6}
t�t|
�dkd|
���|�|
d	|
d56f��qWt
||�}||fS)NcSsg|]}|�qSrr)�.0�imgrrr�57<listcomp>ysz4GeneralizedRCNNTransform.forward.<locals>.<listcomp>�zFimages is expected to be a list of 3d tensors of shape [C, H, W], got cSsg|]}|jdd��qS)rN)r()rIrJrrrrK�s)r<�zMInput tensors expected to have in the last two elements H and W, instead got rr	)�items�append�range�len�dim�58ValueErrorr(�	normalize�resize�batch_imagesr<r$�_assertr59)rErGrH�targets_copy�t�data�kr�ir�target_index�image_sizes�image_sizes_list�60image_size�61image_listrrr�forwardvs:6263646566z GeneralizedRCNNTransform.forward)rrcCst|��std|j�d���|j|j}}tj|j||d�}tj|j||d�}||dd�ddf|dd�ddfS)NzOExpected input images to be of floating type (in range [0, 1]), but found type z instead)r�device)�is_floating_point�	TypeErrorrrcr$�	as_tensorr:r;)rErrrc�mean�stdrrrrT�sz"GeneralizedRCNNTransform.normalize)r[rcCs*tt�d��dtt|������}||S)z�67        Implements `random.choice` via torch ops, so it can be compiled with68        TorchScript and we use PyTorch's RNG (not native RNG)69        r	g)�intr$�empty�uniform_r-rQr.)rEr[�indexrrr�torch_choice�s"z%GeneralizedRCNNTransform.torch_choice)rrrcCs�|jdd�\}}|jr4|jr&||fS|�|j�}n70|jd}t|||j||j�\}}|dkrf||fS|d}t|||f|jdd��}||d<d|kr�|d}t	|||f|jdd��}||d<||fS)Nr������boxes�	keypoints)71r(�trainingr>rmr3r7r4r�resize_boxes�resize_keypoints)rErr�h�wr�bboxrprrrrU�s"72zGeneralizedRCNNTransform.resize)rGr<rc73sg}xPt|d���D]<�t�t��fdd�|D���tj���tj�}|�|�qW|}t�	|d�tj�|�|�tj�|d<t�	|d�tj�|�|�tj�|d<t74|�}g}xX|D]P}dd�t|t75|j��D�}tj
j�|d|dd|dd|df�}	|�|	�q�Wt�|�S)Nrcsg|]}|j��qSr)r()rIrJ)r\rrrK�sz?GeneralizedRCNNTransform._onnx_batch_images.<locals>.<listcomp>r	rMcSsg|]\}}||�qSrr)rI�s1�s2rrrrK�s)rPrRr$r,�stackr*r+�int64rO�ceilrC�zipr(rr/�pad)76rErGr<r4�77max_size_i�stride�padded_imgsrJ�padding�78padded_imgr)r\r�_onnx_batch_images�s.**79(z+GeneralizedRCNNTransform._onnx_batch_images)�the_listrcCsJ|d}x<|dd�D],}x&t|�D]\}}t|||�||<q$WqW|S)Nrr	)�	enumerater,)rEr��maxes�sublistrlr.rrr�max_by_axis�s80z$GeneralizedRCNNTransform.max_by_axisc	Cs�t��r|�||�S|�dd�|D��}t|�}t|�}tt�t|d�|�|�|d<tt�t|d�|�|�|d<t	|�g|}|d�81|d�}xRt|jd�D]@}||}||d|jd�d|jd�d|jd�f�
|�q�W|S)NcSsg|]}t|j��qSr)rBr()rIrJrrrrK�sz9GeneralizedRCNNTransform.batch_images.<locals>.<listcomp>r	rMr)r"r#r�r�r-rBri�mathr{rQ�new_fullrPr(�copy_)	rErGr<r4r�batch_shape�batched_imgsr\rJrrrrV�s""8z%GeneralizedRCNNTransform.batch_images)�result�image_shapes�original_image_sizesrcCs�|jr82|Sx�tt|||��D]~\}\}}}|d}t|||�}|||d<d|krr|d}	t|	||�}	|	||d<d|kr|d}83t|84||�}85|86||d<qW|S)Nror!rp)rqr�r|rr�paste_masks_in_imagers)rEr�r�r�r\�pred�im_s�o_im_sror!rprrr�postprocesss z$GeneralizedRCNNTransform.postprocess)rcCsZ|jj�d�}d}||�d|j�d|j�d�7}||�d|j�d|j�d�7}|d	7}|S)87N�(z88    zNormalize(mean=z, std=�)zResize(min_size=z, max_size=z, mode='bilinear')z89))rF�__name__r:r;r3r4)rE�
format_string�_indentrrr�__repr__sz!GeneralizedRCNNTransform.__repr__)r9N)N)N)r9)r9)r��90__module__�__qualname__�__doc__rirr-rrrr@rr�strr91rbrTrmrUr$r%�unusedr�r�rVr�r��
__classcell__rr)rFrr8Us&92"&'"r8)rp�
original_size�new_sizerc	s��fdd�t||�D�}|\}}���}tj��r�|dd�dd�df|}|dd�dd�df|}tj|||dd�dd�dffdd�}n |d|9<|d|9<|S)	Ncs8g|]0\}}tj|tj�jd�tj|tj�jd��qS))rrc)r$r'r+rc)rI�s�s_orig)rprrrK!sz$resize_keypoints.<locals>.<listcomp>rr	rM)rR).r).r	)r|�cloner$�_C�_get_tracing_statery)	rpr�r��ratios�ratio_h�ratio_w�resized_data�resized_data_0�resized_data_1r)rprrss9394(rs)ror�r�rc95sh�fdd�t||�D�}|\}}��d�\}}}}	||}||}||}|	|}	tj||||	fdd�S)Ncs8g|]0\}}tj|tj�jd�tj|tj�jd��qS))rrc)r$r'r+rc)rIr�r�)rorrrK3sz resize_boxes.<locals>.<listcomp>r	)rR)r|�unbindr$ry)96ror�r�r��ratio_height�ratio_width�xmin�ymin�xmax�ymaxr)rorrr1s97rr)NN)r��typingrrrrrr$r"rrrar98r%r�rr-rrir�r7�Moduler8rsrrrrrr�<module>s 	$"7K