Shortcuts

mmedit.apis.inferencers

Package Contents

Classes

MMEditInferencer

Class to assign task to different inferencers.

Functions

calculate_grid_size(→ int)

Calculate the number of images per row (nrow) to make the grid closer to

colorization_inference(model, img)

Inference image with the model.

delete_cfg(cfg[, key])

Delete key from config object.

init_model(config[, checkpoint, device])

Initialize a model from config file.

inpainting_inference(model, masked_img, mask)

Inference image with the model.

matting_inference(model, img, trimap)

Inference image(s) with the model.

restoration_face_inference(model, img[, ...])

Inference image with the model.

restoration_inference(model, img[, ref])

Inference image with the model.

restoration_video_inference(model, img_dir, ...[, ...])

Inference image with the model.

sample_conditional_model(model[, num_samples, ...])

Sampling from conditional models.

sample_img2img_model(model, image_path[, target_domain])

Sampling from translation models.

sample_unconditional_model(model[, num_samples, ...])

Sampling from unconditional models.

set_random_seed(seed[, deterministic, use_rank_shift])

Set random seed.

video_interpolation_inference(model, input_dir, output_dir)

Inference image with the model.

mmedit.apis.inferencers.calculate_grid_size(num_batches: int = 1, aspect_ratio: int = 1) int[source]

Calculate the number of images per row (nrow) to make the grid closer to square when formatting a batch of images to grid.

Parameters
  • num_batches (int, optional) – Number of images per batch. Defaults to 1.

  • aspect_ratio (int, optional) – The aspect ratio (width / height) of each image sample. Defaults to 1.

Returns

Calculated number of images per row.

Return type

int

mmedit.apis.inferencers.colorization_inference(model, img)[source]

Inference image with the model.

Parameters
  • model (nn.Module) – The loaded model.

  • img (str) – Image file path.

Returns

The predicted colorization result.

Return type

Tensor

mmedit.apis.inferencers.delete_cfg(cfg, key='init_cfg')[source]

Delete key from config object.

Parameters
  • cfg (str or mmengine.Config) – Config object.

  • key (str) – Which key to delete.

mmedit.apis.inferencers.init_model(config, checkpoint=None, device='cuda:0')[source]

Initialize a model from config file.

Parameters
  • config (str or mmengine.Config) – Config file path or the config object.

  • checkpoint (str, optional) – Checkpoint path. If left as None, the model will not load any weights.

  • device (str) – Which device the model will deploy. Default: ‘cuda:0’.

Returns

The constructed model.

Return type

nn.Module

mmedit.apis.inferencers.inpainting_inference(model, masked_img, mask)[source]

Inference image with the model.

Parameters
  • model (nn.Module) – The loaded model.

  • masked_img (str) – File path of image with mask.

  • mask (str) – Mask file path.

Returns

The predicted inpainting result.

Return type

Tensor

mmedit.apis.inferencers.matting_inference(model, img, trimap)[source]

Inference image(s) with the model.

Parameters
  • model (nn.Module) – The loaded model.

  • img (str) – Image file path.

  • trimap (str) – Trimap file path.

Returns

The predicted alpha matte.

Return type

np.ndarray

mmedit.apis.inferencers.restoration_face_inference(model, img, upscale_factor=1, face_size=1024)[source]

Inference image with the model.

Parameters
  • model (nn.Module) – The loaded model.

  • img (str) – File path of input image.

  • upscale_factor (int, optional) – The number of times the input image is upsampled. Default: 1.

  • face_size (int, optional) – The size of the cropped and aligned faces. Default: 1024.

Returns

The predicted restoration result.

Return type

Tensor

mmedit.apis.inferencers.restoration_inference(model, img, ref=None)[source]

Inference image with the model.

Parameters
  • model (nn.Module) – The loaded model.

  • img (str) – File path of input image.

  • ref (str | None) – File path of reference image. Default: None.

Returns

The predicted restoration result.

Return type

Tensor

mmedit.apis.inferencers.restoration_video_inference(model, img_dir, window_size, start_idx, filename_tmpl, max_seq_len=None)[source]

Inference image with the model.

Parameters
  • model (nn.Module) – The loaded model.

  • img_dir (str) – Directory of the input video.

  • window_size (int) – The window size used in sliding-window framework. This value should be set according to the settings of the network. A value smaller than 0 means using recurrent framework.

  • start_idx (int) – The index corresponds to the first frame in the sequence.

  • filename_tmpl (str) – Template for file name.

  • max_seq_len (int | None) – The maximum sequence length that the model processes. If the sequence length is larger than this number, the sequence is split into multiple segments. If it is None, the entire sequence is processed at once.

Returns

The predicted restoration result.

Return type

Tensor

mmedit.apis.inferencers.sample_conditional_model(model, num_samples=16, num_batches=4, sample_model='ema', label=None, **kwargs)[source]

Sampling from conditional models.

Parameters
  • model (nn.Module) – Conditional models in MMGeneration.

  • num_samples (int, optional) – The total number of samples. Defaults to 16.

  • num_batches (int, optional) – The number of batch size for inference. Defaults to 4.

  • sample_model (str, optional) – Which model you want to use. [‘ema’, ‘orig’]. Defaults to ‘ema’.

  • label (int | torch.Tensor | list[int], optional) – Labels used to generate images. Default to None.,

Returns

Generated image tensor.

Return type

Tensor

mmedit.apis.inferencers.sample_img2img_model(model, image_path, target_domain=None, **kwargs)[source]

Sampling from translation models.

Parameters
  • model (nn.Module) – The loaded model.

  • image_path (str) – File path of input image.

  • style (str) – Target style of output image.

Returns

Translated image tensor.

Return type

Tensor

mmedit.apis.inferencers.sample_unconditional_model(model, num_samples=16, num_batches=4, sample_model='ema', **kwargs)[source]

Sampling from unconditional models.

Parameters
  • model (nn.Module) – Unconditional models in MMGeneration.

  • num_samples (int, optional) – The total number of samples. Defaults to 16.

  • num_batches (int, optional) – The number of batch size for inference. Defaults to 4.

  • sample_model (str, optional) – Which model you want to use. [‘ema’, ‘orig’]. Defaults to ‘ema’.

Returns

Generated image tensor.

Return type

Tensor

mmedit.apis.inferencers.set_random_seed(seed, deterministic=False, use_rank_shift=True)[source]

Set random seed.

In this function, we just modify the default behavior of the similar function defined in MMCV.

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.

  • rank_shift (bool) – Whether to add rank number to the random seed to have different random seed in different threads. Default: True.

mmedit.apis.inferencers.video_interpolation_inference(model, input_dir, output_dir, start_idx=0, end_idx=None, batch_size=4, fps_multiplier=0, fps=0, filename_tmpl='{:08d}.png')[source]

Inference image with the model.

Parameters
  • model (nn.Module) – The loaded model.

  • input_dir (str) – Directory of the input video.

  • output_dir (str) – Directory of the output video.

  • start_idx (int) – The index corresponding to the first frame in the sequence. Default: 0

  • end_idx (int | None) – The index corresponding to the last interpolated frame in the sequence. If it is None, interpolate to the last frame of video or sequence. Default: None

  • batch_size (int) – Batch size. Default: 4

  • fps_multiplier (float) – multiply the fps based on the input video. Default: 0.

  • fps (float) – frame rate of the output video. Default: 0.

  • filename_tmpl (str) – template of the file names. Default: ‘{:08d}.png’

class mmedit.apis.inferencers.MMEditInferencer(task: Optional[str] = None, config: Optional[Union[mmedit.utils.ConfigType, str]] = None, ckpt: Optional[str] = None, device: torch.device = None, extra_parameters: Optional[Dict] = None, seed: int = 2022)[source]

Class to assign task to different inferencers.

Parameters
  • task (str) – Inferencer task.

  • config (str or ConfigType) – Model config or the path to it.

  • ckpt (str, optional) – Path to the checkpoint.

  • device (str, optional) – Device to run inference. If None, the best device will be automatically used.

  • seed (int) – The random seed used in inference. Defaults to 2022.

__call__(**kwargs) Union[Dict, List[Dict]][source]

Call the inferencer.

Parameters

kwargs – Keyword arguments for the inferencer.

Returns

Results of inference pipeline.

Return type

Union[Dict, List[Dict]]

get_extra_parameters() List[str][source]

Each inferencer may has its own parameters. Call this function to get these parameters.

Returns

List of unique parameters.

Return type

List[str]

Read the Docs v: latest
Versions
master
latest
stable
zyh-re-docs
zyh-doc-notfound-extend
zyh-api-rendering
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.