Spaces:
Runtime error
Runtime error
hysts commited on
Commit ·
3a3e71c
1
Parent(s): 385c783
Use the original models
Browse files39 unmodified lines4041424344454647484950515253545556575859606162633 unmodified lines67686970717273747576777879808182838485868788899091929394959697989910010110210310410510639 unmodified lines}ORIGINAL_WEIGHT_ROOT = 'https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/'LIGHTWEIGHT_MODEL_NAMES = {'canny': 'control_canny-fp16.safetensors','hough': 'control_mlsd-fp16.safetensors','hed': 'control_hed-fp16.safetensors','scribble': 'control_scribble-fp16.safetensors','pose': 'control_openpose-fp16.safetensors','seg': 'control_seg-fp16.safetensors','depth': 'control_depth-fp16.safetensors','normal': 'control_normal-fp16.safetensors',}LIGHTWEIGHT_WEIGHT_ROOT = 'https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/'class Model:def __init__(self,model_config_path: str = 'ControlNet/models/cldm_v15.yaml',model_dir: str = 'models',use_lightweight: bool = True):self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')self.model = create_model(model_config_path).to(self.device)3 unmodified linesself.model_dir = pathlib.Path(model_dir)self.model_dir.mkdir(exist_ok=True, parents=True)self.use_lightweight = use_lightweightif use_lightweight:self.model_names = LIGHTWEIGHT_MODEL_NAMESself.weight_root = LIGHTWEIGHT_WEIGHT_ROOTbase_model_url = 'https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors'self.load_base_model(base_model_url)else:self.model_names = ORIGINAL_MODEL_NAMESself.weight_root = ORIGINAL_WEIGHT_ROOTself.download_models()def download_base_model(self, model_url: str) -> pathlib.Path:model_name = model_url.split('/')[-1]out_path = self.model_dir / model_nameif not out_path.exists():subprocess.run(shlex.split(f'wget {model_url} -O {out_path}'))return out_pathdef load_base_model(self, model_url: str) -> None:model_path = self.download_base_model(model_url)self.model.load_state_dict(load_state_dict(model_path,location=self.device.type),strict=False)def load_weight(self, task_name: str) -> None:if task_name == self.task_name:returnweight_path = self.get_weight_path(task_name)if not self.use_lightweight:self.model.load_state_dict(load_state_dict(weight_path, location=self.device))else:self.model.control_model.load_state_dict(load_state_dict(weight_path, location=self.device.type))self.task_name = task_namedef get_weight_path(self, task_name: str) -> str:39 unmodified lines40414243444546474849503 unmodified lines5455565758596061626364656667686939 unmodified lines}ORIGINAL_WEIGHT_ROOT = 'https://huggingface.co/lllyasviel/ControlNet/resolve/main/models/'class Model:def __init__(self,model_config_path: str = 'ControlNet/models/cldm_v15.yaml',model_dir: str = 'models'):self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')self.model = create_model(model_config_path).to(self.device)3 unmodified linesself.model_dir = pathlib.Path(model_dir)self.model_dir.mkdir(exist_ok=True, parents=True)self.model_names = ORIGINAL_MODEL_NAMESself.weight_root = ORIGINAL_WEIGHT_ROOTself.download_models()def load_weight(self, task_name: str) -> None:if task_name == self.task_name:returnweight_path = self.get_weight_path(task_name)self.model.load_state_dict(load_state_dict(weight_path, location=self.device))self.task_name = task_namedef get_weight_path(self, task_name: str) -> str:
model.py+5-42