Guest User

Untitled

a guest
Jun 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 2,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stderr",
  10. "output_type": "stream",
  11. "text": [
  12. "Using TensorFlow backend.\n"
  13. ]
  14. }
  15. ],
  16. "source": [
  17. "import pandas as pd\n",
  18. "import numpy as np\n",
  19. "import tensorflow as tf\n",
  20. "import os,sys\n",
  21. "\n",
  22. "from keras.datasets import mnist"
  23. ]
  24. },
  25. {
  26. "cell_type": "code",
  27. "execution_count": 3,
  28. "metadata": {},
  29. "outputs": [],
  30. "source": [
  31. "(x_train,y_train),(x_test,y_test) = mnist.load_data()"
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": 4,
  37. "metadata": {},
  38. "outputs": [],
  39. "source": [
  40. "x_train = x_train.astype(np.int64)\n",
  41. "x_test = x_test.astype(np.int64)"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": 5,
  47. "metadata": {},
  48. "outputs": [],
  49. "source": [
  50. "train_path = os.path.abspath('./mnist_train.tfrecords')\n",
  51. "test_path = os.path.abspath('./mnist_test.tfrecords')"
  52. ]
  53. },
  54. {
  55. "cell_type": "markdown",
  56. "metadata": {},
  57. "source": [
  58. "`Numpy int format ---> Numpy string format ---> TF Example ---> Serialized Example ---> TFRecords Format`"
  59. ]
  60. },
  61. {
  62. "cell_type": "code",
  63. "execution_count": 7,
  64. "metadata": {},
  65. "outputs": [],
  66. "source": [
  67. "def _int64_feature(value):\n",
  68. " return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))\n",
  69. "\n",
  70. "def _bytes_feature(value):\n",
  71. " return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))"
  72. ]
  73. },
  74. {
  75. "cell_type": "code",
  76. "execution_count": 10,
  77. "metadata": {},
  78. "outputs": [],
  79. "source": [
  80. "def convert_to_records(x,y,path):\n",
  81. " print('writing to {}'.format(path))\n",
  82. " with tf.python_io.TFRecordWriter(path) as writer:\n",
  83. " for i in range(x.shape[0]):\n",
  84. " example = tf.train.Example(features = tf.train.Features(\n",
  85. " feature = \n",
  86. " {\n",
  87. " 'image_raw':_bytes_feature(x[i].tostring()),\n",
  88. " 'label':_int64_feature(int(y[i]))\n",
  89. " }\n",
  90. " )\n",
  91. " )\n",
  92. " writer.write(example.SerializeToString())\n",
  93. " if i%5000==0:\n",
  94. " print('writing {}th image'.format(i))\n",
  95. " "
  96. ]
  97. },
  98. {
  99. "cell_type": "code",
  100. "execution_count": null,
  101. "metadata": {},
  102. "outputs": [],
  103. "source": []
  104. },
  105. {
  106. "cell_type": "code",
  107. "execution_count": 11,
  108. "metadata": {},
  109. "outputs": [
  110. {
  111. "name": "stdout",
  112. "output_type": "stream",
  113. "text": [
  114. "writing to /home/lilhope/Downloads/fastai/mnist_train.tfrecords\n",
  115. "writing 0 iteration\n",
  116. "writing 5000 iteration\n",
  117. "writing 10000 iteration\n",
  118. "writing 15000 iteration\n",
  119. "writing 20000 iteration\n",
  120. "writing 25000 iteration\n",
  121. "writing 30000 iteration\n",
  122. "writing 35000 iteration\n",
  123. "writing 40000 iteration\n",
  124. "writing 45000 iteration\n",
  125. "writing 50000 iteration\n",
  126. "writing 55000 iteration\n"
  127. ]
  128. },
  129. {
  130. "data": {
  131. "text/plain": [
  132. "0"
  133. ]
  134. },
  135. "execution_count": 11,
  136. "metadata": {},
  137. "output_type": "execute_result"
  138. }
  139. ],
  140. "source": [
  141. "convert_to_records(x_train,y_train,train_path)"
  142. ]
  143. },
  144. {
  145. "cell_type": "code",
  146. "execution_count": 10,
  147. "metadata": {},
  148. "outputs": [
  149. {
  150. "name": "stdout",
  151. "output_type": "stream",
  152. "text": [
  153. "writing to /home/lilhope/Downloads/fastai/mnist_test.tfrecords\n",
  154. "writing 0 iteration\n",
  155. "writing 5000 iteration\n"
  156. ]
  157. },
  158. {
  159. "data": {
  160. "text/plain": [
  161. "0"
  162. ]
  163. },
  164. "execution_count": 10,
  165. "metadata": {},
  166. "output_type": "execute_result"
  167. }
  168. ],
  169. "source": [
  170. "convert_to_records(x_test,y_test,test_path)"
  171. ]
  172. }
  173. ],
  174. "metadata": {
  175. "kernelspec": {
  176. "display_name": "Python 3",
  177. "language": "python",
  178. "name": "python3"
  179. },
  180. "language_info": {
  181. "codemirror_mode": {
  182. "name": "ipython",
  183. "version": 3
  184. },
  185. "file_extension": ".py",
  186. "mimetype": "text/x-python",
  187. "name": "python",
  188. "nbconvert_exporter": "python",
  189. "pygments_lexer": "ipython3",
  190. "version": "3.6.5"
  191. }
  192. },
  193. "nbformat": 4,
  194. "nbformat_minor": 2
  195. }
Add Comment
Please, Sign In to add comment