4

나는 caffe에서 교육 및 유효성 검사 .prototxt 파일을위한 길쌈 신경망 (CNN)을 프로그래밍 방식으로 생성하는 Python 코드를 작성했습니다.프로그래밍 방식으로 파이썬에서 caffe 용 deploy.txt를 생성하는 방법

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))) 

lmdb 파일에없는 유사 보이지 않는 데이터에 대한 테스트를위한 deploy.prototxt을 생성하는 방법은 무엇입니까 : 다음은 내 기능은 무엇입니까? 그렇다면 누군가가 나를 참조 할 수 있으면 정말 감사 할 것입니다.

답변

4

아주 간단 : 당신이 볼 수 있듯이 (lmdbNone되는 조건으로)에 prototxt에 two modifications이 있습니다

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

:
첫 번째를 대신

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() 

이제 함수를 호출 "Data" 계층의 경우 "data""label"만을 선언하는 선언문 "Input" layer이 있습니다.
두 번째 변경 사항은 출력 레이어입니다. 손실 레이어 대신 예측 레이어가 있습니다 (예 : this answer 참조).

+0

고마워요. 나는 어제 같은 해결책에 도착했다 :). caffe의 문서는 너무 가난합니다. Input 레이어와 각 레이어에 지정할 수있는 매개 변수를 찾기 위해 파고 들었습니다. – cdeepakroy

+0

[this answer] (http://stackoverflow.com/a/35967589/1714410)에서 파이썬 함수를 사용하여 caffe net 블록을 생성하는 방법을 살펴 보겠습니다. – Shai

+1

@Shai'n.prob = L.SoftMax (n.score)'라인은 Softmax의 _m_으로 작성해야합니다 :'n.prob = L.Softmax (n.score)'? – calocedrus