Skip to content

Commit 7ad308e

Browse files
LaurentBergervpisarev
authored andcommitted
Simulated Annealing for ANN_MLP training method (opencv#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
1 parent 6df8ac0 commit 7ad308e

File tree

5 files changed

+685
-26
lines changed

5 files changed

+685
-26
lines changed

doc/opencv.bib

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,17 @@ @ARTICLE{KleeLaskowski85
459459
number = {3},
460460
publisher = {Elsevier}
461461
}
462+
@ARTICLE{Kirkpatrick83,
463+
author = {Kirkpatrick, S. and Gelatt, C. D. Jr and Vecchi, M. P. },
464+
title = {Optimization by Simulated Annealing},
465+
year = {1983},
466+
pages = {671--680},
467+
journal = {Science},
468+
volume = {220},
469+
number = {4598},
470+
publisher = {American Association for the Advancement of Science}
471+
}
472+
462473
@INPROCEEDINGS{Kolmogorov03,
463474
author = {Kim, Junhwan and Kolmogorov, Vladimir and Zabih, Ramin},
464475
title = {Visual correspondence using energy minimization and mutual information},

modules/ml/include/opencv2/ml.hpp

Lines changed: 137 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,13 +1406,14 @@ class CV_EXPORTS_W ANN_MLP : public StatModel
14061406
/** Available training methods */
14071407
enum TrainingMethods {
14081408
BACKPROP=0, //!< The back-propagation algorithm.
1409-
RPROP=1 //!< The RPROP algorithm. See @cite RPROP93 for details.
1409+
RPROP = 1, //!< The RPROP algorithm. See @cite RPROP93 for details.
1410+
ANNEAL = 2 //!< The simulated annealing algorithm. See @cite Kirkpatrick83 for details.
14101411
};
14111412

14121413
/** Sets training method and common parameters.
14131414
@param method Default value is ANN_MLP::RPROP. See ANN_MLP::TrainingMethods.
1414-
@param param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP
1415-
@param param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP.
1415+
@param param1 passed to setRpropDW0 for ANN_MLP::RPROP and to setBackpropWeightScale for ANN_MLP::BACKPROP and to initialT for ANN_MLP::ANNEAL.
1416+
@param param2 passed to setRpropDWMin for ANN_MLP::RPROP and to setBackpropMomentumScale for ANN_MLP::BACKPROP and to finalT for ANN_MLP::ANNEAL.
14161417
*/
14171418
CV_WRAP virtual void setTrainMethod(int method, double param1 = 0, double param2 = 0) = 0;
14181419

@@ -1499,6 +1500,34 @@ class CV_EXPORTS_W ANN_MLP : public StatModel
14991500
/** @copybrief getRpropDWMax @see getRpropDWMax */
15001501
CV_WRAP virtual void setRpropDWMax(double val) = 0;
15011502

1503+
/** ANNEAL: Update initial temperature.
1504+
It must be \>=0. Default value is 10.*/
1505+
/** @see setAnnealInitialT */
1506+
CV_WRAP double getAnnealInitialT() const;
1507+
/** @copybrief getAnnealInitialT @see getAnnealInitialT */
1508+
CV_WRAP void setAnnealInitialT(double val);
1509+
1510+
/** ANNEAL: Update final temperature.
1511+
It must be \>=0 and less than initialT. Default value is 0.1.*/
1512+
/** @see setAnnealFinalT */
1513+
CV_WRAP double getAnnealFinalT() const;
1514+
/** @copybrief getAnnealFinalT @see getAnnealFinalT */
1515+
CV_WRAP void setAnnealFinalT(double val);
1516+
1517+
/** ANNEAL: Update cooling ratio.
1518+
It must be \>0 and less than 1. Default value is 0.95.*/
1519+
/** @see setAnnealCoolingRatio */
1520+
CV_WRAP double getAnnealCoolingRatio() const;
1521+
/** @copybrief getAnnealCoolingRatio @see getAnnealCoolingRatio */
1522+
CV_WRAP void setAnnealCoolingRatio(double val);
1523+
1524+
/** ANNEAL: Update iteration per step.
1525+
It must be \>0 . Default value is 10.*/
1526+
/** @see setAnnealItePerStep */
1527+
CV_WRAP int getAnnealItePerStep() const;
1528+
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
1529+
CV_WRAP void setAnnealItePerStep(int val);
1530+
15021531
/** possible activation functions */
15031532
enum ActivationFunctions {
15041533
/** Identity function: \f$f(x)=x\f$ */
@@ -1838,6 +1867,111 @@ CV_EXPORTS void randMVNormal( InputArray mean, InputArray cov, int nsamples, Out
18381867
CV_EXPORTS void createConcentricSpheresTestSet( int nsamples, int nfeatures, int nclasses,
18391868
OutputArray samples, OutputArray responses);
18401869

1870+
/** @brief Artificial Neural Networks - Multi-Layer Perceptrons.
1871+
1872+
@sa @ref ml_intro_ann
1873+
*/
1874+
class CV_EXPORTS_W ANN_MLP_ANNEAL : public ANN_MLP
1875+
{
1876+
public:
1877+
/** @see setAnnealInitialT */
1878+
CV_WRAP virtual double getAnnealInitialT() const;
1879+
/** @copybrief getAnnealInitialT @see getAnnealInitialT */
1880+
CV_WRAP virtual void setAnnealInitialT(double val);
1881+
1882+
/** ANNEAL: Update final temperature.
1883+
It must be \>=0 and less than initialT. Default value is 0.1.*/
1884+
/** @see setAnnealFinalT */
1885+
CV_WRAP virtual double getAnnealFinalT() const;
1886+
/** @copybrief getAnnealFinalT @see getAnnealFinalT */
1887+
CV_WRAP virtual void setAnnealFinalT(double val);
1888+
1889+
/** ANNEAL: Update cooling ratio.
1890+
It must be \>0 and less than 1. Default value is 0.95.*/
1891+
/** @see setAnnealCoolingRatio */
1892+
CV_WRAP virtual double getAnnealCoolingRatio() const;
1893+
/** @copybrief getAnnealCoolingRatio @see getAnnealCoolingRatio */
1894+
CV_WRAP virtual void setAnnealCoolingRatio(double val);
1895+
1896+
/** ANNEAL: Update iteration per step.
1897+
It must be \>0 . Default value is 10.*/
1898+
/** @see setAnnealItePerStep */
1899+
CV_WRAP virtual int getAnnealItePerStep() const;
1900+
/** @copybrief getAnnealItePerStep @see getAnnealItePerStep */
1901+
CV_WRAP virtual void setAnnealItePerStep(int val);
1902+
1903+
1904+
/** @brief Creates empty model
1905+
1906+
Use StatModel::train to train the model, Algorithm::load\<ANN_MLP\>(filename) to load the pre-trained model.
1907+
Note that the train method has optional flags: ANN_MLP::TrainFlags.
1908+
*/
1909+
// CV_WRAP static Ptr<ANN_MLP> create();
1910+
1911+
};
1912+
1913+
/****************************************************************************************\
1914+
* Simulated annealing solver *
1915+
\****************************************************************************************/
1916+
1917+
/** @brief The class implements simulated annealing for optimization.
1918+
@cite Kirkpatrick83 for details
1919+
*/
1920+
class CV_EXPORTS SimulatedAnnealingSolver : public Algorithm
1921+
{
1922+
public:
1923+
SimulatedAnnealingSolver() { init(); };
1924+
~SimulatedAnnealingSolver();
1925+
/** Give energy value for a state of system.*/
1926+
virtual double energy() =0;
1927+
/** Function which change the state of system (random pertubation).*/
1928+
virtual void changedState() = 0;
1929+
/** Function to reverse to the previous state.*/
1930+
virtual void reverseChangedState() = 0;
1931+
/** Simulated annealing procedure. */
1932+
int run();
1933+
/** Set intial temperature of simulated annealing procedure.
1934+
*@param x new initial temperature. x\>0
1935+
*/
1936+
void setInitialTemperature(double x);
1937+
/** Set final temperature of simulated annealing procedure.
1938+
*@param x new final temperature value. 0\<x\<initial temperature
1939+
*/
1940+
void setFinalTemperature(double x);
1941+
double getFinalTemperature();
1942+
/** Set setCoolingRatio of simulated annealing procedure : T(t) = coolingRatio * T(t-1).
1943+
* @param x new cooling ratio value. 0\<x\<1
1944+
*/
1945+
void setCoolingRatio(double x);
1946+
/** Set number iteration per temperature step.
1947+
* @param ite number of iteration per temperature step ite \> 0
1948+
*/
1949+
void setIterPerStep(int ite);
1950+
struct Impl;
1951+
protected :
1952+
void init();
1953+
Impl* impl;
1954+
};
1955+
struct SimulatedAnnealingSolver::Impl
1956+
{
1957+
RNG rEnergy;
1958+
double coolingRatio;
1959+
double initialT;
1960+
double finalT;
1961+
int iterPerStep;
1962+
Impl()
1963+
{
1964+
initialT = 2;
1965+
finalT = 0.1;
1966+
coolingRatio = 0.95;
1967+
iterPerStep = 100;
1968+
refcount = 1;
1969+
}
1970+
int refcount;
1971+
~Impl() { refcount--;CV_Assert(refcount==0); }
1972+
};
1973+
1974+
18411975
//! @} ml
18421976

18431977
}

0 commit comments

Comments
 (0)