SDK ML Flow API

FlowNetwork

This file defines the training step (forward pass + loss computation) and validation stages

class metavision_ml.flow.flow_network.FlowNetwork(array_dim, flow_loss_weights, feature_extractor_name='eminet', **kwargs)

Torch Module comprised of a feature extractor that computes flow but also modules that are useful for training such as Warpers and loss modules.

in_channels

number of channels in input.

Type

int

feature_extractor

neural network predicts flow pyramid.

Type

nn.Module

pyramid

stores resized inputs. warp modules per level.warping_head (nn.ModuleList): warp modules per level.

Type

object

criterion

Module computing the loss.

Type

nn.Module

Parameters
  • array_dim (int List) – input shape (num_tbins, number of channels, height, width).

  • flow_loss_weight (dict) – dictionary, whose keys are name of flow losses and the values are float weight factors.

  • feature_extractor_name (string) – name of the feature extractor architecture to instantiate.

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

forward(inputs)

Define 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.

sharpen(inputs, micro_tbin=0, depth_level=- 1)

Rescales the input at a given depth level and uses the flow to sharpen the input by moving time bins according to the forward optical flow.

Parameters
  • inputs (torch.Tensor) – batch (T,B,C,H,W)

  • micro_tbin (int) – micro tbin to warp individual flow to

  • depth_level (int) – at which scale we run sharpen

Flow Architectures

Different macro level neural network architectures for flow regression in pytorch

metavision_ml.flow.feature_extractor.eminet(n_input_channels, base=16, scales=3, separable=True, rnn_cell='lstm', **kwargs)

Unet Regressor model with depthwise separable recurrent convolution for minimal footprint.

Parameters
  • n_input_channels (int) – number of channels in input features.

  • scales (int) – number of convolutional layers in the encoder (also in the decoder).

  • base (int) – base multiplier for the number of channels in each layer. For instance with scales = 2 and base = 4 there will be [4, 8, 16, 8, 4] channels in the network.

  • separable (bool) – whether convolutions in the encoder and the decoders are depthwise separable convolutions. This saves a lot of parameters but makes the network harder to train.

  • rnn_cell (string) – type of cell used for the rnn, either ‘lstm’ or ‘gru’.

metavision_ml.flow.feature_extractor.eminet_non_sep(n_input_channels, base=16, scales=3, rnn_cell='lstm', **kwargs)

Constructor for Unet without depthwise separable convolutions.

Parameters
  • n_input_channels (int) – number of channels in input features.

  • scales (int) – number of convolutional layers in the encoder (also in the decoder).

  • base (int) – base multiplier for the number of channels in each layer. For instance with scales = 2 and base = 4 there will be [4, 8, 16, 8, 4] channels in the network.

  • rnn_cell (string) – type of cell used for the rnn, either ‘lstm’ or ‘gru’.

metavision_ml.flow.feature_extractor.midinet(n_input_channels, base=16, scales=3, separable=True, rnn_cell='lstm', **kwargs)

Unet Regressor model with Squeeze excitation layers.

Parameters
  • n_input_channels (int) – number of channels in input features.

  • scales (int) – number of convolutional layers in the encoder (also in the decoder).

  • base (int) – base multiplier for the number of channels in each layer. For instance with scales = 2 and base = 4 there will be [4, 8, 16, 8, 4] channels in the network.

  • separable (boolean) – if True, uses depthwise separable convolutions for the forward convolutional layer.

  • rnn_cell (string) – type of cell used for the rnn, either ‘lstm’ or ‘gru’.

metavision_ml.flow.feature_extractor.midinet2(n_input_channels, base=16, scales=3, separable=True, rnn_cell='lstm', depth=1)

midinet with a fine-tuned middle block (convRNN with tunable depth + residual connection)

Parameters
  • n_input_channels (int) – number of channels in input features.

  • scales (int) – number of convolutional layers in the encoder (also in the decoder).

  • base (int) – base multiplier for the number of channels in each layer. For instance with scales = 2 and base = 4 there will be [4, 8, 16, 8, 4] channels in the network.

  • separable (boolean) – if True, uses depthwise separable convolutions for the forward convolutional layer.

  • depth (int) – number of convRNN layers in the middle part of the Unet. Must be one or above.

  • rnn_cell (string) – type of cell used for the rnn, either ‘lstm’ or ‘gru’.

Losses

Class computing the loss for flow regression.

Contains the following loss functions:

Task-specific loss functions These loss functions are different formulations of the task that the flow is supposed to fulfill: predicting the motion of objects and “deblurring” moving edges.

  • data this loss function ensures that applying the flow to warp a tensor at time $t$ will match the tensor at time $t+1$

  • time consistency this loss function checks that the flow computed at timestamp $t_i$ is also correct at time $t_{i+1}$

    as most motions are consistent over time. This assumption doesn’t hold for fast moving objects.

  • bw deblur this loss function is applied backwards to avoid the degenerate solution of a flow warping all tensors

    into one single point or away from the frame (such a flow would have a really high loss when applied backward). We call this “deblurring loss” as it allows us to warp several time channels to a single point and obtain an image that is sharper than the original (lower variance).

Regularization loss functions
  • smoothness this loss function is a first-order derivative kernel applied to the flow to minimise extreme

    variations of flow.

  • smoothness2 this loss function is a second-order derivative kernel encouraging flow values to be locally co-linear.

  • l1 this term penalizes extreme values of flow.

param flow_weights

dictionary of weights per loss type, keys should be a subset of LOSS_NAMES, values should be floating point coefficients. Those coefficients are to be applied to each loss component.

type flow_weights

dict

param warping_head

module able to warp a feature tensor using a flow tensor. Only useful if you use time_consistency loss.

param smoothness_mask

list of loss functions that should be applied only for pixels with non negative values.

type smoothness_mask

string_list

Visualization

metavision_ml.flow.viz.draw_dense_flow(img, flow, mask)

Draws Mixed of image converted to gray and the flow in HSV color space. :param img: img (RGB or Gray) :type img: np.ndarray :param flow: u,v flow map :type flow: np.ndarray :param mask: mask to display flow or not. :type mask: np.ndarray

metavision_ml.flow.viz.draw_arrows(img, flow, step=16, threshold_px=1, convert_img_to_gray=True, mask=None, thickness=1)

Visualizes Flow, by drawing hsv-colored arrows on top of the input image.

Parameters
  • img (np.ndarray) – RGB uint8 image, to draw the arrows on.

  • flow (np.ndarray) – of shape (2, height width), where the first index is the x component of the flow and the second is the y-component. The flow is in pixel units.

  • step (int) – Draws every step arrow. use to increase clarity, especially with fast motions.

  • threshold_px (float) – doesn’t display arrows shorter the threshold_px pixels.

  • mask (np.ndarray) – boolean tensor of shape (H, W) indicating where flow arrows should be drawn or not.

metavision_ml.flow.viz.get_dense_flow(flow, base_img=None, mask=None)

Creates a flow visualization using dense image Optionally a RGB uint8 image can be passed as canvas to draw on.

Parameters
  • flow (torch.tensor) – tensor of shape (2, H, W) where the first index is the x component of the flow and the second is the y-component.

  • base_img (np.ndarray) – if not None, the flow arrows will be drawn on it. A visualization of the events or some features is usually a good idea. Prefer gray level for clarity.

  • step (int) – Draws every step arrow. Use to increase clarity, especially with fast motions.

  • mask (np.ndarray) – boolean tensor of shape (H, W) indicating where flow arrows should be drawn or not.

metavision_ml.flow.viz.get_arrows(flow, base_img=None, step=8, mask=None)

Creates a flow visualization using colored arrows.

Optionally a RGB uint8 image can be passed as canvas to draw on.

Parameters
  • flow (torch.tensor) – tensor of shape (2, H, W) where the first index is the x component of the flow and the second is the y-component.

  • base_img (np.ndarray) – if not None, the flow arrows will be drawn on it. A visualization of the events or some features is usually a good idea. Prefer gray level for clarity.

  • step (int) – Draws every step arrow. Use to increase clarity, especially with fast motions.

  • mask (np.ndarray) – boolean tensor of shape (H, W) indicating where flow arrows should be drawn or not.

metavision_ml.flow.viz.draw_flow_on_grid(input_tensor, flows, grid, make_img_fun, scale=- 1, step=8, mask_by_input=True, draw_dense=False)

Applies Flow drawing function to a batch of inputs.

Inputs are going to be visualized as a sequence of 2d grids, on top of which flow arrows will be drawn. The feature visualization is on grayscale, whereas the arrows are hsv-colored depending on their orientation.

Parameters
  • input_tensor (numpy ndarray) – tensor of shape (num_time_bins, batchsize, channel, height, width).

  • flows (torch.tensor list) – list of flow tensors. The position in the list indicates the resolution in increasing order, each tensor is of shape (num_time_bins, batchsize, 2, height, width) i.e. it corresponds with the features.

  • grid (np.ndarray) – array of shape (num_time_bins, m * height,n * width,3) where m*n is superior to batchsize. This array is used to draw a sequence of batches as a RGB video.

  • make_img_fun (function) – visualization function corresponding to the feature type.

  • scale (int) – index of the flow scale to use (when regressed by an hour glass network like unet several resolutions of flows might be available).

  • step (int) – Draws every step arrow. Use to increase clarity, especially with fast motions.

  • mask_by_input (boolean) – if True only display flow arrows on pixel with non null input.

  • draw_dense (boolean) – if True display dense flow map

metavision_ml.flow.viz.convert_to_gray(img)

“Converts the input RGB uint8 image to gray scale.

Parameters

img (np.ndarray) – 3-channels (RGB) or 1-channel (grayscale) uint8 image

Pytorch Lightning Model

class metavision_ml.flow.lightning_model.FlowModel(delta_t=50000, preprocess='event_cube', learning_rate=1e-05, array_dim=(1, 6, 240, 320), loss_weights=None, feature_extractor_name='eminet', network_kwargs=None, draw_dense=False)

Pytorch lightning module to learn self supervised flow.

Parameters
  • delta_t (int) – Timeslice delta_t in us.

  • preprocess (string) – Name of the preprocessing function used to turn events into features. Can be any of the functions present in metavision_ml.preprocessing or one registered by the user.

  • learning_rate (float) – factor by which weights update are multiplied during training. A large learning rate means the network is updated faster but the convergence might be harder.

  • array_dim (int list) – Dimensions of feature tensors: (num_tbins, channels, sensor_height * 2^-k, sensor_width * 2^-k).

  • loss_weights (dict) – dictionary, whose keys are names of flow losses and the values are float weight factors.

  • feature_extractor_name (str) – Name of the feature extractor architecture.

  • network_kwargs (dict) – kwargs of parameters for the feature extractor.

compute_loss(inputs)

Computes loss for a given input.

Parameters

inputs (Tensor) – batch of input features. Must be of the shape (T, N, C, H,W).

Returns

a dictionary, where keys are losses names and the Values are Torch float Tensors.

configure_optimizers()

Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple. Optimization with multiple optimizers only works in the manual optimization mode.

Returns

Any of these 6 options.

  • Single optimizer.

  • List or Tuple of optimizers.

  • Two lists - The first list has multiple optimizers, and the second has multiple LR schedulers (or multiple lr_scheduler_config).

  • Dictionary, with an "optimizer" key, and (optionally) a "lr_scheduler" key whose value is a single LR scheduler or lr_scheduler_config.

  • None - Fit will run without any optimizer.

The lr_scheduler_config is a dictionary which contains the scheduler and its associated configuration. The default configuration is shown below.

lr_scheduler_config = {
    # REQUIRED: The scheduler instance
    "scheduler": lr_scheduler,
    # The unit of the scheduler's step size, could also be 'step'.
    # 'epoch' updates the scheduler on epoch end whereas 'step'
    # updates it after a optimizer update.
    "interval": "epoch",
    # How many epochs/steps should pass between calls to
    # `scheduler.step()`. 1 corresponds to updating the learning
    # rate after every epoch/step.
    "frequency": 1,
    # Metric to to monitor for schedulers like `ReduceLROnPlateau`
    "monitor": "val_loss",
    # If set to `True`, will enforce that the value specified 'monitor'
    # is available when the scheduler is updated, thus stopping
    # training if not found. If set to `False`, it will only produce a warning
    "strict": True,
    # If using the `LearningRateMonitor` callback to monitor the
    # learning rate progress, this keyword can be used to specify
    # a custom logged name
    "name": None,
}

When there are schedulers in which the .step() method is conditioned on a value, such as the torch.optim.lr_scheduler.ReduceLROnPlateau scheduler, Lightning requires that the lr_scheduler_config contains the keyword "monitor" set to the metric name that the scheduler should be conditioned on.

Metrics can be made available to monitor by simply logging it using self.log('metric_to_track', metric_val) in your LightningModule.

Note

Some things to know:

  • Lightning calls .backward() and .step() automatically in case of automatic optimization.

  • If a learning rate scheduler is specified in configure_optimizers() with key "interval" (default “epoch”) in the scheduler configuration, Lightning will call the scheduler’s .step() method automatically in case of automatic optimization.

  • If you use 16-bit precision (precision=16), Lightning will automatically handle the optimizer.

  • If you use torch.optim.LBFGS, Lightning handles the closure function automatically for you.

  • If you use multiple optimizers, you will have to switch to ‘manual optimization’ mode and step them yourself.

  • If you need to control how often the optimizer steps, override the optimizer_step() hook.

demo_video(test_data, log_dir='.', epoch=0, num_batches=100, write_video=True, show_video=False, mask_by_input=True)

This runs our detector on several videos of the testing dataset.

Parameters
  • test_data (object) – dataloader for demo the validation set corresponding to the dataset_path.

  • log_dir (str) – directory where to create the video folder containing the result video file.

  • epoch (int) – index of the epoch. Used to name the video.

  • num_batches (int) – Number of batches used to create the video.

  • write_video (boolean) – whether to save a video file in the log_dir/videos/video#{epoch}.mp4

  • show_video (boolean) – whether to display the result in an opencv window.

  • mask_by_input (boolean) – if True only display flow arrows on pixel with non null input.

forward(x)

Same as torch.nn.Module.forward().

Parameters
  • *args – Whatever you decide to pass into the forward method.

  • **kwargs – Keyword arguments are also possible.

Returns

Your model’s output

loss_step(batch, batch_idx, mode='train')

Performs a supervised loss if labels are available, otherwise applies a self supervised criterion.

on_test_epoch_end()

Called in the test loop at the very end of the epoch.

on_test_epoch_start()

Called in the test loop at the very beginning of the epoch.

on_validation_epoch_end()

Called in the validation loop at the very end of the epoch.

on_validation_epoch_start()

Called in the validation loop at the very beginning of the epoch.

set_defaults()

Sets default for backward compatibility purposes.

test_step(batch, batch_nb)

Operates on a single batch of data from the test set. In this step you’d normally generate examples or calculate anything of interest such as accuracy.

Parameters
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one test dataloader:
def test_step(self, batch, batch_idx): ...


# if you have multiple test dataloaders:
def test_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single test dataset
def test_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    test_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'test_loss': loss, 'test_acc': test_acc})

If you pass in multiple test dataloaders, test_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple test dataloaders
def test_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    ...

Note

If you don’t need to test you don’t need to implement this method.

Note

When the test_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of the test epoch, the model goes back to training mode and gradients are enabled.

training_step(batch, batch_idx)

Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.

Parameters
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns

  • Tensor - The loss tensor

  • dict - A dictionary which can include any keys, but must include the key 'loss' in the case of automatic optimization.

  • None - In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.

In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.

Example:

def training_step(self, batch, batch_idx):
    x, y, z = batch
    out = self.encoder(x)
    loss = self.loss(out, x)
    return loss

To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:

def __init__(self):
    super().__init__()
    self.automatic_optimization = False


# Multiple optimizers (e.g.: GANs)
def training_step(self, batch, batch_idx):
    opt1, opt2 = self.optimizers()

    # do training_step with encoder
    ...
    opt1.step()
    # do training_step with decoder
    ...
    opt2.step()

Note

When accumulate_grad_batches > 1, the loss returned here will be automatically normalized by accumulate_grad_batches internally.

validation_step(batch, batch_nb)

Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.

Parameters
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one val dataloader:
def validation_step(self, batch, batch_idx): ...


# if you have multiple val dataloaders:
def validation_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single validation dataset
def validation_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'val_loss': loss, 'val_acc': val_acc})

If you pass in multiple val dataloaders, validation_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple validation dataloaders
def validation_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    ...

Note

If you don’t need to validate you don’t need to implement this method.

Note

When the validation_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.

Pytorch Lightning DataModule

This module creates data loader to load event based data for self supervised flow.

class metavision_ml.flow.data_module.FlowDataModule(hparams, data_dir: str = '', test_data_dir: str = '')

This data module handles unlabeled event based data as well as labeled validation data.

The data_dir is meant to contain two directories train`and `val containing HDF5 files of preprocessed event features.

If a test_data_dir is provided it means labeled flow (usually synthetic data) is provided. In this case it is *_td.dat files along with *_flow.h5 dense flow labels.

Parameters
  • hparams – hyperparameters from the corresponding FlowModel.

  • data_dir (str) – path towards the directory containing train val and test folder of HDF5 files.

  • test_data_dir (str) – optional path towards a directory containing DAT event files and HDF5 dense flow annotations.

prepare_data_per_node

If True, each LOCAL_RANK=0 will call prepare data. Otherwise only NODE_RANK=0, LOCAL_RANK=0 will prepare data.

allow_zero_length_dataloader_with_multiple_devices

If True, dataloader with zero length within local rank is allowed. Default value is False.

setup(stage=None)

During this stage the data module parses the train and val folders.

test_dataloader()

An iterable or collection of iterables specifying test samples.

For more information about multiple dataloaders, see this section.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

Note

If you don’t need a test dataset and a test_step(), you don’t need to implement this method.

train_dataloader()

An iterable or collection of iterables specifying training samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~pytorch_lightning.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

transfer_batch_to_device(batch, device, dataloader_idx)

Override this hook if your DataLoader returns tensors wrapped in a custom data structure.

The data types listed below (and any arbitrary nesting of them) are supported out of the box:

  • torch.Tensor or anything that implements .to(…)

  • list

  • dict

  • tuple

For anything else, you need to define how the data is moved to the target device (CPU, GPU, TPU, …).

Note

This hook should only transfer the data and not modify it, nor should it move the data to any other device than the one passed in as argument (unless you know what you are doing). To check the current state of execution of this hook you can use self.trainer.training/testing/validating/predicting so that you can add different logic as per your requirement.

Parameters
  • batch – A batch of data that needs to be transferred to a new device.

  • device – The target device as defined in PyTorch.

  • dataloader_idx – The index of the dataloader to which the batch belongs.

Returns

A reference to the data on the new device.

Example:

def transfer_batch_to_device(self, batch, device, dataloader_idx):
    if isinstance(batch, CustomBatch):
        # move all tensors in your custom data structure to the device
        batch.samples = batch.samples.to(device)
        batch.targets = batch.targets.to(device)
    elif dataloader_idx == 0:
        # skip device transfer for the first dataloader or anything you wish
        pass
    else:
        batch = super().transfer_batch_to_device(batch, device, dataloader_idx)
    return batch
Raises

MisconfigurationException – If using IPUs, Trainer(accelerator='ipu').

See also

  • move_data_to_device()

  • apply_to_collection()

val_dataloader()

An iterable or collection of iterables specifying validation samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~pytorch_lightning.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

It’s recommended that all data downloads and preparation happen in prepare_data().

  • fit()

  • validate()

  • prepare_data()

  • setup()

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware There is no need to set it yourself.

Note

If you don’t need a validation dataset and a validation_step(), you don’t need to implement this method.

metavision_ml.flow.data_module.load_labels_flow(metadata, start_time, duration, tensor)

Loads flow labels from an HDF5 file

Parameters
  • metadata (FileMetadata) – This class contains information about the sequence that is being read. Ideally the path for the labels should be deducible from metadata.path.

  • start_time (int) – Time in us in the file at which we start reading.

  • duration (int) – Duration in us of the data we need to read from said file.

  • tensor (torch.tensor) – Torch tensor of the feature for which labels are loaded. It can be used for instance to filter out the labels in area where there is no events.

Returns

labels should be indexable by time bin (to differentiate the labels of each time bin). It could

therefore be a list of length num_tbins.

(boolean nd array): This boolean mask array of length num_tbins indicates

whether the frame contains a label. It is used to differentiate between time_bins that actually contain an empty label (for instance no bounding boxes) from time bins that weren’t labeled due to cost constraints. The latter timebins shouldn’t contribute to supervised losses used during training.

Pytorch Lightning Callbacks

class metavision_ml.flow.callbacks.FlowCallback(data_module, curriculum_param=1.414, video_result_every_n_epochs=2, mask_flow_by_input=True)

Determines the number of max_consecutive batch in the training dataloader before each epoch.

Parameters
  • data_module (object) – pytorch lightning data module

  • curriculum_param (float) – parameter used to compute the number of consecutive batches to extract from a video.

  • law (It relies on the following) – max_consecutive_batch = int(p^(epoch-1))

  • video_result_every_n_epochs (int) – every n epoch a video file is generated showing the result on a validation subset.

  • mask_flow_by_input (boolean) – if True only display non-zero flows.

on_train_epoch_end(trainer, pl_module)

When epoch ends the callback launches a demo.

Parameters
  • trainer (object) – trainer object

  • pl_module (object) – pytorch lightning model

on_train_epoch_start(trainer, pl_module)

When epoch starts the callback reschedules the dataset.

Parameters
  • trainer (object) – trainer object

  • pl_module (object) – pytorch lightning model