muneebable commited on
Commit
6379200
·
verified ·
1 Parent(s): cc1aefd

Update README.md

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