torch7 调用caffe model 作为pretrain

torch7
caffe
preTrain
model zoo

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文件

如下图

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了,但是我执行该指令之后出现如下错误:

1487322230701.jpg

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

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

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

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

1487322093594.jpg

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

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

torch7 调用caffe model 作为pretrain的更多相关文章

  1. 如何在程序中调用Caffe做图像分类

    Caffe是目前深度学习比较优秀好用的一个开源库,采样c++和CUDA实现,具有速度快,模型定义方便等优点.学习了几天过后,发现也有一个不方便的地方,就是在我的程序中调用Caffe做图像分类没有直接的 ...

  2. windows下用c++调用caffe做前向

    参考博客: https://blog.csdn.net/muyouhang/article/details/54773265 https://blog.csdn.net/hhh0209/article ...

  3. 创建新的C++工程来调用Caffe对图片进行识别

    前段时间一直在跑Caffe训练数据.之前用训练好的caffemodel对图片进行分类都是用的命令行指令,于是就想着自己新建一个工程来调用caffe,结合classification的代码来对图片进行分 ...

  4. 【神经网络与深度学习】Caffe Model Zoo许多训练好的caffemodel

    Caffe Model Zoo 许多的研究者和工程师已经创建了Caffe模型,用于不同的任务,使用各种种类的框架和数据.这些模型被学习和应用到许多问题上,从简单的回归到大规模的视觉分类,到Siames ...

  5. python调用caffe实现预测

    对于已经训练完成的caffemodel,对于单个的图片预测,用python接口来调用是一件非常方便的事情,下面就来讲述如何用python调用已经训练完成的caffemodel,以及prototxt,网 ...

  6. Windows下caffe的配置和调用caffe库(二)

    二. Caffe库的调用: 新建空白项目,将配置管理器更改为x64运行方式.(release和Debug均可). Debug配置: 1)      包含目录: D:\caffe-master\incl ...

  7. python调用caffe环境配置

    背景是这样的,项目需要,必须将训练的模型通过C++进行调用,所以必须使用caffe或者mxnet,而caffe是用C++实现,所以有时候简单的加载一张图片然后再进行预测十分不方便 用caffe写pro ...

  8. Thinkhphp5控制器调用的Model层的方法总结

    控制器器里: <?php /** * Created by PhpStorm. * User: Haima * Date: 2018/7/8 * Time: 15:58 */ namespace ...

  9. Windows下caffe的配置和调用caffe库(一)

    一.Windows下caffe的配置: 1. 下载caffe官网提供的开发包,https://github.com/microsoft/caffe 2. 将caffe-master目录下的Window ...

随机推荐

  1. Oracle 常见问题汇总

    1.Listener refused the connection with the following error 安装之后如果遇到如下问题状态: 失败 -测试失败: Listener refuse ...

  2. js 有小数点数据求和多出小数点问题记录

    >> +192.92 << 492.91999999999996 >> (*+)/ << 492.92 做统计汇总时,页面加和,展示出一堆的小数点. 网 ...

  3. PL/SQL 表约束

    1. 表相关 a. 主键:constraint [主键的约束名] primary key b. 外键:constraint [外键约束名] foreign key( ) references []() ...

  4. Java分布式:消息队列(Message Queue)

    Java分布式:消息队列(Message Queue) 引入消息队列 消息,是服务间通信的一种数据单位,消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.队列,是一种常见的数据结 ...

  5. Jackson /常用注解/ annotation(转)

    1.@JsonAutoDetect 自动检测,(作用在类上)来开启/禁止自动检测. fieldVisibility:字段的可见级别 ANY:任何级别的字段都可以自动识别 NONE:所有字段都不可以自动 ...

  6. sidekiq-cron定时任务

    参考  时间格式 gem "sidekiq-cron", "~> 1.1" route.rb下添加 require 'sidekiq/cron/web', ...

  7. linux环境上运行.net core 初探

    1.安装 .net core 环境 rpm --import https://packages.microsoft.com/keys/microsoft.ascsh -c 'echo -e " ...

  8. LRU算法---缓存淘汰算法

    计算机中的缓存大小是有限的,如果对所有数据都缓存,肯定是不现实的,所以需要有一种淘汰机制,用于将一些暂时没有用的数据给淘汰掉,以换入新鲜的数据进来,这样可以提高缓存的命中率,减少磁盘访问的次数. LR ...

  9. redis日常操作

    redis针对所有类型的日常操作: keys * ## 取出所有key keys my* ## 模糊匹配 exists name ## 存在name键返回1,否则返回0 del key1 ## 删除一 ...

  10. C++ 第三十三天

    Ⅰ.类成员函数的隐式参数 T *const this . 就是说对于某个类的成员函数 returnType function() 的真实面目其实是这样的 returnType function(T * ...