medsegpy.engine

medsegpy.engine.trainer

class medsegpy.engine.trainer.DefaultTrainer(cfg: medsegpy.config.Config, run_eagerly=None, strategy=None)[source]

Default trainer for medical semantic segmentation.

__init__(cfg: medsegpy.config.Config, run_eagerly=None, strategy=None)[source]
Parameters:
  • cfg (Config) – An experiment config.
  • run_eagerly (bool, optional) – If True, runs eagerly. Only available in tensorflow>=2.0.
  • strategy (tf.distribute.Strategy, optional) – The strategy to use for training. Only available if tf.distribute package is available (tensorflow>=1.14).
train()[source]

Train model specified by config.

Do not call this under a strategy scope. Instead, set self.strategy.

build_loss()[source]

Builds loss function used with model.compile(loss=...).

medsegpy.engine.defaults

Default engine activities.

Adapted from Facebook’s detectron2. https://github.com/facebookresearch/detectron2

medsegpy.engine.defaults.default_argument_parser()[source]

Create a parser with some common arguments used by detectron2 users.

Returns:argparse.ArgumentParser
medsegpy.engine.defaults.default_setup(cfg, args)[source]

Perform some basic common setups at the beginning of a job, including:

  1. Set up the medsegpy logger
  2. Log basic information about environment, cmdline arguments, and config
  3. Backup the config to the output directory
  4. Version experiments
Parameters:
  • cfg (CfgNode) – the full config to be used
  • args (argparse.NameSpace) – the command line arguments to be logged
medsegpy.engine.defaults.default_exp_name(cfg)[source]

Extracts default experiment name from the config.

cfg.EXP_NAME if exists. If basename starts with “version” or “debug”, take both parent directory name and version name to make experiment name (e.g. “my_exp/version_001”).

Returns:exp_name (str) – The default convention for naming experiments.

medsegpy.engine.callbacks

medsegpy.engine.callbacks.lr_callback(optimizer)[source]

Wrapper for learning rate tensorflow metric.

Parameters:optimizer – Optimizer used for training.
Returns:func – To be wrapped in metric or callback.
class medsegpy.engine.callbacks.LossHistory[source]

A Keras callback to log training history

class medsegpy.engine.callbacks.WandBLogger(period: int = 20, experiment='auto', **kwargs)[source]

A Keras callback to log to weights and biases.

Currently only supports logging scalars.

__init__(period: int = 20, experiment='auto', **kwargs)[source]
Parameters:
  • period (int, optional) – Logging period.
  • experiment (wandb.wandb_run.Run | str | None) – The experiment run. If "auto", a run will only be created if wandb.run is None. If None, a run will be created.
  • **kwargs – Options to pass to wandb.init() to create run. Ignored if experiment specified.