Operators

A collection of operators for numpy and torch.

API

kiui.op.dot(x: Tensor | ndarray, y: Tensor | ndarray) Tensor | ndarray[source]

dot product (along the last dim).

Parameters:
  • x (Union[Tensor, ndarray]) – x, […, C]

  • y (Union[Tensor, ndarray]) – y, […, C]

Returns:

x dot y, […, 1]

Return type:

Union[Tensor, ndarray]

kiui.op.length(x: Tensor | ndarray, eps=1e-20) Tensor | ndarray[source]

length of an array (along the last dim).

Parameters:
  • x (Union[Tensor, ndarray]) – x, […, C]

  • eps (float, optional) – eps. Defaults to 1e-20.

Returns:

length, […, 1]

Return type:

Union[Tensor, ndarray]

kiui.op.safe_normalize(x: Tensor | ndarray, eps=1e-20) Tensor | ndarray[source]

normalize an array (along the last dim).

Parameters:
  • x (Union[Tensor, ndarray]) – x, […, C]

  • eps (float, optional) – eps. Defaults to 1e-20.

Returns:

normalized x, […, C]

Return type:

Union[Tensor, ndarray]

kiui.op.normalize(x: Tensor | ndarray, eps=1e-20) Tensor | ndarray[source]

normalize an array (along the last dim). alias of safe_normalize.

Parameters:
  • x (Union[Tensor, ndarray]) – x, […, C]

  • eps (float, optional) – eps. Defaults to 1e-20.

Returns:

normalized x, […, C]

Return type:

Union[Tensor, ndarray]

kiui.op.make_divisible(x: int, m: int = 8)[source]

make an int x divisible by m.

Parameters:
  • x (int) – x

  • m (int, optional) –

    1. Defaults to 8.

Returns:

x + (m - x % m)

Return type:

int

kiui.op.inverse_sigmoid(x: Tensor, eps=1e-06) Tensor[source]

inversion of sigmoid function.

Parameters:
  • x (Tensor) – x

  • eps (float, optional) – eps. Defaults to 1e-6.

Returns:

log(x / (1 - x))

Return type:

Tensor

kiui.op.inverse_softplus(x: Tensor) Tensor[source]

inversion of softplus function.

Parameters:

x (Tensor) – x

Returns:

log(exp(x) - 1)

Return type:

Tensor

kiui.op.scale_img_nhwc(x: Tensor, size: Sequence[int], mag='bilinear', min='bilinear') Tensor[source]

image scaling helper.

Parameters:
  • x (Tensor) – input image, float [N, H, W, C]

  • size (Sequence[int]) – target size, tuple of [H’, W’]

  • mag (str, optional) – upscale interpolation mode. Defaults to ‘bilinear’.

  • min (str, optional) – downscale interpolation mode. Defaults to ‘bilinear’.

Returns:

rescaled image, float [N, H’, W’, C]

Return type:

Tensor

kiui.op.scale_img_hwc(x: Tensor, size: Sequence[int], mag='bilinear', min='bilinear') Tensor[source]

image scaling helper.

Parameters:
  • x (Tensor) – input image, float [H, W, C]

  • size (Sequence[int]) – target size, tuple of [H’, W’]

  • mag (str, optional) – upscale interpolation mode. Defaults to ‘bilinear’.

  • min (str, optional) – downscale interpolation mode. Defaults to ‘bilinear’.

Returns:

rescaled image, float [H’, W’, C]

Return type:

Tensor

kiui.op.scale_img_nhw(x: Tensor, size: Sequence[int], mag='bilinear', min='bilinear') Tensor[source]

image scaling helper.

Parameters:
  • x (Tensor) – input image, float [N, H, W]

  • size (Sequence[int]) – target size, tuple of [H’, W’]

  • mag (str, optional) – upscale interpolation mode. Defaults to ‘bilinear’.

  • min (str, optional) – downscale interpolation mode. Defaults to ‘bilinear’.

Returns:

rescaled image, float [N, H’, W’]

Return type:

Tensor

kiui.op.scale_img_hw(x: Tensor, size: Sequence[int], mag='bilinear', min='bilinear') Tensor[source]

image scaling helper.

Parameters:
  • x (Tensor) – input image, float [H, W]

  • size (Sequence[int]) – target size, tuple of [H’, W’]

  • mag (str, optional) – upscale interpolation mode. Defaults to ‘bilinear’.

  • min (str, optional) – downscale interpolation mode. Defaults to ‘bilinear’.

Returns:

rescaled image, float [H’, W’]

Return type:

Tensor

kiui.op.uv_padding(image: Tensor | ndarray, mask: Tensor | ndarray, padding: int | None = None, backend: Literal[‘knn’, ‘cv2’] = 'knn')[source]

padding the uv-space texture image to avoid seam artifacts in mipmaps.

Parameters:
  • image (Union[Tensor, ndarray]) – texture image, float, [H, W, C] in [0, 1].

  • mask (Union[Tensor, ndarray]) – valid uv region, bool, [H, W].

  • padding (int, optional) – padding size into the unmasked region. Defaults to 0.1 * max(H, W).

  • backend (Literal['knn', 'cv2'], optional) – algorithm backend, knn is faster. Defaults to ‘knn’.

Returns:

padded texture image. float, [H, W, C].

Return type:

Union[Tensor, ndarray]

kiui.op.recenter(image: ndarray, mask: ndarray, border_ratio: float = 0.2) ndarray[source]

recenter an image to leave some empty space at the image border.

Parameters:
  • image (ndarray) – input image, float/uint8 [H, W, 3/4]

  • mask (ndarray) – alpha mask, bool [H, W]

  • border_ratio (float, optional) – border ratio, image will be resized to (1 - border_ratio). Defaults to 0.2.

Returns:

output image, float/uint8 [H, W, 3/4]

Return type:

ndarray