(self, batch_size, phase='TRAIN', stages=(3, 4, 6, 3))
return n.to_proto()
:param batch_size: the batch_size of train and test phase
        :param phase: TRAIN or TEST
        :param stages: the num of layers = 2 + 3*sum(stages), layers would better be chosen from [50, 101, 152]
                       {every stage is composed of 1 residual_branch_shortcut module and stage[i]-1 residual_branch
                       modules, each module consists of 3 conv layers}
                        (3, 4, 6, 3) for 50 layers; (3, 4, 23, 3) for 101 layers; (3, 8, 36, 3) for 152 layers
def resnet_layers_proto(self, batch_size, phase='TRAIN', stages=(3, 4, 6, 3)):
        """

        :param batch_size: the batch_size of train and test phase
        :param phase: TRAIN or TEST
        :param stages: the num of layers = 2 + 3*sum(stages), layers would better be chosen from [50, 101, 152]
                       {every stage is composed of 1 residual_branch_shortcut module and stage[i]-1 residual_branch
                       modules, each module consists of 3 conv layers}
                        (3, 4, 6, 3) for 50 layers; (3, 4, 23, 3) for 101 layers; (3, 8, 36, 3) for 152 layers
        """
        n = caffe.NetSpec()
        if phase == 'TRAIN':
            source_data = self.train_data
            mirror = True
        else:
            source_data = self.test_data
            mirror = False
        n.data, n.label = L.Data(source=source_data, backend=P.Data.LMDB, batch_size=batch_size, ntop=2,
                                 transform_param=dict(crop_size=224, mean_value=[104, 117, 123], mirror=mirror))

        n.conv1, n.conv1_bn, n.conv1_scale, n.conv1_relu = \
            conv_bn_scale_relu(n.data, num_output=64, kernel_size=7, stride=2, pad=3)  # 64x112x112
        n.pool1 = L.Pooling(n.conv1, kernel_size=3, stride=2, pool=P.Pooling.MAX)  # 64x56x56

        for num in xrange(len(stages)):  # num = 0, 1, 2, 3
            for i in xrange(stages[num]):
                if i == 0:
                    stage_string = branch_shortcut_string
                    bottom_string = ['n.pool1', 'n.res2b%s' % str(stages[0] - 1), 'n.res3b%s' % str(stages[1] - 1),
                                     'n.res4b%s' % str(stages[2] - 1)][num]
                else:
                    stage_string = branch_string
                    if i == 1:
                        bottom_string = 'n.res%sa' % str(num + 2)
                    else:
                        bottom_string = 'n.res%sb%s' % (str(num + 2), str(i - 1))
                exec (stage_string.replace('(stage)', str(num + 2)).replace('(bottom)', bottom_string).
                      replace('(num)', str(2 ** num * 64)).replace('(order)', str(i)).
                      replace('(stride)', str(int(num > 0) + 1)))

        exec 'n.pool5 = L.Pooling((bottom), pool=P.Pooling.AVE, global_pooling=True)'.\
            replace('(bottom)', 'n.res5b%s' % str(stages[3] - 1))
        n.classifier = L.InnerProduct(n.pool5, num_output=self.classifier_num,
                                      param=[dict(lr_mult=1, decay_mult=1), dict(lr_mult=2, decay_mult=0)],
                                      weight_filler=dict(type='xavier'),
                                      bias_filler=dict(type='constant', value=0))
        n.loss = L.SoftmaxWithLoss(n.classifier, n.label)
        if phase == 'TRAIN':
            pass
        else:
            n.accuracy_top1 = L.Accuracy(n.classifier, n.label, include=dict(phase=1))
            n.accuracy_top5 = L.Accuracy(n.classifier, n.label, include=dict(phase=1),
                                         accuracy_param=dict(top_k=5))

        return n.to_proto()
def resnet_layers_proto ( self , batch_size , phase = 'TRAIN' , stages = ( 3 , 4 , 6 , 3 ) ) : n = caffe . NetSpec ( ) if phase == 'TRAIN' : source_data = self . train_data mirror = True else : source_data = self . test_data mirror = False n . data , n . label = L . Data ( source = source_data , backend = P . Data . LMDB , batch_size = batch_size , ntop = 2 , transform_param = dict ( crop_size = 224 , mean_value = [ 104 , 117 , 123 ] , mirror = mirror ) ) n . conv1 , n . conv1_bn , n . conv1_scale , n . conv1_relu = conv_bn_scale_relu ( n . data , num_output = 64 , kernel_size = 7 , stride = 2 , pad = 3 ) # 64x112x112 n . pool1 = L . Pooling ( n . conv1 , kernel_size = 3 , stride = 2 , pool = P . Pooling . MAX ) # 64x56x56 for num in xrange ( len ( stages ) ) : # num = 0, 1, 2, 3 for i in xrange ( stages [ num ] ) : if i == 0 : stage_string = branch_shortcut_string bottom_string = [ 'n.pool1' , 'n.res2b%s' % str ( stages [ 0 ] - 1 ) , 'n.res3b%s' % str ( stages [ 1 ] - 1 ) , 'n.res4b%s' % str ( stages [ 2 ] - 1 ) ] [ num ] else : stage_string = branch_string if i == 1 : bottom_string = 'n.res%sa' % str ( num + 2 ) else : bottom_string = 'n.res%sb%s' % ( str ( num + 2 ) , str ( i - 1 ) ) exec ( stage_string . replace ( '(stage)' , str ( num + 2 ) ) . replace ( '(bottom)' , bottom_string ) . replace ( '(num)' , str ( 2 ** num * 64 ) ) . replace ( '(order)' , str ( i ) ) . replace ( '(stride)' , str ( int ( num > 0 ) + 1 ) ) ) exec 'n.pool5 = L.Pooling((bottom), pool=P.Pooling.AVE, global_pooling=True)' . replace ( '(bottom)' , 'n.res5b%s' % str ( stages [ 3 ] - 1 ) ) n . classifier = L . InnerProduct ( n . pool5 , num_output = self . classifier_num , param = [ dict ( lr_mult = 1 , decay_mult = 1 ) , dict ( lr_mult = 2 , decay_mult = 0 ) ] , weight_filler = dict ( type = 'xavier' ) , bias_filler = dict ( type = 'constant' , value = 0 ) ) n . loss = L . SoftmaxWithLoss ( n . classifier , n . label ) if phase == 'TRAIN' : pass else : n . accuracy_top1 = L . Accuracy ( n . classifier , n . label , include = dict ( phase = 1 ) ) n . accuracy_top5 = L . Accuracy ( n . classifier , n . label , include = dict ( phase = 1 ) , accuracy_param = dict ( top_k = 5 ) ) return n . to_proto ( )