Simulated Annealing for ANN_MLP training method (#10213)
* Simulated Annealing for ANN_MLP training method * EXPECT_LT * just to test new data * manage RNG * Try again * Just run buildbot with new data * try to understand * Test layer * New data- new test * Force RNG in backprop * Use Impl to avoid virtual method * reset all weights * try to solve ABI * retry * ABI solved? * till problem with dynamic_cast * Something is wrong * Solved? * disable backprop test * remove ANN_MLP_ANNEALImpl * Disable weight in varmap * Add example for SimulatedAnnealing
This commit is contained in:
committed by
Vadim Pisarevsky
parent
6df8ac0342
commit
7ad308ea47
@@ -79,8 +79,10 @@ int str_to_ann_train_method( String& str )
|
||||
{
|
||||
if( !str.compare("BACKPROP") )
|
||||
return ANN_MLP::BACKPROP;
|
||||
if( !str.compare("RPROP") )
|
||||
if (!str.compare("RPROP"))
|
||||
return ANN_MLP::RPROP;
|
||||
if (!str.compare("ANNEAL"))
|
||||
return ANN_MLP::ANNEAL;
|
||||
CV_Error( CV_StsBadArg, "incorrect ann train method string" );
|
||||
return -1;
|
||||
}
|
||||
@@ -241,13 +243,92 @@ TEST(ML_ANN, ActivationFunction)
|
||||
Mat rx, ry, dst;
|
||||
x->predict(testSamples, rx);
|
||||
y->predict(testSamples, ry);
|
||||
absdiff(rx, ry, dst);
|
||||
double minVal, maxVal;
|
||||
minMaxLoc(dst, &minVal, &maxVal);
|
||||
ASSERT_TRUE(maxVal<FLT_EPSILON) << "Predict are not equal for " << dataname + activationName[i] + ".yml and " << activationName[i];
|
||||
double n = cvtest::norm(rx, ry, NORM_INF);
|
||||
EXPECT_LT(n,FLT_EPSILON) << "Predict are not equal for " << dataname + activationName[i] + ".yml and " << activationName[i];
|
||||
#endif
|
||||
}
|
||||
}
|
||||
//#define GENERATE_TESTDATA
|
||||
TEST(ML_ANN, Method)
|
||||
{
|
||||
String folder = string(cvtest::TS::ptr()->get_data_path());
|
||||
String original_path = folder + "waveform.data";
|
||||
String dataname = folder + "waveform";
|
||||
|
||||
Ptr<TrainData> tdata2 = TrainData::loadFromCSV(original_path, 0);
|
||||
Mat responses(tdata2->getResponses().rows, 3, CV_32FC1, Scalar(0));
|
||||
for (int i = 0; i<tdata2->getResponses().rows; i++)
|
||||
responses.at<float>(i, static_cast<int>(tdata2->getResponses().at<float>(i, 0))) = 1;
|
||||
Ptr<TrainData> tdata = TrainData::create(tdata2->getSamples(), ml::ROW_SAMPLE, responses);
|
||||
|
||||
ASSERT_FALSE(tdata.empty()) << "Could not find test data file : " << original_path;
|
||||
RNG& rng = theRNG();
|
||||
rng.state = 0;
|
||||
tdata->setTrainTestSplitRatio(0.8);
|
||||
|
||||
vector<int> methodType;
|
||||
methodType.push_back(ml::ANN_MLP::RPROP);
|
||||
methodType.push_back(ml::ANN_MLP::ANNEAL);
|
||||
// methodType.push_back(ml::ANN_MLP::BACKPROP); -----> NO BACKPROP TEST
|
||||
vector<String> methodName;
|
||||
methodName.push_back("_rprop");
|
||||
methodName.push_back("_anneal");
|
||||
// methodName.push_back("_backprop"); -----> NO BACKPROP TEST
|
||||
#ifdef GENERATE_TESTDATA
|
||||
Ptr<ml::ANN_MLP> xx = ml::ANN_MLP_ANNEAL::create();
|
||||
Mat_<int> layerSizesXX(1, 4);
|
||||
layerSizesXX(0, 0) = tdata->getNVars();
|
||||
layerSizesXX(0, 1) = 30;
|
||||
layerSizesXX(0, 2) = 30;
|
||||
layerSizesXX(0, 3) = tdata->getResponses().cols;
|
||||
xx->setLayerSizes(layerSizesXX);
|
||||
xx->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM);
|
||||
xx->setTrainMethod(ml::ANN_MLP::RPROP);
|
||||
xx->setTermCriteria(TermCriteria(TermCriteria::COUNT, 1, 0.01));
|
||||
xx->train(tdata, ml::ANN_MLP::NO_OUTPUT_SCALE + ml::ANN_MLP::NO_INPUT_SCALE);
|
||||
FileStorage fs;
|
||||
fs.open(dataname + "_init_weight.yml.gz", FileStorage::WRITE + FileStorage::BASE64);
|
||||
xx->write(fs);
|
||||
fs.release();
|
||||
#endif
|
||||
for (size_t i = 0; i < methodType.size(); i++)
|
||||
{
|
||||
FileStorage fs;
|
||||
fs.open(dataname + "_init_weight.yml.gz", FileStorage::READ + FileStorage::BASE64);
|
||||
Ptr<ml::ANN_MLP> x = ml::ANN_MLP_ANNEAL::create();
|
||||
x->read(fs.root());
|
||||
x->setTrainMethod(methodType[i]);
|
||||
if (methodType[i] == ml::ANN_MLP::ANNEAL)
|
||||
{
|
||||
x->setAnnealInitialT(12);
|
||||
x->setAnnealFinalT(0.15);
|
||||
x->setAnnealCoolingRatio(0.96);
|
||||
x->setAnnealItePerStep(11);
|
||||
}
|
||||
x->setTermCriteria(TermCriteria(TermCriteria::COUNT, 100, 0.01));
|
||||
x->train(tdata, ml::ANN_MLP::NO_OUTPUT_SCALE + ml::ANN_MLP::NO_INPUT_SCALE + ml::ANN_MLP::UPDATE_WEIGHTS);
|
||||
ASSERT_TRUE(x->isTrained()) << "Could not train networks with " << methodName[i];
|
||||
#ifdef GENERATE_TESTDATA
|
||||
x->save(dataname + methodName[i] + ".yml.gz");
|
||||
#endif
|
||||
Ptr<ml::ANN_MLP> y = Algorithm::load<ANN_MLP>(dataname + methodName[i] + ".yml.gz");
|
||||
ASSERT_TRUE(y != NULL) << "Could not load " << dataname + methodName[i] + ".yml";
|
||||
Mat testSamples = tdata->getTestSamples();
|
||||
Mat rx, ry, dst;
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
rx = x->getWeights(j);
|
||||
ry = y->getWeights(j);
|
||||
double n = cvtest::norm(rx, ry, NORM_INF);
|
||||
EXPECT_LT(n, FLT_EPSILON) << "Weights are not equal for " << dataname + methodName[i] + ".yml and " << methodName[i] << " layer : " << j;
|
||||
}
|
||||
x->predict(testSamples, rx);
|
||||
y->predict(testSamples, ry);
|
||||
double n = cvtest::norm(rx, ry, NORM_INF);
|
||||
EXPECT_LT(n, FLT_EPSILON) << "Predict are not equal for " << dataname + methodName[i] + ".yml and " << methodName[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 6. dtree
|
||||
// 7. boost
|
||||
|
||||
Reference in New Issue
Block a user