o
    *jn                     @   s   d dl Z 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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dS )    N)AnyDict)	Pipelines)	Generator)
OutputKeys)InputPipeline)	PIPELINES)
load_image)	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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 )FaceImageGenerationPipelinemodelc                    s
  t  jdd|i| tj rtd| _ntd| _d| _d| _d| _d| _	d| _
d	| _t| j| j| j| j	d
| j| _| dtj | _| jtj| jddd  td d| _| j
dk rt  | j| j| _W d   dS 1 s|w   Y  dS dS )z
        use `model` to create a face image generation pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   cudacpui   i         gffffff?i   )channel_multiplier/T)Zweights_onlyZg_emazload model doneN    )super__init__torchr   Zis_availabledevicesizelatentZn_mlpr   
truncationZtruncation_meanr   to	generatorr   ZTORCH_MODEL_FILEZ
model_fileZload_state_dictloadloggerinfomean_latentno_grad)selfr   kwargs	__class__r   w/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/face_image_generation_pipeline.pyr      s>   



"z$FaceImageGenerationPipeline.__init__inputreturnc                 C      |S Nr   )r'   r,   r   r   r+   
preprocess=      z&FaceImageGenerationPipeline.preprocessc                 C   s   t |tr	t|}t |tsJ t| tj| tj| | j  t	 B t
d| j| j}| j|g| j| jd\}}|d d }|ddddd}t|   ddd }W d    n1 sow   Y  tj|tjiS )Nr   )r   Ztruncation_latentg      ?r   r      )
isinstancestrintr   Zmanual_seedr   Zmanual_seed_allr!   evalr&   Zrandnr   r    r   r   r%   ZsqueezeZpermuteflipnpZclipfloatr   numpyr   Z
OUTPUT_IMGZastypeZuint8)r'   r,   Zsample_zsample_r   r   r+   forward@   s$   




 z#FaceImageGenerationPipeline.forwardinputsc                 C   r.   r/   r   )r'   r>   r   r   r+   postprocessU   r1   z'FaceImageGenerationPipeline.postprocess)__name__
__module____qualname__r4   r   r   r   r   r0   r=   r?   __classcell__r   r   r)   r+   r      s
    #"*r   )ostypingr   r   Zcv2r:   r8   ZPILr   Zmodelscope.metainfor   Z$modelscope.models.cv.face_generationr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr	   Zmodelscope.preprocessorsr
   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r#   Zregister_moduleZface_image_generationr   r   r   r   r+   <module>   s&   