Spaces:
Running
on
Zero
Running
on
Zero
ResearcherXman
commited on
Commit
•
857a5c4
1
Parent(s):
5b3c0e4
fix cuda errors
Browse files- app.py +33 -4
- controlnet_util.py +0 -38
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import os
|
2 |
import cv2
|
3 |
import torch
|
4 |
import random
|
@@ -20,10 +19,36 @@ from insightface.app import FaceAnalysis
|
|
20 |
|
21 |
from style_template import styles
|
22 |
from pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kps
|
23 |
-
from controlnet_util import openpose, get_depth_map, get_canny_image
|
24 |
|
|
|
|
|
25 |
import gradio as gr
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# global variable
|
28 |
MAX_SEED = np.iinfo(np.int32).max
|
29 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -46,10 +71,14 @@ hf_hub_download(repo_id="InstantX/InstantID", filename="ip-adapter.bin", local_d
|
|
46 |
app = FaceAnalysis(
|
47 |
name="antelopev2",
|
48 |
root="./",
|
49 |
-
providers=["
|
50 |
)
|
51 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
52 |
|
|
|
|
|
|
|
|
|
53 |
# Path to InstantID models
|
54 |
face_adapter = f"./checkpoints/ip-adapter.bin"
|
55 |
controlnet_path = f"./checkpoints/ControlNetModel"
|
@@ -59,7 +88,7 @@ controlnet_identitynet = ControlNetModel.from_pretrained(
|
|
59 |
controlnet_path, torch_dtype=dtype
|
60 |
)
|
61 |
|
62 |
-
# controlnet-pose
|
63 |
controlnet_pose_model = "thibaud/controlnet-openpose-sdxl-1.0"
|
64 |
controlnet_canny_model = "diffusers/controlnet-canny-sdxl-1.0"
|
65 |
controlnet_depth_model = "diffusers/controlnet-depth-sdxl-1.0-small"
|
|
|
|
|
1 |
import cv2
|
2 |
import torch
|
3 |
import random
|
|
|
19 |
|
20 |
from style_template import styles
|
21 |
from pipeline_stable_diffusion_xl_instantid_full import StableDiffusionXLInstantIDPipeline, draw_kps
|
|
|
22 |
|
23 |
+
from controlnet_aux import OpenposeDetector
|
24 |
+
from transformers import DPTImageProcessor, DPTForDepthEstimation
|
25 |
import gradio as gr
|
26 |
|
27 |
+
def get_depth_map(image):
|
28 |
+
image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
|
29 |
+
with torch.no_grad(), torch.autocast("cuda"):
|
30 |
+
depth_map = depth_estimator(image).predicted_depth
|
31 |
+
|
32 |
+
depth_map = torch.nn.functional.interpolate(
|
33 |
+
depth_map.unsqueeze(1),
|
34 |
+
size=(1024, 1024),
|
35 |
+
mode="bicubic",
|
36 |
+
align_corners=False,
|
37 |
+
)
|
38 |
+
depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
|
39 |
+
depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
|
40 |
+
depth_map = (depth_map - depth_min) / (depth_max - depth_min)
|
41 |
+
image = torch.cat([depth_map] * 3, dim=1)
|
42 |
+
|
43 |
+
image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
|
44 |
+
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
|
45 |
+
return image
|
46 |
+
|
47 |
+
def get_canny_image(image, t1=100, t2=200):
|
48 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
49 |
+
edges = cv2.Canny(image, t1, t2)
|
50 |
+
return Image.fromarray(edges, "L")
|
51 |
+
|
52 |
# global variable
|
53 |
MAX_SEED = np.iinfo(np.int32).max
|
54 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
71 |
app = FaceAnalysis(
|
72 |
name="antelopev2",
|
73 |
root="./",
|
74 |
+
providers=["CPUExecutionProvider"],
|
75 |
)
|
76 |
app.prepare(ctx_id=0, det_size=(640, 640))
|
77 |
|
78 |
+
depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(device)
|
79 |
+
feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas")
|
80 |
+
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
81 |
+
|
82 |
# Path to InstantID models
|
83 |
face_adapter = f"./checkpoints/ip-adapter.bin"
|
84 |
controlnet_path = f"./checkpoints/ControlNetModel"
|
|
|
88 |
controlnet_path, torch_dtype=dtype
|
89 |
)
|
90 |
|
91 |
+
# controlnet-pose/canny/depth
|
92 |
controlnet_pose_model = "thibaud/controlnet-openpose-sdxl-1.0"
|
93 |
controlnet_canny_model = "diffusers/controlnet-canny-sdxl-1.0"
|
94 |
controlnet_depth_model = "diffusers/controlnet-depth-sdxl-1.0-small"
|
controlnet_util.py
DELETED
@@ -1,38 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
import numpy as np
|
3 |
-
from PIL import Image
|
4 |
-
from controlnet_aux import OpenposeDetector
|
5 |
-
import cv2
|
6 |
-
|
7 |
-
|
8 |
-
from transformers import DPTImageProcessor, DPTForDepthEstimation
|
9 |
-
|
10 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
-
depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to(device)
|
12 |
-
feature_extractor = DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas")
|
13 |
-
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
14 |
-
|
15 |
-
def get_depth_map(image):
|
16 |
-
image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
|
17 |
-
with torch.no_grad(), torch.autocast("cuda"):
|
18 |
-
depth_map = depth_estimator(image).predicted_depth
|
19 |
-
|
20 |
-
depth_map = torch.nn.functional.interpolate(
|
21 |
-
depth_map.unsqueeze(1),
|
22 |
-
size=(1024, 1024),
|
23 |
-
mode="bicubic",
|
24 |
-
align_corners=False,
|
25 |
-
)
|
26 |
-
depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
|
27 |
-
depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
|
28 |
-
depth_map = (depth_map - depth_min) / (depth_max - depth_min)
|
29 |
-
image = torch.cat([depth_map] * 3, dim=1)
|
30 |
-
|
31 |
-
image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
|
32 |
-
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
|
33 |
-
return image
|
34 |
-
|
35 |
-
def get_canny_image(image, t1=100, t2=200):
|
36 |
-
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
37 |
-
edges = cv2.Canny(image, t1, t2)
|
38 |
-
return Image.fromarray(edges, "L")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|