Tries JSON first. Falls back to legacy pickle ONLY for files named .pkl (so a non-JSON payload disguised as .json is refused rather than unpickled, since pickle can execute arbitrary code). Existing .pkl artifacts still load.
Args: config_fn: File path to load.
Returns: The deserialized [size, apply_reorder, target_spacing] list.
Save patch-based training and inference configuration to a JSON file.
Written as JSON for safe, pickle-free sharing; load_patch_variables reads JSON and still falls back to legacy pickle files.
Args: config_fn: Path to save the config file. patch_size: Size of patches [x, y, z]. patch_overlap: Overlap for inference (int, float 0-1, or list). aggregation_mode: GridAggregator mode (‘crop’, ‘average’, ‘hann’). apply_reorder: Whether to reorder to canonical (RAS+) orientation. target_spacing: Target voxel spacing [x, y, z]. sampler_type: Type of sampler used during training. label_probabilities: Label probabilities for LabelSampler. samples_per_volume: Number of patches extracted per volume during training. queue_length: Maximum number of patches in queue buffer. queue_num_workers: Number of workers for parallel patch extraction. keep_largest_component: If True, keep only the largest connected component in binary segmentation predictions during inference. normalization: List of normalization specs (from PatchConfig.normalization) to persist, so inference can reconstruct the same pre-inference normalization.
Load patch-based training and inference configuration.
Tries JSON first. Falls back to legacy pickle ONLY for files named .pkl (a non-JSON payload disguised as .json is refused rather than unpickled, since pickle can execute arbitrary code). Because JSON stringifies dict keys, integer label_probabilities keys are restored to ints after a JSON load.
A FastAI callback for comprehensive MLflow experiment tracking.
This callback automatically logs hyperparameters, metrics, model artifacts, and configuration to MLflow during training. If a checkpoint callback (SaveModelCallback, EMACheckpoint, or any TrackerCallback with fname) is present, the best model checkpoint will also be logged as an artifact.
Create MLflow tracking callback with auto-extracted configuration.
This factory function automatically extracts configuration from the Learner, eliminating the need to manually specify parameters like size, transforms, loss function, etc.
Auto-extracts from Learner: - Preprocessing: apply_reorder, target_spacing, size/patch_size - Transforms: item_tfms or pre_patch_tfms - Training: loss_func, model architecture
Args: learn: fastai Learner instance experiment_name: MLflow experiment name. If None, uses model name. run_name: MLflow run name. If None, auto-generates with timestamp. auto_start: If True, auto-starts/stops MLflow run in before_fit/after_fit. model_name: Override the auto-extracted model name (used as the experiment name when experiment_name is None). extra_params: Additional parameters to log (e.g., {‘dropout’: 0.5}). extra_tags: MLflow tags to set on the run. dataset_version: Dataset version hash string for tracking. log_split: If True, auto-logs train/val split CSV when dls has split_df.
Returns: ModelTrackingCallback ready to use with learn.fit()
Discover one trained learner checkpoint per cross-validation fold from an MLflow experiment.
Reads back what [ModelTrackingCallback](https://fastmonai.no/utils.html#modeltrackingcallback) logs per run: takes the most recent run for each fold in experiment_name and downloads artifact_path from it, returning {fold: local_path} sorted by fold. Folds are identified by each run’s tags.<fold_tag>, falling back to a <fold_tag>_<n> run name. Warns if the selected folds span more than one dataset_version tag (a sign of a partial retrain).
Pair it with the soft-vote ensembling in [patch_inference](https://fastmonai.no/vision_patch.html#patch_inference) – but the returned values are PATHS, so load them first::
from fastai.learner import load_learner
paths = find_fold_learners("vs5f_unet")
learners = [load_learner(p) for p in paths.values()]
preds = patch_inference(learner=learners, config=config, file_paths=cases)
Args: experiment_name: MLflow experiment to search (e.g. "vs5f_unet"). artifact_path: run-relative artifact to download per fold (default "model/best_learner.pkl", what [ModelTrackingCallback](https://fastmonai.no/utils.html#modeltrackingcallback) logs). n_folds: optional expected folds for a missing-fold warning – an int n (labels 1..n) or an explicit iterable of labels (use this for 0-indexed folds). None makes no assumption and just returns what was found. fold_tag: run tag naming the fold; also the <fold_tag>_ run-name prefix fallback.
Returns: {fold_int: local_path} sorted by fold, or {} if the experiment is missing or MLflow is unavailable.
Launch and manage a local mlflow ui server from a notebook.
The UI’s lifetime is tied to the kernel: it is reaped when the interpreter exits, gracefully (via an atexit handler) or on a hard kill (via Linux PR_SET_PDEATHSIG, see [_die_with_parent](https://fastmonai.no/utils.html#_die_with_parent)), so closing/restarting the notebook never leaves an orphaned server holding the port. Call stop() to shut it down sooner. An externally-started UI is reused, not killed.