【问题标题】:How to programmatically generate deploy.txt for caffe in python如何在python中以编程方式为caffe生成deploy.txt
【发布时间】:2017-04-20 12:57:34
【问题描述】:

我编写了 python 代码以编程方式生成卷积神经网络 (CNN),用于在 caffe 中训练和验证 .prototxt 文件。以下是我的功能:

def custom_net(lmdb, batch_size):

    # define your own net!
    n = caffe.NetSpec()

    # keep this data layer for all networks
    n.data, n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=lmdb,
                             ntop=2, transform_param=dict(scale=1. / 255))

    n.conv1 = L.Convolution(n.data, kernel_size=6,
                            num_output=48, 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=48, weight_filler=dict(type='xavier'))
    n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)

    n.conv3 = L.Convolution(n.pool2, kernel_size=4,
                            num_output=48, weight_filler=dict(type='xavier'))
    n.pool3 = L.Pooling(n.conv3, kernel_size=2, stride=2, pool=P.Pooling.MAX)

    n.conv4 = L.Convolution(n.pool3, kernel_size=2,
                            num_output=48, weight_filler=dict(type='xavier'))
    n.pool4 = L.Pooling(n.conv4, kernel_size=2, stride=2, pool=P.Pooling.MAX)

    n.fc1 = L.InnerProduct(n.pool4, num_output=50,
                           weight_filler=dict(type='xavier'))

    n.drop1 = L.Dropout(n.fc1, dropout_param=dict(dropout_ratio=0.5))

    n.score = L.InnerProduct(n.drop1, num_output=2,
                             weight_filler=dict(type='xavier'))

    # keep this loss layer for all networks
    n.loss = L.SoftmaxWithLoss(n.score, n.label)

    return n.to_proto()

with open('net_train.prototxt', 'w') as f:
    f.write(str(custom_net(train_lmdb_path, train_batch_size)))

with open('net_test.prototxt', 'w') as f:
    f.write(str(custom_net(test_lmdb_path, test_batch_size)))

有没有办法类似地生成 deploy.prototxt 以测试不在 lmdb 文件中的看不见的数据?如果是这样,如果有人能指出我的参考资料,我将不胜感激。

【问题讨论】:

    标签: python neural-network deep-learning caffe conv-neural-network


    【解决方案1】:

    很简单:

    from caffe import layers as L, params as P
    def custom_net(lmdb, batch_size):
        # define your own net!
        n = caffe.NetSpec()
    
        if lmdb is None: # "deploy" flavor
            # assuming your data is of shape 3x224x224
            n.data = L.Input(input_param={'shape':{'dim':[1,3,224,224]}})
        else:
            # keep this data layer for all networks
            n.data, n.label = L.Data(batch_size=batch_size, backend=P.Data.LMDB, source=lmdb,
                             ntop=2, transform_param=dict(scale=1. / 255))
        # the other layers common to all flavors: train/val/deploy...
        n.conv1 = L.Convolution(n.data, kernel_size=6,
                            num_output=48, 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=48, weight_filler=dict(type='xavier'))
        n.pool2 = L.Pooling(n.conv2, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    
        n.conv3 = L.Convolution(n.pool2, kernel_size=4,
                            num_output=48, weight_filler=dict(type='xavier'))
        n.pool3 = L.Pooling(n.conv3, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    
        n.conv4 = L.Convolution(n.pool3, kernel_size=2,
                            num_output=48, weight_filler=dict(type='xavier'))
        n.pool4 = L.Pooling(n.conv4, kernel_size=2, stride=2, pool=P.Pooling.MAX)
    
        n.fc1 = L.InnerProduct(n.pool4, num_output=50,
                           weight_filler=dict(type='xavier'))
        # do you "drop" i deploy as well? up to you to decide...
        n.drop1 = L.Dropout(n.fc1, dropout_param=dict(dropout_ratio=0.5))
        n.score = L.InnerProduct(n.drop1, num_output=2,
                             weight_filler=dict(type='xavier'))
    
        if lmdb is None:
            n.prob = L.Softmax(n.score)
        else:
            # keep this loss layer for all networks apart from "Deploy"
            n.loss = L.SoftmaxWithLoss(n.score, n.label)
    
        return n.to_proto()
    

    现在调用函数:

    with open('net_deploy.prototxt', 'w') as f:
        f.write(str(custom_net(None, None)))
    

    如您所见,prototxt 中有two modifications(条件是lmdbNone):
    第一个,而不是"Data" 层,您有声明性"Input" layer 仅声明"data" 而没有"label"
    第二个变化是输出层:你有一个预测层,而不是损失层(参见,例如,this answer)。

    【讨论】:

    • 非常感谢。我昨天得到了同样的解决方案:)。 caffe 的文档太差了。我花了一些时间挖掘和破译才能找到输入层和我可以给每一层的参数。
    • this answer你会看到如何使用python函数来生成caffe net的块。
    • @Shai 行 n.prob = L.SoftMax(n.score) 是否应该为 Softmax 写一个 mn.prob = L.Softmax(n.score) ?
    • @calocedrus 刚刚修正了这个错字。 ;)
    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2013-10-18
    • 2015-04-10
    • 1970-01-01
    • 2011-04-27
    相关资源
    最近更新 更多