Trainning SVM using HOG feature [closed]

asked 2016-06-15 09:10:00 -0600

Tuan Anh gravatar image

updated 2016-06-15 09:21:09 -0600

berak gravatar image

I am trying to train SVM to predict the traffic sign, but I always get the same predict result for difference input image.

Can anyone explain why for me?

Here is my code:

vector < Mat > storeInput;
vector < int > labels;


//load input image
char FullFileName[10000];
char FirstFileName[10000] = "./Prohibit_Blue/prohibit_blue";
int fileNum = 4;
for (int i = 0; i< fileNum; ++i)
{
    //pos
    sprintf_s(FullFileName, "%s_%01d.jpg", FirstFileName, i + 1);
    printf("%s\n", FullFileName);

    //read image file  
    Mat img, img_gray, dst, feature;
    img = imread(FullFileName);

    //resizing  
    resize(img, img, Size(64, 64));

    //convert to grayscale
    cvtColor(img, img_gray, CV_BGR2GRAY);

    storeInput.push_back(img_gray);
    labels.push_back(i);
    imshow("prohibit_blue", img_gray);

    waitKey(5);
}


//compute HOG
cout << "Start hog...";
Size size = Size(64, 64);
Size block_size = Size(size.width / 4, size.height / 4);
Size block_stride = Size(size.width / 8, size.height / 8);
Size cell_size = block_stride;
int num_bins = 9;
HOGDescriptor hog(size, block_size, block_stride, cell_size, num_bins);
Mat inputHOG;
vector< Point > location;
vector< float > descriptors;

for (int i = 0; i < storeInput.size(); i++)
{
    inputHOG = storeInput[i];
    hog.compute(inputHOG, descriptors, Size(0, 0), Size(0, 0), location);
    //cout << "descriptors size: " << descriptors.size() << endl;

    gradient_lst.push_back(Mat(descriptors).clone());

}
cout << "...[done]" << endl;


//trainning with SVM
Mat trainData;

//--Convert data before trainning
const int rows = (int)train_samples.size();
const int cols = (int)std::max(train_samples[0].cols, train_samples[0].rows);
cv::Mat tmp(1, cols, CV_32FC1); //< used for transposition if needed
trainData = cv::Mat(rows, cols, CV_32FC1);
vector< Mat >::const_iterator itr = train_samples.begin();
vector< Mat >::const_iterator end = train_samples.end();
for (int i = 0; itr != end; ++itr, ++i)
{
    if (itr->cols == 1)
    {
        transpose(*(itr), tmp);
        tmp.copyTo(trainData.row(i));
    }
    else if (itr->rows == 1)
    {
        itr->copyTo(trainData.row(i));
    }
}


cout << "Start training...";

Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);                                           // classify more than 2 class
svm->setKernel(SVM::LINEAR);                                       
svm->setC(10);
svm->setTermCriteria(cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, 1e-6));

svm->train(trainData, ROW_SAMPLE, Mat(labels));

cout << "...[done]" << endl;
svm->save("./genfiles/trainSVM_Recog_blue.xml");
cout << "Save XML ..... done!!!" << endl;


//test image

Mat testImg = imread("./Prohibit_Blue/prohibit_blue_1.jpg");
//resizing
resize(img, img, Size(64, 64));
//gray
cvtColor(img, img_gray, COLOR_BGR2GRAY);

//Extract HogFeature
Size size = Size(64, 64);
Size block_size = Size(size.width / 4, size.height / 4);
Size block_stride = Size(size.width / 8, size.height / 8);
Size cell_size = block_stride;
int num_bins = 9;
HOGDescriptor hog(size, block_size, block_stride, cell_size, num_bins);
vector< float> descriptorsValues;
vector< Point> locations;

hog.compute(img_gray, descriptorsValues, Size(0, 0), Size(0, 0), locations);

//vector to Mat
Mat fm = Mat(descriptorsValues);

//Classification data
Ptr<SVM> svm2 = Algorithm::load<SVM>("./genfiles/trainSVM_Recog_blue.xml");
std::cout << "Model Loaded" << std::endl;

Mat image1d(1, fm.rows, CV_32FC1);
imshow("gradient 1", image1d);

float result = svm2->predict(image1d);
cout << "Predict value: " << result << endl;
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-08 02:05:54.369689

Comments

4 classes, but 4 train images only ? try 100 per class.

also, try svm->trainAuto()

berak gravatar imageberak ( 2016-06-15 09:23:13 -0600 )edit

i have to try 2 solution you proposed, but the result are no change. I also print and compare the HOG value of test image and trainning image, it is the same, but why the predict result always are the same value.

Tuan Anh gravatar imageTuan Anh ( 2016-06-15 21:30:21 -0600 )edit