yamildiego commited on
Commit
5b9b0ed
·
1 Parent(s): 7abe9d0

loading model from local and remove depth control net

Browse files
{checkpoints → .checkpoints/ControlNetModel}/config.json RENAMED
File without changes
{checkpoints → .checkpoints/ControlNetModel}/diffusion_pytorch_model.safetensors RENAMED
File without changes
{checkpoints → .checkpoints}/ip-adapter.bin RENAMED
File without changes
checkpoints/.DS_Store DELETED
Binary file (6.15 kB)
 
handler.py CHANGED
@@ -1,16 +1,3 @@
1
- # from typing import List, Any
2
- # import torch
3
- # from diffusers import StableCascadePriorPipeline, StableCascadeDecoderPipeline
4
-
5
- # # Configurar el dispositivo para ejecutar el modelo
6
- # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7
- # if device.type != 'cuda':
8
- # raise ValueError("Se requiere ejecutar en GPU")
9
-
10
- # # Configurar el tipo de dato mixto basado en la capacidad de la GPU
11
- # dtype = torch.bfloat16 if torch.cuda.get_device_capability(device.index)[0] >= 8 else torch.float16
12
-
13
- # start_test
14
  import cv2
15
  import numpy as np
16
 
@@ -45,31 +32,31 @@ dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32
45
 
46
  class EndpointHandler():
47
  def __init__(self, model_dir):
48
- hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/config.json", local_dir="./checkpoints")
49
- hf_hub_download(
50
- repo_id="InstantX/InstantID",
51
- filename="ControlNetModel/diffusion_pytorch_model.safetensors",
52
- local_dir="./checkpoints",
53
- )
54
- hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir="./checkpoints")
55
 
56
  print("Model dir: ", model_dir)
57
  face_adapter = f"./checkpoints/ip-adapter.bin"
58
  controlnet_path = f"./checkpoints/ControlNetModel"
59
 
60
- transform = Compose([
61
- Resize(
62
- width=518,
63
- height=518,
64
- resize_target=False,
65
- keep_aspect_ratio=True,
66
- ensure_multiple_of=14,
67
- resize_method='lower_bound',
68
- image_interpolation_method=cv2.INTER_CUBIC,
69
- ),
70
- NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
71
- PrepareForNet(),
72
- ])
73
 
74
  self.controlnet_identitynet = ControlNetModel.from_pretrained(
75
  controlnet_path, torch_dtype=dtype
@@ -103,7 +90,7 @@ class EndpointHandler():
103
  # controlnet-pose/canny/depth
104
  controlnet_pose_model = "thibaud/controlnet-openpose-sdxl-1.0"
105
  controlnet_canny_model = "diffusers/controlnet-canny-sdxl-1.0"
106
- controlnet_depth_model = "diffusers/controlnet-depth-sdxl-1.0-small"
107
 
108
  controlnet_pose = ControlNetModel.from_pretrained(
109
  controlnet_pose_model, torch_dtype=dtype
@@ -111,49 +98,49 @@ class EndpointHandler():
111
  controlnet_canny = ControlNetModel.from_pretrained(
112
  controlnet_canny_model, torch_dtype=dtype
113
  ).to(device)
114
- controlnet_depth = ControlNetModel.from_pretrained(
115
- controlnet_depth_model, torch_dtype=dtype
116
- ).to(device)
117
 
118
  openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
119
- depth_anything = DepthAnything.from_pretrained('LiheYoung/depth_anything_vitl14').to(device).eval()
120
 
121
  def get_canny_image(image, t1=100, t2=200):
122
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
123
  edges = cv2.Canny(image, t1, t2)
124
  return Image.fromarray(edges, "L")
125
 
126
- def get_depth_map(image):
127
 
128
- image = np.array(image) / 255.0
129
 
130
- h, w = image.shape[:2]
131
 
132
- image = transform({'image': image})['image']
133
- image = torch.from_numpy(image).unsqueeze(0).to("cuda")
134
 
135
- with torch.no_grad():
136
- depth = depth_anything(image)
137
 
138
- depth = F.interpolate(depth[None], (h, w), mode='bilinear', align_corners=False)[0, 0]
139
- depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
140
 
141
- depth = depth.cpu().numpy().astype(np.uint8)
142
 
143
- depth_image = Image.fromarray(depth)
144
 
145
- return depth_image
146
 
147
  self.controlnet_map = {
148
  "pose": controlnet_pose,
149
  "canny": controlnet_canny,
150
- "depth": controlnet_depth,
151
  }
152
 
153
  self.controlnet_map_fn = {
154
  "pose": openpose,
155
  "canny": get_canny_image,
156
- "depth": get_depth_map,
157
  }
158
 
159
  self.app = FaceAnalysis(name="buffalo_l", root="./", providers=["CPUExecutionProvider"])
@@ -167,8 +154,9 @@ class EndpointHandler():
167
  identitynet_strength_ratio = 0.8
168
  pose_strength = 0.4
169
  canny_strength = 0.3
170
- depth_strength = 0.5
171
- controlnet_selection = ["pose", "canny", "depth"]
 
172
 
173
  face_image_path = "https://i.ibb.co/SKg69dD/kaifu-resize.png"
174
  pose_image_path = "https://i.ibb.co/ZSrQ8ZJ/pose.jpg"
@@ -279,7 +267,7 @@ class EndpointHandler():
279
  controlnet_scales = {
280
  "pose": pose_strength,
281
  "canny": canny_strength,
282
- "depth": depth_strength,
283
  }
284
  self.pipe.controlnet = MultiControlNetModel(
285
  [self.controlnet_identitynet]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
  import numpy as np
3
 
 
32
 
33
  class EndpointHandler():
34
  def __init__(self, model_dir):
35
+ # hf_hub_download(repo_id="InstantX/InstantID", filename="ControlNetModel/config.json", local_dir="./checkpoints")
36
+ # hf_hub_download(
37
+ # repo_id="InstantX/InstantID",
38
+ # filename="ControlNetModel/diffusion_pytorch_model.safetensors",
39
+ # local_dir="./checkpoints",
40
+ # )
41
+ # hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_dir="./checkpoints")
42
 
43
  print("Model dir: ", model_dir)
44
  face_adapter = f"./checkpoints/ip-adapter.bin"
45
  controlnet_path = f"./checkpoints/ControlNetModel"
46
 
47
+ # transform = Compose([
48
+ # Resize(
49
+ # width=518,
50
+ # height=518,
51
+ # resize_target=False,
52
+ # keep_aspect_ratio=True,
53
+ # ensure_multiple_of=14,
54
+ # resize_method='lower_bound',
55
+ # image_interpolation_method=cv2.INTER_CUBIC,
56
+ # ),
57
+ # NormalizeImage(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
58
+ # PrepareForNet(),
59
+ # ])
60
 
61
  self.controlnet_identitynet = ControlNetModel.from_pretrained(
62
  controlnet_path, torch_dtype=dtype
 
90
  # controlnet-pose/canny/depth
91
  controlnet_pose_model = "thibaud/controlnet-openpose-sdxl-1.0"
92
  controlnet_canny_model = "diffusers/controlnet-canny-sdxl-1.0"
93
+ # controlnet_depth_model = "diffusers/controlnet-depth-sdxl-1.0-small"
94
 
95
  controlnet_pose = ControlNetModel.from_pretrained(
96
  controlnet_pose_model, torch_dtype=dtype
 
98
  controlnet_canny = ControlNetModel.from_pretrained(
99
  controlnet_canny_model, torch_dtype=dtype
100
  ).to(device)
101
+ # controlnet_depth = ControlNetModel.from_pretrained(
102
+ # controlnet_depth_model, torch_dtype=dtype
103
+ # ).to(device)
104
 
105
  openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
106
+ # depth_anything = DepthAnything.from_pretrained('LiheYoung/depth_anything_vitl14').to(device).eval()
107
 
108
  def get_canny_image(image, t1=100, t2=200):
109
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
110
  edges = cv2.Canny(image, t1, t2)
111
  return Image.fromarray(edges, "L")
112
 
113
+ # def get_depth_map(image):
114
 
115
+ # image = np.array(image) / 255.0
116
 
117
+ # h, w = image.shape[:2]
118
 
119
+ # image = transform({'image': image})['image']
120
+ # image = torch.from_numpy(image).unsqueeze(0).to("cuda")
121
 
122
+ # with torch.no_grad():
123
+ # depth = depth_anything(image)
124
 
125
+ # depth = F.interpolate(depth[None], (h, w), mode='bilinear', align_corners=False)[0, 0]
126
+ # depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
127
 
128
+ # depth = depth.cpu().numpy().astype(np.uint8)
129
 
130
+ # depth_image = Image.fromarray(depth)
131
 
132
+ # return depth_image
133
 
134
  self.controlnet_map = {
135
  "pose": controlnet_pose,
136
  "canny": controlnet_canny,
137
+ # "depth": controlnet_depth,
138
  }
139
 
140
  self.controlnet_map_fn = {
141
  "pose": openpose,
142
  "canny": get_canny_image,
143
+ # "depth": get_depth_map,
144
  }
145
 
146
  self.app = FaceAnalysis(name="buffalo_l", root="./", providers=["CPUExecutionProvider"])
 
154
  identitynet_strength_ratio = 0.8
155
  pose_strength = 0.4
156
  canny_strength = 0.3
157
+ # depth_strength = 0.5
158
+ controlnet_selection = ["pose", "canny"]
159
+ # controlnet_selection = ["pose", "canny", "depth"]
160
 
161
  face_image_path = "https://i.ibb.co/SKg69dD/kaifu-resize.png"
162
  pose_image_path = "https://i.ibb.co/ZSrQ8ZJ/pose.jpg"
 
267
  controlnet_scales = {
268
  "pose": pose_strength,
269
  "canny": canny_strength,
270
+ # "depth": depth_strength,
271
  }
272
  self.pipe.controlnet = MultiControlNetModel(
273
  [self.controlnet_identitynet]