Caffe 深度学习框架上手教程

 

Caffe (CNN, deep learning) 介绍

Caffe -----------Convolution Architecture For Feature Embedding (Extraction)

  1. Caffe 是什么东东?

    • CNN (Deep Learning) 工具箱
    • C++ 语言架构
    • CPU 和GPU 无缝交换
    • Python 和matlab的封装
    • 但是,Decaf只是CPU 版本。
  2. 为什么要用Caffe?

    • 运算速度快。简单 友好的架构 用到的一些库:
    • Google Logging library (Glog): 一个C++语言的应用级日志记录框架,提供了C++风格的流操作和各种助手宏.
    • lebeldb(数据存储): 是一个google实现的非常高效的kv数据库,单进程操作。
    • CBLAS library(CPU版本的矩阵操作)
    • CUBLAS library (GPU 版本的矩阵操作)
  3. Caffe 架构

  1. 预处理图像的leveldb构建
    输入:一批图像和label (2和3)
    输出:leveldb (4)
    指令里包含如下信息:

    • conver_imageset (构建leveldb的可运行程序)
    • train/ (此目录放处理的jpg或者其他格式的图像)
    • label.txt (图像文件名及其label信息)
    • 输出的leveldb文件夹的名字
    • CPU/GPU (指定是在cpu上还是在gpu上运行code)
  2. CNN网络配置文件

    • Imagenet_solver.prototxt (包含全局参数的配置的文件)
    • Imagenet.prototxt (包含训练网络的配置的文件)
    • Imagenet_val.prototxt (包含测试网络的配置文件)

1 回复

2 赞
 

Caffe深度学习之图像分类模型AlexNet解读

在imagenet上的图像分类challenge上Alex提出的alexnet网络结构模型赢得了2012届的冠军。要研究CNN类型DL网络模型在图像分类上的应用,就逃不开研究alexnet,这是CNN在图像分类上的经典模型(DL火起来之后)。

在DL开源实现caffe的model样例中,它也给出了alexnet的复现,具体网络配置文件如下train_val.prototxt452

接下来本文将一步步对该网络配置结构中各个层进行详细的解读(训练阶段):

  1. conv1阶段DFD(data flow diagram):

  2. conv2阶段DFD(data flow diagram):

  3. conv3阶段DFD(data flow diagram):

    SouthEast2455x274
  4. conv4阶段DFD(data flow diagram):

  5. conv5阶段DFD(data flow diagram):

  6. fc6阶段DFD(data flow diagram):

  7. fc7阶段DFD(data flow diagram):

  8. fc8阶段DFD(data flow diagram):

各种layer的operation更多解释可以参考Caffe Layer Catalogue356

从计算该模型的数据流过程中,该模型参数大概5kw+。

caffe的输出中也有包含这块的内容日志,详情如下:

I0721 10:38:15.326920  4692 net.cpp:125] Top shape: 256 3 227 227 (39574272)
I0721 10:38:15.326971 4692 net.cpp:125] Top shape: 256 1 1 1 (256)
I0721 10:38:15.326982 4692 net.cpp:156] data does not need backward computation.
I0721 10:38:15.327003 4692 net.cpp:74] Creating Layer conv1
I0721 10:38:15.327011 4692 net.cpp:84] conv1 <- data
I0721 10:38:15.327033 4692 net.cpp:110] conv1 -> conv1
I0721 10:38:16.721956 4692 net.cpp:125] Top shape: 256 96 55 55 (74342400)
I0721 10:38:16.722030 4692 net.cpp:151] conv1 needs backward computation.
I0721 10:38:16.722059 4692 net.cpp:74] Creating Layer relu1
I0721 10:38:16.722070 4692 net.cpp:84] relu1 <- conv1
I0721 10:38:16.722082 4692 net.cpp:98] relu1 -> conv1 (in-place)
I0721 10:38:16.722096 4692 net.cpp:125] Top shape: 256 96 55 55 (74342400)
I0721 10:38:16.722105 4692 net.cpp:151] relu1 needs backward computation.
I0721 10:38:16.722116 4692 net.cpp:74] Creating Layer pool1
I0721 10:38:16.722125 4692 net.cpp:84] pool1 <- conv1
I0721 10:38:16.722133 4692 net.cpp:110] pool1 -> pool1
I0721 10:38:16.722167 4692 net.cpp:125] Top shape: 256 96 27 27 (17915904)
I0721 10:38:16.722187 4692 net.cpp:151] pool1 needs backward computation.
I0721 10:38:16.722205 4692 net.cpp:74] Creating Layer norm1
I0721 10:38:16.722221 4692 net.cpp:84] norm1 <- pool1
I0721 10:38:16.722234 4692 net.cpp:110] norm1 -> norm1
I0721 10:38:16.722251 4692 net.cpp:125] Top shape: 256 96 27 27 (17915904)
I0721 10:38:16.722260 4692 net.cpp:151] norm1 needs backward computation.
I0721 10:38:16.722272 4692 net.cpp:74] Creating Layer conv2
I0721 10:38:16.722280 4692 net.cpp:84] conv2 <- norm1
I0721 10:38:16.722290 4692 net.cpp:110] conv2 -> conv2
I0721 10:38:16.725225 4692 net.cpp:125] Top shape: 256 256 27 27 (47775744)
I0721 10:38:16.725242 4692 net.cpp:151] conv2 needs backward computation.
I0721 10:38:16.725253 4692 net.cpp:74] Creating Layer relu2
I0721 10:38:16.725261 4692 net.cpp:84] relu2 <- conv2
I0721 10:38:16.725270 4692 net.cpp:98] relu2 -> conv2 (in-place)
I0721 10:38:16.725280 4692 net.cpp:125] Top shape: 256 256 27 27 (47775744)
I0721 10:38:16.725288 4692 net.cpp:151] relu2 needs backward computation.
I0721 10:38:16.725298 4692 net.cpp:74] Creating Layer pool2
I0721 10:38:16.725307 4692 net.cpp:84] pool2 <- conv2
I0721 10:38:16.725317 4692 net.cpp:110] pool2 -> pool2
I0721 10:38:16.725329 4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.725338 4692 net.cpp:151] pool2 needs backward computation.
I0721 10:38:16.725358 4692 net.cpp:74] Creating Layer norm2
I0721 10:38:16.725368 4692 net.cpp:84] norm2 <- pool2
I0721 10:38:16.725378 4692 net.cpp:110] norm2 -> norm2
I0721 10:38:16.725389 4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.725399 4692 net.cpp:151] norm2 needs backward computation.
I0721 10:38:16.725409 4692 net.cpp:74] Creating Layer conv3
I0721 10:38:16.725419 4692 net.cpp:84] conv3 <- norm2
I0721 10:38:16.725427 4692 net.cpp:110] conv3 -> conv3
I0721 10:38:16.735193 4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.735213 4692 net.cpp:151] conv3 needs backward computation.
I0721 10:38:16.735224 4692 net.cpp:74] Creating Layer relu3
I0721 10:38:16.735234 4692 net.cpp:84] relu3 <- conv3
I0721 10:38:16.735242 4692 net.cpp:98] relu3 -> conv3 (in-place)
I0721 10:38:16.735250 4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.735258 4692 net.cpp:151] relu3 needs backward computation.
I0721 10:38:16.735302 4692 net.cpp:74] Creating Layer conv4
I0721 10:38:16.735312 4692 net.cpp:84] conv4 <- conv3
I0721 10:38:16.735321 4692 net.cpp:110] conv4 -> conv4
I0721 10:38:16.743952 4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.743988 4692 net.cpp:151] conv4 needs backward computation.
I0721 10:38:16.744000 4692 net.cpp:74] Creating Layer relu4
I0721 10:38:16.744010 4692 net.cpp:84] relu4 <- conv4
I0721 10:38:16.744020 4692 net.cpp:98] relu4 -> conv4 (in-place)
I0721 10:38:16.744030 4692 net.cpp:125] Top shape: 256 384 13 13 (16613376)
I0721 10:38:16.744038 4692 net.cpp:151] relu4 needs backward computation.
I0721 10:38:16.744050 4692 net.cpp:74] Creating Layer conv5
I0721 10:38:16.744057 4692 net.cpp:84] conv5 <- conv4
I0721 10:38:16.744067 4692 net.cpp:110] conv5 -> conv5
I0721 10:38:16.748935 4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.748955 4692 net.cpp:151] conv5 needs backward computation.
I0721 10:38:16.748965 4692 net.cpp:74] Creating Layer relu5
I0721 10:38:16.748975 4692 net.cpp:84] relu5 <- conv5
I0721 10:38:16.748983 4692 net.cpp:98] relu5 -> conv5 (in-place)
I0721 10:38:16.748998 4692 net.cpp:125] Top shape: 256 256 13 13 (11075584)
I0721 10:38:16.749011 4692 net.cpp:151] relu5 needs backward computation.
I0721 10:38:16.749022 4692 net.cpp:74] Creating Layer pool5
I0721 10:38:16.749030 4692 net.cpp:84] pool5 <- conv5
I0721 10:38:16.749039 4692 net.cpp:110] pool5 -> pool5
I0721 10:38:16.749050 4692 net.cpp:125] Top shape: 256 256 6 6 (2359296)
I0721 10:38:16.749058 4692 net.cpp:151] pool5 needs backward computation.
I0721 10:38:16.749074 4692 net.cpp:74] Creating Layer fc6
I0721 10:38:16.749083 4692 net.cpp:84] fc6 <- pool5
I0721 10:38:16.749091 4692 net.cpp:110] fc6 -> fc6
I0721 10:38:17.160079 4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.160148 4692 net.cpp:151] fc6 needs backward computation.
I0721 10:38:17.160166 4692 net.cpp:74] Creating Layer relu6
I0721 10:38:17.160177 4692 net.cpp:84] relu6 <- fc6
I0721 10:38:17.160190 4692 net.cpp:98] relu6 -> fc6 (in-place)
I0721 10:38:17.160202 4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.160212 4692 net.cpp:151] relu6 needs backward computation.
I0721 10:38:17.160222 4692 net.cpp:74] Creating Layer drop6
I0721 10:38:17.160230 4692 net.cpp:84] drop6 <- fc6
I0721 10:38:17.160238 4692 net.cpp:98] drop6 -> fc6 (in-place)
I0721 10:38:17.160258 4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.160265 4692 net.cpp:151] drop6 needs backward computation.
I0721 10:38:17.160277 4692 net.cpp:74] Creating Layer fc7
I0721 10:38:17.160286 4692 net.cpp:84] fc7 <- fc6
I0721 10:38:17.160295 4692 net.cpp:110] fc7 -> fc7
I0721 10:38:17.342094 4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.342157 4692 net.cpp:151] fc7 needs backward computation.
I0721 10:38:17.342175 4692 net.cpp:74] Creating Layer relu7
I0721 10:38:17.342185 4692 net.cpp:84] relu7 <- fc7
I0721 10:38:17.342198 4692 net.cpp:98] relu7 -> fc7 (in-place)
I0721 10:38:17.342208 4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.342217 4692 net.cpp:151] relu7 needs backward computation.
I0721 10:38:17.342228 4692 net.cpp:74] Creating Layer drop7
I0721 10:38:17.342236 4692 net.cpp:84] drop7 <- fc7
I0721 10:38:17.342245 4692 net.cpp:98] drop7 -> fc7 (in-place)
I0721 10:38:17.342254 4692 net.cpp:125] Top shape: 256 4096 1 1 (1048576)
I0721 10:38:17.342262 4692 net.cpp:151] drop7 needs backward computation.
I0721 10:38:17.342274 4692 net.cpp:74] Creating Layer fc8
I0721 10:38:17.342283 4692 net.cpp:84] fc8 <- fc7
I0721 10:38:17.342291 4692 net.cpp:110] fc8 -> fc8
I0721 10:38:17.343199 4692 net.cpp:125] Top shape: 256 22 1 1 (5632)
I0721 10:38:17.343214 4692 net.cpp:151] fc8 needs backward computation.
I0721 10:38:17.343231 4692 net.cpp:74] Creating Layer loss
I0721 10:38:17.343240 4692 net.cpp:84] loss <- fc8
I0721 10:38:17.343250 4692 net.cpp:84] loss <- label
I0721 10:38:17.343264 4692 net.cpp:151] loss needs backward computation.
I0721 10:38:17.343305 4692 net.cpp:173] Collecting Learning Rate and Weight Decay.
I0721 10:38:17.343327 4692 net.cpp:166] Network initialization done.
I0721 10:38:17.343335 4692 net.cpp:167] Memory required for Data 1073760256
1 赞
 

CIFAR-10在caffe上进行训练与学习

使用数据库:CIFAR-10

60000张 32X32 彩色图像 10类,50000张训练,10000张测试

准备

在终端运行以下指令:

cd $CAFFE_ROOT/data/cifar10
./get_cifar10.sh
cd $CAFFE_ROOT/examples/cifar10
./create_cifar10.sh

其中CAFFE_ROOT是caffe-master在你机子的地址

运行之后,将会在examples中出现数据库文件./cifar10-leveldb和数据库图像均值二进制文件./mean.binaryproto

模型

该CNN由卷积层,POOLing层,非线性变换层,在顶端的局部对比归一化线性分类器组成。该模型的定义在CAFFE_ROOT/examples/cifar10 directory’s cifar10_quick_train.prototxt中,可以进行修改。其实后缀为prototxt很多都是用来修改配置的。

训练和测试

训练这个模型非常简单,当我们写好参数设置的文件cifar10_quick_solver.prototxt和定义的文件cifar10_quick_train.prototxt和cifar10_quick_test.prototxt后,运行train_quick.sh或者在终端输入下面的命令:

cd $CAFFE_ROOT/examples/cifar10
./train_quick.sh

即可,train_quick.sh是一个简单的脚本,会把执行的信息显示出来,培训的工具是train_net.bin,cifar10_quick_solver.prototxt作为参数。

然后出现类似以下的信息:这是搭建模型的相关信息

I0317 21:52:48.945710 2008298256 net.cpp:74] Creating Layer conv1
I0317 21:52:48.945716 2008298256 net.cpp:84] conv1 <- data
I0317 21:52:48.945725 2008298256 net.cpp:110] conv1 -> conv1
I0317 21:52:49.298691 2008298256 net.cpp:125] Top shape: 100 32 32 32 (3276800)
I0317 21:52:49.298719 2008298256 net.cpp:151] conv1 needs backward computation.

接着:

0317 21:52:49.309370 2008298256 net.cpp:166] Network initialization done.
I0317 21:52:49.309376 2008298256 net.cpp:167] Memory required for Data 23790808
I0317 21:52:49.309422 2008298256 solver.cpp:36] Solver scaffolding done.
I0317 21:52:49.309447 2008298256 solver.cpp:47] Solving CIFAR10_quick_train

之后,训练开始

I0317 21:53:12.179772 2008298256 solver.cpp:208] Iteration 100, lr = 0.001
I0317 21:53:12.185698 2008298256 solver.cpp:65] Iteration 100, loss = 1.73643
...
I0317 21:54:41.150030 2008298256 solver.cpp:87] Iteration 500, Testing net
I0317 21:54:47.129461 2008298256 solver.cpp:114] Test score #0: 0.5504
I0317 21:54:47.129500 2008298256 solver.cpp:114] Test score #1: 1.27805

其中每100次迭代次数显示一次训练时lr(learningrate),和loss(训练损失函数),每500次测试一次,输出score 0(准确率)和score 1(测试损失函数)

当5000次迭代之后,正确率约为75%,模型的参数存储在二进制protobuf格式在cifar10_quick_iter_5000

然后,这个模型就可以用来运行在新数据上了。

其他

另外,更改cifar*solver.prototxt文件可以使用CPU训练,

# solver mode: CPU or GPU
solver_mode: CPU

可以看看CPU和GPU训练的差别。

主要资料来源:caffe官网教程

Caffe 深度学习框架上手教程的更多相关文章

  1. [转]Caffe 深度学习框架上手教程

    Caffe 深度学习框架上手教程 机器学习Caffe caffe 原文地址:http://suanfazu.com/t/caffe/281   blink 15年1月 6   Caffe448是一个清 ...

  2. Ubuntu 14.04 安装caffe深度学习框架

    简介:如何在ubuntu 14.04 下安装caffe深度学习框架. 注:安装caffe时一定要保持网络状态好,不然会遇到很多麻烦.例如下载不了,各种报错. 一.安装依赖包 $ sudo apt-ge ...

  3. Caffe 深度学习框架介绍

    转自:http://suanfazu.com/t/caffe/281 Caffe是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清,目前在Google工作. Caffe是 ...

  4. TensorFlow实战Google深度学习框架-人工智能教程-自学人工智能的第二天-深度学习

    自学人工智能的第一天 "TensorFlow 是谷歌 2015 年开源的主流深度学习框架,目前已得到广泛应用.本书为 TensorFlow 入门参考书,旨在帮助读者以快速.有效的方式上手 T ...

  5. 从TensorFlow 到 Caffe2:盘点深度学习框架

    机器之心报道 本文首先介绍GitHub中最受欢迎的开源深度学习框架排名,然后再对其进行系统地对比 下图总结了在GitHub中最受欢迎的开源深度学习框架排名,该排名是基于各大框架在GitHub里的收藏数 ...

  6. 贾扬清分享_深度学习框架caffe

    Caffe是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的 贾扬清,目前在Google工作.本文是根据机器学习研究会组织的online分享的交流内容,简单的整理了一下. 目录 ...

  7. Sony深度学习框架 - Neural Network Console - 教程(1)- 原来深度学习可以如此简单

    “什么情况!?居然不是黑色背景+白色文字的命令行.对,今天要介绍的是一个拥有白嫩的用户界面的深度学习框架.” 人工智能.神经网络.深度学习,这些概念近年已经涌入每个人的生活中,我想很多人早就按捺不住想 ...

  8. 转:TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比

    http://geek.csdn.net/news/detail/138968 Google近日发布了TensorFlow 1.0候选版,这第一个稳定版将是深度学习框架发展中的里程碑的一步.自Tens ...

  9. Caffe深度学习计算框架

    Caffe | Deep Learning Framework是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的 Yangqing Jia,目前在Google工作.Caffe是 ...

随机推荐

  1. springboot 学习之路 17(webflux 入门 (1))

    Webflux: webflux是在springboot2系列引入的技术:补充一些概念: 1> Reactive Streams 是 JVM 中面向流的库标准和规范: 处理可能无限数量的元素 按 ...

  2. Linux服务器redhat配置本地yum源

    前面给大家介绍了很多在Linux安装软件的知识,这些软件往往依赖了很多第三方的工具或者软件,如果在Linux服务器有外网的情况,咱们可以通过yum install这样的命令直接去安装这些相关的工具或者 ...

  3. oracle instr函数(oracle 用instr 来代替 like)

    oracle instr函数 对于instr函数,我们经常这样使用:从一个字符串中查找指定子串的位置.例如: SQL> select instr('Oracle','or') position ...

  4. 详解JNDI的lookup资源引用java:/comp/env

    ENC的概念:     The application component environment is referred to as the ENC, the enterprise naming c ...

  5. zabbix待完整

    fad 下载zabbix3.4的配置文件 wget -O zabbix-3.4.2.tar.gz http://sourceforge.net/projects/zabbix/files/ZABBIX ...

  6. Windows 7 任务栏图标消失(变透明,仍然占有地方,但是点击无反应)的解决方法

    解决方案:清理资源管理器缓存(重启资源管理器) 1.打开程序管理器(ctrl+shift+esc) 2.在进程那里找到"explorer.exe",然后按结束进程 3.然后在文件( ...

  7. 实现CTreeCtrl父子节点的联动选择

    本文实现了下面的功能: 当选中父节点时, 其所有子节点全部选中. 当取消选中父节点时, 其所以子节点全部取消选中. 点击子节点时, 根据子节点与其兄弟节点的选中状态, 自动设置父节点的选中状态. 通过 ...

  8. 35_张孝祥Java高新技术_为注解增加各种属性

    注解的作用之所以那么强大,就是因为它有属性 注解很像接口,属性很像方法.   什么是注解的属性 一个注解相当于一个胸牌,如果你胸前贴了胸牌,就是传智播客的学生,否则,就不是.如果还想区分出事传智播客 ...

  9. 极限编程核心价值:沟通(Communication)

    原文:https://deviq.com/communication 极限编程核心价值:简单(Simplicity) 极限编程核心价值:沟通(Communication) 极限编程核心价值:反馈(Fe ...

  10. css固定footer到浏览器底部的方法

    <html> <head></head> <body> <div class="page-wrapper"> <d ...