Generator training¶
generator.train_generator() trains the generative model on the extracted real
anomaly cutouts. It runs an Optuna hyperparameter search, stores one checkpoint
per trial and finally loads the selected trial, so
generate_synthetic_anomalies() can follow directly.
config.model.set_model("cVAE_ConvNeXt_2D")
config.training.num_trials = 5
config.training.epochs = 500
config.training.batch_size = 32
generator = HybridDataGenerator(config)
generator.train_generator()
Training needs real anomalies in the study, so run extraction first. A CUDA GPU is used when available; otherwise training runs on the CPU.
What a trial does¶
Each of the config.training.num_trials trials:
- samples the model parameters that have a distribution in
config.model.search; all other parameters keep their value fromconfig.model.parameters(see generator models); - splits the real anomalies into training and validation data according to
config.training.validation_ratio. The split is seeded withconfig.study.seedand therefore identical in every trial; - trains with Adam at
config.training.learning_ratefor at mostconfig.training.epochsepochs; - saves the checkpoint of the epoch with the lowest validation value of
config.training.monitor_metric.
The default monitor metric selection is the mask-balanced reconstruction
loss without the KL term. It also is the value Optuna minimizes across trials.
total monitors the complete weighted training loss instead.
Stopping and learning rate¶
- Early stopping (
early_stopping_enabled, default on) ends a trial when the validation value has not improved by more thanearly_stopping["delta"]forearly_stopping["patience"]epochs. - Learning-rate scheduling (
lr_scheduler_enabled, default on) usesReduceLROnPlateauon the validation value with thepatience,factorandthresholdfromconfig.training.lr_scheduler. gradient_clip_normoptionally clips the gradient norm in every step.
config.training.early_stopping = {"patience": 50, "delta": 1e-4}
config.training.lr_scheduler = {"patience": 15, "factor": 0.5, "threshold": 1e-5}
Training augmentation¶
Two augmentations in config.augmentation act during training:
- Random offset (
random_offset_enabled, default on) translates each training cutout and its mask by a random amount. The anomaly foreground always stays inside the cutout;random_offset_max_fractionscales the available range. Validation data is not shifted. - Mask transforms (
config.augmentation.mask_transforms) create the source-to-target pairs for the paired models. For all mask-conditioned models they also produce the target masks during generation.
Outputs¶
study/
<study_name>.db # Optuna study with all trials
trained_models/
model_trial_<n>_best.pth # best checkpoint of trial n
model_trial_<n>_history.csv # per-epoch metrics of trial n
The history CSV contains the learning rate, the KL weight and every training and validation metric per epoch. It is rewritten after each epoch, so a running trial can be inspected.
Selecting a trial¶
config.training.trial_selection decides which trial train_generator() and
load_generator() load:
| Value | Loaded trial |
|---|---|
"best" (default) |
trial with the lowest objective value |
"last" |
trial with the highest trial number |
| a non-negative integer | trial with exactly this number |
To reuse a trained generator in a later process, load it instead of training again:
generator = HybridDataGenerator(config)
generator.load_generator()
generator.generate_synthetic_anomalies()
load_generator(path_to_db_file=...) reads the trials from another Optuna
database, for example to reuse a generator trained in a different study folder.
That database must contain an Optuna study named like config.study.name, and
the checkpoint paths stored in it must still exist.
Repeating training¶
train_generator() continues the existing Optuna study of the study folder:
every call adds num_trials further trials to the earlier ones and keeps
their checkpoints. Existing synthetic anomalies and hybrids are not changed
until generate_synthetic_anomalies() runs again.
The complete parameter list is available in the training reference and the augmentation reference.