valhalla commited on
Commit
1b9d6e2
1 Parent(s): cb8f667

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +73 -32
README.md CHANGED
@@ -19,29 +19,85 @@ These are controlnet weights trained on stabilityai/stable-diffusion-xl-base-1.0
19
 
20
  ## Usage
21
 
22
- Make sure to first install the libraries:
23
 
24
  ```bash
25
  pip install accelerate transformers safetensors diffusers
26
  ```
27
 
28
- And then we're ready to go:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  ```python
31
  import torch
32
  import numpy as np
33
  from PIL import Image
34
 
35
- from transformers import DPTFeatureExtractor, DPTForDepthEstimation
36
  from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
37
  from diffusers.utils import load_image
38
 
39
-
40
- depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
41
- feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
42
  controlnet = ControlNetModel.from_pretrained(
43
  "diffusers/controlnet-depth-sdxl-1.0",
44
- variant="fp16",
45
  use_safetensors=True,
46
  torch_dtype=torch.float16,
47
  ).to("cuda")
@@ -56,41 +112,26 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
56
  ).to("cuda")
57
  pipe.enable_model_cpu_offload()
58
 
59
- def get_depth_map(image):
60
- image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
61
- with torch.no_grad(), torch.autocast("cuda"):
62
- depth_map = depth_estimator(image).predicted_depth
63
 
64
- depth_map = torch.nn.functional.interpolate(
65
- depth_map.unsqueeze(1),
66
- size=(1024, 1024),
67
- mode="bicubic",
68
- align_corners=False,
69
- )
70
- depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
71
- depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
72
- depth_map = (depth_map - depth_min) / (depth_max - depth_min)
73
- image = torch.cat([depth_map] * 3, dim=1)
74
 
75
- image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
76
- image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
77
- return image
78
 
 
79
 
80
- prompt = "stormtrooper lecture, photorealistic"
81
- image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")
82
- controlnet_conditioning_scale = 0.5 # recommended for good generalization
83
-
84
- depth_image = get_depth_map(image)
85
-
86
  images = pipe(
87
- prompt, image=depth_image, num_inference_steps=30, controlnet_conditioning_scale=controlnet_conditioning_scale,
88
  ).images
89
  images[0]
90
 
91
- images[0].save(f"stormtrooper.png")
92
  ```
93
 
 
 
94
  To more details, check out the official documentation of [`StableDiffusionXLControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/controlnet_sdxl).
95
 
96
  ### Training
 
19
 
20
  ## Usage
21
 
22
+ Make sure first to install the libraries:
23
 
24
  ```bash
25
  pip install accelerate transformers safetensors diffusers
26
  ```
27
 
28
+ And then setup the zoe-depth model
29
+
30
+ ```
31
+ import torch
32
+ import matplotlib
33
+ import matplotlib.cm
34
+ import numpy as np
35
+
36
+ torch.hub.help("intel-isl/MiDaS", "DPT_BEiT_L_384", force_reload=True) # Triggers fresh download of MiDaS repo
37
+ model_zoe_n = torch.hub.load("isl-org/ZoeDepth", "ZoeD_NK", pretrained=True).eval()
38
+ model_zoe_n = model_zoe_n.to("cuda")
39
+
40
+
41
+ def colorize(value, vmin=None, vmax=None, cmap='gray_r', invalid_val=-99, invalid_mask=None, background_color=(128, 128, 128, 255), gamma_corrected=False, value_transform=None):
42
+ if isinstance(value, torch.Tensor):
43
+ value = value.detach().cpu().numpy()
44
+
45
+ value = value.squeeze()
46
+ if invalid_mask is None:
47
+ invalid_mask = value == invalid_val
48
+ mask = np.logical_not(invalid_mask)
49
+
50
+ # normalize
51
+ vmin = np.percentile(value[mask],2) if vmin is None else vmin
52
+ vmax = np.percentile(value[mask],85) if vmax is None else vmax
53
+ if vmin != vmax:
54
+ value = (value - vmin) / (vmax - vmin) # vmin..vmax
55
+ else:
56
+ # Avoid 0-division
57
+ value = value * 0.
58
+
59
+ # squeeze last dim if it exists
60
+ # grey out the invalid values
61
+
62
+ value[invalid_mask] = np.nan
63
+ cmapper = matplotlib.cm.get_cmap(cmap)
64
+ if value_transform:
65
+ value = value_transform(value)
66
+ # value = value / value.max()
67
+ value = cmapper(value, bytes=True) # (nxmx4)
68
+
69
+ # img = value[:, :, :]
70
+ img = value[...]
71
+ img[invalid_mask] = background_color
72
+
73
+ # gamma correction
74
+ img = img / 255
75
+ img = np.power(img, 2.2)
76
+ img = img * 255
77
+ img = img.astype(np.uint8)
78
+ img = Image.fromarray(img)
79
+ return img
80
+
81
+
82
+ def get_zoe_depth_map(image):
83
+ with torch.autocast("cuda", enabled=True):
84
+ depth = model_zoe_n.infer_pil(image)
85
+ depth = colorize(depth, cmap="gray_r", gamma_corrected=True)
86
+ return depth
87
+ ```
88
+
89
+ Now we're ready to go:
90
 
91
  ```python
92
  import torch
93
  import numpy as np
94
  from PIL import Image
95
 
 
96
  from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
97
  from diffusers.utils import load_image
98
 
 
 
 
99
  controlnet = ControlNetModel.from_pretrained(
100
  "diffusers/controlnet-depth-sdxl-1.0",
 
101
  use_safetensors=True,
102
  torch_dtype=torch.float16,
103
  ).to("cuda")
 
112
  ).to("cuda")
113
  pipe.enable_model_cpu_offload()
114
 
 
 
 
 
115
 
116
+ prompt = "pixel-art margot robbie as barbie, in a coupé . low-res, blocky, pixel art style, 8-bit graphics"
117
+ negative_prompt = "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic"
118
+ image = load_image("https://media.vogue.fr/photos/62bf04b69a57673c725432f3/3:2/w_1793,h_1195,c_limit/rev-1-Barbie-InstaVert_High_Res_JPEG.jpeg")
 
 
 
 
 
 
 
119
 
120
+ controlnet_conditioning_scale = 0.55
 
 
121
 
122
+ depth_image = get_zoe_depth_map(image).resize((1088, 896))
123
 
124
+ generator = torch.Generator("cuda").manual_seed(978364352)
 
 
 
 
 
125
  images = pipe(
126
+ prompt, image=depth_image, num_inference_steps=50, controlnet_conditioning_scale=controlnet_conditioning_scale, generator=generator
127
  ).images
128
  images[0]
129
 
130
+ images[0].save(f"pixel-barbie.png")
131
  ```
132
 
133
+ ![images_1)](./barbie.png)
134
+
135
  To more details, check out the official documentation of [`StableDiffusionXLControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/controlnet_sdxl).
136
 
137
  ### Training