用caffe训练并测试自己收集的数据,踩了一堆坑

来源:互联网 发布:韩国电视直播软件 编辑:程序博客网 时间:2024/05/17 23:22


先上一张图,聊做安慰。 (虽然事实上,哈哈。。。。。。。)




1.数据准备:

数据是自己收集的数据,两个人,一只猫,总共有三个分类,每个分类有5000张左右的图片。(事实上,我手动采集不了这么多图片,所以,每个分类采集了50张,然后就在自己的分类中不断复制,粘贴。就粘贴了5000张,所以其实每一个图片都有100张或200张一模一样的图片。)


data文件夹里有三个文件夹,分别是

d(存放男士A的所有照片)

g(存放女士B的所有照片)

c(存放猫咪c的所有照片)

先用new_name.py 统一下命名d_1.jpg    ..................   d_5000.jpg 和 g_1.jpg    ..................   g_5000.jpg  和 

c_1.jpg    ..................   c_5000.jpg    


配置和执行文件:

new_name.py  是自己写的

import os


a='miao'
path='/home/thm/caffe-master/face16/data/%s'%a
names = os.listdir(path)
os.chdir(path)
i=0
#for name in names:
 #   print(name)
    
for num in range(len(names)):
    print(names[num])
    os.rename(names[num],'%s'%a+'_'+str(num)+'.jpg')

gettxt.py 是在网上找别人的,

gettxt.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os


root = os.getcwd() 
data = '/data'  
path = os.listdir(root+'/'+ data) 
path.sort()
vp = 0.1 
ftr = open('train.txt','w')
fva = open('val.txt','w')
i = 0


for line in path:
    subdir = root+'/'+ data +'/'+line
    childpath = os.listdir(subdir)
    mid = int(vp*len(childpath))
    for child in childpath[:mid]:
        subpath = data+'/'+line+'/'+child;
        d = ' %s' %(i)
        t = subpath + d
        fva.write(t +'\n')
    for child in childpath[mid:]:
        subpath = data+'/'+line+'/'+child;
        d = ' %s' %(i)
        t = subpath + d
        ftr.write(t +'\n')
    i=i+1
ftr.close()  
fva.close() 


自己使用的时候简单改了一下。(感谢分享)


create_imagenet.sh    和    make_imagenet_mean.sh  都是从caffe-master/examples/imagenet  里复制出来的

train_caffenet.sh  和  solver.prototxt  和   train_val.prototxt 是从 caffe-master/models/bvlc_reference_caffenet/ 里复制出来的


2.测试开始

(1)、用gettxt.py生成用于训练和测试的名字序列:


就是用“1”,生成“2. 3”


(2)、用create_imagenet.sh1生成用于训练和测试的两个lmdb2,3文件。需要修改文件

(如需要,还可以用make_imagenet_mean.sh 生成均值文件。需要修改文件


[踩坑,因为前期生成的名字序列是这样的:./data/d/d_10.jpg。然而训练的时候是在caffe的根目录进行的,所以导致读图片是少了一个文件夹face16的过渡,就“ 暗暗的 没有生成lmdb”,导致训练出现有路径问题,图片尺寸问题。~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~然后为解决所谓的{路径、尺寸} 问题,我还百度了一堆东西,然并卵,根据方法修改之后并没有用]


[再踩坑,训练时出现的问题还有net .原因是路径,之后把一切路径都设成绝对路径,然后只要在solver 和 train_val 中出现路径,就再三核对需不需要改路径。谨慎一点总不会错]


(3)、修改配置和执行文件


train_caffenet.sh 文件可以不需要 

solver.prototxt 是执行的配置文件,可以在其中根据自己的需要修改执行和训练的参数。

train_val.prototxt是具体的layer层的配置文件,包括每一层用什么算法,算法参数的大小等。


上一张别人的“solver.prototxt”配置,作者写的是每一个分类是5000张,因为我和其数据大小类似,就裁取了类似的大小配置。这里放这张图是为了备注下注释。

图的来源:http://www.cnblogs.com/alexcai/p/5469436.html


train_val.prototxt里重点需要改的是mean_file,(均值文件路径)   source, (lmdb 的文件路径)   batch_size, (设为50,不然出现cudunsuccess)   num_output(这个是改为自己的分类个数,我写的是3)  

哈哈哈,颜色真好玩

train_caffenet.sh 文件需要修改的是solver 的路径


(真诚的提醒自己,不用硬性的仿照别人的东西改自己的文件,一定要理解清楚自己改的文件所涉及的关系)


(4)、执行

改完配置的文件就该执行了,不出错是最好的,出错了也可以让自己多了解下其中的运行规则和细节。

对于执行结果,需要关注的是两个 accuracy, loss  。前者正常情况下,总体训练迭代的越多,最后的值对比于最初的值减小的就越多。而loss不能一直变大,否则就是发散,需要调整参数。

我将solver.prototxt 改成这样:


训练产生的accuracy 和loss,就从0.35,1.02 变成了0.966,0.175.。哈哈,实测参数设置一定要合适。


这些是建立在我刚接触caffe 的客观基础上的,所以对caffe的体会可能有些偏颇甚至是错误,期待批评指正。

越接触算法,越觉得算法有趣,尤其是对开源的人萌发出崇高的敬意。


这是画的loss 和accuracy 的图。参数设置是重新进行修改过的,和上述的不一样

 


代码在这里: (是将别人的稍加修改  这是它人的原博客:http://www.cnblogs.com/denny402/p/5686067.html)

# -*- coding: utf-8 -*-


import matplotlib.pyplot as plt
import math  
import caffe
from numpy import *  
caffe.set_device(0)  
caffe.set_mode_gpu()   


solver = caffe.SGDSolver('/home/thm/caffe-master/face16/solver.prototxt')  
  
  
niter = 5000 
  
display= 100  
  


test_iter = 100  
 
test_interval =500  
  
 
train_loss = zeros(math.ceil(niter * 1.0 / display))   
test_loss = zeros(math.ceil(niter * 1.0 / test_interval))  
test_acc = zeros(math.ceil(niter * 1.0 / test_interval))  
  
  
solver.step(1)  
  
  
_train_loss = 0; _test_loss = 0; _accuracy = 0  
 
for it in range(niter):  
      
    solver.step(1)  
     
    _train_loss += solver.net.blobs['loss'].data  
    if it % display == 0:  
          
        train_loss[it // display] = _train_loss / display  
        _train_loss = 0  
  
    if it % test_interval == 0:  
        for test_it in range(test_iter):  
              
            solver.test_nets[0].forward()  
              
            _test_loss += solver.test_nets[0].blobs['loss'].data  
              
            _accuracy += solver.test_nets[0].blobs['accuracy'].data  
          
        test_loss[int(it/test_interval)] = _test_loss / test_iter  
          
        test_acc[int(it / test_interval)] = _accuracy / test_iter  
        _test_loss = 0  
        _accuracy = 0  
  
 
print ('\nplot the train loss and test accuracy\n')  
_, ax1 = plt.subplots()  
ax2 = ax1.twinx()  
  
  
ax1.plot(display * arange(len(train_loss)), train_loss, 'g')  
  
ax1.plot(test_interval * arange(len(test_loss)), test_loss, 'y')  
 
ax2.plot(test_interval * arange(len(test_acc)), test_acc, 'r')  
  
ax1.set_xlabel('iteration')  
ax1.set_ylabel('loss')  
ax2.set_ylabel('accuracy')  
plt.show() 

阅读全文
0 0
原创粉丝点击