def __init__(self,
                 num_classes=10,
                 num_channels=3,
                 dimensions=(28, 28),
                 fc_inputs=12,
                 bias=False):
        super().__init__()

        # AI84 Limits
        assert dimensions[0] == dimensions[1]  # Only square supported

        # Keep track of image dimensions so one constructor works for all image sizes
        dim = dimensions[0]

        self.conv1 = ai8x.FusedConv2dReLU(num_channels,
                                          16,
                                          3,
                                          padding=1,
                                          bias=bias)
        # padding 1 -> no change in dimensions -> 16x28x28

        pad = 2 if dim == 28 else 1
        self.conv2 = ai8x.FusedMaxPoolConv2dReLU(16,
                                                 16,
                                                 3,
                                                 pool_size=2,
                                                 pool_stride=2,
                                                 padding=pad,
                                                 bias=bias)
        dim //= 2  # pooling, padding 0 -> 16x14x14
        if pad == 2:
            dim += 2  # padding 2 -> 16x16x16

        self.conv3 = ai8x.FusedMaxPoolConv2dReLU(16,
                                                 fc_inputs,
                                                 3,
                                                 pool_size=4,
                                                 pool_stride=4,
                                                 padding=1,
                                                 bias=bias)
        dim //= 4  # pooling, padding 0 -> 16x4x4
        # padding 1 -> 12x4x4

        self.fc = ai8x.SoftwareLinear(fc_inputs * dim * dim,
                                      num_classes,
                                      bias=True)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')
Example #2
0
    def __init__(self,
                 num_channels=3,
                 num_classes=10,
                 dimensions=(32, 32),
                 bias=False,
                 **kwargs):
        super().__init__()
        dim1 = dimensions[0]
        dim2 = dimensions[1]
        # 3x32x32
        self.conv1 = ai8x.FusedMaxPoolConv2dReLU(in_channels=num_channels,
                                                 out_channels=64,
                                                 kernel_size=3,
                                                 padding=1,
                                                 bias=bias,
                                                 **kwargs)
        dim1 //= 2
        dim2 //= 2
        # 64x16x16
        self.fire1 = ai8x_fire.Fire(in_planes=64,
                                    squeeze_planes=16,
                                    expand1x1_planes=64,
                                    expand3x3_planes=64,
                                    bias=bias,
                                    **kwargs)
        # 128x16x16
        self.fire2 = ai8x_fire.Fire(in_planes=128,
                                    squeeze_planes=16,
                                    expand1x1_planes=64,
                                    expand3x3_planes=64,
                                    bias=bias,
                                    **kwargs)
        # 128x16x16
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2,
                                  padding=0)  # check if kernel size=3
        dim1 //= 2
        dim2 //= 2
        # 128x8x8
        self.fire3 = ai8x_fire.Fire(in_planes=128,
                                    squeeze_planes=32,
                                    expand1x1_planes=128,
                                    expand3x3_planes=128,
                                    bias=bias,
                                    **kwargs)
        # 256x8x8
        self.fire4 = ai8x_fire.Fire(in_planes=256,
                                    squeeze_planes=32,
                                    expand1x1_planes=128,
                                    expand3x3_planes=128,
                                    bias=bias,
                                    **kwargs)
        # 256x8x8
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2,
                                  padding=0)  # check if kernel size=3
        dim1 //= 2
        dim2 //= 2
        # 256x4x4
        self.fire5 = ai8x_fire.Fire(in_planes=256,
                                    squeeze_planes=48,
                                    expand1x1_planes=192,
                                    expand3x3_planes=192,
                                    bias=bias,
                                    **kwargs)
        # 384x4x4
        self.fire6 = ai8x_fire.Fire(in_planes=384,
                                    squeeze_planes=48,
                                    expand1x1_planes=192,
                                    expand3x3_planes=192,
                                    bias=bias,
                                    **kwargs)
        # 384x4x4
        self.fire7 = ai8x_fire.Fire(in_planes=384,
                                    squeeze_planes=48,
                                    expand1x1_planes=256,
                                    expand3x3_planes=256,
                                    bias=bias,
                                    **kwargs)
        # 512x4x4
        self.fire8 = ai8x_fire.Fire(in_planes=512,
                                    squeeze_planes=64,
                                    expand1x1_planes=256,
                                    expand3x3_planes=256,
                                    bias=bias,
                                    **kwargs)
        # 512x4x4
        # self.conv2 = ai8x.FusedAvgPoolConv2dReLU(in_channels=512, out_channels=num_classes,
        #                                          kernel_size=1, pool_size=4, pool_stride=4)
        self.fc = ai8x.SoftwareLinear(512 * dim1 * dim2,
                                      num_classes,
                                      bias=bias)
        # num_classesx1x1

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')
Example #3
0
    def __init__(self,
                 num_classes=10,
                 num_channels=3,
                 dimensions=(28, 28),
                 planes=128,
                 pool=2,
                 fc_inputs=12,
                 bias=False,
                 **kwargs):
        super().__init__()

        # Keep track of image dimensions so one constructor works for all image sizes
        dim = dimensions[0]

        self.conv1 = ai8x.FusedConv2dReLU(num_channels,
                                          planes,
                                          3,
                                          padding=1,
                                          bias=bias,
                                          **kwargs)
        # padding 1 -> no change in dimensions -> MNIST: 28x28 | CIFAR: 32x32

        pad = 2 if dim == 28 else 1
        self.conv2 = ai8x.FusedMaxPoolConv2dReLU(planes,
                                                 60,
                                                 3,
                                                 pool_size=2,
                                                 pool_stride=2,
                                                 padding=pad,
                                                 bias=bias,
                                                 **kwargs)
        dim //= 2  # pooling, padding 0 -> MNIST: 14x14 | CIFAR: 16x16
        if pad == 2:
            dim += 2  # MNIST: padding 2 -> 16x16 | CIFAR: padding 1 -> 16x16

        self.conv3 = ai8x.FusedMaxPoolConv2dReLU(60,
                                                 56,
                                                 3,
                                                 pool_size=2,
                                                 pool_stride=2,
                                                 padding=1,
                                                 bias=bias,
                                                 **kwargs)
        dim //= 2  # pooling, padding 0 -> 8x8
        # padding 1 -> no change in dimensions

        self.conv4 = ai8x.FusedAvgPoolConv2dReLU(56,
                                                 fc_inputs,
                                                 3,
                                                 pool_size=pool,
                                                 pool_stride=2,
                                                 padding=1,
                                                 bias=bias,
                                                 **kwargs)
        dim //= pool  # pooling, padding 0 -> 4x4
        # padding 1 -> no change in dimensions

        self.fc = ai8x.SoftwareLinear(fc_inputs * dim * dim,
                                      num_classes,
                                      bias=True)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')