flip.py 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. """
  2. Augmenters that apply mirroring/flipping operations to images.
  3. List of augmenters:
  4. * :class:`Fliplr`
  5. * :class:`Flipud`
  6. """
  7. from __future__ import print_function, division, absolute_import
  8. import numpy as np
  9. import cv2
  10. import six.moves as sm
  11. from imgaug.imgaug import _normalize_cv2_input_arr_
  12. from . import meta
  13. from .. import parameters as iap
  14. # pylint:disable=pointless-string-statement
  15. """
  16. Speed comparison by datatype and flip method.
  17. HORIZONTAL FLIPS.
  18. ----------
  19. bool
  20. ----------
  21. slice 0.00052ms
  22. slice, contig 0.21878ms
  23. fliplr 0.00180ms
  24. fliplr contig 0.22000ms
  25. cv2 Error: Expected cv::UMat for argument 'src'
  26. cv2 contig Error: Expected cv::UMat for argument 'src'
  27. fort cv2 Error: Expected cv::UMat for argument 'src'
  28. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  29. cv2_ Error: Expected cv::UMat for argument 'src'
  30. cv2_ contig Error: Expected cv::UMat for argument 'src'
  31. fort cv2_ Error: Expected cv::UMat for argument 'src'
  32. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  33. cv2_ get Error: Expected cv::UMat for argument 'src'
  34. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  35. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  36. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  37. ----------
  38. uint8
  39. ----------
  40. slice 0.00052ms
  41. slice, contig 0.21878ms
  42. fliplr 0.00174ms
  43. fliplr contig 0.21828ms
  44. cv2 0.07037ms (3.11x)
  45. cv2 contig 0.07355ms (2.97x)
  46. fort cv2 0.33900ms (0.65x)
  47. fort cv2 contig 0.34198ms (0.64x)
  48. cv2_ 0.08093ms (2.70x)
  49. cv2_ contig 0.08554ms (2.56x)
  50. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  51. fort cv2_ contig Error: Got dtype object
  52. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  53. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  54. fort cv2_ get 1.25783ms (0.17x)
  55. fort cv2_ get contig 1.25868ms (0.17x)
  56. ----------
  57. uint16
  58. ----------
  59. slice 0.00176ms
  60. slice, contig 0.21250ms
  61. fliplr 0.00489ms
  62. fliplr contig 0.21438ms
  63. cv2 0.16964ms (1.25x)
  64. cv2 contig 0.17314ms (1.23x)
  65. fort cv2 0.50989ms (0.42x)
  66. fort cv2 contig 0.51188ms (0.42x)
  67. cv2_ 0.18803ms (1.13x)
  68. cv2_ contig 0.19136ms (1.11x)
  69. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  70. fort cv2_ contig Error: Got dtype object
  71. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  72. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  73. fort cv2_ get 1.48429ms (0.14x)
  74. fort cv2_ get contig 1.48392ms (0.14x)
  75. ----------
  76. uint32
  77. ----------
  78. slice 0.00181ms
  79. slice, contig 0.22855ms
  80. fliplr 0.00486ms
  81. fliplr contig 0.23070ms
  82. cv2 Error: Expected cv::UMat for argument 'src'
  83. cv2 contig Error: Expected cv::UMat for argument 'src'
  84. fort cv2 Error: Expected cv::UMat for argument 'src'
  85. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  86. cv2_ Error: Expected cv::UMat for argument 'src'
  87. cv2_ contig Error: Expected cv::UMat for argument 'src'
  88. fort cv2_ Error: Expected cv::UMat for argument 'src'
  89. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  90. cv2_ get Error: Expected cv::UMat for argument 'src'
  91. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  92. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  93. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  94. ----------
  95. uint64
  96. ----------
  97. slice 0.00175ms
  98. slice, contig 0.28662ms
  99. fliplr 0.00497ms
  100. fliplr contig 0.28986ms
  101. cv2 Error: Got dtype int32
  102. cv2 contig Error: Got dtype int32
  103. fort cv2 Error: Got dtype int32
  104. fort cv2 contig Error: Got dtype int32
  105. cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  106. cv2_ contig Error: Got dtype object
  107. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  108. fort cv2_ contig Error: Got dtype object
  109. cv2_ get Error: Got dtype int32
  110. cv2_ get contig Error: Got dtype int32
  111. fort cv2_ get Error: Got dtype int32
  112. fort cv2_ get contig Error: Got dtype int32
  113. ----------
  114. int8
  115. ----------
  116. slice 0.00052ms
  117. slice, contig 0.21802ms
  118. fliplr 0.00183ms
  119. fliplr contig 0.21866ms
  120. cv2 0.07026ms (3.10x)
  121. cv2 contig 0.07234ms (3.01x)
  122. fort cv2 0.34137ms (0.64x)
  123. fort cv2 contig 0.35426ms (0.62x)
  124. cv2_ 0.08145ms (2.68x)
  125. cv2_ contig 0.08498ms (2.57x)
  126. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  127. fort cv2_ contig Error: Got dtype object
  128. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  129. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  130. fort cv2_ get 1.27798ms (0.17x)
  131. fort cv2_ get contig 1.26791ms (0.17x)
  132. ----------
  133. int16
  134. ----------
  135. slice 0.00146ms
  136. slice, contig 0.21083ms
  137. fliplr 0.00443ms
  138. fliplr contig 0.21287ms
  139. cv2 0.17461ms (1.21x)
  140. cv2 contig 0.17523ms (1.20x)
  141. fort cv2 0.51030ms (0.41x)
  142. fort cv2 contig 0.50438ms (0.42x)
  143. cv2_ 0.18627ms (1.13x)
  144. cv2_ contig 0.19703ms (1.07x)
  145. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  146. fort cv2_ contig Error: Got dtype object
  147. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  148. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  149. fort cv2_ get 1.47157ms (0.14x)
  150. fort cv2_ get contig 1.48715ms (0.14x)
  151. ----------
  152. int32
  153. ----------
  154. slice 0.00177ms
  155. slice, contig 0.22641ms
  156. fliplr 0.00505ms
  157. fliplr contig 0.22934ms
  158. cv2 0.33548ms (0.67x)
  159. cv2 contig 0.34303ms (0.66x)
  160. fort cv2 0.77874ms (0.29x)
  161. fort cv2 contig 0.78380ms (0.29x)
  162. cv2_ 0.39785ms (0.57x)
  163. cv2_ contig 0.39249ms (0.58x)
  164. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  165. fort cv2_ contig Error: Got dtype object
  166. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  167. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  168. fort cv2_ get 1.89115ms (0.12x)
  169. fort cv2_ get contig 1.89214ms (0.12x)
  170. ----------
  171. int64
  172. ----------
  173. slice 0.00176ms
  174. slice, contig 0.28347ms
  175. fliplr 0.00484ms
  176. fliplr contig 0.28658ms
  177. cv2 Error: Got dtype int32
  178. cv2 contig Error: Got dtype int32
  179. fort cv2 Error: Got dtype int32
  180. fort cv2 contig Error: Got dtype int32
  181. cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  182. cv2_ contig Error: Got dtype object
  183. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  184. fort cv2_ contig Error: Got dtype object
  185. cv2_ get Error: Got dtype int32
  186. cv2_ get contig Error: Got dtype int32
  187. fort cv2_ get Error: Got dtype int32
  188. fort cv2_ get contig Error: Got dtype int32
  189. ----------
  190. float16
  191. ----------
  192. slice 0.00155ms
  193. slice, contig 0.21066ms
  194. fliplr 0.00435ms
  195. fliplr contig 0.21295ms
  196. cv2 Error: Expected cv::UMat for argument 'src'
  197. cv2 contig Error: Expected cv::UMat for argument 'src'
  198. fort cv2 Error: Expected cv::UMat for argument 'src'
  199. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  200. cv2_ Error: Expected cv::UMat for argument 'src'
  201. cv2_ contig Error: Expected cv::UMat for argument 'src'
  202. fort cv2_ Error: Expected cv::UMat for argument 'src'
  203. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  204. cv2_ get Error: Expected cv::UMat for argument 'src'
  205. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  206. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  207. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  208. ----------
  209. float32
  210. ----------
  211. slice 0.00177ms
  212. slice, contig 0.22798ms
  213. fliplr 0.00495ms
  214. fliplr contig 0.22916ms
  215. cv2 0.33821ms (0.67x)
  216. cv2 contig 0.31665ms (0.72x)
  217. fort cv2 0.78119ms (0.29x)
  218. fort cv2 contig 0.77838ms (0.29x)
  219. cv2_ 0.39515ms (0.58x)
  220. cv2_ contig 0.41023ms (0.56x)
  221. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  222. fort cv2_ contig Error: Got dtype object
  223. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  224. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  225. fort cv2_ get 1.88737ms (0.12x)
  226. fort cv2_ get contig 1.89616ms (0.12x)
  227. ----------
  228. float64
  229. ----------
  230. slice 0.00179ms
  231. slice, contig 0.28810ms
  232. fliplr 0.00495ms
  233. fliplr contig 0.29130ms
  234. cv2 0.63258ms (0.46x)
  235. cv2 contig 0.64089ms (0.45x)
  236. fort cv2 1.64795ms (0.17x)
  237. fort cv2 contig 1.65449ms (0.17x)
  238. cv2_ 0.75202ms (0.38x)
  239. cv2_ contig 0.74789ms (0.39x)
  240. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  241. fort cv2_ contig Error: Got dtype object
  242. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  243. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  244. fort cv2_ get 3.99237ms (0.07x)
  245. fort cv2_ get contig 3.97847ms (0.07x)
  246. ----------
  247. float128
  248. ----------
  249. slice 0.00179ms
  250. slice, contig 0.51371ms
  251. fliplr 0.00545ms
  252. fliplr contig 0.51618ms
  253. cv2 Error: Expected cv::UMat for argument 'src'
  254. cv2 contig Error: Expected cv::UMat for argument 'src'
  255. fort cv2 Error: Expected cv::UMat for argument 'src'
  256. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  257. cv2_ Error: Expected cv::UMat for argument 'src'
  258. cv2_ contig Error: Expected cv::UMat for argument 'src'
  259. fort cv2_ Error: Expected cv::UMat for argument 'src'
  260. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  261. cv2_ get Error: Expected cv::UMat for argument 'src'
  262. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  263. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  264. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  265. ==============================
  266. flip method followed by Add
  267. ==============================
  268. slice 1.29597ms
  269. slice, contig 1.32523ms
  270. fliplr 1.29298ms
  271. fliplr contig 1.33087ms
  272. cv2 1.17829ms
  273. cv2 contig 1.18350ms
  274. fort cv2 1.45182ms
  275. fort cv2 contig 1.45762ms
  276. cv2_ 1.19331ms
  277. cv2_ contig 1.19364ms
  278. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  279. fort cv2_ contig Error: Got dtype object
  280. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  281. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  282. fort cv2_ get 2.43293ms
  283. fort cv2_ get contig 2.48836ms
  284. ==============================
  285. flip method followed by Affine
  286. ==============================
  287. slice 2.83081ms
  288. slice, contig 2.88243ms
  289. fliplr 2.84253ms
  290. fliplr contig 2.89106ms
  291. cv2 2.72900ms
  292. cv2 contig 2.74500ms
  293. fort cv2 2.99842ms
  294. fort cv2 contig 3.03457ms
  295. cv2_ 2.73629ms
  296. cv2_ contig 2.77505ms
  297. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  298. fort cv2_ contig Error: Got dtype object
  299. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  300. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  301. fort cv2_ get 4.01583ms
  302. fort cv2_ get contig 4.03347ms
  303. ==============================
  304. flip method followed by AverageBlur
  305. ==============================
  306. slice 0.77109ms
  307. slice, contig 0.80603ms
  308. fliplr 0.77666ms
  309. fliplr contig 0.81088ms
  310. cv2 0.66065ms
  311. cv2 contig 0.66496ms
  312. fort cv2 0.94078ms
  313. fort cv2 contig 0.92662ms
  314. cv2_ 0.65560ms
  315. cv2_ contig 0.66237ms
  316. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  317. fort cv2_ contig Error: Got dtype object
  318. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  319. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  320. fort cv2_ get 1.88932ms
  321. fort cv2_ get contig 1.89190ms
  322. """
  323. """
  324. Speed comparison by datatype and flip method.
  325. VERTICAL FLIPS.
  326. ----------
  327. bool
  328. ----------
  329. slice 0.00047ms
  330. slice, contig 0.02332ms
  331. flipud 0.00143ms
  332. flipud contig 0.02502ms
  333. cv2 Error: Expected cv::UMat for argument 'src'
  334. cv2 contig Error: Expected cv::UMat for argument 'src'
  335. fort cv2 Error: Expected cv::UMat for argument 'src'
  336. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  337. cv2_ Error: Expected cv::UMat for argument 'src'
  338. cv2_ contig Error: Expected cv::UMat for argument 'src'
  339. fort cv2_ Error: Expected cv::UMat for argument 'src'
  340. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  341. cv2_ get Error: Expected cv::UMat for argument 'src'
  342. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  343. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  344. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  345. ----------
  346. uint8
  347. ----------
  348. slice 0.00049ms
  349. slice, contig 0.02311ms
  350. flipud 0.00155ms
  351. flipud contig 0.02506ms
  352. cv2 0.03030ms (0.76x)
  353. cv2 contig 0.03401ms (0.68x)
  354. fort cv2 0.31000ms (0.07x)
  355. fort cv2 contig 0.33619ms (0.07x)
  356. cv2_ 0.01753ms (1.32x)
  357. cv2_ contig 0.01841ms (1.26x)
  358. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  359. fort cv2_ contig Error: Got dtype object
  360. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  361. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  362. fort cv2_ get 1.26680ms (0.02x)
  363. fort cv2_ get contig 1.27027ms (0.02x)
  364. ----------
  365. uint16
  366. ----------
  367. slice 0.00147ms
  368. slice, contig 0.04861ms
  369. flipud 0.00406ms
  370. flipud contig 0.05060ms
  371. cv2 0.06169ms (0.79x)
  372. cv2 contig 0.06397ms (0.76x)
  373. fort cv2 0.39190ms (0.12x)
  374. fort cv2 contig 0.39098ms (0.12x)
  375. cv2_ 0.03375ms (1.44x)
  376. cv2_ contig 0.03619ms (1.34x)
  377. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  378. fort cv2_ contig Error: Got dtype object
  379. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  380. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  381. fort cv2_ get 1.51197ms (0.03x)
  382. fort cv2_ get contig 1.52977ms (0.03x)
  383. ----------
  384. uint32
  385. ----------
  386. slice 0.00167ms
  387. slice, contig 0.09415ms
  388. flipud 0.00476ms
  389. flipud contig 0.09660ms
  390. cv2 Error: Expected cv::UMat for argument 'src'
  391. cv2 contig Error: Expected cv::UMat for argument 'src'
  392. fort cv2 Error: Expected cv::UMat for argument 'src'
  393. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  394. cv2_ Error: Expected cv::UMat for argument 'src'
  395. cv2_ contig Error: Expected cv::UMat for argument 'src'
  396. fort cv2_ Error: Expected cv::UMat for argument 'src'
  397. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  398. cv2_ get Error: Expected cv::UMat for argument 'src'
  399. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  400. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  401. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  402. ----------
  403. uint64
  404. ----------
  405. slice 0.00175ms
  406. slice, contig 0.17413ms
  407. flipud 0.00488ms
  408. flipud contig 0.17508ms
  409. cv2 Error: Got dtype int32
  410. cv2 contig Error: Got dtype int32
  411. fort cv2 Error: Got dtype int32
  412. fort cv2 contig Error: Got dtype int32
  413. cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  414. cv2_ contig Error: Got dtype object
  415. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  416. fort cv2_ contig Error: Got dtype object
  417. cv2_ get Error: Got dtype int32
  418. cv2_ get contig Error: Got dtype int32
  419. fort cv2_ get Error: Got dtype int32
  420. fort cv2_ get contig Error: Got dtype int32
  421. ----------
  422. int8
  423. ----------
  424. slice 0.00057ms
  425. slice, contig 0.02604ms
  426. flipud 0.00146ms
  427. flipud contig 0.02806ms
  428. cv2 0.03434ms (0.76x)
  429. cv2 contig 0.03676ms (0.71x)
  430. fort cv2 0.30920ms (0.08x)
  431. fort cv2 contig 0.31123ms (0.08x)
  432. cv2_ 0.01625ms (1.60x)
  433. cv2_ contig 0.01799ms (1.45x)
  434. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  435. fort cv2_ contig Error: Got dtype object
  436. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  437. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  438. fort cv2_ get 1.29065ms (0.02x)
  439. fort cv2_ get contig 1.29596ms (0.02x)
  440. ----------
  441. int16
  442. ----------
  443. slice 0.00142ms
  444. slice, contig 0.05079ms
  445. flipud 0.00403ms
  446. flipud contig 0.05312ms
  447. cv2 0.06277ms (0.81x)
  448. cv2 contig 0.06527ms (0.78x)
  449. fort cv2 0.39710ms (0.13x)
  450. fort cv2 contig 0.39851ms (0.13x)
  451. cv2_ 0.03419ms (1.49x)
  452. cv2_ contig 0.03668ms (1.38x)
  453. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  454. fort cv2_ contig Error: Got dtype object
  455. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  456. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  457. fort cv2_ get 1.52476ms (0.03x)
  458. fort cv2_ get contig 1.52594ms (0.03x)
  459. ----------
  460. int32
  461. ----------
  462. slice 0.00172ms
  463. slice, contig 0.09621ms
  464. flipud 0.00469ms
  465. flipud contig 0.09868ms
  466. cv2 0.12192ms (0.79x)
  467. cv2 contig 0.12459ms (0.77x)
  468. fort cv2 0.57915ms (0.17x)
  469. fort cv2 contig 0.58571ms (0.16x)
  470. cv2_ 0.07337ms (1.31x)
  471. cv2_ contig 0.07595ms (1.27x)
  472. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  473. fort cv2_ contig Error: Got dtype object
  474. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  475. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  476. fort cv2_ get 1.97937ms (0.05x)
  477. fort cv2_ get contig 1.97919ms (0.05x)
  478. ----------
  479. int64
  480. ----------
  481. slice 0.00167ms
  482. slice, contig 0.17308ms
  483. flipud 0.00470ms
  484. flipud contig 0.17542ms
  485. cv2 Error: Got dtype int32
  486. cv2 contig Error: Got dtype int32
  487. fort cv2 Error: Got dtype int32
  488. fort cv2 contig Error: Got dtype int32
  489. cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  490. cv2_ contig Error: Got dtype object
  491. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  492. fort cv2_ contig Error: Got dtype object
  493. cv2_ get Error: Got dtype int32
  494. cv2_ get contig Error: Got dtype int32
  495. fort cv2_ get Error: Got dtype int32
  496. fort cv2_ get contig Error: Got dtype int32
  497. ----------
  498. float16
  499. ----------
  500. slice 0.00146ms
  501. slice, contig 0.05094ms
  502. flipud 0.00408ms
  503. flipud contig 0.05359ms
  504. cv2 Error: Expected cv::UMat for argument 'src'
  505. cv2 contig Error: Expected cv::UMat for argument 'src'
  506. fort cv2 Error: Expected cv::UMat for argument 'src'
  507. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  508. cv2_ Error: Expected cv::UMat for argument 'src'
  509. cv2_ contig Error: Expected cv::UMat for argument 'src'
  510. fort cv2_ Error: Expected cv::UMat for argument 'src'
  511. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  512. cv2_ get Error: Expected cv::UMat for argument 'src'
  513. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  514. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  515. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  516. ----------
  517. float32
  518. ----------
  519. slice 0.00164ms
  520. slice, contig 0.09606ms
  521. flipud 0.00461ms
  522. flipud contig 0.09897ms
  523. cv2 0.12151ms (0.79x)
  524. cv2 contig 0.12514ms (0.77x)
  525. fort cv2 0.57887ms (0.17x)
  526. fort cv2 contig 0.58280ms (0.16x)
  527. cv2_ 0.07312ms (1.31x)
  528. cv2_ contig 0.07581ms (1.27x)
  529. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  530. fort cv2_ contig Error: Got dtype object
  531. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  532. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  533. fort cv2_ get 1.97501ms (0.05x)
  534. fort cv2_ get contig 1.98010ms (0.05x)
  535. ----------
  536. float64
  537. ----------
  538. slice 0.00170ms
  539. slice, contig 0.17300ms
  540. flipud 0.00483ms
  541. flipud contig 0.17539ms
  542. cv2 0.23941ms (0.72x)
  543. cv2 contig 0.24220ms (0.71x)
  544. fort cv2 1.26645ms (0.14x)
  545. fort cv2 contig 1.27246ms (0.14x)
  546. cv2_ 0.14879ms (1.16x)
  547. cv2_ contig 0.15185ms (1.14x)
  548. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  549. fort cv2_ contig Error: Got dtype object
  550. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  551. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  552. fort cv2_ get 4.02958ms (0.04x)
  553. fort cv2_ get contig 4.02822ms (0.04x)
  554. ----------
  555. float128
  556. ----------
  557. slice 0.00168ms
  558. slice, contig 0.35407ms
  559. flipud 0.00513ms
  560. flipud contig 0.35716ms
  561. cv2 Error: Expected cv::UMat for argument 'src'
  562. cv2 contig Error: Expected cv::UMat for argument 'src'
  563. fort cv2 Error: Expected cv::UMat for argument 'src'
  564. fort cv2 contig Error: Expected cv::UMat for argument 'src'
  565. cv2_ Error: Expected cv::UMat for argument 'src'
  566. cv2_ contig Error: Expected cv::UMat for argument 'src'
  567. fort cv2_ Error: Expected cv::UMat for argument 'src'
  568. fort cv2_ contig Error: Expected cv::UMat for argument 'src'
  569. cv2_ get Error: Expected cv::UMat for argument 'src'
  570. cv2_ get contig Error: Expected cv::UMat for argument 'src'
  571. fort cv2_ get Error: Expected cv::UMat for argument 'src'
  572. fort cv2_ get contig Error: Expected cv::UMat for argument 'src'
  573. ==============================
  574. flip method followed by Add
  575. ==============================
  576. slice 1.11286ms
  577. slice, contig 1.14822ms
  578. flipud 1.11858ms
  579. flipud contig 1.14703ms
  580. cv2 1.14892ms
  581. cv2 contig 1.15923ms
  582. fort cv2 1.43633ms
  583. fort cv2 contig 1.44342ms
  584. cv2_ 1.15194ms
  585. cv2_ contig 1.14864ms
  586. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  587. fort cv2_ contig Error: Got dtype object
  588. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  589. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  590. fort cv2_ get 2.47966ms
  591. fort cv2_ get contig 2.48177ms
  592. ==============================
  593. flip method followed by Affine
  594. ==============================
  595. slice 2.61450ms
  596. slice, contig 2.67402ms
  597. flipud 2.62069ms
  598. flipud contig 2.67267ms
  599. cv2 2.66312ms
  600. cv2 contig 2.68482ms
  601. fort cv2 2.94520ms
  602. fort cv2 contig 2.97767ms
  603. cv2_ 2.64183ms
  604. cv2_ contig 2.64857ms
  605. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  606. fort cv2_ contig Error: Got dtype object
  607. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  608. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  609. fort cv2_ get 3.94321ms
  610. fort cv2_ get contig 4.01652ms
  611. ==============================
  612. flip method followed by AverageBlur
  613. ==============================
  614. slice 0.57750ms
  615. slice, contig 0.60905ms
  616. flipud 0.58156ms
  617. flipud contig 0.61562ms
  618. cv2 0.61704ms
  619. cv2 contig 0.62293ms
  620. fort cv2 0.90804ms
  621. fort cv2 contig 0.90495ms
  622. cv2_ 0.60047ms
  623. cv2_ contig 0.60876ms
  624. fort cv2_ Error: 'cv2.UMat' object has no attribute 'ndim'
  625. fort cv2_ contig Error: Got dtype object
  626. cv2_ get Error: 'numpy.ndarray' object has no attribute 'get'
  627. cv2_ get contig Error: 'numpy.ndarray' object has no attribute 'get'
  628. fort cv2_ get 1.93064ms
  629. fort cv2_ get contig 1.95215ms
  630. """
  631. # pylint:enable=pointless-string-statement
  632. _FLIPLR_DTYPES_CV2 = {"uint8", "uint16", "int8", "int16"}
  633. def fliplr(arr):
  634. """Flip an image-like array horizontally.
  635. **Supported dtypes**:
  636. * ``uint8``: yes; fully tested
  637. * ``uint16``: yes; fully tested
  638. * ``uint32``: yes; fully tested
  639. * ``uint64``: yes; fully tested
  640. * ``int8``: yes; fully tested
  641. * ``int16``: yes; fully tested
  642. * ``int32``: yes; fully tested
  643. * ``int64``: yes; fully tested
  644. * ``float16``: yes; fully tested
  645. * ``float32``: yes; fully tested
  646. * ``float64``: yes; fully tested
  647. * ``float128``: yes; fully tested
  648. * ``bool``: yes; fully tested
  649. Parameters
  650. ----------
  651. arr : ndarray
  652. A 2D/3D `(H, W, [C])` image array.
  653. Returns
  654. -------
  655. ndarray
  656. Horizontally flipped array.
  657. Examples
  658. --------
  659. >>> import numpy as np
  660. >>> import imgaug.augmenters.flip as flip
  661. >>> arr = np.arange(16).reshape((4, 4))
  662. >>> arr_flipped = flip.fliplr(arr)
  663. Create a ``4x4`` array and flip it horizontally.
  664. """
  665. # we don't check here if #channels > 512, because the cv2 function also
  666. # kinda works with that, it is very rare to happen and would induce an
  667. # additional check (with significant relative impact on runtime considering
  668. # flipping is already ultra fast)
  669. if arr.dtype.name in _FLIPLR_DTYPES_CV2:
  670. return _fliplr_cv2(arr)
  671. return _fliplr_sliced(arr)
  672. def _fliplr_sliced(arr):
  673. return arr[:, ::-1, ...]
  674. def _fliplr_cv2(arr):
  675. # cv2.flip() returns None for arrays with zero height or width
  676. # and turns channels=0 to channels=512
  677. if arr.size == 0:
  678. return np.copy(arr)
  679. # cv2.flip() fails for more than 512 channels
  680. if arr.ndim == 3 and arr.shape[-1] > 512:
  681. # TODO this is quite inefficient right now
  682. channels = [
  683. cv2.flip(_normalize_cv2_input_arr_(arr[..., c]), 1)
  684. for c
  685. in sm.xrange(arr.shape[-1])
  686. ]
  687. result = np.stack(channels, axis=-1)
  688. else:
  689. # Normalization from imgaug.imgaug._normalize_cv2_input_arr_().
  690. # Moved here for performance reasons. Keep this aligned.
  691. # TODO recalculate timings, they were computed without this.
  692. flags = arr.flags
  693. if not flags["OWNDATA"]:
  694. arr = np.copy(arr)
  695. flags = arr.flags
  696. if not flags["C_CONTIGUOUS"]:
  697. arr = np.ascontiguousarray(arr)
  698. result = cv2.flip(_normalize_cv2_input_arr_(arr), 1)
  699. if result.ndim == 2 and arr.ndim == 3:
  700. return result[..., np.newaxis]
  701. return result
  702. def flipud(arr):
  703. """Flip an image-like array vertically.
  704. **Supported dtypes**:
  705. * ``uint8``: yes; fully tested
  706. * ``uint16``: yes; fully tested
  707. * ``uint32``: yes; fully tested
  708. * ``uint64``: yes; fully tested
  709. * ``int8``: yes; fully tested
  710. * ``int16``: yes; fully tested
  711. * ``int32``: yes; fully tested
  712. * ``int64``: yes; fully tested
  713. * ``float16``: yes; fully tested
  714. * ``float32``: yes; fully tested
  715. * ``float64``: yes; fully tested
  716. * ``float128``: yes; fully tested
  717. * ``bool``: yes; fully tested
  718. Parameters
  719. ----------
  720. arr : ndarray
  721. A 2D/3D `(H, W, [C])` image array.
  722. Returns
  723. -------
  724. ndarray
  725. Vertically flipped array.
  726. Examples
  727. --------
  728. >>> import numpy as np
  729. >>> import imgaug.augmenters.flip as flip
  730. >>> arr = np.arange(16).reshape((4, 4))
  731. >>> arr_flipped = flip.flipud(arr)
  732. Create a ``4x4`` array and flip it vertically.
  733. """
  734. # Note that this function is currently not called by Flipud for performance
  735. # reasons. Changing this will therefore not affect Flipud.
  736. return arr[::-1, ...]
  737. def HorizontalFlip(*args, **kwargs):
  738. """Alias for Fliplr."""
  739. # pylint: disable=invalid-name
  740. return Fliplr(*args, **kwargs)
  741. def VerticalFlip(*args, **kwargs):
  742. """Alias for Flipud."""
  743. # pylint: disable=invalid-name
  744. return Flipud(*args, **kwargs)
  745. class Fliplr(meta.Augmenter):
  746. """Flip/mirror input images horizontally.
  747. .. note::
  748. The default value for the probability is ``0.0``.
  749. So, to flip *all* input images use ``Fliplr(1.0)`` and *not* just
  750. ``Fliplr()``.
  751. **Supported dtypes**:
  752. See :func:`~imgaug.augmenters.flip.fliplr`.
  753. Parameters
  754. ----------
  755. p : number or imgaug.parameters.StochasticParameter, optional
  756. Probability of each image to get flipped.
  757. seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  758. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  759. name : None or str, optional
  760. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  761. random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  762. Old name for parameter `seed`.
  763. Its usage will not yet cause a deprecation warning,
  764. but it is still recommended to use `seed` now.
  765. Outdated since 0.4.0.
  766. deterministic : bool, optional
  767. Deprecated since 0.4.0.
  768. See method ``to_deterministic()`` for an alternative and for
  769. details about what the "deterministic mode" actually does.
  770. Examples
  771. --------
  772. >>> import imgaug.augmenters as iaa
  773. >>> aug = iaa.Fliplr(0.5)
  774. Flip ``50`` percent of all images horizontally.
  775. >>> aug = iaa.Fliplr(1.0)
  776. Flip all images horizontally.
  777. """
  778. def __init__(self, p=1,
  779. seed=None, name=None,
  780. random_state="deprecated", deterministic="deprecated"):
  781. super(Fliplr, self).__init__(
  782. seed=seed, name=name,
  783. random_state=random_state, deterministic=deterministic)
  784. self.p = iap.handle_probability_param(p, "p")
  785. # Added in 0.4.0.
  786. def _augment_batch_(self, batch, random_state, parents, hooks):
  787. samples = self.p.draw_samples((batch.nb_rows,),
  788. random_state=random_state)
  789. for i, sample in enumerate(samples):
  790. if sample >= 0.5:
  791. if batch.images is not None:
  792. batch.images[i] = fliplr(batch.images[i])
  793. if batch.heatmaps is not None:
  794. batch.heatmaps[i].arr_0to1 = fliplr(
  795. batch.heatmaps[i].arr_0to1)
  796. if batch.segmentation_maps is not None:
  797. batch.segmentation_maps[i].arr = fliplr(
  798. batch.segmentation_maps[i].arr)
  799. if batch.keypoints is not None:
  800. kpsoi = batch.keypoints[i]
  801. width = kpsoi.shape[1]
  802. for kp in kpsoi.keypoints:
  803. kp.x = width - float(kp.x)
  804. if batch.bounding_boxes is not None:
  805. bbsoi = batch.bounding_boxes[i]
  806. width = bbsoi.shape[1]
  807. for bb in bbsoi.bounding_boxes:
  808. # after flip, x1 ends up right of x2
  809. x1, x2 = bb.x1, bb.x2
  810. bb.x1 = width - x2
  811. bb.x2 = width - x1
  812. if batch.polygons is not None:
  813. psoi = batch.polygons[i]
  814. width = psoi.shape[1]
  815. for poly in psoi.polygons:
  816. # TODO maybe reverse the order of points afterwards?
  817. # the flip probably inverts them
  818. poly.exterior[:, 0] = width - poly.exterior[:, 0]
  819. if batch.line_strings is not None:
  820. lsoi = batch.line_strings[i]
  821. width = lsoi.shape[1]
  822. for ls in lsoi.line_strings:
  823. # TODO maybe reverse the order of points afterwards?
  824. # the flip probably inverts them
  825. ls.coords[:, 0] = width - ls.coords[:, 0]
  826. return batch
  827. def get_parameters(self):
  828. """See :func:`~imgaug.augmenters.meta.Augmenter.get_parameters`."""
  829. return [self.p]
  830. # TODO merge with Fliplr
  831. class Flipud(meta.Augmenter):
  832. """Flip/mirror input images vertically.
  833. .. note::
  834. The default value for the probability is ``0.0``.
  835. So, to flip *all* input images use ``Flipud(1.0)`` and *not* just
  836. ``Flipud()``.
  837. **Supported dtypes**:
  838. See :func:`~imgaug.augmenters.flip.flipud`.
  839. Parameters
  840. ----------
  841. p : number or imgaug.parameters.StochasticParameter, optional
  842. Probability of each image to get flipped.
  843. seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  844. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  845. name : None or str, optional
  846. See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.
  847. random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
  848. Old name for parameter `seed`.
  849. Its usage will not yet cause a deprecation warning,
  850. but it is still recommended to use `seed` now.
  851. Outdated since 0.4.0.
  852. deterministic : bool, optional
  853. Deprecated since 0.4.0.
  854. See method ``to_deterministic()`` for an alternative and for
  855. details about what the "deterministic mode" actually does.
  856. Examples
  857. --------
  858. >>> import imgaug.augmenters as iaa
  859. >>> aug = iaa.Flipud(0.5)
  860. Flip ``50`` percent of all images vertically.
  861. >>> aug = iaa.Flipud(1.0)
  862. Flip all images vertically.
  863. """
  864. def __init__(self, p=1,
  865. seed=None, name=None,
  866. random_state="deprecated", deterministic="deprecated"):
  867. super(Flipud, self).__init__(
  868. seed=seed, name=name,
  869. random_state=random_state, deterministic=deterministic)
  870. self.p = iap.handle_probability_param(p, "p")
  871. # Added in 0.4.0.
  872. def _augment_batch_(self, batch, random_state, parents, hooks):
  873. samples = self.p.draw_samples((batch.nb_rows,),
  874. random_state=random_state)
  875. for i, sample in enumerate(samples):
  876. if sample >= 0.5:
  877. if batch.images is not None:
  878. # We currently do not use flip.flipud() here, because that
  879. # saves a function call.
  880. batch.images[i] = batch.images[i][::-1, ...]
  881. if batch.heatmaps is not None:
  882. batch.heatmaps[i].arr_0to1 = \
  883. batch.heatmaps[i].arr_0to1[::-1, ...]
  884. if batch.segmentation_maps is not None:
  885. batch.segmentation_maps[i].arr = \
  886. batch.segmentation_maps[i].arr[::-1, ...]
  887. if batch.keypoints is not None:
  888. kpsoi = batch.keypoints[i]
  889. height = kpsoi.shape[0]
  890. for kp in kpsoi.keypoints:
  891. kp.y = height - float(kp.y)
  892. if batch.bounding_boxes is not None:
  893. bbsoi = batch.bounding_boxes[i]
  894. height = bbsoi.shape[0]
  895. for bb in bbsoi.bounding_boxes:
  896. # after flip, y1 ends up right of y2
  897. y1, y2 = bb.y1, bb.y2
  898. bb.y1 = height - y2
  899. bb.y2 = height - y1
  900. if batch.polygons is not None:
  901. psoi = batch.polygons[i]
  902. height = psoi.shape[0]
  903. for poly in psoi.polygons:
  904. # TODO maybe reverse the order of points afterwards?
  905. # the flip probably inverts them
  906. poly.exterior[:, 1] = height - poly.exterior[:, 1]
  907. if batch.line_strings is not None:
  908. lsoi = batch.line_strings[i]
  909. height = lsoi.shape[0]
  910. for ls in lsoi.line_strings:
  911. # TODO maybe reverse the order of points afterwards?
  912. # the flip probably inverts them
  913. ls.coords[:, 1] = height - ls.coords[:, 1]
  914. return batch
  915. def get_parameters(self):
  916. """See :func:`~imgaug.augmenters.meta.Augmenter.get_parameters`."""
  917. return [self.p]