windows下利用caffe训练自己的图像分类模型

(不赘述)配置caffe环境,编译,会得到convert_imageset.exe、compute_image_mean.exe、caffe.exe等可执行文件

一、生成待训练图片的路径名称和标签文件(.txt)、测试图片的路径名称和标签(.txt)。

 图像放在其所属标签文件夹下会实现起来比较方便,

参考python代码, 

import os

def getFile_name(file_dir):
    L=[]
    for root, dirs,files in os.walk(file_dir):
        for file in files:
            # os.path.splitext()函数将路径拆分为文件名+扩展名
         #   print(root)
            if os.path.splitext(file)[1]=='.bmp':
                L.append( root.split('\\')[-1]+'\\'+file+' '+root.split('\\')[-1]  )
                #print( root.split('\\')[-1]+'\\'+file+' '+root.split('\\')[-1] )
    return (L)

L=getFile_name(r"F:\dataSet\train\Number")
doc=open(r"F:\dataSet\train\trainimgpathAndlabel.txt",'w')
for str in L:
    print(str,file=doc)
doc.close()

二、生成lmdb文件

1.生成训练图像的lmdb文件:

打开dos,进入编译生成convert_imageset.exe的所在文件夹,

命令:convert_imageset.exe  --shuffle   --resize_height=256   --resize_width=256  F:/dataSet/train/Number/  F:/dataSet/train/trainimgpathAndlabel.txt  F:/dataSet/train/db/

第一个参数为你训练图片保存的目录,第二个参数生成的txt,第三个是生成的lmdb文件保存的目录(注意之前不能有该db文件夹)
2. 生成测试图像的lmdb文件:

命令:convert_imageset.exe  --shuffle   --resize_height=256   --resize_width=256  F:/dataSet/test/Number/  F:/dataSet/test/testimgpathAndlabel.txt  F:/dataSet/test/db/

三、生成mean文件(因有网络参数配置里面没有mean文件的路径,加上这个有助于模型效果的提升)

命令:compute_image_mean.exe  F:/dataSet/train/db/  F:/dataSet/train/meanfile/mean.binaryproto

第一个参数是上一步的lmdb文件,第二个路径是输出的mean文件

四:训练

 选择合适的网络,配置网络参数,主要3个文件 solver.prototxt、deploy.prototxt、train_val.prototxt(在相应的文件夹下,建议复制到另外的文件夹下,避免破坏原文件)

具体含义和配置参见博文,http://www.cnblogs.com/david97/p/8961850.html

命令:caffe.exe train -solver F:/dataSet/train/solver.prototxt

-------------等待,训练完毕---------------------

若训练中断,接着训练(前提是有设置自动保存):

dos 命令: caffe.exe  train --solver=F:/dataSet/train/deploy.prototxt  --snapshot=F:/dataSet/proto_file/caffenet_train_iter_5000.solverstate

       即在后面加上--snapshot=......(你的solverstate文件位置)/caffenet_train_iter_5000.solverstate。
 

参考文献:

http://www.cnblogs.com/david97/p/8961850.html

猜你喜欢

转载自blog.csdn.net/ganwenbo2011/article/details/87712774