muneebable commited on
Commit
80fedff
1 Parent(s): 83bc592

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +60 -60
README.md CHANGED
@@ -17,69 +17,69 @@ library_name: diffusers
17
 
18
  A Diffusion model on Cub 200 dataset for generating bird images.
19
 
20
- ## Usage
21
  ```python
22
 
23
- # Predict function to generate images
24
-
25
- def load_model(model_path, device):
26
- # Initialize the same model architecture as during training
27
- model = ClassConditionedUnet().to(device)
28
-
29
- # Load the trained weights
30
- model.load_state_dict(torch.load(model_path))
31
-
32
- # Set model to evaluation mode
33
- model.eval()
34
-
35
- return model
36
-
37
-
38
- def predict(model, class_label, noise_scheduler, num_samples=8, device='cuda'):
39
- model.eval() # Ensure the model is in evaluation mode
40
-
41
- # Prepare a batch of random noise as input
42
- shape = (num_samples, 3, 256, 256) # Input shape: (batch_size, channels, height, width)
43
- noisy_image = torch.randn(shape).to(device)
44
-
45
- # Ensure class_label is a tensor and properly repeated for the batch
46
- class_labels = torch.tensor([class_label] * num_samples, dtype=torch.long).to(device)
47
-
48
- # Reverse the diffusion process step by step
49
- for t in tqdm(range(49, -1, -1), desc="Reverse Diffusion Steps"): # Iterate backwards through timesteps
50
- t_tensor = torch.tensor([t], dtype=torch.long).to(device) # Single time step for the batch
51
-
52
- # Predict noise with the model and remove it from the image
53
- with torch.no_grad():
54
- noise_pred = model(noisy_image, t_tensor.expand(num_samples), class_labels) # Class conditioning here
55
-
56
- # Step with the scheduler (model_output, timestep, sample)
57
- noisy_image = noise_scheduler.step(noise_pred, t, noisy_image).prev_sample
58
-
59
- # Post-process the output to get image values between [0, 1]
60
- generated_images = (noisy_image + 1) / 2 # Rescale from [-1, 1] to [0, 1]
61
-
62
- return generated_images
63
-
64
-
65
- def display_images(images, num_rows=2):
66
- # Create a grid of images
67
- grid = torchvision.utils.make_grid(images, nrow=num_rows)
68
- np_grid = grid.permute(1, 2, 0).cpu().numpy() # Convert to (H, W, C) format for visualization
69
-
70
- # Plot the images
71
- plt.figure(figsize=(12, 6))
72
- plt.imshow(np.clip(np_grid, 0, 1)) # Clip values to ensure valid range
73
- plt.axis('off')
74
- plt.show()
75
 
76
  # Example of loading a model and generating predictions
77
 
78
- model_path = "model_epoch_0.pth" # Path to your saved model
79
- device = 'cuda' if torch.cuda.is_available() else 'cpu'
80
- model = load_model(model_path, device)
81
- noise_scheduler = DDPMScheduler(num_train_timesteps=1000, beta_schedule='squaredcos_cap_v2')
82
- class_label = 1 # Example class label, change to your desired class
83
- generated_images = predict(model, class_label, noise_scheduler, num_samples=2, device=device)
84
- display_images(generated_images)
 
85
  ```
 
17
 
18
  A Diffusion model on Cub 200 dataset for generating bird images.
19
 
20
+ ## Usage Predict function to generate images
21
  ```python
22
 
23
+ def load_model(model_path, device):
24
+ # Initialize the same model architecture as during training
25
+ model = ClassConditionedUnet().to(device)
26
+
27
+ # Load the trained weights
28
+ model.load_state_dict(torch.load(model_path))
29
+
30
+ # Set model to evaluation mode
31
+ model.eval()
32
+
33
+ return model
34
+
35
+
36
+ def predict(model, class_label, noise_scheduler, num_samples=8, device='cuda'):
37
+ model.eval() # Ensure the model is in evaluation mode
38
+
39
+ # Prepare a batch of random noise as input
40
+ shape = (num_samples, 3, 256, 256) # Input shape: (batch_size, channels, height, width)
41
+ noisy_image = torch.randn(shape).to(device)
42
+
43
+ # Ensure class_label is a tensor and properly repeated for the batch
44
+ class_labels = torch.tensor([class_label] * num_samples, dtype=torch.long).to(device)
45
+
46
+ # Reverse the diffusion process step by step
47
+ for t in tqdm(range(49, -1, -1), desc="Reverse Diffusion Steps"): # Iterate backwards through timesteps
48
+ t_tensor = torch.tensor([t], dtype=torch.long).to(device) # Single time step for the batch
49
+
50
+ # Predict noise with the model and remove it from the image
51
+ with torch.no_grad():
52
+ noise_pred = model(noisy_image, t_tensor.expand(num_samples), class_labels) # Class conditioning here
53
+
54
+ # Step with the scheduler (model_output, timestep, sample)
55
+ noisy_image = noise_scheduler.step(noise_pred, t, noisy_image).prev_sample
56
+
57
+ # Post-process the output to get image values between [0, 1]
58
+ generated_images = (noisy_image + 1) / 2 # Rescale from [-1, 1] to [0, 1]
59
+
60
+ return generated_images
61
+
62
+
63
+ def display_images(images, num_rows=2):
64
+ # Create a grid of images
65
+ grid = torchvision.utils.make_grid(images, nrow=num_rows)
66
+ np_grid = grid.permute(1, 2, 0).cpu().numpy() # Convert to (H, W, C) format for visualization
67
+
68
+ # Plot the images
69
+ plt.figure(figsize=(12, 6))
70
+ plt.imshow(np.clip(np_grid, 0, 1)) # Clip values to ensure valid range
71
+ plt.axis('off')
72
+ plt.show()
73
+ ```
 
74
 
75
  # Example of loading a model and generating predictions
76
 
77
+ ```python
78
+ model_path = "model_epoch_0.pth" # Path to your saved model
79
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
80
+ model = load_model(model_path, device)
81
+ noise_scheduler = DDPMScheduler(num_train_timesteps=1000, beta_schedule='squaredcos_cap_v2')
82
+ class_label = 1 # Example class label, change to your desired class
83
+ generated_images = predict(model, class_label, noise_scheduler, num_samples=2, device=device)
84
+ display_images(generated_images)
85
  ```