Shortcuts

mmedit.utils

Package Contents

Functions

modify_args()

Modify args of argparse.ArgumentParser.

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 mmedit 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

MMEDIT_CACHE_DIR

ConfigType

ForwardInputs

LabelVar

NoiseVar

SampleList

mmedit.utils.modify_args()[source]

Modify args of argparse.ArgumentParser.

mmedit.utils.get_box_info(pred_bbox, original_shape, final_size)[source]
Parameters
  • pred_bbox – The bounding box for the instance

  • original_shape – Original image shape

  • final_size – Size of the final output

Returns

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

Return type

List

mmedit.utils.reorder_image(img, input_order='HWC')[source]

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.

Parameters
  • 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’.

Returns

Reordered image.

Return type

np.ndarray

mmedit.utils.tensor2img(tensor, out_type=np.uint8, min_max=(0, 1))[source]

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.

Parameters
  • 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.

Returns

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

Return type

(Tensor | list[Tensor])

mmedit.utils.to_numpy(img, dtype=np.float64)[source]

Convert data into numpy arrays of dtype.

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

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

Returns

Converted numpy arrays data.

Return type

img (np.ndarray)

mmedit.utils.MMEDIT_CACHE_DIR[source]
mmedit.utils.download_from_url(url, dest_path=None, dest_dir=MMEDIT_CACHE_DIR, hash_prefix=None)[source]

Download object at the given URL to a local path.

Parameters
  • 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/mmgen/'.

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

Returns

path for the downloaded file.

Return type

str

mmedit.utils.print_colored_log(msg, level=logging.INFO, color='magenta')[source]

Print colored log with default logger.

Parameters
  • 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’.

mmedit.utils.get_sampler(sample_kwargs: dict, runner: Optional[mmengine.runner.Runner])[source]

Get a sampler to loop input data.

Parameters
  • sample_kwargs (dict) – _description_

  • runner (Optional[Runner]) – _description_

Returns

_description_

Return type

_type_

mmedit.utils.register_all_modules(init_default_scope: bool = True) None[source]

Register all modules in mmedit into the registries.

Parameters

init_default_scope (bool) – Whether initialize the mmedit default scope. When init_default_scope=True, the global default scope will be set to mmedit, and all registries will build modules from mmedit’s registry node. To understand more about the registry, please refer to https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/registry.md Defaults to True.

mmedit.utils.try_import(name: str) Optional[types.ModuleType][source]

Try to import a module.

Parameters

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

Returns

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

Return type

ModuleType or None

mmedit.utils.add_gaussian_noise(img: numpy.ndarray, mu, sigma)[source]

Add Gaussian Noise on the input image.

Parameters
  • img (np.ndarray) – Input image.

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

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

Returns

Gaussian noisy output image.

Return type

noisy_img (np.ndarray)

mmedit.utils.adjust_gamma(image, gamma=1, gain=1)[source]

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.

Parameters
  • image (np.ndarray) – Input image.

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

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

Returns

Gamma corrected output image.

Return type

np.ndarray

mmedit.utils.bbox2mask(img_shape, bbox, dtype='uint8')[source]

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.

Parameters
  • 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’

Returns

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

Return type

mask (np.ndarray)

mmedit.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')[source]

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.

Parameters
  • 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’.

Returns

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

Return type

mask (np.ndarray)

mmedit.utils.get_irregular_mask(img_shape, area_ratio_range=(0.15, 0.5), **kwargs)[source]

Get irregular mask with the constraints in mask ratio.

Parameters
  • 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).

Returns

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

Return type

mask (np.ndarray)

mmedit.utils.make_coord(shape, ranges=None, flatten=True)[source]

Make coordinates at grid centers.

Parameters
  • shape (tuple) – shape of image.

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

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

Returns

coordinates.

Return type

coord (Tensor)

mmedit.utils.random_bbox(img_shape, max_bbox_shape, max_bbox_delta=40, min_margin=20)[source]

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.

Parameters
  • 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).

Returns

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

Return type

tuple[int]

mmedit.utils.random_choose_unknown(unknown, crop_size)[source]

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

Parameters
  • unknown (np.ndarray) – The binary unknown mask.

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

Returns

The top-left point of the chosen bbox.

Return type

tuple[int]

mmedit.utils.ConfigType[source]
mmedit.utils.ForwardInputs[source]
mmedit.utils.LabelVar[source]
mmedit.utils.NoiseVar[source]
mmedit.utils.SampleList[source]
Read the Docs v: zyh/re-docs
Versions
master
latest
stable
zyh-re-docs
zyh-doc-notfound-extend
zyh-api-rendering
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.