o
    )j!                     @   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
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 d d	lmZ d d
lmZ d dlmZmZ dgZejej ej dG dd deZ!dS )    N)AnyDict)disable_verbosityenable_sliced_attention)create_modelload_state_dict)DDIMSampler)Models)
TorchModel)MODELS)compatible_position_ids)Config)	ModelFileTasks
ControlNet)module_namec                       s\   e Zd Zdef fddZdd Zdd Zdd	 Zd
eee	f deee	f fddZ
  ZS )r   	model_dirc                    s  t  j|g|R i | || _ttj| jtj	| _
| j
jdd}| j
jj| _| j
j| _t  |r8t  |dd}|dkrG| jj}nP|dkrP| jj}nG|dkrY| jj}n>|dkrb| jj}n5|dkrk| jj}n,|d	krt| jj}n#|d
kr}| jj}n|dkr| jj}n|dkr| jj}ntd | jj}|| _|| _tj| j|j}tj| j|j}|dd}	|	dkrd}	t| }
t||	d}t |d |
| |
!|	| _"t#| j"| _$dS )aJ  initialize ControlNet from theq `model_dir` path.
        ControlNet:
            Adding Conditional Control to Text-to-Image Diffusion Models.
            Paper: https://arxiv.org/abs/2302.05543
            Origin codes: https://github.com/lllyasviel/ControlNet
        Args:
            model_dir (str): the model path.
        r   TZcontrol_typehedscribblecannyhoughdepthnormalposesegfake_scribblez&Error input type, use HED for default!devicecudaZgpu)locationz?cond_stage_model.transformer.text_model.embeddings.position_idsN)%super__init__r   r   	from_fileospathjoinr   ZCONFIGURATIONconfigmodelsettinggetimage_resolutionr   r   r   r   r   r   r   r   r   r   printinit_control_typeinput_setting	yaml_path	ckpt_pathr   cpur   r   tomodelr   ddim_sampler)selfr   argskwargsZ	enable_sar*   r+   r,   r-   r   r0   Z
state_dict	__class__ ~/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/cv/controllable_image_generation/controlnet.pyr       sd   	











zControlNet.__init__c                 C      | j S N)r(   r2   r7   r7   r8   get_resolution]      zControlNet.get_resolutionc                 C   r9   r:   )r&   r;   r7   r7   r8   
get_config`   r=   zControlNet.get_configc                 C   r9   r:   )r   r;   r7   r7   r8   get_model_dirc   r=   zControlNet.get_model_dirinputsreturnc                    s(  |d }|d }|d }| dd}| d| jj}| d| jj}| d| jj}| d	| jj}	| d
| jj}
| d| jj}| d| jj}| d| jj	t
d| d d t
d| d| d| d|	  t
d|
 d| d t  |j\}}}t|   d  tj fddt|D dd t d  |r| jjdd  g| j|d |
 g| gd }|rt n g| j|g| gd }d!|d" |d" f}|r| jjd#d |rfd$dtd%D ngd% | j_| jj||||d|	||d&\}}|r| jjdd | j|td'd( d(    !dd)"t#j$fd*dt|D }W d+   n	1 sJw   Y  | j%d,krdt&j'|t#j(d-t#j$d.d/d0}n(| j%d1kry|d+d+d+d+d+d+d2f }n| j%d3ks| j%d4krd)| }n|}|||d5 d6S )7a  return the result by the model
        Args:
            inputs (Dict[str, Any]) should contains the keys:
            - "image", a numpy array
            - "prompt", string of prompt
            - "detected_map", a numpy array of detected map
            - "save_memory", boolean indicating whether to save memory
            - "is_cat_img", boolean indicating whether to concatenate results

            inputs (Dict[str, Any]) can also contains the keys, but not required:
            - "image_resolution", int
            - "strength", float
            - "guess_mode", bool
            - "ddim_steps", int
            - "scale", float
            - "num_samples", int
            - "eta", float
            - "a_prompt", string of added prompt
            - "n_prompt", string of negative prompt

        Returns:
            Dict[str, Any]: A dict contains result, detected_map and boolean 'is_cat_img'
            indicating whether to concatenate the result and the detected_map.

        imagepromptdetected_mapsave_memoryFnum_samplesscale
ddim_stepsetaa_promptn_prompt
guess_modestrengthzProcess with guess_mode:z
,strength:,znum_samples:z,scale:z,ddim_steps:z,eta:z
a_prompt:'z',n_prompt:'z',g     o@c                    s   g | ]} qS r7   r7   ).0_)controlr7   r8   
<listcomp>   s    z&ControlNet.forward.<locals>.<listcomp>r   )dimzb h w c -> b c h w)Zis_diffusingz, )Zc_concatZc_crossattn      Tc                    s    g | ]} d t d|   qS )gffffff?   )floatrO   i)rM   r7   r8   rR      s       )verboserI   Zunconditional_guidance_scaleZunconditional_conditioningzb c h w -> b h w cg     _@   c                    s   g | ]} | qS r7   r7   rX   )	x_samplesr7   r8   rR      s    Nr   )   r^   )shapeZdtype   )Z
iterationsr   r   r   
is_cat_img)resultrD   rb   ))r'   r+   rF   rG   rH   rI   rJ   rK   rL   rM   r)   torchZno_gradr_   Z
from_numpycopyrW   r   stackrangeeinopsZ	rearrangecloner0   Zlow_vram_shiftZget_learned_conditioningZ
zeros_likeZcontrol_scalesr1   sampleZdecode_first_stager.   numpyZclipZastypenpZuint8r*   cv2ZdilateZones)r2   r@   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   HWCZcondZun_condr_   ZsamplesZintermediatesresultsZshow_det_mapr7   )rQ   rM   r]   r8   forwardf   s   

 	


6
zControlNet.forward)__name__
__module____qualname__strr    r<   r>   r?   r   r   rr   __classcell__r7   r7   r5   r8   r      s    >*)"r"   typingr   r   rm   rh   rk   rl   rd   Zcontrol_ldm.cldm.hackr   r   Zcontrol_ldm.cldm.modelr   r   Z%control_ldm.ldm.models.diffusion.ddimr   Zmodelscope.metainfor	   Z'modelscope.models.base.base_torch_modelr
   Zmodelscope.models.builderr   Z-modelscope.utils.compatible_with_transformersr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   __all__Zregister_moduleZcontrollable_image_generationr   r7   r7   r7   r8   <module>   s*   