Aore commited on
Commit
74fe6a0
·
1 Parent(s): 18da007

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -55
app.py CHANGED
@@ -19,64 +19,22 @@ from PIL import Image
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
- controlnet_path = "./models/sks_crack500_epoch_19.ckpt"
 
27
  model.load_state_dict(load_state_dict(controlnet_path, location='cpu'), strict = False)
28
 
29
  model = model.cuda()
30
  ddim_sampler = DDIMSampler(model)
 
31
 
32
 
33
- def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, detect_resolution, ddim_steps, guess_mode, strength, scale, seed, eta):
34
- with torch.no_grad():
35
- input_image = HWC3(input_image)
36
- detected_map = apply_uniformer(resize_image(input_image, detect_resolution))
37
- img = resize_image(input_image, image_resolution)
38
- H, W, C = img.shape
39
-
40
- detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_NEAREST)
41
-
42
- control = torch.from_numpy(detected_map.copy()).float().cuda() / 255.0
43
- control = torch.stack([control for _ in range(num_samples)], dim=0)
44
- control = einops.rearrange(control, 'b h w c -> b c h w').clone()
45
-
46
- if seed == -1:
47
- seed = random.randint(0, 65535)
48
- seed_everything(seed)
49
-
50
- if config.save_memory:
51
- model.low_vram_shift(is_diffusing=False)
52
-
53
- cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
54
- un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
55
- shape = (4, H // 8, W // 8)
56
-
57
- if config.save_memory:
58
- model.low_vram_shift(is_diffusing=True)
59
-
60
- 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
61
- samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
62
- shape, cond, verbose=False, eta=eta,
63
- unconditional_guidance_scale=scale,
64
- unconditional_conditioning=un_cond)
65
-
66
- if config.save_memory:
67
- model.low_vram_shift(is_diffusing=False)
68
-
69
- x_samples = model.decode_first_stage(samples)
70
- 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)
71
-
72
- results = [x_samples[i] for i in range(num_samples)]
73
- return [detected_map] + results
74
-
75
  def model_sample(mask,
76
  prompt = "sks crack, pavement cracks, HDR, Asphalt road, mudded",
77
  a_prompt="",
78
  n_prompt="",
79
- num_samples=1, ddim_steps=50, guess_mode=False, strength=1.0, scale=7.0, seed=-1, eta=1.0):
80
  # mask --- numpy
81
  ddim_sampler = DDIMSampler(model)
82
 
@@ -120,13 +78,13 @@ with block:
120
  with gr.Column():
121
  with gr.Row():
122
  with gr.Tabs(elem_id="mode_img2img"):
123
- with gr.TabItem('img2img', id='img2img', elem_id="img2img_img2img_tab") as tab_img2img:
124
- 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)
125
  init_run_button = gr.Button(label="Run Init")
126
  with gr.TabItem('Sketch', id='img2img_sketch', elem_id="img2img_img2img_sketch_tab") as tab_sketch:
127
  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)
128
  sketch_run_button = gr.Button(label="Run Sketch")
129
- prompt = gr.Textbox(label="Prompt")
130
  with gr.Row():
131
  with gr.Accordion("Advanced options", open=False):
132
  num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
@@ -135,18 +93,18 @@ with block:
135
  guess_mode = gr.Checkbox(label='Guess Mode', value=False)
136
  detect_resolution = gr.Slider(label="Segmentation Resolution", minimum=128, maximum=1024, value=512, step=1)
137
  ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
138
- scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
139
  seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)
140
  eta = gr.Number(label="eta (DDIM)", value=0.0)
141
- a_prompt = gr.Textbox(label="Added Prompt", value='best quality, extremely detailed')
142
  n_prompt = gr.Textbox(label="Negative Prompt",
143
- value='longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality')
144
  with gr.Column():
145
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
146
-
147
  init_ips = [init_img, prompt, a_prompt, n_prompt, num_samples, ddim_steps, guess_mode, strength, scale, seed, eta]
148
  sketch_ips = [sketch_img, prompt, a_prompt, n_prompt, num_samples, ddim_steps, guess_mode, strength, scale, seed, eta]
149
  init_run_button.click(fn=model_sample, inputs=init_ips, outputs=[result_gallery])
150
  sketch_run_button.click(fn=model_sample, inputs=sketch_ips, outputs=[result_gallery])
151
 
152
- block.launch(server_name='0.0.0.0', server_port=3001)
 
19
  # os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
20
  device = "cuda:0"
21
 
 
 
22
  model = create_model('./models/cldm_v15.yaml').cpu()
23
+ sd_model_path = "/home/leiqin/stable-diffusion-webui/models/Stable-diffusion/sks_crack_ppl.ckpt"
24
+ controlnet_path = "/home/leiqin/stable-diffusion-webui/extensions/sd-webui-controlnet/models/sks_crack_controlnet.pth"
25
+ model.load_state_dict(load_state_dict(sd_model_path, location='cpu'), strict = False)
26
  model.load_state_dict(load_state_dict(controlnet_path, location='cpu'), strict = False)
27
 
28
  model = model.cuda()
29
  ddim_sampler = DDIMSampler(model)
30
+ init_mask = Image.open("379.png").convert("L")
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def model_sample(mask,
34
  prompt = "sks crack, pavement cracks, HDR, Asphalt road, mudded",
35
  a_prompt="",
36
  n_prompt="",
37
+ num_samples=1, ddim_steps=50, guess_mode=False, strength=1.0, scale=7.0, seed=-1, eta=0.0):
38
  # mask --- numpy
39
  ddim_sampler = DDIMSampler(model)
40
 
 
78
  with gr.Column():
79
  with gr.Row():
80
  with gr.Tabs(elem_id="mode_img2img"):
81
+ with gr.TabItem('txt2img', id='img2img', elem_id="img2img_img2img_tab") as tab_img2img:
82
+ 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", value=init_mask).style(height=480)
83
  init_run_button = gr.Button(label="Run Init")
84
  with gr.TabItem('Sketch', id='img2img_sketch', elem_id="img2img_img2img_sketch_tab") as tab_sketch:
85
  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)
86
  sketch_run_button = gr.Button(label="Run Sketch")
87
+ prompt = gr.Textbox(label="Prompt", value="sks crack")
88
  with gr.Row():
89
  with gr.Accordion("Advanced options", open=False):
90
  num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
 
93
  guess_mode = gr.Checkbox(label='Guess Mode', value=False)
94
  detect_resolution = gr.Slider(label="Segmentation Resolution", minimum=128, maximum=1024, value=512, step=1)
95
  ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
96
+ scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=7.0, step=0.1)
97
  seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)
98
  eta = gr.Number(label="eta (DDIM)", value=0.0)
99
+ a_prompt = gr.Textbox(label="Added Prompt", value='')
100
  n_prompt = gr.Textbox(label="Negative Prompt",
101
+ value='')
102
  with gr.Column():
103
  result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
104
+
105
  init_ips = [init_img, prompt, a_prompt, n_prompt, num_samples, ddim_steps, guess_mode, strength, scale, seed, eta]
106
  sketch_ips = [sketch_img, prompt, a_prompt, n_prompt, num_samples, ddim_steps, guess_mode, strength, scale, seed, eta]
107
  init_run_button.click(fn=model_sample, inputs=init_ips, outputs=[result_gallery])
108
  sketch_run_button.click(fn=model_sample, inputs=sketch_ips, outputs=[result_gallery])
109
 
110
+ block.launch(server_name='0.0.0.0', server_port=6667, share=True)