利用pycaffe提取caffe model中的参数

实验平台: ubuntu 14.04, python2.7, caffe当前最新版

有时候我们需要查看、修改caffe的网络参数,苦于caffe本身是c++编写,如果想从c++中提取,明显这个太困难。我们使用python来实现这个,每一步都可以查看参数,所以就容易很多。

import sys
import os

# Add caffe package
caffe_python_dir = "your/caffe-master/python"
sys.path.append(caffe_python_dir)
if not os.path.exists(caffe_python_dir):
    print "python caffe not found."
    exit(-1)
import caffe

import numpy as np
from PIL import Image

# load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe
im = Image.open('your/2007_000129.jpg')
in_ = np.array(im, dtype=np.float32)
in_ = in_[:,:,::-1]
in_ -= np.array((104.00698793,116.66876762,122.67891434))
in_ = in_.transpose((2,0,1))

# load net
net = caffe.Net('your/deploy.prototxt',
                'your.caffemodel', caffe.TEST)
# shape for input (data blob is N x C x H x W), set data
net.blobs['data'].reshape(1, *in_.shape)
net.blobs['data'].data[...] = in_
# run net and take argmax for prediction
net.forward()
out = net.blobs['score'].data[0].argmax(axis=0)
print net
print('net.params: {0}'.format(net.params))
t = net.params["conv1_1"]  #这个就是net.params字典里面的一个层,"conv1_1"是层的名字。
print("conv1_1 length: {0}".format(len(t))) #看看这个有多少个Blob,这里输出的是2
t0 = t[0] #这个就是卷积核weight的参数
t1 = t[1] #这个就是bias
print(t1)

查看其他层的方法也是类似的。
那么问题来了,learning rate等参数在哪儿呢?

你可能感兴趣的:(caffe)