Ask Your Question
1

Cv::ml::TrainData::loadFromCSV Parameters

asked 2016-10-20 23:54:11 -0600

damiya14 gravatar image

i want to load a csv file as train data.

in csv file first column contains the labels (which is 1 - 12) other columns contains the data.

cv::ml::TrainData::loadFromCSV  (   const String &  filename,
int     headerLineCount,
int     responseStartIdx = -1,
int     responseEndIdx = -1,
const String &  varTypeSpec = String(),
char    delimiter = ',',
char    missch = '?' 
)

i am confused with parameters. please explain the parameters.

Thank you!

edit retag flag offensive close merge delete

Comments

can you take a look at the docs and maybe refine your question ?

berak gravatar imageberak ( 2016-10-20 23:59:41 -0600 )edit

i read it like 10 times. i am confused with thees parameters

int     responseStartIdx = -1,
int     responseEndIdx = -1,
const String &  varTypeSpec = String()
damiya14 gravatar imagedamiya14 ( 2016-10-21 00:23:42 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-10-21 01:09:35 -0600

berak gravatar image

updated 2016-10-21 01:10:56 -0600

if your csv is like this (label is 1st column):

2,  1.4, 5.7, 6.6, 0.2
^            ^
label      data

then responseStartIdx is 0 and responseEndIdx is 1 .

(if your labels are in the last column, leave both at -1 (the default))

if your data is all numeric, you do not need to pass anything for varTypeSpec, this is only nessecary if you have "mixed" data like:

  1.2, apple, 17
edit flag offensive delete link more

Comments

1

thanks a lot!!!!!

damiya14 gravatar imagedamiya14 ( 2016-10-21 01:22:30 -0600 )edit

SVM.train(TrainData,labelsMat, Mat(), Mat(), params);

this is the normal way of training svm right? so when i load it from csv what is my labelsMat?

damiya14 gravatar imagedamiya14 ( 2016-10-21 02:28:01 -0600 )edit

wait, no, yours looks like opencv2.4 code.

remember, that traindata already contains the labels, so you would use this overload

Ptr<cv::ml::TrainData> tdata = cv::ml::TrainData::loadFromCSV("my.csv", 0, 0, 1);
Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setC(10); // maybe you want to set some params
svm->train(tdata);

if you don't use a csv file, you'd have a Mat features, and another Mat labels, and use it like :

svm->train(features, 0, labels);
berak gravatar imageberak ( 2016-10-21 02:34:32 -0600 )edit

Sorry for being so bad at this! i am kinda new to this. Is there a way that i can contact you?

damiya14 gravatar imagedamiya14 ( 2016-10-21 02:53:18 -0600 )edit

do not feel sorry in the 1st place, it's not your fault, that the api changed significantly, causing a lot of confusion !

then, i somewhat believe, it's better, to keep it here, (or on #opencv), so more folks can look at it, and help !

berak gravatar imageberak ( 2016-10-21 03:05:03 -0600 )edit

thanks mate!

  1. How can i save the svm.train and access in the classification program?
  2. Then how can i classify the image using the trained file?
damiya14 gravatar imagedamiya14 ( 2016-10-30 23:49:53 -0600 )edit

sure:

svm->save("my.xml");
// careful, this makes a **new** instance !
Ptr<SVM> svm = Algorithm::load<SVM>("my.xml");
berak gravatar imageberak ( 2016-10-31 02:38:40 -0600 )edit

i am using opencv2.4 so the code do not work with me. :(

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

is it,

svm.save("my.xmal");
damiya14 gravatar imagedamiya14 ( 2016-10-31 23:19:24 -0600 )edit

and then after saving is done i want to use that save file to classify the image. are these steps correct?

  1. Load the xml file
  2. Get features from the image
  3. Classify using svm

is there any example for this?

damiya14 gravatar imagedamiya14 ( 2016-11-01 00:46:23 -0600 )edit

oh, sorry, it seems, i got your opencv version wrong then, all the time ;(

but yes, your steps are correct (once you trained and saved the model, you do not need to reload the csv or retrain it, you can just load the saved model and do your prediction)

berak gravatar imageberak ( 2016-11-01 01:17:27 -0600 )edit

ok i managed to do it. i am just using two classes (white & silver) So train the svm and use the model as below,

 CvSVM mySVM1;
    mySVM1.load("/home/damiya/test/Sakura/my.xml");
    .
    .
    .
    .
    float response = mySVM1.predict(sampleMat);

but the modele returns only one class every time. (ex. it returns everything as white even i feed the features that i used to train it)

damiya14 gravatar imagedamiya14 ( 2016-11-02 02:00:38 -0600 )edit

well, once you got the general pipeline working, the real problems start (tweaking svm params, cleaning data)

what is actually in your csv file ? some sort of (hsv?) pixels ? silver and white are incredibly difficult to seperate anyway.

berak gravatar imageberak ( 2016-11-02 02:25:55 -0600 )edit

my csv file contains hsv rgb and yCrCb data for each pixel this is csv file.

first column contains the label. then in order R G B H S V Y Cr Cb and it repeats.

damiya14 gravatar imagedamiya14 ( 2016-11-02 02:46:06 -0600 )edit

cancelled the load after a couple mb's (how large is it?)

so it seems, you got enough data, maybe you need to play with the svm params (C would be a good start, like 0.1, 1, 10, 100, etc) , try different kernel types(linear/rbm/poly), maybe even train_auto() works on 2.4 (haven't tried)

unfortunately that means retraining your SVM for each param change, so huge time sink.

berak gravatar imageberak ( 2016-11-02 02:58:20 -0600 )edit

its 31mb. i tried different kernel types such as linear,rbm, and poly but every time its wrong.

i will try train_auto().. hops that works :) :)

damiya14 gravatar imagedamiya14 ( 2016-11-02 03:16:43 -0600 )edit

hey,

i tried train_auto() and it takes a lot of time. Does it take a lot of time?

damiya14 gravatar imagedamiya14 ( 2016-11-03 08:11:15 -0600 )edit

yea, ages. one complete training run for each combination of params

again, huge time-sink ahead ;(

berak gravatar imageberak ( 2016-11-03 08:13:10 -0600 )edit

its still running :(

damiya14 gravatar imagedamiya14 ( 2016-11-03 08:30:26 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-20 23:54:11 -0600

Seen: 2,516 times

Last updated: Oct 21 '16