Dense Flow Prediction Network¶
Module containing the implementation of the Dense Flow Prediction Network (DFPN) and the methods required to train it using PyTorch Lightning.
- class master_thesis.model_dfpn.DFPN(model_vgg, **kwargs)¶
Bases:
pytorch_lightning.core.lightning.LightningModule
Implementation of the Dense Flow Prediction Network (DFPN).
- LOSSES_NAMES = ['corr_loss', 'flow_16', 'flow_64', 'flow_256', 'alignment_recons_64', 'alignment_recons_256']¶
- forward(x_target, m_target, x_refs, m_refs)¶
Forward pass through the Dense Flow Prediction Network (DFPN).
- Parameters
x_target –
m_target –
x_refs –
m_refs –
Returns:
- configure_optimizers()¶
Configures the optimizer used to train the Dense Flow Prediction Network (DFPN).
- Returns
Configured
torch.optim.Adam
optimizer object.
- on_epoch_start()¶
Called when either of train/val/test epoch begins.
- training_step(batch, batch_idx)¶
- Parameters
batch –
batch_idx –
- Returns
The loss.
- validation_step(batch, batch_idx)¶
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 (
Tensor
| (Tensor
, …) | [Tensor
, …]) – The output of yourDataLoader
. A tensor, tuple or list.batch_idx (int) – The index of this batch
dataloader_idx (int) – 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): ...
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.# CASE 2: multiple validation dataloaders def validation_step(self, batch, batch_idx, dataloader_idx): # 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.
- test_step(batch, batch_idx)¶
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 (
Tensor
| (Tensor
, …) | [Tensor
, …]) – The output of yourDataLoader
. A tensor, tuple or list.batch_idx (int) – The index of this batch.
dataloader_idx (int) – 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): ...
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.# CASE 2: multiple test dataloaders def test_step(self, batch, batch_idx, dataloader_idx): # 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.
- compute_loss(corr, xs, vs, ys, xs_aligned, flows, flows_gt, flows_use, t, r_list)¶
- static train_step_propagate(model, x, m, y, flow_gt, flows_use, t, r_list)¶
- static infer_step_propagate(model, x_target, m_target, x_ref, m_ref)¶
- static get_indexes(size)¶
- static init_model_with_state(checkpoint_path, device='cpu')¶
- training: bool¶
- class master_thesis.model_dfpn.CorrelationVGG(model_vgg, use_softmax=False)¶
Bases:
torch.nn.modules.module.Module
- forward(x_target, m_target, x_ref, m_ref)¶
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.
- static correlation_masked_4d(x_target_feats, v_target, x_ref_feats, v_ref)¶
Computes the normalized correlation between the feature maps of t and r_list.
- Parameters
x_target_feats – tensor of size (B,C,H,W) containing the feature map
frame. (visibility map of the target) –
v_target – tensor of size (B,1,H,W) containing
frame. –
x_ref_feats – tensor of size (B,C,F,H,W)
frames. (visibility maps of the reference) –
v_ref – tensor of size (B,1,H,W) containing
frames. –
- Returns
4D correlation volume of size (B,F,H,W,H,W).
- static softmax_3d(x)¶
- training: bool¶
- class master_thesis.model_dfpn.SeparableConv4d(in_c=1, out_c=1)¶
Bases:
torch.nn.modules.module.Module
Implementation of the separable convolution module of the DFPN.
- forward(corr)¶
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 master_thesis.model_dfpn.AlignmentCorrelationMixer(corr_size=16)¶
Bases:
torch.nn.modules.module.Module
- forward(corr)¶
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 master_thesis.model_dfpn.FlowEstimator(in_c=10)¶
Bases:
torch.nn.modules.module.Module
- forward(x_target, m_target, x_ref, m_ref, flow_pre)¶
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¶