Guest User

Untitled

a guest
Feb 23rd, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stderr",
  10. "output_type": "stream",
  11. "text": [
  12. "/home/hgaiser/.local/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
  13. " from ._conv import register_converters as _register_converters\n",
  14. "Using TensorFlow backend.\n"
  15. ]
  16. }
  17. ],
  18. "source": [
  19. "# show images inline\n",
  20. "%matplotlib inline\n",
  21. "\n",
  22. "# automatically reload modules when they have changed\n",
  23. "%load_ext autoreload\n",
  24. "%autoreload 2\n",
  25. "\n",
  26. "# import keras\n",
  27. "import keras\n",
  28. "\n",
  29. "# import keras_retinanet\n",
  30. "from keras_retinanet.models.resnet import custom_objects\n",
  31. "from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image\n",
  32. "from keras_retinanet.utils.transform import random_transform_generator\n",
  33. "from keras_retinanet.preprocessing.coco import CocoGenerator\n",
  34. "\n",
  35. "# import miscellaneous modules\n",
  36. "import matplotlib.pyplot as plt\n",
  37. "import cv2\n",
  38. "import os\n",
  39. "import numpy as np\n",
  40. "import time\n",
  41. "\n",
  42. "# set tf backend to allow memory to grow, instead of claiming everything\n",
  43. "import tensorflow as tf\n",
  44. "\n",
  45. "def get_session():\n",
  46. " config = tf.ConfigProto()\n",
  47. " config.gpu_options.allow_growth = True\n",
  48. " return tf.Session(config=config)\n",
  49. "\n",
  50. "# use this environment flag to change which GPU to use\n",
  51. "#os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"1\"\n",
  52. "\n",
  53. "# set the modified tf session as backend in keras\n",
  54. "keras.backend.tensorflow_backend.set_session(get_session())"
  55. ]
  56. },
  57. {
  58. "cell_type": "code",
  59. "execution_count": 2,
  60. "metadata": {},
  61. "outputs": [
  62. {
  63. "name": "stderr",
  64. "output_type": "stream",
  65. "text": [
  66. "/home/hgaiser/.local/lib/python3.6/site-packages/keras/models.py:274: UserWarning: Output \"non_maximum_suppression_1\" missing from loss dictionary. We assume this was done on purpose, and we will not be expecting any data to be passed to \"non_maximum_suppression_1\" during training.\n",
  67. " sample_weight_mode=sample_weight_mode)\n"
  68. ]
  69. }
  70. ],
  71. "source": [
  72. "# adjust this to point to your downloaded/trained model\n",
  73. "model_path = os.path.join('snapshots', 'resnet50_coco_best_v1.2.2.h5')\n",
  74. "\n",
  75. "# load retinanet model\n",
  76. "model = keras.models.load_model(model_path, custom_objects=custom_objects)"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": 3,
  82. "metadata": {},
  83. "outputs": [
  84. {
  85. "name": "stdout",
  86. "output_type": "stream",
  87. "text": [
  88. "loading annotations into memory...\n",
  89. "Done (t=9.34s)\n",
  90. "creating index...\n",
  91. "index created!\n"
  92. ]
  93. }
  94. ],
  95. "source": [
  96. "transform_generator = random_transform_generator(flip_x_chance=0.5)\n",
  97. "\n",
  98. "train_generator = CocoGenerator(\n",
  99. " '/srv/datasets/COCO/',\n",
  100. " 'train2017',\n",
  101. " transform_generator=transform_generator,\n",
  102. " batch_size=8\n",
  103. ")"
  104. ]
  105. },
  106. {
  107. "cell_type": "code",
  108. "execution_count": 4,
  109. "metadata": {},
  110. "outputs": [
  111. {
  112. "name": "stdout",
  113. "output_type": "stream",
  114. "text": [
  115. "(8, 600, 899, 3)\n"
  116. ]
  117. }
  118. ],
  119. "source": [
  120. "inputs, targets = next(train_generator)\n",
  121. "print(inputs.shape)"
  122. ]
  123. },
  124. {
  125. "cell_type": "code",
  126. "execution_count": 5,
  127. "metadata": {},
  128. "outputs": [
  129. {
  130. "name": "stdout",
  131. "output_type": "stream",
  132. "text": [
  133. "[0.84327817, 0.72799397, 0.115284175]\n"
  134. ]
  135. }
  136. ],
  137. "source": [
  138. "loss = model.test_on_batch(inputs, targets)\n",
  139. "print(loss)"
  140. ]
  141. },
  142. {
  143. "cell_type": "code",
  144. "execution_count": 6,
  145. "metadata": {},
  146. "outputs": [
  147. {
  148. "name": "stdout",
  149. "output_type": "stream",
  150. "text": [
  151. "0.8432783745229244\n",
  152. "0.7279941476881504\n",
  153. "0.11528422217816114\n"
  154. ]
  155. }
  156. ],
  157. "source": [
  158. "total_losses = []\n",
  159. "regression_losses = []\n",
  160. "classification_losses = []\n",
  161. "for i in range(8):\n",
  162. " inputs_ = inputs[i:i+1, ...]\n",
  163. " targets_ = [targets[0][i:i+1, ...], targets[1][i:i+1, ...]]\n",
  164. "\n",
  165. " loss = model.test_on_batch(inputs_, targets_)\n",
  166. " total_losses.append(loss[0])\n",
  167. " regression_losses.append(loss[1])\n",
  168. " classification_losses.append(loss[2])\n",
  169. " \n",
  170. "print(sum(total_losses) / 8)\n",
  171. "print(sum(regression_losses) / 8)\n",
  172. "print(sum(classification_losses) / 8)"
  173. ]
  174. },
  175. {
  176. "cell_type": "code",
  177. "execution_count": 23,
  178. "metadata": {},
  179. "outputs": [
  180. {
  181. "name": "stderr",
  182. "output_type": "stream",
  183. "text": [
  184. "/usr/lib/python3.6/site-packages/tensorflow/python/ops/gradients_impl.py:97: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.\n",
  185. " \"Converting sparse IndexedSlices to a dense Tensor of unknown shape. \"\n"
  186. ]
  187. }
  188. ],
  189. "source": [
  190. "outputs = model.outputs\n",
  191. "weights = model.trainable_weights\n",
  192. "gradients = keras.backend.gradients(outputs, weights)\n",
  193. "\n",
  194. "sess = tf.InteractiveSession()\n",
  195. "sess.run(tf.initialize_all_variables())\n",
  196. "evaluated_gradients = sess.run(gradients, feed_dict={model.input:inputs})\n",
  197. "\n",
  198. "evaluated_gradients_split = [np.zeros_like(x) for x in evaluated_gradients]\n",
  199. "\n",
  200. "for i in range(8):\n",
  201. " inputs_ = inputs[i:i+1, ...]\n",
  202. " targets_ = [targets[0][i:i+1, ...], targets[1][i:i+1, ...]]\n",
  203. "\n",
  204. " for idx, g in enumerate(sess.run(gradients, feed_dict={model.input:inputs_})):\n",
  205. " evaluated_gradients_split[idx] += g\n",
  206. " \n",
  207. "for i in range(len(evaluated_gradients_split)):\n",
  208. " evaluated_gradients_split[i] /= 8"
  209. ]
  210. },
  211. {
  212. "cell_type": "code",
  213. "execution_count": 24,
  214. "metadata": {},
  215. "outputs": [
  216. {
  217. "name": "stdout",
  218. "output_type": "stream",
  219. "text": [
  220. "[-127909.53 44277.484 -165517.14 172065.67 -288450.4 ]\n",
  221. "[-15919.898 5572.7393 -20385.074 21735.04 -36725.746 ]\n",
  222. "[-127359.19 44581.914 -163080.6 173880.31 -293805.97 ]\n"
  223. ]
  224. }
  225. ],
  226. "source": [
  227. "print(evaluated_gradients[0].flatten()[:5])\n",
  228. "print(evaluated_gradients_split[0].flatten()[:5])\n",
  229. "print(evaluated_gradients_split[0].flatten()[:5] * 8)"
  230. ]
  231. }
  232. ],
  233. "metadata": {
  234. "kernelspec": {
  235. "display_name": "Python 3",
  236. "language": "python",
  237. "name": "python3"
  238. },
  239. "language_info": {
  240. "codemirror_mode": {
  241. "name": "ipython",
  242. "version": 3
  243. },
  244. "file_extension": ".py",
  245. "mimetype": "text/x-python",
  246. "name": "python",
  247. "nbconvert_exporter": "python",
  248. "pygments_lexer": "ipython3",
  249. "version": "3.6.4"
  250. }
  251. },
  252. "nbformat": 4,
  253. "nbformat_minor": 2
  254. }
Add Comment
Please, Sign In to add comment