o
    *j                     @   s   d dl mZmZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ d d	lmZ e Zejejejd
G dd deZdd Zdd ZG dd deZdd ZdddZG dd dejZ dS )    )AnyDictN)nn)	Pipelines)InputPipeline)	PIPELINES)	LoadImage)Tasks)
get_logger)module_namec                       s\   e Zd Zdef fddZdefddZdeeef fddZ	deeef fd	d
Z
  ZS )VidtPipelinemodelc                    sj   t  jd
d|i| | j  ttddgt tjg dg ddg| _	t
 | _ddd| _d	S )a  
        use `model` to create a vidt pipeline for prediction
        Args:
            model: model id on modelscope hub.
        Example:
            >>> from modelscope.pipelines import pipeline
            >>> vidt_pipeline = pipeline('image-object-detection', 'damo/ViDT-logo-detection')
            >>> result = vidt_pipeline(
                'data/test/images/vidt_test1.png')
            >>> print(f'Output: {result}.')
        r   i  )g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?)meanZstdnegativeZpositive)r      N )super__init__r   eval
transformsZComposeZResizeZToTensorZ	Normalize	transformPostProcesspostprocessors	label_dic)selfr   kwargs	__class__r   f/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/vidt_pipeline.pyr      s   
zVidtPipeline.__init__inputsc           
      K   s~   t |}|jd |jd g}| |}|g}|g}t|| j}t|}|| j}i }	|j	|	d< |j
|	d< ||	d< |	S )Nr   r   tensorsmaskorig_target_sizes)r	   Zconvert_to_imgsizer   torchtensortodevicenested_tensor_from_tensor_listr!   r"   )
r   r    Zpreprocess_paramsimgZori_sizeimagetensor_listr#   Zsamplesresr   r   r   
preprocess.   s   



zVidtPipeline.preprocessc           	      K   st   |d }|d }|d }t    | ||\}}i }||d< ||d< ||d< |W  d    S 1 s3w   Y  d S )Nr!   r"   r#   out_pred_logitsout_pred_boxes)r%   no_gradr   )	r   r    Zforward_paramsr!   r"   r#   r/   r0   r-   r   r   r   forward=   s   
$zVidtPipeline.forwardc           
      K   s   |  |d |d |d }t|d }g }g }g }|D ]}||d  || j|d   ||d  qi }	||	d< ||	d< ||	d	< |	S )
Nr/   r0   r#   r   r      scoreslabelsboxes)r   get_predictionsappendr   )
r   r    Zpost_paramsresultsZbatch_predictionsr4   r5   r6   Zsub_preZoutputsr   r   r   postprocessI   s"   
zVidtPipeline.postprocess)__name__
__module____qualname__strr   r   r.   r   r   r2   r:   __classcell__r   r   r   r   r      s
    r   c                 C   s   t dd | D }t| g| }|\}}}}| d j}| d j}tj|||d}	tj|||ftj|d}
t| |	|
D ].\}}}|d |j	d d |j	d d |j	d f 
| d|d |j	d d |j	d f< q:t|	|
S )Nc                 S   s   g | ]}t |jqS r   )listshape).0r*   r   r   r   
<listcomp>^   s    z2nested_tensor_from_tensor_list.<locals>.<listcomp>r   )dtyper(   r   r3   F)_max_by_axislenrD   r(   r%   ZzerosZonesboolziprA   Zcopy_NestedTensor)r,   max_sizeZbatch_shapebchwrD   r(   r&   r"   r*   Zpad_imgmr   r   r   r)   \   s   

2"
r)   c                 C   sB   | d }| dd  D ]}t |D ]\}}t|| |||< qq
|S )Nr   r   )	enumeratemax)Zthe_listZmaxesZsublistindexitemr   r   r   rE   k   s   rE   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
rI   c                 C   s   || _ || _d S Nr!   r"   )r   r!   r"   r   r   r   r   v   s   
zNestedTensor.__init__c                 C   s@   | j |}| j}|d ur|d usJ ||}nd }t||S rT   )r!   r'   r"   rI   )r   r(   Zcast_tensorr"   Z	cast_maskr   r   r   r'   z   s   
zNestedTensor.toc                 C   s   | j | jfS rT   rU   r   r   r   r   	decompose   s   zNestedTensor.decomposec                 C   s
   t | jS rT   )r>   r!   rV   r   r   r   __repr__   s   
zNestedTensor.__repr__N)r;   r<   r=   r   r'   rW   rX   r   r   r   r   rI   t   s
    rI   c                 C   sL   |  d\}}}}|d|  |d|  |d|  |d|  g}tj|ddS )Ng      ?dim)unbindr%   stack)xZx_cZy_crN   rM   rK   r   r   r   box_cxcywh_to_xyxy   s   ,r_   皙?c           
      C   s   g }| D ]H}g }t t|d D ]6}t|d |  }t|d |  }g }|d |  D ]	}	|t|	 q0||krF||||g q|| q|S )Nr4   r5   r6   )rangerF   floatcpuintr8   )
Zpost_resultsZbbox_thuZbatch_final_resZper_img_resZper_img_final_resiZscorelabelZbboxitr   r   r   r7      s   r7   c                       s2   e Zd ZdZd fdd	Ze dd Z  ZS )r   zQ This module converts the model's output into the format expected by the coco apiNc                    s   t    || _d S rT   )r   r   processor_dct)r   rh   r   r   r   r      s   

zPostProcess.__init__c              	   C   s   t |t |ks
J |jd dksJ | }tj||jd dddd\}}|}||jd  }||jd  }	t|}
t|
d|d	ddd}
|
d\}}tj||||gddtj}|
|dddddf  }
d	d
 t||	|
D }|S )a   Perform the computation

        Parameters:
            out_logits: raw logits outputs of the model
            out_bbox: raw bbox outputs of the model
            target_sizes: tensor of dimension [batch_size x 2] containing the size of each images 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
        r   r3   r   rY   d   rZ      Nc                 S   s   g | ]\}}}|||d qS ))r4   r5   r6   r   )rB   slrK   r   r   r   rC      s    
z'PostProcess.forward.<locals>.<listcomp>)rF   rA   Zsigmoidr%   Ztopkviewr_   ZgatherZ	unsqueezerepeatr\   r]   r'   Zfloat32rH   )r   Z
out_logitsZout_bboxZtarget_sizesZprobZtopk_valuesZtopk_indexesr4   Z
topk_boxesr5   r6   Zimg_hZimg_wZ	scale_fctr9   r   r   r   r2      s0   

zPostProcess.forwardrT   )	r;   r<   r=   __doc__r   r%   r1   r2   r?   r   r   r   r   r      s
    r   )r`   )!typingr   r   r%   Ztorchvision.transformsr   r   Zmodelscope.metainfor   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr	   Zmodelscope.utils.constantr
   Zmodelscope.utils.loggerr   loggerZregister_moduleZimage_object_detectionZvidtr   r)   rE   objectrI   r_   r7   Moduler   r   r   r   r   <module>   s*   H	
