亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

caffe的python接口之手寫(xiě)數(shù)字識(shí)別mnist實(shí)例

 更新時(shí)間:2022年06月29日 12:25:48   作者:denny402  
這篇文章主要為大家介紹了caffe的python接口之手寫(xiě)數(shù)字識(shí)別mnist實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

引言

深度學(xué)習(xí)的第一個(gè)實(shí)例一般都是mnist,只要這個(gè)例子完全弄懂了,其它的就是舉一反三的事了。由于篇幅原因,本文不具體介紹配置文件里面每個(gè)參數(shù)的具體函義,如果想弄明白的,請(qǐng)參看我以前的博文:

數(shù)據(jù)層及參數(shù)

視覺(jué)層及參數(shù)

solver配置文件及參數(shù)

一、數(shù)據(jù)準(zhǔn)備

官網(wǎng)提供的mnist數(shù)據(jù)并不是圖片,但我們以后做的實(shí)際項(xiàng)目可能是圖片。因此有些人并不知道該怎么辦。在此我將mnist數(shù)據(jù)進(jìn)行了轉(zhuǎn)化,變成了一張張的圖片,我們練習(xí)就從圖片開(kāi)始。mnist圖片數(shù)據(jù)我放在了百度云盤(pán)。

mnist圖片數(shù)據(jù)點(diǎn)擊下載

數(shù)據(jù)分成了訓(xùn)練集(60000張共10類)和測(cè)試集(共10000張10類),每個(gè)類別放在一個(gè)單獨(dú)的文件夾里。并且將所有的圖片,都生成了txt列表清單(train.txt和test.txt)。大家下載下來(lái)后,直接解壓到當(dāng)前用戶根目錄下就可以了。由于我是在windows下壓縮的,因此是winrar文件。如果大家要在linux下解壓縮,需要安裝rar的linux版本,也是十分簡(jiǎn)單

sudo apt-get install rar

二、導(dǎo)入caffe庫(kù),并設(shè)定文件路徑

我是將mnist直接放在根目錄下的,所以代碼如下:

# -*- coding: utf-8 -*-
import caffe
from caffe import layers as L,params as P,proto,to_proto
#設(shè)定文件的保存路徑
root='/home/xxx/'                           #根目錄
train_list=root+'mnist/train/train.txt'     #訓(xùn)練圖片列表
test_list=root+'mnist/test/test.txt'        #測(cè)試圖片列表
train_proto=root+'mnist/train.prototxt'     #訓(xùn)練配置文件
test_proto=root+'mnist/test.prototxt'       #測(cè)試配置文件
solver_proto=root+'mnist/solver.prototxt'   #參數(shù)文件

其中train.txt 和test.txt文件已經(jīng)有了,其它三個(gè)文件,我們需要自己編寫(xiě)。

此處注意:一般caffe程序都是先將圖片轉(zhuǎn)換成lmdb文件,但這樣做有點(diǎn)麻煩。因此我就不轉(zhuǎn)換了,我直接用原始圖片進(jìn)行操作,所不同的就是直接用圖片操作,均值很難計(jì)算,因此可以不減均值。

二、生成配置文件

配置文件實(shí)際上就是一些txt文檔,只是后綴名是prototxt,我們可以直接到編輯器里編寫(xiě),也可以用代碼生成。此處,我用python來(lái)生成。

#編寫(xiě)一個(gè)函數(shù),生成配置文件prototxt
def Lenet(img_list,batch_size,include_acc=False):
    #第一層,數(shù)據(jù)輸入層,以ImageData格式輸入
    data, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2,root_folder=root,
        transform_param=dict(scale= 0.00390625))
    #第二層:卷積層
    conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=20, pad=0,weight_filler=dict(type='xavier'))
    #池化層
    pool1=L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    #卷積層
    conv2=L.Convolution(pool1, kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type='xavier'))
    #池化層
    pool2=L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    #全連接層
    fc3=L.InnerProduct(pool2, num_output=500,weight_filler=dict(type='xavier'))
    #激活函數(shù)層
    relu3=L.ReLU(fc3, in_place=True)
    #全連接層
    fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type='xavier'))
    #softmax層
    loss = L.SoftmaxWithLoss(fc4, label)
    if include_acc:             # test階段需要有accuracy層
        acc = L.Accuracy(fc4, label)
        return to_proto(loss, acc)
    else:
        return to_proto(loss)
def write_net():
    #寫(xiě)入train.prototxt
    with open(train_proto, 'w') as f:
        f.write(str(Lenet(train_list,batch_size=64)))
    #寫(xiě)入test.prototxt    
    with open(test_proto, 'w') as f:
        f.write(str(Lenet(test_list,batch_size=100, include_acc=True)))

配置文件里面存放的,就是我們所說(shuō)的network。我這里生成的network,可能和原始的Lenet不太一樣,不過(guò)影響不大。

三、生成參數(shù)文件solver

同樣,可以在編輯器里面直接書(shū)寫(xiě),也可以用代碼生成。

#編寫(xiě)一個(gè)函數(shù),生成參數(shù)文件
def gen_solver(solver_file,train_net,test_net):
    s=proto.caffe_pb2.SolverParameter()
    s.train_net =train_net
    s.test_net.append(test_net)
    s.test_interval = 938    #60000/64,測(cè)試間隔參數(shù):訓(xùn)練完一次所有的圖片,進(jìn)行一次測(cè)試  
    s.test_iter.append(100)  #10000/100 測(cè)試迭代次數(shù),需要迭代100次,才完成一次所有數(shù)據(jù)的測(cè)試
    s.max_iter = 9380       #10 epochs , 938*10,最大訓(xùn)練次數(shù)
    s.base_lr = 0.01    #基礎(chǔ)學(xué)習(xí)率
    s.momentum = 0.9    #動(dòng)量
    s.weight_decay = 5e-4  #權(quán)值衰減項(xiàng)
    s.lr_policy = 'step'   #學(xué)習(xí)率變化規(guī)則
    s.stepsize=3000         #學(xué)習(xí)率變化頻率
    s.gamma = 0.1          #學(xué)習(xí)率變化指數(shù)
    s.display = 20         #屏幕顯示間隔
    s.snapshot = 938       #保存caffemodel的間隔
    s.snapshot_prefix =root+'mnist/lenet'   #caffemodel前綴
    s.type ='SGD'         #優(yōu)化算法
    s.solver_mode = proto.caffe_pb2.SolverParameter.GPU    #加速
    #寫(xiě)入solver.prototxt
    with open(solver_file, 'w') as f:
        f.write(str(s))

四、開(kāi)始訓(xùn)練模型

訓(xùn)練過(guò)程中,也在不停的測(cè)試。

#開(kāi)始訓(xùn)練
def training(solver_proto):
    caffe.set_device(0)
    caffe.set_mode_gpu()
    solver = caffe.SGDSolver(solver_proto)
    solver.solve()

最后,調(diào)用以上的函數(shù)就可以了。

if __name__ == '__main__':
    write_net()
    gen_solver(solver_proto,train_proto,test_proto) 
    training(solver_proto)

五、完成的python文件

mnist.py

# -*- coding: utf-8 -*-
import caffe
from caffe import layers as L,params as P,proto,to_proto
#設(shè)定文件的保存路徑
root='/home/xxx/'                           #根目錄
train_list=root+'mnist/train/train.txt'     #訓(xùn)練圖片列表
test_list=root+'mnist/test/test.txt'        #測(cè)試圖片列表
train_proto=root+'mnist/train.prototxt'     #訓(xùn)練配置文件
test_proto=root+'mnist/test.prototxt'       #測(cè)試配置文件
solver_proto=root+'mnist/solver.prototxt'   #參數(shù)文件
#編寫(xiě)一個(gè)函數(shù),生成配置文件prototxt
def Lenet(img_list,batch_size,include_acc=False):
    #第一層,數(shù)據(jù)輸入層,以ImageData格式輸入
    data, label = L.ImageData(source=img_list, batch_size=batch_size, ntop=2,root_folder=root,
        transform_param=dict(scale= 0.00390625))
    #第二層:卷積層
    conv1=L.Convolution(data, kernel_size=5, stride=1,num_output=20, pad=0,weight_filler=dict(type='xavier'))
    #池化層
    pool1=L.Pooling(conv1, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    #卷積層
    conv2=L.Convolution(pool1, kernel_size=5, stride=1,num_output=50, pad=0,weight_filler=dict(type='xavier'))
    #池化層
    pool2=L.Pooling(conv2, pool=P.Pooling.MAX, kernel_size=2, stride=2)
    #全連接層
    fc3=L.InnerProduct(pool2, num_output=500,weight_filler=dict(type='xavier'))
    #激活函數(shù)層
    relu3=L.ReLU(fc3, in_place=True)
    #全連接層
    fc4 = L.InnerProduct(relu3, num_output=10,weight_filler=dict(type='xavier'))
    #softmax層
    loss = L.SoftmaxWithLoss(fc4, label)
    if include_acc:             # test階段需要有accuracy層
        acc = L.Accuracy(fc4, label)
        return to_proto(loss, acc)
    else:
        return to_proto(loss)
def write_net():
    #寫(xiě)入train.prototxt
    with open(train_proto, 'w') as f:
        f.write(str(Lenet(train_list,batch_size=64)))
    #寫(xiě)入test.prototxt    
    with open(test_proto, 'w') as f:
        f.write(str(Lenet(test_list,batch_size=100, include_acc=True)))
#編寫(xiě)一個(gè)函數(shù),生成參數(shù)文件
def gen_solver(solver_file,train_net,test_net):
    s=proto.caffe_pb2.SolverParameter()
    s.train_net =train_net
    s.test_net.append(test_net)
    s.test_interval = 938    #60000/64,測(cè)試間隔參數(shù):訓(xùn)練完一次所有的圖片,進(jìn)行一次測(cè)試  
    s.test_iter.append(500)  #50000/100 測(cè)試迭代次數(shù),需要迭代500次,才完成一次所有數(shù)據(jù)的測(cè)試
    s.max_iter = 9380       #10 epochs , 938*10,最大訓(xùn)練次數(shù)
    s.base_lr = 0.01    #基礎(chǔ)學(xué)習(xí)率
    s.momentum = 0.9    #動(dòng)量
    s.weight_decay = 5e-4  #權(quán)值衰減項(xiàng)
    s.lr_policy = 'step'   #學(xué)習(xí)率變化規(guī)則
    s.stepsize=3000         #學(xué)習(xí)率變化頻率
    s.gamma = 0.1          #學(xué)習(xí)率變化指數(shù)
    s.display = 20         #屏幕顯示間隔
    s.snapshot = 938       #保存caffemodel的間隔
    s.snapshot_prefix = root+'mnist/lenet'   #caffemodel前綴
    s.type ='SGD'         #優(yōu)化算法
    s.solver_mode = proto.caffe_pb2.SolverParameter.GPU    #加速
    #寫(xiě)入solver.prototxt
    with open(solver_file, 'w') as f:
        f.write(str(s))
#開(kāi)始訓(xùn)練
def training(solver_proto):
    caffe.set_device(0)
    caffe.set_mode_gpu()
    solver = caffe.SGDSolver(solver_proto)
    solver.solve()
#
if __name__ == '__main__':
    write_net()
    gen_solver(solver_proto,train_proto,test_proto) 
    training(solver_proto)

我將此文件放在根目錄下的mnist文件夾下,因此可用以下代碼執(zhí)行

sudo python mnist/mnist.py

在訓(xùn)練過(guò)程中,會(huì)保存一些caffemodel。多久保存一次,保存多少次,都可以在solver參數(shù)文件里進(jìn)行設(shè)置。

我設(shè)置為訓(xùn)練10 epoch,9000多次,測(cè)試精度可以達(dá)到99%

以上就是caffe的python接口之手寫(xiě)數(shù)字識(shí)別mnist實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于caffe python手寫(xiě)數(shù)字識(shí)別mnist的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例

    PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例

    今天小編就為大家分享一篇關(guān)于PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • python分?jǐn)?shù)實(shí)例用法

    python分?jǐn)?shù)實(shí)例用法

    在本篇文章里小編給大家整理的是一篇關(guān)于python分?jǐn)?shù)實(shí)例用法的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-04-04
  • python中re.findall函數(shù)實(shí)例用法

    python中re.findall函數(shù)實(shí)例用法

    在本篇文章里小編給大家整理了一篇關(guān)于python中re.findall函數(shù)實(shí)例用法相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-09-09
  • python模擬實(shí)現(xiàn)斗地主發(fā)牌

    python模擬實(shí)現(xiàn)斗地主發(fā)牌

    這篇文章主要為大家詳細(xì)介紹了python代碼模擬實(shí)現(xiàn)斗地主發(fā)牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • Python格式化輸出之format用法詳解

    Python格式化輸出之format用法詳解

    Python中格式化字符串目前有兩種陣營(yíng):%和format,這篇文章主要給大家介紹了關(guān)于Python格式化輸出之format用法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • Python中bytes和str的區(qū)別與聯(lián)系詳解

    Python中bytes和str的區(qū)別與聯(lián)系詳解

    Python3最重要的新特性之一是對(duì)字符串和二進(jìn)制數(shù)據(jù)流做了明確的區(qū),下面這篇文章主要給大家介紹了關(guān)于Python中bytes和str區(qū)別與聯(lián)系的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Python制作春聯(lián)的示例代碼

    Python制作春聯(lián)的示例代碼

    春聯(lián)是中國(guó)傳統(tǒng)文化中最具內(nèi)涵的元素之一,它以對(duì)仗工整、簡(jiǎn)潔精巧的文字描繪美好形象,抒發(fā)美好愿望,是中國(guó)特有的文學(xué)形式,是華人們過(guò)年的重要習(xí)俗。本文將通過(guò)Python制作春聯(lián),需要的可以參考一下
    2022-01-01
  • Django視圖、傳參和forms驗(yàn)證操作

    Django視圖、傳參和forms驗(yàn)證操作

    這篇文章主要介紹了Django視圖、傳參和forms驗(yàn)證操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Python編程pygame模塊實(shí)現(xiàn)移動(dòng)的小車示例代碼

    Python編程pygame模塊實(shí)現(xiàn)移動(dòng)的小車示例代碼

    這篇文章主要介紹了Python編程pygame模塊實(shí)現(xiàn)移動(dòng)的小車示例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python實(shí)現(xiàn)爬蟲(chóng)爬取NBA數(shù)據(jù)功能示例

    Python實(shí)現(xiàn)爬蟲(chóng)爬取NBA數(shù)據(jù)功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)爬蟲(chóng)爬取NBA數(shù)據(jù)功能,涉及Python針對(duì)URL模塊、字符串、列表遍歷、Excel寫(xiě)入等相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05

最新評(píng)論