Skip to content Skip to sidebar Skip to footer

How Can I Know Whether "bias" Exists In A Layer?

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

Solution 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.


Solution 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'

Post a Comment for "How Can I Know Whether "bias" Exists In A Layer?"