Update README.md
Browse files
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 |
-
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
plt.show()
|
| 75 |
|
| 76 |
# Example of loading a model and generating predictions
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 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 |
```
|