This project implements Conditional Flow Matching (CFM) with a focus on Optimal Transport Conditional Flow Matching (OTCFM), providing a flexible framework for generative modeling.
- Optimal Transport Conditional Flow Matcher (OTCFM)
- Independent Conditional Flow Matcher (ICFM)
- EMA (Exponential Moving Average) model tracking
- TensorBoard logging
- Flexible sampling methods (Euler and Heun solvers)
- Python 3.8+
- CUDA-compatible GPU (recommended)
- Git
# Clone the repository
git clone https://github.com/Thehunk1206/flow-based-models.git
cd flow-based-models
# Create and activate a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # Linux/Mac
# or
.\venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
python train_cfm.py \
--model "otcfm" \
--lr 1e-4 \
--batch_size 16 \
--total_steps 1000000 \
--output_dir "outputs/otcfm_default" \
--log_dir "logs/otcfm_default" \
--save_step 5000 \
--warmup 20000
--model
: Model type (otcfm
oricfm
)--lr
: Learning rate--batch_size
: Batch size for training--total_steps
: Total training steps--output_dir
: Directory for saving model checkpoints--log_dir
: Directory for TensorBoard logs--save_step
: Frequency of saving checkpoints--warmup
: Number of warmup steps
python generate_samples.py \
--checkpoint "outputs/results_otcfm_32_otcfm-large-batch_exp/otcfm/otcfm_weights_step_2000000.pt" \
--num_samples 4 \
--batch_size 4 \
--output_dir "sample_ot-cfm_large_batch" \
--image_size 128 128 \
--num_steps 8 \
--use_ema \
--solver heun \
--save_grid \
--save_intermediates \
--intermediate_freq 1
--checkpoint
: Path to the model checkpoint--num_samples
: Number of samples to generate--batch_size
: Batch size for generation--output_dir
: Output directory for generated samples--image_size
: Size of generated images (height width)--num_steps
: Number of steps in the generation process--use_ema
: Use EMA weights for generation--solver
: Solver type (euler
orheun
)--save_grid
: Save samples in a grid format--save_intermediates
: Save intermediate generation steps--intermediate_freq
: Frequency of saving intermediates generations
The project includes experiment_runner.sh
for automated experiment execution and tracking.
# Make script executable
chmod +x experiment_runner.sh
# Run all experiments
./experiment_runner.sh
- OTCFM Default
Model: otcfm
Learning Rate: 1e-4
Batch Size: 16
Total Steps: 2,000,000
- OTCFM Large Batch
Model: otcfm
Learning Rate: 1e-4
Batch Size: 32
Total Steps: 2,000,000
- ICFM Default
Model: icfm
Learning Rate: 1e-4
Batch Size: 16
Total Steps: 2,000,000
- Experiment tracking via
completed_experiments.log
- Automatic resume of interrupted experiments
- Individual output/log directories per experiment
- Error handling and status reporting
Add new experiments by modifying experiment_runner.sh
:
run_experiment "model_type" "learning_rate" "batch_size" "total_steps" "experiment_name"
./
βββ outputs/ # Model checkpoints
β βββ results_*_exp/
βββ logs/ # Training logs
β βββ *_logs/
βββ completed_experiments.log
MIT License
Copyright (c) 2024 Tauhid Khan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
- Original Flow Matching Paper Authors
- PyTorch Community
- Open-source Generative Modeling Researchers