Spaces:
Runtime error
Runtime error
hysts commited on
Commit ·
b2c2519
1
Parent(s): ca01e24
Use extracted lightweight models
Browse filesFiles changed (3)
- app.py+3-2
- model.py+60-20
- requirements.txt+1
62 unmodified lines63646566676869707162 unmodified linescreate_demo_scribble(model.process_scribble, max_images=MAX_IMAGES)with gr.TabItem('Scribble Interactive'):create_demo_scribble_interactive(model.process_scribble_interactive, max_images=MAX_IMAGES)with gr.TabItem('Fake Scribble'):create_demo_fake_scribble(model.process_fake_scribble, max_images=MAX_IMAGES)with gr.TabItem('Pose'):create_demo_pose(model.process_pose, max_images=MAX_IMAGES)with gr.TabItem('Segmentation'):62 unmodified lines6364656667686970717262 unmodified linescreate_demo_scribble(model.process_scribble, max_images=MAX_IMAGES)with gr.TabItem('Scribble Interactive'):create_demo_scribble_interactive(model.process_scribble_interactive, max_images=MAX_IMAGES)with gr.TabItem('Fake Scribble'):create_demo_fake_scribble(model.process_fake_scribble,max_images=MAX_IMAGES)with gr.TabItem('Pose'):create_demo_pose(model.process_pose, max_images=MAX_IMAGES)with gr.TabItem('Segmentation'):
27 unmodified lines282930313233343536373839404142434445464748491 unmodified line5152535455565758596061626364656667686970717273747576777879808127 unmodified linesfrom ldm.models.diffusion.ddim import DDIMSamplerfrom share import *class Model:WEIGHT_NAMES = {'canny': 'control_sd15_canny.pth','hough': 'control_sd15_mlsd.pth','hed': 'control_sd15_hed.pth','scribble': 'control_sd15_scribble.pth','pose': 'control_sd15_openpose.pth','seg': 'control_sd15_seg.pth','depth': 'control_sd15_depth.pth','normal': 'control_sd15_normal.pth',}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)1 unmodified lineself.task_name = ''self.model_dir = pathlib.Path(model_dir)self.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:if 'scribble' in task_name:task_name = 'scribble'return f'{self.model_dir}/{self.WEIGHT_NAMES[task_name]}'def download_models(self):self.model_dir.mkdir(exist_ok=True, parents=True)for name in self.WEIGHT_NAMES.values():out_path = self.model_dir / nameif out_path.exists():continuesubprocess.run(shlex.split(f'wget https://huggingface.co/ckpt/ControlNet/resolve/main/{name} -O {out_path}'))@torch.inference_mode()def process_canny(self, input_image, prompt, a_prompt, n_prompt,27 unmodified lines2829303132333435363738394041424344454647484950515253545556575859606162631 unmodified line656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012127 unmodified linesfrom ldm.models.diffusion.ddim import DDIMSamplerfrom share import *ORIGINAL_MODEL_NAMES = {'canny': 'control_sd15_canny.pth','hough': 'control_sd15_mlsd.pth','hed': 'control_sd15_hed.pth','scribble': 'control_sd15_scribble.pth','pose': 'control_sd15_openpose.pth','seg': 'control_sd15_seg.pth','depth': 'control_sd15_depth.pth','normal': 'control_sd15_normal.pth',}ORIGINAL_WEIGHT_ROOT = 'https://huggingface.co/ckpt/ControlNet/resolve/main/'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)1 unmodified lineself.task_name = ''self.model_dir = pathlib.Path(model_dir)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.download_base_model(base_model_url)base_model_path = self.model_dir / base_model_url.split('/')[-1]self.load_base_model(base_model_path)else:self.model_names = ORIGINAL_MODEL_NAMESself.weight_root = ORIGINAL_WEIGHT_ROOTself.download_models()def download_base_model(self, base_model_url: str) -> None:model_name = base_model_url.split('/')[-1]out_path = self.model_dir / model_nameif out_path.exists():returnsubprocess.run(shlex.split(f'wget {base_model_url} -O {out_path}'))def load_base_model(self, model_path: pathlib.Path) -> None: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:if 'scribble' in task_name:task_name = 'scribble'return f'{self.model_dir}/{self.model_names[task_name]}'def download_models(self) -> None:self.model_dir.mkdir(exist_ok=True, parents=True)for name in self.model_names.values():out_path = self.model_dir / nameif out_path.exists():continuesubprocess.run(shlex.split(f'wget {self.weight_root}{name} -O {out_path}'))@torch.inference_mode()def process_canny(self, input_image, prompt, a_prompt, n_prompt,
10 unmodified lines11121314151610 unmodified linesopencv-python-headless==4.7.0.68prettytable==3.6.0pytorch-lightning==1.9.0timm==0.6.12torch==1.13.1torchvision==0.14.110 unmodified lines1112131415161710 unmodified linesopencv-python-headless==4.7.0.68prettytable==3.6.0pytorch-lightning==1.9.0safetensors==0.2.8timm==0.6.12torch==1.13.1torchvision==0.14.1
app.py+3-2
model.py+60-20
requirements.txt+1