Beispiel #1
0
def test_conv_bn_relu_lego():
    from lego.hybrid import ConvBNReLULego
    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'))
    params = dict(name='1', kernel_size=5, num_output=16, pad=2,
                   stride=1, use_global_stats=False)
    lego = ConvBNReLULego(params)
    lego.attach(n, [n.data])
    assert n['conv_' + params['name']] is not None
    print >> sys.stderr, n.to_proto()
Beispiel #2
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
def test_conv_bn_relu_lego():
    from lego.hybrid import ConvBNReLULego
    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'))
    params = dict(name='1',
                  kernel_size=5,
                  num_output=16,
                  pad=2,
                  stride=1,
                  use_global_stats=False)
    lego = ConvBNReLULego(params)
    lego.attach(n, [n.data])
    assert n['conv_' + params['name']] is not None
    print >> sys.stderr, n.to_proto()
Beispiel #4
0
    # Data layer
    params = dict(name='data',
                  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
        source='/projects/flickr_sciences/jay/cifar10/dataframes/train',
        batch_size=64,
        include='test',
        mean_file='mean.binaryproto')
    data, label = ImageDataLego(params).attach(netspec)
    params = dict(
        name='data',
        source='/projects/flickr_sciences/jay/cifar10/dataframes/train',
        batch_size=64,
        include='train',
        mean_file='mean.binaryproto')
    data, label = ImageDataLego(params).attach(netspec)

    # 1st conv
    params = dict(name='1', num_output=16, kernel_size=3, pad=1, stride=1)
    conv1 = ConvBNReLULego(params).attach(netspec, [data])

    last = conv1
    for stage in range(3):
        for block in range(2 * N):

            # subsample at start of every stage except 1st
            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,
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()
Beispiel #7
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()
Beispiel #8
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
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()