README.md
1# TensorFlow C++ and Python Image Recognition Demo
2
3This example shows how you can load a pre-trained TensorFlow network and use it
4to recognize objects in images in C++. For Java see the [Java
5README](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java),
6and for Go see the [godoc
7example](https://godoc.org/github.com/tensorflow/tensorflow/tensorflow/go#ex-package).
8
9## Description
10
11This demo uses a Google Inception model to classify image files that are passed
12in on the command line.
13
14## To build/install/run
15
16The TensorFlow `GraphDef` that contains the model definition and weights is not
17packaged in the repo because of its size. Instead, you must first download the
18file to the `data` directory in the source tree:
19
20```bash
21$ curl -L "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz" |
22 tar -C tensorflow/examples/label_image/data -xz
23```
24
25Then, as long as you've managed to build the main TensorFlow framework, you
26should have everything you need to run this example installed already.
27
28Once extracted, see the labels file in the data directory for the possible
29classifications, which are the 1,000 categories used in the Imagenet
30competition.
31
32To build it, run this command:
33
34```bash
35$ bazel build tensorflow/examples/label_image/...
36```
37
38That should build a binary executable that you can then run like this:
39
40```bash
41$ bazel-bin/tensorflow/examples/label_image/label_image
42```
43
44This uses the default example image that ships with the framework, and should
45output something similar to this:
46
47```
48I tensorflow/examples/label_image/main.cc:206] military uniform (653): 0.834306
49I tensorflow/examples/label_image/main.cc:206] mortarboard (668): 0.0218692
50I tensorflow/examples/label_image/main.cc:206] academic gown (401): 0.0103579
51I tensorflow/examples/label_image/main.cc:206] pickelhaube (716): 0.00800814
52I tensorflow/examples/label_image/main.cc:206] bulletproof vest (466): 0.00535088
53```
54
55In this case, we're using the default image of Admiral Grace Hopper, and you can
56see the network correctly spots she's wearing a military uniform, with a high
57score of 0.8.
58
59Next, try it out on your own images by supplying the --image= argument, e.g.
60
61```bash
62$ bazel-bin/tensorflow/examples/label_image/label_image --image=my_image.png
63```
64
65For a more detailed look at this code, you can check out the C++ section of the
66[Inception tutorial](https://www.tensorflow.org/tutorials/image_recognition/).
67
68## Python implementation
69
70label_image.py is a python implementation that provides code corresponding
71to the C++ code here. This gives more intuitive mapping between C++ and
72Python than the Python code mentioned in the
73[Inception tutorial](https://www.tensorflow.org/tutorials/image_recognition/).
74and could be easier to add visualization or debug code.
75
76
77`bazel-bin/tensorflow/examples/label_image/label_image_py` should be there after
78```bash
79$ bazel build tensorflow/examples/label_image/...
80```
81
82Run
83
84```bash
85$ bazel-bin/tensorflow/examples/label_image/label_image_py
86```
87
88Or, with tensorflow python package installed, you can run it like:
89```bash
90$ python3 tensorflow/examples/label_image/label_image.py
91```
92
93And get result similar to this:
94```
95military uniform 0.834305
96mortarboard 0.0218694
97academic gown 0.0103581
98pickelhaube 0.00800818
99bulletproof vest 0.0053509
100```
101