File size: 1,848 Bytes
3d09cb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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)