MCPcopy Create free account
hub / github.com/ShihaoZhaoZSH/Uni-ControlNet / quantize

Function quantize

annotator/uniformer/mmcv/arraymisc/quantization.py:5–29  ·  view source on GitHub ↗

Quantize an array of (-inf, inf) to [0, levels-1]. Args: arr (ndarray): Input array. min_val (scalar): Minimum value to be clipped. max_val (scalar): Maximum value to be clipped. levels (int): Quantization levels. dtype (np.type): The type of the quantize

(arr, min_val, max_val, levels, dtype=np.int64)

Source from the content-addressed store, hash-verified

3
4
5def quantize(arr, min_val, max_val, levels, dtype=np.int64):
6 """Quantize an array of (-inf, inf) to [0, levels-1].
7
8 Args:
9 arr (ndarray): Input array.
10 min_val (scalar): Minimum value to be clipped.
11 max_val (scalar): Maximum value to be clipped.
12 levels (int): Quantization levels.
13 dtype (np.type): The type of the quantized array.
14
15 Returns:
16 tuple: Quantized array.
17 """
18 if not (isinstance(levels, int) and levels > 1):
19 raise ValueError(
20 f'levels must be a positive integer, but got {levels}')
21 if min_val >= max_val:
22 raise ValueError(
23 f'min_val ({min_val}) must be smaller than max_val ({max_val})')
24
25 arr = np.clip(arr, min_val, max_val) - min_val
26 quantized_arr = np.minimum(
27 np.floor(levels * arr / (max_val - min_val)).astype(dtype), levels - 1)
28
29 return quantized_arr
30
31
32def dequantize(arr, min_val, max_val, levels, dtype=np.float64):

Callers 1

quantize_flowFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected