This project now includes full integration with Weights & Biases for experiment tracking, metrics visualization, and model management.
- 🔐 Automatic API Key Management: Checks for
keys.txt, prompts for key if needed - 📊 Rich Metrics Logging: Automatically logs training/validation losses, accuracies, F1 scores, precision, and recall
- 🎯 Hyperparameter Tracking: Logs model architecture, optimizer settings, and training configuration
- 📦 Model Artifacts: Saves trained models and configs as wandb artifacts
- 🎨 Beautiful Dashboards: Real-time visualization of training progress
- 🚀 Easy Setup: One-command integration with interactive prompts
pip install -r pipeline_requirements.txtpython src/test_wandb_integration.pypython example_wandb_setup.pyThis will:
- Guide you through getting a wandb API key
- Save it securely to
keys.txt - Test the integration
- Show example usage
# With wandb enabled (default)
python src/train_model_mlp.py -i /path/to/your/dataset
# Disable wandb if needed
python src/train_model_mlp.py -i /path/to/your/dataset --no-wandb- Automatic Detection: Checks for
keys.txtin the root folder - Format: Stores as
WANDB_KEY=your_api_key_here - Security: Automatically added to
.gitignore - User Prompt: Asks for key interactively if not found
train_loss/val_losstrain_acc/val_acctrain_f1_score/val_f1_scoretrain_precision/val_precisiontrain_recall/val_recall
- Model architecture details
- Optimizer configuration
- Training parameters (epochs, batch size, etc.)
- CLIP model information
- Class labels
- Final trained model (
model.pth) - Model configuration (
model_config.json) - Automatically versioned and tagged
The wandb integration uses PyTorch Lightning's WandbLogger which provides:
- Seamless metric logging via
self.log() - Automatic hyperparameter detection
- Model checkpointing as artifacts
- Integration with Lightning callbacks
If you prefer to set up the API key manually:
- Get your key from https://wandb.ai/authorize
- Create
keys.txtin the project root:WANDB_KEY=your_api_key_here - The training script will automatically detect and use it
You can also set the key as an environment variable:
export WANDB_API_KEY=your_api_key_here
python src/train_model_mlp.py -i /path/to/datasetwandb runs are organized as follows:
- Project Name:
aesthetica-training - Run Names: Automatically generated based on model configuration
- Tags: Based on model architecture and dataset size
After starting training:
- Check the console for the wandb URL
- Visit your wandb dashboard
- View real-time metrics, system stats, and logs
- Compare different runs and experiments
- Download model artifacts
from src.wandb_utils import setup_wandb_key, initialize_wandb
from src.train_model_mlp import start_training
# Setup wandb
setup_wandb_key(".", ask_user=False) # Silent mode
# Train with custom config
start_training(
root_folder="./my_dataset",
database_file="image_classifier_data.csv",
train_from="embeddings",
clip_models=[("hf-hub:timm", "ViT-SO400M-14-SigLIP-384")],
epochs=100,
batch_size=64,
enable_wandb=True
)The PyTorch Lightning model automatically logs metrics using self.log(). To add custom metrics:
# In your LightningModule
def training_step(self, batch, batch_idx):
# ... existing code ...
# Custom metric
custom_metric = compute_my_metric(outputs, targets)
self.log("custom_metric", custom_metric)
return loss# Test all wandb functionality
python src/test_wandb_integration.py
# Or run from project root
python -m src.test_wandb_integrationThe test script will verify:
- All imports work correctly
- Key detection functionality
- Training script integration
-
"No wandb API key found"
- Run
python example_wandb_setup.pyto set up your key - Or manually create
keys.txtwithWANDB_KEY=your_key
- Run
-
"Failed to initialize wandb"
- Check your internet connection
- Verify your API key is valid
- Try logging in manually:
wandb login
-
"Permission denied"
- Ensure you have write permissions in the project directory
- Check that
.gitignoreincludeskeys.txt
If you need to run without internet:
# In your training script
os.environ['WANDB_MODE'] = 'offline'Logs will be saved locally and can be synced later with wandb sync.
- Security: Never commit
keys.txtto version control - Organization: Use descriptive run names for easy identification
- Tagging: Add tags to group related experiments
- Notes: Add run descriptions for important experiments
- Artifacts: Use artifacts for model versioning and deployment
setup_wandb_key(root_folder, ask_user): Set up wandb API keyinitialize_wandb(project_name, run_name, config, enabled): Initialize wandb runget_wandb_enabled(): Check if wandb is configured
enable_wandb(bool): Enable/disable wandb logging--no-wandb(CLI flag): Disable wandb via command line