from diffusers import DDPMPipeline from diffusers.utils import make_image_grid import torch from PIL import Image import os from dataclasses import dataclass @dataclass class TrainingConfig: image_size = 256 # the generated image resolution mixed_precision = "fp16" # `no` for float32, `fp16` for automatic mixed precision output_dir = "skin_lesion_cancer_diffusion_256_V2" # the model name locally and on the HF Hub saved_image_dir="diffusion_generated_image_256" seed = 0 config = TrainingConfig() # Define the path to the saved model saved_model_path = config.output_dir # torch.set_default_dtype(torch.float16) # Load the model from the saved folder # pipeline = DDPMPipeline.from_pretrained(saved_model_path,torch_dtype=torch.float16) pipeline = DDPMPipeline.from_pretrained(saved_model_path) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") pipeline.to(device) # Set the seed for reproducibility generator = torch.manual_seed(config.seed) # Generate images def generate_images(): images = pipeline( batch_size=1, generator=generator ).images return images[0] # Save the images def save_images(image, num): os.makedirs(config.saved_image_dir, exist_ok=True) image.save(os.path.join(config.saved_image_dir, f"{num}.png")) for x in range(1000): image=generate_images() save_images(image,x) # Save the generated images # save_images(generated_images, config.saved_image_dir) # import gradio as gr # with gr.Blocks() as demo: # gr.Markdown("Start typing below and then click **Run** to see the output.") # with gr.Row(): # img=gr.Image(type="pil",height=256,width=256) # with gr.Row(): # btn = gr.Button("Generate image") # btn.click(fn=generate_images, inputs=None, outputs=img) # demo.launch(share=True)