LogisticRegression.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.MlModule
  7. {
  8. // C++: class LogisticRegression
  9. /**
  10. * Implements Logistic Regression classifier.
  11. *
  12. * SEE: REF: ml_intro_lr
  13. */
  14. public class LogisticRegression : StatModel
  15. {
  16. protected override void Dispose(bool disposing)
  17. {
  18. try
  19. {
  20. if (disposing)
  21. {
  22. }
  23. if (IsEnabledDispose)
  24. {
  25. if (nativeObj != IntPtr.Zero)
  26. ml_LogisticRegression_delete(nativeObj);
  27. nativeObj = IntPtr.Zero;
  28. }
  29. }
  30. finally
  31. {
  32. base.Dispose(disposing);
  33. }
  34. }
  35. protected internal LogisticRegression(IntPtr addr) : base(addr) { }
  36. // internal usage only
  37. public static new LogisticRegression __fromPtr__(IntPtr addr) { return new LogisticRegression(addr); }
  38. // C++: enum cv.ml.LogisticRegression.Methods
  39. public const int BATCH = 0;
  40. public const int MINI_BATCH = 1;
  41. // C++: enum cv.ml.LogisticRegression.RegKinds
  42. public const int REG_DISABLE = -1;
  43. public const int REG_L1 = 0;
  44. public const int REG_L2 = 1;
  45. //
  46. // C++: double cv::ml::LogisticRegression::getLearningRate()
  47. //
  48. /**
  49. * SEE: setLearningRate
  50. * return automatically generated
  51. */
  52. public double getLearningRate()
  53. {
  54. ThrowIfDisposed();
  55. return ml_LogisticRegression_getLearningRate_10(nativeObj);
  56. }
  57. //
  58. // C++: void cv::ml::LogisticRegression::setLearningRate(double val)
  59. //
  60. /**
  61. * getLearningRate SEE: getLearningRate
  62. * param val automatically generated
  63. */
  64. public void setLearningRate(double val)
  65. {
  66. ThrowIfDisposed();
  67. ml_LogisticRegression_setLearningRate_10(nativeObj, val);
  68. }
  69. //
  70. // C++: int cv::ml::LogisticRegression::getIterations()
  71. //
  72. /**
  73. * SEE: setIterations
  74. * return automatically generated
  75. */
  76. public int getIterations()
  77. {
  78. ThrowIfDisposed();
  79. return ml_LogisticRegression_getIterations_10(nativeObj);
  80. }
  81. //
  82. // C++: void cv::ml::LogisticRegression::setIterations(int val)
  83. //
  84. /**
  85. * getIterations SEE: getIterations
  86. * param val automatically generated
  87. */
  88. public void setIterations(int val)
  89. {
  90. ThrowIfDisposed();
  91. ml_LogisticRegression_setIterations_10(nativeObj, val);
  92. }
  93. //
  94. // C++: int cv::ml::LogisticRegression::getRegularization()
  95. //
  96. /**
  97. * SEE: setRegularization
  98. * return automatically generated
  99. */
  100. public int getRegularization()
  101. {
  102. ThrowIfDisposed();
  103. return ml_LogisticRegression_getRegularization_10(nativeObj);
  104. }
  105. //
  106. // C++: void cv::ml::LogisticRegression::setRegularization(int val)
  107. //
  108. /**
  109. * getRegularization SEE: getRegularization
  110. * param val automatically generated
  111. */
  112. public void setRegularization(int val)
  113. {
  114. ThrowIfDisposed();
  115. ml_LogisticRegression_setRegularization_10(nativeObj, val);
  116. }
  117. //
  118. // C++: int cv::ml::LogisticRegression::getTrainMethod()
  119. //
  120. /**
  121. * SEE: setTrainMethod
  122. * return automatically generated
  123. */
  124. public int getTrainMethod()
  125. {
  126. ThrowIfDisposed();
  127. return ml_LogisticRegression_getTrainMethod_10(nativeObj);
  128. }
  129. //
  130. // C++: void cv::ml::LogisticRegression::setTrainMethod(int val)
  131. //
  132. /**
  133. * getTrainMethod SEE: getTrainMethod
  134. * param val automatically generated
  135. */
  136. public void setTrainMethod(int val)
  137. {
  138. ThrowIfDisposed();
  139. ml_LogisticRegression_setTrainMethod_10(nativeObj, val);
  140. }
  141. //
  142. // C++: int cv::ml::LogisticRegression::getMiniBatchSize()
  143. //
  144. /**
  145. * SEE: setMiniBatchSize
  146. * return automatically generated
  147. */
  148. public int getMiniBatchSize()
  149. {
  150. ThrowIfDisposed();
  151. return ml_LogisticRegression_getMiniBatchSize_10(nativeObj);
  152. }
  153. //
  154. // C++: void cv::ml::LogisticRegression::setMiniBatchSize(int val)
  155. //
  156. /**
  157. * getMiniBatchSize SEE: getMiniBatchSize
  158. * param val automatically generated
  159. */
  160. public void setMiniBatchSize(int val)
  161. {
  162. ThrowIfDisposed();
  163. ml_LogisticRegression_setMiniBatchSize_10(nativeObj, val);
  164. }
  165. //
  166. // C++: TermCriteria cv::ml::LogisticRegression::getTermCriteria()
  167. //
  168. /**
  169. * SEE: setTermCriteria
  170. * return automatically generated
  171. */
  172. public TermCriteria getTermCriteria()
  173. {
  174. ThrowIfDisposed();
  175. double[] tmpArray = new double[3];
  176. ml_LogisticRegression_getTermCriteria_10(nativeObj, tmpArray);
  177. TermCriteria retVal = new TermCriteria(tmpArray);
  178. return retVal;
  179. }
  180. //
  181. // C++: void cv::ml::LogisticRegression::setTermCriteria(TermCriteria val)
  182. //
  183. /**
  184. * getTermCriteria SEE: getTermCriteria
  185. * param val automatically generated
  186. */
  187. public void setTermCriteria(TermCriteria val)
  188. {
  189. ThrowIfDisposed();
  190. ml_LogisticRegression_setTermCriteria_10(nativeObj, val.type, val.maxCount, val.epsilon);
  191. }
  192. //
  193. // C++: float cv::ml::LogisticRegression::predict(Mat samples, Mat& results = Mat(), int flags = 0)
  194. //
  195. /**
  196. * Predicts responses for input samples and returns a float type.
  197. *
  198. * param samples The input data for the prediction algorithm. Matrix [m x n], where each row
  199. * contains variables (features) of one object being classified. Should have data type CV_32F.
  200. * param results Predicted labels as a column matrix of type CV_32S.
  201. * param flags Not used.
  202. * return automatically generated
  203. */
  204. public override float predict(Mat samples, Mat results, int flags)
  205. {
  206. ThrowIfDisposed();
  207. if (samples != null) samples.ThrowIfDisposed();
  208. if (results != null) results.ThrowIfDisposed();
  209. return ml_LogisticRegression_predict_10(nativeObj, samples.nativeObj, results.nativeObj, flags);
  210. }
  211. /**
  212. * Predicts responses for input samples and returns a float type.
  213. *
  214. * param samples The input data for the prediction algorithm. Matrix [m x n], where each row
  215. * contains variables (features) of one object being classified. Should have data type CV_32F.
  216. * param results Predicted labels as a column matrix of type CV_32S.
  217. * return automatically generated
  218. */
  219. public override float predict(Mat samples, Mat results)
  220. {
  221. ThrowIfDisposed();
  222. if (samples != null) samples.ThrowIfDisposed();
  223. if (results != null) results.ThrowIfDisposed();
  224. return ml_LogisticRegression_predict_11(nativeObj, samples.nativeObj, results.nativeObj);
  225. }
  226. /**
  227. * Predicts responses for input samples and returns a float type.
  228. *
  229. * param samples The input data for the prediction algorithm. Matrix [m x n], where each row
  230. * contains variables (features) of one object being classified. Should have data type CV_32F.
  231. * return automatically generated
  232. */
  233. public override float predict(Mat samples)
  234. {
  235. ThrowIfDisposed();
  236. if (samples != null) samples.ThrowIfDisposed();
  237. return ml_LogisticRegression_predict_12(nativeObj, samples.nativeObj);
  238. }
  239. //
  240. // C++: Mat cv::ml::LogisticRegression::get_learnt_thetas()
  241. //
  242. /**
  243. * This function returns the trained parameters arranged across rows.
  244. *
  245. * For a two class classification problem, it returns a row matrix. It returns learnt parameters of
  246. * the Logistic Regression as a matrix of type CV_32F.
  247. * return automatically generated
  248. */
  249. public Mat get_learnt_thetas()
  250. {
  251. ThrowIfDisposed();
  252. return new Mat(DisposableObject.ThrowIfNullIntPtr(ml_LogisticRegression_get_1learnt_1thetas_10(nativeObj)));
  253. }
  254. //
  255. // C++: static Ptr_LogisticRegression cv::ml::LogisticRegression::create()
  256. //
  257. /**
  258. * Creates empty model.
  259. *
  260. * Creates Logistic Regression model with parameters given.
  261. * return automatically generated
  262. */
  263. public static LogisticRegression create()
  264. {
  265. return LogisticRegression.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_LogisticRegression_create_10()));
  266. }
  267. //
  268. // C++: static Ptr_LogisticRegression cv::ml::LogisticRegression::load(String filepath, String nodeName = String())
  269. //
  270. /**
  271. * Loads and creates a serialized LogisticRegression from a file
  272. *
  273. * Use LogisticRegression::save to serialize and store an LogisticRegression to disk.
  274. * Load the LogisticRegression from this file again, by calling this function with the path to the file.
  275. * Optionally specify the node for the file containing the classifier
  276. *
  277. * param filepath path to serialized LogisticRegression
  278. * param nodeName name of node containing the classifier
  279. * return automatically generated
  280. */
  281. public static LogisticRegression load(string filepath, string nodeName)
  282. {
  283. return LogisticRegression.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_LogisticRegression_load_10(filepath, nodeName)));
  284. }
  285. /**
  286. * Loads and creates a serialized LogisticRegression from a file
  287. *
  288. * Use LogisticRegression::save to serialize and store an LogisticRegression to disk.
  289. * Load the LogisticRegression from this file again, by calling this function with the path to the file.
  290. * Optionally specify the node for the file containing the classifier
  291. *
  292. * param filepath path to serialized LogisticRegression
  293. * return automatically generated
  294. */
  295. public static LogisticRegression load(string filepath)
  296. {
  297. return LogisticRegression.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_LogisticRegression_load_11(filepath)));
  298. }
  299. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  300. const string LIBNAME = "__Internal";
  301. #else
  302. const string LIBNAME = "opencvforunity";
  303. #endif
  304. // C++: double cv::ml::LogisticRegression::getLearningRate()
  305. [DllImport(LIBNAME)]
  306. private static extern double ml_LogisticRegression_getLearningRate_10(IntPtr nativeObj);
  307. // C++: void cv::ml::LogisticRegression::setLearningRate(double val)
  308. [DllImport(LIBNAME)]
  309. private static extern void ml_LogisticRegression_setLearningRate_10(IntPtr nativeObj, double val);
  310. // C++: int cv::ml::LogisticRegression::getIterations()
  311. [DllImport(LIBNAME)]
  312. private static extern int ml_LogisticRegression_getIterations_10(IntPtr nativeObj);
  313. // C++: void cv::ml::LogisticRegression::setIterations(int val)
  314. [DllImport(LIBNAME)]
  315. private static extern void ml_LogisticRegression_setIterations_10(IntPtr nativeObj, int val);
  316. // C++: int cv::ml::LogisticRegression::getRegularization()
  317. [DllImport(LIBNAME)]
  318. private static extern int ml_LogisticRegression_getRegularization_10(IntPtr nativeObj);
  319. // C++: void cv::ml::LogisticRegression::setRegularization(int val)
  320. [DllImport(LIBNAME)]
  321. private static extern void ml_LogisticRegression_setRegularization_10(IntPtr nativeObj, int val);
  322. // C++: int cv::ml::LogisticRegression::getTrainMethod()
  323. [DllImport(LIBNAME)]
  324. private static extern int ml_LogisticRegression_getTrainMethod_10(IntPtr nativeObj);
  325. // C++: void cv::ml::LogisticRegression::setTrainMethod(int val)
  326. [DllImport(LIBNAME)]
  327. private static extern void ml_LogisticRegression_setTrainMethod_10(IntPtr nativeObj, int val);
  328. // C++: int cv::ml::LogisticRegression::getMiniBatchSize()
  329. [DllImport(LIBNAME)]
  330. private static extern int ml_LogisticRegression_getMiniBatchSize_10(IntPtr nativeObj);
  331. // C++: void cv::ml::LogisticRegression::setMiniBatchSize(int val)
  332. [DllImport(LIBNAME)]
  333. private static extern void ml_LogisticRegression_setMiniBatchSize_10(IntPtr nativeObj, int val);
  334. // C++: TermCriteria cv::ml::LogisticRegression::getTermCriteria()
  335. [DllImport(LIBNAME)]
  336. private static extern void ml_LogisticRegression_getTermCriteria_10(IntPtr nativeObj, double[] retVal);
  337. // C++: void cv::ml::LogisticRegression::setTermCriteria(TermCriteria val)
  338. [DllImport(LIBNAME)]
  339. private static extern void ml_LogisticRegression_setTermCriteria_10(IntPtr nativeObj, int val_type, int val_maxCount, double val_epsilon);
  340. // C++: float cv::ml::LogisticRegression::predict(Mat samples, Mat& results = Mat(), int flags = 0)
  341. [DllImport(LIBNAME)]
  342. private static extern float ml_LogisticRegression_predict_10(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj, int flags);
  343. [DllImport(LIBNAME)]
  344. private static extern float ml_LogisticRegression_predict_11(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj);
  345. [DllImport(LIBNAME)]
  346. private static extern float ml_LogisticRegression_predict_12(IntPtr nativeObj, IntPtr samples_nativeObj);
  347. // C++: Mat cv::ml::LogisticRegression::get_learnt_thetas()
  348. [DllImport(LIBNAME)]
  349. private static extern IntPtr ml_LogisticRegression_get_1learnt_1thetas_10(IntPtr nativeObj);
  350. // C++: static Ptr_LogisticRegression cv::ml::LogisticRegression::create()
  351. [DllImport(LIBNAME)]
  352. private static extern IntPtr ml_LogisticRegression_create_10();
  353. // C++: static Ptr_LogisticRegression cv::ml::LogisticRegression::load(String filepath, String nodeName = String())
  354. [DllImport(LIBNAME)]
  355. private static extern IntPtr ml_LogisticRegression_load_10(string filepath, string nodeName);
  356. [DllImport(LIBNAME)]
  357. private static extern IntPtr ml_LogisticRegression_load_11(string filepath);
  358. // native support for java finalize()
  359. [DllImport(LIBNAME)]
  360. private static extern void ml_LogisticRegression_delete(IntPtr nativeObj);
  361. }
  362. }