{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tensorflow for poets\n", "\n", "This notebook steps through the [Tensorflow for poets](https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/) tutorial.\n", "\n", "First clone the code repository." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!git clone https://github.com/googlecodelabs/tensorflow-for-poets-2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Move into the new directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cd tensorflow-for-poets-2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Download the flowers dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!curl http://download.tensorflow.org/example_images/flower_photos.tgz | tar xz -C tf_files" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ls tf_files/flower_photos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run this in a terminal, Jupyter doesn't allow background processes...\n", "\n", "I'm assuming this won't be possible on Binder?\n", "\n", "```\n", "tensorboard --logdir tf_files/training_summaries &\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Train the model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "IMAGE_SIZE=224\n", "ARCHITECTURE=\"mobilenet_0.50_${IMAGE_SIZE}\"\n", "\n", "python -m scripts.retrain \\\n", " --bottleneck_dir=tf_files/bottlenecks \\\n", " --how_many_training_steps=500 \\\n", " --model_dir=tf_files/models/ \\\n", " --summaries_dir=tf_files/training_summaries/\"${ARCHITECTURE}\" \\\n", " --output_graph=tf_files/retrained_graph.pb \\\n", " --output_labels=tf_files/retrained_labels.txt \\\n", " --architecture=\"${ARCHITECTURE}\" \\\n", " --image_dir=tf_files/flower_photos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test the trained model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "python -m scripts.label_image \\\n", " --graph=tf_files/retrained_graph.pb \\\n", " --image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More testing\n", "\n", "This bit isn't in the tutorial. I just thought it would be good to do some random testing..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Make a list of all the flower images\n", "import os\n", "import random\n", "from IPython.display import display, HTML\n", "flowers = []\n", "flower_dir = 'tf_files/flower_photos/'\n", "for img_dir in [d for d in os.listdir(flower_dir) if os.path.isdir(os.path.join(flower_dir, d))]:\n", " for img in [i for i in os.listdir(os.path.join(flower_dir, img_dir)) if i[-4:] == '.jpg']:\n", " flowers.append(os.path.join(flower_dir, img_dir, img)) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Choose one flower at random\n", "flower = random.sample(flowers, 1)[0]\n", "display(HTML('
{0}'.format(flower)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=$flower" ] }, { "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }