コード例 #1
0
    def create(self, dataLayerParams, phase="train"):
        n = caffe.NetSpec()

        n.data, n.label = L.Python(module="NpyDataLayer",
                                   layer=self.dataLayer,
                                   ntop=2,
                                   param_str=str(dataLayerParams))

        n.input_conv = L.Convolution(n.data,
                                     num_output=16,
                                     kernel_size=1,
                                     stride=1,
                                     pad=1,
                                     bias_term=False,
                                     param=[dict(lr_mult=1, decay_mult=1)],
                                     weight_filler=dict(type="xavier"))
        n.input_relu = L.ReLU(n.input_conv, in_place=False)

        for i in range(len(self.stages)):
            for j in range(self.stages[i]):
                stageString = self.resnetString

                bottomString = 'n.input_relu'
                if (i != 0) or (j != 0):
                    bottomString = 'n.res{}_add'.format(
                        str(sum(self.stages[:i]) + j))

                exec(
                    stageString.replace('(bottom)', bottomString).replace(
                        '(output)', str(2**i * 64)).replace(
                            '(n)', str(sum(self.stages[:i]) + j + 1)))

        exec(
            'n.pool_ave = L.Pooling(n.res{}_add, pool=P.Pooling.AVE, global_pooling=True)'
            .format(str(sum(self.stages))))
        n.classifier = L.InnerProduct(n.pool_ave,
                                      num_output=self.classCount,
                                      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))

        if phase == "train":
            n.loss = L.SoftmaxWithLoss(n.classifier, n.label)
        elif phase == "test":
            n.softmax_out = L.Softmax(n.classifier)
            n.accuracy_top1 = L.Accuracy(n.softmax_out,
                                         n.label,
                                         accuracy_param=dict(top_k=1, axis=1))
            n.accuracy_top5 = L.Accuracy(n.softmax_out,
                                         n.label,
                                         accuracy_param=dict(top_k=5, axis=1))
        else:  # deploy
            n.softmax_out = L.Softmax(n.classifier)
            n.result = L.ArgMax(n.softmax_out, argmax_param=dict(axis=1))

        return n.to_proto()
コード例 #2
0
    def create(self, dataLayerParams, phase="train"):
        n = caffe.NetSpec()

        n.data, n.label = L.Python(module="VDataLayer",
                                   layer=self.dataLayer,
                                   ntop=2,
                                   param_str=str(dataLayerParams))

        n.input_conv = L.Convolution(n.data,
                                     num_output=16,
                                     kernel_size=1,
                                     stride=1,
                                     pad=1,
                                     bias_term=False,
                                     param=[dict(lr_mult=1, decay_mult=1)],
                                     weight_filler=dict(type="xavier"))
        n.input_relu = L.ReLU(n.input_conv, in_place=False)

        for i in range(len(self.stages)):
            for j in range(self.stages[i]):
                stageString = self.resnetString

                bottomString = 'n.input_relu'
                if (i != 0) or (j != 0):
                    bottomString = 'n.res{}_add'.format(
                        str(sum(self.stages[:i]) + j))

                exec(
                    stageString.replace('(bottom)', bottomString).replace(
                        '(output)', str(2**i * 64)).replace(
                            '(n)', str(sum(self.stages[:i]) + j + 1)))

        exec(
            'n.conv_output = L.Convolution(n.res{}_add, num_output=2, kernel_size=1, stride=1, pad=1, bias_term=False, param=[dict(lr_mult=1, decay_mult=1)], weight_filler=dict(type="xavier"))'
            .format(str(sum(self.stages))))

        # reshape result and label
        n.flat_output = L.Reshape(n.conv_output,
                                  reshape_param={"shape": {
                                      "dim": [0, 2, -1]
                                  }})
        n.flat_label = L.Reshape(n.label,
                                 reshape_param={"shape": {
                                     "dim": [0, 1, -1]
                                 }})

        if phase == "train":
            n.softmax_out = L.Softmax(n.flat_output)
            n.loss = L.DiceLoss(n.softmax_out, n.flat_label)
        elif phase == "test":
            n.softmax_out = L.Softmax(n.flat_output)
            n.accu = L.DiceLoss(n.softmax_out, n.flat_label)
        else:  # deploy
            n.softmax_out = L.Softmax(n.flat_output)
            n.result = L.ArgMax(n.softmax_out, argmax_param=dict(axis=1))

        return n.to_proto()
コード例 #3
0
ファイル: chenyun_cls_2.py プロジェクト: zijundeng/Tianchi
    def define_model(self):
        n = caffe.NetSpec()
        pylayer = 'ClsDataLayer'

        pydata_params = dict(phase='train',
                             data_root=opt.cls_data_root,
                             batch_size=16,
                             ratio=5,
                             augument=True)
        n.data, n.pre_prob, _, n.label = L.Python(module='data.ClsDataLayer',
                                                  layer=pylayer,
                                                  ntop=4,
                                                  param_str=str(pydata_params))

        n.pre_conv = SingleConv(n.data,
                                64,
                                kernel_size=[3, 5, 5],
                                stride=[1, 1, 1],
                                padding=0)
        n.pre_conv = SingleConv(n.pre_conv,
                                128,
                                kernel_size=[3, 3, 3],
                                stride=[2, 2, 2],
                                padding=0)

        n.res2 = ResBlock(n.pre_conv, 128, z=True)
        n.res2 = ResBlock(n.res2, 128)

        n.conv = SingleConv(n.res2,
                            128,
                            kernel_size=[2, 3, 3],
                            stride=[1, 1, 1],
                            padding=[0, 0, 0])
        n.conv = SingleConv(n.conv,
                            128,
                            kernel_size=[3, 5, 5],
                            stride=[1, 1, 1],
                            padding=0)
        n.pool = L.Pooling(n.conv,
                           kernel_size=[6, 3, 3],
                           stride=1,
                           pad=0,
                           pool=P.Pooling.MAX)

        # n.features = L.Flatten(n.conv)
        # n.hidden = L.InnerProduct(n.features,num_output=256,weight_filler=dict(type='xavier'))
        n.cls = L.InnerProduct(n.pool,
                               num_output=2,
                               weight_filler=dict(type='xavier'))
        n.prob = L.Softmax(n.cls)
        n.bprob = L.Eltwise(n.prob,
                            n.pre_prob,
                            operation=1,
                            engine=3,
                            coeff=0.5)
        # n.bprob = L.Power(n.bprob,scale=0.5)
        n.loss = L.MultinomialLogisticLoss(n.bprob, n.label)

        with open(self.model_def, 'w') as f:
            f.write(str(n.to_proto()))
コード例 #4
0
def lenet(lmdb, batch_size):
    # our version of LeNet: a series of linear and simple nonlinear transformations
    n = caffe.NetSpec()
    n.data, n.label = L.Data(batch_size=batch_size,
                             backend=P.Data.LMDB,
                             source=lmdb,
                             transform_param=dict(scale=1. / 255),
                             ntop=2)
    n.conv1 = L.Convolution(n.data,
                            kernel_size=5,
                            num_output=20,
                            weight_filler=dict(type='xavier'))
    n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    n.conv2 = L.Convolution(n.pool1,
                            kernel_size=5,
                            num_output=50,
                            weight_filler=dict(type='xavier'))
    n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    n.ip1 = L.InnerProduct(n.pool2,
                           num_output=500,
                           weight_filler=dict(type='xavier'))
    n.relu1 = L.ReLU(n.ip1, in_place=True)
    n.ip2 = L.InnerProduct(n.relu1,
                           num_output=10,
                           weight_filler=dict(type='xavier'))
    n.loss = L.SoftmaxWithLoss(n.ip2, n.label)
    n.score = L.Softmax(n.ip2)
    return n.to_proto()
コード例 #5
0
def lenethdf5d():
    # our version of LeNet: a series of linear and simple nonlinear transformations
    n = caffe.NetSpec()
    #cambiano il primo e gli ultimi 2 strati
    n.data = L.Input(input_param=dict(shape=dict(dim=[1, 1, 128, 128])))

    # the base net
    n.conv1, n.relu1 = conv_relu(n.data, 32)
    n.pool1 = max_pool(n.relu1)
    #n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75)
    n.conv2, n.relu2 = conv_relu(n.pool1, 64)
    n.pool2 = max_pool(n.relu2)
    n.conv3, n.relu3 = conv_relu(n.pool2, 128)
    n.pool3 = max_pool(n.relu3)

    # fully convolutional
    n.fc1, n.rlfc1 = conv_relu(n.pool3, 512, ks=3, pad=1)

    n.decov5 = deconv(n.rlfc1, 128, pad=1)
    n.relu5, n.conv5 = relu_conv(n.decov5, 128, pad=0)
    n.decov6 = deconv(n.conv5, 64, pad=1)
    n.relu6, n.conv6 = relu_conv(n.decov6, 64, pad=0)
    n.decov7 = deconv(n.conv6, 32, pad=1)
    n.relu7, n.conv7 = relu_conv(n.decov7, 32, pad=0)

    n.relu8, n.conv8 = relu_conv(n.conv7, 2, pad=0)

    n.prob = L.Softmax(n.conv8)

    return n.to_proto()
コード例 #6
0
ファイル: boost.py プロジェクト: zijundeng/Tianchi
    def define_model(self):
        n = caffe.NetSpec()
        pylayer = 'ClsDataLayer'

        pydata_params = dict(phase='train', data_root=opt.cls_data_root,
                         batch_size=16,ratio=5,augument=True)
        n.data,n.pre_prob,_,n.label = L.Python(module='data.ClsDataLayer', layer=pylayer,
            ntop=4, param_str=str(pydata_params))

        n.pre_conv=SingleConv(n.data,32,kernel_size=3,stride=1,padding=1)
        
        n.res = ResDown(n.pre_conv,128)
        n.res = ResInception(n.res,128)
        n.res = ResDown(n.res,512)
        n.res = ResInception(n.res,512)
        n.res = ResInception(n.res,512)
        n.res = ResInception(n.res,512)

        n.cls=L.Pooling(n.res, kernel_size=8,stride=1,pad=0, pool=P.Pooling.AVE)
        n.cls=L.InnerProduct(n.cls, num_output=2,weight_filler=dict(type='xavier'))
        
        n.prob = L.Softmax(n.cls)
        n.bprob = L.Eltwise(n.prob,n.pre_prob,operation=1,engine=3,coeff=0.5)
        # n.bprob = L.Power(n.bprob,scale=0.5)
        n.loss= L.MultinomialLogisticLoss(n.bprob, n.label)

        with open(self.model_def, 'w') as f:
            f.write(str(n.to_proto()))
コード例 #7
0
ファイル: gen_deploy.py プロジェクト: taowucn/studio
def create_deploy():
    # No the first layer: data layer
    conv1 = L.Convolution(bottom='data',
                          kernel_size=5,
                          stride=1,
                          num_output=20,
                          pad=0,
                          weight_filler=dict(type='xavier'))
    pool1 = L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    conv2 = L.Convolution(pool1,
                          kernel_size=5,
                          stride=1,
                          num_output=50,
                          pad=0,
                          weight_filler=dict(type='xavier'))
    pool2 = L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    fc3 = L.InnerProduct(pool2,
                         num_output=500,
                         weight_filler=dict(type='xavier'))
    relu3 = L.ReLU(fc3, in_place=True)
    fc4 = L.InnerProduct(relu3,
                         num_output=10,
                         weight_filler=dict(type='xavier'))
    # No accuracy layer, but has softmax layer
    prob = L.Softmax(fc4)
    return to_proto(prob)
コード例 #8
0
ファイル: generate.py プロジェクト: ztalyx/demoCaffe
 def __create_output_layer(self, n, bottom, deploy):
     if deploy:
         n['prob'] = L.Softmax(bottom)
     else:
         n['score/loss'] = L.SoftmaxWithLoss(bottom, n.label)
         n['score/top-1'] = L.Accuracy(bottom, n.label, top_k=1)
         n['score/top-5'] = L.Accuracy(bottom, n.label, top_k=5)
コード例 #9
0
def create_deploy():
    n = caffe.NetSpec()
    # n.data, n.label = L.HDF5Data(batch_size = batch_size, source = hdf5, ntop = 2)
    # n.data, n.label = L.MemoryData(batch_size = batch_size, channels = 1, height = 28, width = 28, ntop = 2,
    #                         transform_param=dict(scale=1./255))
    # 直接少了data层,
    # n.data = L.Input(shape = 1)
    n.conv1 = L.Convolution(bottom='data',
                            kernel_size=5,
                            num_output=20,
                            weight_filler=dict(type='xavier'))
    n.pool1 = L.Pooling(n.conv1, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    n.conv2 = L.Convolution(n.pool1,
                            kernel_size=5,
                            num_output=50,
                            weight_filler=dict(type='xavier'))
    n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)

    n.fc1 = L.InnerProduct(n.pool2,
                           num_output=500,
                           weight_filler=dict(type='xavier'))
    n.relu1 = L.ReLU(n.fc1, in_place=True)
    n.score = L.InnerProduct(n.relu1,
                             num_output=10,
                             weight_filler=dict(type='xavier'))
    n.loss = L.Softmax(n.score)

    return n.to_proto()
コード例 #10
0
def create_deploy():
    #少了第一层,data层
    conv1 = L.Convolution(bottom='data',
                          kernel_size=5,
                          stride=1,
                          num_output=20,
                          pad=0,
                          weight_filler=dict(type='xavier'))
    pool1 = L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    conv2 = L.Convolution(pool1,
                          kernel_size=5,
                          stride=1,
                          num_output=50,
                          pad=0,
                          weight_filler=dict(type='xavier'))
    pool2 = L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    fc3 = L.InnerProduct(pool2,
                         num_output=500,
                         weight_filler=dict(type='xavier'))
    relu3 = L.ReLU(fc3, in_place=True)
    fc4 = L.InnerProduct(relu3,
                         num_output=10,
                         weight_filler=dict(type='xavier'))
    #最后没有accuracy层,但有一个Softmax层
    prob = L.Softmax(fc4)
    return to_proto(prob)
コード例 #11
0
def construc_net():
  net = caffe.NetSpec()

  net.data  = L.Input(input_param = dict(shape = dict(dim = [1,3,224,224])))

  block1    = _block_first(net, net.data)

  net.pool1 = L.Pooling(block1, pool = P.Pooling.MAX, kernel_size = 3, stride = 2)

  branch_2a = _branch('2a', net, net.pool1, 64, has_branch1 = True, is_branch_2a = True)
  branch_2b = _branch('2b', net, branch_2a, 64)
  branch_2c = _branch('2c', net, branch_2b, 64)

  branch_3a = _branch('3a', net, branch_2c, 128, has_branch1 = True)
  branch_pre = branch_3a
  for idx in xrange(1,8,1): # conv3_x total 1+7=8
    flag = '3b{}'.format(idx)
    branch_pre = _branch(flag, net, branch_pre, 128)

  branch_4a = _branch('4a', net, branch_pre, 256, has_branch1 = True)
  branch_pre = branch_4a
  for idx in xrange(1,36,1): # conv4_x total 1+35=36
    flag = '4b{}'.format(idx)
    branch_pre = _branch(flag, net, branch_pre, 256)

  branch_5a = _branch('5a', net, branch_pre, 512, has_branch1 = True)
  branch_5b = _branch('5b', net, branch_5a, 512)
  branch_5c = _branch('5c', net, branch_5b, 512)

  net.pool5 = L.Pooling(branch_5c, pool = P.Pooling.AVE, kernel_size = 7, stride = 1)
  
  net.fc6 = L.InnerProduct(net.pool5, num_output = 1000)
  net.prob = L.Softmax(net.fc6)

  return net.to_proto()
コード例 #12
0
def gen_train_proto_single(target_interface, shape=(28, 28, 3)):
    path = path_origin + '/train_' + target_interface + '.prototxt'
    n = caffe.NetSpec()

    n.data = L.DummyData(shape=dict(dim=[1, 3, shape[0], shape[1]]))
    if target_interface == 'conv1':
        n.conv1 = L.Convolution(n.data,
                                kernel_size=11,
                                stride=4,
                                num_output=1,
                                weight_filler={
                                    "type": "constant",
                                    "value": 1
                                })
    elif target_interface == 'pool1':
        n.pool1 = L.Pooling(n.data,
                            kernel_size=2,
                            stride=2,
                            pool=P.Pooling.MAX)
    elif target_interface == 'pool2':
        n.pool2 = L.Pooling(n.data,
                            kernel_size=2,
                            stride=2,
                            pool=P.Pooling.AVE)
    elif target_interface == 'relu1':
        n.relu1 = L.ReLU(n.data)
    elif target_interface == 'sigmoid1':
        n.sigmoid1 = L.Sigmoid(n.data)
    elif target_interface == 'softmax1':
        n.softmax1 = L.Softmax(n.data)
    elif target_interface == 'tanh1':
        n.tanh1 = L.TanH(n.data)

    save_proto(n.to_proto(), path)
    return path
コード例 #13
0
def test_softmax2():
    # type: ()->caffe.NetSpec

    n = caffe.NetSpec()
    n.input1 = L.Input(shape=make_shape([6, 4, 64, 64]))
    n.softmax1 = L.Softmax(n.input1, axis=2)
    return n
コード例 #14
0
    def assemble_net(self, lmdb, mean_file, proto_filename, batch_size, crop_size,img_channels, phase):
        layer_dict = {}
        n = caffe.NetSpec()

        if phase in ['Train', 'TRAIN','train']:
            dropout_flag = True
            loss_layer_flag = True
            prob_layer_flag = False
            acc_layer_flag = True

        if phase in ['test', 'TEST', 'Test']:
            dropout_flag = False
            loss_layer_flag = True
            prob_layer_flag = False
            acc_layer_flag = True

        if phase in ['deploy', 'DEPLOY', 'Deploy']:
            dropout_flag = False
            loss_layer_flag = False
            prob_layer_flag = True
            acc_layer_flag = False

        if phase in ['deploy', 'DEPLOY', 'Deploy']:
            n.data = L.Input(input_param= {'shape':{'dim':[1,img_channels,crop_size,crop_size]}})
        else:
            n.data, n.label = L.Data(transform_param={'crop_size': crop_size, 'mean_file':mean_file}, data_param={'source':lmdb,'batch_size':batch_size, 'backend': P.Data.LMDB},
                                        ntop=2)

        for i, layer_name in enumerate(self.topology):
            #print layer_name
            if 'conv' in layer_name:
                if i==0:
                    n.conv = self.conv2d(n.data, layer_name)
                else:
                    n.conv = self.conv2d(layer_dict[str(i-1)], layer_name)
                layer_dict[str(i)] = n.conv

            elif 'pool' in layer_name:
                pool_params = {'pool': 0, 'kernel_size': 3, 'stride': 2}
                n.pool = L.Pooling(layer_dict[str(i - 1)], pooling_param=pool_params, name=layer_name)
                layer_dict[str(i)] = n.pool

            elif 'fc' in layer_name or 'output' in layer_name:
                n.fc = self.fc(layer_dict[str(i - 1)], layer_name, dropout_flag)
                layer_dict[str(i)] = n.fc

            elif 'lrn' in layer_name:
                n.norm = L.LRN(layer_dict[str(i - 1)], local_size=5, alpha=1e-4, beta=0.75, name=layer_name)
                layer_dict[str(i)] = n.norm

            if loss_layer_flag:
                n.loss = L.SoftmaxWithLoss(layer_dict[str(i)], n.label, name='loss')
            if prob_layer_flag:
                n.prob = L.Softmax(layer_dict[str(i)], name='loss')
            if acc_layer_flag:
                n.accuracy = L.Accuracy(layer_dict[str(i)], n.label, name='accuracy')

        print layer_dict
	with open(proto_filename, 'w') as f:
                f.write(str(n.to_proto()))
def create_deploy():
    #少了第一层,data层
    conv1 = L.Convolution(bottom = "data", kernel_size=5, stride=1, num_output=20, pad=4, weight_filler=dict(type='xavier'))
    # 激活函数层
    relu1 = L.ReLU(conv1, in_place=True)
    # 卷积层
    conv2 = L.Convolution(relu1, kernel_size=5, stride=1, num_output=50, pad=4, weight_filler=dict(type='xavier'))
    # 激活函数层
    relu2 = L.ReLU(conv2, in_place=True)
    # 池化层
    pool1 = L.Pooling(relu2, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    # 第二层:卷积层
    conv3 = L.Convolution(pool1, kernel_size=5, stride=1, num_output=20, pad=4, weight_filler=dict(type='xavier'))
    # 激活函数层
    relu3 = L.ReLU(conv3, in_place=True)
    # 卷积层
    conv4 = L.Convolution(relu3, kernel_size=5, stride=1, num_output=50, pad=4, weight_filler=dict(type='xavier'))
    # 激活函数层
    relu4 = L.ReLU(conv4, in_place=True)
    # 全连接层
    fc3 = L.InnerProduct(relu4, num_output=500, weight_filler=dict(type='xavier'))
    # 激活函数层
    relu3 = L.ReLU(fc3, in_place=True)
    # 全连接层
    fc4 = L.InnerProduct(relu3, num_output=10, weight_filler=dict(type='xavier'))
    #最后没有accuracy层,但有一个Softmax层
    prob=L.Softmax(fc4)
    return to_proto(prob)
コード例 #16
0
def minivggnet(data,
               labels=None,
               train=False,
               cudnn=False,
               param=learned_param,
               num_classes=100,
               with_labels=True):
    """
    Returns a protobuf text file specifying a variant of VGG
    """
    n = caffe.NetSpec()
    n.data = data
    conv_kwargs = dict(param=param, train=train, cudnn=cudnn)
    n.conv1, n.relu1 = conv_relu(n.data, 7, 96, stride=2, **conv_kwargs)
    n.norm1 = L.LRN(n.relu1, local_size=5, alpha=0.0005, beta=0.75, k=2)
    n.pool1 = max_pool(n.norm1, 3, stride=3, train=train, cudnn=cudnn)
    n.conv2, n.relu2 = conv_relu(n.pool1,
                                 5,
                                 256,
                                 pad=1,
                                 stride=2,
                                 group=2,
                                 **conv_kwargs)
    n.pool2 = max_pool(n.relu2, 2, stride=2, train=train, cudnn=cudnn)
    n.conv3, n.relu3 = conv_relu(n.pool2, 3, 512, pad=1, **conv_kwargs)
    n.conv4, n.relu4 = conv_relu(n.relu3,
                                 3,
                                 512,
                                 pad=1,
                                 group=2,
                                 **conv_kwargs)
    n.conv5, n.relu5 = conv_relu(n.relu4,
                                 3,
                                 512,
                                 pad=1,
                                 group=2,
                                 **conv_kwargs)
    n.pool5 = max_pool(n.relu5, 3, stride=3, train=train, cudnn=cudnn)
    n.fc6, n.relu6 = fc_relu(n.pool5, 1024, param=param)
    n.drop6 = L.Dropout(n.relu6, in_place=True)
    n.fc7, n.relu7 = fc_relu(n.drop6, 1024, param=param)
    n.drop7 = L.Dropout(n.relu7, in_place=True)
    preds = n.fc8 = L.InnerProduct(n.drop7,
                                   num_output=num_classes,
                                   param=param)
    if not train:
        # Compute the per-label probabilities at test/inference time.
        preds = n.probs = L.Softmax(n.fc8)
    if with_labels:
        n.label = labels
        n.loss = L.SoftmaxWithLoss(n.fc8, n.label)
        n.accuracy_at_1 = L.Accuracy(preds, n.label)
        n.accuracy_at_5 = L.Accuracy(preds,
                                     n.label,
                                     accuracy_param=dict(top_k=5))
    else:
        n.ignored_label = labels
        n.silence_label = L.Silence(n.ignored_label, ntop=0)
    return to_tempfile(str(n.to_proto()))
コード例 #17
0
def caffenet(data,
             label=None,
             train=True,
             num_classes=1000,
             classifier_name='fc8',
             learn_all=False):
    """Returns a NetSpec specifying CaffeNet, following the original proto text
       specification (./models/bvlc_reference_caffenet/train_val.prototxt)."""
    n = caffe.NetSpec()
    n.data = data

    # frozen ?? #
    # 解释:如果只训练最后一层即全连接层,那么其他层的参数都不变也就是frozen
    param = learned_param if learn_all else frozen_param

    n.conv1, n.relu1 = conv_relu(n.data, 11, 96, stride=4, param=param)
    n.pool1 = max_pool(n.relu1, 3, stride=2)
    n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75)
    n.conv2, n.relu2 = conv_relu(n.norm1, 5, 256, pad=2, group=2, param=param)
    n.pool2 = max_pool(n.relu2, 3, stride=2)
    n.norm2 = L.LRN(n.pool2, local_size=5, alpha=1e-4, beta=0.75)
    n.conv3, n.relu3 = conv_relu(n.norm2, 3, 384, pad=1, param=param)
    n.conv4, n.relu4 = conv_relu(n.relu3, 3, 384, pad=1, group=2, param=param)
    n.conv5, n.relu5 = conv_relu(n.relu4, 3, 256, pad=1, group=2, param=param)
    n.pool5 = max_pool(n.relu5, 3, stride=2)
    n.fc6, n.relu6 = fc_relu(n.pool5, 4096, param=param)

    ##  这个if else 目的是什么? ##
    # 如果只预测不训练,params取多少都无所谓,因为不用反向传播,如果需要训练,那么就得分情况啦
    # 如果训练,则还要增加一个层:L.Dropout
    if train:
        n.drop6 = fc7input = L.Dropout(n.relu6, in_place=True)
    else:
        fc7input = n.relu6
    n.fc7, n.relu7 = fc_relu(fc7input, 4096, param=param)
    if train:
        n.drop7 = fc8input = L.Dropout(n.relu7, in_place=True)
    else:
        fc8input = n.relu7

    # always learn fc8 (param=learned_param)
    fc8 = L.InnerProduct(fc8input, num_output=num_classes, param=learned_param)
    # give fc8 the name specified by argument `classifier_name`
    n.__setattr__(classifier_name, fc8)

    # 对于只预测不训练,当然只是输出每种类型对应的可能结果啦
    if not train:
        n.probs = L.Softmax(fc8)

    # 对于要训练的,那么就是得输出训练集的损失函数和正确率啦
    if label is not None:
        n.label = label
        n.loss = L.SoftmaxWithLoss(fc8, n.label)
        n.acc = L.Accuracy(fc8, n.label)
    # write the net to a temporary file and return its filename
    with tempfile.NamedTemporaryFile(delete=False) as f:
        f.write(str(n.to_proto()))
        print(n.to_proto())
        return f.name
コード例 #18
0
def main():
    parser = argparse.ArgumentParser(
        description='Build a model for Edge Classification')
    parser.add_argument('output_prototxt',
                        type=str,
                        help='Output prototxt file')
    parser.add_argument('-n',
                        '--num-classes',
                        type=int,
                        default=5,
                        help='Number of output classes (default 5)')

    args = parser.parse_args()

    n = caffe.NetSpec()
    n['conv1'] = L.Convolution(bottom='data',
                               kernel_size=3,
                               stride=1,
                               pad=1,
                               num_output=16,
                               weight_filler=dict(type='msra'),
                               bias_term=False)
    n['bn1'] = L.BatchNorm(
        n['conv1'], param=[dict(lr_mult=0),
                           dict(lr_mult=0),
                           dict(lr_mult=0)])
    n['relu1'] = L.ReLU(n['bn1'], in_place=True)

    top = residual_block(n, n['relu1'], num_output=64, first_stride=1)
    top = residual_block(n, top, num_output=128)
    top = residual_block(n, top, num_output=256)

    n['conv_final'] = L.Convolution(top,
                                    kernel_size=3,
                                    stride=1,
                                    pad=1,
                                    num_output=args.num_classes,
                                    weight_filler=dict(type='msra'),
                                    bias_filler=dict(type='constant'))
    n['relu_final'] = L.ReLU(n['conv_final'], in_place=True)

    n['global_pool'] = L.Pooling(n['relu_final'],
                                 pool=P.Pooling.AVE,
                                 global_pooling=True)
    n['loss'] = L.SoftmaxWithLoss(
        bottom=['global_pool', 'label'],
        include=[dict(phase=caffe.TRAIN),
                 dict(phase=caffe.TEST, stage='val')],
        exclude=dict(phase=caffe.TEST, stage='deploy'))
    n['accuracy'] = L.Accuracy(bottom=['global_pool', 'label'],
                               include=dict(phase=caffe.TEST, stage='val'))
    n['prob'] = L.Softmax(bottom='global_pool',
                          include=dict(phase=caffe.TEST, stage='deploy'))

    net = n.to_proto()
    net.name = 'EdgeNet-Res'

    with open(args.output_prototxt, 'wb') as f:
        f.write(str(net))
コード例 #19
0
def vgg16_score(input_param = dict(shape=dict(dim=[1, 3, 224, 224]))):
  # setup the python data layer 
  net = caffe.NetSpec()
  net['data']       = L.Input(input_param = input_param)
  net, final        = vgg16_body(net, 'data', '', is_train=False)
  net['score']      = fc_relu(net[final],     nout=751, is_train=False, has_relu=False)
  net['prediction'] = L.Softmax(net['score'])
  return str(net.to_proto())
コード例 #20
0
def convLayerSoftmax(prev, bn=False, **kwargs):
    conv = L.Convolution(prev,
                         param=[dict(lr_mult=1),
                                dict(lr_mult=2)],
                         weight_filler=dict(type='msra'),
                         **kwargs)
    probs = L.Softmax(conv)
    return probs
コード例 #21
0
def MaskNet_Val_MTD(net,
                    from_layer="data",
                    label="label",
                    lr=1,
                    decay=1,
                    visualize=False):
    # net = YoloNetPart(net, from_layer=from_layer, use_bn=True, use_layers=6, use_sub_layers=7, lr=0, decay=0)
    net, mbox_layers, parts_layers = MTD_BODY(net)
    net.bbox, net.parts = L.SplitLabel(net[label],
                                       name="SplitLabel",
                                       ntop=2,
                                       split_label_param=dict(add_parts=True))
    # ConvBNUnitLayer(net,"conv2", "conv2_pool", use_bn=True, use_relu=True, num_output=64,
    #            kernel_size=1, pad=0, stride=2)

    # net = UnifiedMultiScaleLayers(net,layers=["conv2_pool","conv3_3","conv4_3"],tags=["Down","Down","Ref"],unifiedlayer="featuremap1",dnsampleMethod=[["Conv"],["MaxPool"]],dnsampleChannels=64)
    # net = UnifiedMultiScaleLayers(net,layers=["conv4_3","conv5_5"],tags=["Down","Ref"],unifiedlayer="featuremap2",dnsampleMethod=[["MaxPool"]])
    # net = UnifiedMultiScaleLayers(net,layers=["conv5_5","conv6_7"],tags=["Down","Ref"],unifiedlayer="featuremap3",dnsampleMethod=[["MaxPool"]],pad=True)
    # mbox_layers = SSDHeader(net,data_layer="data",from_layers=["featuremap1","featuremap2","featuremap3"],input_height=Input_Height,input_width=Input_Width,loc_postfix='det',**ssdparam)
    reshape_name = "mbox_conf_reshape"
    net[reshape_name] = L.Reshape(mbox_layers[1], \
     shape=dict(dim=[0, -1, ssdparam.get("num_classes",2)]))
    softmax_name = "mbox_conf_softmax"
    net[softmax_name] = L.Softmax(net[reshape_name], axis=2)
    flatten_name = "mbox_conf_flatten"
    net[flatten_name] = L.Flatten(net[softmax_name], axis=1)
    mbox_layers[1] = net[flatten_name]
    net.detection_out = L.DenseDetOut(
        *mbox_layers,
        detection_output_param=det_out_param,
        include=dict(phase=caffe_pb2.Phase.Value('TEST')))
    net.detection_eval_accu = L.DetEval(
        net.detection_out,
        net.bbox,
        detection_evaluate_param=det_eval_param,
        include=dict(phase=caffe_pb2.Phase.Value('TEST')))
    # net = UnifiedMultiScaleLayers(net,layers=["conv2","conv3_3"],tags=["Down","Ref"],unifiedlayer="conf23",dnsampleMethod=[["Conv"]],dnsampleChannels=64)
    # net = UnifiedMultiScaleLayers(net,layers=["conf23","conv4_3"],tags=["Down","Ref"],unifiedlayer="conf34",dnsampleMethod=[["MaxPool"]])
    # net = UnifiedMultiScaleLayers(net,layers=["conv3_3","conv4_3"],tags=["Down","Ref"],unifiedlayer="conf34",dnsampleMethod=[["MaxPool"]])
    # net = UnifiedMultiScaleLayers(net,layers=["conf34","conv5_5"],tags=["Down","Ref"],unifiedlayer="conf45",dnsampleMethod=[["MaxPool"]])
    # # net = UnifiedMultiScaleLayers(net,layers=["conf45","conv6_7"],tags=["Down","Ref"],unifiedlayer="conf56",dnsampleMethod=[["MaxPool"]],pad=True)
    # parts_layers = SSDHeader(net,data_layer="data",from_layers=["conf34","conf45","conv6_7"],input_height=Input_Height,input_width=Input_Width,loc_postfix='parts',**partsparam)

    # parts_layers = SSDHeader(net,data_layer="data",from_layers=["conf23","conf34","conf45","conf56"],input_height=Input_Height,input_width=Input_Width,loc_postfix='parts',**partsparam)
    sigmoid_name = "parts_conf_sigmoid"
    net[sigmoid_name] = L.Sigmoid(parts_layers[1])
    parts_layers[1] = net[sigmoid_name]
    net.parts_out = L.DenseDetOut(
        *parts_layers,
        detection_output_param=parts_out_param,
        include=dict(phase=caffe_pb2.Phase.Value('TEST')))
    net.parts_eval_accu = L.DetEval(
        net.parts_out,
        net.parts,
        detection_evaluate_param=parts_eval_param,
        include=dict(phase=caffe_pb2.Phase.Value('TEST')))
    # net.out=L.Concat(net.detection_eval_accu,net.parts_eval_accu,axis=2)
    return net
コード例 #22
0
def convert_configuration(config, train=False, loc_layer=False):
    """ given a list of YOLO layers as dictionaries, convert them to Caffe """
    layers = []
    conv_layers = []
    count = 0
    reorg_count = 0
    concat_count = 0
    region_count = 0
    conv_count = 0

    for section, params in config:
        if   section == "net":
            input_params = params
            layers.append(data_layer("data", input_params, train))
        elif section == "crop":
            if train:    # update data layer with crop parameters
                input_params.update(params)
                layers[-1] = data_layer("data", input_params, train)
        elif section == "convolutional":
            conv_count += 1
            count += 1
            add_convolutional_layer(layers, count, params, train)
            conv_layers.append(layers[-1])
        elif section == "maxpool":
            layers.append(max_pooling_layer(layers[-1], "pool{0}".format(count),
                                            params))
        elif section == "avgpool":
            layers.append(global_pooling_layer(layers[-1], "pool{0}".format(count)))
        elif section == "softmax":
            layers.append(cl.Softmax(layers[-1], name="softmax{0}".format(count)))
        elif section == "connected":
            count += 1
            add_dense_layer(layers, count, params, train)
        elif section == "route":
            concat_count += 1
            layers.append(cl.Concat(conv_layers[-8], name="concat{0}".format(concat_count)))
        elif section == "reorg":
            reorg_count += 1
            add_reorg_layer(layers, reorg_count, params, train)
        elif section == "region":
            region_count += 1
            add_regionloss_layer(layers, reorg_count, params, train)
        elif section == "dropout":
            if train:
                layers.append(cl.Dropout(layers[-1], name="drop{0}".format(count),
                                         dropout_ratio=float(params["probability"])))
        elif section == "local" and loc_layer:
            count += 1
            add_local_layer(layers, count, params, train)
        else:
            print("WARNING: {0} layer not recognized".format(section))

    model = caffe.NetSpec()
    for layer in layers:
        setattr(model, layer.fn.params["name"], layer)
    model.result = layers[-1]       

    return model
コード例 #23
0
    def construct_model(self,
                        net_cfg,
                        lmdb,
                        batch_size=256,
                        train_val=False,
                        deploy=False):
        net = caffe.NetSpec()
        if train_val:
            net.data, net.label = L.Data(name='data',
                                         source=lmdb,
                                         backend=P.Data.LMDB,
                                         batch_size=batch_size,
                                         ntop=2,
                                         include=dict(phase=caffe.TRAIN))

        elif deploy:
            net.data = L.Input(
                name='data',
                ntop=1,
                shape=dict(dim=[
                    batch_size, self.img_channels, self.img_rows, self.img_rows
                ]),
                include=dict(phase=caffe.TEST))

        # 7x7 convolution followed by 3x3 max pooling
        net.conv1 = conv_bn_scale_relu(net.data, 7, 64, stride=2, pad=3)
        net.pool1 = L.Pooling(net.conv1,
                              pool=P.Pooling.MAX,
                              kernel_size=2,
                              stride=2)

        # Build the denseblock
        _out = net.pool1
        dense_block_id = 0
        for i, item in enumerate(net_cfg):
            layer_type = item['Type']
            if layer_type == 'DenseBlock':
                _out = dense_block(net, _out, item['KernelNum'],
                                   dense_block_id, item['LayerNum'])
                dense_block_id += 1
            elif layer_type == 'Transition':
                _out = transition_layer(_out, item['KernelNum'])
            elif layer_type == 'Classification':
                _out = classfication_layer(_out, item['OutputNum'])
            else:
                raise ValueError, 'layer_type not supported' + item['Type']

        #Connect full connected network to softmax
        net.fc = _out
        if train_val:
            net.loss = L.SoftmaxWithLoss(net.fc, net.label)
            net.acc = L.Accuracy(net.fc,
                                 net.label,
                                 include=dict(phase=caffe.TEST))
        if deploy:
            net.prob = L.Softmax(net.fc, name='prob')

        return net.to_proto()
コード例 #24
0
def densenet(net_cfg=None,
             data_cfg=None,
             batch_size=None,
             mode='train',
             datafile=None):
    if mode == 'deploy':
        data = L.Input(name='data',
                       ntop=1,
                       shape=dict(dim=data_cfg['imgsize']))
    else:
        data, label = L.Data(name='data',
                             source=datafile,
                             backend=P.Data.LMDB,
                             batch_size=batch_size,
                             ntop=2,
                             transform_param=dict(mirror=True,
                                                  crop_size=32,
                                                  mean_value=[0, 0, 0],
                                                  scale=1))

    if data_cfg['Dataset'] == 'IMAGENET':
        # 7x7 convolution followed by 3x3 max pooling
        conv1 = conv_bn_scale_relu(data, 7, 64, stride=2, pad=3)
        pool1 = L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=3, stride=2)
        _out = pool1

    if data_cfg['Dataset'] == 'CIFAR':
        # 3x3 convolution
        # conv1 = L.Convolution(data, kernel_size=3, stride=1, num_output=nchannels,
        #                     pad=1, bias_term=False, weight_filler=dict(type='msra'), bias_filler=dict(type='constant'))
        conv1 = conv_bn_scale_relu(data, 3, 24, stride=1, pad=1)
        _out = conv1

    # Build the denseblock
    dense_block_id = 0
    for i, item in enumerate(net_cfg):
        layer_type = item['Type']
        if layer_type == 'DenseBlock':
            _out = dense_block(_out, item['conv1Num'], item['conv3Num'],
                               dense_block_id, item['layerNum'],
                               item['dropout'])
            dense_block_id += 1
        elif layer_type == 'Transition':
            _out = transition_layer(_out, item['conv1Num'])
        elif layer_type == 'Classification':
            _out = classfication_layer(_out, item['OutputNum'])
        else:
            raise ValueError, 'layer_type not supported' + item['Type']

    #Connect full connected network to softmax
    fc = _out
    if mode == 'deploy':
        prob = L.Softmax(fc, name='prob')
        return to_proto(prob)
    else:
        loss = L.SoftmaxWithLoss(fc, label)
        acc = L.Accuracy(fc, label)
        return to_proto(loss, acc)
コード例 #25
0
def add_classify_header(net, classes=120):
  bottom = net.tops.keys()[-1]

  net.global_pool = L.Pooling(net[bottom], pool=P.Pooling.AVE, global_pooling=True) 

  net.classifier = L.InnerProduct(net.global_pool, num_output=classes, bias_term=True, weight_filler=dict(type='xavier'), bias_filler=dict(type='constant'))

  net.prob = L.Softmax(net.classifier)
  return net
コード例 #26
0
def make_deploy_net(config):
    configuration = get_configuration(config)
    variant = configuration.get('variant', 'v0')
    num_hidden_layers = len(configuration['layer_dims']) - 2
    layers_dims = configuration['layer_dims']

    ns = caffe.NetSpec()
    ns.input_hot_vecotr = L.Input(name='input_hot_vecotr', ntop=1, input_param={'shape': {'dim': [1, SEQ_GLOBALS.DIM]}})

    # # hiddens
    # rec_param = {'debug': False,
    #              'output_shapes': [(1, h_) for h_ in layers_dims[1:-1]]}
    # hiddens = L.Python(name='recurrence_handler', ntop=num_hidden_layers,
    #                    python_param={'module': 'r2d2_text',
    #                                  'layer': 'recurrence_handler',
    #                                  'param_str': json.dumps(rec_param)})
    # input hiddens
    hidden_in = []
    for li in xrange(num_hidden_layers):
        hin = L.Parameter(name='h{}_t0'.format(li),
                          parameter_param={'shape':{'dim':[1, layers_dims[li+1]]}},
                          param={'lr_mult': 0, 'decay_mult': 0, 'name': 'h{}_share'.format(li)})
        ns.__setattr__('h{}_t0'.format(li), hin)
        hidden_in.append(('h{}_t0'.format(li), hin))

    if variant == 'v0':
        ns, outputs = one_time_step_v0(ns, ns.input_hot_vecotr, hidden_in, layers_dims,
                                       prefix_='v0_t0', phase=caffe.TEST)
    elif variant == 'v1':
        ns, outputs = one_time_step_v1(ns, ns.input_hot_vecotr, hidden_in, layers_dims,
                                       prefix_='v1_t0', phase=caffe.TEST)
    elif variant == 'v2':
        ns, outputs = one_time_step_v2(ns, ns.input_hot_vecotr, hidden_in, layers_dims,
                                       prefix_='v2_t0', phase=caffe.TEST)
    elif variant in ('vnl', 'vanilla', 'rnn'):
        ns, outputs = one_time_step_vanilla(ns, ns.input_hot_vecotr, hidden_in, layers_dims,
                                            prefix_='vnl_t0', phase=caffe.TEST)
    else:
        raise Exception('unknown variant {}'.format(variant))

    # take care of hiddens of last time step...
    param = []
    inputs = []
    for li in xrange(num_hidden_layers):
        param.append({'lr_mult': 0, 'decay_mult': 0, 'name': 'h{}_share'.format(li)})
        inputs.append(outputs[li][1])
    ns.keeing_hidden_states = L.Python(*inputs, name='keeing_hidden_states', ntop=0,
                                       python_param={'module': 'r2d2_text', 'layer': 'parameter_in'},
                                       param=param)
    # add SoftmaxLayer
    ns.prob = L.Softmax(outputs[-1][1], name='prob', softmax_param={'axis': -1})

    deploy_file_name = os.path.join(configuration['base_dir'], config + '_deploy.prototxt')
    with open(deploy_file_name, 'w') as W:
        W.write(str(ns.to_proto()))
    net = caffe.Net(deploy_file_name, caffe.TEST)
    return net
コード例 #27
0
ファイル: create_net.py プロジェクト: lemene/misc
def create_deploy_net():
    '''创建部署模型'''

    # 建立缩写
    from caffe import layers as L, params as P 

    n = caffe.NetSpec()
    n.data = L.Input(input_param=dict(shape=dict(dim=[1, 1, 28, 28])))
    n = create_net_body(n)
    n.prob  = L.Softmax(n.ip2)

    return n
コード例 #28
0
def tail(ns, index_width=1):
    pool_params = dict(pool=pb2.PoolingParameter.AVE, global_pooling=True)
    ns['avgpool'] = L.Pooling(ns[ns.keys()[-1]], **pool_params)
    conv10_params = dict(kernel_size=1,
                         num_output=1000,
                         weight_filler=default_weight_filler,
                         bias_filler=dict(type='constant', value=0),
                         param=dict(lr_mult=2, decay_mult=0))
    conv_index = get_conv_index(ns) + 1
    ns[f'conv{conv_index:0{index_width}d}'] = L.Convolution(
        ns[ns.keys()[-1]], **conv10_params)
    ns['prob'] = L.Softmax(ns[ns.keys()[-1]])
コード例 #29
0
ファイル: layers.py プロジェクト: zjz5250/py-mask-rcnn
def roi_proposals(net,
                  rpn_cls_score_reshape,
                  rpn_bbox_pred,
                  im_info,
                  anchors,
                  feat_stride,
                  scales,
                  classes,
                  out_w,
                  deploy=False):
    net["rpn_cls_prob"] = L.Softmax(rpn_cls_score_reshape, name="rpn_cls_prob")
    net["rpn_cls_prob_reshape"] = L.Reshape(net["rpn_cls_prob"], name="rpn_cls_prob_reshape", \
                                                 reshape_param={"shape": {"dim": [0, 2 * anchors, -1, 0]}})

    if not deploy:
        net["rpn_rois"] = L.Python(
            net["rpn_cls_prob_reshape"],
            rpn_bbox_pred,
            im_info,
            name='proposal',
            python_param=dict(module='rpn.proposal_layer',
                              layer='ProposalLayer',
                              param_str='{"feat_stride": %s,"scales": %s}' %
                              (feat_stride, scales)),
            # param_str='"feat_stride": %s \n "scales": !!python/tuple %s ' %(feat_stride, scales)),
            ntop=1,
        )
        net["rois"], net["labels"], net["bbox_targets"], net["bbox_inside_weights"], net[
            "bbox_outside_weights"] \
            , net["mask_rois"], net["masks"] = \
            L.Python(net["rpn_rois"], net["gt_boxes"], net["ins"],
                     name='roi-data',
                     python_param=dict(
                         module='rpn.proposal_target_layer',
                         layer='ProposalTargetLayer',
                         param_str='{"num_classes": %s,"out_size": %s}' % (classes, out_w)),
                     ntop=7, )
        return net["rois"], net["labels"], net["bbox_targets"], net["bbox_inside_weights"], \
               net["bbox_outside_weights"], net["mask_rois"], net["masks"]
    else:
        net["rois"], net["scores"] = L.Python(
            net["rpn_cls_prob_reshape"],
            rpn_bbox_pred,
            im_info,
            name='proposal',
            python_param=dict(module='rpn.proposal_layer',
                              layer='ProposalLayer',
                              param_str='{"feat_stride": %s,"scales": %s}' %
                              (feat_stride, scales)),
            # param_str='"feat_stride": %s \n "scales": !!python/tuple %s ' %(feat_stride, scales)),
            ntop=2,
        )
        return net["rois"], net["scores"]
コード例 #30
0
ファイル: solver.py プロジェクト: h2oai/deepwater-nae
    def net_def(self, phase):
        print('sizes', self.cmd.sizes, file = sys.stderr)
        print('types', self.cmd.types, file = sys.stderr)
        if len(self.cmd.sizes) != len(self.cmd.types):
            raise Exception

        n = caffe.NetSpec()
        name = ''

        for i in range(len(self.cmd.types)):
            if self.cmd.types[i] == 'data':
                name = 'data'
                if phase == caffe.TRAIN:
                    n[name], n.label = L.Python(
                        module = 'solver',
                        layer = 'DataLayer',
                        ntop = 2,
                    )
                else:
                    n[name] = L.Python(
                        module = 'solver',
                        layer = 'DataLayer',
                    )

            else:
                fc = L.InnerProduct(
                    n[name],
                    inner_product_param = {'num_output': self.cmd.sizes[i],
                                           'weight_filler': {'type': 'xavier',
                                                             'std': 0.1},
                                           'bias_filler': {'type': 'constant',
                                                           'value': 0}})
                name = 'fc%d' % i
                n[name] = fc

                if self.cmd.types[i] == 'relu':
                    relu = L.ReLU(n[name], in_place = True)
                    name = 'relu%d' % i
                    n[name] = relu
                elif self.cmd.types[i] == 'loss':
                    if self.cmd.regression:
                        if phase == caffe.TRAIN:
                            n.loss = L.EuclideanLoss(n[name], n.label)
                    else:
                        if phase == caffe.TRAIN:
                            n.loss = L.SoftmaxWithLoss(n[name], n.label)
                        else:
                            n.output = L.Softmax(n[name])
                else:
                    raise Exception('TODO unsupported: ' + self.cmd.types[i])

        return n.to_proto()