Noename commited on
Commit
cab137e
·
verified ·
1 Parent(s): d59d585

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -77
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  from PIL import Image
3
  import json
@@ -11,98 +12,138 @@ import torch
11
 
12
  from pytorch_lightning import seed_everything
13
  from annotator.util import resize_image, HWC3
14
- from cldm.model import create_model, load_state_dict
15
- from cldm.ddim_hacked import DDIMSampler
16
-
17
- import torch.nn as nn
18
- from torch.nn.functional import threshold, normalize,interpolate
19
- from torch.utils.data import Dataset
20
- from torch.optim import Adam
21
- from torch.utils.data import Dataset
22
- from torchvision import transforms
23
- from torch.utils.data import DataLoader
24
  from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
 
25
 
26
  import argparse
27
 
28
  device = "cuda" if torch.cuda.is_available() else "cpu"
29
 
30
- parseargs = argparse.ArgumentParser()
31
- parseargs.add_argument('--model', type=str, default='control_sd15_colorize_epoch=156.ckpt')
32
- args = parseargs.parse_args()
33
- model_path = args.model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  feature_extractor = SegformerFeatureExtractor.from_pretrained("matei-dorian/segformer-b5-finetuned-human-parsing")
36
  segmodel = SegformerForSemanticSegmentation.from_pretrained("matei-dorian/segformer-b5-finetuned-human-parsing")
37
 
38
- model = create_model('./models/control_sd15_colorize.yaml').cpu()
39
- model.load_state_dict(load_state_dict(f"./models/{model_path}", location=device))
40
- model = model.to(device)
41
- ddim_sampler = DDIMSampler(model)
42
 
43
  def LGB_TO_RGB(gray_image, rgb_image):
44
- # gray_image [H, W, 1]
45
  # rgb_image [H, W, 3]
46
 
 
 
 
 
47
  lab_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2LAB)
48
- lab_image[:, :, 0] = gray_image[:, :, 0]
49
 
50
  return cv2.cvtColor(lab_image, cv2.COLOR_LAB2RGB)
51
 
52
 
53
- def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, threshold, save_memory=False):
54
- # center crop image to square
55
- # H, W, _ = input_image.shape
56
- # if H > W:
57
- # input_image = input_image[(H - W) // 2:(H + W) // 2, :, :]
58
- # elif W > H:
59
- # input_image = input_image[:, (W - H) // 2:(H + W) // 2, :]
60
-
61
  with torch.no_grad():
62
  img = resize_image(input_image, image_resolution)
63
  H, W, C = img.shape
64
- print("img shape: ", img.shape)
65
- if C == 3:
66
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
67
- detected_map = img[:, :, None]
68
- print("Gray image shape: ", detected_map.shape)
69
- control = torch.from_numpy(detected_map.copy()).float().to(device)
70
- # control = einops.rearrange(control, 'h w c -> 1 c h w')
71
- print("Control shape: ", control.shape)
72
-
73
- control = control / 255.0
74
- control = torch.stack([control for _ in range(num_samples)], dim=0)
75
- print("Stacked control shape: ", control.shape)
76
- control = einops.rearrange(control, 'b h w c -> b c h w').clone()
77
-
78
- if seed == -1:
79
- seed = random.randint(0, 65535)
80
- seed_everything(seed)
81
-
82
- if save_memory:
83
- model.low_vram_shift(is_diffusing=False)
84
 
85
- cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
86
- un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
87
- shape = (4, H // 8, W // 8)
88
 
89
- if save_memory:
90
- model.low_vram_shift(is_diffusing=True)
91
 
92
- model.control_scales = [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else ([strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01
93
- samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
94
- shape, cond, verbose=False, eta=eta,
95
- unconditional_guidance_scale=scale,
96
- unconditional_conditioning=un_cond)
97
 
98
- if save_memory:
99
- model.low_vram_shift(is_diffusing=False)
100
-
101
- x_samples = model.decode_first_stage(samples)
102
- x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)
103
 
104
- results = [x_samples[i] for i in range(num_samples)]
105
- results = [LGB_TO_RGB(detected_map, result) for result in results]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  # results의 각 이미지를 mask로 변환
108
  masks = []
@@ -113,7 +154,7 @@ def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resoluti
113
  logits = logits.squeeze(0)
114
  thresholded = torch.zeros_like(logits)
115
  thresholded[logits > threshold] = 1
116
- mask = thresholded[1: ,:, :].sum(dim=0)
117
  mask = mask.unsqueeze(0).unsqueeze(0)
118
  mask = interpolate(mask, size=(H, W), mode='bilinear')
119
  mask = mask.detach().numpy()
@@ -123,13 +164,15 @@ def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resoluti
123
 
124
  # results의 각 이미지를 mask를 이용해 mask가 0인 부분은 img 즉 흑백 이미지로 변환.
125
  # img를 channel이 3인 rgb 이미지로 변환
126
- gray_img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) # [H, W, 3]
127
  final = [gray_img * (1 - mask[:, :, None]) + result * mask[:, :, None] for result, mask in zip(results, masks)]
128
 
129
  # mask to 255 img
130
 
131
  mask_img = [mask * 255 for mask in masks]
132
- return [detected_map.squeeze(-1)] + results + mask_img + final
 
 
 
133
 
134
 
135
  block = gr.Blocks().queue()
@@ -142,23 +185,25 @@ with block:
142
  prompt = gr.Textbox(label="Prompt")
143
  run_button = gr.Button(value="Run")
144
  with gr.Accordion("Advanced options", open=False):
145
- num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1, visible=False)
146
- image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=512, value=512, step=64)
 
147
  strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
148
- guess_mode = gr.Checkbox(label='Guess Mode', value=False, visible=False)
149
  ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=20, value=20, step=1)
150
- scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=1.0, step=0.1)
151
- threshold = gr.Slider(label="segmentation threshold", minimum=0.1, maximum=0.9, value=0.5, step=0.05)
152
- seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, value=-1)
153
  eta = gr.Number(label="eta (DDIM)", value=0.0)
154
- a_prompt = gr.Textbox(label="Added Prompt", value='best quality, extremely detailed')
155
  n_prompt = gr.Textbox(label="Negative Prompt",
156
  value='longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality')
157
  with gr.Column():
158
  # result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
159
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery")
160
- ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, threshold]
161
- run_button.click(fn=process, inputs=ips, outputs=[result_gallery], concurrency_limit=2)
 
162
 
163
  block.queue(max_size=100)
164
  block.launch(share=True)
 
1
+ import gc
2
  import os
3
  from PIL import Image
4
  import json
 
12
 
13
  from pytorch_lightning import seed_everything
14
  from annotator.util import resize_image, HWC3
15
+ from torch.nn.functional import threshold, normalize, interpolate
16
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
 
 
 
 
 
 
 
 
17
  from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
18
+ from einops import rearrange, repeat
19
 
20
  import argparse
21
 
22
  device = "cuda" if torch.cuda.is_available() else "cpu"
23
 
24
+ # parse= argparse.ArgumentParser()
25
+ # parseadd_argument('--pretrained_model', type=str, default='runwayml/stable-diffusion-v1-5')
26
+ # parseadd_argument('--controlnet', type=str, default='controlnet')
27
+ # parseadd_argument('--precision', type=str, default='fp32')
28
+ # = parseparse_)
29
+ # pretrained_model = pretrained_model
30
+ pretrained_model = 'runwayml/stable-diffusion-v1-5'
31
+ controlnet = 'models'
32
+ # controlnet = 'checkpoint-34000/controlnet'
33
+ precision = 'bf16'
34
+
35
+ # Check for different hardware architectures
36
+ if torch.cuda.is_available():
37
+ device = "cuda"
38
+ # Check for xformers
39
+ try:
40
+ import xformers
41
+
42
+ enable_xformers = True
43
+ except ImportError:
44
+ enable_xformers = False
45
+ elif torch.backends.mps.is_available():
46
+ device = "mps"
47
+ else:
48
+ device = "cpu"
49
+
50
+ print(f"Using device: {device}")
51
+
52
+ # Load models
53
+ if precision == 'fp32':
54
+ torch_dtype = torch.float32
55
+ elif precision == 'fp16':
56
+ torch_dtype = torch.float16
57
+ elif precision == 'bf16':
58
+ torch_dtype = torch.bfloat16
59
+ else:
60
+ raise ValueError(f"Invalid precision: {precision}")
61
+
62
+ controlnet = ControlNetModel.from_pretrained(controlnet, torch_dtype=torch_dtype, use_safetensors=True)
63
+
64
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
65
+ pretrained_model, controlnet=controlnet, torch_dtype=torch_dtype
66
+ )
67
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
68
+ pipe = pipe.to(device)
69
+
70
+ # Apply optimizations based on hardware
71
+ if device == "cuda":
72
+ pipe = pipe.to(device)
73
+ if enable_xformers:
74
+ pipe.enable_xformers_memory_efficient_attention()
75
+ print("xformers optimization enabled")
76
+ elif device == "mps":
77
+ pipe = pipe.to(device)
78
+ pipe.enable_attention_slicing()
79
+ print("Attention slicing enabled for Apple Silicon")
80
+ else:
81
+ # CPU-specific optimizations
82
+ pipe = pipe.to(device)
83
+ # pipe.enable_sequential_cpu_offload()
84
+ # pipe.enable_attention_slicing()
85
+
86
+ pipe.safety_checker = None
87
+ pipe.requires_safety_checker = False
88
 
89
  feature_extractor = SegformerFeatureExtractor.from_pretrained("matei-dorian/segformer-b5-finetuned-human-parsing")
90
  segmodel = SegformerForSemanticSegmentation.from_pretrained("matei-dorian/segformer-b5-finetuned-human-parsing")
91
 
 
 
 
 
92
 
93
  def LGB_TO_RGB(gray_image, rgb_image):
94
+ # gray_image [H, W, 3]
95
  # rgb_image [H, W, 3]
96
 
97
+ # print("gray_image shape: ", gray_image.shape)
98
+ # print("rgb_image shape: ", rgb_image.shape)
99
+
100
+ gray_image = cv2.cvtColor(gray_image, cv2.COLOR_RGB2GRAY)
101
  lab_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2LAB)
102
+ lab_image[:, :, 0] = gray_image[:, :]
103
 
104
  return cv2.cvtColor(lab_image, cv2.COLOR_LAB2RGB)
105
 
106
 
107
+ @torch.inference_mode()
108
+ def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, strength,
109
+ guidance_scale, seed, eta, threshold, save_memory=False):
 
 
 
 
 
110
  with torch.no_grad():
111
  img = resize_image(input_image, image_resolution)
112
  H, W, C = img.shape
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
+ gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
115
+ gray_img = cv2.cvtColor(gray_img, cv2.COLOR_GRAY2RGB)
 
116
 
117
+ control = Image.fromarray(img)
118
+ control = control.convert('L')
119
 
120
+ if a_prompt:
121
+ prompt = prompt + ', ' + a_prompt
 
 
 
122
 
123
+ if seed == -1:
124
+ seed = random.randint(0, 65535)
125
+ seed_everything(seed)
 
 
126
 
127
+ generator = torch.Generator(device=device).manual_seed(seed)
128
+ # Generate images
129
+ output = pipe(
130
+ num_images_per_prompt=num_samples,
131
+ prompt=prompt,
132
+ image=control,
133
+ negative_prompt=n_prompt,
134
+ num_inference_steps=ddim_steps,
135
+ guidance_scale=guidance_scale,
136
+ generator=generator,
137
+ eta=eta,
138
+ strength=strength,
139
+ output_type='np',
140
+ ).images
141
+
142
+ # output = einops.rearrange(output, 'b c h w -> b h w c')
143
+ output = (output * 127.5 + 127.5).clip(0, 255).astype(np.uint8)
144
+
145
+ results = [output[i] for i in range(num_samples)]
146
+ results = [LGB_TO_RGB(gray_img, result) for result in results]
147
 
148
  # results의 각 이미지를 mask로 변환
149
  masks = []
 
154
  logits = logits.squeeze(0)
155
  thresholded = torch.zeros_like(logits)
156
  thresholded[logits > threshold] = 1
157
+ mask = thresholded[1:, :, :].sum(dim=0)
158
  mask = mask.unsqueeze(0).unsqueeze(0)
159
  mask = interpolate(mask, size=(H, W), mode='bilinear')
160
  mask = mask.detach().numpy()
 
164
 
165
  # results의 각 이미지를 mask를 이용해 mask가 0인 부분은 img 즉 흑백 이미지로 변환.
166
  # img를 channel이 3인 rgb 이미지로 변환
 
167
  final = [gray_img * (1 - mask[:, :, None]) + result * mask[:, :, None] for result, mask in zip(results, masks)]
168
 
169
  # mask to 255 img
170
 
171
  mask_img = [mask * 255 for mask in masks]
172
+
173
+ gc.collect()
174
+
175
+ return [gray_img] + results + mask_img + final
176
 
177
 
178
  block = gr.Blocks().queue()
 
185
  prompt = gr.Textbox(label="Prompt")
186
  run_button = gr.Button(value="Run")
187
  with gr.Accordion("Advanced options", open=False):
188
+ num_samples = gr.Slider(label="Images", minimum=1, maximum=1, value=1, step=1, visible=False)
189
+ # num_samples = 1
190
+ image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=768, step=64)
191
  strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
192
+ # guess_mode = gr.Checkbox(label='Guess Mode', value=False)
193
  ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=20, value=20, step=1)
194
+ scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=7.0, step=0.1)
195
+ threshold = gr.Slider(label="Segmentation Threshold", minimum=0.1, maximum=0.9, value=0.5, step=0.05)
196
+ seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, value=-1, step=1)
197
  eta = gr.Number(label="eta (DDIM)", value=0.0)
198
+ a_prompt = gr.Textbox(label="Added Prompt", value='best quality, extremely detailed, vivid colors')
199
  n_prompt = gr.Textbox(label="Negative Prompt",
200
  value='longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality')
201
  with gr.Column():
202
  # result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
203
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery")
204
+ ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, strength, scale, seed,
205
+ eta, threshold]
206
+ run_button.click(fn=process, inputs=ips, outputs=[result_gallery], concurrency_limit=4)
207
 
208
  block.queue(max_size=100)
209
  block.launch(share=True)