Esempio n. 1
0
def mnist_inq_resnet_prediction(image, num_bits=3,
                                inq_iterations=(5000, 6000, 7000, 8000, 9000),
                                selection_algorithm='largest_abs', test=False):
    """
    Construct ResNet for MNIST (INQ Version).
    """
    image /= 255.0

    def bn(x):
        return PF.batch_normalization(x, batch_stat=not test)

    def res_unit(x, scope):
        C = x.shape[1]
        with nn.parameter_scope(scope):
            with nn.parameter_scope('conv1'):
                h = F.elu(bn(PF.inq_convolution(x, C / 2, (1, 1),
                                                num_bits=num_bits,
                                                inq_iterations=inq_iterations,
                                                selection_algorithm=selection_algorithm,
                                                with_bias=False)))
            with nn.parameter_scope('conv2'):
                h = F.elu(
                    bn(PF.inq_convolution(h, C / 2, (3, 3), pad=(1, 1),
                                          num_bits=num_bits,
                                          inq_iterations=inq_iterations,
                                          selection_algorithm=selection_algorithm,
                                          with_bias=False)))
            with nn.parameter_scope('conv3'):
                h = bn(PF.inq_convolution(h, C, (1, 1), num_bits=num_bits,
                                          inq_iterations=inq_iterations,
                                          selection_algorithm=selection_algorithm,
                                          with_bias=False))
        return F.elu(F.add2(h, x, inplace=True))
    # Conv1 --> 64 x 32 x 32
    with nn.parameter_scope("conv1"):
        c1 = F.elu(
            bn(PF.inq_convolution(image, 64, (3, 3), pad=(3, 3),
                                  num_bits=num_bits,
                                  inq_iterations=inq_iterations,
                                  selection_algorithm=selection_algorithm,
                                  with_bias=False)))
    # Conv2 --> 64 x 16 x 16
    c2 = F.max_pooling(res_unit(c1, "conv2"), (2, 2))
    # Conv3 --> 64 x 8 x 8
    c3 = F.max_pooling(res_unit(c2, "conv3"), (2, 2))
    # Conv4 --> 64 x 8 x 8
    c4 = res_unit(c3, "conv4")
    # Conv5 --> 64 x 4 x 4
    c5 = F.max_pooling(res_unit(c4, "conv5"), (2, 2))
    # Conv5 --> 64 x 4 x 4
    c6 = res_unit(c5, "conv6")
    pl = F.average_pooling(c6, (4, 4))
    with nn.parameter_scope("classifier"):
        y = PF.inq_affine(pl, 10, num_bits=num_bits,
                          inq_iterations=inq_iterations,
                          selection_algorithm=selection_algorithm)
    return y
Esempio n. 2
0
    def res_unit(x, scope_name, dn=False):
        C = x.shape[1]
        with nn.parameter_scope(scope_name):
            # Conv -> BN -> Relu
            with nn.parameter_scope("conv1"):
                h = PF.inq_convolution(x,
                                       C // 2,
                                       kernel=(1, 1),
                                       pad=(0, 0),
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
                h = F.relu(h)
            # Conv -> BN -> Relu
            with nn.parameter_scope("conv2"):
                h = PF.inq_convolution(h,
                                       C // 2,
                                       kernel=(3, 3),
                                       pad=(1, 1),
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
                h = F.relu(h)
            # Conv -> BN
            with nn.parameter_scope("conv3"):
                h = PF.inq_convolution(h,
                                       C,
                                       kernel=(1, 1),
                                       pad=(0, 0),
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
            # Residual -> Relu
            h = F.relu(h + x)

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

        return h
Esempio n. 3
0
 def res_unit(x, scope):
     C = x.shape[1]
     with nn.parameter_scope(scope):
         with nn.parameter_scope('conv1'):
             h = F.elu(bn(PF.inq_convolution(x, C / 2, (1, 1),
                                             num_bits=num_bits,
                                             inq_iterations=inq_iterations,
                                             selection_algorithm=selection_algorithm,
                                             with_bias=False)))
         with nn.parameter_scope('conv2'):
             h = F.elu(
                 bn(PF.inq_convolution(h, C / 2, (3, 3), pad=(1, 1),
                                       num_bits=num_bits,
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False)))
         with nn.parameter_scope('conv3'):
             h = bn(PF.inq_convolution(h, C, (1, 1), num_bits=num_bits,
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False))
     return F.elu(F.add2(h, x, inplace=True))
Esempio n. 4
0
def mnist_inq_lenet_prediction(image, num_bits=3,
                               inq_iterations=(5000, 6000, 7000, 8000, 9000),
                               selection_algorithm='largest_abs', test=False):
    """
    Construct LeNet for MNIST (INQ Version).
    """
    image /= 255.0
    c1 = PF.inq_convolution(image, 16, (5, 5), name='conv1', num_bits=num_bits,
                            inq_iterations=inq_iterations,
                            selection_algorithm=selection_algorithm)
    c1 = F.relu(F.max_pooling(c1, (2, 2)), inplace=True)
    c2 = PF.inq_convolution(c1, 16, (5, 5), name='conv2', num_bits=num_bits,
                            inq_iterations=inq_iterations,
                            selection_algorithm=selection_algorithm)
    c2 = F.relu(F.max_pooling(c2, (2, 2)), inplace=True)
    c3 = F.relu(PF.inq_affine(c2, 50, name='fc3', num_bits=num_bits,
                              inq_iterations=inq_iterations,
                              selection_algorithm=selection_algorithm),
                inplace=True)
    c4 = PF.inq_affine(c3, 10, name='fc4', num_bits=num_bits,
                       inq_iterations=inq_iterations,
                       selection_algorithm=selection_algorithm)
    return c4
Esempio n. 5
0
def cifar10_inq_resnet23_prediction(image,
                                    maps=64,
                                    num_bits=4,
                                    inq_iterations=(5000, 6000, 7000, 8000,
                                                    9000),
                                    selection_algorithm='largest_abs',
                                    test=False):
    """
    Construct INQ Network using resnet23.
    """

    # Residual Unit
    def res_unit(x, scope_name, dn=False):
        C = x.shape[1]
        with nn.parameter_scope(scope_name):
            # Conv -> BN -> Relu
            with nn.parameter_scope("conv1"):
                h = PF.inq_convolution(x,
                                       C // 2,
                                       kernel=(1, 1),
                                       pad=(0, 0),
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
                h = F.relu(h)
            # Conv -> BN -> Relu
            with nn.parameter_scope("conv2"):
                h = PF.inq_convolution(h,
                                       C // 2,
                                       kernel=(3, 3),
                                       pad=(1, 1),
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
                h = F.relu(h)
            # Conv -> BN
            with nn.parameter_scope("conv3"):
                h = PF.inq_convolution(h,
                                       C,
                                       kernel=(1, 1),
                                       pad=(0, 0),
                                       inq_iterations=inq_iterations,
                                       selection_algorithm=selection_algorithm,
                                       with_bias=False)
                h = PF.batch_normalization(h, batch_stat=not test)
            # Residual -> Relu
            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
    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.inq_convolution(image,
                               maps,
                               kernel=(3, 3),
                               pad=(1, 1),
                               inq_iterations=inq_iterations,
                               selection_algorithm=selection_algorithm,
                               with_bias=False)
        h = PF.batch_normalization(h, batch_stat=not test)
        h = F.relu(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.inq_affine(h,
                         ncls,
                         inq_iterations=inq_iterations,
                         selection_algorithm=selection_algorithm)
    return pred