日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

caffe 练习1:training LeNet on MNIST with Caffe/ 用LeNet识别手写字符集 Mnist------by 香蕉麦乐迪

發(fā)布時(shí)間:2025/4/16 226 豆豆
生活随笔 收集整理的這篇文章主要介紹了 caffe 练习1:training LeNet on MNIST with Caffe/ 用LeNet识别手写字符集 Mnist------by 香蕉麦乐迪 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這個(gè)實(shí)驗(yàn)?zāi)脕砣腴T最好了,很輕松,執(zhí)行作者寫好的腳本就可以;

官網(wǎng)鏈接:http://caffe.berkeleyvision.org/gathered/examples/mnist.html

跟著這個(gè)實(shí)驗(yàn),可以觀察caffe使用的流程,深入一點(diǎn),可以看看作者寫的shell腳本做了哪些事情;

下面我們一步步來執(zhí)行,并作簡(jiǎn)單的分析:

1 首先準(zhǔn)備數(shù)據(jù)集

執(zhí)行下面的命令。$CAFFE_ROOT是指caffe安裝的目錄,比如說我的是/home/sloanqin/caffe-master/

[cpp] view plaincopy
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh //執(zhí)行下載mnist數(shù)據(jù)集腳本
./examples/mnist/create_mnist.sh //將下載的mnist數(shù)據(jù)集轉(zhuǎn)換成lmdb格式的數(shù)據(jù)
我們來看看這兩個(gè)腳本做了什么:

1.1 執(zhí)行腳本get_mnist.sh:

[cpp] view plaincopy
get_mnist.sh腳本文件如下:
[cpp] view plaincopy

# This scripts downloads the mnist data and unzips it.

DIR=”(cd"(dirname “0);pwd?P)cdDIR

echo “Downloading…”

wget –no-check-certificate http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
wget –no-check-certificate http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
wget –no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
wget –no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz

echo “Unzipping…”

gunzip train-images-idx3-ubyte.gz
gunzip train-labels-idx1-ubyte.gz
gunzip t10k-images-idx3-ubyte.gz
gunzip t10k-labels-idx1-ubyte.gz

# Creation is split out because leveldb sometimes causes segfault
# and needs to be re-created.

echo “Done.”
[html] view plaincopy
作用:下載下面四個(gè)文件,并解壓;
[html] view plaincopy
train-images-idx3-ubyte.gz訓(xùn)練集
train-labels-idx1-ubyte.gz訓(xùn)練集的標(biāo)簽
t10k-images-idx3-ubyte.gz測(cè)試集
t10k-labels-idx1-ubyte.gz測(cè)試集的標(biāo)簽
2 定義caffe的網(wǎng)絡(luò)結(jié)構(gòu):.prototxt 文件

caffe的網(wǎng)絡(luò)結(jié)構(gòu)定義在后綴名為.prototxt的文件中,我們根據(jù)自己的需要定義自己的網(wǎng)絡(luò)結(jié)構(gòu);

在這個(gè)實(shí)驗(yàn)中,我們使用作者已經(jīng)為我們定義好的lenet網(wǎng)絡(luò)結(jié)構(gòu),大家可以在下面的目錄中找到該文件:

$CAFFE_ROOT/examples/mnist/lenet_train_test.prototxt

在我的電腦上,目錄是/home/sloanqin/examples/mnist/lenet_train_test.prototxt

在后續(xù)的工作中,定義好自己的網(wǎng)絡(luò)結(jié)構(gòu)是最關(guān)鍵的,直接決定了性能,這里我們就不多說了;

3 定義caffe運(yùn)算的時(shí)候的一些規(guī)則:solver.prototxt 文件

該文件在下面的目錄中:

$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt

文件內(nèi)容如下:作者給出了英文注釋,我再給出中文的注釋

[html] view plaincopy
# The train/test net protocol buffer definition
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
# Carry out testing every 500 training iterations.
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
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: “examples/mnist/lenet”
# solver mode: CPU or GPU
solver_mode: GPU
中文注釋版本:
[html] view plaincopy

# The train/test net protocol buffer definition
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 // 這個(gè)參數(shù)指定測(cè)試的時(shí)候送入多少個(gè)
// 這里說明一個(gè)知識(shí):GPU在計(jì)算的時(shí)候,每次迭代是多張圖片,我們稱為一個(gè)batch
// 作者提到:test batch size 100,就是說每個(gè)包有100張圖片
// 這里設(shè)置 st_iter=100,就是測(cè)試的時(shí)候一共輸入100*100=10000張圖片
//所以,test_iter 的英文翻譯就是:測(cè)試時(shí)迭代次數(shù)
# Carry out testing every 500 training iterations.
test_interval: 500 //定義每500次迭代,做一次測(cè)試
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01 // 定義了剛開始的學(xué)習(xí)速率是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
display: 100 //每迭代100次,顯示一次計(jì)算結(jié)果
# The maximum number of iterations
max_iter: 10000 //設(shè)置最大的迭代次數(shù)
# snapshot intermediate results
snapshot: 5000 // 保存中間運(yùn)行時(shí)得到的參數(shù)結(jié)果,這里設(shè)置成每5000次迭代保存一次,這樣運(yùn)行中間斷掉了,我們可以從斷掉的地方繼續(xù)開始
snapshot_prefix: “examples/mnist/lenet”
# solver mode: CPU or GPU
solver_mode: GPU //使用CPU還是GPU計(jì)算
4 執(zhí)行命令進(jìn)行訓(xùn)練
最后一步就是執(zhí)行腳本開始訓(xùn)練:

cd $CAFFE_ROOT
./examples/mnist/train_lenet.sh
我們打開這個(gè)腳本,可以看到特別簡(jiǎn)單,就一行:
./build/tools/caffe train –solver=examples/mnist/lenet_solver.prototxt
這行代碼的意思:調(diào)用./build/tools/caffe目錄下面的train函數(shù),train函數(shù)的輸入?yún)?shù)是solver.prototxt文件的路徑:–solver=examples/mnist/lenet_solver.prototxt

5 結(jié)果

運(yùn)行的過程中,可以卡到test 的準(zhǔn)確率在不斷上升;運(yùn)行結(jié)束后,會(huì)生成模型文件:lenet_iter_10000.caffemodel

還有一個(gè)文件是snapshot 保存的:lenet_iter_10000.solverstate

原文鏈接:http://write.blog.csdn.net/postedit/49147935

總結(jié)

以上是生活随笔為你收集整理的caffe 练习1:training LeNet on MNIST with Caffe/ 用LeNet识别手写字符集 Mnist------by 香蕉麦乐迪的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。