(input_node, quantization_mode)
return [quint8_const_node, min_node, max_node, dequantize_node]
Returns replacement nodes for input_node using the Dequantize op.
Returns replacement nodes for input_node using the Dequantize op.
Returns replacement nodes for input_node using the Dequantize op .
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]
def quantize_weight_eightbit ( input_node , quantization_mode ) : 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 ]