2017-09-07 74 views
1

我想從Python中的.prototxt中定義的caffe網絡讀出網絡參數,因爲layer_dict中的圖層對象只告訴我它是一個「卷積」層,但不包括在.prototxt文件中明確定義的諸如kernel_sizestrides等。從Python中的caffe .prototxt模型定義讀取網絡參數

所以可以說我有一個model.prototxt像這樣:

name: "Model" 
layer { 
    name: "data" 
    type: "Input" 
    top: "data" 
    input_param { 
    shape: { 
     dim: 64 
     dim: 1 
     dim: 28 
     dim: 28 
    } 
    } 
} 
layer { 
    name: "conv2d_1" 
    type: "Convolution" 
    bottom: "data" 
    top: "conv2d_1" 
    convolution_param { 
    num_output: 32 
    kernel_size: 3 
    stride: 1 
    weight_filler { 
     type: "gaussian" # initialize the filters from a Gaussian 
     std: 0.01  # distribution with stdev 0.01 (default mean: 0) 
    } 
    bias_filler { 
     type: "constant" 
     value: 0 
    } 
    } 
} 

layer { 
    name: "dense_1" 
    type: "InnerProduct" 
    bottom: "conv2d_1" 
    top: "out" 
    inner_product_param { 
    num_output: 1024 
    weight_filler { 
     type: "gaussian" 
     std: 0.01 
    } 
    bias_filler { 
     type: "constant" 
     value: 0 
    } 
    } 
} 

我發現,一個可以分析模型,如下所示:

from caffe.proto import caffe_pb2 
import google.protobuf.text_format 
net = caffe_pb2.NetParameter() 
f = open('model.prototxt', 'r') 
net = google.protobuf.text_format.Merge(str(f.read()), net) 
f.close() 

,但我不知道怎麼去田野從protobuf消息中傳出結果對象。

回答

1

你可以通過遍歷層,詢問他們的相應PARAM,例如:

for i in range(0, len(net.layer)): 
    if net.layer[i].type == 'Convolution': 
     net.layer[i].convolution_param.bias_term = True # bias term, for example 

適當* _Param類型可以在caffe.proto中找到,例如:

optional ConvolutionParameter convolution_param = 106