Skip to content

Commit 798f231

Browse files
[SD] Update metadata info and canvas size (huggingface#1109)
* [SD] Save missing metadata in case of img2img and outpaint Signed-Off-by: Gaurav Shukla <[email protected]> * [SD] Update the canvas size for inpaint/outpaint Signed-Off-by: Gaurav Shukla <[email protected]> * [SD] Update output gallery on each inference Signed-Off-by: Gaurav Shukla <[email protected]> --------- Signed-off-by: Gaurav Shukla <[email protected]>
1 parent 7136890 commit 798f231

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

apps/stable_diffusion/scripts/img2img.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def img2img_inf(
146146
generated_imgs = []
147147
seeds = []
148148
img_seed = utils.sanitize_seed(seed)
149+
extra_info = {"STRENGTH": strength}
149150
for current_batch in range(batch_count):
150151
if current_batch > 0:
151152
img_seed = utils.sanitize_seed(-1)
@@ -165,7 +166,7 @@ def img2img_inf(
165166
args.use_base_vae,
166167
cpu_scheduling,
167168
)
168-
save_output_img(out_imgs[0], img_seed)
169+
save_output_img(out_imgs[0], img_seed, extra_info)
169170
generated_imgs.extend(out_imgs)
170171
seeds.append(img_seed)
171172
img2img_obj.log += "\n"
@@ -260,5 +261,6 @@ def img2img_inf(
260261
text_output += img2img_obj.log
261262
text_output += f"\nTotal image generation time: {total_time:.4f}sec"
262263

263-
save_output_img(generated_imgs[0], seed)
264+
extra_info = {"STRENGTH": args.strength}
265+
save_output_img(generated_imgs[0], seed, extra_info)
264266
print(text_output)

apps/stable_diffusion/scripts/outpaint.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Config:
3535
def outpaint_inf(
3636
prompt: str,
3737
negative_prompt: str,
38-
init_image: str,
38+
init_image: Image,
3939
pixels: int,
4040
mask_blur: int,
4141
directions: list,
@@ -66,7 +66,7 @@ def outpaint_inf(
6666
args.guidance_scale = guidance_scale
6767
args.steps = steps
6868
args.scheduler = scheduler
69-
args.img_path = init_image
69+
args.img_path = "not none"
7070

7171
# set ckpt_loc and hf_model_id.
7272
types = (
@@ -143,7 +143,6 @@ def outpaint_inf(
143143
generated_imgs = []
144144
seeds = []
145145
img_seed = utils.sanitize_seed(seed)
146-
image = Image.open(args.img_path)
147146

148147
left = True if "left" in directions else False
149148
right = True if "right" in directions else False
@@ -156,7 +155,7 @@ def outpaint_inf(
156155
out_imgs = outpaint_obj.generate_images(
157156
prompt,
158157
negative_prompt,
159-
image,
158+
init_image,
160159
pixels,
161160
mask_blur,
162161
left,
@@ -271,5 +270,22 @@ def outpaint_inf(
271270
text_output += outpaint_obj.log
272271
text_output += f"\nTotal image generation time: {total_time:.4f}sec"
273272

274-
save_output_img(generated_imgs[0], seed)
273+
# save this information as metadata of output generated image.
274+
directions = []
275+
if args.left:
276+
directions.append("left")
277+
if args.right:
278+
directions.append("right")
279+
if args.top:
280+
directions.append("up")
281+
if args.bottom:
282+
directions.append("down")
283+
extra_info = {
284+
"PIXELS": args.pixels,
285+
"MASK_BLUR": args.mask_blur,
286+
"DIRECTIONS": directions,
287+
"NOISE_Q": args.noise_q,
288+
"COLOR_VARIATION": args.color_variation,
289+
}
290+
save_output_img(generated_imgs[0], seed, extra_info)
275291
print(text_output)

apps/stable_diffusion/scripts/txt2img.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def txt2img_inf(
158158
generated_imgs.extend(out_imgs)
159159
seeds.append(img_seed)
160160
txt2img_obj.log += "\n"
161+
yield generated_imgs, txt2img_obj.log
161162

162163
total_time = time.time() - start_time
163164
text_output = f"prompt={args.prompts}"
@@ -168,10 +169,10 @@ def txt2img_inf(
168169
f"\nsteps={steps}, guidance_scale={guidance_scale}, seed={seeds}"
169170
)
170171
text_output += f"\nsize={height}x{width}, batch_count={batch_count}, batch_size={batch_size}, max_length={args.max_length}"
171-
text_output += txt2img_obj.log
172+
# text_output += txt2img_obj.log
172173
text_output += f"\nTotal image generation time: {total_time:.4f}sec"
173174

174-
return generated_imgs, text_output
175+
yield generated_imgs, text_output
175176

176177

177178
if __name__ == "__main__":

apps/stable_diffusion/src/utils/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def clear_all():
551551

552552

553553
# save output images and the inputs corresponding to it.
554-
def save_output_img(output_img, img_seed):
554+
def save_output_img(output_img, img_seed, extra_info={}):
555555
output_path = args.output_dir if args.output_dir else Path.cwd()
556556
generated_imgs_path = Path(
557557
output_path, "generated_imgs", dt.now().strftime("%Y%m%d")
@@ -604,6 +604,8 @@ def save_output_img(output_img, img_seed):
604604
"OUTPUT": out_img_path,
605605
}
606606

607+
new_entry.update(extra_info)
608+
607609
with open(csv_path, "a") as csv_obj:
608610
dictwriter_obj = DictWriter(csv_obj, fieldnames=list(new_entry.keys()))
609611
dictwriter_obj.writerow(new_entry)

apps/stable_diffusion/web/ui/img2img_ui.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@
7575
elem_id="negative_prompt_box",
7676
)
7777

78-
init_image = gr.Image(label="Input Image", type="pil")
78+
init_image = gr.Image(label="Input Image", type="pil").style(
79+
height=300, full_width=True
80+
)
7981

8082
with gr.Accordion(label="Advanced Options", open=False):
8183
with gr.Row():

apps/stable_diffusion/web/ui/inpaint_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
source="upload",
7777
tool="sketch",
7878
type="pil",
79-
)
79+
).style(height=350, full_width=True)
8080

8181
with gr.Accordion(label="Advanced Options", open=False):
8282
with gr.Row():

apps/stable_diffusion/web/ui/outpaint_ui.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
elem_id="negative_prompt_box",
7272
)
7373

74-
init_image = gr.Image(label="Input Image", type="filepath")
74+
init_image = gr.Image(label="Input Image", type="pil").style(
75+
height=300, full_width=True
76+
)
7577

7678
with gr.Accordion(label="Advanced Options", open=False):
7779
with gr.Row():

0 commit comments

Comments
 (0)