easycv.apis package

Submodules

easycv.apis.export module

easycv.apis.export.export(cfg, ckpt_path, filename)[source]

export model for inference

Parameters
  • cfg – Config object

  • ckpt_path (str) – path to checkpoint file

  • filename (str) – filename to save exported models

class easycv.apis.export.PreProcess(target_size: Tuple[int, int] = (640, 640), keep_ratio: bool = True)[source]

Bases: object

Process the data input to model.

Parameters
  • target_size (Tuple[int, int]) – output spatial size.

  • keep_ratio (bool) – Whether to keep the aspect ratio when resizing the image.

__init__(target_size: Tuple[int, int] = (640, 640), keep_ratio: bool = True)[source]

Initialize self. See help(type(self)) for accurate signature.

class easycv.apis.export.DetPostProcess(max_det: int = 100, score_thresh: float = 0.5)[source]

Bases: object

Process output values of detection models.

Parameters

max_det – max number of detections to keep.

__init__(max_det: int = 100, score_thresh: float = 0.5)[source]

Initialize self. See help(type(self)) for accurate signature.

class easycv.apis.export.End2endModelExportWrapper(model, example_inputs, preprocess_fn: Optional[Callable] = None, postprocess_fn: Optional[Callable] = None, trace_model: bool = True)[source]

Bases: torch.nn.modules.module.Module

Model export wrapper that supports end-to-end export of pre-processing and post-processing. We support some built-in preprocessing and postprocessing functions. If the requirements are not met, you can customize the preprocessing and postprocessing functions. The custom functions must support satisfy requirements of torch.jit.script, please refer to: https://pytorch.org/docs/stable/jit_language_reference_v2.html

Parameters
  • model (torch.nn.Module) – torch.nn.Module that will be run with example_inputs. model arguments and return values must be tensors or (possibly nested) tuples that contain tensors. When a module is passed torch.jit.trace, only the forward_export method is run and traced (see torch.jit.trace for details).

  • example_inputs (tuple or torch.Tensor) – A tuple of example inputs that will be passed to the function while tracing. The resulting trace can be run with inputs of different types and shapes assuming the traced operations support those types and shapes. example_inputs may also be a single Tensor in which case it is automatically wrapped in a tuple.

  • preprocess_fn (callable or None) – A Python function for processing example_input. If there is only one return value, it will be passed to model.forward_export. If there are multiple return values, the first return value will be passed to model.forward_export, and the remaining return values ​​will be passed to postprocess_fn.

  • postprocess_fn (callable or None) – A Python function for processing the output value of the model. If preprocess_fn has multiple outputs, the output value of preprocess_fn will also be passed to postprocess_fn. For details, please refer to: preprocess_fn.

  • trace_model (bool) – If True, before exporting the end-to-end model, torch.jit.trace will be used to export the model first. Traceing an nn.Module by default will compile the forward_export method and recursively.

Examples

import torch

batch_size = 1 example_inputs = 255 * torch.rand((batch_size, 3, 640, 640), device=’cuda’) end2end_model = End2endModelExportWrapper(

model, example_inputs, preprocess_fn=PreProcess(target_size=(640, 640)), # PreProcess refer to ev_torch.apis.export.PreProcess postprocess_fn=DetPostProcess() # DetPostProcess refer to ev_torch.apis.export.DetPostProcess trace_model=True)

model_script = torch.jit.script(end2end_model) with io.open(‘/tmp/model.jit’, ‘wb’) as f:

torch.jit.save(model_script, f)

__init__(model, example_inputs, preprocess_fn: Optional[Callable] = None, postprocess_fn: Optional[Callable] = None, trace_model: bool = True)None[source]

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

trace_module(**kwargs)[source]
training: bool
forward(image)[source]

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.

easycv.apis.test module

easycv.apis.test.single_cpu_test(model, data_loader, mode='test', show=False, out_dir=None, show_score_thr=0.3, **kwargs)[source]
easycv.apis.test.single_gpu_test(model, data_loader, mode='test', use_fp16=False, **kwargs)[source]

Test model with single.

This method tests model with single

Parameters
  • model (str) – Model to be tested.

  • data_loader (nn.Dataloader) – Pytorch data loader.

  • model – mode for model to forward

  • use_fp16 – Use fp16 inference

Returns

The prediction results.

Return type

list

easycv.apis.test.multi_gpu_test(model, data_loader, mode='test', tmpdir=None, gpu_collect=False, use_fp16=False, **kwargs)[source]

Test model with multiple gpus.

This method tests model with multiple gpus and collects the results under two different modes: gpu and cpu modes. By setting ‘gpu_collect=True’ it encodes results to gpu tensors and use gpu communication for results collection. On cpu mode it saves the results on different gpus to ‘tmpdir’ and collects them by the rank 0 worker.

Parameters
  • model (str) – Model to be tested.

  • data_loader (nn.Dataloader) – Pytorch data loader.

  • model – mode for model to forward

  • tmpdir (str) – Path of directory to save the temporary results from different gpus under cpu mode.

  • gpu_collect (bool) – Option to use either gpu or cpu to collect results.

  • use_fp16 – Use fp16 inference

Returns

The prediction results.

Return type

list

easycv.apis.test.collect_results_cpu(result_part, size, tmpdir=None)[source]
easycv.apis.test.serialize_tensor(tensor_collection)[source]
easycv.apis.test.collect_results_gpu(result_part, size)[source]

easycv.apis.train module

easycv.apis.train.set_random_seed(seed, deterministic=False)[source]

Set random seed.

Parameters
  • seed (int) – Seed to be used.

  • deterministic (bool) – Whether to set the deterministic option for CUDNN backend, i.e., set torch.backends.cudnn.deterministic to True and torch.backends.cudnn.benchmark to False. Default: False.

easycv.apis.train.train_model(model, data_loaders, cfg, distributed=False, timestamp=None, meta=None, use_fp16=False, validate=True, gpu_collect=True)[source]

Training API.

Parameters
  • model (nn.Module) – user defined model

  • data_loaders – a list of dataloader for training data

  • cfg – config object

  • distributed – distributed training or not

  • timestamp – time str formated as ‘%Y%m%d_%H%M%S’

  • meta – a dict containing meta data info, such as env_info, seed, iter, epoch

  • use_fp16 – use fp16 training or not

  • validate – do evaluation while training

  • gpu_collect – use gpu collect or cpu collect for tensor gathering

easycv.apis.train.get_skip_list_keywords(model)[source]
easycv.apis.train.build_optimizer(model, optimizer_cfg)[source]

Build optimizer from configs.

Parameters
  • model (nn.Module) – The model with parameters to be optimized.

  • optimizer_cfg (dict) –

    The config dict of the optimizer.

    Positional fields are:
    • type: class name of the optimizer.

    • lr: base learning rate.

    Optional fields are:
    • any arguments of the corresponding optimizer type, e.g., weight_decay, momentum, etc.

    • paramwise_options: a dict with regular expression as keys to match parameter names and a dict containing options as values. Options include 6 fields: lr, lr_mult, momentum, momentum_mult, weight_decay, weight_decay_mult.

Returns

The initialized optimizer.

Return type

torch.optim.Optimizer

Example

>>> model = torch.nn.modules.Conv1d(1, 1, 1)
>>> paramwise_options = {
>>>     '(bn|gn)(\d+)?.(weight|bias)': dict(weight_decay_mult=0.1),
>>>     '\Ahead.': dict(lr_mult=10, momentum=0)}
>>> optimizer_cfg = dict(type='SGD', lr=0.01, momentum=0.9,
>>>                      weight_decay=0.0001,
>>>                      paramwise_options=paramwise_options)
>>> optimizer = build_optimizer(model, optimizer_cfg)

easycv.apis.train_misc module

easycv.apis.train_misc.build_yolo_optimizer(model, optimizer_cfg)[source]

build optimizer for yolo.