Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "cellView": "both",
  8. "colab_type": "code",
  9. "collapsed": false,
  10. "id": "i9hkSm1IOZNR"
  11. },
  12. "outputs": [
  13. {
  14. "name": "stdout",
  15. "output_type": "stream",
  16. "text": [
  17. "data: 1505280 = 10x3x224x224\n",
  18. "conv1_1: 32112640 = 10x64x224x224\n",
  19. "conv1_2: 32112640 = 10x64x224x224\n",
  20. "pool1: 8028160 = 10x64x112x112\n",
  21. "conv2_1: 16056320 = 10x128x112x112\n",
  22. "conv2_2: 16056320 = 10x128x112x112\n",
  23. "pool2: 4014080 = 10x128x56x56\n",
  24. "conv3_1: 8028160 = 10x256x56x56\n",
  25. "conv3_2: 8028160 = 10x256x56x56\n",
  26. "conv3_3: 8028160 = 10x256x56x56\n",
  27. "conv3_4: 8028160 = 10x256x56x56\n",
  28. "pool3: 2007040 = 10x256x28x28\n",
  29. "conv4_1: 4014080 = 10x512x28x28\n",
  30. "conv4_2: 4014080 = 10x512x28x28\n",
  31. "conv4_3: 4014080 = 10x512x28x28\n",
  32. "conv4_4: 4014080 = 10x512x28x28\n",
  33. "pool4: 1003520 = 10x512x14x14\n",
  34. "conv5_1: 1003520 = 10x512x14x14\n",
  35. "conv5_2: 1003520 = 10x512x14x14\n",
  36. "conv5_3: 1003520 = 10x512x14x14\n",
  37. "conv5_4: 1003520 = 10x512x14x14\n",
  38. "pool5: 250880 = 10x512x7x7\n",
  39. "fc6: 40960 = 10x4096x1x1\n",
  40. "fc6_fc6_0_split_0: 40960 = 10x4096x1x1\n",
  41. "fc6_fc6_0_split_1: 40960 = 10x4096x1x1\n",
  42. "fc6_fc6_0_split_2: 40960 = 10x4096x1x1\n",
  43. "relu6: 40960 = 10x4096x1x1\n",
  44. "drop6: 40960 = 10x4096x1x1\n",
  45. "fc7: 40960 = 10x4096x1x1\n",
  46. "relu7: 40960 = 10x4096x1x1\n",
  47. "drop7: 40960 = 10x4096x1x1\n",
  48. "fc8: 10000 = 10x1000x1x1\n",
  49. "prob: 10000 = 10x1000x1x1\n"
  50. ]
  51. }
  52. ],
  53. "source": [
  54. "import numpy as np\n",
  55. "from google.protobuf import text_format\n",
  56. "import caffe\n",
  57. "\n",
  58. "# load network\n",
  59. "# the prototxt has force_backward: true, and fc7 is separated into multiple blobs\n",
  60. "model_name = 'vgg_ilsvrc_19'\n",
  61. "model_path = '../caffe/models/' + model_name + '/'\n",
  62. "net_fn = model_path + 'deploy-expanded.prototxt'\n",
  63. "param_fn = model_path + 'net.caffemodel'\n",
  64. "net = caffe.Classifier(net_fn, param_fn)\n",
  65. "\n",
  66. "# print blob names and sizes\n",
  67. "for end in net.blobs.keys():\n",
  68. " cur = net.blobs[end]\n",
  69. " print end + ': {} = {}x{}x{}x{}'.format(cur.count, cur.num, cur.channels, cur.width, cur.height)"
  70. ]
  71. },
  72. {
  73. "cell_type": "code",
  74. "execution_count": 2,
  75. "metadata": {
  76. "collapsed": false
  77. },
  78. "outputs": [],
  79. "source": [
  80. "def optimize(net,\n",
  81. " hot=0,\n",
  82. " step_size=.01,\n",
  83. " iter_n=100,\n",
  84. " mu=.9,\n",
  85. " basename='fc7',\n",
  86. " start='relu7',\n",
  87. " end='prob'):\n",
  88. " base = net.blobs[basename]\n",
  89. " first = net.blobs[start]\n",
  90. " last = net.blobs[end]\n",
  91. " base.data[0] = np.random.normal(.5, .1, base.data[0].shape)\n",
  92. " base.diff[0] = 0.\n",
  93. " velocity = np.zeros_like(base.data[0])\n",
  94. " velocity_previous = np.zeros_like(base.data[0])\n",
  95. " for i in range(iter_n):\n",
  96. " net.forward(start=start, end=end)\n",
  97. " target = np.zeros_like(last.data[0])\n",
  98. " target.flat[hot] = 1.\n",
  99. " error = target - last.data[0]\n",
  100. " last.diff[0] = error\n",
  101. " net.backward(start=end, end=start)\n",
  102. " grad = base.diff[0]\n",
  103. " learning_rate = (step_size / np.abs(grad).mean())\n",
  104. " velocity_previous = velocity\n",
  105. " velocity = mu * velocity + learning_rate * grad\n",
  106. " base.data[0] += -mu * velocity_previous + (1 + mu) * velocity\n",
  107. " base.data[0] = np.clip(base.data[0], 0, +1)\n",
  108. " return base.data[0]"
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": 8,
  114. "metadata": {
  115. "collapsed": false
  116. },
  117. "outputs": [
  118. {
  119. "name": "stdout",
  120. "output_type": "stream",
  121. "text": [
  122. "in: [ 1. 0. 1. 0. 0. 0. 0. 0.]\n",
  123. "out: [ 9.91814137e-01 4.00208119e-05 9.94682705e-06 9.07168669e-06\n",
  124. " 1.13390133e-05 1.70246039e-05 1.00811658e-05 3.26871736e-06]\n"
  125. ]
  126. }
  127. ],
  128. "source": [
  129. "print 'in:', optimize(net, hot=0)[0:8]\n",
  130. "print 'out:', net.blobs['prob'].data[0,0:8]"
  131. ]
  132. },
  133. {
  134. "cell_type": "code",
  135. "execution_count": 9,
  136. "metadata": {
  137. "collapsed": false
  138. },
  139. "outputs": [
  140. {
  141. "name": "stderr",
  142. "output_type": "stream",
  143. "text": [
  144. "100% (1000 of 1000) |#####################| Elapsed Time: 0:07:12 Time: 0:07:12\n"
  145. ]
  146. }
  147. ],
  148. "source": [
  149. "from progressbar import ProgressBar\n",
  150. "vectors = []\n",
  151. "pbar = ProgressBar()\n",
  152. "for i in pbar(range(1000)):\n",
  153. " vectors.append(optimize(net, hot=i).copy())\n",
  154. "np.savetxt('vectors', vectors, fmt='%.2f', delimiter='\\t')"
  155. ]
  156. }
  157. ],
  158. "metadata": {
  159. "colabVersion": "0.3.1",
  160. "default_view": {},
  161. "kernelspec": {
  162. "display_name": "Python 2",
  163. "language": "python",
  164. "name": "python2"
  165. },
  166. "language_info": {
  167. "codemirror_mode": {
  168. "name": "ipython",
  169. "version": 2
  170. },
  171. "file_extension": ".py",
  172. "mimetype": "text/x-python",
  173. "name": "python",
  174. "nbconvert_exporter": "python",
  175. "pygments_lexer": "ipython2",
  176. "version": "2.7.10"
  177. },
  178. "views": {}
  179. },
  180. "nbformat": 4,
  181. "nbformat_minor": 0
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement