Use caffe to train your own pictures and complete picture classification

Briefly describe the process:

1. Get the picture data.

2. Use the python program to generate train.txt and test.txt. Each line in the two txt is the picture name + space + category (the category is represented by numbers)

3. Use script to convert to lmdb format

4. Modify the model parameters, prototxt file, training, get caffemodel

5. Run a C ++ program in opencv to call the trained model and output the test result (picture class name)

detailed steps:

https://www.cnblogs.com/david97/p/8961850.html  
caffe training stage, I am referring to this blog, except that I did not follow this blog when I first started to obtain train.txt and test.txt, all other photos do

It is recommended to refer to this blog. The following is my summary of the main steps: (The path is modified to your own)

1. Get the picture data and put the pictures in the train and test folders.

2. Use the python script to get the file name, get the picture name into train.txt and test.txt, according to the file name, use the search and replace function to write a label after the file name, 0 1 (must start from 0, if there are three For each category, use 0 1 2) and put it in the create_img_db folder

Python program to get file name to txt file: (change the path to your own) 

import os

def ListFilesToTxt(dir, file, wildcard, recursion):
    exts = wildcard.split(" ")
    files = os.listdir(dir)
    for name in files:
        fullname = os.path.join(dir, name)
        if (os.path.isdir(fullname) & recursion):
            ListFilesToTxt(fullname, file, wildcard, recursion)
        else:
            for ext in exts:
                if (name.endswith(ext)):
                    file.write(name + "\n")
                    break

def Test():

    dir = "D:/images/total"

    outfile = "D:/images/total/imgName.txt"
    wildcard = ".txt .exe .dll .lib .bmp"

    file = open(outfile, "w")
    if not file:
        print ("cannot open the file %s for writing" % outfile)
    ListFilesToTxt(dir, file, wildcard, 1)

    file.close()

Test()

 

3. Modify and run create_db.sh (D: \ software_engineer \ caffe-master \ create_img_db), convert to lmdb format, at the same time you can convert the image size, and shuffle the order

(I started to use 126 * 82 data. At the end of the test, opencv reported an error, which could not be solved. Later, it was changed to 32 * 32 here, and in my simple example, reducing the picture did not affect the accuracy. In addition , The picture does not need to be changed during the final test)

4. Calculate the average value of the image (this step can not be done, I did not do it)

5. Prepare caffe files (train_val.prototxt, solver.prototxt, deploy.prototxt) in caffe-master \ examples \ testImg, and modify the parameters inside

6. Train in the d / software_engineer / caffe-master / Build / x64 / Release folder to open the command line (right click, git bash here),

执行./caffe.exe train --solver=D:/software_engineer/caffe-master/examples/testImg/solver.prototxt

7. Test (three methods)

1. opencv3.3 DNN module (easy to use), the code is in the next blog https://blog.csdn.net/sinat_41852207/article/details/88197593

2. (caffe comes with test classification tool)

3. Use Python program

4.c ++ interface I will not

 

 

Published 59 original articles · Likes46 · Visits 30,000+

Guess you like

Origin blog.csdn.net/sinat_41852207/article/details/88194744