a
    
d.                     @   s   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dZdddZdddZdS )    N)nn)deepcopy)load_file_from_url)download_pretrained_models)Conv   )
RetinaFace)YoloDetectorFcudac                 C   s<   d| v rt | ||}n"d| v r*t| |}nt|  d|S )N
retinafaceZYOLOv5 is not implemented.)init_retinaface_modelinit_yolov5face_modelNotImplementedError)
model_namehalfdevicemodel r   Z/var/www/html/stable-diffusion-webui/repositories/CodeFormer/facelib/detection/__init__.pyinit_detection_model   s    r   c           	      C   s   | dkrt d|d}d}n(| dkr4t d|d}d}nt|  dt|d	d
d d}tj|dd d}t| D ],\}}|drp|||dd  < || qp|j	|d
d |
  ||}|S )Nretinaface_resnet50resnet50)Znetwork_namer   zYhttps://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pthzretinaface_mobile0.25z
mobile0.25z^https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_mobilenet0.25_Final.pthr   weights/facelibTurl	model_dirprogress	file_namec                 S   s   | S Nr   storagelocr   r   r   <lambda>$       z'init_retinaface_model.<locals>.<lambda>map_locationzmodule.   strict)r   r   r   torchloadr   items
startswithpopload_state_dictevalto)	r   r   r   r   	model_url
model_pathload_netkvr   r   r   r      s"    

r   c                 C   s   | dkrt d|d}d}n(| dkr4t d|d}d}nt|  dt|d	d
d d}tj|dd d}|jj|d
d |j  |j|	 |_|j
 D ]>}t|tjtjtjtjtjfv rd
|_qt|trt |_q|S )NZYOLOv5lz0facelib/detection/yolov5face/models/yolov5l.yaml)config_namer   zNhttps://github.com/sczhou/CodeFormer/releases/download/v0.1.0/yolov5l-face.pthZYOLOv5nz0facelib/detection/yolov5face/models/yolov5n.yamlzNhttps://github.com/sczhou/CodeFormer/releases/download/v0.1.0/yolov5n-face.pthr   r   Tr   c                 S   s   | S r   r   r    r   r   r   r#   <   r$   z'init_yolov5face_model.<locals>.<lambda>r%   r(   )r	   r   r   r*   r+   detectorr/   r0   r1   floatmodulestyper   	Hardswish	LeakyReLUReLUReLU6SiLUinplace
isinstancer   set_non_persistent_buffers_set)r   r   r   r2   r3   r4   mr   r   r   r   1   s$    
 

r   )Fr
   )Fr
   )r
   )osr*   r   copyr   Zfacelib.utilsr   r   Z*facelib.detection.yolov5face.models.commonr   Zretinaface.retinafacer   Zyolov5face.face_detectorr	   r   r   r   r   r   r   r   <module>   s   

