コード例 #1
0
def fpq_weight_lenet(image,
                     test=False,
                     n=8,
                     delta=2e-4,
                     name="fixed-point-weight-graph-ref"):
    with nn.parameter_scope(name):
        h = PF.fixed_point_quantized_convolution(image,
                                                 16, (5, 5), (1, 1),
                                                 with_bias=False,
                                                 n_w=n,
                                                 delta_w=delta,
                                                 name='conv1')
        h = PF.batch_normalization(h, batch_stat=not test, name='conv1-bn')
        h = F.max_pooling(h, (2, 2))
        h = F.relu(h)

        h = PF.fixed_point_quantized_convolution(h,
                                                 16, (5, 5), (1, 1),
                                                 with_bias=True,
                                                 n_w=n,
                                                 delta_w=delta,
                                                 name='conv2')
        h = PF.batch_normalization(h, batch_stat=not test, name='conv2-bn')
        h = F.max_pooling(h, (2, 2))
        h = F.relu(h)

        h = PF.fixed_point_quantized_affine(h,
                                            10,
                                            with_bias=False,
                                            n_w=n,
                                            delta_w=delta,
                                            name='fc1')
        h = PF.batch_normalization(h, batch_stat=not test, name='fc1-bn')
        h = F.relu(h)

        pred = PF.fixed_point_quantized_affine(h,
                                               10,
                                               with_bias=True,
                                               n_w=n,
                                               delta_w=delta,
                                               name='fc2')
    return pred
コード例 #2
0
    def _fixed_point_weight_conversion(self, inner_prod_func):
        # Input
        w_init = inner_prod_func.inputs[1].d
        b_init = inner_prod_func.inputs[2].d if len(
            inner_prod_func.inputs) == 3 else None
        x = inner_prod_func.inputs[0]
        x = self.input_map[x] if x in self.input_map else x

        # Quantization params
        sign_w = self.args_fpq["sign_w"]
        n_w = self.args_fpq["n_w"]
        delta_w = self.args_fpq["delta_w"]
        sign_b = self.args_fpq["sign_b"]
        n_b = self.args_fpq["n_b"]
        quantize_b = self.args_fpq["quantize_b"]
        delta_b = self.args_fpq["delta_b"]

        # Determine delta
        if not "delta_w" in self.args_fpq:
            n = n_w - 1 if sign_w is True else n_w
            w_abs_max = np.sort(np.abs(w_init.flatten()))[-1]
            delta_w = 2**np.round_func(np.log2(w_abs_max / (2**n - 1)))

        if not "delta_b" in self.args_fpq and len(inner_prod_func.inputs) == 3:
            n = n_b - 1 if sign_b is True else n_b
            b_abs_max = np.sort(np.abs(b_init.flatten()))[-1]
            if b_abs_max != 0:
                delta_b = 2**np.round_func(np.log2(b_abs_max / (2**n - 1)))
            else:
                delta_b = 0

        # Parameter name
        w = inner_prod_func.inputs[1]
        idx = list(self.params.values()).index(w)
        name = list(self.params.keys())[idx].rstrip("W/")

        # Bias
        with_bias = True if len(inner_prod_func.inputs) == 3 else False

        # Conversion
        if inner_prod_func.name == "Affine":
            omaps = w_init.shape[1]
            args = inner_prod_func.info.args
            o = PF.fixed_point_quantized_affine(x,
                                                omaps,
                                                with_bias=with_bias,
                                                w_init=w_init,
                                                b_init=b_init,
                                                sign_w=sign_w,
                                                n_w=n_w,
                                                delta_w=delta_w,
                                                sign_b=sign_b,
                                                n_b=n_b,
                                                delta_b=delta_b,
                                                name=name,
                                                **args)

        if inner_prod_func.name == "Convolution":
            omaps = w_init.shape[0]
            kernel = w_init.shape[2:]
            args = inner_prod_func.info.args
            if args.get('channel_last', False):
                raise ValueError(
                    'channel_last=True is not supported in fixed_point_quantized_convolution.'
                )
            del args['channel_last']

            o = PF.fixed_point_quantized_convolution(x,
                                                     omaps,
                                                     kernel,
                                                     with_bias=with_bias,
                                                     w_init=w_init,
                                                     b_init=b_init,
                                                     sign_w=sign_w,
                                                     n_w=n_w,
                                                     delta_w=delta_w,
                                                     sign_b=sign_b,
                                                     n_b=n_b,
                                                     delta_b=delta_b,
                                                     name=name,
                                                     **args)

        if inner_prod_func.name == "Deconvolution":
            omaps = w_init.shape[0]
            kernel = w_init.shape[2:]
            args = inner_prod_func.info.args
            o = PF.fixed_point_quantized_deconvolution(x,
                                                       omaps,
                                                       kernel,
                                                       with_bias=with_bias,
                                                       w_init=w_init,
                                                       b_init=b_init,
                                                       sign_w=sign_w,
                                                       n_w=n_w,
                                                       delta_w=delta_w,
                                                       sign_b=sign_b,
                                                       n_b=n_b,
                                                       delta_b=delta_b,
                                                       name=name,
                                                       **args)

        # Map output of ref graph to output of new graph
        x = inner_prod_func.outputs[0]
        self.input_map[x] = o

        # Store output (just in case)
        self.outputs.append(o)

        return o
コード例 #3
0
def cifar10_fp_net_resnet23_prediction(image, maps=64,
                                       test=False):
    """
    Construct Fixed-Point Net using resnet23. Fixed-Point Net quantizes weights 
    and activations using FixedPointQuantize function.
    """
    # Residual Unit
    def res_unit(x, scope_name, rng, dn=False, test=False):
        C = x.shape[1]
        with nn.parameter_scope(scope_name):

            # Conv -> BN -> FixedPointQuantize -> Relu
            with nn.parameter_scope("conv1"):
                h = PF.fixed_point_quantized_convolution(x, C / 2, kernel=(1, 1), pad=(0, 0),
                                                         with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
                h = F.fixed_point_quantize(h)
                h = F.relu(h)
            # Conv -> BN -> FixedPointQuantize -> Relu
            with nn.parameter_scope("conv2"):
                h = PF.fixed_point_quantized_convolution(h, C / 2, kernel=(3, 3), pad=(1, 1),
                                                         with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
                h = F.fixed_point_quantize(h)
                h = F.relu(h)
            # Conv -> BN
            with nn.parameter_scope("conv3"):
                h = PF.fixed_point_quantized_convolution(h, C, kernel=(1, 1), pad=(0, 0),
                                                         with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
            # Residual -> FixedPointQuantize -> Relu
            h = F.fixed_point_quantize(h)
            h = F.relu(h + x)

            # Maxpooling
            if dn:
                h = F.max_pooling(h, kernel=(2, 2), stride=(2, 2))

        return h

    ncls = 10

    # Conv -> BN -> FixedPointQuantize
    with nn.parameter_scope("conv1"):
        # Preprocess
        image /= 255.0
        if not test:
            image = F.image_augmentation(image, contrast=1.0,
                                         angle=0.25,
                                         flip_lr=True)
            image.need_grad = False
        h = PF.fixed_point_quantized_convolution(image, maps, kernel=(3, 3), pad=(1, 1),
                                                 with_bias=False)
        h = PF.batch_normalization(h, batch_stat=not test)
        h = F.fixed_point_quantize(h)

    h = res_unit(h, "conv2", False)    # -> 32x32
    h = res_unit(h, "conv3", True)     # -> 16x16
    h = res_unit(h, "conv4", False)    # -> 16x16
    h = res_unit(h, "conv5", True)     # -> 8x8
    h = res_unit(h, "conv6", False)    # -> 8x8
    h = res_unit(h, "conv7", True)     # -> 4x4
    h = res_unit(h, "conv8", False)    # -> 4x4
    h = F.average_pooling(h, kernel=(4, 4))  # -> 1x1
    pred = PF.fixed_point_quantized_affine(h, ncls)

    return pred
コード例 #4
0
ファイル: resnets.py プロジェクト: yuhonghong7035/nnabla
def fpq_weight_small_resnet(image,
                            test=False,
                            sign=True,
                            n=8,
                            delta=2e-4,
                            name="fixed-point-graph-ref"):
    with nn.parameter_scope(name):
        h = image
        h /= 255.0
        h = PF.fixed_point_quantized_convolution(h,
                                                 16,
                                                 kernel=(3, 3),
                                                 pad=(1, 1),
                                                 sign_w=sign,
                                                 n_w=n,
                                                 delta_w=delta,
                                                 sign_b=sign,
                                                 n_b=n,
                                                 delta_b=delta,
                                                 with_bias=False,
                                                 name="first-conv")
        h = PF.batch_normalization(h, name="first-bn", batch_stat=not test)
        h = F.relu(h)
        h = fpq_weight_resblock(h,
                                maps=16,
                                test=test,
                                sign=sign,
                                n=n,
                                delta=delta,
                                name="cb1")
        h = fpq_weight_resblock(h,
                                maps=16,
                                test=test,
                                sign=sign,
                                n=n,
                                delta=delta,
                                name="cb2")
        h = fpq_weight_resblock(h,
                                maps=16,
                                test=test,
                                sign=sign,
                                n=n,
                                delta=delta,
                                name="cb3")
        h = fpq_weight_resblock(h,
                                maps=16,
                                test=test,
                                sign=sign,
                                n=n,
                                delta=delta,
                                name="cb4")
        pred = PF.fixed_point_quantized_affine(h,
                                               10,
                                               sign_w=sign,
                                               n_w=n,
                                               delta_w=delta,
                                               sign_b=sign,
                                               n_b=n,
                                               delta_b=delta,
                                               name='fc')
    return pred