Ask Your Question
0

error when training SVM

asked 2017-02-09 16:26:11 -0600

Tomna gravatar image

updated 2017-02-09 18:49:34 -0600

berak gravatar image

i am new to such machine learning algorithm so ask if something is not clear and the error i am getting is

Exception in thread "main" java.lang.NullPointerException on this line s.setType(SVM.C_SVC);

   here is my code 

     Mat[] image = new Mat[2];
            image[0] = Imgcodecs.imread("/home/tomna/NetBeansProjects/main/src/main/PLATES/1.jpg", 0);
            image[1] = Imgcodecs.imread("/home/tomna/NetBeansProjects/main/src/main/PLATES/2.jpg", 0);
//        
        image[0] = image[0].reshape(0,1);
         image[1] = image[1].reshape(0,1);
            Mat img_mat = Imgcodecs.imread("/home/tomna/NetBeansProjects/main/src/main/Dadcar3.png", 0);
            img_mat.reshape(0,1);


            Mat new_img = new Mat(2, 50367, CV_32FC1);


            float[] labels= {-1,1};
            Mat labelsmat = new Mat(2,1,CV_32FC1);
            labelsmat.convertTo(labelsmat, CV_32FC1);


            SVM s = null;
            s.setType(SVM.C_SVC);
            s.setKernel(SVM.LINEAR);
            s.setGamma(3);
            s.setDegree(3);
            s.train(new_img, 3, labelsmat);
            s.save("/home/tomna/NetBeansProjects/main/src/main/images.xml");
           s.predict(img_mat);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-02-09 18:29:39 -0600

berak gravatar image

updated 2017-02-09 18:30:38 -0600

error : you need a real SVM instance , not a null pointer:

 SVM s = Ml.SVM.create();

error : your labelsmat does not have valid data, also rather make it int initially:

 int[] labels= {-1,1};
 Mat labelsmat = new Mat(2,1,CV_32SC1);
 labelsmat.put(0,0,labels);

error: your traindata is invalid, too ! you load 2 images, but never use them, it should rather look like:

 Mat traindata = new Mat();

 Mat image0 = Imgcodecs.imread("/home/tomna/NetBeansProjects/main/src/main/PLATES/1.jpg", 0);
 traindata.push_back(image0.reshape(0,1));

 Mat image1 = Imgcodecs.imread("/home/tomna/NetBeansProjects/main/src/main/PLATES/2.jpg", 0);
 traindata.push_back(image1.reshape(0,1));

 traindata.convertTo(traindata, CvType.CV_32F);
 // then, 50k features per row is probably a bit too much...

error : invalid flag in train(), this should be ROW_SAMPLES (0):

 s.train(traindata, 0, labelsmat);

error : if you want to predict many rows at a time (2 in your case), you need to supply a result Mat to predict()

(since it it can return only 1 value):

 Mat result = new Mat();
 s.predict(traindata, result);
 System.out.println(result.dump());

good luck !

edit flag offensive delete link more

Comments

@berak i am sorry but what did u mean by that 50k features per row is a bit too much from where did you get this 50k ?

Tomna gravatar imageTomna ( 2017-02-09 19:36:39 -0600 )edit

@berak sorry i just realised that Mat new_img = new Mat(2, 50367, CV_32FC1); but does that mean that i have to get images with less area ?

Tomna gravatar imageTomna ( 2017-02-09 19:38:58 -0600 )edit

at least you need more images (like 100 per class), and most likely, you need smaller images, or better features (HOG, LBPH instead of PIXELS)

berak gravatar imageberak ( 2017-02-09 23:51:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-09 16:26:11 -0600

Seen: 190 times

Last updated: Feb 09 '17