torch7 调用caffe model 作为pretrain

torch7 调用caffe model 作为pretrain

torch7 通过 loadcaffe 包,可以调用caffe训练得到的model 参数作为自己网络的初始参数。

loadcaffe 的安装需要caffe的依赖项,所以首先执行如下指令

sudo apt-get install libprotobuf-dev protobuf-compiler

然后在ubuntu上安装loadcaffe包

sudo luarocks install loadcaffe

参考 loadcaffe

接下来,为了使用caffe model,需要下载caffe model。 caffe model zoo中提供了许多caffemodel的下载。

如何下载 caffemodel? 首先将BVLC/caffe中的包下载并解压,我是解压到 Documents/caffe_master文件夹下的
压缩包里面提供了一些bvlc的models的基本信息,所以可以使用指令
./scripts/download_model_binary.py (model_dir) 下载 (model_dir) 对应的model,看下面代码

~$ cd Documents/caffe_master/caffe_master
~$ sudo ./scripts/download_model_binary.py models/bvlc_reference_caffenet

这时候我的机子提示没有安装python的yaml库,所以在文件头require'yaml'时出错,那么安装yaml库

~$ sudo apt-get install python-yaml

再次执行上面的代码就没问题了,然后在 Documents/caffe_master/caffe_master/models/bvlc_reference_caffenet文件夹下出现的 .caffemodel文件
如下图

enter description here

1487322253246.jpg

依次可以下载bvlc相关的model。

而对于不是bvlc下的model,比如VGG_CNN网络则可以使用 ./scripts/download_model_from_gist.sh (gist_id) (dirname) 指令下载对应caffemodel的元数据、结构以及配置信息等, (gist_id)是对应的gist上的ID号,可以在model zoo查看,(dirname)默认是models文件夹,所以没必要给出。比如在model zoo网站上我们可以查到VGG_CNN_M
的 gist_id是f194575702fae63b2829,那么可以如下代码

~$ cd Documents/caffe_master/caffe_master
~$ sudo ./scripts/download_model_from_gist.sh f194575702fae63b2829

但是这里优惠碰到一个问题,gist被墙了,链接不了,参考 GitHub Gist 被墙

~$ sudo gedit /etc/hosts

添加

192.30.253.118 http://gist.github.com
192.30.253.119 http://gist.github.com

然后再执行上面的指令就好了

按照官方的文档
) To acquire a model:
) 1. download the model gist by ./scripts/download_model_from_gist.sh (gist_id) (dirname)to load the model metadata, architecture, solver configuration, and so on. (dirname) is optional and defaults to caffe/models).
) 2. download the model weights by ./scripts/download_model_binary.py (model_dir) where (model_dir) is the gist directory from the first step.

接下来使用./scripts/download_model_binary.py f194575702fae63b2829 指令就可以下载VGG_CNN_M的model了,但是我执行该指令之后出现如下错误:

enter description here

1487322230701.jpg

注:这里我改成文件名 VGG_CNN_M了

查看download_model_binary.py文件可以发现,readme.md文件需要有三个键值:

required_keys = ['caffemodel', 'caffemodel_url', 'sha1']

自带的bvlc下model的readme文件如下:

enter description here

1487322093594.jpg

而我们下载的VGG_CNN_M的readme文件缺少了sha1校验码,所以总是出错

enter description here

1487322129751.jpg

但同时我们可以发现readme文件中有一项: caffemodel_url
所以我们可以自己下载caffemodel

直接打开 VGG_CNN_M的readme文件中的 caffemodel_url指示的链接 http://www.robots.ox.ac.uk/~vgg/software/deep_eval/releases/bvlc/VGG_CNN_M.caffemodel 便可以下载到对应的caffemodel了,将对应的deploy.prototxt和 .caffemodel放在一起就可以使用 torch7中loadcaffe包调用了

~$ th
th) require 'loadcaffe'
th) proto='VGG_CNN_M_deploy.prototxt'
th) caffemodel = 'VGG_CNN_M.caffemodel'
th) net=loadcaffe.load(proto,caffemodel,'nn')

输出

Successfully loaded VGG_CNN_M.caffemodel
conv1: 96 3 7 7
conv2: 256 96 5 5
conv3: 512 256 3 3
conv4: 512 512 3 3
conv5: 512 512 3 3
fc6: 1 1 18432 4096
fc7: 1 1 4096 4096
fc8: 1 1 4096 1000

OK,至此表示我们已经可以使用torch加载caffemodel了,关于caffemodel如何使用,后面我们继续来看。

references:
gist:
https://www.zhihu.com/question/20732532
http://blog.csdn.net/chclvzxx/article/details/50098515
http://ruby-china.org/topics/22594
model zoo:
http://www.modelzoo.co/
http://caffe.berkeleyvision.org/model_zoo.html
https://github.com/BVLC/caffe/wiki/Model-Zoo
caffeload:
https://github.com/szagoruyko/loadcaffe
http://blog.csdn.net/DreamD1987/article/details/52397906
yaml:
http://blog.csdn.net/philip502/article/details/12838953

原文地址:https://www.cnblogs.com/YiXiaoZhou/p/6411031.html