Ketansomewhere commited on
Commit
d01863a
·
verified ·
1 Parent(s): 520c86d

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +155 -0
README.md ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: afl-3.0
3
+ language:
4
+ - en
5
+ library_name: diffusers
6
+ pipeline_tag: text-to-image
7
+ tags:
8
+ - Class conditioned Diffusion
9
+ - CIFAR10 Diffusion
10
+ ---
11
+ Here is Custom Pipeline for Class conditioned diffusion model. For training script, pipeline, tutorial nb and sampling please check my Github Repo:- https://github.com/KetanMann/Class_Conditioned_Diffusion_Training_Script
12
+ Here is Class Conditional Diffusion Pipeline and Sampling.
13
+
14
+ First Run ``` bash
15
+ !pip install git+https://github.com/huggingface/diffusers
16
+ ```
17
+
18
+ ``` bash
19
+ from diffusers import UNet2DModel, DDPMScheduler
20
+ from diffusers.utils.torch_utils import randn_tensor
21
+ from diffusers.pipelines.pipeline_utils import DiffusionPipeline, ImagePipelineOutput
22
+ from huggingface_hub import hf_hub_download
23
+ import torch
24
+ import os
25
+ from PIL import Image
26
+ import matplotlib.pyplot as plt
27
+ from typing import List, Optional, Tuple, Union
28
+
29
+ class DDPMPipelinenew(DiffusionPipeline):
30
+ def __init__(self, unet, scheduler, num_classes: int):
31
+ super().__init__()
32
+ self.register_modules(unet=unet, scheduler=scheduler)
33
+ self.num_classes = num_classes
34
+ self._device = unet.device # Ensure the pipeline knows the device
35
+
36
+ @torch.no_grad()
37
+ def __call__(
38
+ self,
39
+ batch_size: int = 64,
40
+ class_labels: Optional[torch.Tensor] = None,
41
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
42
+ num_inference_steps: int = 1000,
43
+ output_type: Optional[str] = "pil",
44
+ return_dict: bool = True,
45
+ ) -> Union[ImagePipelineOutput, Tuple]:
46
+
47
+ # Ensure class_labels is on the same device as the model
48
+ class_labels = class_labels.to(self._device)
49
+ if class_labels.ndim == 0:
50
+ class_labels = class_labels.unsqueeze(0).expand(batch_size)
51
+ else:
52
+ class_labels = class_labels.expand(batch_size)
53
+
54
+ # Sample gaussian noise to begin loop
55
+ if isinstance(self.unet.config.sample_size, int):
56
+ image_shape = (
57
+ batch_size,
58
+ self.unet.config.in_channels,
59
+ self.unet.config.sample_size,
60
+ self.unet.config.sample_size,
61
+ )
62
+ else:
63
+ image_shape = (batch_size, self.unet.config.in_channels, *self.unet.config.sample_size)
64
+
65
+ image = randn_tensor(image_shape, generator=generator, device=self._device)
66
+
67
+ # Set step values
68
+ self.scheduler.set_timesteps(num_inference_steps)
69
+
70
+ for t in self.progress_bar(self.scheduler.timesteps):
71
+ # Ensure the class labels are correctly broadcast to match the input tensor shape
72
+ model_output = self.unet(image, t, class_labels).sample
73
+
74
+ image = self.scheduler.step(model_output, t, image, generator=generator).prev_sample
75
+
76
+ image = (image / 2 + 0.5).clamp(0, 1)
77
+ image = image.cpu().permute(0, 2, 3, 1).numpy()
78
+ if output_type == "pil":
79
+ image = self.numpy_to_pil(image)
80
+
81
+ if not return_dict:
82
+ return (image,)
83
+
84
+ return ImagePipelineOutput(images=image)
85
+
86
+ def to(self, device: torch.device):
87
+ self._device = device
88
+ self.unet.to(device)
89
+ return self
90
+
91
+ def load_pipeline(repo_id, num_classes, device):
92
+ unet = UNet2DModel.from_pretrained(repo_id, subfolder="unet").to(device)
93
+ scheduler = DDPMScheduler.from_pretrained(repo_id, subfolder="scheduler")
94
+ pipeline = DDPMPipelinenew(unet=unet, scheduler=scheduler, num_classes=num_classes)
95
+ return pipeline.to(device) # Move the entire pipeline to the device
96
+
97
+ def save_images_locally(images, save_dir, epoch, class_label):
98
+ os.makedirs(save_dir, exist_ok=True)
99
+ for i, image in enumerate(images):
100
+ image_path = os.path.join(save_dir, f"image_epoch{epoch}_class{class_label}_idx{i}.png")
101
+ image.save(image_path)
102
+
103
+ def generate_images(pipeline, class_label, batch_size, num_inference_steps, save_dir, epoch):
104
+ generator = torch.Generator(device=pipeline._device).manual_seed(0)
105
+ class_labels = torch.tensor([class_label] * batch_size).to(pipeline._device)
106
+ images = pipeline(
107
+ generator=generator,
108
+ batch_size=batch_size,
109
+ num_inference_steps=num_inference_steps,
110
+ class_labels=class_labels,
111
+ output_type="pil",
112
+ ).images
113
+ save_images_locally(images, save_dir, epoch, class_label)
114
+ return images
115
+
116
+ def create_image_grid(images, grid_size, save_path):
117
+ assert len(images) == grid_size ** 2, "Number of images must be equal to grid_size squared"
118
+ width, height = images[0].size
119
+ grid_img = Image.new('RGB', (grid_size * width, grid_size * height))
120
+
121
+ for i, image in enumerate(images):
122
+ x = i % grid_size * width
123
+ y = i // grid_size * height
124
+ grid_img.paste(image, (x, y))
125
+
126
+ grid_img.save(save_path)
127
+ return grid_img
128
+
129
+ if __name__ == "__main__":
130
+ repo_id = "Ketansomewhere/cifar10_conditional_diffusion1"
131
+ num_classes = 10 # Adjust to your number of classes
132
+ batch_size = 64
133
+ num_inference_steps = 1000 # Can be as low as 50 for faster generation
134
+ save_dir = "generated_images"
135
+ epoch = 0
136
+ grid_size = 8 # 8x8 grid
137
+
138
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
139
+ pipeline = load_pipeline(repo_id, num_classes, device)
140
+
141
+ for class_label in range(num_classes):
142
+ images = generate_images(pipeline, class_label, batch_size, num_inference_steps, save_dir, epoch)
143
+
144
+ # Create and save the grid image
145
+ grid_img_path = os.path.join(save_dir, f"grid_image_class{class_label}.png")
146
+ grid_img = create_image_grid(images, grid_size, grid_img_path)
147
+
148
+ # Plot the grid image
149
+ plt.figure(figsize=(10, 10))
150
+ plt.imshow(grid_img)
151
+ plt.axis('off')
152
+ plt.title(f'Class {class_label}')
153
+ plt.savefig(os.path.join(save_dir, f"grid_image_class{class_label}.png"))
154
+ plt.show()
155
+ ```