[OpenR8 solution] Image_Classify_VGG16_Caffe(Using VGG16 algorithm and Caffe framework for object classification)
  1. Image_Classify_VGG16_Caffe


This Image_Classify_VGG16_Caffe is using the Caffe framework, VGG16 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_VGG16_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_VGG16_Caffe Image recognition process.png

Fig. 1.Image_Classify_VGG16_Caffe Image recognition process.



  1. Step 1: Pre-processing – Prepare the training image


For this solution, we want the model to identify the age of the portrait in the image, so we need to prepare the images for training.


Use folders to distinguish between portraits of different ages and pure background images, and the folder position is located in the Image in the data, as shown in Fig. 2. 


Fig. 2.Training image folder.png

Fig. 2.Training image folder.


Fig. 3.Image in folder 20.png

Fig. 3.Image in folder 20.


Fig. 4.Image in folder 30.png

Fig. 4.Image in folder 30.


Fig. 5.Image in folder background.png

Fig. 5.Image in folder background.



  1. Step 2: Preprocessing - create_label


Fig. 5.1Image in folder background.png


  1. Purpose:

Define the image position of 【Training】and 【Test】.


  1. Instructions:


Step 1: Open the OpenR8 folder and execute the OpenR8.

Under the OpenR8 folder, there are two bat files for executing the OpenR8, 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. 6.


Fig. 6.execute bat run the OpenR8.png

Fig. 6.execute bat run the OpenR8.


Step 2: Select and open the solution.


Open the first py:1_create_label.


Fig. 7.Open solution 1_create_label.png

Fig. 7.Open solution 1_create_label.


Step 3: Execute the solution and generate label.txt, train.txt, val.txt.


Fig. 8.Execution solution.png

Fig. 8.Execution solution.


Fig. 9.Generate three.png

Fig. 9.Generate three txt.


label.txt: The label that produces the classification.


The classification label is the same as the folder.


Fig. 10.label.png

Fig. 10.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 20, it corresponds to 0, and if it is classified to 30, it corresponds to 1.


Fig. 11.train.png

Fig. 11.train.txt.


val.txt: Generates a list of tests.


Fig. 12.val.png

Fig. 12.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. 13


Fig. 13.Change the training and test image ratio.png

Fig. 13.Change the training and test image ratio.


If you set TrainSplitRate to 1, all images in the folder will be converted to trained images.



  1. Step 3: Training model – train.txt


Fig. 13.1Change the training and test image ratio.png


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.


  1. Instructions for use: Training model.

First, use the “OpenR8” open “2_train.py” file to load “2_train”, such as Fig. 14,Fig. 15. 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. 16.

When this execution is completed, a trained model will be generated, and its model file generation will be placed, as shown in Fig. 17.


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. 18,Fig. 19 operation.


Fig. 14.2_train file path.png

Fig. 14.2_train file path.


Fig. 15.load 2_train.png

Fig. 15.load 2_train.


Fig. 16.Perform 2_train and its execution process.png

Fig. 16.Perform 2_train and its execution process.


Fig. 17.The production model is finished after training.png

Fig. 17.The production model is finished after training.


Fig. 18.No GPU error message - Training.png

Fig. 18.No GPU error message - Training.


Fig. 19.No GPU the way set it up - Training.png

Fig. 19.No GPU the way set it up - Training.


  1.  3_train introduction to processes and parameters:

Caffe_ Init:Caffe framework Initialization.


Caffe_ Train:

【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. 20,Fig. 21.


【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. 20.2_train - Caffe_Train.png

Fig. 20.2_train - Caffe_Train.


Fig. 21.solver.prototxt content.png

Fig. 21.solver.prototxt content.


Parameter Description:


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.



  1. Step 4: Inference using a trained model - inference


  1. 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.


  1. Instructions for use:

(1) Test sample: 【Open OpenR8】 => 【Open and load 3_inference_images.py】=> 【Select the model to be tested】 => 【Execution py】


Fig. 22.load 3_inference_images.png

Fig. 22.load 3_inference_images.py.


Fig. 23.Select the model to be tested.png

Fig. 23.Select the model to be tested.


Fig. 24.Select the picture folder to be tested.png

Fig. 24.Select the picture folder to be tested.


Press 『RUN』 to execute 3_inference_images.py


Fig. 25.Run py.png

Fig. 25.Run py.


Fig. 26.Implementation content.png

Fig. 26.Implementation content.


Recommended Article

OpenR8 Community Edition - AI Software for Everyone (Free Download)