Getting Started with TensorFlow Image Recognition / Classification on OS X


You can use TensorBoard to visualize your TensorFlow graph, plot quantitative metrics about the execution of your graph, and show additional data like images that pass through it.

You can use TensorBoard to visualize your TensorFlow graph, plot quantitative metrics about the execution of your graph, and show additional data like images that pass through it.

It took me a while to get to this point on my MacBook Pro.

Only because I’m a total TensorFlow and Python noob. Well, I have done a little Python before, but that was like 10 years ago.

Anyway, here is how you can go from zero to being able to re-train Inception and classify images using TensorFlow and Python on your Mac. I’m on a top model rMBP from 2015 NVIDIA GeForce GT 750M 2048 MB so I’ve been able to use the GPU enabled TensorFlow but if you don’t have a GPU you can still retrain the Inception model – it just takes a lot longer.

1. Install Homebrew

2. Install Python 3

3. Install TensorFlow

4. Install Java & Bazel

5A. Install CUDA (only if using GPU)

5B. Sign up for NVIDIA Developer & Download CUDA Toolkit (only if using GPU)

5C. Extract CUDA Toolkit (only if using GPU)

Make sure cudnn.h is available under  /usr/local/cuda/include and libcudnn.dylib under /usr/local/cuda/lib

6. Clone the TensorFlow models repository from GitHub

7. Go to the ImageNet directory

8. Classify an image!

This should show an output similar to:

It should be noted here that even though I have a compatible GPU and am using the GPU version of TensorFlow, I actually needed to run pip install tensorflow to install the CPU version to get this to work – without it, it resulted in a segfault 11.

You may also need to run the following if you are getting errors similar to image not found:

(or edit .bash_profile in your home directory – btw I use vim) and paste in:

If this works for you, congrats! You’ve just classified an image using TensorFlow and Inception. Follow the next steps to retrain Inception.

9. Clone the main TensorFlow repository

10. Get an image set ready. You can use the flowers image set if you wish:

Or alternatively you can have a directory set up with subdirectory names as the class name, and have JPGs within it. Look into the flower photos file if you want to know what I mean but it’s basically:

That will retrain Inception to tell between Jordans and Yeezys. (Not down to the exact model).

11. Run Bazel

12. Configure

Make sure you select correctly between GPU and non-GPU (select CUDA). Everything else can pretty much be the default selection.

13. Retrain!

14. View training results in TensorBoard (optional)

15. Classify an image using the newly trained model!

If this all works for you, step 15 will give you an output similar to step 8. And step 14 will allow you to access TensorBoard on your local server, just like the first image of this article.

A *.pb file is called a Protocol Buffer file. That is the actual model (or the final layer of it). Feel free to move the /tmp/output_graph.pb and /tmp/output_labels.txt files to a more appropriate directory and file name. I rename mind according to the subject of the recognition / classification. For example you might rename it to sneakers.pb and sneakers.txt under ~/my-trained-models

Good luck! Leave a comment if you get stuck.




  • John

    May 7, 2017 at 6:06 pm

    Hi – and thanks very much for your post. Lots of very clear, explicit guidance there – and whilst it may exist elsewhere I suppose, your presentation of it suited my needs extremely well, so thank you!
    One minor question – it appears above as though you’ve got the command
    bazel build tensorboard
    This may well be correct, but I had more luck with
    bazel build tensorflow/tensorboard
    or actually,
    bazel build –config=opt –config=cuda tensorflow/tensorboard tensorflow/examples/label_image
    to build label_image as well. I feel like the “–config=opt –config=cuda” shouldn’t have been necessary after I’d run ./configure – but I seemed to have more success getting GPU support if I included them.
    Anyway – much obliged!

  • Francis Kim

    May 9, 2017 at 1:36 pm

    Thanks for your input John!

  • Ron

    June 8, 2017 at 11:40 pm

    It gives me an error when i try to retrain using bazel-bin/tensorflow/examples/image_retraining/retrain –image_dir ~/flower_photos

    It says ImportError: no module named backports

    Traceback (most recent call last):
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/examples/image_retraining/”, line 91, in
    import tensorflow as tf
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/”, line 24, in
    from tensorflow.python import *
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/”, line 64, in
    from tensorflow.python.framework.framework_lib import *
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/framework/”, line 100, in
    from tensorflow.python.framework.subscribe import subscribe
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/framework/”, line 26, in
    from tensorflow.python.ops import variables
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/ops/”, line 26, in
    from tensorflow.python.ops import control_flow_ops
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/ops/”, line 70, in
    from tensorflow.python.ops import tensor_array_ops
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/ops/”, line 33, in
    from tensorflow.python.util import tf_should_use
    File “/Users/ronit/models/tutorials/image/imagenet/tensorflow/bazel-bin/tensorflow/examples/image_retraining/retrain.runfiles/org_tensorflow/tensorflow/python/util/”, line 28, in
    from backports import weakref # pylint: disable=g-bad-import-order
    ImportError: No module named backports

  • Bodurin

    July 14, 2017 at 12:44 am

    Hey Francis, Thanks for sharing. Wonderful post.

Write a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.