Ask Your Question
0

opencv error: assertion failed (samples.cols == var_count && samples.type() == 5) in cv::ml:: etc.

asked 2018-08-17 04:47:55 -0500

Samjith888 gravatar image

updated 2018-08-17 05:06:24 -0500

I created a xml file by using svm . In my test program i have loaded that xml file and i use svm predict, but it showing the error. how can i correct this ?

int main(){
HOGDescriptor hog;
vector<float> featureVector;
Ptr<ml::SVM> svm = ml::SVM::create();

Mat testdata, feature;


Mat img = imread("C:/Users/Sam/Desktop/Test/1.jpg");


    hog.compute(img, featureVector, Size(32, 32), Size(0, 0));


    feature = Mat(featureVector).reshape(1, 1);
    testdata.push_back(feature);


    svm->load("Features.xml");

    svm->predict(testdata);

}

edit retag flag offensive close merge delete

Comments

can you please replace the (useless) screenshot with a text version of the error ? thank you .

berak gravatar imageberak ( 2018-08-17 04:58:24 -0500 )edit
1

@berak : the error is 'opencv error: assertion failed (samples.cols == var_count && samples.type() == 5) in cv::ml::SVMImpl::predict, file c:\opencv\sources etc '

Samjith888 gravatar imageSamjith888 ( 2018-08-17 05:10:02 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-08-17 05:07:41 -0500

berak gravatar image

updated 2018-08-17 07:06:11 -0500

the error means, that your hog feature had a different size, than those, you trained the svm on.

most likely, your test image had a different size, than the train images, too (larger image -> more features).

you have to keep the size of your train images around, and crop or resize() your test image to this, before extracting the hog features for the prediction:

Size trainSize = ...
resize(img, img, trainSize);

[EDIT]:

apologies, i missed the elephant in the room: it has to be:

Ptr<ml::SVM> svm = ml::SVM::load("Features.xml");

(the load() method is static, and returns a new SVM. if you call it on an existing one, it does simply nothing, thus your svm is not trained yet)

edit flag offensive delete link more

Comments

@berak : i have 74 images. How can i get the size of the each image ?

Samjith888 gravatar imageSamjith888 ( 2018-08-17 05:35:50 -0500 )edit
1

Size s = img.size();

berak gravatar imageberak ( 2018-08-17 05:40:36 -0500 )edit

@berak: i correct the code as you mentioned. place the code resize(img, img, Size(64, 64)); above hog.compute(img, featureVector, Size(32, 32), Size(64, 64)); in both training and testing section. I created Features.xml files in the training section . But test section showing the same error.

Samjith888 gravatar imageSamjith888 ( 2018-08-17 06:50:53 -0500 )edit

^please see update !

berak gravatar imageberak ( 2018-08-17 07:06:29 -0500 )edit

@berak: yes. its working now..Thank you

Samjith888 gravatar imageSamjith888 ( 2018-08-17 07:14:20 -0500 )edit

@berak : But the result is always showing -1 for every test images (once i tried input from my positive training database, but the result is always -1 )

Samjith888 gravatar imageSamjith888 ( 2018-08-17 07:29:11 -0500 )edit

yea, expected, that's where the REAL work starts ;)

  • read up on SVM's and their params. try a different kernel. (LINEAR should work better)
  • 80 images are a laugh. having one image per feature is a good rule of thumb.
  • don't take 1 hog feature per image, but make a grid of say 4x4 squares, take seperate hog features for each, concatenate the resulting 16 vectors to a single one
  • vary the hog params
  • read up on cross-fold validation
berak gravatar imageberak ( 2018-08-17 07:38:40 -0500 )edit

@berak : I wrote the training section in the following section , Is there any error in my code? is there any error while training the positive (labelled as 1) image hog features into the svm train method?

` int main(){ for (size_t i = 1; i <= 37; ++i)   
{  //reading positive images
           resize(img, img, Size(64, 64));
    hog.compute(img, featureVector, Size(32, 32), Size(64, 64));
            Mat feature = Mat(featureVector).reshape(1, 1);
        trainData.push_back(feature);
        trainLabels.push_back(1);  } for (size_t i = 1; i <= 37; ++i)    {
 //do the same above for negative images }Ptr<ml::SVM> svm = ml::SVM::create();
svm->train(trainData, ml::ROW_SAMPLE, trainLabels);

svm->save("Features.xml");

} `

Samjith888 gravatar imageSamjith888 ( 2018-08-17 07:54:41 -0500 )edit

no, i don't think, there's an error. again, see the comment above.

berak gravatar imageberak ( 2018-08-17 08:05:48 -0500 )edit

@berak : I read some stuffs. I realized that RBF is better choice if training dataset is large; if not Linear would be sufficient. what do you think?

Samjith888 gravatar imageSamjith888 ( 2018-08-18 00:28:58 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-08-17 04:47:55 -0500

Seen: 610 times

Last updated: Aug 17 '18