MCPcopy Create free account
hub / github.com/adobe-research/custom-diffusion / sample

Function sample

src/diffusers_sample.py:18–55  ·  view source on GitHub ↗
(ckpt, delta_ckpt, from_file, prompt, compress, batch_size, freeze_model, sdxl=False)

Source from the content-addressed store, hash-verified

16
17
18def sample(ckpt, delta_ckpt, from_file, prompt, compress, batch_size, freeze_model, sdxl=False):
19 model_id = ckpt
20 if sdxl:
21 pipe = CustomDiffusionXLPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
22 else:
23 pipe = CustomDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
24 pipe.load_model(delta_ckpt, compress)
25
26 outdir = os.path.dirname(delta_ckpt)
27 generator = torch.Generator(device='cuda').manual_seed(42)
28
29 all_images = []
30 if prompt is not None:
31 images = pipe([prompt]*batch_size, num_inference_steps=200, guidance_scale=6., eta=1., generator=generator).images
32 all_images += images
33 images = np.hstack([np.array(x) for x in images])
34 images = Image.fromarray(images)
35 # takes only first 50 characters of prompt to name the image file
36 name = '-'.join(prompt[:50].split())
37 images.save(f'{outdir}/{name}.png')
38 else:
39 print(f"reading prompts from {from_file}")
40 with open(from_file, "r") as f:
41 data = f.read().splitlines()
42 data = [[prompt]*batch_size for prompt in data]
43
44 for prompt in data:
45 images = pipe(prompt, num_inference_steps=200, guidance_scale=6., eta=1., generator=generator).images
46 all_images += images
47 images = np.hstack([np.array(x) for x in images], 0)
48 images = Image.fromarray(images)
49 # takes only first 50 characters of prompt to name the image file
50 name = '-'.join(prompt[0][:50].split())
51 images.save(f'{outdir}/{name}.png')
52
53 os.makedirs(f'{outdir}/samples', exist_ok=True)
54 for i, im in enumerate(all_images):
55 im.save(f'{outdir}/samples/{i}.jpg')
56
57
58def parse_args():

Callers 1

Calls 1

load_modelMethod · 0.45

Tested by

no test coverage detected