4/18/2014

To test SVM trained data is whether reliable or not. (example source code)

After training SVM, we should test the trained XML data is reliable or not..

The method to extract HOG feature is refer to -> http://feelmare.blogspot.kr/2014/04/example-source-code-of-extract-hog.html
The method to training SVM of HOG feature is refer to -> http://feelmare.blogspot.kr/2014/04/example-source-code-hog-feature-to.html

The method is using training data.
Again training data make HOG feature, and check the feature is positive or not using trained SVM data.

The example source code is like that.
...
#include < stdio.h>
#include < opencv2\opencv.hpp>
//#include < opencv2\gpu\gpu.hpp>

using namespace cv;
using namespace std;


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")         
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing        
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")        
//#pragma comment(lib, "opencv_features2d247d.lib")        
#pragma comment(lib, "opencv_highgui247d.lib")        
#pragma comment(lib, "opencv_ml247d.lib")      
//#pragma comment(lib, "opencv_stitching247d.lib");      
//#pragma comment(lib, "opencv_nonfree247d.lib");      
  
#else        
#pragma comment(lib, "opencv_core247.lib")        
#pragma comment(lib, "opencv_imgproc247.lib")        
#pragma comment(lib, "opencv_objdetect247.lib")        
//#pragma comment(lib, "opencv_gpu247.lib")        
//#pragma comment(lib, "opencv_features2d247.lib")        
#pragma comment(lib, "opencv_highgui247.lib")        
#pragma comment(lib, "opencv_ml247.lib")        
//#pragma comment(lib, "opencv_stitching247.lib");      
//#pragma comment(lib, "opencv_nonfree247.lib");      
#endif 


void main()
{

 //variables
 char FullFileName[100];
 char FirstFileName[100]="./images/upperbody"; //"./NegaImages/Negative";      // 
 int FileNum=96; //262;

 //Load trained SVM xml data
 CvSVM svm;
 svm.load("trainedSVM.xml");

 //count variable
 int nnn=0, ppp=0;

 for(int i=0; i< FileNum; ++i)
 {
  sprintf_s(FullFileName, "%s%d.png", FirstFileName, i+1);
  //printf("%s\n", FullFileName);

  //read image file
  Mat img, img_gray;
  img = imread(FullFileName);
  
  //resizing
  //resize(img, img, Size(16,8) ); //Size(64,48) ); //Size(32*2,16*2)); //Size(80,72) ); 
  resize(img, img, Size(64,48) ); //Size(32*2,16*2)); //Size(80,72) ); 
  //gray
  cvtColor(img, img_gray, CV_RGB2GRAY);

  //Extract HogFeature
  HOGDescriptor d( Size(32,16), Size(8,8), Size(4,4), Size(4,4), 9);
  vector< float> descriptorsValues;
  vector< Point> locations;
  d.compute( img_gray, descriptorsValues, Size(0,0), Size(0,0), locations);
  //vector to Mat
  Mat fm = Mat(descriptorsValues);
  
  //Classification whether data is positive or negative
  int result = svm.predict(fm);
  printf("%s - > %d\n", FullFileName, result);

  //Count data
  if(result == 1)
   ppp++;
  else
   nnn++;

  //show image
  imshow("origin", img);

  waitKey(5);
 }

 printf(" positive/negative = (%d/%d) \n", ppp, nnn);

}
---


16 comments:

  1. μ•ˆλ…•ν•˜μ„Έμš”" 또 μ§ˆλ¬Έλ“œλ¦¬κ²Œ λ˜λ„€μš”;;
    μ˜μ–΄λ‘œ λ‹΅λ³€ν•΄μ£Όμ‹œλŠ”λ°;; μ§ˆλ¬Έμ„ 계속 ν•œκΈ€λ‘œ λ“œλ € μ£„μ†‘ν•©λ‹ˆλ‹€;;

    λΈ”λ‘œκ·Έμ— μ„€λͺ…ν•΄μ£Όμ‹ λŒ€λ‘œ 따라 ν•˜λ©΄μ„œ trainedSVM.xml νŒŒμΌμ„ 얻을 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
    μœ„ xmlνŒŒμΌμ„ μ‚¬μš©ν•΄μ„œ λ³΄ν–‰μžλ₯Ό κ²€μΆœν•΄λ³΄κ³ μž ν•©λ‹ˆλ‹€. κ·Έλž˜μ„œ

    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
    --> hog.load("trainedSVM.xml");
    getDefaultPeopleDetector()λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  ν•™μŠ΅ν•œ xml을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œ
    μœ„μ™€ 같이 μˆ˜μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€. 그리고 hog.detectMultiScale()을 μˆ˜ν–‰ν•˜μ˜€λŠ”λ°
    μ—λŸ¬κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€;; 쀑간에 λ‹€λ₯Έ μ½”λ“œκ°€ ν•„μš”ν•œμ§€... μ•„λ‹ˆλ©΄... ν•™μŠ΅λœ xml데이터 포멧을 μˆ˜μ •ν•΄μ•Ό ν•˜λŠ”μ§€;; κΆκΈˆν•©λ‹ˆλ‹€...

    HOGDescriptor hog;
    hog.load("trainedSVM.xml");
    hog.detectMultiScale(matImage, Peds, 0, cv::Size(8, 8), cv::Size(0, 0), 1.05, 1);
    for(int i=0; i<(int)Peds.size(); i++)
    {
    rectangle(matImage, Peds[i], Scalar(0,255,0), 1);
    }
    이런 μ‹μœΌλ‘œ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

    OpenCV Error : Assertion failed (dsize.area() || (in_scale_x >0 && inv_scale_y > 0))
    in cv::resize, file ..\..\..\soureces\modules\imgproc\src\imgwarp.cpp, line 1835
    μ—λŸ¬λŠ” μœ„μ™€ 같이 λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€;;

    μ˜€λŠ˜λ„ 많이 λ°°μ›Œκ°‘λ‹ˆλ‹€. κ°μ‚¬ν•©λ‹ˆλ‹€.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. You need to convert "trainedSVM.xml" file learned by SVM to 1D vector for using function of hog.detectMultiscale().

      After training by SVM, use this source code.

      --
      //make firstly, inherited class to access alpha vector and value
      int svmVectorSize = svm.get_support_vector_count();
      int featureSize = pCol;
      //prepare, variables


      Mat sv = Mat(svmVectorSize, featureSize, CV_32FC1, 0.0);
      Mat alp = Mat(1, svmVectorSize, CV_32FC1, 0.0);
      Mat re = Mat(1, featureSize, CV_32FC1, 0.0);
      Mat re2 = Mat(1, featureSize+1, CV_32FC1, 0.0);



      //set value to variables
      for(int i=0; i< svmVectorSize; ++i)
      memcpy( (sv.data + i*featureSize), svm.get_support_vector(i), featureSize*sizeof(float) ); //ok


      double * alphaArr = svm.get_alpha();
      int alphaCount = svm.get_alpha_count();

      for(int i=0; i< svmVectorSize; ++i)
      {
      alp.at< float>(0, i) = (float)alphaArr[i];
      //printf("alpha[%d] = %lf \n", i, (float)alphaArr[i] );
      }

      //cvMatMul(alp, sv, re);
      re = alp * sv;

      for(int i=0; i< featureSize; ++i)
      re2.at< float>(0,i) = re.at< float>(0,i) * -1;
      re2.at< float>(0,featureSize) = svm.get_rho();

      //save to 1d vector to XML format!!
      FileStorage svmSecondXML(SVM_HOGDetectorFile, FileStorage::WRITE);
      svmSecondXML << "SecondSVMd" << re2 ;

      svmSecondXML.release();

      --

      Delete
    2. SVM_HOGDetectorFile means new name of xml file.
      ex) AA.xml

      thank you

      Delete
    3. λ‹΅λ³€ν•΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€!!
      μ—ΌμΉ˜μ—†μ§€λ§Œ...;; 또 μ§ˆλ¬Έλ“œλ¦½λ‹ˆλ‹€;;
      http://feelmare.blogspot.kr/2014/04/example-source-code-hog-feature-to.html
      λΈ”λ‘œκ·Έμ˜ μœ„ λ‚΄μš©μ— μ΄μ–΄μ„œ μ˜¬λ €μ£Όμ‹  μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ˜€κ³ ,
      μ €λŠ” opencv 2.4.9. 버전을 μ‚¬μš©ν•΄μ„œ lib만 2.4.7μ—μ„œ 2.4.9. 둜 λ°”κΏ”μ„œ 예제λ₯Ό μ‹€ν–‰λ³΄μ•˜μŠ΅λ‹ˆλ‹€

      μœ„ μ½”λ“œμ—μ„œ svmꡬ쑰체의 멀버(?)λ₯Ό 읽지 λͺ»ν•˜λŠ” 뢀뢄이 λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€. svm.get_alpha(), svm.get_alpha_count(), svm.get_rho()
      이 μ„Έ κ°€μ§€μž…λ‹ˆλ‹€.

      그리고 SVM_HOGDetectorFile이 μƒˆλ‘œμš΄ *.xml파일의 이름이라고 ν•˜μ…¨λŠ”
      데, 이뢀뢄도 μ—λŸ¬κ°€ λ°œμƒν•©λ‹ˆλ‹€;;

      λ„μ›€μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€""

      Delete
    4. I'm sorry, I don't teach you exact description.
      I will post about the article(hog.detectMultiscale) tomorrow.
      Thank you.


      Delete
    5. λ„€" λ‹΅λ³€ κ°μ‚¬ν•©λ‹ˆλ‹€""

      Delete
    6. refer to this page
      http://feelmare.blogspot.kr/2014/11/opencv-svm-learning-method-and-xml.html
      thank you.

      Delete
    7. Hey JH, I still got the problem "OpenCV Error : Assertion failed (dsize.area() || (in_scale_x >0 && inv_scale_y > 0))
      in cv::resize, file ..\..\..\soureces\modules\imgproc\src\imgwarp.cpp, line 1835" after used 1D vector.
      you have any suggestion?

      Delete
  3. Anonymous25/2/16 09:54

    Hey i have problem , when i get to svm.predict it throw me some exception. Looks like its even don t load svm from xml.
    Any idea why ? I just copypaste your codes, even extracting hog and train svm

    ReplyDelete
    Replies
    1. Could you show your code or send source code to me?
      Thank you.

      Delete
    2. Anonymous26/2/16 14:35

      Oh... i find out that i changed size of hog descriptor in training source code(that was the problem - i am new in this so i am littlebit confused in some things)...
      Just if i could ask another things:
      Should be training images for svm in same aspect ratio ? I saw in source codes befor ... that you resizing them, so i gues it should be in same ratio as you resize them.Am i right ?
      Nex question: after i have converted it in 1d vector like in upper comments i just load big image , use detectmultiscale and it returns some vector of rectangles(or what is return of detectmultiscale)? Didn t find topic of it here, but maybe i am blind.
      Sorry for too much questions. By the way a really appreciate this site and all codes here. I learned much more than anywhere else.
      Thank you

      Delete
    3. Anonymous26/2/16 14:41

      Sorry there is topic, but with gpu, so i will check it

      Delete
  4. Hi, how can I use this algorithm to see exactly what kind of images I have ? For example If I trained 30 photo with cars, 30 with planes and 30 with houses, and another 100 negative images, when I read an image with a car to figure out that in my pictures I have a car. Here we find out only if it is a positive picture or negative. Thanks.

    ReplyDelete
  5. μ•ˆλ…•ν•˜μ„Έμš” μ €λŠ” μ˜μƒμ²˜λ¦¬λ₯Ό 배우고 μžˆλŠ” ν•™μƒμž…λ‹ˆλ‹€.
    SVM ν•™μŠ΅μ„ 끝내고 xmlνŒŒμΌμ„ setSVMDetector()에 적용 μ‹œν‚€κ³  싢은데 κ·Έ 뢀뢄이 μ–΄λ ΅μŠ΅λ‹ˆλ‹€.
    μ˜¬λ¦¬μ‹  μ†ŒμŠ€ μ½”λ“œλ₯Ό 보면 svm["SecondSVMd"]λΌλŠ” 것을 xMat에 넣은 ν›„ vector에 dataλ₯Ό μ €μž₯ν•˜μ‹œλ˜λ° μ €λŠ” xmlνŒŒμΌμ„ μƒμ„±ν• λ•Œ svm.saveλ₯Ό μ΄μš©ν•΄μ„œ "SecondSVMd"λΌλŠ” 것이 xmlνŒŒμΌμ— μ—†μŠ΅λ‹ˆλ‹€.
    ν˜Ήμ‹œ 이런 κ²½μš°μ— ν•™μŠ΅μ‹œν‚¨ 데이터λ₯Ό μ–΄λ–»κ²Œ 뢈러올 수 μžˆλŠ”μ§€ μ•Œλ €μ£Όμ‹€μˆ˜ μžˆλ‚˜μš”..? λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete