o
    ॵiN                     @   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d  td d| _| j
dk rt  | j| j| _W d   dS 1 szw   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/g_emazload model doneN    )super__init__torchr   is_availabledevicesizelatentn_mlpr   
truncationtruncation_meanr   to	generatorr   TORCH_MODEL_FILE
model_fileload_state_dictloadloggerinfomean_latentno_grad)selfr   kwargs	__class__r   j/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/face_image_generation_pipeline.pyr      s:   



"z$FaceImageGenerationPipeline.__init__inputreturnc                 C      |S Nr   )r.   r3   r   r   r2   
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"   truncation_latentg      ?r   r      )
isinstancestrintr   manual_seedr   manual_seed_allr%   evalr-   randnr    r$   r   r"   r,   squeezepermuteflipnpclipfloatr   numpyr   
OUTPUT_IMGastypeuint8)r.   r3   sample_zsample_r   r   r2   forward?   s$   




 z#FaceImageGenerationPipeline.forwardinputsc                 C   r5   r6   r   )r.   rP   r   r   r2   postprocessT   r8   z'FaceImageGenerationPipeline.postprocess)__name__
__module____qualname__r<   r   r   r   r   r7   rO   rQ   __classcell__r   r   r0   r2   r      s
    ""*r   )ostypingr   r   cv2rH   rE   PILr   modelscope.metainfor   $modelscope.models.cv.face_generationr   modelscope.outputsr   modelscope.pipelines.baser   r   modelscope.pipelines.builderr	   modelscope.preprocessorsr
   modelscope.utils.constantr   r   modelscope.utils.loggerr   r*   register_moduleface_image_generationr   r   r   r   r2   <module>   s&   