Ejemplo n.º 1
0
  def __init__(self, model, process, source_quantizers=None,
               is_inference=False, weights_path=None,
               keras_quantizer=None, keras_accumulator=None,
               for_reference=False):

    if model is not None:
      self._model = model

    if weights_path is not None:
      self._model.load_weights(weights_path)

    cfg.update(process, config_settings)

    # if source_quantizers is None, CreateGraph will use
    # default_source_quantizers defined in cfg
    (graph, source_quantizer_list) = qgraph.CreateGraph(
        model, source_quantizers, cfg.default_source_quantizer)

    # qgraph.PrintGraph(graph)
    qgraph.GraphPropagateActivationsToEdges(graph)
    self._layer_map = generate_layer_data_type_map.generate_layer_data_type_map(
        graph, source_quantizer_list, is_inference,
        keras_quantizer, keras_accumulator, for_reference)

    self._output_dict = interface.map_to_json(self._layer_map)
Ejemplo n.º 2
0
def run(model, input_quantizers, is_inference=False, verbose=False):
    (graph,
     source_quantizer_list) = qgraph.CreateGraph(model, input_quantizers)
    # qgraph.PrintGraph(graph)
    qgraph.GraphPropagateActivationsToEdges(graph)

    layer_map = generate_layer_data_type_map.generate_layer_data_type_map(
        graph, source_quantizer_list, is_inference)

    # interface.print_layer_data_type_map(dict)
    output_dict = interface.map_to_json(layer_map)

    if verbose:
        dict_to_json = json.dumps(output_dict, indent=4)
        print(dict_to_json)

    return output_dict
Ejemplo n.º 3
0
def test_qpooling_in_qtools():
    input_size = (16, 16, 3)
    pool_size = (2, 2)
    input_quantizers = [quantized_bits(8, 0, 1)]
    is_inference = False

    x = Input(input_size)
    xin = x
    x = QAveragePooling2D(pool_size=pool_size,
                          average_quantizer=binary(),
                          activation=quantized_bits(4, 0, 1),
                          name="pooling")(x)
    x = QGlobalAveragePooling2D(average_quantizer=quantized_bits(4, 0, 1),
                                activation=ternary(),
                                name="global_pooling")(x)
    model = Model(inputs=xin, outputs=x)

    (graph,
     source_quantizer_list) = qgraph.CreateGraph(model, input_quantizers)

    qgraph.GraphPropagateActivationsToEdges(graph)

    layer_map = generate_layer_data_type_map.generate_layer_data_type_map(
        graph, source_quantizer_list, is_inference)

    dtype_dict = interface.map_to_json(layer_map)

    # Checks the QAveragePpooling layer datatype
    multiplier = dtype_dict["pooling"]["pool_avg_multiplier"]
    accumulator = dtype_dict["pooling"]["pool_sum_accumulator"]
    average_quantizer = dtype_dict["pooling"]["average_quantizer"]
    output = dtype_dict["pooling"]["output_quantizer"]

    assert_equal(multiplier["quantizer_type"], "quantized_bits")
    assert_equal(multiplier["bits"], 10)
    assert_equal(multiplier["int_bits"], 3)
    assert_equal(multiplier["is_signed"], 1)
    assert_equal(multiplier["op_type"], "mux")

    assert_equal(accumulator["quantizer_type"], "quantized_bits")
    assert_equal(accumulator["bits"], 10)
    assert_equal(accumulator["int_bits"], 3)
    assert_equal(accumulator["is_signed"], 1)
    assert_equal(accumulator["op_type"], "add")

    assert_equal(output["quantizer_type"], "quantized_bits")
    assert_equal(output["bits"], 4)
    assert_equal(output["int_bits"], 1)
    assert_equal(output["is_signed"], 1)

    assert_equal(average_quantizer["quantizer_type"], "binary")
    assert_equal(average_quantizer["bits"], 1)
    assert_equal(average_quantizer["int_bits"], 1)
    assert_equal(average_quantizer["is_signed"], 1)

    # Checks the QGlobalAveragePooling layer datatype
    multiplier = dtype_dict["global_pooling"]["pool_avg_multiplier"]
    accumulator = dtype_dict["global_pooling"]["pool_sum_accumulator"]
    average_quantizer = dtype_dict["global_pooling"]["average_quantizer"]
    output = dtype_dict["global_pooling"]["output_quantizer"]

    assert_equal(multiplier["quantizer_type"], "quantized_bits")
    assert_equal(multiplier["bits"], 13)
    assert_equal(multiplier["int_bits"], 7)
    assert_equal(multiplier["is_signed"], 1)
    assert_equal(multiplier["op_type"], "mul")

    assert_equal(accumulator["quantizer_type"], "quantized_bits")
    assert_equal(accumulator["bits"], 10)
    assert_equal(accumulator["int_bits"], 7)
    assert_equal(accumulator["is_signed"], 1)
    assert_equal(accumulator["op_type"], "add")

    assert_equal(output["quantizer_type"], "ternary")
    assert_equal(output["bits"], 2)
    assert_equal(output["int_bits"], 2)
    assert_equal(output["is_signed"], 1)

    assert_equal(average_quantizer["quantizer_type"], "quantized_bits")
    assert_equal(average_quantizer["bits"], 4)
    assert_equal(average_quantizer["int_bits"], 1)
    assert_equal(average_quantizer["is_signed"], 1)