Python tensorflow.python.ops.array_ops.quantize_v2() Examples
The following are 5
code examples of tensorflow.python.ops.array_ops.quantize_v2().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.ops.array_ops
, or try the search function
.
Example #1
Source File: quantize_graph.py From tensorflow-for-poets-2 with Apache License 2.0 | 4 votes |
def quantize_weight_eightbit(input_node, quantization_mode): """Returns replacement nodes for input_node using the Dequantize op.""" base_name = input_node.name + "_" quint8_const_name = base_name + "quint8_const" min_name = base_name + "min" max_name = base_name + "max" float_tensor = tensor_util.MakeNdarray(input_node.attr["value"].tensor) min_value = np.min(float_tensor.flatten()) max_value = np.max(float_tensor.flatten()) # Make sure that the range includes zero. if min_value > 0.0: min_value = 0.0 # min_value == max_value is a tricky case. It can occur for general # tensors, and of course for scalars. The quantized ops cannot deal # with this case, so we set max_value to something else. # It's a tricky question what is the numerically best solution to # deal with this degeneracy. # TODO(petewarden): Better use a tolerance than a hard comparison? if min_value == max_value: if abs(min_value) < 0.000001: max_value = min_value + 1.0 elif min_value > 0: max_value = 2 * min_value else: max_value = min_value / 2.0 sess = session.Session() with sess.as_default(): quantize_op = array_ops.quantize_v2( float_tensor, min_value, max_value, dtypes.quint8, mode=quantization_mode) quint8_tensor = quantize_op[0].eval() shape = tensor_util.TensorShapeProtoToList(input_node.attr["value"] .tensor.tensor_shape) quint8_const_node = create_constant_node( quint8_const_name, quint8_tensor, dtypes.quint8, shape=shape) min_node = create_constant_node(min_name, min_value, dtypes.float32) max_node = create_constant_node(max_name, max_value, dtypes.float32) dequantize_node = create_node("Dequantize", input_node.name, [quint8_const_name, min_name, max_name]) set_attr_dtype(dequantize_node, "T", dtypes.quint8) set_attr_string(dequantize_node, "mode", quantization_mode) return [quint8_const_node, min_node, max_node, dequantize_node]
Example #2
Source File: quantize_graph.py From MobileNet with Apache License 2.0 | 4 votes |
def quantize_weight_eightbit(input_node, quantization_mode): """Returns replacement nodes for input_node using the Dequantize op.""" base_name = input_node.name + "_" quint8_const_name = base_name + "quint8_const" min_name = base_name + "min" max_name = base_name + "max" float_tensor = tensor_util.MakeNdarray(input_node.attr["value"].tensor) min_value = np.min(float_tensor.flatten()) max_value = np.max(float_tensor.flatten()) # Make sure that the range includes zero. if min_value > 0.0: min_value = 0.0 # min_value == max_value is a tricky case. It can occur for general # tensors, and of course for scalars. The quantized ops cannot deal # with this case, so we set max_value to something else. # It's a tricky question what is the numerically best solution to # deal with this degeneracy. # TODO(petewarden): Better use a tolerance than a hard comparison? if min_value == max_value: if abs(min_value) < 0.000001: max_value = min_value + 1.0 elif min_value > 0: max_value = 2 * min_value else: max_value = min_value / 2.0 sess = session.Session() with sess.as_default(): quantize_op = array_ops.quantize_v2( float_tensor, min_value, max_value, dtypes.quint8, mode=quantization_mode) quint8_tensor = quantize_op[0].eval() shape = tensor_util.TensorShapeProtoToList(input_node.attr["value"] .tensor.tensor_shape) quint8_const_node = create_constant_node( quint8_const_name, quint8_tensor, dtypes.quint8, shape=shape) min_node = create_constant_node(min_name, min_value, dtypes.float32) max_node = create_constant_node(max_name, max_value, dtypes.float32) dequantize_node = create_node("Dequantize", input_node.name, [quint8_const_name, min_name, max_name]) set_attr_dtype(dequantize_node, "T", dtypes.quint8) set_attr_string(dequantize_node, "mode", quantization_mode) return [quint8_const_node, min_node, max_node, dequantize_node]
Example #3
Source File: quantize_graph.py From sketch-to-react-native with MIT License | 4 votes |
def quantize_weight_eightbit(input_node, quantization_mode): """Returns replacement nodes for input_node using the Dequantize op.""" base_name = input_node.name + "_" quint8_const_name = base_name + "quint8_const" min_name = base_name + "min" max_name = base_name + "max" float_tensor = tensor_util.MakeNdarray(input_node.attr["value"].tensor) min_value = np.min(float_tensor.flatten()) max_value = np.max(float_tensor.flatten()) # Make sure that the range includes zero. if min_value > 0.0: min_value = 0.0 # min_value == max_value is a tricky case. It can occur for general # tensors, and of course for scalars. The quantized ops cannot deal # with this case, so we set max_value to something else. # It's a tricky question what is the numerically best solution to # deal with this degeneracy. # TODO(petewarden): Better use a tolerance than a hard comparison? if min_value == max_value: if abs(min_value) < 0.000001: max_value = min_value + 1.0 elif min_value > 0: max_value = 2 * min_value else: max_value = min_value / 2.0 sess = session.Session() with sess.as_default(): quantize_op = array_ops.quantize_v2( float_tensor, min_value, max_value, dtypes.quint8, mode=quantization_mode) quint8_tensor = quantize_op[0].eval() shape = tensor_util.TensorShapeProtoToList(input_node.attr["value"] .tensor.tensor_shape) quint8_const_node = create_constant_node( quint8_const_name, quint8_tensor, dtypes.quint8, shape=shape) min_node = create_constant_node(min_name, min_value, dtypes.float32) max_node = create_constant_node(max_name, max_value, dtypes.float32) dequantize_node = create_node("Dequantize", input_node.name, [quint8_const_name, min_name, max_name]) set_attr_dtype(dequantize_node, "T", dtypes.quint8) set_attr_string(dequantize_node, "mode", quantization_mode) return [quint8_const_node, min_node, max_node, dequantize_node]
Example #4
Source File: quantize_graph.py From pokemon-mini with Apache License 2.0 | 4 votes |
def quantize_weight_eightbit(input_node, quantization_mode): """Returns replacement nodes for input_node using the Dequantize op.""" base_name = input_node.name + "_" quint8_const_name = base_name + "quint8_const" min_name = base_name + "min" max_name = base_name + "max" float_tensor = tensor_util.MakeNdarray(input_node.attr["value"].tensor) min_value = np.min(float_tensor.flatten()) max_value = np.max(float_tensor.flatten()) # Make sure that the range includes zero. if min_value > 0.0: min_value = 0.0 # min_value == max_value is a tricky case. It can occur for general # tensors, and of course for scalars. The quantized ops cannot deal # with this case, so we set max_value to something else. # It's a tricky question what is the numerically best solution to # deal with this degeneracy. # TODO(petewarden): Better use a tolerance than a hard comparison? if min_value == max_value: if abs(min_value) < 0.000001: max_value = min_value + 1.0 elif min_value > 0: max_value = 2 * min_value else: max_value = min_value / 2.0 sess = session.Session() with sess.as_default(): quantize_op = array_ops.quantize_v2( float_tensor, min_value, max_value, dtypes.quint8, mode=quantization_mode) quint8_tensor = quantize_op[0].eval() shape = tensor_util.TensorShapeProtoToList(input_node.attr["value"] .tensor.tensor_shape) quint8_const_node = create_constant_node( quint8_const_name, quint8_tensor, dtypes.quint8, shape=shape) min_node = create_constant_node(min_name, min_value, dtypes.float32) max_node = create_constant_node(max_name, max_value, dtypes.float32) dequantize_node = create_node("Dequantize", input_node.name, [quint8_const_name, min_name, max_name]) set_attr_dtype(dequantize_node, "T", dtypes.quint8) set_attr_string(dequantize_node, "mode", quantization_mode) return [quint8_const_node, min_node, max_node, dequantize_node]
Example #5
Source File: quantize_graph.py From AudioNet with MIT License | 4 votes |
def quantize_weight_eightbit(input_node, quantization_mode): """Returns replacement nodes for input_node using the Dequantize op.""" base_name = input_node.name + "_" quint8_const_name = base_name + "quint8_const" min_name = base_name + "min" max_name = base_name + "max" float_tensor = tensor_util.MakeNdarray(input_node.attr["value"].tensor) min_value = np.min(float_tensor.flatten()) max_value = np.max(float_tensor.flatten()) # Make sure that the range includes zero. if min_value > 0.0: min_value = 0.0 # min_value == max_value is a tricky case. It can occur for general # tensors, and of course for scalars. The quantized ops cannot deal # with this case, so we set max_value to something else. # It's a tricky question what is the numerically best solution to # deal with this degeneracy. # TODO(petewarden): Better use a tolerance than a hard comparison? if min_value == max_value: if abs(min_value) < 0.000001: max_value = min_value + 1.0 elif min_value > 0: max_value = 2 * min_value else: max_value = min_value / 2.0 sess = session.Session() with sess.as_default(): quantize_op = array_ops.quantize_v2( float_tensor, min_value, max_value, dtypes.quint8, mode=quantization_mode) quint8_tensor = quantize_op[0].eval() shape = tensor_util.TensorShapeProtoToList(input_node.attr["value"] .tensor.tensor_shape) quint8_const_node = create_constant_node( quint8_const_name, quint8_tensor, dtypes.quint8, shape=shape) min_node = create_constant_node(min_name, min_value, dtypes.float32) max_node = create_constant_node(max_name, max_value, dtypes.float32) dequantize_node = create_node("Dequantize", input_node.name, [quint8_const_name, min_name, max_name]) set_attr_dtype(dequantize_node, "T", dtypes.quint8) set_attr_string(dequantize_node, "mode", quantization_mode) return [quint8_const_node, min_node, max_node, dequantize_node]