I. BACKGROUND
Originally intended to be based on the "DEX deep expectation of apparent-a single image" for the surface age training, can be due to the Imdb-wiki data set is large, uneven distribution of all ages, difficult to divide the training set and validation set. Later, in order to first run through the main part of the training process, directly with the lap data set, reference Caffe Finetune_flickr_style, some parameter modification, the use of Bvlc_reference_ Caffenet.caffemodel complete the age estimate of finetune.
Second, training data set preparation
1. Download lap datasets, including train, Validation, Test, and corresponding age label,http://chalearnlap.cvc.uab.es/dataset/18/description/, to register.
2. Convert the marked CSV file to the TXT format recognized by Caffe. The information for each row of the CSV is: Picture name, age, standard deviation. When training does not need the standard deviation information, we simply write the picture name and the age to TXT, and by the space separates, obtains Train.txt as follows:
Similarly, the completion of the validation set CVS file conversion, get Validation.txt.
Iii. models and copies of related documents
1, copy pre-trained VGG16 model Caffe\models\bvlc_reference_caffenet\bvlc_reference_caffenet.caffemodel to the working directory, the file about 232M;
2, copy Caffe\models\finetune_flickr_style folder Deploy.prototxt, Solver.prototxt, train_val.prototxt to the working directory;
3, copy imagenet the mean value file Caffe\data\ilsvrc12\imagenet_mean.binaryproto to the working directory.
Iv. Modification of parameters
1, modify the Train_val.prototxt
And the final output layer number, because we want to train for [0,100] years of output, a total of 101 classes, so:
2, modify the Solver.protxt
3. Modify the deployment file for the actual test deploy.protxt
The number of output layers should also be changed:
V. Start of training
1. New Train.bat
Caffe Train-solver Solver.prototxt---solver solver.prototxt--snapshot snapshot/bvlc_iter_48000. Solverstatepause
Double-click to start training, when the training process unexpected interruption, you can comment on the first line, close the second line of comments, according to the actual situation to modify the save, continue to double-click training.
My Computer CPU is i5 6500, graphics card for gtx1050ti,8g memory, roughly to train 10 hours, midway also appeared some memory shortage training termination situation.
2. End of training
Vi. Evaluation of models
Age estimation is originally a linear problem, not a clear classification problem, people can not accurately get someone's age, not to mention the machine. So the evaluation of the age classification model can not be easily measured by the accuracy of the Mae (mean absolute error) and ε-error to measure, which
1, the validation set Validation.txt all the pictures to predict
With the help of https://github.com/eveningglow/age-and-gender-classification, the environment can be set up for reference https://www.cnblogs.com/smbx-ztbz/p/9399016.html
Modify the main function
intSplit (std::stringSTR, std::stringPattern, STD::VECTOR<STD::string> &words) {std::string:: Size_type pos; STD::stringWord; intnum =0; STR+=pattern; STD::string:: Size_type size =str.size (); for(Auto i =0; i < size; i++) {pos=Str.find (pattern, i); if(pos = =i) {Continue;//If first string is pattern } if(Pos <size) {Word= Str.substr (i, POS-i); Words.push_back (word); I= pos + pattern.size ()-1; Num++; } } returnnum;}//param example:model/deploy_age2.prototxt model/age_net.caffemodel model/mean.binaryproto img/0008.jpgintMainintargcChar*argv[]) { if(ARGC! =5) {cout<<"Command shoud is like ..."<<Endl; cout<<"ageandgenderclassification"; cout<<"\ "Age_net_model_file_path\" \ "age_net_weight_file_path\" \ "mean_file_path\" \ "test_image\""<<Endl; Std::cout<<"argc ="<< argc <<Std::endl; GetChar (); return 0; } //Get each file path stringAge_model (argv[1]); stringAge_weight (argv[2]); stringMean_file (argv[3]); //string Test_image (Argv[4]); //probability vectorVector<dtype>Prob_age_vec; //Set ModeCaffe::set_mode (CAFFE::GPU); //Make agenetagenet age_net (Age_model, Age_weight, mean_file); //initiailize both Netsage_net.initnetwork (); //read the name of the picture you want to testStd::ifstream Fin ("E:\\caffe\\dex_age_gender_predict\\lap2\\validation.txt"); STD::stringLine ; Std::vector<STD::string>test_images; Std::vector<int>Test_images_age; while(!fin.eof ()) {Std::getline (fin, line); Std::vector<STD::string>words; Split (line," ", words); Test_images.push_back (words[0]); Test_images_age.push_back (atoi (words[1].c_str ())); } std::cout<<"test_images size ="<< test_images.size () <<Std::endl; Std::ofstream Fout ("E:\\caffe\\dex_age_gender_predict\\lap2\\validation_predict.txt"); for(intK =0; K < Test_images.size (); ++k) {std::cout<<"k ="<< k <<Std::endl; STD::stringTest_image; Test_image=Test_images[k]; //classify and get probabilitiesMat test_img =Imread (Test_image, Cv_load_image_color); intAge =age_net.classify (test_img, Prob_age_vec); //Print result and show image//std::cout << "Prob_age_vec size =" << prob_age_vec.size () << Std::endl; //for (int i = 0; i < prob_age_vec.size (); ++i) {//std::cout << "[" << i << "] =" << prob_age_vec[i] << Std::endl; //} //Dtype prob; //int index; //get_max_value (Prob_age_vec, prob, index); //std::cout << "prob =" << prob << ", index =" << index << Std::endl; //imshow ("Ageandgender", test_img); //waitkey (0);Fout << Test_images[k] <<" "<< Test_images_age[k] <<" "<< Age <<Std::endl; } std::cout<<"finish!"<<Std::endl; GetChar (); return 0;}
My command parameters are: E:\caffe\DEX_age_gender_predict\lap2\deploy.prototxt E:\CAFFE\DEX_AGE_GENDER_PREDICT\LAP2\SNAPSHOT\BVLC _iter_50000.caffemodel Model\mean.binaryproto img\0008.jpg
Can be modified according to the actual situation. can get validation_predict.txt file. Running may run as a result of low memory outages and may run multiple times in batches.
2. Calculate Mae and Ε-error
(1) Copy the Validation_predict.txt file and the validation set's labeling file Reference.csv to the new VS project's working directory;
(2) Calculation
#include <iostream>#include<string>#include<fstream>#include<vector>intSplit (std::stringSTR, std::stringPattern, STD::VECTOR<STD::string> &words) {std::string:: Size_type pos; STD::stringWord; intnum =0; STR+=pattern; STD::string:: Size_type size =str.size (); for(Auto i =0; i < size; i++) {pos=Str.find (pattern, i); if(pos = =i) {Continue;//If first string is pattern } if(Pos <size) {Word= Str.substr (i, POS-i); Words.push_back (word); I= pos + pattern.size ()-1; Num++; } } returnnum;}intMainintargcChar**argv) { //u, Sigma, xstd::vector<int>u; Std::vector<float>Sigma; Std::vector<int>predict; STD::stringLine ; Std::ifstream Csv_file ("Reference.csv"); while(!csv_file.eof ()) {std::getline (csv_file, line); Std::vector<STD::string>words; Split (line,";", words); U.push_back (atoi (words[1].c_str ())); Sigma.push_back (Atof (words[2].c_str ())); } Std::ifstream Predict_file ("Validation_predict.txt"); while(!predict_file.eof ()) {std::getline (predict_file, line); Std::vector<STD::string>words; Split (line," ", words); Predict.push_back (atoi (words[2].c_str ())); } if(U.size ()! =predict.size ()) {Std::cout<<"u.size ()! = Predict.size ()"<<Std::endl; GetChar (); return-1; } //MAE intSum_err =0; floatMAE =0; for(inti =0; I < u.size (); ++i) {sum_err+ = ABS (u[i)-Predict[i]); } MAE= static_cast<float> (SUM_ERR)/u.size (); Std::cout<<"MAE ="<< MAE << Std::endl;//11.7184//Esro-errorstd::vector<float>errors; floatErr =0; floatError =0.0; for(inti =0; I < u.size (); ++i) {err=1.0-Exp (-1.0* (Predict[i]-u[i]) * (Predict[i]-u[i])/(2* Sigma[i] *sigma[i])); Errors.push_back (ERR); Error+=err; } Error/=errors.size (); Std::cout<<"error ="<< error << Std::endl;//0.682652Std::cout<<"finish!"<<Std::endl; GetChar (); return 0;}
Finally, Mae is 11.7184,ε-error for 0.682652.
Seven, the actual application of the forecast
1, the input image can be estimated using the classification tool provided by Caffe
Classification Deploy.prototxt Snapshot\bvlc_iter_50000.caffemodel Imagenet_mean.binaryproto. \age_labels.txt. \test_image\test_3.jpgpause
Among them, Age_labels.txt is 0-100 label description information, each label corresponds to a row, a total of 101 lines, my wording is as follows:
End
Age training and estimation using lap datasets