MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / sample

Function sample

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

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 1

load_modelMethod · 0.80

Tested by

no test coverage detected