(self, input_node)
Handles quantizing a single node.
Handles quantizing a single node.
Handles quantizing a single node .
def quantize_node(self, input_node):
    """Handles quantizing a single node."""
    input_name = input_node.name
    if input_name in self.already_quantized:
      return
    self.already_quantized[input_name] = True
    original_input_name = input_name + "_original"
    reshape_name = input_name + "_reshape"
    reshape_dims_name = input_name + "_reshape_dims"
    max_name = input_name + "_max"
    min_name = input_name + "_min"
    dims_name = input_name + "_dims"
    quantize_name = input_name + "_quantize"
    dequantize_name = input_name
    original_input_node = node_def_pb2.NodeDef()
    original_input_node.CopyFrom(input_node)
    original_input_node.name = original_input_name
    self.add_output_graph_node(original_input_node)
    reshape_dims_node = create_constant_node(reshape_dims_name, -1,
                                             dtypes.int32, [1])
    self.add_output_graph_node(reshape_dims_node)
    reshape_node = create_node("Reshape", reshape_name,
                               [original_input_name, reshape_dims_name])
    set_attr_dtype(reshape_node, "T", dtypes.float32)
    self.add_output_graph_node(reshape_node)
    dims_node = create_constant_node(dims_name, 0, dtypes.int32, [1])
    self.add_output_graph_node(dims_node)
    max_node = create_node("Max", max_name, [reshape_name, dims_name])
    set_attr_dtype(max_node, "T", dtypes.float32)
    set_attr_bool(max_node, "keep_dims", False)
    self.add_output_graph_node(max_node)
    min_node = create_node("Min", min_name, [reshape_name, dims_name])
    set_attr_dtype(min_node, "T", dtypes.float32)
    set_attr_bool(min_node, "keep_dims", False)
    self.add_output_graph_node(min_node)
    quantize_node = create_node("Quantize", quantize_name,
                                [original_input_name, min_name, max_name])
    set_attr_dtype(quantize_node, "T", dtypes.quint8)
    set_attr_string(quantize_node, "mode", b"MIN_FIRST")
    self.add_output_graph_node(quantize_node)
    dequantize_node = create_node("Dequantize", dequantize_name,
                                  [quantize_name, min_name, max_name])
    set_attr_dtype(dequantize_node, "T", dtypes.quint8)
    set_attr_string(dequantize_node, "mode", b"MIN_FIRST")
    self.add_output_graph_node(dequantize_node)
def quantize_node ( self , input_node ) : input_name = input_node . name if input_name in self . already_quantized : return self . already_quantized [ input_name ] = True original_input_name = input_name + "_original" reshape_name = input_name + "_reshape" reshape_dims_name = input_name + "_reshape_dims" max_name = input_name + "_max" min_name = input_name + "_min" dims_name = input_name + "_dims" quantize_name = input_name + "_quantize" dequantize_name = input_name original_input_node = node_def_pb2 . NodeDef ( ) original_input_node . CopyFrom ( input_node ) original_input_node . name = original_input_name self . add_output_graph_node ( original_input_node ) reshape_dims_node = create_constant_node ( reshape_dims_name , - 1 , dtypes . int32 , [ 1 ] ) self . add_output_graph_node ( reshape_dims_node ) reshape_node = create_node ( "Reshape" , reshape_name , [ original_input_name , reshape_dims_name ] ) set_attr_dtype ( reshape_node , "T" , dtypes . float32 ) self . add_output_graph_node ( reshape_node ) dims_node = create_constant_node ( dims_name , 0 , dtypes . int32 , [ 1 ] ) self . add_output_graph_node ( dims_node ) max_node = create_node ( "Max" , max_name , [ reshape_name , dims_name ] ) set_attr_dtype ( max_node , "T" , dtypes . float32 ) set_attr_bool ( max_node , "keep_dims" , False ) self . add_output_graph_node ( max_node ) min_node = create_node ( "Min" , min_name , [ reshape_name , dims_name ] ) set_attr_dtype ( min_node , "T" , dtypes . float32 ) set_attr_bool ( min_node , "keep_dims" , False ) self . add_output_graph_node ( min_node ) quantize_node = create_node ( "Quantize" , quantize_name , [ original_input_name , min_name , max_name ] ) set_attr_dtype ( quantize_node , "T" , dtypes . quint8 ) set_attr_string ( quantize_node , "mode" , b"MIN_FIRST" ) self . add_output_graph_node ( quantize_node ) dequantize_node = create_node ( "Dequantize" , dequantize_name , [ quantize_name , min_name , max_name ] ) set_attr_dtype ( dequantize_node , "T" , dtypes . quint8 ) set_attr_string ( dequantize_node , "mode" , b"MIN_FIRST" ) self . add_output_graph_node ( dequantize_node )