ai4med.components.handlers package

class AdjustLayerLRs

Bases: ai4med.common.train_handler.TrainHandler

This is just an example to show you how to adjust layer LRs with a handler. You must have set up the LR placeholders already in the optimizer.

You can adjust layer LRs at any event during training: start/end of epoch/iteration, etc. See TrainHandler for all the defined events.

Simply write the LR adjusting code within the event(s) handling method. This example shows you how to do it with the Start Epoch event.

start_epoch(ctx: ai4med.common.train_ctx.TrainContext)
class StatsHandler

Bases: ai4med.common.train_handler.TrainHandler

The following are initialized as items in ai4med.common.train_ctx.TrainContext to use for stats or not:


self.unreportable_list = ('graph', 'tf_config', 'session', 'summary_writer') self.reporting_dir = None self.epoch_reporting_list = ('current_epoch', 'train_summaries_dict', 'best_validation_epoch', 'best_validation_metric', 'validation_summaries_dict') self.iteration_reporting_list = ('current_epoch', 'current_iteration', 'current_train_loss', 'best_validation_metric', 'best_validation_epoch', 'train_summaries_dict', 'validation_summaries_dict', 'current_learning_rate' ) self.reporting_dict = { 'epoch': ('epoch_info.csv', self.epoch_reporting_list), 'iteration': ('iteration_info.csv', self.iteration_reporting_list) }

allowed_to_write(ctx: ai4med.common.train_ctx.TrainContext)

Helper function to determine whether to write or not for multi-GPU setups


ctx – TrainContext

Returns: True if my_rank == 0, for the thread to write on

end_epoch(ctx: ai4med.common.train_ctx.TrainContext)

Runs write_file for epoch stats at the end of the epoch.

end_iteration(ctx: ai4med.common.train_ctx.TrainContext)

Runs write_file for iteration stats at the end of the iteration.

start_train(ctx: ai4med.common.train_ctx.TrainContext)

Initializes at start_train:


self.reporting_dir = ctx.model_log_dir if self.allowed_to_write(ctx): for _, v in self.reporting_dict.items(): with open(os.path.join(self.reporting_dir, v[0]), 'wt') as f: f.write(','.join(v[1])) f.write('


write_file(ctx: ai4med.common.train_ctx.TrainContext, stage)

ctx_dict = vars(ctx) with open(os.path.join(self.reporting_dir, stage[0]), 'at') as f: value = ['"'+str(ctx_dict[v])+'"' for v in stage[1]] f.write(','.join(value)) f.write('


© Copyright 2020, NVIDIA. Last updated on Feb 2, 2023.