|
| 1 | +import deepspeed |
| 2 | +import torch |
| 3 | +import os |
| 4 | +from local_pipeline_stable_diffusion import StableDiffusionPipeline |
| 5 | +from diffusers import StableDiffusionPipeline as StableDiffusionPipelineBaseline |
| 6 | +import argparse |
| 7 | + |
| 8 | +seed = 123450011 |
| 9 | +parser = argparse.ArgumentParser() |
| 10 | +parser.add_argument("--ft_model", default="new_sd-distill-v21-10k-1e", type=str, help="Path to the fine-tuned model") |
| 11 | +parser.add_argument("--b_model", default="stabilityai/stable-diffusion-2-1-base", type=str, help="Path to the baseline model") |
| 12 | +parser.add_argument("--out_dir", default="image_out/", type=str, help="Path to the generated images") |
| 13 | +parser.add_argument('--guidance_scale', type=float, default=7.5, help='Guidance Scale') |
| 14 | +parser.add_argument("--use_local_pipe", action='store_true', help="Use local SD pipeline") |
| 15 | +parser.add_argument("--local_rank", type=int, default=int(os.getenv("LOCAL_RANK", "0")), help="local rank") |
| 16 | +args = parser.parse_args() |
| 17 | + |
| 18 | + |
| 19 | +local_rank = int(os.getenv("LOCAL_RANK", "0")) |
| 20 | +device = torch.device(f"cuda:{local_rank}") |
| 21 | +world_size = int(os.getenv('WORLD_SIZE', '1')) |
| 22 | + |
| 23 | + |
| 24 | +if not os.path.exists(args.out_dir): |
| 25 | + os.makedirs(args.out_dir) |
| 26 | + print(f"Directory '{args.out_dir}' has been created to store the generated images.") |
| 27 | +else: |
| 28 | + print(f"Directory '{args.out_dir}' already exists and stores the generated images.") |
| 29 | + |
| 30 | + |
| 31 | +prompts = ["A boy is watching TV", |
| 32 | + "A photo of a person dancing in the rain", |
| 33 | + "A photo of a boy jumping over a fence", |
| 34 | + "A photo of a boy is kicking a ball", |
| 35 | + "A beach with a lot of waves on it", |
| 36 | + "A road that is going down a hill", |
| 37 | + "3d rendering of 5 tennis balls on top of a cake", |
| 38 | + "A person holding a drink of soda", |
| 39 | + "A person is squeezing a lemon", |
| 40 | + "A person holding a cat"] |
| 41 | + |
| 42 | + |
| 43 | +for prompt in prompts: |
| 44 | + #--- new image |
| 45 | + pipe_new = StableDiffusionPipeline.from_pretrained(args.ft_model, torch_dtype=torch.float16).to("cuda") |
| 46 | + generator = torch.Generator("cuda").manual_seed(seed) |
| 47 | + pipe_new = deepspeed.init_inference(pipe_new, mp_size=world_size, dtype=torch.half) |
| 48 | + image_new = pipe_new(prompt, num_inference_steps=50, guidance_scale=args.guidance_scale, generator=generator).images[0] |
| 49 | + image_new.save(args.out_dir+"/NEW__seed_"+str(seed)+"_"+prompt[0:100]+".png") |
| 50 | + |
| 51 | + #--- baseline image |
| 52 | + pipe_baseline = StableDiffusionPipelineBaseline.from_pretrained(args.b_model, torch_dtype=torch.float16).to("cuda") |
| 53 | + generator = torch.Generator("cuda").manual_seed(seed) |
| 54 | + pipe_baseline = deepspeed.init_inference(pipe_baseline, mp_size=world_size, dtype=torch.half) |
| 55 | + image_baseline = pipe_baseline(prompt, num_inference_steps=50, guidance_scale=args.guidance_scale, generator=generator).images[0] |
| 56 | + image_baseline.save(args.out_dir+"/BASELINE_seed_"+str(seed)+"_"+prompt[0:100]+".png") |
0 commit comments