objective
orchard.optimization.objective.objective
¶
Optuna Objective Function for Vision Pipeline.
Provides OptunaObjective, the callable that Optuna invokes for each
trial. It orchestrates the full trial lifecycle — config sampling, data
loading, model creation, training, and metric extraction — using
dependency injection for maximum testability.
Key Components:
OptunaObjective: High-level orchestration callable. All external dependencies (dataset loader, dataloader factory, model factory) are injectable via Protocol-based abstractions. Settings are read fromcfg.optuna.*as single source of truth.TrialConfigBuilder: Builds trial-specificConfiginstances from sampled hyperparameters.MetricExtractor: Handles metric extraction and best-value tracking across epochs.TrialTrainingExecutor: Executes training loops with Optuna pruning integration.
Example
objective = OptunaObjective(cfg, search_space, device) study = optuna.create_study(direction="maximize") study.optimize(objective, n_trials=50)
DatasetLoaderProtocol
¶
Bases: Protocol
Protocol for dataset loading (enables dependency injection).
DataloaderFactoryProtocol
¶
Bases: Protocol
Protocol for dataloader creation (enables dependency injection).
__call__(metadata, dataset_cfg, training_cfg, aug_cfg, num_workers, is_optuna=False)
¶
Create train/val/test dataloaders.
Source code in orchard/optimization/objective/objective.py
ModelFactoryProtocol
¶
Bases: Protocol
Protocol for model creation (enables dependency injection).
__call__(device, dataset_cfg, arch_cfg)
¶
OptunaObjective(cfg, search_space, device, dataset_loader=None, dataloader_factory=None, model_factory=None, tracker=None)
¶
Optuna objective function with dependency injection.
Orchestrates hyperparameter optimization trials by:
- Building trial-specific configurations
- Creating data loaders, models, and optimizers
- Executing training with pruning
- Tracking and returning best metrics
All external dependencies are injectable for testability:
- dataset_loader: Dataset loading function
- dataloader_factory: DataLoader creation function
- model_factory: Model instantiation function
Attributes:
| Name | Type | Description |
|---|---|---|
cfg |
Base configuration (single source of truth) |
|
search_space |
Hyperparameter search space |
|
device |
Training device (CPU/CUDA/MPS) |
|
config_builder |
Builds trial-specific configs |
|
metric_extractor |
Handles metric extraction |
|
dataset_data |
Cached dataset (loaded once, reused across trials) |
Example
objective = OptunaObjective( ... cfg=config, ... search_space=search_space, ... device=torch.device("cuda"), ... ) study = optuna.create_study(direction="maximize") study.optimize(objective, n_trials=50)
Initialize Optuna objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
Config
|
Base configuration (reads optuna.* settings) |
required |
search_space
|
Mapping[str, Any]
|
Hyperparameter search space |
required |
device
|
device
|
Training device |
required |
dataset_loader
|
DatasetLoaderProtocol | None
|
Dataset loading function (default: load_dataset) |
None
|
dataloader_factory
|
DataloaderFactoryProtocol | None
|
DataLoader factory (default: get_dataloaders) |
None
|
model_factory
|
ModelFactoryProtocol | None
|
Model factory (default: get_model) |
None
|
tracker
|
TrackerProtocol | None
|
Optional experiment tracker for nested trial logging |
None
|
Source code in orchard/optimization/objective/objective.py
__call__(trial)
¶
Execute single Optuna trial.
Samples hyperparameters, builds trial configuration, trains model, and returns best validation metric. Failed trials return the worst possible metric instead of crashing the study.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trial
|
Trial
|
Optuna trial object |
required |
Returns:
| Type | Description |
|---|---|
float
|
Best validation metric achieved during training, |
float
|
or worst-case metric if the trial fails. |
Raises:
| Type | Description |
|---|---|
TrialPruned
|
If trial is pruned during training |
Source code in orchard/optimization/objective/objective.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |