1

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?

Shai
  • 111,146
  • 38
  • 238
  • 371
ID.W
  • 58
  • 8

2 Answers2

2

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.

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    Thank you very much! It works! But, actually, that is `len(net.params[layer_name]) >= 2`. – ID.W Oct 11 '17 at 07:22
0

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'
Shai
  • 111,146
  • 38
  • 238
  • 371
rkellerm
  • 5,362
  • 8
  • 58
  • 95