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. beego——发行部署

    开发模式 通过bee创建的项目,beego默认情况下是开发模式. 我们可以通过如下的方式改变我们的模式: beego.RunMode = "prod" 或者我们在conf/app. ...

  2. 将jar包发布到nexus仓库

    版本的快速迭代不适合release发布到仓库,snapshot方便版本的快速迭代. 1.pom改为snapshot <dependency> <groupId>com.sf.c ...

  3. 19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  4. 论文笔记:dropout

    Improving neural networks by preventing co-adaptation of feature detectors arXiv preprint arXiv: 120 ...

  5. SpringBoot入门学习(三)

    基于第二讲,这一讲我们主要讲解包含以下内容 springBoot添加对freemarker的支持 使用@RestController处理ajax请求 使用@PathVariable注解获取url参数 ...

  6. JVM基本配置与调优

    JVM基本配置与调优 JVM调优,一般都是针对堆内存配置调优. 如图:堆内存分新生代和老年代,新生代又划分为eden区.from区.to区. 一.区域释义 JVM内存模型,堆内存代划分为新生代和老年代 ...

  7. Python3.x:Selenium中的webdriver进行页面元素定位

    Python3.x:Selenium中的webdriver进行页面元素定位 页面上的元素就像人一样,有各种属性,比如元素名字,元素id,元素属性(class属性,name属性)等等.webdriver ...

  8. linux-android(任务处理)

    1.开辟任务内存 2.设置每个任务优先级 ,,

  9. Python高阶函数(Map、Reduce、Filter)和lambda函数一起使用 ,三剑客

    Map函数 map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 举例说明 比如我们有一个函数f(x)=x2,要把这个函数作用 ...

  10. .net 数据缓存(二)之Redis部署

    现在的业务系统越来复杂,大型门户网站内容越来越多,数据库的数据量也越来愈大,所以有了“大数据”这一概念的出现.但是我们都知道当数据库的数据量和访问过于频繁都会影响系统整体性能体验,特别是并发量高的系统 ...