LeNet实例实践

简介: 一个寒假一起入门深度学习

紧接之前Yann Lecun的LeNet的论文Gradient-Based Learning Applied to Document Recognition,我们这边来实践一下LeNet网络
这里我们使用Caffe框架进行LeNet的实践,用的数据集是mnist
首先先下载mnist数据集;
在Caffe目录下执行:

./data/mnist/get_mnist.sh
./exampels/mnist/create_mnist.sh

获取mnist数据集并将数据集转换为mnist_train_lmdb和mnist_test_lmdb格式。
在caffe/example/mnist目录下有一个lenet_train_test.prototxt

name: "LeNet"
layer {
  name: "mnist"//名字
  type: "Data"//类型为数据
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625//归一化 1/256
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"
    batch_size: 64//分批处理的图像个数
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"//生成two blobs,分别为data blob 和label blob
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"//卷积层
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1//权重的学习率与solver运行的学习率一致
  }
  param {
    lr_mult: 2//偏置的学习率是solver运行的学习率的2倍
  }
  convolution_param {
    num_output: 20//20个featuremap
    kernel_size: 5//卷积核为5x5
    stride: 1//步长为1
    weight_filler {
      type: "xavier"//用 xavier算法初始化权重
    }
    bias_filler {
      type: "constant"//用常数初始化权重
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2//核大小2
    stride: 2//步长大小2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

用Netscope可视化的结果:
2018_01_30_20_11_30_
(这边用relu代替了原来的sigmoid函数)
在/caffe/examples/mnist/lenet_solver.prototxt中有训练参数的配置:

# The train/test net protocol buffer definition//使bi用网络结构
net: "examples/mnist/lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100//测试时bitch_size100
# Carry out testing every 500 training iterations.//每500轮测试一次
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations每100次迭代次数显示一次训练时lr和loss
display: 100
# The maximum number of iterations最大迭代次数
max_iter: 10000
# snapshot intermediate results每5000次迭代输出模型
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"//模型保存路径
# solver mode: CPU or GPU
solver_mode: GPU

通过运行

./examples/mnist/train_lenet.sh 

train_lenet.sh:

#!/usr/bin/env sh
set -e

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt $@

可以进行训练,下面是训练结果:
2018_01_30_20_44_29_
可以看到准确率可以达到99.03%,已经是相当高了,但网络对于像mnist效果好,但对于稍大一点的数据集效果就会下降地很明显。
可以看到caffe能快速地进行网络的搭建,但是相对于tensorflow来说不够灵活,而且caffe无法进行递归循环网络的搭建。
55cf39c9f60555467220312a80352b3a_hd

目录
相关文章
|
存储 算法 Linux
DNF和Yum
DNF和Yum
1417 3
|
存储 SQL 运维
大规模订单系统解读-架构篇
从最早的互联网高速发展、到移动互联网的爆发式增长,再到今天的产业互联网、物联网的快速崛起,各种各样新应用、新系统产生了众多订单类型的需求,比如购物订单、交流流水,外卖订单、支付账单、设备信息等。数据范围不仅越来越广,而且数据量越来越大,原有的经典架构方案已经很难满足当前新的业务场景。在新的需求下,对存储规模、开发效率、查询功能、未来扩展性等众多方面提出了更高的要求,要设计一款可靠稳定且扩展性好
9845 1
大规模订单系统解读-架构篇
|
JavaScript 前端开发 Android开发
在Eclipse中使用JSLint保证JavaScript代码质量
JSLint简介   JavaScript 作为一门语法灵活多变且对格式要求相对松散的语言,代码格式的混乱和某些语言特性的不正确使用,往往使得最终交付的产品中包含许多因编码风格约定造成的未预见的行为或错误,这种习惯性的问题如果不及时指出并修改,往往会在项目的迭代过程中不断的重现,严重影响 Web 产品的稳定性与安全性。
1166 0
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16945 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1249 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2338 7