decode.neuralfitter.utils package#

Submodules#

decode.neuralfitter.utils.collate module#

decode.neuralfitter.utils.last_layer_dynamics module#

decode.neuralfitter.utils.last_layer_dynamics.weight_by_gradient(layer, loss, optimizer)[source]#
Parameters:
  • layer (ModuleList) – module layers

  • loss (Tensor) – not reduced loss values

  • optimizer (Optimizer) – optimizer

Returns:

weight per channel (1x C x 1 x 1) loss_ch: channel-wise loss loss_w: weighted loss

Return type:

weight_cX_h1_w1

decode.neuralfitter.utils.log_train_val_progress module#

decode.neuralfitter.utils.log_train_val_progress.log_dists(tp, tp_match, pred, px_border, px_size, logger, step)[source]#

Log z vs z_gt

decode.neuralfitter.utils.log_train_val_progress.log_frames(x, y_out, y_tar, weight, em_out, em_tar, tp, tp_match, logger, step, colorbar=True)[source]#
decode.neuralfitter.utils.log_train_val_progress.log_kpi(loss_scalar, loss_cmp, eval_set, logger, step)[source]#
decode.neuralfitter.utils.log_train_val_progress.log_train(*, loss_p_batch, loss_mean, logger, step)[source]#
decode.neuralfitter.utils.log_train_val_progress.post_process_log_test(*, loss_cmp, loss_scalar, x, y_out, y_tar, weight, em_tar, px_border, px_size, post_processor, matcher, logger, step)[source]#

Post-Process

decode.neuralfitter.utils.logger module#

class decode.neuralfitter.utils.logger.DictLogger[source]#

Bases: NoLog

Parameters:
  • filter_keys – keys to be filtered in add_scalar_dict method

  • *args

  • **kwargs

add_scalar(prefix, scalar_value, global_step=None, walltime=None)[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

  • new_style (boolean) – Whether to use new style (tensor field) or old style (simple_value field). New style could lead to faster data loading.

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
add_scalar_dict(prefix, scalar_dict, global_step=None, walltime=None)[source]#

Adds a couple of scalars that are in a dictionary to the summary. Note that this is different from ‘add_scalars’

class decode.neuralfitter.utils.logger.MultiLogger(logger)[source]#

Bases: object

A ‘Meta-Logger’, i.e. a logger that calls its components. Note all component loggers are assumed to have the same methods.

class decode.neuralfitter.utils.logger.NoLog(*args, **kwargs)[source]#

Bases: SummaryWriter

Parameters:
  • filter_keys – keys to be filtered in add_scalar_dict method

  • *args

  • **kwargs

add_audio(*args, **kwargs)[source]#

Add audio data to summary.

Parameters:
  • tag (string) – Data identifier

  • snd_tensor (torch.Tensor) – Sound data

  • global_step (int) – Global step value to record

  • sample_rate (int) – sample rate in Hz

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

Shape:

snd_tensor: \((1, L)\). The values should lie between [-1, 1].

add_custom_scalars(*args, **kwargs)[source]#

Create special chart by collecting charts tags in ‘scalars’. Note that this function can only be called once for each SummaryWriter() object. Because it only provides metadata to tensorboard, the function can be called before or after the training loop.

Parameters:

layout (dict) – {categoryName: charts}, where charts is also a dictionary {chartName: ListOfProperties}. The first element in ListOfProperties is the chart’s type (one of Multiline or Margin) and the second element should be a list containing the tags you have used in add_scalar function, which will be collected into the new chart.

Examples:

layout = {'Taiwan':{'twse':['Multiline',['twse/0050', 'twse/2330']]},
             'USA':{ 'dow':['Margin',   ['dow/aaa', 'dow/bbb', 'dow/ccc']],
                  'nasdaq':['Margin',   ['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]}}

writer.add_custom_scalars(layout)
add_embedding(*args, **kwargs)[source]#

Add embedding projector data to summary.

Parameters:
  • mat (torch.Tensor or numpy.array) – A matrix which each row is the feature vector of the data point

  • metadata (list) – A list of labels, each element will be convert to string

  • label_img (torch.Tensor) – Images correspond to each data point

  • global_step (int) – Global step value to record

  • tag (string) – Name for the embedding

Shape:

mat: \((N, D)\), where N is number of data and D is feature dimension

label_img: \((N, C, H, W)\)

Examples:

import keyword
import torch
meta = []
while len(meta)<100:
    meta = meta+keyword.kwlist # get some strings
meta = meta[:100]

for i, v in enumerate(meta):
    meta[i] = v+str(i)

label_img = torch.rand(100, 3, 10, 32)
for i in range(100):
    label_img[i]*=i/100.0

writer.add_embedding(torch.randn(100, 5), metadata=meta, label_img=label_img)
writer.add_embedding(torch.randn(100, 5), label_img=label_img)
writer.add_embedding(torch.randn(100, 5), metadata=meta)
add_figure(tag, 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_figures(*args, **kwargs)[source]#
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.

  • use_strict_trace (bool) – Whether to pass keyword argument strict to torch.jit.trace. Pass False when you want the tracer to record your mutable container types (list, dict)

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_hparams(*args, **kwargs)[source]#

Add a set of hyperparameters to be compared in TensorBoard.

Parameters:
  • hparam_dict (dict) – Each key-value pair in the dictionary is the name of the hyper parameter and it’s corresponding value. The type of the value can be one of bool, string, float, int, or None.

  • metric_dict (dict) – Each key-value pair in the dictionary is the name of the metric and it’s corresponding value. Note that the key used here should be unique in the tensorboard record. Otherwise the value you added by add_scalar will be displayed in hparam plugin. In most cases, this is unwanted.

  • hparam_domain_discrete – (Optional[Dict[str, List[Any]]]) A dictionary that contains names of the hyperparameters and all discrete values they can hold

  • run_name (str) – Name of the run, to be included as part of the logdir. If unspecified, will use current timestamp.

Examples:

from torch.utils.tensorboard import SummaryWriter
with SummaryWriter() as w:
    for i in range(5):
        w.add_hparams({'lr': 0.1*i, 'bsize': i},
                      {'hparam/accuracy': 10*i, 'hparam/loss': 10*i})

Expected result:

_static/img/tensorboard/add_hparam.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

  • dataformats (string) – Image data format specification of the form CHW, HWC, HW, WH, etc.

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_images(*args, **kwargs)[source]#

Add batched 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

  • dataformats (string) – Image data format specification of the form NCHW, NHWC, CHW, HWC, HW, WH, etc.

Shape:

img_tensor: Default is \((N, 3, H, W)\). If dataformats is specified, other shape will be accepted. e.g. NCHW or NHWC.

Examples:

from torch.utils.tensorboard import SummaryWriter
import numpy as np

img_batch = np.zeros((16, 3, 100, 100))
for i in range(16):
    img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i
    img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i

writer = SummaryWriter()
writer.add_images('my_image_batch', img_batch, 0)
writer.close()

Expected result:

_static/img/tensorboard/add_images.png
add_mesh(*args, **kwargs)[source]#

Add meshes or 3D point clouds to TensorBoard. The visualization is based on Three.js, so it allows users to interact with the rendered object. Besides the basic definitions such as vertices, faces, users can further provide camera parameter, lighting condition, etc. Please check https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene for advanced usage.

Parameters:
  • tag (string) – Data identifier

  • vertices (torch.Tensor) – List of the 3D coordinates of vertices.

  • colors (torch.Tensor) – Colors for each vertex

  • faces (torch.Tensor) – Indices of vertices within each triangle. (Optional)

  • config_dict – Dictionary with ThreeJS classes names and configuration.

  • global_step (int) – Global step value to record

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

Shape:

vertices: \((B, N, 3)\). (batch, number_of_vertices, channels)

colors: \((B, N, 3)\). The values should lie in [0, 255] for type uint8 or [0, 1] for type float.

faces: \((B, N, 3)\). The values should lie in [0, number_of_vertices] for type uint8.

Examples:

from torch.utils.tensorboard import SummaryWriter
vertices_tensor = torch.as_tensor([
    [1, 1, 1],
    [-1, -1, 1],
    [1, -1, -1],
    [-1, 1, -1],
], dtype=torch.float).unsqueeze(0)
colors_tensor = torch.as_tensor([
    [255, 0, 0],
    [0, 255, 0],
    [0, 0, 255],
    [255, 0, 255],
], dtype=torch.int).unsqueeze(0)
faces_tensor = torch.as_tensor([
    [0, 2, 3],
    [0, 3, 1],
    [0, 1, 2],
    [1, 3, 2],
], dtype=torch.int).unsqueeze(0)

writer = SummaryWriter()
writer.add_mesh('my_mesh', vertices=vertices_tensor, colors=colors_tensor, faces=faces_tensor)

writer.close()
add_pr_curve(*args, **kwargs)[source]#

Adds precision recall curve. Plotting a precision-recall curve lets you understand your model’s performance under different threshold settings. With this function, you provide the ground truth labeling (T/F) and prediction confidence (usually the output of your model) for each target. The TensorBoard UI will let you choose the threshold interactively.

Parameters:
  • tag (string) – Data identifier

  • labels (torch.Tensor, numpy.array, or string/blobname) – Ground truth data. Binary label for each element.

  • predictions (torch.Tensor, numpy.array, or string/blobname) – The probability that an element be classified as true. Value should be in [0, 1]

  • global_step (int) – Global step value to record

  • num_thresholds (int) – Number of thresholds used to draw the curve.

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

Examples:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
labels = np.random.randint(2, size=100)  # binary label
predictions = np.random.rand(100)
writer = SummaryWriter()
writer.add_pr_curve('pr_curve', labels, predictions, 0)
writer.close()
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

  • new_style (boolean) – Whether to use new style (tensor field) or old style (simple_value field). New style could lead to faster data loading.

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
add_scalar_dict(*args, **kwargs)[source]#

Adds a couple of scalars that are in a dictionary to the summary. Note that this is different from ‘add_scalars’

add_scalars(*args, **kwargs)[source]#

Adds many scalar data to summary.

Parameters:
  • main_tag (string) – The parent name for the tags

  • tag_scalar_dict (dict) – Key-value pair storing the tag and corresponding values

  • global_step (int) – Global step value to record

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

Examples:

from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
r = 5
for i in range(100):
    writer.add_scalars('run_14h', {'xsinx':i*np.sin(i/r),
                                    'xcosx':i*np.cos(i/r),
                                    'tanx': np.tan(i/r)}, i)
writer.close()
# This call adds three values to the same scalar plot with the tag
# 'run_14h' in TensorBoard's scalar section.

Expected result:

_static/img/tensorboard/add_scalars.png
add_text(*args, **kwargs)[source]#

Add text data to summary.

Parameters:
  • tag (string) – Data identifier

  • text_string (string) – String to save

  • global_step (int) – Global step value to record

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

Examples:

writer.add_text('lstm', 'This is an lstm', 0)
writer.add_text('rnn', 'This is an rnn', 10)
add_video(*args, **kwargs)[source]#

Add video data to summary.

Note that this requires the moviepy package.

Parameters:
  • tag (string) – Data identifier

  • vid_tensor (torch.Tensor) – Video data

  • global_step (int) – Global step value to record

  • fps (float or int) – Frames per second

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

Shape:

vid_tensor: \((N, T, C, H, W)\). The values should lie in [0, 255] for type uint8 or [0, 1] for type float.

class decode.neuralfitter.utils.logger.SummaryWriter(filter_keys=(), *args, **kwargs)[source]#

Bases: SummaryWriter

Parameters:
  • filter_keys – keys to be filtered in add_scalar_dict method

  • *args

  • **kwargs

add_scalar_dict(prefix, scalar_dict, global_step=None, walltime=None)[source]#

Adds a couple of scalars that are in a dictionary to the summary. Note that this is different from ‘add_scalars’

decode.neuralfitter.utils.padding_calc module#

decode.neuralfitter.utils.padding_calc.outsize_calc(i, p, k, s, d)[source]#

i = input_size o = output p = padding k = kernel_size s = stride d = dilation :return:

decode.neuralfitter.utils.padding_calc.pad_same_calc(i, k, s, d)[source]#

decode.neuralfitter.utils.probability module#

decode.neuralfitter.utils.probability.binom_pdiverse(p)[source]#

binomial probability but unequal probabilities :type p: :param p: (torch.Tensor) of probabilities

Returns:

(torch.Tensor) vector of probabilities with length p.size() + 1

Return type:

z

decode.neuralfitter.utils.processing module#

class decode.neuralfitter.utils.processing.ParallelTransformSequence(components, input_slice, merger=None)[source]#

Bases: TransformSequence

Parameters:
  • components – components with forward method

  • input_slice – list of lists which indicate what is the output to the i-th component; e.g. [[0, 1], [0]]

  • forward (means that the first component get's the 0th and 1st element which are input to this instances) –

  • method

  • the (the 1st component will get the 0th output of the 0th component. Input slice is ignored when) –

  • anyways (potential input is not a tuple) –

forward(*x)[source]#

Forwards the input data sequentially through all components

Parameters:

*x – arbitrary input data

Returns:

Output of the last component

Return type:

Any

class decode.neuralfitter.utils.processing.TransformSequence(components, input_slice=None)[source]#

Bases: object

Parameters:
  • components – components with forward method

  • input_slice – list of lists which indicate what is the output to the i-th component; e.g. [[0, 1], [0]]

  • forward (means that the first component get's the 0th and 1st element which are input to this instances) –

  • method

  • the (the 1st component will get the 0th output of the 0th component. Input slice is ignored when) –

  • anyways (potential input is not a tuple) –

forward(*x)[source]#

Forwards the input data sequentially through all components

Parameters:

*x – arbitrary input data

Returns:

Output of the last component

Return type:

Any

classmethod parse(components, param, **kwargs)[source]#

If all components implemented a parse method, you can do it globally only once for the whole sequence.

Parameters:
  • components – component reference (unintialised) with forward method

  • param (dict) – parameters which are forwarded to the constructor of the components

  • kwargs – arbitrary keyword arguments subject to this class constructor

Returns:

TransformSequence or subclass of it

decode.neuralfitter.utils.processing.wrap_callable(func)[source]#

Wrapps a callable in a class to provide a forward method. This is mainly a helper to wrap arbitrary functions to fit into the transform sequence as above.

Parameters:

func (Callable) –

Module contents#