Guest User

Untitled

a guest
Nov 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Transformation performance test"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {
  14. "collapsed": true
  15. },
  16. "outputs": [],
  17. "source": [
  18. "import numpy as np"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 2,
  24. "metadata": {
  25. "collapsed": true
  26. },
  27. "outputs": [],
  28. "source": [
  29. "def transform_apply(tensor, transform):\n",
  30. " feat_tensor = []\n",
  31. " org_shape = tensor.shape\n",
  32. " for plane in tensor[0]:\n",
  33. " feat_tensor.append(transform(plane))\n",
  34. " ret = np.concatenate(feat_tensor)\n",
  35. " ret = ret.reshape(org_shape)\n",
  36. " return ret"
  37. ]
  38. },
  39. {
  40. "cell_type": "code",
  41. "execution_count": 3,
  42. "metadata": {
  43. "collapsed": true
  44. },
  45. "outputs": [],
  46. "source": [
  47. "BD_transforms = [\n",
  48. " lambda f:f, \n",
  49. " lambda f:np.rot90(f,1), \n",
  50. " lambda f:np.rot90(f,2), \n",
  51. " lambda f:np.rot90(f,3), \n",
  52. " lambda f:np.fliplr(f), \n",
  53. " lambda f:np.flipud(f), \n",
  54. " lambda f:np.transpose(f), \n",
  55. " lambda f:np.fliplr(np.rot90(f, 1))\n",
  56. "]"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": 4,
  62. "metadata": {
  63. "collapsed": true
  64. },
  65. "outputs": [],
  66. "source": [
  67. "BD_fasttrans = [\n",
  68. " lambda f: f,\n",
  69. " lambda f: np.rot90(f, 1, axes=(2,3)),\n",
  70. " lambda f: np.rot90(f, 2, axes=(2,3)),\n",
  71. " lambda f: np.rot90(f, 3, axes=(2,3)),\n",
  72. " lambda f: f[:, :, :, ::-1],\n",
  73. " lambda f: f[:, :, ::-1, :],\n",
  74. " lambda f: np.transpose(f, axes=(0, 1, 3, 2)),\n",
  75. " lambda f: np.rot90(f, 1, axes=(2,3))[:, :, :, ::-1]\n",
  76. "]"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": 7,
  82. "metadata": {},
  83. "outputs": [
  84. {
  85. "name": "stdout",
  86. "output_type": "stream",
  87. "text": [
  88. "True\n",
  89. "org 2.79942035675e-05\n",
  90. "fast 5.63967943192e-08\n",
  91. "times 496.379340448\n",
  92. "\n",
  93. "True\n",
  94. "org 0.00023686504364\n",
  95. "fast 4.14942979813e-06\n",
  96. "times 57.0837573266\n",
  97. "\n",
  98. "True\n",
  99. "org 0.000192616319656\n",
  100. "fast 3.43266010284e-06\n",
  101. "times 56.1128436505\n",
  102. "\n",
  103. "True\n",
  104. "org 0.000235383987427\n",
  105. "fast 4.21041965485e-06\n",
  106. "times 55.9051132007\n",
  107. "\n",
  108. "True\n",
  109. "org 6.21682882309e-05\n",
  110. "fast 3.42023849487e-07\n",
  111. "times 181.765945048\n",
  112. "\n",
  113. "True\n",
  114. "org 6.14371061325e-05\n",
  115. "fast 3.40197086334e-07\n",
  116. "times 180.592687593\n",
  117. "\n",
  118. "True\n",
  119. "org 5.45855045319e-05\n",
  120. "fast 5.8767914772e-07\n",
  121. "times 92.8831739966\n",
  122. "\n",
  123. "True\n",
  124. "org 0.000263137102127\n",
  125. "fast 4.56223011017e-06\n",
  126. "times 57.6772972368\n",
  127. "\n"
  128. ]
  129. }
  130. ],
  131. "source": [
  132. "for i in range(0, 8):\n",
  133. " t = np.random.randint(low=0, high=2, size=[1,49,19,19])\n",
  134. " transform = BD_transforms[i]\n",
  135. " fasttrans = BD_fasttrans[i]\n",
  136. " \n",
  137. " # sanity check\n",
  138. " r1 = transform_apply(t, transform)\n",
  139. " r2 = fasttrans(t)\n",
  140. " sanity = (r1 == r2).all()\n",
  141. " print sanity\n",
  142. " assert sanity, \"The transformation is not same\"\n",
  143. " \n",
  144. " # time check\n",
  145. " t1 = %timeit -oq transform_apply(t, transform)\n",
  146. " t2 = %timeit -oq fasttrans(t)\n",
  147. " \n",
  148. " print \"org \", t1.best\n",
  149. " print \"fast \", t2.best\n",
  150. " print \"times\", t1.best / t2.best \n",
  151. " print"
  152. ]
  153. },
  154. {
  155. "cell_type": "code",
  156. "execution_count": null,
  157. "metadata": {
  158. "collapsed": true
  159. },
  160. "outputs": [],
  161. "source": []
  162. }
  163. ],
  164. "metadata": {
  165. "kernelspec": {
  166. "display_name": "Python 2",
  167. "language": "python",
  168. "name": "python2"
  169. },
  170. "language_info": {
  171. "codemirror_mode": {
  172. "name": "ipython",
  173. "version": 2
  174. },
  175. "file_extension": ".py",
  176. "mimetype": "text/x-python",
  177. "name": "python",
  178. "nbconvert_exporter": "python",
  179. "pygments_lexer": "ipython2",
  180. "version": "2.7.6"
  181. }
  182. },
  183. "nbformat": 4,
  184. "nbformat_minor": 2
  185. }
Add Comment
Please, Sign In to add comment