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.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(inputs) 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.
-
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_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_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.
- 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 orlr_scheduler_config
.Tuple of dictionaries as described above, with an optional
"frequency"
key.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 thetorch.optim.lr_scheduler.ReduceLROnPlateau
scheduler, Lightning requires that thelr_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 yourLightningModule
.Note
The
frequency
value specified in a dict along with theoptimizer
key is an int corresponding to the number of sequential batches optimized with the specific optimizer. It should be given to none or to all of the optimizers. There is a difference between passing multiple optimizers in a list, and passing multiple optimizers in dictionaries with a frequency of 1:In the former case, all optimizers will operate on the given batch in each optimization step.
In the latter, only one optimizer will operate on the given batch at every step.
This is different from the
frequency
value specified in thelr_scheduler_config
mentioned above.def configure_optimizers(self): optimizer_one = torch.optim.SGD(self.model.parameters(), lr=0.01) optimizer_two = torch.optim.SGD(self.model.parameters(), lr=0.01) return [ {"optimizer": optimizer_one, "frequency": 5}, {"optimizer": optimizer_two, "frequency": 10}, ]
In this example, the first optimizer will be used for the first 5 steps, the second optimizer for the next 10 steps and that cycle will continue. If an LR scheduler is specified for an optimizer using the
lr_scheduler
key in the above dict, the scheduler will only be updated when its optimizer is being used.Examples:
# most cases. no learning rate scheduler def configure_optimizers(self): return Adam(self.parameters(), lr=1e-3) # multiple optimizer case (e.g.: GAN) def configure_optimizers(self): gen_opt = Adam(self.model_gen.parameters(), lr=0.01) dis_opt = Adam(self.model_dis.parameters(), lr=0.02) return gen_opt, dis_opt # example with learning rate schedulers def configure_optimizers(self): gen_opt = Adam(self.model_gen.parameters(), lr=0.01) dis_opt = Adam(self.model_dis.parameters(), lr=0.02) dis_sch = CosineAnnealing(dis_opt, T_max=10) return [gen_opt, dis_opt], [dis_sch] # example with step-based learning rate schedulers # each optimizer has its own scheduler def configure_optimizers(self): gen_opt = Adam(self.model_gen.parameters(), lr=0.01) dis_opt = Adam(self.model_dis.parameters(), lr=0.02) gen_sch = { 'scheduler': ExponentialLR(gen_opt, 0.99), 'interval': 'step' # called after each training step } dis_sch = CosineAnnealing(dis_opt, T_max=10) # called every epoch return [gen_opt, dis_opt], [gen_sch, dis_sch] # example with optimizer frequencies # see training procedure in `Improved Training of Wasserstein GANs`, Algorithm 1 # https://arxiv.org/abs/1704.00028 def configure_optimizers(self): gen_opt = Adam(self.model_gen.parameters(), lr=0.01) dis_opt = Adam(self.model_dis.parameters(), lr=0.02) n_critic = 5 return ( {'optimizer': dis_opt, 'frequency': n_critic}, {'optimizer': gen_opt, 'frequency': 1} )
Note
Some things to know:
Lightning calls
.backward()
and.step()
on each optimizer as needed.If 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 optimizers.If you use multiple optimizers,
training_step()
will have an additionaloptimizer_idx
parameter.If you use
torch.optim.LBFGS
, Lightning handles the closure function automatically for you.If you use multiple optimizers, gradients will be calculated only for the parameters of current optimizer at each training step.
If you need to control how often those optimizers step or override the default
.step()
schedule, override theoptimizer_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.
-
set_defaults
() Sets default for backward compatibility purposes.
-
test_epoch_end
(validation_step_outputs) Called at the end of a test epoch with the output of all test steps.
# the pseudocode for these calls test_outs = [] for test_batch in test_data: out = test_step(test_batch) test_outs.append(out) test_epoch_end(test_outs)
- Parameters
outputs – List of outputs you defined in
test_step_end()
, or if there are multiple dataloaders, a list containing a list of outputs for each dataloader- Returns
None
Note
If you didn’t define a
test_step()
, this won’t be called.Examples
With a single dataloader:
def test_epoch_end(self, outputs): # do something with the outputs of all test batches all_test_preds = test_step_outputs.predictions some_result = calc_all_results(all_test_preds) self.log(some_result)
With multiple dataloaders, outputs will be a list of lists. The outer list contains one entry per dataloader, while the inner list contains the individual outputs of each test step for that dataloader.
def test_epoch_end(self, outputs): final_value = 0 for dataloader_outputs in outputs: for test_step_out in dataloader_outputs: # do something final_value += test_step_out self.log("final_metric", final_value)
-
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.
# the pseudocode for these calls test_outs = [] for test_batch in test_data: out = test_step(test_batch) test_outs.append(out) test_epoch_end(test_outs)
- Parameters
batch – The output of your
DataLoader
.batch_idx – The index of this batch.
dataloader_id – The index of the dataloader that produced this batch. (only if multiple test dataloaders used).
- Returns
Any of.
Any object or value
None
- Testing will 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 (
Tensor
| (Tensor
, …) | [Tensor
, …]) – The output of yourDataLoader
. A tensor, tuple or list.batch_idx (
int
) – Integer displaying index of this batchoptimizer_idx (
int
) – When using multiple optimizers, this argument will also be present.hiddens (
Any
) – Passed in if :paramref:`~pytorch_lightning.core.module.LightningModule.truncated_bptt_steps` > 0.
- Returns
Any of.
Tensor
- The loss tensordict
- A dictionary. Can include any keys, but must include the key'loss'
None
- Training will skip to the next batch. This is only for automatic optimization.This is not supported for multi-GPU, TPU, IPU, or DeepSpeed.
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
If you define multiple optimizers, this step will be called with an additional
optimizer_idx
parameter.# Multiple optimizers (e.g.: GANs) def training_step(self, batch, batch_idx, optimizer_idx): if optimizer_idx == 0: # do training_step with encoder ... if optimizer_idx == 1: # do training_step with decoder ...
If you add truncated back propagation through time you will also get an additional argument with the hidden states of the previous step.
# Truncated back-propagation through time def training_step(self, batch, batch_idx, hiddens): # hiddens are the hidden states from the previous truncated backprop step out, hiddens = self.lstm(data, hiddens) loss = ... return {"loss": loss, "hiddens": hiddens}
Note
The loss value shown in the progress bar is smoothed (averaged) over the last values, so it differs from the actual loss returned in train/validation step.
Note
When
accumulate_grad_batches
> 1, the loss returned here will be automatically normalized byaccumulate_grad_batches
internally.
-
validation_epoch_end
(validation_step_outputs) Called at the end of the validation epoch with the outputs of all validation steps.
# the pseudocode for these calls val_outs = [] for val_batch in val_data: out = validation_step(val_batch) val_outs.append(out) validation_epoch_end(val_outs)
- Parameters
outputs – List of outputs you defined in
validation_step()
, or if there are multiple dataloaders, a list containing a list of outputs for each dataloader.- Returns
None
Note
If you didn’t define a
validation_step()
, this won’t be called.Examples
With a single dataloader:
def validation_epoch_end(self, val_step_outputs): for out in val_step_outputs: ...
With multiple dataloaders, outputs will be a list of lists. The outer list contains one entry per dataloader, while the inner list contains the individual outputs of each validation step for that dataloader.
def validation_epoch_end(self, outputs): for dataloader_output_result in outputs: dataloader_outs = dataloader_output_result.dataloader_i_outputs self.log("final_metric", final_value)
-
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.
# the pseudocode for these calls val_outs = [] for val_batch in val_data: out = validation_step(val_batch) val_outs.append(out) validation_epoch_end(val_outs)
- Parameters
batch – The output of your
DataLoader
.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple val dataloaders used)
- Returns
Any object or value
None
- Validation will skip to the next batch
# pseudocode of order val_outs = [] for val_batch in val_data: out = validation_step(val_batch) if defined("validation_step_end"): out = validation_step_end(out) val_outs.append(out) val_outs = validation_epoch_end(val_outs)
# 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.
Attributes: 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
() Implement one or multiple PyTorch DataLoaders for testing.
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
test()
prepare_data()
Note
Lightning adds the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.
- Returns
A
torch.utils.data.DataLoader
or a sequence of them specifying testing samples.
Example:
def test_dataloader(self): transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))]) dataset = MNIST(root='/path/to/mnist/', train=False, transform=transform, download=True) loader = torch.utils.data.DataLoader( dataset=dataset, batch_size=self.batch_size, shuffle=False ) return loader # can also return multiple dataloaders def test_dataloader(self): return [loader_a, loader_b, ..., loader_n]
Note
If you don’t need a test dataset and a
test_step()
, you don’t need to implement this method.Note
In the case where you return multiple test dataloaders, the
test_step()
will have an argumentdataloader_idx
which matches the order here.
-
train_dataloader
() Implement one or more PyTorch DataLoaders for training.
- Returns
A collection of
torch.utils.data.DataLoader
specifying training samples. In the case of multiple dataloaders, please see this section.
The dataloader you return will not be reloaded unless you set :paramref:`~pytorch_lightning.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
fit()
prepare_data()
Note
Lightning adds the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.
Example:
# single dataloader def train_dataloader(self): transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))]) dataset = MNIST(root='/path/to/mnist/', train=True, transform=transform, download=True) loader = torch.utils.data.DataLoader( dataset=dataset, batch_size=self.batch_size, shuffle=True ) return loader # multiple dataloaders, return as list def train_dataloader(self): mnist = MNIST(...) cifar = CIFAR(...) mnist_loader = torch.utils.data.DataLoader( dataset=mnist, batch_size=self.batch_size, shuffle=True ) cifar_loader = torch.utils.data.DataLoader( dataset=cifar, batch_size=self.batch_size, shuffle=True ) # each batch will be a list of tensors: [batch_mnist, batch_cifar] return [mnist_loader, cifar_loader] # multiple dataloader, return as dict def train_dataloader(self): mnist = MNIST(...) cifar = CIFAR(...) mnist_loader = torch.utils.data.DataLoader( dataset=mnist, batch_size=self.batch_size, shuffle=True ) cifar_loader = torch.utils.data.DataLoader( dataset=cifar, batch_size=self.batch_size, shuffle=True ) # each batch will be a dict of tensors: {'mnist': batch_mnist, 'cifar': batch_cifar} return {'mnist': mnist_loader, 'cifar': cifar_loader}
-
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.Note
This hook only runs on single GPU training and DDP (no data-parallel). Data-Parallel support will come in near future.
- 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(data, device, dataloader_idx) return batch
- Raises
MisconfigurationException – If using data-parallel,
Trainer(strategy='dp')
.MisconfigurationException – If using IPUs,
Trainer(accelerator='ipu')
.
See also
move_data_to_device()
apply_to_collection()
-
val_dataloader
() Implement one or multiple PyTorch DataLoaders for validation.
The dataloader you return will not be reloaded unless you set :paramref:`~pytorch_lightning.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()
Note
Lightning adds the correct sampler for distributed and arbitrary hardware There is no need to set it yourself.
- Returns
A
torch.utils.data.DataLoader
or a sequence of them specifying validation samples.
Examples:
def val_dataloader(self): transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))]) dataset = MNIST(root='/path/to/mnist/', train=False, transform=transform, download=True) loader = torch.utils.data.DataLoader( dataset=dataset, batch_size=self.batch_size, shuffle=False ) return loader # can also return multiple dataloaders def val_dataloader(self): return [loader_a, loader_b, ..., loader_n]
Note
If you don’t need a validation dataset and a
validation_step()
, you don’t need to implement this method.Note
In the case where you return multiple validation dataloaders, the
validation_step()
will have an argumentdataloader_idx
which matches the order here.
-
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.
relies on the following law (It) – 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