Shortcuts

mmedit.apis

Package Contents

Classes

MMEditInferencer

Class to assign task to different inferencers.

Functions

init_model(config[, checkpoint, device])

Initialize a model from config file.

delete_cfg(cfg[, key])

Delete key from config object.

set_random_seed(seed[, deterministic, use_rank_shift])

Set random seed.

matting_inference(model, img, trimap)

Inference image(s) with the model.

inpainting_inference(model, masked_img, mask)

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.

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

Inference image with the model.

video_interpolation_inference(model, input_dir, output_dir)

Inference image with the model.

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

Sampling from conditional models.

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

Sampling from unconditional models.

sample_img2img_model(model, image_path[, target_domain])

Sampling from translation models.

colorization_inference(model, img)

Inference image with the model.

calculate_grid_size(→ int)

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

class mmedit.apis.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)

Class to assign task to different inferencers.

参数
  • 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]]

Call the inferencer.

参数

kwargs – Keyword arguments for the inferencer.

返回

Results of inference pipeline.

返回类型

Union[Dict, List[Dict]]

get_extra_parameters() List[str]

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

返回

List of unique parameters.

返回类型

List[str]

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

Initialize a model from config file.

参数
  • 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’.

返回

The constructed model.

返回类型

nn.Module

mmedit.apis.delete_cfg(cfg, key='init_cfg')

Delete key from config object.

参数
  • cfg (str or mmengine.Config) – Config object.

  • key (str) – Which key to delete.

mmedit.apis.set_random_seed(seed, deterministic=False, use_rank_shift=True)

Set random seed.

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

参数
  • 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.matting_inference(model, img, trimap)

Inference image(s) with the model.

参数
  • model (nn.Module) – The loaded model.

  • img (str) – Image file path.

  • trimap (str) – Trimap file path.

返回

The predicted alpha matte.

返回类型

np.ndarray

mmedit.apis.inpainting_inference(model, masked_img, mask)

Inference image with the model.

参数
  • model (nn.Module) – The loaded model.

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

  • mask (str) – Mask file path.

返回

The predicted inpainting result.

返回类型

Tensor

mmedit.apis.restoration_inference(model, img, ref=None)

Inference image with the model.

参数
  • model (nn.Module) – The loaded model.

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

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

返回

The predicted restoration result.

返回类型

Tensor

mmedit.apis.restoration_video_inference(model, img_dir, window_size, start_idx, filename_tmpl, max_seq_len=None)

Inference image with the model.

参数
  • 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.

返回

The predicted restoration result.

返回类型

Tensor

mmedit.apis.restoration_face_inference(model, img, upscale_factor=1, face_size=1024)

Inference image with the model.

参数
  • 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.

返回

The predicted restoration result.

返回类型

Tensor

mmedit.apis.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')

Inference image with the model.

参数
  • 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’

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

Sampling from conditional models.

参数
  • 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.,

返回

Generated image tensor.

返回类型

Tensor

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

Sampling from unconditional models.

参数
  • 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’.

返回

Generated image tensor.

返回类型

Tensor

mmedit.apis.sample_img2img_model(model, image_path, target_domain=None, **kwargs)

Sampling from translation models.

参数
  • model (nn.Module) – The loaded model.

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

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

返回

Translated image tensor.

返回类型

Tensor

mmedit.apis.colorization_inference(model, img)

Inference image with the model.

参数
  • model (nn.Module) – The loaded model.

  • img (str) – Image file path.

返回

The predicted colorization result.

返回类型

Tensor

mmedit.apis.calculate_grid_size(num_batches: int = 1, aspect_ratio: int = 1) int

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

参数
  • 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.

返回

Calculated number of images per row.

返回类型

int

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

Free document hosting provided by Read the Docs.