Skip to content

Commit e4efdb5

Browse files
PhaneeshBpowderluv
andauthored
add json data for each image (huggingface#790)
Co-authored-by: powderluv <[email protected]>
1 parent 187f0fa commit e4efdb5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

shark/examples/shark_inference/stable_diffusion/main.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import numpy as np
1818
from random import randint
1919
from stable_args import args
20+
from datetime import datetime as dt
21+
import json
22+
import re
23+
from pathlib import Path
2024

2125
# This has to come before importing cache objects
2226
if args.clear_all:
@@ -250,5 +254,25 @@ def end_profiling(device):
250254
pil_images = [
251255
transform(image) for image in torch.from_numpy(images).to(torch.uint8)
252256
]
257+
258+
if args.output_dir is not None:
259+
output_path = Path(args.output_dir)
260+
output_path.mkdir(parents=True, exist_ok=True)
261+
else:
262+
output_path = Path.cwd()
253263
for i in range(batch_size):
254-
pil_images[i].save(f"{args.prompts[i]}_{i}.jpg")
264+
json_store = {
265+
"prompt": args.prompts[i],
266+
"negative prompt": args.negative_prompts[i],
267+
"seed": args.seed,
268+
"variant": args.variant,
269+
"precision": args.precision,
270+
"steps": args.steps,
271+
"guidance_scale": args.guidance_scale,
272+
"scheduler": args.scheduler,
273+
}
274+
prompt_slice = re.sub("[^a-zA-Z0-9]", "_", args.prompts[i][:15])
275+
img_name = f"{prompt_slice}_{args.seed}_{i}_{dt.now().strftime('%y%m%d_%H%M%S')}"
276+
pil_images[i].save(output_path / f"{img_name}.jpg")
277+
with open(output_path / f"{img_name}.json", "w") as f:
278+
f.write(json.dumps(json_store, indent=4))

shark/examples/shark_inference/stable_diffusion/stable_args.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def path_expand(s):
123123
help="other supported schedulers are [PNDM, DDIM, LMSDiscrete, EulerDiscrete, DPMSolverMultistep]",
124124
)
125125

126+
p.add_argument(
127+
"--output_dir",
128+
type=str,
129+
default=None,
130+
help="Directory path to save the output images and json",
131+
)
126132
##############################################################################
127133
### IREE - Vulkan supported flags
128134
##############################################################################

0 commit comments

Comments
 (0)