o
    ॵi                     @   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| 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)num_classes	last_poolcudacpuFzload model done )super__init__ospjoinr   r   TORCH_MODEL_FILEloggerinfoCONFIGURATIONr   	from_filecfgr   torchr   is_availabledevice_devicetoevalrequires_grad_load_state_dictload)selfr   kwargs
model_pathconfig_path	__class__r   k/home/ubuntu/.local/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)dtypeg       @dimc                    s"   g | ]} d d |  D qS )c                 S      g | ]}t |qS r   )r   	fromarray).0fr   r   r0   
<listcomp>O       zFCMDSSLVideoEmbeddingPipeline.preprocess.<locals>.<listcomp>.<listcomp>)numpyrA   u
transformsr   r0   rC   N   s    z;CMDSSLVideoEmbeddingPipeline.preprocess.<locals>.<listcomp>
video_data)!decordbridge
set_bridgeVComposeVRescaler    DATA
scale_sizeVCenterCrop	crop_size	VToTensor
VNormalizer7   r8   video_framesvideo_strideVideoReaderr   lennpzeros
multi_cropintlinspaceastypearangereshaper!   
from_numpy	get_batchasnumpychunkstack)r*   r1   clip_lenvrinit_framesindicesframesresultr   rH   r0   
preprocess2   sL   
&

z'CMDSSLVideoEmbeddingPipeline.preprocessc                 C   s8   |d  | j}| |}|d}tj|j  iS )NrJ   r   )	r%   r$   r   r7   r   VIDEO_EMBEDDINGdatar   rE   )r*   r1   rk   featurer   r   r0   forwardU   s   

z$CMDSSLVideoEmbeddingPipeline.forwardinputsc                 C   s   |S Nr   )r*   rr   r   r   r0   postprocess\   s   z(CMDSSLVideoEmbeddingPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   rm   r!   no_gradrq   rt   __classcell__r   r   r.   r0   r      s    #$*r   c                   @   s   e Zd Zdd Zdd ZdS )rN   c                 C   
   || _ d S rs   rH   )r*   rI   r   r   r0   r   b      
zVCompose.__init__c                 C   s   | j D ]}||}q|S rs   rH   )r*   itemtr   r   r0   __call__e   s   

zVCompose.__call__Nru   rv   rw   r   r   r   r   r   r0   rN   `   s    rN   c                   @      e Zd ZdddZdd ZdS )rO      c                 C   r{   rs   r4   r*   r5   r   r   r0   r   m   r|   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   BILINEARrF   out_hout_wr   r0   rC   t   s    z%VRescale.__call__.<locals>.<listcomp>)r5   minr]   round)r*   vclipwhscaler   r   r0   r   p   s
   "zVRescale.__call__N)r   r   r   r   r   r0   rO   k       
rO   c                   @   r   )rR   p   c                 C   r{   rs   r4   r   r   r   r0   r   z   r|   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   )cropr5   rF   r*   x1y1r   r0   rC      s    z(VCenterCrop.__call__.<locals>.<listcomp>)r5   r   )r*   r   r   r   r   r   r0   r   }   s   zVCenterCrop.__call__N)r   r   r   r   r   r0   rR   x   r   rR   c                   @   s   e Zd Zdd ZdS )rT   c                 C   s   t jdd |D dd}|S )Nc                 S   r?   r   )TF	to_tensorrF   r   r   r0   rC      rD   z&VToTensor.__call__.<locals>.<listcomp>r9   r=   )r!   rf   r*   r   r   r   r0   r      s   zVToTensor.__call__N)ru   rv   rw   r   r   r   r   r0   rT      s    rT   c                   @   s*   e Zd Zg dg dfddZdd ZdS )rU   )g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?c                 C   s   || _ || _d S rs   r6   )r*   r7   r8   r   r   r0   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]r<   r9   )r   maxclone
isinstancer7   r!   Tensor
new_tensorviewr8   sub_div_r   r   r   r0   r      s   zVNormalize.__call__Nr   r   r   r   r0   rU      s    rU   )-os.pathpathr   typingr   r   rK   rE   rZ   r!   !torchvision.transforms.functionalrI   
functionalr   PILr   modelscope.metainfor   +modelscope.models.cv.cmdssl_video_embeddingr   modelscope.outputsr   modelscope.pipelines.baser   r	   modelscope.pipelines.builderr
   modelscope.utils.configr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r   register_modulevideo_embeddingcmdssl_video_embeddingr   objectrN   rO   rR   rT   rU   r   r   r   r0   <module>   s2   F