Created
December 10, 2018 22:56
-
-
Save mvsusp/88a7efd6ba790201b01c5b8161dc8d0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Download the MNIST dataset" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"scrolled": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import utils\n", | |
"from tensorflow.contrib.learn.python.learn.datasets import mnist\n", | |
"import tensorflow as tf\n", | |
"\n", | |
"data_sets = mnist.read_data_sets('data', dtype=tf.uint8, reshape=False, validation_size=5000)\n", | |
"\n", | |
"utils.convert_to(data_sets.train, 'train', 'data')\n", | |
"utils.convert_to(data_sets.validation, 'validation', 'data')\n", | |
"utils.convert_to(data_sets.test, 'test', 'data')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<TFRecordDataset shapes: (), types: tf.string>" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import tensorflow as tf\n", | |
"import os\n", | |
"tfrecord_dir = 'data'\n", | |
"type=\"test\"\n", | |
"tfrecord_file = [os.path.join(tfrecord_dir,f) for f in os.listdir(tfrecord_dir) if type in f and \".tfrecord\" in f]\n", | |
"dataset = tf.data.TFRecordDataset([tfrecord_file])\n", | |
"dataset" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<RepeatDataset shapes: (), types: tf.string>" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dataset = dataset.repeat()\n", | |
"dataset" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<PrefetchDataset shapes: ((?, 784), (?,)), types: (tf.float32, tf.int32)>" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def parse_record(serialized_example):\n", | |
" features = tf.parse_single_example(\n", | |
" serialized_example,\n", | |
" features={\n", | |
" 'image_raw': tf.FixedLenFeature([], tf.string),\n", | |
" 'label': tf.FixedLenFeature([], tf.int64),\n", | |
" })\n", | |
"\n", | |
" image = tf.decode_raw(features['image_raw'], tf.uint8)\n", | |
" image.set_shape([784])\n", | |
" image = tf.cast(image, tf.float32) * (1. / 255)\n", | |
" label = tf.cast(features['label'], tf.int32)\n", | |
"\n", | |
" return image, label\n", | |
"\n", | |
"\n", | |
"shuffle_buffer_size = 5000\n", | |
"batch_size = 128\n", | |
"num_parallel_batches=1\n", | |
"prefetch_buffer_size = 5000\n", | |
"\n", | |
"\n", | |
"dataset = dataset.shuffle(buffer_size=shuffle_buffer_size)\n", | |
"\n", | |
"\n", | |
"dataset = dataset.apply(tf.contrib.data.map_and_batch(\n", | |
" map_func=parse_record, batch_size=batch_size, num_parallel_batches=num_parallel_batches))\n", | |
"\n", | |
"dataset = dataset.prefetch(buffer_size=prefetch_buffer_size)\n", | |
"dataset" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(array([[0., 0., 0., ..., 0., 0., 0.],\n", | |
" [0., 0., 0., ..., 0., 0., 0.],\n", | |
" [0., 0., 0., ..., 0., 0., 0.],\n", | |
" ...,\n", | |
" [0., 0., 0., ..., 0., 0., 0.],\n", | |
" [0., 0., 0., ..., 0., 0., 0.],\n", | |
" [0., 0., 0., ..., 0., 0., 0.]], dtype=float32), array([6, 7, 1, 8, 4, 1, 3, 9, 8, 4, 9, 4, 6, 9, 6, 9, 1, 0, 7, 1, 4, 2,\n", | |
" 9, 6, 1, 2, 1, 1, 2, 7, 1, 2, 1, 8, 4, 7, 9, 4, 3, 5, 8, 4, 4, 4,\n", | |
" 1, 5, 7, 8, 3, 1, 5, 5, 2, 6, 5, 1, 6, 9, 8, 2, 6, 0, 3, 6, 6, 1,\n", | |
" 3, 5, 3, 9, 9, 1, 8, 9, 1, 8, 8, 2, 5, 1, 0, 7, 0, 4, 5, 5, 9, 0,\n", | |
" 2, 7, 3, 0, 2, 0, 8, 7, 7, 6, 6, 8, 3, 5, 6, 7, 6, 4, 0, 8, 9, 5,\n", | |
" 9, 9, 9, 4, 4, 5, 8, 3, 4, 8, 7, 1, 4, 7, 3, 7, 5, 1], dtype=int32))\n" | |
] | |
} | |
], | |
"source": [ | |
"iterator = dataset.make_one_shot_iterator()\n", | |
"next_element = iterator.get_next()\n", | |
"\n", | |
"sess = tf.Session()\n", | |
"print(sess.run(next_element))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.7" | |
}, | |
"notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment