Team Ai
Apppublic

xdecoder/Instruct-X-Decoder

sourceHugging Faceafl-3.0updated 3y agoView on Hugging Face
163likes
postprocessing.cpython-38.pyc60 linesDownload Raw Back to __pycache__
1U

2E$�c$�@sNddlZddlmZddlmZmZdeeeed�dd�Z	dd	�Z3d4d�ZdS)
�N)�5functional)�	Instances�ROIMasks��?)�results�
output_height�output_width�mask_thresholdcCsnt|tj�r,|��}|��}t�||g�}n||f}|}|}||jd||jd}}t|f|���}|�d�r||j	}	n|�d�r�|j6}	nd}	|	dk	s�td��|	�||�|	�
|j�||	��}|�d��rt|jt�r�|j}7n"t|jdd�ddd�dd�f�}8|9�|j	|||�j|_|�d��rj|jdd�dd�df|9<|jdd�dd�df|9<|S)	a�10    Resize the output instances.11    The input images are often resized when entering an object detector.12    As a result, we often need the outputs of the detector in a different13    resolution from its inputs.14 15    This function will resize the raw outputs of an R-CNN detector16    to produce outputs according to the desired output resolution.17 18    Args:19        results (Instances): the raw outputs from the detector.20            `results.image_size` contains the input image resolution the detector sees.21            This object might be modified in-place.22        output_height, output_width: the desired output resolution.23 24    Returns:25        Instances: the resized output from the model, based on the output resolution26    �r�27pred_boxes�proposal_boxesNzPredictions must contain boxes!�28pred_masks�pred_keypoints)�29isinstance�torch�Tensor�float�stack�30image_sizer�31get_fields�hasrr�AssertionError�scale�clip�nonemptyr
r�to_bitmasks�tensorr)rrrr	�output_width_tmp�output_height_tmp�new_size�scale_x�scale_y�output_boxes�	roi_masks�r$�8/data/arXiv/demo/Demo/xdecoder/modules/postprocessing.py�detector_postprocess	sD�3233"�  r&c
Cs~|dkrdSt�|d|d|d|dg�ddd�f�|j�}|��|}|dd�df|dd�dfd|dd�df|dd�dfd|dd�df|dd�dfd|dd�df|dd�dfdf\}}}}	|\}34}|jd|d�}|jd|35d�}|jd|d�}|	jd|36d�}	t�||||	g��dd�}t�||||37||||38g�ddd�f�|j�}||}|S)zM39    result: [xc,yc,w,h] range [0,1] to [x1,y1,x2,y2] range [0,w], [0,h]40    Nr41r��)�min�max)rr�to�device�sigmoid�clampr�permute)
�result�42input_size�img_sizerrr�x1�y1�x2�y2�h�w�boxr$r$r%�bbox_postprocessMs6�6r:cCsL|dd�d|d�d|d�f�dddd�}tj|||fddd�d}|S)ax43    Return semantic segmentation predictions in the original resolution.44 45    The input images are often resized when entering semantic segmentor. Moreover, in same46    cases, they also padded inside segmentor to be divisible by maximum network stride.47    As a result, we often need the predictions of the segmentor in a different48    resolution from its inputs.49 50    Args:51        result (Tensor): semantic segmentation prediction logits. A tensor of shape (C, H, W),52            where C is the number of classes, and H, W are the height and width of the prediction.53        img_size (tuple): image size that segmentor is taking as input.54        output_height, output_width: the desired output resolution.55 56    Returns:57        semantic segmentation prediction (Tensor): A tensor of the shape58            (C, output_height, output_width) that contains per-pixel soft predictions.59    Nrr60������bilinearF)�size�mode�
align_corners)�expand�F�interpolate)r0r2rrr$r$r%�sem_seg_postprocesscs.��rC)r)r�torch.nnrrA�detectron2.structuresrr�intrr&r:rCr$r$r$r%�<module>s��D