示例#1
0
def test_complexity():
    from lego.base import BaseLegoFunction
    from lego.hybrid import ConvBNReLULego
    from tools.complexity import get_complexity

    n = caffe.NetSpec()
    params = dict(name='data', batch_size=16, ntop=2,
                  transform_param=dict(crop_size=224),
                  memory_data_param=dict(batch_size=16, channels=3,
                                         height=224, width=224))
    n.data, n.label = BaseLegoFunction('MemoryData', params).attach(n, [])
    params = dict(name='1', num_output=64, kernel_size=7,
                  use_global_stats=True, pad=3, stride=2)
    stage1 = ConvBNReLULego(params).attach(n, [n.data])
    params = dict(kernel_size=3, stride=2, pool=P.Pooling.MAX, name='pool1')
    pool1 = BaseLegoFunction('Pooling', params).attach(n, [stage1])


    ip_params = dict(name='fc10', num_output=10,
                     param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)])
    ip = BaseLegoFunction('InnerProduct', ip_params).attach(n, [pool1])
    smax_loss = BaseLegoFunction('SoftmaxWithLoss', dict(name='loss')).attach(n, [ip, n.label])

    params, conn = get_complexity(netspec=n)

    print >> sys.stderr, params, conn
示例#2
0
def AddExtraLayers(netspec, last):
    from lego.hybrid import ConvBNLego
    from lego.base import BaseLegoFunction

    use_relu = True
    for i in xrange(6, 9):
        name = 'conv' + str(i) + '_1'
        num_output = 256 if i == 6 else 128
        params = dict(name=name,
                      kernel_size=1,
                      num_output=num_output,
                      pad=0,
                      stride=1)  # use_global_stats=True)
        conv1 = BaseLegoFunction('Convolution', params).attach(netspec, [last])
        relu1 = BaseLegoFunction('ReLU', dict(name=name + '_relu')).attach(
            netspec, [conv1])

        name = 'conv' + str(i) + '_2'
        num_output = 512 if i == 6 else 256
        params = dict(name=name,
                      kernel_size=3,
                      num_output=num_output,
                      pad=1,
                      stride=2)  # use_global_stats=True)
        conv2 = BaseLegoFunction('Convolution',
                                 params).attach(netspec, [relu1])
        last = BaseLegoFunction('ReLU', dict(name=name + '_relu')).attach(
            netspec, [conv2])

    # Add global pooling layer.
    pool_param = dict(name='pool6', pool=P.Pooling.AVE, global_pooling=True)
    pool = BaseLegoFunction('Pooling', pool_param).attach(netspec, [last])

    return netspec
示例#3
0
def test_conv():
    from lego.base import BaseLegoFunction
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp' , batch_size=100),
                                   ntop=2, transform_param=dict(mean_file='tmp'))
    conv_params = dict(name='conv1', kernel_size=5, num_output=16, pad=2, stride=1)
    conv_lego = BaseLegoFunction('Convolution', conv_params)
    conv_lego.attach(n, [n.data])
    assert n[conv_params['name']] is not None
    print >> sys.stderr, n.to_proto()
示例#4
0
def test_conv():
    from lego.base import BaseLegoFunction
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp',
                                                        batch_size=100),
                                  ntop=2,
                                  transform_param=dict(mean_file='tmp'))
    conv_params = dict(name='conv1',
                       kernel_size=5,
                       num_output=16,
                       pad=2,
                       stride=1)
    conv_lego = BaseLegoFunction('Convolution', conv_params)
    conv_lego.attach(n, [n.data])
    assert n[conv_params['name']] is not None
    print >> sys.stderr, n.to_proto()
示例#5
0
def test_relu():
    from lego.base import BaseLegoFunction
    n = caffe.NetSpec()
    n.data, n.label = L.ImageData(image_data_param=dict(source='tmp',
                                                        batch_size=100),
                                  ntop=2,
                                  transform_param=dict(mean_file='tmp'))
    BaseLegoFunction('ReLU', dict(name='relu1')).attach(n, [n.data])
    assert n['relu1'] is not None
示例#6
0
def add_extra_layers_resnet(netspec, last, params):
    from lego.hybrid import ShortcutLego, ConvBNReLULego

    from lego.base import BaseLegoFunction

    blocks = params['extra_blocks']
    num_outputs = params['extra_num_outputs']
    is_train = params['is_train']
    main_branch = params['main_branch']

    use_global_stats = False if is_train else True

    for stage in range(len(blocks)):
        for block in range(blocks[stage]):
            if block == 0:
                shortcut = 'projection'
                stride = 2
            else:
                shortcut = 'identity'
                stride = 1

            name = 'stage' + str(stage + 4) + '_block' + str(block)
            curr_num_output = num_outputs[stage]

            params = dict(name=name,
                          num_output=curr_num_output,
                          shortcut=shortcut,
                          main_branch=main_branch,
                          stride=stride,
                          use_global_stats=use_global_stats,
                          filter_mult=None)
            last = ShortcutLego(params).attach(netspec, [last])

    # Add global pooling layer.
    pool_param = dict(name='pool_last',
                      pool=P.Pooling.AVE,
                      global_pooling=True)
    pool = BaseLegoFunction('Pooling', pool_param).attach(netspec, [last])
    return netspec
示例#7
0
                  source='tmp',
                  batch_size=100,
                  include='test',
                  mean_file='tmp')
    data, label = ImageDataLego(params).attach(netspec)

    # Stage 1
    params = dict(name='1',
                  num_output=96,
                  kernel_size=7,
                  pad=0,
                  stride=2,
                  use_global_stats=True)
    stage1 = ConvBNReLULego(params).attach(netspec, [data])
    params = dict(kernel_size=3, stride=2, pool=P.Pooling.MAX, name='pool1')
    pool1 = BaseLegoFunction('Pooling', params).attach(netspec, [stage1])

    # Fire modules 2 - 9
    num_output = 16
    last = pool1
    ctr = 0
    for i in range(2, 7):
        params = dict(name='fire' + str(i),
                      squeeze_num_output=num_output,
                      use_global_stats=True)
        last = FireLego(params).attach(netspec, [last])
        ctr += 1
        if ctr % 2 == 0:
            num_output += 16

    # Pool8
            if stage > 0 and block == 0:
                stride = 2
            else:
                stride = 1
            name = 'stage' + str(stage) + '_block' + str(block)
            curr_num_output = num_output * (2**(stage))
            params = dict(name=name + str(),
                          num_output=curr_num_output,
                          kernel_size=3,
                          pad=1,
                          stride=stride)
            last = ConvBNReLULego(params).attach(netspec, [last])

    # Last stage
    pool_params = dict(kernel_size=8,
                       stride=1,
                       pool=P.Pooling.AVE,
                       name='pool')
    pool = BaseLegoFunction('Pooling', pool_params).attach(netspec, [last])

    ip_params = dict(
        name='fc10',
        num_output=10,
        param=[dict(lr_mult=1, decay_mult=1),
               dict(lr_mult=2, decay_mult=0)])
    ip = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [pool])
    smax_loss = BaseLegoFunction('SoftmaxWithLoss', dict(name='loss')).attach(
        netspec, [ip, label])

    print netspec.to_proto()
示例#9
0
def write_prototxt(is_train, source, output_folder):
    netspec = caffe.NetSpec()
    if is_train:
        include = 'train'
        use_global_stats = False
        batch_size = 128
    else:
        include = 'test'
        use_global_stats = True
        batch_size = 1

    # Data layer
    params = dict(name='data',
                  batch_size=1,
                  ntop=2,
                  transform_param=dict(crop_size=224),
                  memory_data_param=dict(batch_size=1,
                                         channels=3,
                                         height=224,
                                         width=224))
    netspec.data, netspec.label = BaseLegoFunction('MemoryData',
                                                   params).attach(netspec, [])

    last = netspec.data
    num_outputs = [64, 128, 256, 512, 512]
    # Conv layers stages
    for stage in range(1, 6):
        blocks = 2 if stage < 3 else 3
        for b in range(1, blocks + 1):
            name = str(stage) + '_' + str(b)

            params = dict(name=name,
                          num_output=num_outputs[stage - 1],
                          kernel_size=3,
                          pad=1,
                          stride=1)
            last = ConvReLULego(params).attach(netspec, [last])

        pool_params = dict(name='pool_' + str(stage),
                           kernel_size=2,
                           stride=2,
                           pool=P.Pooling.MAX)
        last = BaseLegoFunction('Pooling', pool_params).attach(netspec, [last])

    # FC layers
    ip_params = dict(name='fc6', num_output=4096)
    fc6 = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [last])
    relu_params = dict(name='relu6')
    relu6 = BaseLegoFunction('ReLU', relu_params).attach(netspec, [fc6])
    drop_params = dict(name='drop6', dropout_param=dict(dropout_ratio=0.5))
    drop6 = BaseLegoFunction('Dropout', drop_params).attach(netspec, [relu6])

    ip_params = dict(name='fc7', num_output=4096)
    fc7 = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [drop6])
    relu_params = dict(name='relu7')
    relu7 = BaseLegoFunction('ReLU', relu_params).attach(netspec, [fc7])
    drop_params = dict(name='drop7', dropout_param=dict(dropout_ratio=0.5))
    drop7 = BaseLegoFunction('Dropout', drop_params).attach(netspec, [relu7])

    ip_params = dict(name='fc8', num_output=1000)
    fc8 = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [drop7])

    # Loss and accuracy
    smax_loss = BaseLegoFunction('SoftmaxWithLoss', dict(name='loss')).attach(
        netspec, [fc8, netspec.label])

    if include == 'test':
        BaseLegoFunction('Accuracy',
                         dict(name='accuracy')).attach(netspec,
                                                       [fc8, netspec.label])
    filename = 'train.prototxt' if is_train else 'test.prototxt'
    filepath = output_folder + '/' + filename
    fp = open(filepath, 'w')
    print >> fp, netspec.to_proto()
    fp.close()
示例#10
0
def write_prototxt(is_train, source, output_folder, main_branch,
                   num_output_stage1, fc_layers, blocks):

    from lego.hybrid import ConvBNReLULego, EltwiseReLULego, ShortcutLego
    from lego.data import ImageDataLego
    from lego.base import BaseLegoFunction

    netspec = caffe.NetSpec()

    if is_train:
        include = 'train'
        use_global_stats = False
        batch_size = 128
    else:
        include = 'test'
        use_global_stats = True
        batch_size = 1

    # Data layer
    params = dict(name='data',
                  batch_size=1,
                  ntop=2,
                  transform_param=dict(crop_size=224),
                  memory_data_param=dict(batch_size=1,
                                         channels=3,
                                         height=224,
                                         width=224))
    netspec.data, netspec.label = BaseLegoFunction('MemoryData',
                                                   params).attach(netspec, [])

    # Stage 1
    params = dict(name='1',
                  num_output=64,
                  kernel_size=7,
                  use_global_stats=use_global_stats,
                  pad=3,
                  stride=2)
    stage1 = ConvBNReLULego(params).attach(netspec, [netspec.data])
    params = dict(kernel_size=3, stride=2, pool=P.Pooling.MAX, name='pool1')
    pool1 = BaseLegoFunction('Pooling', params).attach(netspec, [stage1])

    num_output = num_output_stage1

    # Stages 2 - 5
    last = pool1
    for stage in range(4):
        name = str(stage + 1)

        for block in range(blocks[stage]):
            if block == 0:
                shortcut = 'projection'
                if stage > 0:
                    stride = 2
                else:
                    stride = 1
            else:
                shortcut = 'identity'
                stride = 1

            # this is for resnet 18 / 34, where the first block of stage
            # 0 does not need projection shortcut
            if block == 0 and stage == 0 and main_branch in [
                    'normal', 'inception_trick'
            ]:
                shortcut = 'identity'

            name = 'stage' + str(stage) + '_block' + str(block)
            curr_num_output = num_output * (2**(stage))

            params = dict(
                name=name,
                num_output=curr_num_output,
                shortcut=shortcut,
                main_branch=main_branch,
                stride=stride,
                use_global_stats=use_global_stats,
            )
            last = ShortcutLego(params).attach(netspec, [last])

    # Last stage
    if not fc_layers:
        pool_params = dict(kernel_size=7,
                           stride=1,
                           pool=P.Pooling.AVE,
                           name='pool')
        pool = BaseLegoFunction('Pooling', pool_params).attach(netspec, [last])
        ip_params = dict(name='fc1000', num_output=1000)
        ip = BaseLegoFunction('InnerProduct',
                              ip_params).attach(netspec, [pool])
    else:
        conv_last1_params = dict(name='1by1_2048',
                                 kernel_size=1,
                                 num_output=2048,
                                 use_global_stats=use_global_stats,
                                 pad=0,
                                 stride=1)
        conv_last1 = ConvBNReLULego(conv_last1_params).attach(netspec, [last])

        pool_last_params = dict(kernel_size=7,
                                stride=1,
                                pool=P.Pooling.AVE,
                                name='pool')
        pool_last = BaseLegoFunction('Pooling', pool_last_params).attach(
            netspec, [conv_last1])
        # pool_last = BaseLegoFunction('Pooling', pool_last_params).attach(netspec, [last])

        conv_last2_params = dict(name='1by1_4096',
                                 kernel_size=1,
                                 num_output=4096,
                                 use_global_stats=use_global_stats,
                                 pad=0,
                                 stride=1)
        conv_last2 = ConvBNReLULego(conv_last2_params).attach(
            netspec, [pool_last])
        drop_last2 = BaseLegoFunction(
            'Dropout', dict(name='drop_1by1_4096',
                            dropout_ratio=0.2)).attach(netspec, [conv_last2])

        conv_last3_params = dict(name='1by1_1000',
                                 kernel_size=1,
                                 num_output=1000,
                                 use_global_stats=use_global_stats,
                                 pad=0,
                                 stride=1)
        ip = ConvBNReLULego(conv_last3_params).attach(netspec, [drop_last2])

    smax_loss = BaseLegoFunction('SoftmaxWithLoss', dict(name='loss')).attach(
        netspec, [ip, netspec.label])

    if include == 'test':
        BaseLegoFunction('Accuracy',
                         dict(name='accuracy')).attach(netspec,
                                                       [ip, netspec.label])
    filename = 'train.prototxt' if is_train else 'test.prototxt'
    filepath = output_folder + '/' + filename
    fp = open(filepath, 'w')
    print >> fp, netspec.to_proto()
    fp.close()
示例#11
0
def write_prototxt(is_train, source, output_folder):
    netspec = caffe.NetSpec()
    if is_train:
        include = 'train'
        use_global_stats = False
        batch_size = 128
    else:
        include = 'test'
        use_global_stats = True
        batch_size = 1

    # Data layer
    params = dict(name='data',
                  batch_size=1,
                  ntop=2,
                  transform_param=dict(crop_size=256),
                  memory_data_param=dict(batch_size=1,
                                         channels=3,
                                         height=227,
                                         width=227))
    netspec.data, netspec.label = BaseLegoFunction('MemoryData',
                                                   params).attach(netspec, [])

    # Conv 1
    params = dict(name='1',
                  num_output=96,
                  kernel_size=11,
                  pad=1,
                  stride=4,
                  use_global_stats=use_global_stats)
    conv1 = ConvBNReLULego(params).attach(netspec, [netspec.data])
    pool_params = dict(name='pool_1',
                       kernel_size=3,
                       stride=2,
                       pool=P.Pooling.MAX)
    pool1 = BaseLegoFunction('Pooling', pool_params).attach(netspec, [conv1])

    # Conv 2
    params = dict(name='2',
                  num_output=256,
                  kernel_size=5,
                  pad=2,
                  stride=1,
                  use_global_stats=use_global_stats)
    conv2 = ConvBNReLULego(params).attach(netspec, [pool1])
    pool_params = dict(name='pool_2',
                       kernel_size=3,
                       stride=2,
                       pool=P.Pooling.MAX)
    pool2 = BaseLegoFunction('Pooling', pool_params).attach(netspec, [conv2])

    # Conv 3
    params = dict(name='3',
                  num_output=384,
                  kernel_size=3,
                  pad=1,
                  stride=1,
                  use_global_stats=use_global_stats)
    conv3 = ConvBNReLULego(params).attach(netspec, [pool2])

    # Conv 4
    params = dict(name='4',
                  num_output=384,
                  kernel_size=3,
                  pad=1,
                  stride=1,
                  use_global_stats=use_global_stats)
    conv4 = ConvBNReLULego(params).attach(netspec, [conv3])

    # Conv 5
    params = dict(name='5',
                  num_output=256,
                  kernel_size=3,
                  pad=1,
                  stride=1,
                  use_global_stats=use_global_stats)
    conv5 = ConvBNReLULego(params).attach(netspec, [conv4])

    # FC layers
    ip_params = dict(name='fc6', num_output=4096)
    fc6 = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [conv5])
    relu_params = dict(name='relu6')
    relu6 = BaseLegoFunction('ReLU', relu_params).attach(netspec, [fc6])
    drop_params = dict(name='drop6', dropout_param=dict(dropout_ratio=0.5))
    drop6 = BaseLegoFunction('Dropout', drop_params).attach(netspec, [relu6])

    ip_params = dict(name='fc7', num_output=4096)
    fc7 = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [drop6])
    relu_params = dict(name='relu7')
    relu7 = BaseLegoFunction('ReLU', relu_params).attach(netspec, [fc7])
    drop_params = dict(name='drop7', dropout_param=dict(dropout_ratio=0.5))
    drop7 = BaseLegoFunction('Dropout', drop_params).attach(netspec, [relu7])

    ip_params = dict(name='fc8', num_output=1000)
    fc8 = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [drop7])

    # Loss and accuracy
    smax_loss = BaseLegoFunction('SoftmaxWithLoss', dict(name='loss')).attach(
        netspec, [fc8, netspec.label])

    if include == 'test':
        BaseLegoFunction('Accuracy',
                         dict(name='accuracy')).attach(netspec,
                                                       [fc8, netspec.label])
    filename = 'train.prototxt' if is_train else 'test.prototxt'
    filepath = output_folder + '/' + filename
    fp = open(filepath, 'w')
    print >> fp, netspec.to_proto()
    fp.close()
示例#12
0
    def stitch(self, is_train, source, main_branch, num_output_stage1,
               fc_layers, blocks):
        from lego.hybrid import ConvBNReLULego, EltwiseReLULego, ShortcutLego, ConvBNLego
        from lego.data import ImageDataLego
        from lego.base import BaseLegoFunction

        netspec = caffe.NetSpec()

        if is_train:
            include = 'train'
            use_global_stats = False
            batch_size = 256
        else:
            include = 'test'
            use_global_stats = True
            batch_size = 1

        # Freeze 1st 2 stages and dont update batch norm stats
        Config.set_default_params(
            'Convolution', 'param',
            [dict(lr_mult=0, decay_mult=0),
             dict(lr_mult=0, decay_mult=0)])
        if is_train:
            use_global_stats = True

        # Data layer, its dummy, you need to replace this with Annotated data layer
        params = dict(name='data',
                      batch_size=1,
                      ntop=2,
                      memory_data_param=dict(batch_size=1,
                                             channels=3,
                                             height=300,
                                             width=300))
        netspec.data, netspec.label = BaseLegoFunction('MemoryData',
                                                       params).attach(
                                                           netspec, [])

        # Stage 1
        params = dict(name='1',
                      num_output=64,
                      kernel_size=7,
                      use_global_stats=use_global_stats,
                      pad=3,
                      stride=2)
        stage1 = ConvBNReLULego(params).attach(netspec, [netspec.data])
        params = dict(kernel_size=3,
                      stride=2,
                      pool=P.Pooling.MAX,
                      name='pool1')
        pool1 = BaseLegoFunction('Pooling', params).attach(netspec, [stage1])

        num_output = num_output_stage1

        # Stages 2 - 5
        last = pool1
        for stage in range(4):
            name = str(stage + 1)

            for block in range(blocks[stage]):
                if block == 0:
                    shortcut = 'projection'
                    if stage > 0:
                        stride = 2
                    else:
                        stride = 1
                else:
                    shortcut = 'identity'
                    stride = 1

                # this is for resnet 18 / 34, where the first block of stage
                # 0 does not need projection shortcut
                if block == 0 and stage == 0 and main_branch == 'normal':
                    shortcut = 'identity'

                # This is for not downsampling while creating detection
                # network
                # if block == 0 and stage == 1:
                #    stride = 1

                name = 'stage' + str(stage) + '_block' + str(block)
                curr_num_output = num_output * (2**(stage))

                params = dict(
                    name=name,
                    num_output=curr_num_output,
                    shortcut=shortcut,
                    main_branch=main_branch,
                    stride=stride,
                    use_global_stats=use_global_stats,
                )
                last = ShortcutLego(params).attach(netspec, [last])

            # TODO: Note this should be configurable
            if stage == 0:
                Config.set_default_params('Convolution', 'param', [
                    dict(lr_mult=1, decay_mult=1),
                    dict(lr_mult=2, decay_mult=0)
                ])
                if is_train:
                    use_global_stats = False
        '''
                You should modify these layers in order to experiment with different 
                architectures specific for detection
        '''
        if not fc_layers:
            # Last stage
            pool_params = dict(kernel_size=7,
                               stride=1,
                               pool=P.Pooling.AVE,
                               name='pool',
                               pad=3)
            pool = BaseLegoFunction('Pooling',
                                    pool_params).attach(netspec, [last])
        else:
            '''pool_params = dict(name='pool_before1024', kernel_size=3, stride=1, pool=P.Pooling.MAX, pad=1)
            pool = BaseLegoFunction('Pooling', pool_params).attach(netspec, [last])
            conv_last1_params = dict(name='3by3_1024', num_output=1024, use_global_stats=use_global_stats,
                     # pad=0, kernel_size=1)
                     pad=1, kernel_size=3, dilation=3)
            conv_last1 = ConvBNReLULego(conv_last1_params).attach(netspec, [pool])
            '''

            conv_last1_params = dict(name='1by1_2048',
                                     num_output=2048,
                                     use_global_stats=use_global_stats,
                                     pad=1,
                                     kernel_size=1,
                                     dilation=1)
            # pad=3, kernel_size=7)
            conv_last1 = ConvBNReLULego(conv_last1_params).attach(
                netspec, [last])

            pool_last_params = dict(kernel_size=7,
                                    stride=1,
                                    pool=P.Pooling.AVE,
                                    name='pool',
                                    pad=3)
            pool_last = BaseLegoFunction('Pooling', pool_last_params).attach(
                netspec, [conv_last1])

            conv_last2_params = dict(name='1by1_4096',
                                     kernel_size=1,
                                     num_output=4096,
                                     use_global_stats=use_global_stats,
                                     stride=1,
                                     pad=0)
            conv_last2 = ConvBNReLULego(conv_last2_params).attach(
                netspec, [pool_last])

        return netspec
示例#13
0
    def stitch(self,
               is_train=True,
               reduced=True,
               fully_conv=True,
               dilated=True,
               dropout=False):
        netspec = caffe.NetSpec()
        if is_train:
            include = 'train'
            use_global_stats = False
            batch_size = 128
        else:
            include = 'test'
            use_global_stats = True
            batch_size = 1

        Config.set_default_params(
            'Convolution', 'param',
            [dict(lr_mult=0, decay_mult=0),
             dict(lr_mult=0, decay_mult=0)])

        # Data layer
        params = dict(name='data',
                      batch_size=1,
                      ntop=2,
                      transform_param=dict(crop_size=300),
                      memory_data_param=dict(batch_size=1,
                                             channels=3,
                                             height=300,
                                             width=300))
        netspec.data, netspec.label = BaseLegoFunction('MemoryData',
                                                       params).attach(
                                                           netspec, [])

        last = netspec.data
        num_outputs = [64, 128, 256, 512, 512]
        # Conv layers stages
        for stage in range(1, 6):
            blocks = 2 if stage < 3 else 3
            for b in range(1, blocks + 1):
                name = str(stage) + '_' + str(b)

                params = dict(name=name,
                              num_output=num_outputs[stage - 1],
                              kernel_size=3,
                              pad=1,
                              stride=1)
                last = ConvReLULego(params).attach(netspec, [last])

            if dilated and stage == 5:
                pool_params = dict(name='pool' + str(stage),
                                   kernel_size=3,
                                   stride=1,
                                   pool=P.Pooling.MAX,
                                   pad=1)
            else:
                pool_params = dict(name='pool' + str(stage),
                                   kernel_size=2,
                                   stride=2,
                                   pool=P.Pooling.MAX)
            last = BaseLegoFunction('Pooling',
                                    pool_params).attach(netspec, [last])

            if stage == 2:
                Config.set_default_params('Convolution', 'param', [
                    dict(lr_mult=1, decay_mult=1),
                    dict(lr_mult=2, decay_mult=0)
                ])

        # FC layers
        if fully_conv:
            # FC6 fully convolutional
            if dilated:
                if reduced:
                    ip_params = dict(name='fc6',
                                     num_output=1024,
                                     pad=6,
                                     kernel_size=3,
                                     dilation=6)
                else:
                    ip_params = dict(name='fc6',
                                     num_output=4096,
                                     pad=6,
                                     kernel_size=7,
                                     dilation=2)
            else:
                if reduced:
                    ip_params = dict(name='fc6',
                                     num_output=1024,
                                     pad=3,
                                     kernel_size=3,
                                     dilation=3)
                else:
                    ip_params = dict(name='fc6',
                                     num_output=4096,
                                     pad=3,
                                     kernel_size=7)

            fc6 = BaseLegoFunction('Convolution',
                                   ip_params).attach(netspec, [last])
            relu_params = dict(name='relu6', in_place=True)
            out6 = BaseLegoFunction('ReLU', relu_params).attach(netspec, [fc6])
            if dropout:
                drop_params = dict(name='drop6',
                                   dropout_param=dict(dropout_ratio=0.5),
                                   in_place=True)
                out6 = BaseLegoFunction('Dropout',
                                        drop_params).attach(netspec, [out6])

            # FC7 fully convolutional
            if reduced:
                ip_params = dict(name='fc7', num_output=1024, kernel_size=1)
            else:
                ip_params = dict(name='fc7', num_output=4096, kernel_size=1)
            fc7 = BaseLegoFunction('Convolution',
                                   ip_params).attach(netspec, [out6])
            relu_params = dict(name='relu7', in_place=True)
            out7 = BaseLegoFunction('ReLU', relu_params).attach(netspec, [fc7])
            if dropout:
                drop_params = dict(name='drop6',
                                   dropout_param=dict(dropout_ratio=0.5),
                                   in_place=True)
                out7 = BaseLegoFunction('Dropout',
                                        drop_params).attach(netspec, [out7])

        else:
            ip_params = dict(name='fc6', num_output=4096)
            fc6 = BaseLegoFunction('InnerProduct',
                                   ip_params).attach(netspec, [last])
            relu_params = dict(name='relu6')
            relu6 = BaseLegoFunction('ReLU',
                                     relu_params).attach(netspec, [fc6])
            drop_params = dict(name='drop6',
                               dropout_param=dict(dropout_ratio=0.5))
            drop6 = BaseLegoFunction('Dropout',
                                     drop_params).attach(netspec, [relu6])

            ip_params = dict(name='fc7', num_output=4096)
            fc7 = BaseLegoFunction('InnerProduct',
                                   ip_params).attach(netspec, [drop6])
            relu_params = dict(name='relu7')
            relu7 = BaseLegoFunction('ReLU',
                                     relu_params).attach(netspec, [fc7])
            drop_params = dict(name='drop7',
                               dropout_param=dict(dropout_ratio=0.5))
            drop7 = BaseLegoFunction('Dropout',
                                     drop_params).attach(netspec, [relu7])

        return netspec
示例#14
0
def write_prototxt(is_train, output_folder, N, main_branch):
    from lego.hybrid import ConvBNReLULego, EltwiseReLULego, ShortcutLego
    from lego.base import BaseLegoFunction

    netspec = caffe.NetSpec()

    num_output = 16

    if is_train:
        include = 'train'
        use_global_stats = False
        batch_size = 128
    else:
        include = 'test'
        use_global_stats = True
        batch_size = 1

    # Data layer
    params = dict(name='data',
                  batch_size=1,
                  ntop=2,
                  transform_param=dict(crop_size=32),
                  memory_data_param=dict(batch_size=1,
                                         channels=3,
                                         height=32,
                                         width=32))
    netspec.data, netspec.label = BaseLegoFunction('MemoryData',
                                                   params).attach(netspec, [])
    # 1st conv
    params = dict(name='1',
                  num_output=num_output,
                  kernel_size=3,
                  pad=1,
                  stride=1,
                  use_global_stats=use_global_stats)
    conv1 = ConvBNReLULego(params).attach(netspec, [netspec.data])

    last = conv1

    for stage in range(3):
        for block in range(N):

            # subsample at start of every stage except 1st
            if stage > 0 and block == 0:
                shortcut = 'projection'
                stride = 2
            else:
                shortcut = 'identity'
                stride = 1

            name = 'stage' + str(stage) + '_block' + str(block)
            curr_num_output = num_output * (2**(stage))

            params = dict(name=name,
                          num_output=curr_num_output,
                          shortcut=shortcut,
                          main_branch=main_branch,
                          stride=stride,
                          use_global_stats=use_global_stats)
            last = ShortcutLego(params).attach(netspec, [last])

    # Last stage
    pool_params = dict(kernel_size=8,
                       stride=1,
                       pool=P.Pooling.AVE,
                       name='pool')
    pool = BaseLegoFunction('Pooling', pool_params).attach(netspec, [last])
    ip_params = dict(
        name='fc10',
        num_output=10,
        param=[dict(lr_mult=1, decay_mult=1),
               dict(lr_mult=2, decay_mult=0)])
    ip = BaseLegoFunction('InnerProduct', ip_params).attach(netspec, [pool])
    smax_loss = BaseLegoFunction('SoftmaxWithLoss', dict(name='loss')).attach(
        netspec, [ip, netspec.label])

    if include == 'test':
        BaseLegoFunction('Accuracy',
                         dict(name='accuracy')).attach(netspec,
                                                       [ip, netspec.label])
    filename = 'train.prototxt' if is_train else 'test.prototxt'
    filepath = output_folder + '/' + filename
    fp = open(filepath, 'w')
    print >> fp, netspec.to_proto()
    fp.close()