利用pycaffe提取caffe model中的参数

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)

你可能感兴趣的:(利用pycaffe提取caffe model中的参数)