This Image_Classify_AlexNet_Caffe is using the Caffe framework, AlexNet network architecture is used to train the model and to classify the images through trained models.
The main process is shown in Fig. 1.
First, we need to prepare the images we want to learn from the model, and divide the images into different folders according to categories.
Then, through the py file in Image_Classify_AlexNet_Caffe, the three txt manifest files are automatically generated. In this way, let the model know which files are to be tested for training, and which categories are divided into these files. After completing these pre-processing, execute the training py file. After the training is completed, test the model that has been trained.
Fig. 1. Image_Classify_AlexNet_Caffe Image recognition process.
Step 1: Pre-processing – Prepare the training image
For this solution, we want the model to distinguish between two types of beverage packages, coffee and FIN, for which we need to prepare the images for training.
The two types of images are respectively classified by the folder, and the folder position is located in the Image in the data, as shown in Fig. 2.
Fig. 2. Training image folder.
Fig. 3. Image in folder COFFEE.
Fig. 4. Image in folder FIN.
Step 2: Preprocessing - create_label
- Purpose: Define the image position of 【Training】and 【Test】.
Step 1: Open the OpenR8 folder and execute R8.exe.
Under the OpenR8 folder, there are two bat files for executing R8.exe, namely R8_Python3.6_CPU.bat and R8_Python3.6_GPU.bat.
If the computer has no GPU, execute R8_Python3.6_CPU.bat.
If your computer has a GPU and your solution needs to use GPU acceleration, you can choose R8_Python3.6_GPU.bat, as shown in Fig. 5.
Fig. 5. execute bat run R8 software.
Step 2: Select and open the solution.
Open the first flow:1_create_label.
Fig. 6. Open solution 1_create_label.
Step 3: Execute the solution and generate label.txt, train.txt, val.txt.
Fig. 7. Execution solution.
Fig. 8. Generate three txt.
label.txt: The label that produces the classification.
The classification label is the same as the folder.
Fig. 9. label.txt.
train.txt: Generates a list of training.
The list is the image path to be trained and the corresponding classification. Taking this scheme as an example, the classification starts from 0, so if it is classified as COFFEE, it corresponds to 0, and if it is classified as FIN, it corresponds to 1.
Fig. 10. train.txt.
val.txt: Generates a list of tests.
Fig. 11. val.txt.
In 1_create_label, you can modify the parameters from Caffe_Classify_CreateTrainTxt to select what ratio of images to train and test, as shown in Fig. 12
Fig. 12. Change the training and test image ratio.
If you set TrainSplitRate to 1, all images in the folder will be converted to trained images.
Step 3: Training model – train.txt
The introduction is divided into two parts, the first describes how to start training, and then if you are interested in knowing that each process block can be viewed in the 2nd to describe the details of this process.
- Instructions for use: Training model.
First, use the “OpenR8” open “2_train.py” file to load “2_train”, such as Fig. 13, Fig. 14. Next, click "Run" to start the training model, and this step takes a little time to wait for the program to build the model. such as Fig. 15.
When this execution is completed, a trained model will be generated, and its model file generation will be placed, as shown in Fig. 16.
File name 1：xxx.Caffemodel
File name 2：xxx. Solverstate
If the appliance does not support the NVIDIA GPU. An error message appears. Please follow the Fig. 17, Fig. 18 operation.
Fig. 13. 2_train file path.
Fig. 14. load 2_train.
Fig. 15. Perform 2_train and its execution process.
Fig. 16. The production model is finished after training.
Fig. 17. No GPU error message Training.
Fig. 18. No GPU the way set it up - Training.
- 3_train introduction to processes and parameters:
►Caffe_ Init：Caffe framework Initialization.
【CaffeObject】The setting that follows the previous initialization.
【GPU】Whether the device used to train the model needs to use GPU acceleration, if so, change the value to all; Conversely, fill in the blanks. (If the selection is, make sure the appliance has a GPU)
【SolverPath】The main parameter. This is the input file “Solver.prototxt” required for Caffe training, which contains the number of training iterations, training parameters, and a list of samples. The following Fig. 19,Fig. 20.
【continueTrainModelPath】Continue training with pretrained models. Fill in the value of the model path that you want to train, and the model extension must be .caffemodel.
Fig. 19. 2_train - Caffe_Train.
Fig. 20. solver.prototxt content.
test_iter：test a few images at a time.
test_interval：train a few rounds to run a test.
base_lr：learning rate、the smaller the value, the more sophisticated the training.
max_iter：Set up to a few rounds and stop training.
snapshot：Set a few rounds to create a model.
Step 4: Inference using a trained model - inference
- Purpose: To test the accuracy of each stage of the model, to find a suitable model, to avoid over-fitting the training sample, and the test sample is not effective.
- Instructions for use:
(1) Test sample: 【Open OpenR8】 => 【Open and load 3_inference_images.py】=> 【Select the model to be tested】 => 【Execution flow】
Fig. 21. load 3_inference_images.py.
Fig. 22. Select the model to be tested.
Fig. 23. Select the picture folder to be tested.
Press 『RUN』 to execute 3_inference_images.py
Fig. 24. Run py.
Fig. 25. Implementation content.
How to use this example to execute the image you want to train
If you want to train your own images, we need to prepare the images for training. For example, we are preparing a set of gesture recognition,gestures 0~5, and images with no gestures.Then make a folder classification, as shown in Fig. 26.
Fig. 26. Image sample.
Fig. 27. Folder 0 (gesture 0).
Fig. 28. Folder 1 (gesture 1).
Fig. 29. Folder no(without gesture).
Move the finished image folder to dataimage, as shown in Fig. 30.
Fig. 30. Move the finished image folder to data/image.
It should be noted that since the original example has only two classifications, and the current test sample has 7 classifications, we need to make corresponding modifications to the deployment.prototxt and train.prototxt in the data folder.
Open train.prototxt, deploy.prototxt, and find the num_output parameter. This parameter is classified into several categories for the classification. Therefore, the 2 inside should be changed to the current number of classifications 7, as shown in Fig. 31,Fig. 32.
Fig. 31. Modification train.prototxt.
Fig. 32. Modification deploy.prototxt.
After the modification is completed, the py file in the solution is executed in the same order, and the training and generation model can be successfully completed.