torch7 调用caffe model 作为pretrain

torch7
caffe
preTrain
model zoo

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

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

  1. sudo apt-get install libprotobuf-dev protobuf-compiler

然后在ubuntu上安装loadcaffe包

  1. 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,看下面代码

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

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

  1. ~$ 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,那么可以如下代码

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

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

  1. ~$ sudo gedit /etc/hosts

添加

  1. 192.30.253.118 http://gist.github.com
  2. 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文件需要有三个键值:

  1. 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包调用了

  1. ~$ th
  2. th) require 'loadcaffe'
  3. th) proto='VGG_CNN_M_deploy.prototxt'
  4. th) caffemodel = 'VGG_CNN_M.caffemodel'
  5. th) net=loadcaffe.load(proto,caffemodel,'nn')

输出

  1. Successfully loaded VGG_CNN_M.caffemodel
  2. conv1: 96 3 7 7
  3. conv2: 256 96 5 5
  4. conv3: 512 256 3 3
  5. conv4: 512 512 3 3
  6. conv5: 512 512 3 3
  7. fc6: 1 1 18432 4096
  8. fc7: 1 1 4096 4096
  9. 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. C++学习笔记--名称空间

    名称空间是为了更好的控制名称的作用域,以管理不同的类库,避免发生冲突. 1.创建名称空间 如下,使用namespace关键字创建了pers和debts两个名称空间. #ifndef NAMESP_H_ ...

  2. javascript 闭包 内存

  3. [Windows Powershell]-学习笔记(6)

    Powershell环境变量 传统的控制台一般没有象Powershell这么高级的变量系统.它们都是依赖于机器本身的环境变量,进行操作 .环境变量对于powershell显得很重要,因为它涵盖了许多操 ...

  4. 网页图片jpg,gif,png对比。

    总结:照片类的jpg如:banner图,小图gif或png8 GIF格式特点: 透明性 Gif是一种布尔透明类型,既它可以是全透明,也可以是全不透明,但是它并没有半透明(alpha 透明). 动画 G ...

  5. Word 为标题设置段前段后间距设置与异常

    一.概述 在进行Word文档写作时,常常要求我们对(节)标题设置段前段后间距.例如: (2)按照标题的不同,分别采用不同的段前段后间距: 标题级别 段前段后间距 章标题 30磅 一级节标题 18磅 二 ...

  6. 在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示

    在vue项目中使用codemirror插件实现代码编辑器功能(代码高亮显示及自动提示) 1.使用npm安装依赖 npm install --save codemirror; 2.在页面中放入如下代码 ...

  7. Django学习笔记之Models与ORM操作

    一.ORM增加 from django.db import models class Publisher(models.Model): name = models.CharField(max_leng ...

  8. 20145211《网络渗透》Adobe阅读器渗透攻击

    20145211<网络渗透>Adobe阅读器渗透攻击 实验准备 1.用了一个kali,一个English Winxp3,并保证能相互ping通 2.开启显示隐藏文件 实验步骤: 1.开启m ...

  9. Btrace使用入门

    1.什么是BTrace BTrace是sun公司推出的一款Java 动态.安全追踪(监控)工具,可以在不用重启的情况下监控系统运行情况,方便的获取程序运行时的数据信息,如方法参数.返回值.全局变量和堆 ...

  10. 4.9版本linux内核的ina220电流检测芯片源码在哪里

    答:在drivers/hwmon/ina2xx.c中,内核配置项为CONFIG_SENSORS_INA2XX Location: -> Device Drivers -> Hardware ...