MCPcopy Create free account
hub / github.com/DragonisCV/RAM / quantize

Function quantize

ram/utils/flow_util.py:126–147  ·  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

124
125
126def quantize(arr, min_val, max_val, levels, dtype=np.int64):
127 """Quantize an array of (-inf, inf) to [0, levels-1].
128
129 Args:
130 arr (ndarray): Input array.
131 min_val (scalar): Minimum value to be clipped.
132 max_val (scalar): Maximum value to be clipped.
133 levels (int): Quantization levels.
134 dtype (np.type): The type of the quantized array.
135
136 Returns:
137 tuple: Quantized array.
138 """
139 if not (isinstance(levels, int) and levels > 1):
140 raise ValueError(f'levels must be a positive integer, but got {levels}')
141 if min_val >= max_val:
142 raise ValueError(f'min_val ({min_val}) must be smaller than max_val ({max_val})')
143
144 arr = np.clip(arr, min_val, max_val) - min_val
145 quantized_arr = np.minimum(np.floor(levels * arr / (max_val - min_val)).astype(dtype), levels - 1)
146
147 return quantized_arr
148
149
150def dequantize(arr, min_val, max_val, levels, dtype=np.float64):

Callers 1

quantize_flowFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected