o
    ZhZ                      @   s   d dl mZmZmZmZ d dlmZ ddlmZ ddl	m
Z
mZmZ e r)d dlZeeZ	ddeeef d	ed
ee dedeeef f
ddZG dd deZdgZdS )    )ListOptionalTupleUnion)DetrImageProcessorFast   )center_to_corners_format)
TensorTypeis_torch_availableloggingN   
image_sizesizemax_sizemod_sizereturnc                 C   sJ  | \}}d}|dur.t t||f}t t||f}|| | |kr.|| | }tt|}||k rN|}	|durE|durEt|| | }
n:t|| | }
n1||krV||ks^||krd||krd||}
}	n|}
|durw|durwt|| | }	nt|| | }	|durtt|	| }tt|
| }|	| }	|
| }
|
|	fS )a  
    Computes the output image size given the input image size and the desired output size with multiple of divisible_size.

    Args:
        image_size (`Tuple[int, int]`):
            The input image size.
        size (`int`):
            The desired output size.
        max_size (`int`, *optional*):
            The maximum allowed output size.
        mod_size (`int`, *optional*):
            The size to make multiple of mod_size.
    N)	floatminmaxintroundtorch	remainderZtensoritem)r   r   r   r   heightwidthZraw_sizeZmin_original_sizeZmax_original_sizeowZohZow_modZoh_mod r   V/var/www/auris/lib/python3.10/site-packages/transformers/models/yolos/modular_yolos.pyget_size_with_aspect_ratio   s2    r   c                   @   sj   e Zd Zdd Z	ddedeeee f de	fd	d
Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )YolosImageProcessorFastc              	   C   s  t d |j|j}}t|t|krtd|jd dkr#td| }tj	|
|jd dddd	\}}|}tj||jd d
d}	||jd  }
t|}t|d|	dddd}|d\}}tj||||gdd	}||dddddf  }dd t||
|D }|S )a[  
        Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x,
        top_left_y, bottom_right_x, bottom_right_y) format. Only supports PyTorch.

        Args:
            outputs ([`YolosObjectDetectionOutput`]):
                Raw outputs of the model.
            target_sizes (`torch.Tensor` of shape `(batch_size, 2)`):
                Tensor containing the size (height, width) of each image of the batch. For evaluation, this must be the
                original image size (before any data augmentation). For visualization, this should be the image size
                after data augment, but before padding.
        Returns:
            `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
            in the batch as predicted by the model.
        z`post_process` is deprecated and will be removed in v5 of Transformers, please use `post_process_object_detection` instead, with `threshold=0.` for equivalent results.TMake sure that you pass in as many target sizes as the batch dimension of the logits      zTEach element of target_sizes must contain the size (h, w) of each image of the batchr   d   dimfloorZrounding_mode   Nc                 S   s   g | ]\}}}|||d qS )scoreslabelsboxesr   ).0slbr   r   r   
<listcomp>o   s    z8YolosImageProcessorFast.post_process.<locals>.<listcomp>)loggerZwarning_oncelogits
pred_boxeslen
ValueErrorshapesigmoidr   topkviewdivr   gather	unsqueezerepeatunbindstackzip)selfoutputstarget_sizes
out_logitsout_bboxprobtopk_valuestopk_indexesr,   
topk_boxesr-   r.   img_himg_w	scale_fctresultsr   r   r   post_processF   s(   "z$YolosImageProcessorFast.post_process      ?Nr%   	thresholdrF   top_kc              	   C   s  |j |j}}|durt|t|krtd| }||jd d}t||d}t	j
||dd\}	}
|	}t	j|
|jd dd	}|
|jd  }t|}t	|d|dddd
}|durt|tr{t	dd |D }t	dd |D }n|d\}}t	j||||gdd|j}||dddddf  }g }t|||D ] \}}}|||k }|||k }|||k }||||d q|S )a   
        Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x,
        top_left_y, bottom_right_x, bottom_right_y) format. Only supports PyTorch.

        Args:
            outputs ([`YolosObjectDetectionOutput`]):
                Raw outputs of the model.
            threshold (`float`, *optional*):
                Score threshold to keep object detection predictions.
            target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*):
                Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size
                (height, width) of each image in the batch. If left to None, predictions will not be resized.
            top_k (`int`, *optional*, defaults to 100):
                Keep only top k bounding boxes before filtering by thresholding.

        Returns:
            `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image
            in the batch as predicted by the model.
        Nr!   r   r$   r"   r&   r#   r(   r)   r*   c                 S      g | ]}|d  qS )r   r   r/   ir   r   r   r3          zIYolosImageProcessorFast.post_process_object_detection.<locals>.<listcomp>c                 S   rU   )r"   r   rV   r   r   r   r3      rX   r+   )r5   r6   r7   r8   r:   r<   r9   r   r   r   r;   r=   r   r>   r?   r@   
isinstancer   ZTensorrA   rB   toZdevicerC   append)rD   rE   rS   rF   rT   rG   rH   rI   Zk_valuerJ   rK   r,   rL   r-   r.   rM   rN   rO   rP   r0   r1   r2   Zscorelabelboxr   r   r   post_process_object_detections   s:   
z5YolosImageProcessorFast.post_process_object_detectionc                   C      t dNzHSegmentation post-processing is not implemented for Deformable DETR yet.NotImplementedErrorr   r   r   r   post_process_segmentation      z1YolosImageProcessorFast.post_process_segmentationc                   C   r_   )NzDInstance post-processing is not implemented for Deformable DETR yet.ra   r   r   r   r   post_process_instance   rd   z-YolosImageProcessorFast.post_process_instancec                   C   r_   )NzDPanoptic post-processing is not implemented for Deformable DETR yet.ra   r   r   r   r   post_process_panoptic   rd   z-YolosImageProcessorFast.post_process_panopticc                   C   r_   r`   ra   r   r   r   r   "post_process_instance_segmentation   rd   z:YolosImageProcessorFast.post_process_instance_segmentationc                   C   r_   )NzQSemantic segmentation post-processing is not implemented for Deformable DETR yet.ra   r   r   r   r   "post_process_semantic_segmentation   rd   z:YolosImageProcessorFast.post_process_semantic_segmentationc                   C   r_   )NzQPanoptic segmentation post-processing is not implemented for Deformable DETR yet.ra   r   r   r   r   "post_process_panoptic_segmentation   rd   z:YolosImageProcessorFast.post_process_panoptic_segmentation)rR   Nr%   )__name__
__module____qualname__rQ   r   r   r	   r   r   r   r^   rc   re   rf   rg   rh   ri   r   r   r   r   r    E   s     .
;r    )Nr   )typingr   r   r   r   Z3transformers.models.detr.image_processing_detr_fastr   Zimage_transformsr   utilsr	   r
   r   r   Z
get_loggerrj   r4   r   r   r    __all__r   r   r   r   <module>   s*    



1
|