Exemplo n.º 1
0
def get_graph(train=False):
    g = Graph('YOLOv2-Test: 16-bit',
              dataset='imagenet',
              log_level=logging.INFO)
    batch_size = 1

    with g.as_default():

        with g.name_scope('inputs'):
            # Input dimensions are (Batch_size, Height, Width, Channels)
            i = get_tensor(shape=(batch_size, 28, 28, 1),
                           name='data',
                           dtype=FQDtype.FXP16,
                           trainable=False)

        with g.name_scope('conv0'):
            # Weight dimensions are (Output Channels, Kernel Height, Kernel Width, Input Channels)
            weights = get_tensor(shape=(20, 5, 5, 1),
                                 name='weights',
                                 dtype=FixedPoint(16, 12))
            # Bias dimensions are (Output Channels,)
            biases = get_tensor(shape=(20),
                                name='biases',
                                dtype=FixedPoint(32, 20))
            # Intermediate data dimensions are (Batch_size, Height, Width, Channels)
            conv = conv2D(i,
                          weights,
                          biases,
                          pad='VALID',
                          dtype=FixedPoint(16, 12))

    return g
Exemplo n.º 2
0
def get_LSTM(name, size):
    g = Graph(name, dataset='PTB', log_level=logging.INFO)
    batch_size = 16

    with g.as_default():
        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, size * 4),
                           name='data',
                           dtype=FQDtype.FXP4,
                           trainable=False)
        with g.name_scope('matmul'):
            out = fc(i,
                     output_channels=4 * size,
                     w_dtype=FQDtype.FXP4,
                     f_dtype=None)
    return g
Exemplo n.º 3
0
def get_bench_nn(bench_name, WRPN=False):

    g = Graph(bench_name, dataset='nop', log_level=logging.INFO)
    batch_size = 16
    hyparamater = parase_bench_name(bench_name)
    width = hyparamater['width']
    width = int(width)
    height = hyparamater['height']
    height = int(height)
    cin = hyparamater['cin']
    cin = int(cin)
    cout = hyparamater['cout']
    cout = int(cout)
    kernel_size = hyparamater['kernel']
    kernel_size = int(kernel_size)
    stride = hyparamater['stride']
    stride = (1, int(stride), int(stride), 1)
    pad = hyparamater['pad'].upper()
    group = hyparamater['group']
    group = int(group)
    fb = hyparamater['fb']
    wb = hyparamater['wb']
    fb = {'8': FQDtype.FXP8, '4': FQDtype.FXP4, '2': FQDtype.FXP2}[fb]
    wb = {'8': FQDtype.FXP8, '4': FQDtype.FXP4, '2': FQDtype.FXP2}[wb]

    with g.as_default():
        with g.name_scope('inputs'):
            prev = get_tensor(shape=(batch_size, width, height, cin),
                              name='data',
                              dtype=FQDtype.FXP8,
                              trainable=False)

        with g.name_scope('conv1'):  # prepare
            prev = conv(prev,
                        filters=cin,
                        kernel_size=1,
                        stride=(1, 1, 1, 1),
                        pad='SAME',
                        c_dtype=FQDtype.FXP8,
                        w_dtype=FQDtype.FXP8)

        if 'base' in bench_name:
            return g

        with g.name_scope('conv2'):  # profile
            prev = conv(prev,
                        filters=cout,
                        kernel_size=kernel_size,
                        stride=stride,
                        pad=pad,
                        group=group,
                        c_dtype=fb,
                        w_dtype=wb)

    return g
Exemplo n.º 4
0
def get_graph(train=False):
    g = Graph('YOLOv2-Test: 16-bit',
              dataset='imagenet',
              log_level=logging.INFO)
    batch_size = 1

    with g.as_default():

        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, 416, 416, 3),
                           name='data',
                           dtype=FQDtype.FXP16,
                           trainable=False)

        with g.name_scope('conv0'):
            conv0 = yolo_convolution(i,
                                     filters=16,
                                     kernel_size=3,
                                     batch_normalize=True,
                                     act='leakyReLU',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 12),
                                     s_dtype=FixedPoint(16, 9),
                                     bn_dtype=FixedPoint(16, 8))
        with g.name_scope('pool0'):
            pool0 = maxPool(conv0,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv1'):
            conv1 = yolo_convolution(pool0,
                                     filters=32,
                                     kernel_size=3,
                                     batch_normalize=True,
                                     act='leakyReLU',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 8),
                                     s_dtype=FixedPoint(16, 14),
                                     bn_dtype=FixedPoint(16, 8))
        with g.name_scope('pool1'):
            pool1 = maxPool(conv1,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv2'):
            conv2 = yolo_convolution(
                pool1,
                filters=64,
                kernel_size=3,
                batch_normalize=True,
                act='leakyReLU',
                # batch_normalize=False, act='linear',
                w_dtype=FixedPoint(16, 14),
                c_dtype=FixedPoint(16, 10),
                s_dtype=FixedPoint(16, 13),
                bn_dtype=FixedPoint(16, 9))
        with g.name_scope('pool2'):
            pool2 = maxPool(conv2,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv3'):
            conv3 = yolo_convolution(pool2,
                                     filters=128,
                                     kernel_size=3,
                                     batch_normalize=True,
                                     act='leakyReLU',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 10),
                                     s_dtype=FixedPoint(16, 13),
                                     bn_dtype=FixedPoint(16, 10))
        with g.name_scope('pool3'):
            pool3 = maxPool(conv3,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv4'):
            conv4 = yolo_convolution(pool3,
                                     filters=256,
                                     kernel_size=3,
                                     batch_normalize=True,
                                     act='leakyReLU',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 11),
                                     s_dtype=FixedPoint(16, 13),
                                     bn_dtype=FixedPoint(16, 10))
        with g.name_scope('pool4'):
            pool4 = maxPool(conv4,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv5'):
            conv5 = yolo_convolution(pool4,
                                     filters=512,
                                     kernel_size=3,
                                     batch_normalize=True,
                                     act='leakyReLU',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 12),
                                     s_dtype=FixedPoint(16, 13),
                                     bn_dtype=FixedPoint(16, 11))
        with g.name_scope('pool5'):
            pool5 = maxPool(conv5,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 1, 1, 1),
                            pad=((0, 0), (0, 1), (0, 1), (0, 0)))

        with g.name_scope('conv6'):
            conv6 = yolo_convolution(pool5,
                                     filters=1024,
                                     kernel_size=3,
                                     batch_normalize=True,
                                     act='leakyReLU',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 12),
                                     s_dtype=FixedPoint(16, 11),
                                     bn_dtype=FixedPoint(16, 9))

        with g.name_scope('conv7'):
            conv7 = yolo_convolution(conv6,
                                     filters=1024,
                                     kernel_size=3,
                                     batch_normalize=True,
                                     act='leakyReLU',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 11),
                                     s_dtype=FixedPoint(16, 14),
                                     bn_dtype=FixedPoint(16, 12))

        with g.name_scope('conv8'):
            conv8 = yolo_convolution(conv7,
                                     filters=125,
                                     kernel_size=1,
                                     batch_normalize=False,
                                     act='linear',
                                     w_dtype=FixedPoint(16, 14),
                                     c_dtype=FixedPoint(16, 11))

    return g
Exemplo n.º 5
0
 def initialize_pass(self, node, ctx):
     self.dnnw_ir['graph'] = Graph(node.name, None, log_level=logging.INFO)
     return node
Exemplo n.º 6
0
def get_resnet_18_twn():
    '''
    ResNet-18

    Note that this isn't ResNet-18B
    '''

    g = Graph('ResNet-18-TWN', dataset='ImageNet', log_level=logging.INFO)
    batch_size = 16

    with g.as_default():
        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, 224, 224, 3),
                           name='data',
                           dtype=FQDtype.FXP8,
                           trainable=False)

        with g.name_scope('conv1_a'):
            conv1_a = conv(i,
                           filters=64,
                           kernel_size=7,
                           pad='SAME',
                           stride=(1, 2, 2, 1),
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP8)
        with g.name_scope('pool1_a'):
            pool1_a = maxPool(conv1_a,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('module2'):
            for i in range(1, 3):
                if i == 1:
                    prev = pool1_a
                else:
                    prev = conv2_b
                with g.name_scope('conv2_{}_a'.format(i)):
                    conv2_a = conv(prev,
                                   filters=64,
                                   kernel_size=1,
                                   pad='SAME',
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)
                with g.name_scope('conv2_{}_b'.format(i)):
                    conv2_b = conv(conv2_a,
                                   filters=64,
                                   kernel_size=3,
                                   pad='SAME',
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)

        with g.name_scope('add2'):
            add2 = add((conv2_a, conv2_b))

        with g.name_scope('module3'):
            for i in range(1, 3):
                if i == 1:
                    prev = add2
                    stride = (1, 2, 2, 1)
                else:
                    prev = conv3_b
                    stride = (1, 1, 1, 1)
                with g.name_scope('conv3_{}_a'.format(i)):
                    conv3_a = conv(prev,
                                   filters=128,
                                   kernel_size=1,
                                   pad='SAME',
                                   stride=stride,
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)
                with g.name_scope('conv3_{}_b'.format(i)):
                    conv3_b = conv(conv3_a,
                                   filters=128,
                                   kernel_size=3,
                                   pad='SAME',
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)

        with g.name_scope('add3'):
            add3 = add((conv3_a, conv3_b))

        with g.name_scope('module4'):
            for i in range(1, 3):
                if i == 1:
                    prev = add3
                    stride = (1, 2, 2, 1)
                else:
                    prev = conv4_b
                    stride = (1, 1, 1, 1)
                with g.name_scope('conv4_{}_a'.format(i)):
                    conv4_a = conv(prev,
                                   filters=256,
                                   kernel_size=1,
                                   pad='SAME',
                                   stride=stride,
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)
                with g.name_scope('conv4_{}_b'.format(i)):
                    conv4_b = conv(conv4_a,
                                   filters=256,
                                   kernel_size=3,
                                   pad='SAME',
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)

        with g.name_scope('add4'):
            add4 = add((conv4_a, conv4_b))

        with g.name_scope('module5'):
            for i in range(1, 3):
                if i == 1:
                    prev = add4
                    stride = (1, 2, 2, 1)
                else:
                    prev = conv5_b
                    stride = (1, 1, 1, 1)
                with g.name_scope('conv5_{}_a'.format(i)):
                    conv5_a = conv(prev,
                                   filters=512,
                                   kernel_size=1,
                                   pad='SAME',
                                   stride=stride,
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)
                with g.name_scope('conv5_{}_b'.format(i)):
                    conv5_b = conv(conv5_a,
                                   filters=512,
                                   kernel_size=3,
                                   pad='SAME',
                                   c_dtype=FQDtype.FXP4,
                                   w_dtype=FQDtype.FXP4)
    return g
Exemplo n.º 7
0
def get_vgg_7_twn():
    '''
    VGG-7
    '''

    g = Graph('VGG-7-CIFAR-10', dataset='CIFAR-10', log_level=logging.INFO)
    batch_size = 16

    with g.as_default():
        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, 32, 32, 3),
                           name='data',
                           dtype=FQDtype.FXP2,
                           trainable=False)

        with g.name_scope('conv1_a'):
            conv1_a = conv(i,
                           filters=128,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP2,
                           w_dtype=FQDtype.FXP2)
        with g.name_scope('pool1_a'):
            pool1_a = maxPool(conv1_a,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv1_b'):
            conv1_b = conv(i,
                           filters=128,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP2,
                           w_dtype=FQDtype.FXP2)
        with g.name_scope('pool1_b'):
            pool1_b = maxPool(conv1_b,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv2_a'):
            conv2_a = conv(pool1_a,
                           filters=256,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP2,
                           w_dtype=FQDtype.FXP2)
        with g.name_scope('pool2_a'):
            pool2_a = maxPool(conv2_a,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv2_b'):
            conv2_b = conv(pool1_b,
                           filters=256,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP2,
                           w_dtype=FQDtype.FXP2)
        with g.name_scope('pool2_b'):
            pool2_b = maxPool(conv2_b,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv3_a'):
            conv3_a = conv(pool2_a,
                           filters=512,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP2,
                           w_dtype=FQDtype.FXP2)
        with g.name_scope('pool3_a'):
            pool3_a = maxPool(conv3_a,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv3_b'):
            conv3_b = conv(pool2_b,
                           filters=512,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP2,
                           w_dtype=FQDtype.FXP2)
        with g.name_scope('pool3_b'):
            pool3_b = maxPool(conv3_b,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('add3'):
            add3 = add((pool3_a, pool3_b))

        with g.name_scope('flatten3'):
            flatten3 = flatten(add3)

        with g.name_scope('fc1'):
            fc1 = fc(flatten3,
                     output_channels=1024,
                     w_dtype=FQDtype.FXP2,
                     f_dtype=FQDtype.FXP2)

    return g
Exemplo n.º 8
0
def get_lenet_5_twn():
    '''
    LeNet-5
    TWN
    '''

    g = Graph('LeNet-5', dataset='MNIST', log_level=logging.INFO)
    batch_size = 16

    with g.as_default():
        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, 32, 32, 1),
                           name='data',
                           dtype=FQDtype.FXP2,
                           trainable=False)

        with g.name_scope('conv0'):
            conv0 = conv(i,
                         filters=32,
                         kernel_size=5,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP2)
        with g.name_scope('pool0'):
            pool0 = maxPool(conv0,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv1'):
            conv1 = conv(pool0,
                         filters=64,
                         kernel_size=5,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP2)
        with g.name_scope('pool1'):
            pool1 = maxPool(conv1,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('flatten1'):
            flatten1 = flatten(pool1)

        with g.name_scope('fc1'):
            fc1 = fc(flatten1,
                     output_channels=512,
                     w_dtype=FQDtype.FXP2,
                     f_dtype=FQDtype.FXP2)

        with g.name_scope('fc2'):
            fc2 = fc(fc1,
                     output_channels=10,
                     w_dtype=FQDtype.FXP2,
                     f_dtype=FQDtype.FXP2)

    return g
Exemplo n.º 9
0
def get_cifar10_qnn():
    '''
    CIFAR-10
    QNN
    '''

    g = Graph('CIFAR-10-QNN', dataset='Cifar-10', log_level=logging.INFO)
    batch_size = 16

    with g.as_default():
        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, 32, 32, 3),
                           name='data',
                           dtype=FQDtype.FXP8,
                           trainable=False)

        with g.name_scope('conv0'):
            conv0 = conv(i,
                         filters=128,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP8)

        with g.name_scope('conv1'):
            conv1 = conv(conv0,
                         filters=128,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP2)
        with g.name_scope('pool1'):
            pool1 = maxPool(conv1,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv2'):
            conv2 = conv(pool1,
                         filters=256,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP2)

        with g.name_scope('conv3'):
            conv3 = conv(conv2,
                         filters=256,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP2)
        with g.name_scope('pool3'):
            pool3 = maxPool(conv3,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv4'):
            conv4 = conv(pool3,
                         filters=512,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP2)

        with g.name_scope('conv5'):
            conv5 = conv(conv4,
                         filters=512,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.FXP2,
                         w_dtype=FQDtype.FXP2)
        with g.name_scope('pool5'):
            pool5 = maxPool(conv5,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('flatten5'):
            flatten5 = flatten(pool5)

        with g.name_scope('fc6'):
            fc6 = fc(flatten5,
                     output_channels=1024,
                     w_dtype=FQDtype.FXP2,
                     f_dtype=FQDtype.FXP2)

        with g.name_scope('fc7'):
            fc7 = fc(fc6,
                     output_channels=1024,
                     w_dtype=FQDtype.FXP2,
                     f_dtype=FQDtype.FXP2)

        with g.name_scope('fc8'):
            fc8 = fc(fc7,
                     output_channels=10,
                     w_dtype=FQDtype.FXP2,
                     f_dtype=None)
    return g
Exemplo n.º 10
0
def get_svhn_qnn():
    '''
    SVHN
    QNN

    Weights are 1-bit
    Inputs are 8-bit for first layer, 1-bit for the rest

    '''
    g = Graph('SVHN-QNN', dataset='SVHN', log_level=logging.INFO)
    batch_size = 16

    with g.as_default():
        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, 32, 32, 3),
                           name='data',
                           dtype=FQDtype.FXP8,
                           trainable=False)

        with g.name_scope('conv0'):
            conv0 = conv(i,
                         filters=64,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.Bin,
                         w_dtype=FQDtype.Bin)

        with g.name_scope('conv1'):
            conv1 = conv(conv0,
                         filters=64,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.Bin,
                         w_dtype=FQDtype.Bin)
        with g.name_scope('pool1'):
            pool1 = maxPool(conv1,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv2'):
            conv2 = conv(pool1,
                         filters=128,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.Bin,
                         w_dtype=FQDtype.Bin)

        with g.name_scope('conv3'):
            conv3 = conv(conv2,
                         filters=128,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.Bin,
                         w_dtype=FQDtype.Bin)
        with g.name_scope('pool3'):
            pool3 = maxPool(conv3,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('conv4'):
            conv4 = conv(pool3,
                         filters=256,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.Bin,
                         w_dtype=FQDtype.Bin)

        with g.name_scope('conv5'):
            conv5 = conv(conv4,
                         filters=256,
                         kernel_size=3,
                         pad='SAME',
                         c_dtype=FQDtype.Bin,
                         w_dtype=FQDtype.Bin)
        with g.name_scope('pool5'):
            pool5 = maxPool(conv5,
                            pooling_kernel=(1, 2, 2, 1),
                            stride=(1, 2, 2, 1),
                            pad='VALID')

        with g.name_scope('flatten5'):
            flatten5 = flatten(pool5)

        with g.name_scope('fc6'):
            fc6 = fc(flatten5,
                     output_channels=1024,
                     w_dtype=FQDtype.Bin,
                     f_dtype=FQDtype.Bin)

        with g.name_scope('fc7'):
            fc7 = fc(fc6,
                     output_channels=1024,
                     w_dtype=FQDtype.Bin,
                     f_dtype=FQDtype.Bin)

        with g.name_scope('fc8'):
            fc8 = fc(fc7,
                     output_channels=10,
                     w_dtype=FQDtype.Bin,
                     f_dtype=None)
    return g
Exemplo n.º 11
0
def get_alex_net_wrpn():
    '''
    AlexNet
    Krizhevsky, Sutskever, and Hinton, 2012
    '''
    g = Graph('AlexNet-WRPN (2x)', dataset='imagenet', log_level=logging.INFO)
    batch_size = 16

    with g.as_default():
        with g.name_scope('inputs'):
            i = get_tensor(shape=(batch_size, 227, 227, 3),
                           name='data',
                           dtype=FQDtype.FXP8,
                           trainable=False)

        with g.name_scope('conv1_a'):
            conv1_a = conv(i,
                           filters=96,
                           kernel_size=11,
                           stride=(1, 4, 4, 1),
                           pad='VALID',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP8)
        with g.name_scope('pool1_a'):
            pool1_a = maxPool(conv1_a,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv1_b'):
            conv1_b = conv(i,
                           filters=96,
                           kernel_size=11,
                           stride=(1, 4, 4, 1),
                           pad='VALID',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP8)
        with g.name_scope('pool1_b'):
            pool1_b = maxPool(conv1_b,
                              pooling_kernel=(1, 2, 2, 1),
                              stride=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv2_a'):
            conv2_a = conv(pool1_a,
                           filters=256,
                           kernel_size=5,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)
        with g.name_scope('pool2_a'):
            pool2_a = maxPool(conv2_a,
                              pooling_kernel=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv2_b'):
            conv2_b = conv(pool1_b,
                           filters=256,
                           kernel_size=5,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)
        with g.name_scope('pool2_b'):
            pool2_b = maxPool(conv2_b,
                              pooling_kernel=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('concat2'):
            concat2 = concat((pool2_a, pool2_b),
                             concat_dim=-1,
                             dtype=FQDtype.FXP4)

        with g.name_scope('conv3_a'):
            conv3_a = conv(concat2,
                           filters=384,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)

        with g.name_scope('conv3_b'):
            conv3_b = conv(concat2,
                           filters=384,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)

        with g.name_scope('conv4_a'):
            conv4_a = conv(conv3_a,
                           filters=384,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)

        with g.name_scope('conv4_b'):
            conv4_b = conv(conv3_b,
                           filters=384,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)

        with g.name_scope('conv5_a'):
            conv5_a = conv(conv4_a,
                           filters=256,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)
        with g.name_scope('pool5_a'):
            pool5_a = maxPool(conv5_a,
                              pooling_kernel=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('conv5_b'):
            conv5_b = conv(conv4_b,
                           filters=256,
                           kernel_size=3,
                           pad='SAME',
                           c_dtype=FQDtype.FXP4,
                           w_dtype=FQDtype.FXP4)
        with g.name_scope('pool5_b'):
            pool5_b = maxPool(conv5_b,
                              pooling_kernel=(1, 2, 2, 1),
                              pad='VALID')

        with g.name_scope('concat5'):
            concat5 = concat((pool5_a, pool5_b), concat_dim=-1)

        with g.name_scope('flatten5'):
            flatten5 = flatten(concat5)

        with g.name_scope('fc1'):
            fc1 = fc(flatten5,
                     output_channels=8192,
                     w_dtype=FQDtype.FXP4,
                     f_dtype=FQDtype.FXP4)

        with g.name_scope('fc2'):
            fc2 = fc(fc1,
                     output_channels=8192,
                     w_dtype=FQDtype.FXP4,
                     f_dtype=FQDtype.FXP8)

        with g.name_scope('fc3'):
            fc3 = fc(fc2,
                     output_channels=1000,
                     w_dtype=FQDtype.FXP8,
                     f_dtype=None)

    return g