Shortcuts

mmagic.utils

Package Contents

Functions

modify_args()

Modify args of argparse.ArgumentParser.

all_to_tensor(value)

Trans image and sequence of frames to tensor.

can_convert_to_image(value)

Judge whether the input value can be converted to image tensor via

get_box_info(pred_bbox, original_shape, final_size)

param pred_bbox

The bounding box for the instance

reorder_image(img[, input_order])

Reorder images to 'HWC' order.

tensor2img(tensor[, out_type, min_max])

Convert torch Tensors into image numpy arrays.

to_numpy(img[, dtype])

Convert data into numpy arrays of dtype.

download_from_url(url[, dest_path, dest_dir, hash_prefix])

Download object at the given URL to a local path.

print_colored_log(msg[, level, color])

Print colored log with default logger.

get_sampler(sample_kwargs, runner)

Get a sampler to loop input data.

register_all_modules(→ None)

Register all modules in mmagic into the registries.

try_import(→ Optional[types.ModuleType])

Try to import a module.

add_gaussian_noise(img, mu, sigma)

Add Gaussian Noise on the input image.

adjust_gamma(image[, gamma, gain])

Performs Gamma Correction on the input image.

bbox2mask(img_shape, bbox[, dtype])

Generate mask in np.ndarray from bbox.

brush_stroke_mask(img_shape[, num_vertices, ...])

Generate free-form mask.

get_irregular_mask(img_shape[, area_ratio_range])

Get irregular mask with the constraints in mask ratio.

make_coord(shape[, ranges, flatten])

Make coordinates at grid centers.

random_bbox(img_shape, max_bbox_shape[, ...])

Generate a random bbox for the mask on a given image.

random_choose_unknown(unknown, crop_size)

Randomly choose an unknown start (top-left) point for a given crop_size.

Attributes

MMAGIC_CACHE_DIR

ConfigType

ForwardInputs

LabelVar

NoiseVar

SampleList

mmagic.utils.modify_args()[源代码]

Modify args of argparse.ArgumentParser.

mmagic.utils.all_to_tensor(value)[源代码]

Trans image and sequence of frames to tensor.

参数

value (np.ndarray | list[np.ndarray] | Tuple[np.ndarray]) – The original image or list of frames.

返回

The output tensor.

返回类型

Tensor

mmagic.utils.can_convert_to_image(value)[源代码]

Judge whether the input value can be converted to image tensor via images_to_tensor() function.

参数

value (any) – The input value.

返回

If true, the input value can convert to image with

images_to_tensor(), and vice versa.

返回类型

bool

mmagic.utils.get_box_info(pred_bbox, original_shape, final_size)[源代码]
参数
  • pred_bbox – The bounding box for the instance

  • original_shape – Original image shape

  • final_size – Size of the final output

返回

[L_pad, R_pad, T_pad, B_pad, rh, rw]

返回类型

List

mmagic.utils.reorder_image(img, input_order='HWC')[源代码]

Reorder images to ‘HWC’ order.

If the input_order is (h, w), return (h, w, 1); If the input_order is (c, h, w), return (h, w, c); If the input_order is (h, w, c), return as it is.

参数
  • img (np.ndarray) – Input image.

  • input_order (str) – Whether the input order is ‘HWC’ or ‘CHW’. If the input image shape is (h, w), input_order will not have effects. Default: ‘HWC’.

返回

Reordered image.

返回类型

np.ndarray

mmagic.utils.tensor2img(tensor, out_type=np.uint8, min_max=(0, 1))[源代码]

Convert torch Tensors into image numpy arrays.

After clamping to (min, max), image values will be normalized to [0, 1].

For different tensor shapes, this function will have different behaviors:

  1. 4D mini-batch Tensor of shape (N x 3/1 x H x W):

    Use make_grid to stitch images in the batch dimension, and then convert it to numpy array.

  2. 3D Tensor of shape (3/1 x H x W) and 2D Tensor of shape (H x W):

    Directly change to numpy array.

Note that the image channel in input tensors should be RGB order. This function will convert it to cv2 convention, i.e., (H x W x C) with BGR order.

参数
  • tensor (Tensor | list[Tensor]) – Input tensors.

  • out_type (numpy type) – Output types. If np.uint8, transform outputs to uint8 type with range [0, 255]; otherwise, float type with range [0, 1]. Default: np.uint8.

  • min_max (tuple) – min and max values for clamp.

返回

3D ndarray of shape (H x W x C) or 2D ndarray of shape (H x W).

返回类型

(Tensor | list[Tensor])

mmagic.utils.to_numpy(img, dtype=np.float64)[源代码]

Convert data into numpy arrays of dtype.

参数
  • img (Tensor | np.ndarray) – Input data.

  • dtype (np.dtype) – Set the data type of the output. Default: np.float64

返回

Converted numpy arrays data.

返回类型

img (np.ndarray)

mmagic.utils.MMAGIC_CACHE_DIR[源代码]
mmagic.utils.download_from_url(url, dest_path=None, dest_dir=MMAGIC_CACHE_DIR, hash_prefix=None)[源代码]

Download object at the given URL to a local path.

参数
  • url (str) – URL of the object to download.

  • dest_path (str) – Path where object will be saved.

  • dest_dir (str) – The directory of the destination. Defaults to '~/.cache/openmmlab/mmagic/'.

  • hash_prefix (string, optional) – If not None, the SHA256 downloaded file should start with hash_prefix. Default: None.

返回

path for the downloaded file.

返回类型

str

mmagic.utils.print_colored_log(msg, level=logging.INFO, color='magenta')[源代码]

Print colored log with default logger.

参数
  • msg (str) – Message to log.

  • level (int) – The root logger level. Note that only the process of rank 0 is affected, while other processes will set the level to “Error” and be silent most of the time.Log level, default to ‘info’.

  • color (str, optional) – Color ‘magenta’.

mmagic.utils.get_sampler(sample_kwargs: dict, runner: Optional[mmengine.runner.Runner])[源代码]

Get a sampler to loop input data.

参数
  • sample_kwargs (dict) – _description_

  • runner (Optional[Runner]) – _description_

返回

_description_

返回类型

_type_

mmagic.utils.register_all_modules(init_default_scope: bool = True) None[源代码]

Register all modules in mmagic into the registries.

参数

init_default_scope (bool) – Whether initialize the mmagic default scope. When init_default_scope=True, the global default scope will be set to mmagic, and all registries will build modules from mmagic’s registry node. To understand more about the registry, please refer to https://mmengine.readthedocs.io/en/latest/advanced_tutorials/registry.html Defaults to True.

mmagic.utils.try_import(name: str) Optional[types.ModuleType][源代码]

Try to import a module.

参数

name (str) – Specifies what module to import in absolute or relative terms (e.g. either pkg.mod or ..mod).

返回

If importing successfully, returns the imported module, otherwise returns None.

返回类型

ModuleType or None

mmagic.utils.add_gaussian_noise(img: numpy.ndarray, mu, sigma)[源代码]

Add Gaussian Noise on the input image.

参数
  • img (np.ndarray) – Input image.

  • mu (float) – The mu value of the Gaussian function.

  • sigma (float) – The sigma value of the Gaussian function.

返回

Gaussian noisy output image.

返回类型

noisy_img (np.ndarray)

mmagic.utils.adjust_gamma(image, gamma=1, gain=1)[源代码]

Performs Gamma Correction on the input image.

This function is adopted from skimage: https://github.com/scikit-image/scikit-image/blob/ 7e4840bd9439d1dfb6beaf549998452c99f97fdd/skimage/exposure/ exposure.py#L439-L494

Also known as Power Law Transform. This function transforms the input image pixelwise according to the equation O = I**gamma after scaling each pixel to the range 0 to 1.

参数
  • image (np.ndarray) – Input image.

  • gamma (float, optional) – Non negative real number. Defaults to 1.

  • gain (float, optional) – The constant multiplier. Defaults to 1.

返回

Gamma corrected output image.

返回类型

np.ndarray

mmagic.utils.bbox2mask(img_shape, bbox, dtype='uint8')[源代码]

Generate mask in np.ndarray from bbox.

The returned mask has the shape of (h, w, 1). ‘1’ indicates the hole and ‘0’ indicates the valid regions.

We prefer to use uint8 as the data type of masks, which may be different from other codes in the community.

参数
  • img_shape (tuple[int]) – The size of the image.

  • bbox (tuple[int]) – Configuration tuple, (top, left, height, width)

  • np.dtype (str) – Indicate the data type of returned masks. Default: ‘uint8’

返回

Mask in the shape of (h, w, 1).

返回类型

mask (np.ndarray)

mmagic.utils.brush_stroke_mask(img_shape, num_vertices=(4, 12), mean_angle=2 * math.pi / 5, angle_range=2 * math.pi / 15, brush_width=(12, 40), max_loops=4, dtype='uint8')[源代码]

Generate free-form mask.

The method of generating free-form mask is in the following paper: Free-Form Image Inpainting with Gated Convolution.

When you set the config of this type of mask. You may note the usage of np.random.randint and the range of np.random.randint is [left, right).

We prefer to use uint8 as the data type of masks, which may be different from other codes in the community.

TODO: Rewrite the implementation of this function.

参数
  • img_shape (tuple[int]) – Size of the image.

  • num_vertices (int | tuple[int]) – Min and max number of vertices. If only give an integer, we will fix the number of vertices. Default: (4, 12).

  • mean_angle (float) – Mean value of the angle in each vertex. The angle is measured in radians. Default: 2 * math.pi / 5.

  • angle_range (float) – Range of the random angle. Default: 2 * math.pi / 15.

  • brush_width (int | tuple[int]) – (min_width, max_width). If only give an integer, we will fix the width of brush. Default: (12, 40).

  • max_loops (int) – The max number of for loops of drawing strokes. Default: 4.

  • np.dtype (str) – Indicate the data type of returned masks. Default: ‘uint8’.

返回

Mask in the shape of (h, w, 1).

返回类型

mask (np.ndarray)

mmagic.utils.get_irregular_mask(img_shape, area_ratio_range=(0.15, 0.5), **kwargs)[源代码]

Get irregular mask with the constraints in mask ratio.

参数
  • img_shape (tuple[int]) – Size of the image.

  • area_ratio_range (tuple(float)) – Contain the minimum and maximum area

  • Default (ratio.) – (0.15, 0.5).

返回

Mask in the shape of (h, w, 1).

返回类型

mask (np.ndarray)

mmagic.utils.make_coord(shape, ranges=None, flatten=True)[源代码]

Make coordinates at grid centers.

参数
  • shape (tuple) – shape of image.

  • ranges (tuple) – range of coordinate value. Default: None.

  • flatten (bool) – flatten to (n, 2) or Not. Default: True.

返回

coordinates.

返回类型

coord (Tensor)

mmagic.utils.random_bbox(img_shape, max_bbox_shape, max_bbox_delta=40, min_margin=20)[源代码]

Generate a random bbox for the mask on a given image.

In our implementation, the max value cannot be obtained since we use np.random.randint. And this may be different with other standard scripts in the community.

参数
  • img_shape (tuple[int]) – The size of a image, in the form of (h, w).

  • max_bbox_shape (int | tuple[int]) – Maximum shape of the mask box, in the form of (h, w). If it is an integer, the mask box will be square.

  • max_bbox_delta (int | tuple[int]) – Maximum delta of the mask box, in the form of (delta_h, delta_w). If it is an integer, delta_h and delta_w will be the same. Mask shape will be randomly sampled from the range of max_bbox_shape - max_bbox_delta and max_bbox_shape. Default: (40, 40).

  • min_margin (int | tuple[int]) – The minimum margin size from the edges of mask box to the image boarder, in the form of (margin_h, margin_w). If it is an integer, margin_h and margin_w will be the same. Default: (20, 20).

返回

The generated box, (top, left, h, w).

返回类型

tuple[int]

mmagic.utils.random_choose_unknown(unknown, crop_size)[源代码]

Randomly choose an unknown start (top-left) point for a given crop_size.

参数
  • unknown (np.ndarray) – The binary unknown mask.

  • crop_size (tuple[int]) – The given crop size.

返回

The top-left point of the chosen bbox.

返回类型

tuple[int]

mmagic.utils.ConfigType[源代码]
mmagic.utils.ForwardInputs[源代码]
mmagic.utils.LabelVar[源代码]
mmagic.utils.NoiseVar[源代码]
mmagic.utils.SampleList[源代码]
Read the Docs v: latest
Versions
latest
stable
0.x
Downloads
pdf
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.