Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,13 +14,22 @@ from annotator.uniformer import UniformerDetector
|
|
| 14 |
from cldm.model import create_model, load_state_dict
|
| 15 |
from cldm.ddim_hacked import DDIMSampler
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
# os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
|
|
|
|
| 19 |
|
| 20 |
apply_uniformer = UniformerDetector()
|
| 21 |
|
| 22 |
model = create_model('./models/cldm_v15.yaml').cpu()
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
model = model.cuda()
|
| 25 |
ddim_sampler = DDIMSampler(model)
|
| 26 |
|
|
@@ -67,6 +76,45 @@ def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resoluti
|
|
| 67 |
results = [x_samples[i] for i in range(num_samples)]
|
| 68 |
return [detected_map] + results
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
block = gr.Blocks().queue()
|
| 72 |
with block:
|
|
@@ -74,26 +122,35 @@ with block:
|
|
| 74 |
gr.Markdown("## Control Stable Diffusion with Segmentation Maps")
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Column():
|
| 94 |
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
|
| 95 |
-
ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, detect_resolution, ddim_steps, guess_mode, strength, scale, seed, eta]
|
| 96 |
-
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
block.launch(server_name='
|
|
|
|
| 14 |
from cldm.model import create_model, load_state_dict
|
| 15 |
from cldm.ddim_hacked import DDIMSampler
|
| 16 |
|
| 17 |
+
from PIL import Image
|
| 18 |
|
| 19 |
# os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
|
| 20 |
+
device = "cuda:0"
|
| 21 |
|
| 22 |
apply_uniformer = UniformerDetector()
|
| 23 |
|
| 24 |
model = create_model('./models/cldm_v15.yaml').cpu()
|
| 25 |
+
|
| 26 |
+
# ckpt for sd 1.5 DB finetuned with crack 500
|
| 27 |
+
resume_path ="./models/control_sks_crack_ppl.ckpt"
|
| 28 |
+
# ckpt for controlnet with sd 1.5 weights ( strict = False ), finetuned with ADE20K controlnet weight by crack500
|
| 29 |
+
controlnet_path = "./models/sks_crack500_epoch_19.ckpt"
|
| 30 |
+
model.load_state_dict(load_state_dict(resume_path, location='cpu'))
|
| 31 |
+
model.load_state_dict(load_state_dict(controlnet_path, location='cpu'), strict = False)
|
| 32 |
+
|
| 33 |
model = model.cuda()
|
| 34 |
ddim_sampler = DDIMSampler(model)
|
| 35 |
|
|
|
|
| 76 |
results = [x_samples[i] for i in range(num_samples)]
|
| 77 |
return [detected_map] + results
|
| 78 |
|
| 79 |
+
def model_sample(mask,
|
| 80 |
+
prompt = "sks crack, pavement cracks, HDR, Asphalt road, mudded",
|
| 81 |
+
a_prompt="",
|
| 82 |
+
n_prompt="",
|
| 83 |
+
num_samples=1, ddim_steps=50, guess_mode=False, strength=1.0, scale=7.0, seed=-1, eta=1.0):
|
| 84 |
+
# mask --- numpy
|
| 85 |
+
ddim_sampler = DDIMSampler(model)
|
| 86 |
+
|
| 87 |
+
with torch.no_grad():
|
| 88 |
+
mask = HWC3(mask)
|
| 89 |
+
mask = resize_image(mask, 512)
|
| 90 |
+
H, W, C= mask.shape
|
| 91 |
+
|
| 92 |
+
control = torch.from_numpy(mask.copy()).float().cuda() / 255.0
|
| 93 |
+
control = torch.stack([control for _ in range(num_samples)], dim=0)
|
| 94 |
+
control = einops.rearrange(control, 'b h w c -> b c h w').clone()
|
| 95 |
+
|
| 96 |
+
if seed == -1:
|
| 97 |
+
seed = random.randint(0, 65535)
|
| 98 |
+
seed_everything(seed)
|
| 99 |
+
|
| 100 |
+
cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
|
| 101 |
+
un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
|
| 102 |
+
shape = (4, H // 8, W // 8)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
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
|
| 106 |
+
samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
|
| 107 |
+
shape, cond, verbose=False, eta=eta,
|
| 108 |
+
unconditional_guidance_scale=scale,
|
| 109 |
+
unconditional_conditioning=un_cond)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
x_samples = model.decode_first_stage(samples)
|
| 113 |
+
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)
|
| 114 |
+
|
| 115 |
+
results = [x_samples[i] for i in range(num_samples)]
|
| 116 |
+
|
| 117 |
+
return results
|
| 118 |
|
| 119 |
block = gr.Blocks().queue()
|
| 120 |
with block:
|
|
|
|
| 122 |
gr.Markdown("## Control Stable Diffusion with Segmentation Maps")
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column():
|
| 125 |
+
with gr.Row():
|
| 126 |
+
with gr.Tabs(elem_id="mode_img2img"):
|
| 127 |
+
with gr.TabItem('img2img', id='img2img', elem_id="img2img_img2img_tab") as tab_img2img:
|
| 128 |
+
init_img = gr.Image(label="Image for img2img", elem_id="img2img_image", show_label=False, source="upload", interactive=True, type="numpy", tool="editor", image_mode="L").style(height=480)
|
| 129 |
+
init_run_button = gr.Button(label="Run Init")
|
| 130 |
+
with gr.TabItem('Sketch', id='img2img_sketch', elem_id="img2img_img2img_sketch_tab") as tab_sketch:
|
| 131 |
+
sketch_img = gr.Image(label="Image for img2img", elem_id="img2img_sketch", show_label=False, source="canvas", interactive=True, type="numpy", tool="color-sketch", image_mode="L").style(height=480)
|
| 132 |
+
sketch_run_button = gr.Button(label="Run Sketch")
|
| 133 |
+
prompt = gr.Textbox(label="Prompt")
|
| 134 |
+
with gr.Row():
|
| 135 |
+
with gr.Accordion("Advanced options", open=False):
|
| 136 |
+
num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
|
| 137 |
+
image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64)
|
| 138 |
+
strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
|
| 139 |
+
guess_mode = gr.Checkbox(label='Guess Mode', value=False)
|
| 140 |
+
detect_resolution = gr.Slider(label="Segmentation Resolution", minimum=128, maximum=1024, value=512, step=1)
|
| 141 |
+
ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
|
| 142 |
+
scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
|
| 143 |
+
seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)
|
| 144 |
+
eta = gr.Number(label="eta (DDIM)", value=0.0)
|
| 145 |
+
a_prompt = gr.Textbox(label="Added Prompt", value='best quality, extremely detailed')
|
| 146 |
+
n_prompt = gr.Textbox(label="Negative Prompt",
|
| 147 |
+
value='longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality')
|
| 148 |
with gr.Column():
|
| 149 |
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
init_ips = [init_img, prompt, a_prompt, n_prompt, num_samples, ddim_steps, guess_mode, strength, scale, seed, eta]
|
| 152 |
+
sketch_ips = [sketch_img, prompt, a_prompt, n_prompt, num_samples, ddim_steps, guess_mode, strength, scale, seed, eta]
|
| 153 |
+
init_run_button.click(fn=model_sample, inputs=init_ips, outputs=[result_gallery])
|
| 154 |
+
sketch_run_button.click(fn=model_sample, inputs=sketch_ips, outputs=[result_gallery])
|
| 155 |
|
| 156 |
+
block.launch(server_name='0.0.0.0', server_port=3001)
|