API Reference¶
This section contains the complete API documentation for DeepAugment, automatically generated from the source code.
Main API¶
Core Functions¶
- deepaugment.optimize(X_train, y_train, X_val=None, y_val=None, iterations=None, epochs=None, verbose=True, **kwargs)[source]¶
Quick optimization function for minimal code.
Convention: if no val set, auto-split 80/20.
- Parameters:
X_train – Training images
y_train – Training labels
X_val – Validation images (optional, will split if None)
y_val – Validation labels (optional)
iterations – Optimization iterations (default from config)
epochs – Training epochs per policy (default from config)
verbose – Show progress
**kwargs – Additional args for DeepAugment
- Returns:
Best augmentation policy
Example
>>> from deepaugment import optimize >>> best = optimize(X, y, iterations=50)
DeepAugment Class¶
- class deepaugment.DeepAugment[source]¶
Bases:
objectFind optimal image augmentation policies using Bayesian optimization.
Beautiful API inspired by Rails: Convention over Configuration, but with sharp knives when you need them.
Example
>>> aug = DeepAugment(X_train, y_train, X_val, y_val) >>> best = aug.optimize(iterations=50) >>> aug.show_best()
- __init__(X_train, y_train, X_val, y_val, model='simple', device='auto', random_state=None, experiment_name=None, method='bayesian', save_history=None, transform_categories=None, custom_reward_fn=None, resume_from=None, n_operations=None, train_size=None, val_size=None)[source]¶
Initialize DeepAugment with beautiful defaults and configuration freedom.
All args are optional - sensible defaults from config.
- Parameters:
X_train – Training data (N, H, W, C) and labels (N,)
y_train – Training data (N, H, W, C) and labels (N,)
X_val – Validation data and labels
y_val – Validation data and labels
1 (# PHASE) – Essential
model – Model architecture (default: ‘simple’)
device – ‘cuda’, ‘mps’, ‘cpu’, or ‘auto’ (default: ‘auto’)
random_state – Seed for reproducibility (default from config)
experiment_name – Name for tracking (default: timestamp)
2 (# PHASE) – Useful
method – ‘bayesian’ or ‘random’ search (default: ‘bayesian’)
save_history – Save optimization history (default from config)
3 (# PHASE) – Advanced
transform_categories – Restrict transforms, e.g., [‘geometric’, ‘color’]
custom_reward_fn – Custom reward function(entry) -> float
resume_from – Path to checkpoint to continue from
Core (#)
n_operations – Transforms per policy (default from config)
train_size – Training subset size (default from config)
val_size – Validation subset size (default from config)
- optimize(iterations=None, epochs=None, samples=None, batch_size=None, learning_rate=None, early_stopping=None, patience=None, verbose=True)[source]¶
Search for best augmentation policy.
All args optional - sensible defaults from config.
- Parameters:
iterations – Number of policies to try (default from config)
epochs – Training epochs per evaluation (default from config)
samples – Training runs per policy, for averaging (default from config)
batch_size – Training batch size (default from config)
learning_rate – Learning rate (default from config)
early_stopping – Stop if no improvement (default from config)
patience – Early stopping patience (default from config)
verbose – Show progress bar
- Returns:
Best policy as list of (transform, magnitude) tuples
Transforms¶
Image transformations - elegant, minimal, powerful.
Uses torchvision v2 transforms. Convention: magnitude in [0, 1].
- deepaugment.transforms.make_transform(name, magnitude)[source]¶
Create single transform. Fail fast if invalid.
Convention: magnitude clamped to [0, 1].
- deepaugment.transforms.apply_policy(image, policy)[source]¶
Apply augmentation policy to image.
Pure function: image → policy → augmented image.
- Parameters:
image – RGB image (H, W, C) numpy array or PIL Image
policy – List of (transform_name, magnitude) tuples
- Returns:
Augmented image as numpy array
- deepaugment.transforms.create_augmenter(policy)[source]¶
Create reusable augmenter from policy.
Returns: Callable that augments images.
- deepaugment.transforms.get_transform_names(categories=None)[source]¶
Get transform names, optionally filtered by category.
Convention: None means all transforms.
Transform Constants¶
- deepaugment.TRANSFORMS¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s
(key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
d = {} for k, v in iterable:
d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
Models¶
Model architectures - minimal, elegant, effective.
Just the models. Training logic lives in trainer.py (separation of concerns).
- class deepaugment.models.SimpleCNN[source]¶
Bases:
ModuleMinimal CNN for fast policy evaluation.
Compact but effective: ~1.2M parameters for 32x32 images.
- deepaugment.models.create_model(model_name='simple', num_classes=10, in_channels=3)[source]¶
Create model by name. Convention: ‘simple’ is default.
Extensible: add more models here as needed.
Model Creation¶
SimpleCNN Model¶
Policy¶
Policy operations - elegant, composable, functional.
A policy is a list of (transform_name, magnitude) tuples. Everything related to policy representation lives here (SSOT).
- class deepaugment.policy.PolicySpace[source]¶
Bases:
objectPolicy search space. Knows everything about what policies can be.
Follows SSOT: all policy space logic lives here.
- format_policy(raw_policy)[source]¶
Convert raw policy (optimizer format) to human-readable.
Raw: [idx1, mag1, idx2, mag2, …] Human: [(name1, mag1), (name2, mag2), …]
- class deepaugment.policy.PolicyHistory[source]¶
Bases:
objectTrack evaluated policies and scores.
Minimal, elegant storage. Unix: do one thing well.
- deepaugment.policy.display_policy(policy, indent=4)[source]¶
Pretty print policy. For programmer happiness.
PolicySpace¶
- class deepaugment.PolicySpace[source]¶
Bases:
objectPolicy search space. Knows everything about what policies can be.
Follows SSOT: all policy space logic lives here.
- format_policy(raw_policy)[source]¶
Convert raw policy (optimizer format) to human-readable.
Raw: [idx1, mag1, idx2, mag2, …] Human: [(name1, mag1), (name2, mag2), …]
PolicyHistory¶
Search¶
Search strategies - Bayesian optimization and random search.
Minimal, composable, elegant. Strategy pattern without the boilerplate.
- class deepaugment.search.SearchStrategy[source]¶
Bases:
objectBase for search strategies. Minimal interface.
ask() → get next policy to try tell() → report result
- policy_space: PolicySpace¶
- history: PolicyHistory¶
- __init__(policy_space, history=None)¶
Method generated by attrs for class SearchStrategy.
- Parameters:
policy_space (PolicySpace)
history (PolicyHistory)
- Return type:
None
- class deepaugment.search.RandomSearch[source]¶
Bases:
SearchStrategyRandom search baseline. Simple, fast, surprisingly effective.
No learning, just exploration. Perfect for sanity checks.
- __init__(policy_space, history=None)¶
Method generated by attrs for class RandomSearch.
- Parameters:
policy_space (PolicySpace)
history (PolicyHistory)
- Return type:
None
- class deepaugment.search.BayesianSearch[source]¶
Bases:
SearchStrategyBayesian optimization using scikit-optimize.
Learn from past evaluations to suggest better policies. Uses Random Forest + Expected Improvement.
- tell(policy, score)[source]¶
Report result to optimizer.
scikit-optimize minimizes, so we negate the score.
- __init__(policy_space, history=None, n_initial=None, random_state=None, optimizer=None)¶
Method generated by attrs for class BayesianSearch.
- Parameters:
policy_space (PolicySpace)
history (PolicyHistory)
n_initial (int)
random_state (int)
optimizer (Optimizer)
- Return type:
None
Training¶
Training logic - functional, composable, minimal.
Pure functions for training models. No classes, just functions. Unix philosophy: do one thing well.
- deepaugment.trainer.prepare_data(X, y)[source]¶
Convert numpy to torch tensors.
Convention: assumes (N, H, W, C) format, converts to (N, C, H, W).
- deepaugment.trainer.create_loaders(train_data, val_data, batch_size=None)[source]¶
Create data loaders. Convention: sensible batch size from config.
- deepaugment.trainer.train_epoch(model, loader, optimizer, criterion, device)[source]¶
Train for one epoch. Pure function, returns metrics.
- deepaugment.trainer.validate_epoch(model, loader, criterion, device)[source]¶
Validate for one epoch. Pure function, returns metrics.
- deepaugment.trainer.train_model(model, train_data, val_data, epochs=None, batch_size=None, learning_rate=None, device=None)[source]¶
Train model to completion. Functional composition.
- Parameters:
model – PyTorch model
train_data – (X_train, y_train) as numpy arrays
val_data – (X_val, y_val) as numpy arrays
epochs – Training epochs (default from config)
batch_size – Batch size (default from config)
learning_rate – Learning rate (default from config)
device – Device string (default from config)
- Returns:
Training history dict
- deepaugment.trainer.evaluate_policy(policy, train_data, val_data, num_classes, augmenter, model_factory, epochs=None, samples=None, **kwargs)[source]¶
Evaluate augmentation policy by training model.
Functional approach: augment → train → evaluate → score.
- Parameters:
policy – List of (transform, magnitude) tuples
train_data – (X_train, y_train)
val_data – (X_val, y_val)
num_classes – Number of classes
augmenter – Function to augment images
model_factory – Function that creates model
epochs – Training epochs
samples – Number of training runs (for averaging)
**kwargs – Additional args for train_model
- Returns:
Average validation accuracy
Configuration¶
Single Source of Truth for all configuration.
Following Rails doctrine: Convention over Configuration. All magic numbers live here. Change once, apply everywhere.
- class deepaugment.config.Defaults[source]¶
Bases:
objectBeautiful defaults that just work.
- __init__(epochs=10, batch_size=64, learning_rate=0.001, iterations=50, samples=1, n_operations=4, n_initial_points=10, train_size=2000, val_size=500, image_size=32, model_name='simple', dropout_rate=0.3, device='auto', method='bayesian', random_state=42, save_history=True, checkpoint_every=10, early_stopping=False, patience=10)¶
Method generated by attrs for class Defaults.
- Parameters:
epochs (int)
batch_size (int)
learning_rate (float)
iterations (int)
samples (int)
n_operations (int)
n_initial_points (int)
train_size (int)
val_size (int)
image_size (int)
model_name (str)
dropout_rate (float)
device (str)
method (str)
random_state (int)
save_history (bool)
checkpoint_every (int)
early_stopping (bool)
patience (int)
- Return type:
None
Utilities¶
Elegant utilities inspired by Unix philosophy and functional programming.
Each function does one thing well. Compose them for power.
- deepaugment.utils.sample_indices(total='__no__default__', size='__no__default__', seed=42)[source]¶
Sample indices without replacement. Functional and reproducible.
- deepaugment.utils.split_data(X, y, ratio=0.8, seed=42)[source]¶
Split data into train/val. Convention: 80/20 split.
- deepaugment.utils.to_native(obj)[source]¶
Convert numpy types to native Python for JSON serialization.
Recursive, handles nested structures. Unix philosophy: do one thing well.
- deepaugment.utils.save_checkpoint(data, filename, directory=None)[source]¶
Save checkpoint as JSON. Convention: pretty print for human readability.
- deepaugment.utils.generate_experiment_name(prefix='exp')[source]¶
Generate unique experiment name. Convention: ISO format timestamp.
- deepaugment.utils.format_policy(policy, indent=4)[source]¶
Format policy as beautiful string for display.