tomopt.optimisation.callbacks package¶
Submodules¶
tomopt.optimisation.callbacks.callback module¶
- class tomopt.optimisation.callbacks.callback.Callback[source]¶
Bases:
objectImplements the base class from which all callback should inherit. Callbacks are used as part of the fitting, validation, and prediction methods of
AbsVolumeWrapper. They can interject at various points, but by default do nothing. Please check in theAbsVolumeWrapperto see when exactly their interjections are called.When writing new callbacks, the
VolumeWrapperthey are associated with will be their wrapper attribute. Their wrapper will have a fit_params attribute (FitParams) which is a data-class style object. It contains all the objects associated with the fit and predictions, including other callbacks. Callback interjections should read/write to wrapper.fit_params, rather than returning values.Accounting for the interjection calls (on_*_begin & on_*_end), the full optimisation loop is:
Associate callbacks with wrapper (set_wrapper)
on_train_begin
- for epoch in n_epochs:
state = “train”
on_epoch_begin
- for p, passive in enumerate(trn_passives):
- if p % passive_bs == 0:
on_volume_batch_begin
loss = 0
load passive into passive volume
on_volume_begin
- for muon_batch in range(n_mu_per_volume//mu_bs):
on_mu_batch_begin
Irradiate volume with mu_bs muons
Infer scatter locations
on_scatter_end
Infer x0 and append to list of x0 predictions
on_mu_batch_end
on_x0_pred_begin
Compute overall x0 prediction
on_x0_pred_end
Compute loss based on precision and cost, and add to loss
- if p`+1 % `passive_bs == 0:
loss = loss/passive_bs
on_volume_batch_end
Zero parameter gradients
on_backwards_begin
Backpropagate loss and compute parameter gradients
on_backwards_end
Update detector parameters
viii. Ensure detector parameters are within physical boundaries (AbsDetectorLayer.conform_detector) viv. loss = 0
- if len(trn_passives)-(p`+1) < `passive_bs:
Break
on_epoch_end
state = “valid”
on_epoch_begin
- for p, passive in enumerate(val_passives):
- if p % passive_bs == 0:
on_volume_batch_begin
loss = 0
on_volume_begin
- for muon_batch in range(n_mu_per_volume//mu_bs):
on_mu_batch_begin
Irradiate volume with mu_bs muons
Infer scatter locations
on_scatter_end
Infer x0 and append to list of x0 predictions
on_mu_batch_end
on_x0_pred_begin
Compute overall x0 prediction
on_x0_pred_end
Compute loss based on precision and cost, and add to loss
- if p`+1 % `passive_bs == 0:
loss = loss/passive_bs
on_volume_batch_end
- if len(val_passives)-(p`+1) < `passive_bs:
Break
on_epoch_end
on_train_end
- on_backwards_begin()[source]¶
Runs when the loss for a batch of passive volumes has been computed, but not yet backpropagated.
- Return type:
None
- on_backwards_end()[source]¶
Runs when the loss for a batch of passive volumes has been backpropagated, but parameters have not yet been updated.
- Return type:
None
- on_mu_batch_end()[source]¶
Runs when a batch muons ends and scatters have been added to the volume inferrer.
- Return type:
None
- on_pred_begin()[source]¶
Runs when the wrapper is about to begin in prediction mode.
- Return type:
None
- on_scatter_end()[source]¶
Runs when a scatters for the latest muon batch have been computed, but not yet added to the volume inferrer.
- Return type:
None
- on_volume_batch_begin()[source]¶
Runs when a new batch of passive volume layouts is begins.
- Return type:
None
- on_volume_batch_end()[source]¶
Runs when a batch of passive volume layouts is ends.
- Return type:
None
- on_x0_pred_begin()[source]¶
Runs when the all the muons for a volume have propagated, and the volume inferrer is about to make its final prediction.
- Return type:
None
- on_x0_pred_end()[source]¶
Runs after the volume inferrer has made its final prediction, but before the loss is computed.
- Return type:
None
- set_wrapper(wrapper)[source]¶
- Parameters:
wrapper (
AbsVolumeWrapper) – Volume wrapper to associate with the callback- Return type:
None
-
wrapper:
Optional[AbsVolumeWrapper] = None¶
tomopt.optimisation.callbacks.cyclic_callbacks module¶
tomopt.optimisation.callbacks.data_callbacks module¶
- class tomopt.optimisation.callbacks.data_callbacks.MuonResampler[source]¶
Bases:
CallbackResamples muons to only include those which will impact the passive volume at some point, even if they only hit the bottom layer.
- static check_mu_batch(mu, volume)[source]¶
Checks the provided muon batch to determine which muons will impact the passive volume at any point
- on_mu_batch_begin()[source]¶
Resamples muons prior to propagation through the volume such that all muons will hit the passive volume.
# TODO Add check for realistic validation
- Return type:
None
- static resample(mus, volume, gen)[source]¶
Resamples muons until all muons will hit the passive volume.
- Parameters:
mus (
Tensor) – xy_p_theta_phi tensor designed to initialise aMuonBatchvolume (
Volume) – Volume containing the passive volume to test againstgen (
AbsMuonGenerator) – Muon generator for sampling replacement muons
- Return type:
Tensor- Returns:
xy_p_theta_phi tensor designed to initialise a
MuonBatch
tomopt.optimisation.callbacks.detector_callbacks module¶
- class tomopt.optimisation.callbacks.detector_callbacks.PanelCentring[source]¶
Bases:
CallbackCallback class for panel centring in the optimisation process.
This callback is used to centre the panels of PanelDetectorLayer objects by setting their xy coordinates to the mean xy value of all panels in the layer.
This update takes place after the panel positions have been updated in the optimisation process.
- class tomopt.optimisation.callbacks.detector_callbacks.PanelUpdateLimiter(max_xy_step=None, max_z_step=None, max_xy_span_step=None)[source]¶
Bases:
CallbackLimits the maximum difference that optimisers can make to panel parameters, to prevent them from being affected by large updates from anomolous gradients. This is enacted by a hard-clamping based on the initial and final parameter values before/after each update step.
- Parameters:
max_xy_step (
Optional[Tuple[float,float]]) – maximum update in xy position of panelsmax_z_step (
Optional[float]) – maximum update in z position of panelsmax_xy_span_step (
Optional[Tuple[float,float]]) – maximum update in xy_span position of panels
- class tomopt.optimisation.callbacks.detector_callbacks.SigmoidPanelSmoothnessSchedule(smooth_range)[source]¶
Bases:
PostWarmupCallbackCreates an annealing schedule for the smooth attribute of
SigmoidDetectorPanel. This can be used to move from smooth, unphysical panel with high sensitivity outside the physical panel boundaries, to one with sharper decrease in resolution | efficiency at the edge, and so more closely resembles a physical panel, whilst still being differentiable.- Parameters:
smooth_range (
Tuple[float,float]) – tuple of initial and final values for the smooth attributes of all panels in the volume. A base-10 log schedule used over the number of epochs-total number of warmup epochs.
- on_epoch_begin()[source]¶
At the start of each training epoch, will anneal the
SigmoidDetectorPanels’ smooth attributes, if the callback is active.- Return type:
None
- on_train_begin()[source]¶
Sets all
SigmoidDetectorPanels to their initial smooth values.- Return type:
None
tomopt.optimisation.callbacks.diagnostic_callbacks module¶
- class tomopt.optimisation.callbacks.diagnostic_callbacks.HitRecord[source]¶
Bases:
ScatterRecordRecords the hits of the muons. Once recorded, the hits can be retrieved via the
get_record()method.plot_hit_density()may be used to plot the hit record.Warning
Currently this callback makes no distinction between different volume layouts, and is designed to used over a single volume layout.
# TODO extend these to create one record per volume
- class tomopt.optimisation.callbacks.diagnostic_callbacks.ScatterRecord[source]¶
Bases:
CallbackRecords the PoCAs of the muons which are located inside the passive volume. Once recorded, the PoCAs can be retrieved via the
get_record()method.plot_scatter_density()may be used to plot the scatter record.Warning
Currently this callback makes no distinction between different volume layouts, and is designed to used over a single volume layout.
# TODO extend these to create one record per volume
tomopt.optimisation.callbacks.eval_metric module¶
- class tomopt.optimisation.callbacks.eval_metric.EvalMetric(lower_metric_better, name=None, main_metric=True)[source]¶
Bases:
CallbackBase class from which metric should inherit and implement the computation of their metric values. Inheriting classes will automatically be detected by
MetricLoggerand included in live feedback if it is the “main metric”- Parameters:
lower_metric_better (
bool) – if True, a lower value of the metric should be considered better than a higher valuename (
Optional[str]) – name to associate with the metricmain_metric (
bool) – whether this metric should be considered the “main metric”
- get_metric()[source]¶
This will be called by
on_epoch_end()- Return type:
float- Returns:
metric value
tomopt.optimisation.callbacks.grad_callbacks module¶
- class tomopt.optimisation.callbacks.grad_callbacks.NoMoreNaNs[source]¶
Bases:
CallbackPrior to parameter updates, this callback will check and set any NaN gradients to zero. Updates based on NaN gradients will set the parameter value to NaN.
Important
As new parameters are introduced, e.g. through new detector models, this callback will need to be updated.
tomopt.optimisation.callbacks.heatmap_gif module¶
- class tomopt.optimisation.callbacks.heatmap_gif.HeatMapGif(gif_filename='heatmap.gif')[source]¶
Bases:
CallbackRecords a gif of the first heatmap in the first detector layer during training.
- Parameters:
gif_filename (
str) – savename for the gif (will be appended to the callback savepath)
tomopt.optimisation.callbacks.monitors module¶
- class tomopt.optimisation.callbacks.monitors.MetricLogger(gif_filename='optimisation_history.gif', gif_length=10.0, show_plots=False)[source]¶
Bases:
CallbackProvides live feedback during training showing a variety of metrics to help highlight problems or test hyper-parameters without completing a full training. If show_plots is false, will instead print training and validation losses at the end of each epoch. The full history is available as a dictionary by calling
get_loss_history(). Additionally, a gif of the optimisation can be saved.- Parameters:
gif_filename (
Optional[str]) – optional savename for recording a gif of the optimisation process (None -> no gif) The savename will be appended to the callback savepathgif_length (
float) – If saving gifs, controls the total length in secondsshow_plots (
bool) – whether to provide live plots during optimisation in notebooks
- cat_palette = 'tab10'¶
- get_loss_history()[source]¶
Get the current history of losses and metrics
- Returns:
tuple of ordered dictionaries: first with losses, second with validation metrics
- Return type:
history
- h_mid = 8¶
- lbl_col = 'black'¶
- lbl_sz = 24¶
- leg_sz = 16¶
- on_epoch_begin()[source]¶
Prepare to track new loss and snapshot the plots if training
- Return type:
None
- on_epoch_end()[source]¶
If validation epoch finished, record validation losses, compute info and update plots
- Return type:
None
- on_train_end()[source]¶
Cleans up plots, and optionally creates a gif of the training history
- Return type:
None
- on_volume_batch_end()[source]¶
Grabs the validation losses for the latest volume batch
- Return type:
None
- style = {'rc': {'patch.edgecolor': 'none'}, 'style': 'whitegrid'}¶
- tk_col = 'black'¶
- tk_sz = 16¶
- w_mid = 14.222222222222221¶
- class tomopt.optimisation.callbacks.monitors.PanelMetricLogger(gif_filename='optimisation_history.gif', gif_length=10.0, show_plots=False)[source]¶
Bases:
MetricLoggerLogger for use with
PanelDetectorLayers- Parameters:
gif_filename (
Optional[str]) – optional savename for recording a gif of the optimisation process (None -> no gif) The savename will be appended to the callback savepathgif_length (
float) – If saving gifs, controls the total length in secondsshow_plots (
bool) – whether to provide live plots during optimisation in notebooks
tomopt.optimisation.callbacks.opt_callbacks module¶
- class tomopt.optimisation.callbacks.opt_callbacks.EpochSave[source]¶
Bases:
CallbackSaves the state of the volume at the end of each training epoch to a unique file. This can be used to load a specifc state to either be used, or to resume training.
- class tomopt.optimisation.callbacks.opt_callbacks.OneCycle(opt_name, warmup_length, init_lr=None, init_mom=None, mid_lr=None, mid_mom=None, final_lr=None, final_mom=None)[source]¶
Bases:
AbsOptScheduleCallback implementing Smith 1-cycle evolution for lr and momentum (beta_1) https://arxiv.org/abs/1803.09820
- In the warmup phase:
Learning rate is increased from init_lr to mid_lr, Momentum is decreased from init_mom to mid_mom, to stabilise the use of high LRs
- In the convergence phase:
Learning rate is decreased from mid_lr to final_lr, Momentum is increased from mid_mom to final_mom
Setting the learning rate or momentum here will override the values specified when instantiating the VolumeWrapper. learning rate or momentum arguments can be None to avoid annealing or overriding their values.
- Parameters:
opt_name (
str) – name of optimiser that should be affected by the schedulerwarmup_length (
int) – number of epochs to use for the warmup phaseinit_lr (
Optional[float]) – initial learning rate (low)init_mom (
Optional[float]) – initial momentum (high)mid_lr (
Optional[float]) – nominal learning rate (high),mid_mom (
Optional[float]) – nominal momentum (moderate),final_lr (
Optional[float]) – final learning rate (low),final_mom (
Optional[float]) – final momentum (high)
tomopt.optimisation.callbacks.pred_callbacks module¶
- class tomopt.optimisation.callbacks.pred_callbacks.PredHandler[source]¶
Bases:
CallbackDefault callback for predictions. Collects predictions and true voxelwise X0 pairs for a range of volumes and returns them as list of tuples of numpy arrays when
get_preds()is called.
- class tomopt.optimisation.callbacks.pred_callbacks.Save2HDF5PredHandler(path, use_volume_target, overwrite=False, x02id=None, compression='lzf')[source]¶
Bases:
VolumeTargetPredHandlerSaves predictions and targets to an HDF5 file, rather than caching and returning them. Samples are written incrementally. Can optionally save volume targets rather than voxel-wise X0 targets If an x02id lookup is provided, it transforms the target from an X0 value to a material class ID.
- Parameters:
path (
Path) – savename of file to save predictions and targetsuse_volume_target (
bool) – if True, saves the volume target value instead of the volume X0soverwrite (
bool) – if True will overwrite existing files with the same path, otherwise will append to themx02id (
Optional[Dict[float,int]]) – optional map from X0 values to class IDscompression (
Optional[str]) – optional string representation of any compression to use when saving data
- class tomopt.optimisation.callbacks.pred_callbacks.VolumeTargetPredHandler(x02id=None)[source]¶
Bases:
PredHandlerReturns the volume target as the target value, rather than the voxel-wise X0s. If an x02id lookup is provided, it transforms the target from an X0 value to a material class ID.
- Parameters:
x02id (
Optional[Dict[float,int]]) – optional map from X0 values to class IDs
tomopt.optimisation.callbacks.warmup_callbacks module¶
- class tomopt.optimisation.callbacks.warmup_callbacks.CostCoefWarmup(n_warmup)[source]¶
Bases:
WarmupCallbackSets a more stable cost coefficient in the
AbsDetectorLossby averaging the inference-error component for several epochs. During this warm-up monitoring phase, the detectors will be kept fixed.- Parameters:
n_warmup (
int) – number of training epochs to wait before setting the cost coefficient
- class tomopt.optimisation.callbacks.warmup_callbacks.OptConfig(n_warmup, rates)[source]¶
Bases:
WarmupCallbackAllows the user to specify the desired update steps for parameters in physical units. Over the course of several warm-up epochs the gradients on the parameters are monitored, after which suitable learning rates for the optimisers are set, such that the parameters will move by the desired amount every update. During the warm-up, the detectors will not be updated as optimiser learning rates will be set to zero.
The calculation here does not account for the effect of the optimiser’s momentum, nor scheduling and adaption of learning rates, and so the actual update rates may be different from the desired ones.
- Parameters:
n_warmup (
int) – number of training epochs to wait before setting learning ratesrates (
Dict[str,float]) – dictionary of desired update rates for the parameters The keys are the names of the optimisers specified in the optimiser dictionary of the wrapper. The values are the desired update rates for the parameters in physical units. For example, if the optimiser is SGD, and the parameter is the xy position of a panel, then the update rate should be in metres. The parameters that are being optimisered are expected to be found in the zeroth parameter group of the optimiser, i.e. wrapper.opts[opt].param_groups[0][‘params’]. This implies that the optimiser is expected to have only one parameter group.
- Example::
>>> OptConfig(n_warmup=2, rates={'xy_pos_opt':xy_pos_rate, 'z_pos_opt':z_pos_rate, 'xy_span_opt':xy_span_rate})
- class tomopt.optimisation.callbacks.warmup_callbacks.PostWarmupCallback[source]¶
Bases:
CallbackCallback class that waits for all
WarmupCallbackobejcts to finish their warmups before activating.
- class tomopt.optimisation.callbacks.warmup_callbacks.WarmupCallback(n_warmup)[source]¶
Bases:
CallbackWarmup callbacks act at the start of training to track and set parameters based on the initial state of the detector. During warmup, optimisation of the detector is prevented, via a flag. If multiple warmup callbacks are present, they will wait to warmup according to the order they are provided in. Once the last warmup callback finished, the flag will be set to allow the detectors to be optimised. When a WarmupCallback is warming up, its warmup_active attribute will be True.
Important
When inheriting from WarmupCallback, the super methods of on_train_begin, on_epoch_begin, and on_epoch_end must be called.
- Parameters:
n_warmup (
int) – number of training epochs over-which to warmup
- check_warmups()[source]¶
If a WarmupCallback has finished, then its warmup_active is set to False, and the next WarmupCallback will have its warmup_active is set to True. If the finishing callback was the last WarmupCallback, then the “skip optimisation” flag is unset.
- Return type:
None
- on_epoch_begin()[source]¶
Ensures that when one WarmupCallback has finished, either the next is called, or the detectors are set to be optimised.
- Return type:
None