Created
March 21, 2018 15:40
-
-
Save aoikonomop/f7264c12195cfea1c91eb7ea55875d69 to your computer and use it in GitHub Desktop.
QueueRunnerExample
This file contains 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": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 1. Get some dummy data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"records_dict = {\n", | |
"\"records\": [\n", | |
" \"predator-5.jpg,predator-5.csv\",\n", | |
" \"predator-18.jpg,predator-18.csv\",\n", | |
" \"predator-55.jpg,predator-55.csv\",\n", | |
" \"predator-186.jpg,predator-186.csv\",\n", | |
" \"predator-539.jpg,predator-539.csv\",\n", | |
" \"predator-1976.jpg,predator-1976.csv\",\n", | |
" \"predator-2006.jpg,predator-2006.csv\",\n", | |
" \"predator-2015.jpg,predator-2015.csv\",\n", | |
" \"predator-5477.jpg,predator-5477.csv\",\n", | |
" \"predator-71940.jpg,predator-71940.csv\"\n", | |
" ]\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"records = records_dict[\"records\"]\n", | |
"data = tf.convert_to_tensor(records, dtype=tf.string)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 2. Create a queue" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data_queue = tf.train.string_input_producer(data, shuffle=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 3. Decode the data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_location, bb_location = tf.decode_csv(data_queue.dequeue(), [[''], ['']])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 4. Shuffle and batch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"record = tf.train.shuffle_batch(\n", | |
" [image_location, bb_location],\n", | |
" batch_size=2,\n", | |
" capacity=20,\n", | |
" min_after_dequeue=16,\n", | |
" num_threads=16)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 5. Run it!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with tf.Session() as sess:\n", | |
" coord = tf.train.Coordinator()\n", | |
" threads = tf.train.start_queue_runners(coord=coord)\n", | |
"\n", | |
" rec = sess.run(record)\n", | |
" \n", | |
" print(rec[0], rec[1])\n", | |
" \n", | |
" coord.request_stop()\n", | |
" coord.join(threads)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment