src.train.utils namespace

Submodules

src.train.utils.common module

src.train.utils.common.cp_projects(to_path)[source]
src.train.utils.common.get_logger(work_dir, cfg)[source]
src.train.utils.common.save_model(net, optimizer, epoch, save_path, distributed)[source]

src.train.utils.dist_utils module

class src.train.utils.dist_utils.DistSummaryWriter(*args, **kwargs)[source]

Bases: torch.utils.tensorboard.writer.SummaryWriter

Creates a SummaryWriter that will write out events and summaries to the event file.

Parameters
  • log_dir (string) – Save directory location. Default is runs/CURRENT_DATETIME_HOSTNAME, which changes after each run. Use hierarchical folder structure to compare between runs easily. e.g. pass in ‘runs/exp1’, ‘runs/exp2’, etc. for each new experiment to compare across them.

  • comment (string) – Comment log_dir suffix appended to the default log_dir. If log_dir is assigned, this argument has no effect.

  • purge_step (int) – When logging crashes at step \(T+X\) and restarts at step \(T\), any events whose global_step larger or equal to \(T\) will be purged and hidden from TensorBoard. Note that crashed and resumed experiments should have the same log_dir.

  • max_queue (int) – Size of the queue for pending events and summaries before one of the ‘add’ calls forces a flush to disk. Default is ten items.

  • flush_secs (int) – How often, in seconds, to flush the pending events and summaries to disk. Default is every two minutes.

  • filename_suffix (string) – Suffix added to all event filenames in the log_dir directory. More details on filename construction in tensorboard.summary.writer.event_file_writer.EventFileWriter.

Examples:

from torch.utils.tensorboard import SummaryWriter

# create a summary writer with automatically generated folder name.
writer = SummaryWriter()
# folder location: runs/May04_22-14-54_s-MacBook-Pro.local/

# create a summary writer using the specified folder name.
writer = SummaryWriter("my_experiment")
# folder location: my_experiment

# create a summary writer with comment appended.
writer = SummaryWriter(comment="LR_0.1_BATCH_16")
# folder location: runs/May04_22-14-54_s-MacBook-Pro.localLR_0.1_BATCH_16/
add_figure(*args, **kwargs)[source]

Render matplotlib figure into an image and add it to summary.

Note that this requires the matplotlib package.

Parameters
  • tag (string) – Data identifier

  • figure (matplotlib.pyplot.figure) – Figure or a list of figures

  • global_step (int) – Global step value to record

  • close (bool) – Flag to automatically close the figure

  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

add_graph(*args, **kwargs)[source]

Add graph data to summary.

Parameters
  • model (torch.nn.Module) – Model to draw.

  • input_to_model (torch.Tensor or list of torch.Tensor) – A variable or a tuple of variables to be fed.

  • verbose (bool) – Whether to print graph structure in console.

add_histogram(*args, **kwargs)[source]

Add histogram to summary.

Parameters
  • tag (string) – Data identifier

  • values (torch.Tensor, numpy.array, or string/blobname) – Values to build histogram

  • global_step (int) – Global step value to record

  • bins (string) – One of {‘tensorflow’,’auto’, ‘fd’, …}. This determines how the bins are made. You can find other options in: https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html

  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

Examples:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter()
for i in range(10):
    x = np.random.random(1000)
    writer.add_histogram('distribution centers', x + i, i)
writer.close()

Expected result:

_static/img/tensorboard/add_histogram.png
add_image(*args, **kwargs)[source]

Add image data to summary.

Note that this requires the pillow package.

Parameters
  • tag (string) – Data identifier

  • img_tensor (torch.Tensor, numpy.array, or string/blobname) – Image data

  • global_step (int) – Global step value to record

  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

Shape:

img_tensor: Default is \((3, H, W)\). You can use torchvision.utils.make_grid() to convert a batch of tensor into 3xHxW format or call add_images and let us do the job. Tensor with \((1, H, W)\), \((H, W)\), \((H, W, 3)\) is also suitable as long as corresponding dataformats argument is passed, e.g. CHW, HWC, HW.

Examples:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
img = np.zeros((3, 100, 100))
img[0] = np.arange(0, 10000).reshape(100, 100) / 10000
img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

img_HWC = np.zeros((100, 100, 3))
img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000
img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

writer = SummaryWriter()
writer.add_image('my_image', img, 0)

# If you have non-default dimension setting, set the dataformats argument.
writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')
writer.close()

Expected result:

_static/img/tensorboard/add_image.png
add_scalar(*args, **kwargs)[source]

Add scalar data to summary.

Parameters
  • tag (string) – Data identifier

  • scalar_value (float or string/blobname) – Value to save

  • global_step (int) – Global step value to record

  • walltime (float) – Optional override default walltime (time.time()) with seconds after epoch of event

Examples:

from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
x = range(100)
for i in x:
    writer.add_scalar('y=2x', i * 2, i)
writer.close()

Expected result:

_static/img/tensorboard/add_scalar.png
close()[source]
src.train.utils.dist_utils.all_gather(data)[source]

Run all_gather on arbitrary picklable data (not necessarily tensors) :Parameters: data – any picklable object

Returns

list of data gathered from each rank

Return type

list[data]

src.train.utils.dist_utils.can_log()[source]
src.train.utils.dist_utils.dist_cat_reduce_tensor(tensor)[source]
src.train.utils.dist_utils.dist_mean_reduce_tensor(tensor)[source]
src.train.utils.dist_utils.dist_print(*args, **kwargs)[source]
src.train.utils.dist_utils.dist_sum_reduce_tensor(tensor)[source]
src.train.utils.dist_utils.dist_tqdm(obj, *args, **kwargs)[source]
src.train.utils.dist_utils.get_rank()[source]
src.train.utils.dist_utils.get_world_size()[source]
src.train.utils.dist_utils.is_main_process()[source]
src.train.utils.dist_utils.synchronize()[source]

Helper function to synchronize (barrier) among all processes when using distributed training

src.train.utils.dist_utils.to_python_float(t)[source]

src.train.utils.factory module

class src.train.utils.factory.CosineAnnealingLR(optimizer, T_max, eta_min=0, warmup=None, warmup_iters=None)[source]

Bases: object

step(external_iter=None)[source]
class src.train.utils.factory.MultiStepLR(optimizer, steps, gamma=0.1, iters_per_epoch=None, warmup=None, warmup_iters=None)[source]

Bases: object

step(external_iter=None)[source]
src.train.utils.factory.get_loss_dict(cfg)[source]
src.train.utils.factory.get_metric_dict(cfg)[source]
src.train.utils.factory.get_optimizer(net, cfg)[source]
src.train.utils.factory.get_scheduler(optimizer, cfg, iters_per_epoch)[source]

src.train.utils.loss module

class src.train.utils.loss.OhemCELoss(thresh, n_min, ignore_lb=255, *args, **kwargs)[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(logits, labels)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class src.train.utils.loss.ParsingRelationDis[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class src.train.utils.loss.ParsingRelationLoss[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(logits)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class src.train.utils.loss.SoftmaxFocalLoss(gamma, ignore_lb=255, *args, **kwargs)[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(logits, labels)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool

src.train.utils.metrics module

class src.train.utils.metrics.AccTopk(background_classes, k)[source]

Bases: object

get()[source]
reset()[source]
update(predict, target)[source]
class src.train.utils.metrics.Metric_mIoU(class_num)[source]

Bases: object

get()[source]
get_acc()[source]
get_miou()[source]
reset()[source]
update(predict, target)[source]
class src.train.utils.metrics.MultiLabelAcc[source]

Bases: object

get()[source]
get_acc()[source]
reset()[source]
update(predict, target)[source]
src.train.utils.metrics.converter(data)[source]
src.train.utils.metrics.fast_hist(label_pred, label_true, num_classes)[source]
src.train.utils.metrics.reset_metrics(metric_dict)[source]
src.train.utils.metrics.update_metrics(metric_dict, pair_data)[source]