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

目录
相关文章
|
9月前
|
机器学习/深度学习 算法 PyTorch
【PyTorch实战演练】自调整学习率实例应用(附代码)
【PyTorch实战演练】自调整学习率实例应用(附代码)
267 0
|
9月前
|
机器学习/深度学习 自然语言处理 PyTorch
【PyTorch实战演练】基于AlexNet的预训练模型介绍
【PyTorch实战演练】基于AlexNet的预训练模型介绍
345 0
|
9月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【PyTorch实战演练】AlexNet网络模型构建并使用Cifar10数据集进行批量训练(附代码)
【PyTorch实战演练】AlexNet网络模型构建并使用Cifar10数据集进行批量训练(附代码)
595 0
|
9月前
|
机器学习/深度学习 PyTorch 算法框架/工具
【PyTorch实战演练】使用Cifar10数据集训练LeNet5网络并实现图像分类(附代码)
【PyTorch实战演练】使用Cifar10数据集训练LeNet5网络并实现图像分类(附代码)
573 0
|
7月前
LeNet-5实现的过程
【7月更文挑战第28天】LeNet-5实现的过程。
80 2
|
9月前
|
机器学习/深度学习 PyTorch 算法框架/工具
基于Pytorch通过实例详细剖析CNN
基于Pytorch通过实例详细剖析CNN
97 1
基于Pytorch通过实例详细剖析CNN
|
9月前
|
网络架构
经典神经网络架构参考 v1.0(2)
经典神经网络架构参考 v1.0
52 0
|
9月前
|
机器学习/深度学习 自然语言处理 网络架构
经典神经网络架构参考 v1.0(4)
经典神经网络架构参考 v1.0
70 0
|
9月前
|
机器学习/深度学习 网络架构
经典神经网络架构参考 v1.0(1)
经典神经网络架构参考 v1.0
54 0
|
9月前
|
网络架构
经典神经网络架构参考 v1.0(3)
经典神经网络架构参考 v1.0
50 0