Upload folder using huggingface_hub
Browse files- .gitattributes +3 -0
- .ipynb_checkpoints/Diffusion Models-checkpoint.ipynb +0 -0
- Diffusion Models.ipynb +0 -0
- README.md +261 -0
- README_HF.md +261 -0
- diffusion_logs/sampling_log.json +1 -0
- diffusion_logs/training_log.json +0 -0
- diffusion_model_complete.pth +3 -0
- diffusion_process.png +3 -0
- diffusion_results.png +3 -0
- training_metrics.png +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
diffusion_process.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
diffusion_results.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
training_metrics.png filter=lfs diff=lfs merge=lfs -text
|
.ipynb_checkpoints/Diffusion Models-checkpoint.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Diffusion Models.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
README.md
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Diffusion Models - Complete DDPM Implementation
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: pytorch
|
| 7 |
+
app_file: "Diffusion Models.ipynb"
|
| 8 |
+
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
tags:
|
| 11 |
+
- deep-learning
|
| 12 |
+
- generative-ai
|
| 13 |
+
- pytorch
|
| 14 |
+
- diffusion-models
|
| 15 |
+
- ddpm
|
| 16 |
+
- denoising
|
| 17 |
+
- generative-modeling
|
| 18 |
+
- computer-vision
|
| 19 |
+
- unsupervised-learning
|
| 20 |
+
datasets:
|
| 21 |
+
- synthetic-2d-data
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# Diffusion Models: Complete DDPM Implementation
|
| 25 |
+
|
| 26 |
+
A comprehensive PyTorch implementation of Denoising Diffusion Probabilistic Models (DDPM) with detailed mathematical foundations and educational content.
|
| 27 |
+
|
| 28 |
+
## Model Description
|
| 29 |
+
|
| 30 |
+
This repository contains a complete implementation of Diffusion Models (DDPM) trained on 2D synthetic datasets. The model learns to generate new data points by mastering the art of noise removal through a reverse diffusion process. This implementation serves as both a working model and an educational resource for understanding the mathematics and implementation of diffusion models.
|
| 31 |
+
|
| 32 |
+
### Architecture Details
|
| 33 |
+
|
| 34 |
+
- **Model Type**: Denoising Diffusion Probabilistic Model (DDPM)
|
| 35 |
+
- **Framework**: PyTorch
|
| 36 |
+
- **Input**: 2D point coordinates
|
| 37 |
+
- **Diffusion Steps**: 1000 timesteps
|
| 38 |
+
- **Hidden Dimensions**: 256 units with SiLU activations
|
| 39 |
+
- **Time Embedding**: 64-dimensional rich representations
|
| 40 |
+
- **Total Parameters**: ~130K
|
| 41 |
+
- **Model Size**: 1.8MB
|
| 42 |
+
|
| 43 |
+
### Key Components
|
| 44 |
+
|
| 45 |
+
1. **Noise Predictor Network**: Neural network that predicts noise Ξ΅_ΞΈ(x_t, t)
|
| 46 |
+
2. **Forward Diffusion Process**: Gradually adds Gaussian noise over T steps
|
| 47 |
+
3. **Reverse Diffusion Process**: Iteratively removes noise to generate samples
|
| 48 |
+
4. **Time Embedding Module**: Converts timesteps to rich feature representations
|
| 49 |
+
|
| 50 |
+
## Training Details
|
| 51 |
+
|
| 52 |
+
- **Dataset**: Synthetic 2D point clusters
|
| 53 |
+
- **Diffusion Steps**: 1000
|
| 54 |
+
- **Beta Schedule**: Linear (0.0001 to 0.02)
|
| 55 |
+
- **Optimizer**: AdamW with cosine annealing
|
| 56 |
+
- **Learning Rate**: 0.001
|
| 57 |
+
- **Training Epochs**: 2000
|
| 58 |
+
- **Batch Processing**: Dynamic batching for efficient training
|
| 59 |
+
|
| 60 |
+
## Mathematical Foundation
|
| 61 |
+
|
| 62 |
+
### Forward Process
|
| 63 |
+
The forward process adds noise according to:
|
| 64 |
+
```
|
| 65 |
+
q(x_t | x_{t-1}) = N(x_t; β(1-Ξ²_t) x_{t-1}, Ξ²_t I)
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
With direct sampling:
|
| 69 |
+
```
|
| 70 |
+
x_t = βαΎ±_t x_0 + β(1-αΎ±_t) Ξ΅
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### Reverse Process
|
| 74 |
+
The model learns to reverse noise:
|
| 75 |
+
```
|
| 76 |
+
p_ΞΈ(x_{t-1} | x_t) = N(x_{t-1}; ΞΌ_ΞΈ(x_t, t), Ξ£_ΞΈ(x_t, t))
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
### Loss Function
|
| 80 |
+
Trained by minimizing noise prediction error:
|
| 81 |
+
```
|
| 82 |
+
L = E[||Ξ΅ - Ξ΅_ΞΈ(x_t, t)||Β²]
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
## Model Performance
|
| 86 |
+
|
| 87 |
+
### Training Metrics
|
| 88 |
+
- **Final Training Loss**: Converged to stable low values
|
| 89 |
+
- **Training Time**: ~30 minutes on GPU
|
| 90 |
+
- **Memory Usage**: <500MB GPU memory
|
| 91 |
+
- **Convergence**: Stable training without mode collapse
|
| 92 |
+
|
| 93 |
+
### Capabilities
|
| 94 |
+
- β
High-quality 2D point generation
|
| 95 |
+
- β
Smooth interpolation in data space
|
| 96 |
+
- β
Stable training without adversarial dynamics
|
| 97 |
+
- β
Mathematically grounded approach
|
| 98 |
+
- β
Excellent sample diversity
|
| 99 |
+
|
| 100 |
+
## Usage
|
| 101 |
+
|
| 102 |
+
### Quick Start
|
| 103 |
+
|
| 104 |
+
```python
|
| 105 |
+
import torch
|
| 106 |
+
import torch.nn as nn
|
| 107 |
+
import matplotlib.pyplot as plt
|
| 108 |
+
|
| 109 |
+
# Load the model components (full implementation in notebook)
|
| 110 |
+
class NoisePredictor(nn.Module):
|
| 111 |
+
def __init__(self, data_dim=2, hidden_dim=256, time_embed_dim=64):
|
| 112 |
+
super(NoisePredictor, self).__init__()
|
| 113 |
+
# ... (complete implementation in notebook)
|
| 114 |
+
|
| 115 |
+
def forward(self, x, t):
|
| 116 |
+
# ... (complete implementation in notebook)
|
| 117 |
+
return noise_prediction
|
| 118 |
+
|
| 119 |
+
class DiffusionModel:
|
| 120 |
+
def __init__(self, T=1000, beta_start=0.0001, beta_end=0.02):
|
| 121 |
+
# ... (complete implementation in notebook)
|
| 122 |
+
|
| 123 |
+
def sample(self, n_samples=100):
|
| 124 |
+
# Generate new samples from pure noise
|
| 125 |
+
# ... (complete implementation in notebook)
|
| 126 |
+
return generated_samples
|
| 127 |
+
|
| 128 |
+
# Load trained model
|
| 129 |
+
model = DiffusionModel()
|
| 130 |
+
# Load weights: model.model.load_state_dict(torch.load('diffusion_model_complete.pth'))
|
| 131 |
+
|
| 132 |
+
# Generate new samples
|
| 133 |
+
samples = model.sample(n_samples=100)
|
| 134 |
+
plt.scatter(samples[:, 0], samples[:, 1])
|
| 135 |
+
plt.title("Generated 2D Points")
|
| 136 |
+
plt.show()
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
### Advanced Usage
|
| 140 |
+
|
| 141 |
+
```python
|
| 142 |
+
# Visualize the diffusion process
|
| 143 |
+
model.visualize_diffusion_process()
|
| 144 |
+
|
| 145 |
+
# Monitor training progress
|
| 146 |
+
model.plot_training_curves()
|
| 147 |
+
|
| 148 |
+
# Sample with different parameters
|
| 149 |
+
high_quality_samples = model.sample(n_samples=500, guidance_scale=1.0)
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
## Visualizations Available
|
| 153 |
+
|
| 154 |
+
1. **Diffusion Process**: Step-by-step noise addition and removal
|
| 155 |
+
2. **Training Curves**: Loss evolution and learning dynamics
|
| 156 |
+
3. **Generated Samples**: Comparison with original data distribution
|
| 157 |
+
4. **Sampling Process**: Real-time generation visualization
|
| 158 |
+
5. **Parameter Analysis**: Beta schedule and noise analysis
|
| 159 |
+
|
| 160 |
+
## Files and Outputs
|
| 161 |
+
|
| 162 |
+
- `Diffusion Models.ipynb`: Complete implementation with educational content
|
| 163 |
+
- `diffusion_model_complete.pth`: Trained model weights
|
| 164 |
+
- `diffusion_process.png`: Visualization of forward and reverse processes
|
| 165 |
+
- `diffusion_results.png`: Generated samples and quality assessment
|
| 166 |
+
- `training_metrics.png`: Comprehensive training analytics
|
| 167 |
+
- `diffusion_logs/`: Detailed training and sampling logs
|
| 168 |
+
|
| 169 |
+
## Applications
|
| 170 |
+
|
| 171 |
+
This diffusion model implementation can be adapted for:
|
| 172 |
+
|
| 173 |
+
- **Image Generation**: Extend to pixel-based image synthesis
|
| 174 |
+
- **Audio Synthesis**: Apply to waveform or spectrogram generation
|
| 175 |
+
- **3D Point Clouds**: Generate 3D shapes and objects
|
| 176 |
+
- **Time Series**: Financial data, sensor readings, weather patterns
|
| 177 |
+
- **Scientific Data**: Molecular structures, particle physics
|
| 178 |
+
- **Data Augmentation**: Synthetic training data creation
|
| 179 |
+
|
| 180 |
+
## Educational Value
|
| 181 |
+
|
| 182 |
+
This implementation is designed as a learning resource featuring:
|
| 183 |
+
|
| 184 |
+
- **Complete Mathematical Derivations**: From first principles to implementation
|
| 185 |
+
- **Step-by-Step Explanations**: Every component explained in detail
|
| 186 |
+
- **Visual Learning**: Rich plots and animations for understanding
|
| 187 |
+
- **Progressive Complexity**: Build understanding gradually
|
| 188 |
+
- **Practical Implementation**: Real working code with best practices
|
| 189 |
+
|
| 190 |
+
## Research Applications
|
| 191 |
+
|
| 192 |
+
The model demonstrates key concepts in:
|
| 193 |
+
|
| 194 |
+
- **Generative Modeling**: Alternative to GANs and VAEs
|
| 195 |
+
- **Probability Theory**: Markov chains and stochastic processes
|
| 196 |
+
- **Neural Network Architecture**: Time conditioning and embeddings
|
| 197 |
+
- **Optimization**: Stable training of generative models
|
| 198 |
+
- **Sampling Methods**: DDPM and potential DDIM extensions
|
| 199 |
+
|
| 200 |
+
## Comparison with Other Generative Models
|
| 201 |
+
|
| 202 |
+
### Advantages over GANs
|
| 203 |
+
- β
Stable training (no adversarial dynamics)
|
| 204 |
+
- β
No mode collapse
|
| 205 |
+
- β
Mathematical foundation
|
| 206 |
+
- β
High-quality samples
|
| 207 |
+
|
| 208 |
+
### Advantages over VAEs
|
| 209 |
+
- β
Higher sample quality
|
| 210 |
+
- β
No posterior collapse
|
| 211 |
+
- β
Better likelihood estimates
|
| 212 |
+
- β
Flexible architectures
|
| 213 |
+
|
| 214 |
+
### Trade-offs
|
| 215 |
+
- β οΈ Slower sampling (requires multiple steps)
|
| 216 |
+
- β οΈ More computationally intensive
|
| 217 |
+
- β οΈ Memory requirements for long sequences
|
| 218 |
+
|
| 219 |
+
## Citation
|
| 220 |
+
|
| 221 |
+
If you use this implementation in your research or projects, please cite:
|
| 222 |
+
|
| 223 |
+
```bibtex
|
| 224 |
+
@misc{ddpm_implementation_2024,
|
| 225 |
+
title={Complete DDPM Implementation: Educational Diffusion Models},
|
| 226 |
+
author={Gruhesh Kurra},
|
| 227 |
+
year={2024},
|
| 228 |
+
url={https://huggingface.co/karthik-2905/DiffusionModels}
|
| 229 |
+
}
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
## Future Extensions
|
| 233 |
+
|
| 234 |
+
Planned improvements and extensions:
|
| 235 |
+
|
| 236 |
+
- π **DDIM Implementation**: Faster sampling with deterministic steps
|
| 237 |
+
- π¨ **Conditional Generation**: Text-guided or class-conditional generation
|
| 238 |
+
- π **Alternative Schedules**: Cosine and sigmoid beta schedules
|
| 239 |
+
- πΌοΈ **Image Diffusion**: Extension to CIFAR-10 and other image datasets
|
| 240 |
+
- π΅ **Audio Applications**: Waveform and spectrogram generation
|
| 241 |
+
- 𧬠**Scientific Applications**: Molecular and protein structure generation
|
| 242 |
+
|
| 243 |
+
## License
|
| 244 |
+
|
| 245 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 246 |
+
|
| 247 |
+
## Additional Resources
|
| 248 |
+
|
| 249 |
+
- **GitHub Repository**: [DiffusionModels](https://github.com/GruheshKurra/DiffusionModels)
|
| 250 |
+
- **Detailed Notebook**: Complete implementation with educational content
|
| 251 |
+
- **Training Logs**: Comprehensive metrics and analysis
|
| 252 |
+
|
| 253 |
+
## Model Card Authors
|
| 254 |
+
|
| 255 |
+
**Gruhesh Kurra** - Implementation, documentation, and educational content
|
| 256 |
+
|
| 257 |
+
---
|
| 258 |
+
|
| 259 |
+
**Tags**: diffusion-models, generative-ai, pytorch, ddpm, deep-learning, denoising
|
| 260 |
+
|
| 261 |
+
**Model Card Last Updated**: December 2024
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Diffusion Models - Complete DDPM Implementation
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: pytorch
|
| 7 |
+
app_file: "Diffusion Models.ipynb"
|
| 8 |
+
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
tags:
|
| 11 |
+
- deep-learning
|
| 12 |
+
- generative-ai
|
| 13 |
+
- pytorch
|
| 14 |
+
- diffusion-models
|
| 15 |
+
- ddpm
|
| 16 |
+
- denoising
|
| 17 |
+
- generative-modeling
|
| 18 |
+
- computer-vision
|
| 19 |
+
- unsupervised-learning
|
| 20 |
+
datasets:
|
| 21 |
+
- synthetic-2d-data
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# Diffusion Models: Complete DDPM Implementation
|
| 25 |
+
|
| 26 |
+
A comprehensive PyTorch implementation of Denoising Diffusion Probabilistic Models (DDPM) with detailed mathematical foundations and educational content.
|
| 27 |
+
|
| 28 |
+
## Model Description
|
| 29 |
+
|
| 30 |
+
This repository contains a complete implementation of Diffusion Models (DDPM) trained on 2D synthetic datasets. The model learns to generate new data points by mastering the art of noise removal through a reverse diffusion process. This implementation serves as both a working model and an educational resource for understanding the mathematics and implementation of diffusion models.
|
| 31 |
+
|
| 32 |
+
### Architecture Details
|
| 33 |
+
|
| 34 |
+
- **Model Type**: Denoising Diffusion Probabilistic Model (DDPM)
|
| 35 |
+
- **Framework**: PyTorch
|
| 36 |
+
- **Input**: 2D point coordinates
|
| 37 |
+
- **Diffusion Steps**: 1000 timesteps
|
| 38 |
+
- **Hidden Dimensions**: 256 units with SiLU activations
|
| 39 |
+
- **Time Embedding**: 64-dimensional rich representations
|
| 40 |
+
- **Total Parameters**: ~130K
|
| 41 |
+
- **Model Size**: 1.8MB
|
| 42 |
+
|
| 43 |
+
### Key Components
|
| 44 |
+
|
| 45 |
+
1. **Noise Predictor Network**: Neural network that predicts noise Ξ΅_ΞΈ(x_t, t)
|
| 46 |
+
2. **Forward Diffusion Process**: Gradually adds Gaussian noise over T steps
|
| 47 |
+
3. **Reverse Diffusion Process**: Iteratively removes noise to generate samples
|
| 48 |
+
4. **Time Embedding Module**: Converts timesteps to rich feature representations
|
| 49 |
+
|
| 50 |
+
## Training Details
|
| 51 |
+
|
| 52 |
+
- **Dataset**: Synthetic 2D point clusters
|
| 53 |
+
- **Diffusion Steps**: 1000
|
| 54 |
+
- **Beta Schedule**: Linear (0.0001 to 0.02)
|
| 55 |
+
- **Optimizer**: AdamW with cosine annealing
|
| 56 |
+
- **Learning Rate**: 0.001
|
| 57 |
+
- **Training Epochs**: 2000
|
| 58 |
+
- **Batch Processing**: Dynamic batching for efficient training
|
| 59 |
+
|
| 60 |
+
## Mathematical Foundation
|
| 61 |
+
|
| 62 |
+
### Forward Process
|
| 63 |
+
The forward process adds noise according to:
|
| 64 |
+
```
|
| 65 |
+
q(x_t | x_{t-1}) = N(x_t; β(1-Ξ²_t) x_{t-1}, Ξ²_t I)
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
With direct sampling:
|
| 69 |
+
```
|
| 70 |
+
x_t = βαΎ±_t x_0 + β(1-αΎ±_t) Ξ΅
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### Reverse Process
|
| 74 |
+
The model learns to reverse noise:
|
| 75 |
+
```
|
| 76 |
+
p_ΞΈ(x_{t-1} | x_t) = N(x_{t-1}; ΞΌ_ΞΈ(x_t, t), Ξ£_ΞΈ(x_t, t))
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
### Loss Function
|
| 80 |
+
Trained by minimizing noise prediction error:
|
| 81 |
+
```
|
| 82 |
+
L = E[||Ξ΅ - Ξ΅_ΞΈ(x_t, t)||Β²]
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
## Model Performance
|
| 86 |
+
|
| 87 |
+
### Training Metrics
|
| 88 |
+
- **Final Training Loss**: Converged to stable low values
|
| 89 |
+
- **Training Time**: ~30 minutes on GPU
|
| 90 |
+
- **Memory Usage**: <500MB GPU memory
|
| 91 |
+
- **Convergence**: Stable training without mode collapse
|
| 92 |
+
|
| 93 |
+
### Capabilities
|
| 94 |
+
- β
High-quality 2D point generation
|
| 95 |
+
- β
Smooth interpolation in data space
|
| 96 |
+
- β
Stable training without adversarial dynamics
|
| 97 |
+
- β
Mathematically grounded approach
|
| 98 |
+
- β
Excellent sample diversity
|
| 99 |
+
|
| 100 |
+
## Usage
|
| 101 |
+
|
| 102 |
+
### Quick Start
|
| 103 |
+
|
| 104 |
+
```python
|
| 105 |
+
import torch
|
| 106 |
+
import torch.nn as nn
|
| 107 |
+
import matplotlib.pyplot as plt
|
| 108 |
+
|
| 109 |
+
# Load the model components (full implementation in notebook)
|
| 110 |
+
class NoisePredictor(nn.Module):
|
| 111 |
+
def __init__(self, data_dim=2, hidden_dim=256, time_embed_dim=64):
|
| 112 |
+
super(NoisePredictor, self).__init__()
|
| 113 |
+
# ... (complete implementation in notebook)
|
| 114 |
+
|
| 115 |
+
def forward(self, x, t):
|
| 116 |
+
# ... (complete implementation in notebook)
|
| 117 |
+
return noise_prediction
|
| 118 |
+
|
| 119 |
+
class DiffusionModel:
|
| 120 |
+
def __init__(self, T=1000, beta_start=0.0001, beta_end=0.02):
|
| 121 |
+
# ... (complete implementation in notebook)
|
| 122 |
+
|
| 123 |
+
def sample(self, n_samples=100):
|
| 124 |
+
# Generate new samples from pure noise
|
| 125 |
+
# ... (complete implementation in notebook)
|
| 126 |
+
return generated_samples
|
| 127 |
+
|
| 128 |
+
# Load trained model
|
| 129 |
+
model = DiffusionModel()
|
| 130 |
+
# Load weights: model.model.load_state_dict(torch.load('diffusion_model_complete.pth'))
|
| 131 |
+
|
| 132 |
+
# Generate new samples
|
| 133 |
+
samples = model.sample(n_samples=100)
|
| 134 |
+
plt.scatter(samples[:, 0], samples[:, 1])
|
| 135 |
+
plt.title("Generated 2D Points")
|
| 136 |
+
plt.show()
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
### Advanced Usage
|
| 140 |
+
|
| 141 |
+
```python
|
| 142 |
+
# Visualize the diffusion process
|
| 143 |
+
model.visualize_diffusion_process()
|
| 144 |
+
|
| 145 |
+
# Monitor training progress
|
| 146 |
+
model.plot_training_curves()
|
| 147 |
+
|
| 148 |
+
# Sample with different parameters
|
| 149 |
+
high_quality_samples = model.sample(n_samples=500, guidance_scale=1.0)
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
## Visualizations Available
|
| 153 |
+
|
| 154 |
+
1. **Diffusion Process**: Step-by-step noise addition and removal
|
| 155 |
+
2. **Training Curves**: Loss evolution and learning dynamics
|
| 156 |
+
3. **Generated Samples**: Comparison with original data distribution
|
| 157 |
+
4. **Sampling Process**: Real-time generation visualization
|
| 158 |
+
5. **Parameter Analysis**: Beta schedule and noise analysis
|
| 159 |
+
|
| 160 |
+
## Files and Outputs
|
| 161 |
+
|
| 162 |
+
- `Diffusion Models.ipynb`: Complete implementation with educational content
|
| 163 |
+
- `diffusion_model_complete.pth`: Trained model weights
|
| 164 |
+
- `diffusion_process.png`: Visualization of forward and reverse processes
|
| 165 |
+
- `diffusion_results.png`: Generated samples and quality assessment
|
| 166 |
+
- `training_metrics.png`: Comprehensive training analytics
|
| 167 |
+
- `diffusion_logs/`: Detailed training and sampling logs
|
| 168 |
+
|
| 169 |
+
## Applications
|
| 170 |
+
|
| 171 |
+
This diffusion model implementation can be adapted for:
|
| 172 |
+
|
| 173 |
+
- **Image Generation**: Extend to pixel-based image synthesis
|
| 174 |
+
- **Audio Synthesis**: Apply to waveform or spectrogram generation
|
| 175 |
+
- **3D Point Clouds**: Generate 3D shapes and objects
|
| 176 |
+
- **Time Series**: Financial data, sensor readings, weather patterns
|
| 177 |
+
- **Scientific Data**: Molecular structures, particle physics
|
| 178 |
+
- **Data Augmentation**: Synthetic training data creation
|
| 179 |
+
|
| 180 |
+
## Educational Value
|
| 181 |
+
|
| 182 |
+
This implementation is designed as a learning resource featuring:
|
| 183 |
+
|
| 184 |
+
- **Complete Mathematical Derivations**: From first principles to implementation
|
| 185 |
+
- **Step-by-Step Explanations**: Every component explained in detail
|
| 186 |
+
- **Visual Learning**: Rich plots and animations for understanding
|
| 187 |
+
- **Progressive Complexity**: Build understanding gradually
|
| 188 |
+
- **Practical Implementation**: Real working code with best practices
|
| 189 |
+
|
| 190 |
+
## Research Applications
|
| 191 |
+
|
| 192 |
+
The model demonstrates key concepts in:
|
| 193 |
+
|
| 194 |
+
- **Generative Modeling**: Alternative to GANs and VAEs
|
| 195 |
+
- **Probability Theory**: Markov chains and stochastic processes
|
| 196 |
+
- **Neural Network Architecture**: Time conditioning and embeddings
|
| 197 |
+
- **Optimization**: Stable training of generative models
|
| 198 |
+
- **Sampling Methods**: DDPM and potential DDIM extensions
|
| 199 |
+
|
| 200 |
+
## Comparison with Other Generative Models
|
| 201 |
+
|
| 202 |
+
### Advantages over GANs
|
| 203 |
+
- β
Stable training (no adversarial dynamics)
|
| 204 |
+
- β
No mode collapse
|
| 205 |
+
- β
Mathematical foundation
|
| 206 |
+
- β
High-quality samples
|
| 207 |
+
|
| 208 |
+
### Advantages over VAEs
|
| 209 |
+
- β
Higher sample quality
|
| 210 |
+
- β
No posterior collapse
|
| 211 |
+
- β
Better likelihood estimates
|
| 212 |
+
- β
Flexible architectures
|
| 213 |
+
|
| 214 |
+
### Trade-offs
|
| 215 |
+
- β οΈ Slower sampling (requires multiple steps)
|
| 216 |
+
- β οΈ More computationally intensive
|
| 217 |
+
- β οΈ Memory requirements for long sequences
|
| 218 |
+
|
| 219 |
+
## Citation
|
| 220 |
+
|
| 221 |
+
If you use this implementation in your research or projects, please cite:
|
| 222 |
+
|
| 223 |
+
```bibtex
|
| 224 |
+
@misc{ddpm_implementation_2024,
|
| 225 |
+
title={Complete DDPM Implementation: Educational Diffusion Models},
|
| 226 |
+
author={Gruhesh Kurra},
|
| 227 |
+
year={2024},
|
| 228 |
+
url={https://huggingface.co/karthik-2905/DiffusionModels}
|
| 229 |
+
}
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
## Future Extensions
|
| 233 |
+
|
| 234 |
+
Planned improvements and extensions:
|
| 235 |
+
|
| 236 |
+
- π **DDIM Implementation**: Faster sampling with deterministic steps
|
| 237 |
+
- π¨ **Conditional Generation**: Text-guided or class-conditional generation
|
| 238 |
+
- π **Alternative Schedules**: Cosine and sigmoid beta schedules
|
| 239 |
+
- πΌοΈ **Image Diffusion**: Extension to CIFAR-10 and other image datasets
|
| 240 |
+
- π΅ **Audio Applications**: Waveform and spectrogram generation
|
| 241 |
+
- 𧬠**Scientific Applications**: Molecular and protein structure generation
|
| 242 |
+
|
| 243 |
+
## License
|
| 244 |
+
|
| 245 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 246 |
+
|
| 247 |
+
## Additional Resources
|
| 248 |
+
|
| 249 |
+
- **GitHub Repository**: [DiffusionModels](https://github.com/GruheshKurra/DiffusionModels)
|
| 250 |
+
- **Detailed Notebook**: Complete implementation with educational content
|
| 251 |
+
- **Training Logs**: Comprehensive metrics and analysis
|
| 252 |
+
|
| 253 |
+
## Model Card Authors
|
| 254 |
+
|
| 255 |
+
**Gruhesh Kurra** - Implementation, documentation, and educational content
|
| 256 |
+
|
| 257 |
+
---
|
| 258 |
+
|
| 259 |
+
**Tags**: diffusion-models, generative-ai, pytorch, ddpm, deep-learning, denoising
|
| 260 |
+
|
| 261 |
+
**Model Card Last Updated**: December 2024
|
diffusion_logs/sampling_log.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[]
|
diffusion_logs/training_log.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
diffusion_model_complete.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b5657fbdbd7bcb9b250d0680980cea7f710bb5ce36b31a1ebbc6794818455f47
|
| 3 |
+
size 1923075
|
diffusion_process.png
ADDED
|
Git LFS Details
|
diffusion_results.png
ADDED
|
Git LFS Details
|
training_metrics.png
ADDED
|
Git LFS Details
|