Exemplo n.º 1
0
    def res_unit(x, scope_name, rng, dn=False, test=False):
        C = x.shape[1]
        with nn.parameter_scope(scope_name):
            # Conv -> BN -> Pow2Quantize -> Relu
            with nn.parameter_scope("conv1"):
                h = PF.pow2_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.pow2_quantize(h)
                h = F.relu(h)
            # Conv -> BN -> Pow2Quantize -> Relu
            with nn.parameter_scope("conv2"):
                h = PF.pow2_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.pow2_quantize(h)
                h = F.relu(h)
            # Conv -> BN
            with nn.parameter_scope("conv3"):
                h = PF.pow2_quantized_convolution(h, C, kernel=(1, 1), pad=(0, 0),
                                                  with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
            # Residual -> Pow2Quantize -> Relu
            h = F.pow2_quantize(h)
            h = F.relu(h + x)

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

        return h
Exemplo n.º 2
0
def cifar10_pow2_net_resnet23_prediction(image, maps=64,
                                         test=False):
    """
    Construct Pow2 Net using resnet23. Pow2 Net quantizes weights and 
    activations using Pow2Quantize 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 -> Pow2Quantize -> Relu
            with nn.parameter_scope("conv1"):
                h = PF.pow2_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.pow2_quantize(h)
                h = F.relu(h)
            # Conv -> BN -> Pow2Quantize -> Relu
            with nn.parameter_scope("conv2"):
                h = PF.pow2_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.pow2_quantize(h)
                h = F.relu(h)
            # Conv -> BN
            with nn.parameter_scope("conv3"):
                h = PF.pow2_quantized_convolution(h, C, kernel=(1, 1), pad=(0, 0),
                                                  with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
            # Residual -> Pow2Quantize -> Relu
            h = F.pow2_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 -> Pow2Quantize
    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.pow2_quantized_convolution(image, maps, kernel=(3, 3), pad=(1, 1),
                                          with_bias=False)
        h = PF.batch_normalization(h, batch_stat=not test)
        h = F.pow2_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.pow2_quantized_affine(h, ncls)

    return pred