o
    ॵix                     @   s   d dl mZ d dlmZmZmZ d dlZd dlZ	d dl
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 d dlmZ d dlmZ d d	lmZ d d
lmZmZ ejejejdG dd deZdS )    N)AnyDictUnion)Image)	Pipelines)SDAFNet_Tryon)
OutputKeys)Pipeline)	PIPELINES)
load_image)	ModelFileTasks)module_namec                       s   e Zd Zdef fddZdeeeef 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
deeef deeef fddZ  ZS )VirtualTryonPipelinemodelc                    s   t  j|d ttj rdnd| _dd   fdd}| j| _tt	
| jtjd}tdd	| j| _|| j| | j  d
| _ddlm} ||j| jdd| |ddg| _dS )z
        use `model` to create a virtual tryon pipeline for prediction
        Args:
            model: model id on modelscope hub.
        )r   cudacpuc                 S   s   g }|   D ]:\}}d|dd kr|dd  }dt| d vr(|dd}||v r@|| j|jkr@|| | || qd S )Nzmodule.r      z.module..)itemslistkeysreplaceshapecopy_append)
src_params	own_statecopied_keysnameparam r!   c/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/virtual_try_on_pipeline.pyfilter_param#   s   
z3VirtualTryonPipeline.__init__.<locals>.filter_paramc                    s0   d|v r|d }|   } || | | d S )N
state_dict)r$   load_state_dict)r   r   r   r#   r!   r"   load_pretrained/   s
   
z6VirtualTryonPipeline.__init__.<locals>.load_pretrained   )ref_in_channel   r   )
transforms   )interpolation)      ?r.   r.   N)super__init__torchdevicer   is_availabler   
local_pathloadospjoinr   TORCH_MODEL_FILEr   toevalsizetorchvisionr+   ComposeResizeToTensor	Normalizetest_transforms)selfr   kwargsr'   r   r+   	__class__r&   r"   r0      s(   

zVirtualTryonPipeline.__init__inputreturnc           	      C   s  t |trd}d}d}nd}d}d}t || tr,t|| }t|| }t|| }nt || tjjrK|| d}|| d}|| d}nyt || tjrt	|j
dkrut|| tj}t|| tj}t|| tj}t|d d d d d d df d	d}t|d d d d d d df d	d}t|d d d d d d df d	d}n	td
t| | |}| |}| |}|d|d|dd}|S )Nr      r,   masked_modelposeclothRGBuint8z9input should be either str, PIL.Image, np.array, but got )rI   rJ   rK   )
isinstancetuplestrr   PILr   convertnpndarraylenr   cv2cvtColorCOLOR_GRAY2BGR	fromarrayastype	TypeErrortyperA   	unsqueeze)	rB   rF   index_model
index_poseindex_clothimg_agnosticrJ   	cloth_imginputsr!   r!   r"   
preprocessD   sd   


   


zVirtualTryonPipeline.preprocessrd   c                 C   sZ   |d  | j}|d  | j}|d  | j}tj||fdd}| |||}tj|iS )NrI   rJ   rK   rH   )dim)r9   r2   r1   catr   r   
OUTPUT_IMG)rB   rd   rb   rJ   rc   	ref_inputtryon_resultr!   r!   r"   forwardq   s   
zVirtualTryonPipeline.forwardoutputsc                 C   sJ   |t j ddddd}|dddj 	 }||t j< |S )Nr   r,      rH   g      ?g       @   )
r   rh   permutesqueezeadddivmuldatar   numpy)rB   rl   rj   r!   r!   r"   postprocessz   s    
z VirtualTryonPipeline.postprocess)__name__
__module____qualname__rQ   r0   r   r   r   rP   re   rk   rv   __classcell__r!   r!   rD   r"   r      s    +

"-*	r   )os.pathpathr6   typingr   r   r   rW   ru   rT   rR   r1   r   modelscope.metainfor   !modelscope.models.cv.virual_tryonr   modelscope.outputsr   modelscope.pipelines.baser	   modelscope.pipelines.builderr
   modelscope.preprocessorsr   modelscope.utils.constantr   r   register_modulevirtual_try_onr   r!   r!   r!   r"   <module>   s$   