SVMSGD.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 SVMSGD
  9. /**
  10. * *************************************************************************************\
  11. * Stochastic Gradient Descent SVM Classifier *
  12. * \***************************************************************************************
  13. */
  14. public class SVMSGD : 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_SVMSGD_delete(nativeObj);
  27. nativeObj = IntPtr.Zero;
  28. }
  29. }
  30. finally
  31. {
  32. base.Dispose(disposing);
  33. }
  34. }
  35. protected internal SVMSGD(IntPtr addr) : base(addr) { }
  36. // internal usage only
  37. public static new SVMSGD __fromPtr__(IntPtr addr) { return new SVMSGD(addr); }
  38. // C++: enum cv.ml.SVMSGD.MarginType
  39. public const int SOFT_MARGIN = 0;
  40. public const int HARD_MARGIN = 1;
  41. // C++: enum cv.ml.SVMSGD.SvmsgdType
  42. public const int SGD = 0;
  43. public const int ASGD = 1;
  44. //
  45. // C++: Mat cv::ml::SVMSGD::getWeights()
  46. //
  47. /**
  48. * return the weights of the trained model (decision function f(x) = weights * x + shift).
  49. */
  50. public Mat getWeights()
  51. {
  52. ThrowIfDisposed();
  53. return new Mat(DisposableObject.ThrowIfNullIntPtr(ml_SVMSGD_getWeights_10(nativeObj)));
  54. }
  55. //
  56. // C++: float cv::ml::SVMSGD::getShift()
  57. //
  58. /**
  59. * return the shift of the trained model (decision function f(x) = weights * x + shift).
  60. */
  61. public float getShift()
  62. {
  63. ThrowIfDisposed();
  64. return ml_SVMSGD_getShift_10(nativeObj);
  65. }
  66. //
  67. // C++: static Ptr_SVMSGD cv::ml::SVMSGD::create()
  68. //
  69. /**
  70. * Creates empty model.
  71. * Use StatModel::train to train the model. Since %SVMSGD has several parameters, you may want to
  72. * find the best parameters for your problem or use setOptimalParameters() to set some default parameters.
  73. * return automatically generated
  74. */
  75. public static SVMSGD create()
  76. {
  77. return SVMSGD.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_SVMSGD_create_10()));
  78. }
  79. //
  80. // C++: static Ptr_SVMSGD cv::ml::SVMSGD::load(String filepath, String nodeName = String())
  81. //
  82. /**
  83. * Loads and creates a serialized SVMSGD from a file
  84. *
  85. * Use SVMSGD::save to serialize and store an SVMSGD to disk.
  86. * Load the SVMSGD from this file again, by calling this function with the path to the file.
  87. * Optionally specify the node for the file containing the classifier
  88. *
  89. * param filepath path to serialized SVMSGD
  90. * param nodeName name of node containing the classifier
  91. * return automatically generated
  92. */
  93. public static SVMSGD load(string filepath, string nodeName)
  94. {
  95. return SVMSGD.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_SVMSGD_load_10(filepath, nodeName)));
  96. }
  97. /**
  98. * Loads and creates a serialized SVMSGD from a file
  99. *
  100. * Use SVMSGD::save to serialize and store an SVMSGD to disk.
  101. * Load the SVMSGD from this file again, by calling this function with the path to the file.
  102. * Optionally specify the node for the file containing the classifier
  103. *
  104. * param filepath path to serialized SVMSGD
  105. * return automatically generated
  106. */
  107. public static SVMSGD load(string filepath)
  108. {
  109. return SVMSGD.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_SVMSGD_load_11(filepath)));
  110. }
  111. //
  112. // C++: void cv::ml::SVMSGD::setOptimalParameters(int svmsgdType = SVMSGD::ASGD, int marginType = SVMSGD::SOFT_MARGIN)
  113. //
  114. /**
  115. * Function sets optimal parameters values for chosen SVM SGD model.
  116. * param svmsgdType is the type of SVMSGD classifier.
  117. * param marginType is the type of margin constraint.
  118. */
  119. public void setOptimalParameters(int svmsgdType, int marginType)
  120. {
  121. ThrowIfDisposed();
  122. ml_SVMSGD_setOptimalParameters_10(nativeObj, svmsgdType, marginType);
  123. }
  124. /**
  125. * Function sets optimal parameters values for chosen SVM SGD model.
  126. * param svmsgdType is the type of SVMSGD classifier.
  127. */
  128. public void setOptimalParameters(int svmsgdType)
  129. {
  130. ThrowIfDisposed();
  131. ml_SVMSGD_setOptimalParameters_11(nativeObj, svmsgdType);
  132. }
  133. /**
  134. * Function sets optimal parameters values for chosen SVM SGD model.
  135. */
  136. public void setOptimalParameters()
  137. {
  138. ThrowIfDisposed();
  139. ml_SVMSGD_setOptimalParameters_12(nativeObj);
  140. }
  141. //
  142. // C++: int cv::ml::SVMSGD::getSvmsgdType()
  143. //
  144. /**
  145. * SEE: setSvmsgdType
  146. * return automatically generated
  147. */
  148. public int getSvmsgdType()
  149. {
  150. ThrowIfDisposed();
  151. return ml_SVMSGD_getSvmsgdType_10(nativeObj);
  152. }
  153. //
  154. // C++: void cv::ml::SVMSGD::setSvmsgdType(int svmsgdType)
  155. //
  156. /**
  157. * getSvmsgdType SEE: getSvmsgdType
  158. * param svmsgdType automatically generated
  159. */
  160. public void setSvmsgdType(int svmsgdType)
  161. {
  162. ThrowIfDisposed();
  163. ml_SVMSGD_setSvmsgdType_10(nativeObj, svmsgdType);
  164. }
  165. //
  166. // C++: int cv::ml::SVMSGD::getMarginType()
  167. //
  168. /**
  169. * SEE: setMarginType
  170. * return automatically generated
  171. */
  172. public int getMarginType()
  173. {
  174. ThrowIfDisposed();
  175. return ml_SVMSGD_getMarginType_10(nativeObj);
  176. }
  177. //
  178. // C++: void cv::ml::SVMSGD::setMarginType(int marginType)
  179. //
  180. /**
  181. * getMarginType SEE: getMarginType
  182. * param marginType automatically generated
  183. */
  184. public void setMarginType(int marginType)
  185. {
  186. ThrowIfDisposed();
  187. ml_SVMSGD_setMarginType_10(nativeObj, marginType);
  188. }
  189. //
  190. // C++: float cv::ml::SVMSGD::getMarginRegularization()
  191. //
  192. /**
  193. * SEE: setMarginRegularization
  194. * return automatically generated
  195. */
  196. public float getMarginRegularization()
  197. {
  198. ThrowIfDisposed();
  199. return ml_SVMSGD_getMarginRegularization_10(nativeObj);
  200. }
  201. //
  202. // C++: void cv::ml::SVMSGD::setMarginRegularization(float marginRegularization)
  203. //
  204. /**
  205. * getMarginRegularization SEE: getMarginRegularization
  206. * param marginRegularization automatically generated
  207. */
  208. public void setMarginRegularization(float marginRegularization)
  209. {
  210. ThrowIfDisposed();
  211. ml_SVMSGD_setMarginRegularization_10(nativeObj, marginRegularization);
  212. }
  213. //
  214. // C++: float cv::ml::SVMSGD::getInitialStepSize()
  215. //
  216. /**
  217. * SEE: setInitialStepSize
  218. * return automatically generated
  219. */
  220. public float getInitialStepSize()
  221. {
  222. ThrowIfDisposed();
  223. return ml_SVMSGD_getInitialStepSize_10(nativeObj);
  224. }
  225. //
  226. // C++: void cv::ml::SVMSGD::setInitialStepSize(float InitialStepSize)
  227. //
  228. /**
  229. * getInitialStepSize SEE: getInitialStepSize
  230. * param InitialStepSize automatically generated
  231. */
  232. public void setInitialStepSize(float InitialStepSize)
  233. {
  234. ThrowIfDisposed();
  235. ml_SVMSGD_setInitialStepSize_10(nativeObj, InitialStepSize);
  236. }
  237. //
  238. // C++: float cv::ml::SVMSGD::getStepDecreasingPower()
  239. //
  240. /**
  241. * SEE: setStepDecreasingPower
  242. * return automatically generated
  243. */
  244. public float getStepDecreasingPower()
  245. {
  246. ThrowIfDisposed();
  247. return ml_SVMSGD_getStepDecreasingPower_10(nativeObj);
  248. }
  249. //
  250. // C++: void cv::ml::SVMSGD::setStepDecreasingPower(float stepDecreasingPower)
  251. //
  252. /**
  253. * getStepDecreasingPower SEE: getStepDecreasingPower
  254. * param stepDecreasingPower automatically generated
  255. */
  256. public void setStepDecreasingPower(float stepDecreasingPower)
  257. {
  258. ThrowIfDisposed();
  259. ml_SVMSGD_setStepDecreasingPower_10(nativeObj, stepDecreasingPower);
  260. }
  261. //
  262. // C++: TermCriteria cv::ml::SVMSGD::getTermCriteria()
  263. //
  264. /**
  265. * SEE: setTermCriteria
  266. * return automatically generated
  267. */
  268. public TermCriteria getTermCriteria()
  269. {
  270. ThrowIfDisposed();
  271. double[] tmpArray = new double[3];
  272. ml_SVMSGD_getTermCriteria_10(nativeObj, tmpArray);
  273. TermCriteria retVal = new TermCriteria(tmpArray);
  274. return retVal;
  275. }
  276. //
  277. // C++: void cv::ml::SVMSGD::setTermCriteria(TermCriteria val)
  278. //
  279. /**
  280. * getTermCriteria SEE: getTermCriteria
  281. * param val automatically generated
  282. */
  283. public void setTermCriteria(TermCriteria val)
  284. {
  285. ThrowIfDisposed();
  286. ml_SVMSGD_setTermCriteria_10(nativeObj, val.type, val.maxCount, val.epsilon);
  287. }
  288. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  289. const string LIBNAME = "__Internal";
  290. #else
  291. const string LIBNAME = "opencvforunity";
  292. #endif
  293. // C++: Mat cv::ml::SVMSGD::getWeights()
  294. [DllImport(LIBNAME)]
  295. private static extern IntPtr ml_SVMSGD_getWeights_10(IntPtr nativeObj);
  296. // C++: float cv::ml::SVMSGD::getShift()
  297. [DllImport(LIBNAME)]
  298. private static extern float ml_SVMSGD_getShift_10(IntPtr nativeObj);
  299. // C++: static Ptr_SVMSGD cv::ml::SVMSGD::create()
  300. [DllImport(LIBNAME)]
  301. private static extern IntPtr ml_SVMSGD_create_10();
  302. // C++: static Ptr_SVMSGD cv::ml::SVMSGD::load(String filepath, String nodeName = String())
  303. [DllImport(LIBNAME)]
  304. private static extern IntPtr ml_SVMSGD_load_10(string filepath, string nodeName);
  305. [DllImport(LIBNAME)]
  306. private static extern IntPtr ml_SVMSGD_load_11(string filepath);
  307. // C++: void cv::ml::SVMSGD::setOptimalParameters(int svmsgdType = SVMSGD::ASGD, int marginType = SVMSGD::SOFT_MARGIN)
  308. [DllImport(LIBNAME)]
  309. private static extern void ml_SVMSGD_setOptimalParameters_10(IntPtr nativeObj, int svmsgdType, int marginType);
  310. [DllImport(LIBNAME)]
  311. private static extern void ml_SVMSGD_setOptimalParameters_11(IntPtr nativeObj, int svmsgdType);
  312. [DllImport(LIBNAME)]
  313. private static extern void ml_SVMSGD_setOptimalParameters_12(IntPtr nativeObj);
  314. // C++: int cv::ml::SVMSGD::getSvmsgdType()
  315. [DllImport(LIBNAME)]
  316. private static extern int ml_SVMSGD_getSvmsgdType_10(IntPtr nativeObj);
  317. // C++: void cv::ml::SVMSGD::setSvmsgdType(int svmsgdType)
  318. [DllImport(LIBNAME)]
  319. private static extern void ml_SVMSGD_setSvmsgdType_10(IntPtr nativeObj, int svmsgdType);
  320. // C++: int cv::ml::SVMSGD::getMarginType()
  321. [DllImport(LIBNAME)]
  322. private static extern int ml_SVMSGD_getMarginType_10(IntPtr nativeObj);
  323. // C++: void cv::ml::SVMSGD::setMarginType(int marginType)
  324. [DllImport(LIBNAME)]
  325. private static extern void ml_SVMSGD_setMarginType_10(IntPtr nativeObj, int marginType);
  326. // C++: float cv::ml::SVMSGD::getMarginRegularization()
  327. [DllImport(LIBNAME)]
  328. private static extern float ml_SVMSGD_getMarginRegularization_10(IntPtr nativeObj);
  329. // C++: void cv::ml::SVMSGD::setMarginRegularization(float marginRegularization)
  330. [DllImport(LIBNAME)]
  331. private static extern void ml_SVMSGD_setMarginRegularization_10(IntPtr nativeObj, float marginRegularization);
  332. // C++: float cv::ml::SVMSGD::getInitialStepSize()
  333. [DllImport(LIBNAME)]
  334. private static extern float ml_SVMSGD_getInitialStepSize_10(IntPtr nativeObj);
  335. // C++: void cv::ml::SVMSGD::setInitialStepSize(float InitialStepSize)
  336. [DllImport(LIBNAME)]
  337. private static extern void ml_SVMSGD_setInitialStepSize_10(IntPtr nativeObj, float InitialStepSize);
  338. // C++: float cv::ml::SVMSGD::getStepDecreasingPower()
  339. [DllImport(LIBNAME)]
  340. private static extern float ml_SVMSGD_getStepDecreasingPower_10(IntPtr nativeObj);
  341. // C++: void cv::ml::SVMSGD::setStepDecreasingPower(float stepDecreasingPower)
  342. [DllImport(LIBNAME)]
  343. private static extern void ml_SVMSGD_setStepDecreasingPower_10(IntPtr nativeObj, float stepDecreasingPower);
  344. // C++: TermCriteria cv::ml::SVMSGD::getTermCriteria()
  345. [DllImport(LIBNAME)]
  346. private static extern void ml_SVMSGD_getTermCriteria_10(IntPtr nativeObj, double[] retVal);
  347. // C++: void cv::ml::SVMSGD::setTermCriteria(TermCriteria val)
  348. [DllImport(LIBNAME)]
  349. private static extern void ml_SVMSGD_setTermCriteria_10(IntPtr nativeObj, int val_type, int val_maxCount, double val_epsilon);
  350. // native support for java finalize()
  351. [DllImport(LIBNAME)]
  352. private static extern void ml_SVMSGD_delete(IntPtr nativeObj);
  353. }
  354. }