Skip to content

Commit

Permalink
Move ml to opencv_contrib
Browse files Browse the repository at this point in the history
  • Loading branch information
kaingwade committed Feb 13, 2024
1 parent f63396a commit 19c0016
Show file tree
Hide file tree
Showing 73 changed files with 22,183 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/ml/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set(the_description "Machine Learning")

ocv_add_module(ml opencv_core WRAP java objc python)
ocv_glob_module_sources()
ocv_module_include_directories()
ocv_create_module()

ocv_add_accuracy_tests()
ocv_add_perf_tests()
ocv_add_samples(opencv_imgproc opencv_imgcodecs opencv_objdetect opencv_videoio opencv_video)
481 changes: 481 additions & 0 deletions modules/ml/doc/ml_intro.markdown

Large diffs are not rendered by default.

Binary file added modules/ml/doc/pics/SVM_Comparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added modules/ml/doc/pics/mlp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added modules/ml/doc/pics/neuron_model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added modules/ml/doc/pics/sigmoid_bipolar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,956 changes: 1,956 additions & 0 deletions modules/ml/include/opencv2/ml.hpp

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions modules/ml/include/opencv2/ml/ml.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#ifdef __OPENCV_BUILD
#error this is a compatibility header which should not be used inside the OpenCV library
#endif

#include "opencv2/ml.hpp"
60 changes: 60 additions & 0 deletions modules/ml/include/opencv2/ml/ml.inl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.

#ifndef OPENCV_ML_INL_HPP
#define OPENCV_ML_INL_HPP

namespace cv { namespace ml {

// declared in ml.hpp
template<class SimulatedAnnealingSolverSystem>
int simulatedAnnealingSolver(SimulatedAnnealingSolverSystem& solverSystem,
double initialTemperature, double finalTemperature, double coolingRatio,
size_t iterationsPerStep,
CV_OUT double* lastTemperature,
cv::RNG& rngEnergy
)
{
CV_Assert(finalTemperature > 0);
CV_Assert(initialTemperature > finalTemperature);
CV_Assert(iterationsPerStep > 0);
CV_Assert(coolingRatio < 1.0f);
double Ti = initialTemperature;
double previousEnergy = solverSystem.energy();
int exchange = 0;
while (Ti > finalTemperature)
{
for (size_t i = 0; i < iterationsPerStep; i++)
{
solverSystem.changeState();
double newEnergy = solverSystem.energy();
if (newEnergy < previousEnergy)
{
previousEnergy = newEnergy;
exchange++;
}
else
{
double r = rngEnergy.uniform(0.0, 1.0);
if (r < std::exp(-(newEnergy - previousEnergy) / Ti))
{
previousEnergy = newEnergy;
exchange++;
}
else
{
solverSystem.reverseState();
}
}
}
Ti *= coolingRatio;
}
if (lastTemperature)
*lastTemperature = Ti;
return exchange;
}

}} //namespace

#endif // OPENCV_ML_INL_HPP
42 changes: 42 additions & 0 deletions modules/ml/misc/java/test/MLTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.opencv.test.ml;

import org.opencv.ml.Ml;
import org.opencv.ml.SVM;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.CvType;
import org.opencv.test.OpenCVTestCase;
import org.opencv.test.OpenCVTestRunner;

public class MLTest extends OpenCVTestCase {

public void testSaveLoad() {
Mat samples = new MatOfFloat(new float[] {
5.1f, 3.5f, 1.4f, 0.2f,
4.9f, 3.0f, 1.4f, 0.2f,
4.7f, 3.2f, 1.3f, 0.2f,
4.6f, 3.1f, 1.5f, 0.2f,
5.0f, 3.6f, 1.4f, 0.2f,
7.0f, 3.2f, 4.7f, 1.4f,
6.4f, 3.2f, 4.5f, 1.5f,
6.9f, 3.1f, 4.9f, 1.5f,
5.5f, 2.3f, 4.0f, 1.3f,
6.5f, 2.8f, 4.6f, 1.5f
}).reshape(1, 10);
Mat responses = new MatOfInt(new int[] {
0, 0, 0, 0, 0, 1, 1, 1, 1, 1
}).reshape(1, 10);
SVM saved = SVM.create();
assertFalse(saved.isTrained());

saved.train(samples, Ml.ROW_SAMPLE, responses);
assertTrue(saved.isTrained());

String filename = OpenCVTestRunner.getTempFileName("yml");
saved.save(filename);
SVM loaded = SVM.load(filename);
assertTrue(loaded.isTrained());
}

}
9 changes: 9 additions & 0 deletions modules/ml/misc/objc/gen_dict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"enum_fix" : {
"EM" : { "Types": "EMTypes" },
"SVM" : { "Types": "SVMTypes" },
"KNearest" : { "Types": "KNearestTypes" },
"DTrees" : { "Flags": "DTreeFlags" },
"StatModel" : { "Flags": "StatModelFlags" }
}
}
22 changes: 22 additions & 0 deletions modules/ml/misc/python/pyopencv_ml.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
template<>
bool pyopencv_to(PyObject *obj, CvTermCriteria& dst, const ArgInfo& info)
{
CV_UNUSED(info);
if(!obj)
return true;
return PyArg_ParseTuple(obj, "iid", &dst.type, &dst.max_iter, &dst.epsilon) > 0;
}

template<>
bool pyopencv_to(PyObject* obj, CvSlice& r, const ArgInfo& info)
{
CV_UNUSED(info);
if(!obj || obj == Py_None)
return true;
if(PyObject_Size(obj) == 0)
{
r = CV_WHOLE_SEQ;
return true;
}
return PyArg_ParseTuple(obj, "ii", &r.start_index, &r.end_index) > 0;
}
Loading

0 comments on commit 19c0016

Please sign in to comment.