OpenCV 3 Java - How to train neural network with TrainData [closed]
I was recently updating my Java OpenCV 2.4 code to OpenCV 3.2. I had some code to train an ANN_MLP. I found out that OpenCV 3.2 has a new class called TrainData and decided to use it. I think I may have a discovered a bug but figured I would ask here first.
I have a Mat of samples and a Mat of responses in the ROW_SAMPLE layout, that is each row is a sample. Here is the code I use to train the neural network:
// ann is my ANN_MLP
ann.train(samples, Ml.ROW_SAMPLE, responses);
This code runs with no errors and on testing the prediction is correct.
The following code however does not run but instead crashes java:
TrainData td = TrainData.create(samples, Ml.ROW_SAMPLE, responses);
ann.train(td);
The "samples" and "responses" Mats are the exact same ones used in the above code snippet. This code snippet throws the following error:
Exception in thread "main" java.lang.Exception: unknown exception
at org.opencv.ml.StatModel.train_2(Native Method)
at org.opencv.ml.StatModel.train(StatModel.java:97)
and then it shows the stack trace to ann.train(td)
Is there any way to find out what the "unknown exception" is? Is this a bug only present in the Java port? Maybe I am just doing something wrong but according to the documentation this is correct usage of TrainData. Maybe somebody else has run into this issue.
I don't feel there is any need to provide more code due to it working with the same Mats when not using TrainData but if I need to provide more code just let me know.
EDIT: I am pretty sure this is a bug now because I tried running the following code
TrainData td = TrainData.create(samples, Ml.ROW_SAMPLE, responses);
ann.train(td.getSamples(), Ml.ROW_SAMPLE, td.getResponses());
and it works just fine.
there's a few cases, where you want to use TrainData, e.g. for reading data from csv files, or conveniently get randomly split train/testdata.
but in your case, i'd simply stick with the old method.
(no java here, so i cannot test, so not really an answer !)
@berak Yes I was planning on just sticking to the old method for now. However, using TrainData allows you to use calcError and, as you said, to randomly split the train/testdata which are really quite handy. Plus I found myself implementing all those functions anyways.
This isn't the first machine learning bug I have encountered in the Java port of OpenCV 3.2. It seems pretty unstable actually.
but i have to admit, it pretty much looks like a bug.
any chance, you still have the temp files from building around ? the generated c++ / java code might be of interest in this case !
@berak Unfortunately no. I didn't build it myself. I just downloaded the pre-built .jar file. If that's what you mean.
ah, ok, i see. (didn't even know, that the prebuild packages come with java wrappers)
no idea, how bold you feel, atm, but nothing will change, unless someone makes an issue here ;)
(maybe wait a bit, and see, if anybody else has an idea, but in the end .. someeone has to do it !)
@berak I'll give it a while. Don't want to embarrass myself :)
Dear Sipho, I am completely new to OpenCv with Android + java. It would be of great help if you or anyone here can help me understand how do I interpret images as MAT so as to train my classifier.
My code is: strong text Mat originalMat = new Mat(); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.a); Utils.bitmapToMat(bmp,originalMat);
:
this is the error:
E/org.opencv.ml: ml::create_11() caught cv::Exception: /build/master_pack-android/opencv/modules/ml/src/data.cpp:259: error: (-215) samples.type() == CV_32F || samples.type() == CV_32S in function void cv::ml::TrainDataImpl::setData(cv::InputArray, int, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray)