示例#1
0
def qbn_model_inference():

    x = x_in = keras.layers.Input((23, 23, 1), name="input")
    x = QConv2D(4,
                2,
                23,
                kernel_quantizer=quantizers.quantized_ulaw(4, 1, 1),
                bias_quantizer=quantizers.stochastic_ternary(),
                use_bias=False,
                name="qconv2d_1")(x)
    x = QBatchNormalization(gamma_quantizer=quantizers.quantized_relu_po2(
        3, 2),
                            variance_quantizer=quantizers.quantized_po2(
                                3, 2, quadratic_approximation=False),
                            beta_quantizer=quantizers.quantized_bits(6, 0, 1),
                            scale=False,
                            center=False,
                            gamma_range=8,
                            beta_range=4,
                            name="qbn_2")(x)

    x = QConv2D(2,
                1,
                1,
                kernel_quantizer=quantizers.quantized_po2(3, 0),
                bias_quantizer=quantizers.quantized_po2(3, 2),
                name="qconv2d_3")(x)

    model = keras.Model(inputs=[x_in], outputs=[x])

    layer = model.get_layer("qbn_2")

    weight_arr = [
        np.array([3, 4, 1, 7]),
        np.array([6, 4, 1, -7]),
        np.array([2, 7, -8, 2]),
        np.array([-1, -7, 4, 9])
    ]

    # quantize the weights
    quantizer_list = layer.get_quantizers()
    for (i, quantizer) in enumerate(quantizer_list):
        if quantizer is not None:
            weight_arr[i] = keras.backend.eval(
                quantizer(keras.backend.constant(weight_arr[i])))

    num_weights = 4
    if not layer.scale:
        num_weights -= 1
    if not layer.center:
        num_weights -= 1

    layer.set_weights(weight_arr[:num_weights])

    return model
示例#2
0
def test_StochasticTernary():
  qkeras_quantizer = quantizers.stochastic_ternary()
  qtools_quantizer = quantizer_impl.StochasticTernary()
  qtools_quantizer.convert_qkeras_quantizer(qkeras_quantizer)
  new_quantizer = qtools_quantizer.convert_to_qkeras_quantizer(
      alpha=qkeras_quantizer.alpha, threshold=qkeras_quantizer.threshold,
      temperature=qkeras_quantizer.temperature,
      use_real_sigmoid=qkeras_quantizer.use_real_sigmoid,
      number_of_unrolls=qkeras_quantizer.number_of_unrolls)

  result = new_quantizer.__dict__
  for (key, val) in result.items():
    assert_equal(val, qkeras_quantizer.__dict__[key])
示例#3
0
    def convert_to_qkeras_quantizer(self,
                                    alpha=None,
                                    threshold=None,
                                    temperature=8.0,
                                    use_real_sigmoid=True,
                                    number_of_unrolls=5):
        """convert qtools quantizer to qkeras quantizer."""

        return quantizers.stochastic_ternary(
            alpha=alpha,
            threshold=threshold,
            temperature=temperature,
            use_real_sigmoid=use_real_sigmoid,
            number_of_unrolls=number_of_unrolls)