Shortcuts

mmedit.apis.inferencers.inference_functions

Module Contents

Functions

set_random_seed(seed[, deterministic, use_rank_shift])

Set random seed.

delete_cfg(cfg[, key])

Delete key from config object.

init_model(config[, checkpoint, device])

Initialize a model from config file.

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

Sampling from unconditional models.

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

Sampling from conditional models.

inpainting_inference(model, masked_img, mask)

Inference image with the model.

matting_inference(model, img, trimap)

Inference image(s) with the model.

sample_img2img_model(model, image_path[, target_domain])

Sampling from translation models.

restoration_inference(model, img[, ref])

Inference image with the model.

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

Inference image with the model.

pad_sequence(data, window_size)

Pad frame sequence data.

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

Inference image with the model.

read_image(filepath)

Read image from file.

read_frames(source, start_index, num_frames, ...)

Read frames from file or video.

video_interpolation_inference(model, input_dir, output_dir)

Inference image with the model.

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

Attributes

VIDEO_EXTENSIONS

FILE_CLIENT

has_facexlib

mmedit.apis.inferencers.inference_functions.VIDEO_EXTENSIONS = ('.mp4', '.mov', '.avi')[source]
mmedit.apis.inferencers.inference_functions.FILE_CLIENT[source]
mmedit.apis.inferencers.inference_functions.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.inference_functions.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.inference_functions.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.inference_functions.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.inference_functions.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.inference_functions.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.inference_functions.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.inference_functions.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.inference_functions.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.inference_functions.has_facexlib = True[source]
mmedit.apis.inferencers.inference_functions.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.inference_functions.pad_sequence(data, window_size)[source]

Pad frame sequence data.

Parameters
  • data (Tensor) – The frame sequence data.

  • window_size (int) – The window size used in sliding-window framework.

Returns

The padded result.

Return type

data (Tensor)

mmedit.apis.inferencers.inference_functions.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.inference_functions.read_image(filepath)[source]

Read image from file.

Parameters

filepath (str) – File path.

Returns

Image.

Return type

image (np.array)

mmedit.apis.inferencers.inference_functions.read_frames(source, start_index, num_frames, from_video, end_index)[source]

Read frames from file or video.

Parameters
  • source (list | mmcv.VideoReader) – Source of frames.

  • start_index (int) – Start index of frames.

  • num_frames (int) – frames number to be read.

  • from_video (bool) – Weather read frames from video.

  • end_index (int) – The end index of frames.

Returns

Images.

Return type

images (np.array)

mmedit.apis.inferencers.inference_functions.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’

mmedit.apis.inferencers.inference_functions.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.inference_functions.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

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.