Ask Your Question
0

ANN_MLP huge training sets

asked 2017-12-11 15:27:58 -0500

sjhalayka gravatar image

updated 2017-12-11 15:30:20 -0500

I am having difficulty understanding the artificial neural network in OpenCV.

I found a code sample that trains the ANN to perform the XOR operation. The code looks something like this:

TermCriteria termCrit = TermCriteria(TermCriteria::Type::COUNT + TermCriteria::Type::EPS, 10000, 0.000001);
mlp->setTermCriteria(termCrit);

Ptr<TrainData> trainingData = TrainData::create(inputTrainingData, SampleTypes::ROW_SAMPLE, outputTrainingData);

mlp->train(trainingData);

.. which, as I understand, trains the network for up to 10000 times while looking for a maximum error of 0.000001.

This code says to me that all of the training data must be made available to the ANN before training occurs. Is this true, and if so, what if the training data consists of thousands of images?

I tried the following code, but it appears that the training is reset whenever mlp->train() is called:

TermCriteria termCrit = TermCriteria(TermCriteria::Type::COUNT + TermCriteria::Type::EPS, 1, 0.000001);
mlp->setTermCriteria(termCrit);

Ptr<TrainData> trainingData = TrainData::create(inputTrainingData, SampleTypes::ROW_SAMPLE, outputTrainingData);

for(size_t i = 0; i < 10000; i++)
mlp->train(trainingData);

Is there no way to continue the training instead of resetting it each time?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-12-11 15:36:44 -0500

LBerger gravatar image

updated 2017-12-11 15:37:55 -0500

use flag UPDATE_WEIGHTS in train method

edit flag offensive delete link more

Comments

Thank you! That seems to do the trick as long as I call train without UPDATE_WEIGHTS first, then do the rest of the iterations with UPDATE_WEIGHTS:

Ptr<TrainData> trainingData = TrainData::create(inputTrainingData, SampleTypes::ROW_SAMPLE, outputTrainingData);

mlp->train(trainingData);

for(int i = 1; i < 10000; i++)
    mlp->train(trainingData, ANN_MLP::TrainFlags::UPDATE_WEIGHTS);
sjhalayka gravatar imagesjhalayka ( 2017-12-11 15:43:02 -0500 )edit

@LBerger -- When doing training based on an image, I was wondering if you'd recommend converting the many row 2D image Mats to single row 1D Mats? Thanks again!

sjhalayka gravatar imagesjhalayka ( 2017-12-11 20:36:39 -0500 )edit

use reshape method

LBerger gravatar imageLBerger ( 2017-12-12 03:49:07 -0500 )edit

Thank you so much! I will have reshaped them to 1D, and will have num_columns*4 input neurons; one input neuron per r, g, b, and a, times the number of pixels.

sjhalayka gravatar imagesjhalayka ( 2017-12-12 09:14:51 -0500 )edit

Don't forget data type must be CV_32FC or CV_64FC

LBerger gravatar imageLBerger ( 2017-12-12 12:39:21 -0500 )edit

OK. Thank you for the reminder. As well, the network's input values and output values must each be within [0, 1]. The output values can be (precisely) 0 or 1, more like it -- "the output [can] range from [-1.7159, 1.7159]": https://docs.opencv.org/3.0-beta/modu...

Edit: It turns out that you cannot train for values like 1 when using UPDATE_WEIGHTS. See: http://answers.opencv.org/question/18...

sjhalayka gravatar imagesjhalayka ( 2017-12-12 13:34:39 -0500 )edit

No. I haven't got enough time to test your code there is no relation between UPDAE_WEIGHTS and values. Problem is in predict. If you want to know values in output layers you must get response in predict function

LBerger gravatar imageLBerger ( 2017-12-14 07:40:48 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-12-11 15:27:58 -0500

Seen: 89 times

Last updated: Dec 11 '17