o
    *j                     @   s6  d dl mZ d dlmZmZ d dlZd dlZd dl	Z	d dl
m  mZ d dlm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mZ d dl m!Z! e! Z"ej#ej$ej%dG dd deZ&G dd de'Z(G dd de'Z)G dd de'Z*G dd de'Z+G dd de'Z,dS )    N)AnyDict)Image)	Pipelines)resnet26_2p1d)
OutputKeys)InputPipeline)	PIPELINES)Config)	ModelFileTasks)
get_logger)module_namec                       s   e Zd Zdef fddZdedeeef fddZe	
 deeef deeef fdd	Zd
eeef deeef fddZ  ZS )CMDSSLVideoEmbeddingPipelinemodelc                    s   t  jdd|i| t| jtj}td|  t| jtj	}td|  t
|| _tddd| _tj rDtd| _ntd| _| j| j d	| _| jtj|dd
 td dS )z
        use `model` to create a CMDSSL Video Embedding pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   zloading model from zloading config from NT)Znum_classesZ	last_poolcudacpuF)Zweights_onlyzload model done )super__init__ospjoinr   r   ZTORCH_MODEL_FILEloggerinfoZCONFIGURATIONr   	from_filecfgr   torchr   Zis_availableZdevice_devicetoevalZrequires_grad_Zload_state_dictload)selfr   kwargsZ
model_pathZconfig_path	__class__r   x/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/cmdssl_video_embedding_pipeline.pyr      s   
z%CMDSSLVideoEmbeddingPipeline.__init__inputreturnc                    sx  t jd tt| jjjdt| jjj	dt
 t| jjj| jjjdg | jjjd | jjj d }t j|t dd}t||krNtj| jjjtd}n!tdt|| | jjjd }|dd  |d d  d	 t}td|| jjj}|d d d f |d d d f  d}d||t|k< t|| j| jjjdd
} fdd|D }tj |dd
}d|i}|S )Nnativesizemeanstd   r   )ctx)Zdtypeg       @dimc                    s"   g | ]} d d |  D qS )c                 S      g | ]}t |qS r   )r   Z	fromarray).0fr   r   r&   
<listcomp>O       zFCMDSSLVideoEmbeddingPipeline.preprocess.<locals>.<listcomp>.<listcomp>)numpyr5   u
transformsr   r&   r7   N   s    z;CMDSSLVideoEmbeddingPipeline.preprocess.<locals>.<listcomp>
video_data)!decordZbridgeZ
set_bridgeVComposeVRescaler   ZDATAZ
scale_sizeVCenterCropZ	crop_size	VToTensor
VNormalizer-   r.   Zvideo_framesZvideo_strideZVideoReaderr   lennpZzerosZ
multi_cropintZlinspaceZastypeZarangeZreshaper   Z
from_numpyZ	get_batchZasnumpychunkstack)r"   r'   Zclip_lenZvrZinit_framesindicesframesresultr   r<   r&   
preprocess2   sL   
&

z'CMDSSLVideoEmbeddingPipeline.preprocessc                 C   s8   |d  | j}| |}|d}tj|j  iS )Nr>   r   )	r   r   r   r-   r   ZVIDEO_EMBEDDINGdatar   r9   )r"   r'   rK   featurer   r   r&   forwardU   s   

z$CMDSSLVideoEmbeddingPipeline.forwardinputsc                 C   s   |S Nr   )r"   rQ   r   r   r&   postprocess\   s   z(CMDSSLVideoEmbeddingPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   rM   r   Zno_gradrP   rS   __classcell__r   r   r$   r&   r      s    #$*r   c                   @   s   e Zd Zdd Zdd ZdS )r@   c                 C   
   || _ d S rR   r<   )r"   r=   r   r   r&   r   b      
zVCompose.__init__c                 C   s   | j D ]}||}q|S rR   r<   )r"   itemtr   r   r&   __call__e   s   

zVCompose.__call__NrT   rU   rV   r   r]   r   r   r   r&   r@   `   s    r@   c                   @      e Zd ZdddZdd ZdS )rA      c                 C   rY   rR   r*   r"   r+   r   r   r&   r   m   rZ   zVRescale.__init__c                    sX   |d j \}}| j t|| }tt|| tt||   fdd|D }|S )Nr   c                    s   g | ]}|  ftjqS r   )resizer   ZBILINEARr:   Zout_hZout_wr   r&   r7   t   s    z%VRescale.__call__.<locals>.<listcomp>)r+   minrG   round)r"   vclipwhscaler   rc   r&   r]   p   s
   "zVRescale.__call__N)r`   r^   r   r   r   r&   rA   k       
rA   c                   @   r_   )rB   p   c                 C   rY   rR   r*   ra   r   r   r&   r   z   rZ   zVCenterCrop.__init__c                    sX   |d j \}}t|| j ksJ | j  d | j  d  fdd|D }|S )Nr      c              	      s*   g | ]}|  j  j fqS r   )cropr+   r:   r"   x1y1r   r&   r7      s    z(VCenterCrop.__call__.<locals>.<listcomp>)r+   rd   )r"   rf   rg   rh   r   rn   r&   r]   }   s   zVCenterCrop.__call__N)rk   r^   r   r   r   r&   rB   x   rj   rB   c                   @   s   e Zd Zdd ZdS )rC   c                 C   s   t jdd |D dd}|S )Nc                 S   r4   r   )TFZ	to_tensorr:   r   r   r&   r7      r8   z&VToTensor.__call__.<locals>.<listcomp>r/   r2   )r   rI   r"   rf   r   r   r&   r]      s   zVToTensor.__call__N)rT   rU   rV   r]   r   r   r   r&   rC      s    rC   c                   @   s*   e Zd Zg dg dfddZdd ZdS )rD   )g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?c                 C   s   || _ || _d S rR   r,   )r"   r-   r.   r   r   r&   r      s   
zVNormalize.__init__c                 C   s   |  dkr| dk sJ d| }t| jtjs(|| jdddd| _t| j	tjs<|| j	dddd| _	|
| j| j	 |S )Ngg?z vclip values should be in [0, 1]r1   r/   )rd   maxclone
isinstancer-   r   ZTensorZ
new_tensorviewr.   Zsub_Zdiv_rr   r   r   r&   r]      s   zVNormalize.__call__Nr^   r   r   r   r&   rD      s    rD   )-Zos.pathpathr   typingr   r   r?   r9   rF   r   Z!torchvision.transforms.functionalr=   Z
functionalrq   ZPILr   Zmodelscope.metainfor   Z+modelscope.models.cv.cmdssl_video_embeddingr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r	   Zmodelscope.pipelines.builderr
   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r   Zregister_moduleZvideo_embeddingZcmdssl_video_embeddingr   objectr@   rA   rB   rC   rD   r   r   r   r&   <module>   s2   F