# Revision history [back]

### OpenCV 3 Java - How to train neural network with TrainData

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.

### OpenCV 3 Java - How to train neural network with TrainData

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.