import caffe
if __name__ == "__main__":
#文件的存放路径
root = '/home/xhq11/caffe-master/examples/mnist/'
caffe.set_mode_cpu
net = caffe.Net(root+'lenet.prototxt',\
root+'lenet_iter_10000.caffemodel',caffe.TEST')
#在这部分做任何你希望的对权值的修改
net.save('/path of your new caffemodel/newname.caffemodel')
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
但是这种方法的弊端也很明显,这种方法只能在原有的结构上进行权值的修改,而不能对原有结构进行修改,比如,删除原有结构中的某一层或增加新的层,或更改原有层的维度等。 在caffe 的官方文档中提供了一种修改caffemodel文件的方法,具体参考http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb(需翻墙)。这里面的案例是讲caffenet的后三个全连接层(fc6/fc7/fc8)改成全卷基层(fc6-conv/fc7-conv/fc8-conv)以形成新的网络权值文件bvlc_caffenet_full_conv.caffemodel。值得注意的是,这里面的案例也仅仅是将原来后三个全连接层的权值“摊平”(文中的写法为flat)并赋给卷积层,由于全连接层和卷基层的参数个数是相同的,因此这个案例本质上也属于权值的进一步修改。文中最后有这么一段话: Note that this model isn’t totally appropriate for sliding-window detection since it was trained for whole-image classification. Nevertheless it can work just fine. Sliding-window training and finetuning can be done by defining a sliding-window ground truth and loss such that a loss map is made for every location and solving as usual. (This is an exercise for the reader.)