问题
I'm trying to read weight and bias in a caffe network with pycaffe. Here is my code
weight = net.params[layer_name][0].data
bias = net.params[layer_name][1].data
But, some layers in my network has no bias, so that there will be an error which is Index out of range
.
So my question is can I use
if(net.params[layer_name][1] exists):
bias = net.params[layer_name][1].data
to control the assignments to bias
?
And how to write the code?
回答1:
You can simply iterate over net.params[layer_name]
:
layer_params = [blob.data for blob in net.params[layer_name]]
This way, you get all layer_params
(which might be more than 2 for some layers, e.g., "BatchNorm")
If you only want to check for the second parameters blob, you can use len
:
if len(net.params[layer_name]) >= 2:
bias = net.params[layer_name][1].data
PS,
It might be the case that net.params[layer_name]
is not exactly a python list
, but rather some python boost wrapper object, thus you might need to explicitly cast it to list (list(net.params[layer_name])
) in some of the methods I suggested in this answer.
回答2:
In case you want to do it for convolution layers, you can find whether the layer has bias through reading the prototxt without the need for the caffemodel, i.e.
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()
for i in range(0, len(net.layer)):
if net.layer[i].type == 'Convolution':
if net.layer[i].convolution_param.bias_term == True:
print 'layer has bias'
来源:https://stackoverflow.com/questions/46663424/how-can-i-know-whether-bias-exists-in-a-layer