o
    *j                     @   s  d dl 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
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 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S )    )AnyDictN)	rearrange)Image)default_collate)Resize)	Pipelines)StablediffusionPaintbyexample)
OutputKeys)InputPipeline)	PIPELINES)
load_image)Tasks)
get_logger)module_namec                       s   e Zd Zdef fddZdddZd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
dd Zdeeef d
eeef fddZ  ZS )ImagePaintbyexamplePipelinemodelc                    sN   t |ts	J dddlm} t jd|dd| || jj| _d| _dS )z8
            model: model id on modelscope hub.
        zmodel must be a single strr   )PLMSSamplerF)r   Zauto_collateN )	
isinstancestrZpaint_ldm.models.diffusion.plmsr   super__init__r   sampler
start_code)selfr   kwargsr   	__class__r   v/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/image_paintbyexample_pipeline.pyr       s
   
z$ImagePaintbyexamplePipeline.__init__Tc                 C   s<   g }|r|t j g7 }|r|t jddg7 }t j|S )N)      ?r!   r!   torchvisionZ
transformsZToTensorZ	NormalizeZComposer   	normalizeZtoTensorZtransform_listr   r   r    
get_tensor*   s   z&ImagePaintbyexamplePipeline.get_tensorc                 C   s<   g }|r|t j g7 }|r|t jddg7 }t j|S )N)g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?r"   r$   r   r   r    get_tensor_clip6   s   z+ImagePaintbyexamplePipeline.get_tensor_clipinputreturnc           
      C   sd  t |d tr.|d |d |d }}}t|d}t|d}t|dd}n,t |d tjjrV|d dd}|d dd}|d dd}ntd|  |}|	d	}| 
 |}|	d	}t|d
 }d|tjd  }d	||dk < d||dk< t|}|| }i }	|| j|	d< || j|	d< || j|	d< |	S )Nimgmask	reference)   r-   )   r.   LRGBzRinput should be either str or PIL.Image, and both inputs should have the same typer   )NN        o@r!   inpaint_maskinpaint_image
ref_tensor)r   r   r   resizeconvertPILr   	TypeErrorr&   Z	unsqueezer'   nparrayastypefloat32torch
from_numpytoZdevice)
r   r(   Z
image_nameZ	mask_nameref_namer*   refr+   r4   test_model_kwargsr   r   r    
preprocessC   s>   



z&ImagePaintbyexamplePipeline.preprocessc                 C   s   |  |}tj|iS N)perform_inferencer
   Z
OUTPUT_IMG)r   r(   resultr   r   r    forwardc   s   

z#ImagePaintbyexamplePipeline.forwardc                 C   s  t   | jj  |d }| jjj}| jj|t j}| jj|}| jj	|d }| jj
| }||d< t|jd |jd g|d |d< g d}| jjd|d|d	d
|d| j|d
\}}| jj|}	t j|	d d ddd}	|	 dddd }	|	}
t |
ddddd }dt|  d }|tj}t|tj}W d    n1 sw   Y  W d    |S W d    |S 1 sw   Y  |S )Nr5   r4   r3   )   @   rL   2   r1   F   g        )
SZconditioningZ
batch_sizeshapeverboseZunconditional_guidance_scaleZunconditional_conditioningetaZx_TrC   g      ?g       @)minmaxr         r2   zc h w -> h w c)r>   Zno_gradr   Z	ema_scopeZlearnable_vectorZget_learned_conditioningr@   r=   Zproj_outZencode_first_stageZget_first_stage_encodingdetachr   rP   r   sampler   Zdecode_first_stageclampcpuZpermutenumpyr?   r   r<   r:   Zuint8cv2ZcvtColorZCOLOR_RGB2BGR)r   rC   r5   ZuccZ	z_inpaintrP   Zsamples_ddim_Zx_samples_ddimZx_checked_imageZx_checked_image_torchZx_sampler*   r   r   r    rF   g   s   




,
,,z-ImagePaintbyexamplePipeline.perform_inferenceinputsc                 C   s   |S rE   r   )r   r_   r   r   r    postprocess   s   z'ImagePaintbyexamplePipeline.postprocess)TT)__name__
__module____qualname__r   r   r&   r'   r   r   rD   rH   rF   r`   __classcell__r   r   r   r    r      s    


"" */r   )'typingr   r   r\   r[   r:   r8   r>   Ztorch.nnnnr#   Zeinopsr   r   Ztorch.utils.data._utils.collater   Ztorchvision.transformsr   Zmodelscope.metainfor   Z)modelscope.models.cv.image_paintbyexampler	   Zmodelscope.outputsr
   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessors.imager   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   loggerZregister_moduleZimage_paintbyexampler   r   r   r   r    <module>   s0   