src.train.utils namespace¶
Submodules¶
src.train.utils.common module¶
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
. Iflog_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:
-
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 calladd_images
and let us do the job. Tensor with \((1, H, W)\), \((H, W)\), \((H, W, 3)\) is also suitable as long as correspondingdataformats
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:
-
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:
-
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.factory module¶
-
class
src.train.utils.factory.
CosineAnnealingLR
(optimizer, T_max, eta_min=0, warmup=None, warmup_iters=None)[source]¶ Bases:
object
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¶
-