Models¶
Vortex provides several pre-defined models you can use directly. All those models
share the use of common base class ModelBase
.
-
class
vortex.development.networks.models.
ModelBase
¶ -
abstract property
available_metrics
¶ explain all available metrics and the strategy for getting the best value of it (‘max’ or ‘min’) as dict. or just return all available metrics as list but the strategy will be infered by the name of the metrics, if the name contains ‘loss’, the strategy is ‘min’ else use ‘max’.
example:
{ 'val_loss': 'min', 'accuracy': 'max' }
or:
['val_loss', 'accuracy']
-
configure_optimizers
()¶ Create optimizer and lr_scheduler
-
on_export_end
(exporter, exported_model)¶ This method will be called after the model is exported
- Parameters
exporter – exporter that is exporting this model
exported_model – exported model by the exporter
-
on_export_start
(exporter)¶ This method will be called at the start of export session.
- Parameters
exporter – exporter that is exporting this model
-
abstract property
output_format
¶ Define the format (‘indices’ and ‘axis’ for each output) to extract the output for single batch.
-
abstract
predict
(*args, **kwargs) → Union[List[torch.Tensor], torch.Tensor]¶ Predict function, called in inference includes preprocess, and postprocess. Expected:
input -> image in tensor/numpy with dim (NHWC) [N: batch, H: height, W: width, C: channel]
output -> list of tensors each member of the list corresponds to prediction of the image, or a tensor of dim (NxP) [N:batch, P: prediction]
-
test_epoch_end
(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_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
- 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.
-
test_step_end
(outputs)¶ Use this when testing with dp or ddp2 because
test_step()
will operate on only part of the batch. However, this is still optional and only needed for things like softmax or NCE loss.Note
If you later switch to ddp or some other mode, this will still be called so that you don’t have to change your code.
# pseudocode sub_batches = split_batches_for_dp(batch) batch_parts_outputs = [test_step(sub_batch) for sub_batch in sub_batches] test_step_end(batch_parts_outputs)
- Parameters
batch_parts_outputs – What you return in
test_step()
for each batch part.- Returns
None or anything
# WITHOUT test_step_end # if used in DP or DDP2, this batch is 1/num_gpus large def test_step(self, batch, batch_idx): # batch is 1/num_gpus big x, y = batch out = self(x) loss = self.softmax(out) self.log('test_loss', loss) # -------------- # with test_step_end to do softmax over the full batch def test_step(self, batch, batch_idx): # batch is 1/num_gpus big x, y = batch out = self.encoder(x) return out def test_step_end(self, output_results): # this out is now the full size of the batch all_test_step_outs = output_results.out loss = nce_loss(all_test_step_outs) self.log('test_loss', loss)
See also
See the multi_gpu guide for more details.
-
abstract property