Skip to content

Commit 342d143

Browse files
committed
Add 'tensorflow-examples-legacy/' from commit '1fe112cc0b5b4a9646691e21620ef8138dc39dd5'
git-subtree-dir: tensorflow-examples-legacy git-subtree-mainline: 3e8d410 git-subtree-split: 1fe112c
2 parents 3e8d410 + 1fe112c commit 342d143

File tree

24 files changed

+2587
-0
lines changed

24 files changed

+2587
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TensorFlow for Java: Examples
2+
3+
These examples include using pre-trained models for [image
4+
classification](label_image) and [object detection](object_detection),
5+
and driving the [training](training) of a pre-defined model - all using the
6+
TensorFlow Java API.
7+
8+
The TensorFlow Java API does not have feature parity with the Python API.
9+
The Java API is most suitable for inference using pre-trained models
10+
and for training pre-defined models from a single Java process.
11+
12+
Python will be the most convenient language for defining the
13+
numerical computation of a model.
14+
15+
- [Slides](https://docs.google.com/presentation/d/e/2PACX-1vQ6DzxNTBrJo7K5P8t5_rBRGnyJoPUPBVOJR4ooHCwi4TlBFnIriFmI719rDNpcQzojqsV58aUqmBBx/pub?start=false&loop=false&delayms=3000) from January 2018.
16+
- See README.md in each subdirectory for details.
17+
18+
For a recent real-world example, see the use of this API to [assess microscope
19+
image quality](https://research.googleblog.com/2018/03/using-deep-learning-to-facilitate.html)
20+
in the image processing package [Fiji (ImageJ)](https://fiji.sc/).
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM tensorflow/tensorflow:1.4.0
2+
WORKDIR /
3+
RUN apt-get update
4+
RUN apt-get -y install maven openjdk-8-jdk
5+
RUN mvn dependency:get -Dartifact=org.tensorflow:tensorflow:1.4.0
6+
RUN mvn dependency:get -Dartifact=org.tensorflow:proto:1.4.0
7+
CMD ["/bin/bash", "-l"]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Dockerfile for building an image suitable for running the Java examples.
2+
3+
Typical usage:
4+
5+
```
6+
docker build -t java-tensorflow .
7+
docker run -it --rm -v ${PWD}/..:/examples -w /examples java-tensorflow
8+
```
9+
10+
That second command will pop you into a shell which has all
11+
the dependencies required to execute the scripts and Java
12+
examples.
13+
14+
The script `sanity_test.sh` builds this container and runs a compilation
15+
check on all the maven projects.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
#
3+
# Silly sanity test
4+
DIR="$(cd "$(dirname "$0")" && pwd -P)"
5+
6+
docker build -t java-tensorflow .
7+
docker run -it --rm -v ${PWD}/..:/examples java-tensorflow bash /examples/docker/test_inside_container.sh
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
cd /examples/label_image
6+
mvn compile
7+
8+
cd /examples/object_detection
9+
mvn compile
10+
11+
cd /examples/training
12+
mvn compile
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
images
2+
src/main/resources
3+
target
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Image Classification Example
2+
3+
1. Download the model:
4+
- If you have [TensorFlow 1.4+ for Python installed](https://www.tensorflow.org/install/),
5+
run `python ./download.py`
6+
- If not, but you have [docker](https://www.docker.com/get-docker) installed,
7+
run `download.sh`.
8+
9+
2. Compile [`LabelImage.java`](src/main/java/LabelImage.java):
10+
11+
```
12+
mvn compile
13+
```
14+
15+
3. Download some sample images:
16+
If you already have some images, great. Otherwise `download_sample_images.sh`
17+
gets a few.
18+
19+
3. Classify!
20+
21+
```
22+
mvn -q exec:java -Dexec.args="<path to image file>"
23+
```
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Create an image classification graph.
2+
3+
Script to download a pre-trained image classifier and tweak it so that
4+
the model accepts raw bytes of an encoded image.
5+
6+
Doing so involves some model-specific normalization of an image.
7+
Ideally, this would have been part of the image classifier model,
8+
but the particular model being used didn't include this normalization,
9+
so this script does the necessary tweaking.
10+
"""
11+
12+
from __future__ import absolute_import
13+
from __future__ import division
14+
from __future__ import print_function
15+
16+
from six.moves import urllib
17+
import os
18+
import zipfile
19+
import tensorflow as tf
20+
21+
URL = 'https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip'
22+
LABELS_FILE = 'imagenet_comp_graph_label_strings.txt'
23+
GRAPH_FILE = 'tensorflow_inception_graph.pb'
24+
25+
GRAPH_INPUT_TENSOR = 'input:0'
26+
GRAPH_PROBABILITIES_TENSOR = 'output:0'
27+
28+
IMAGE_HEIGHT = 224
29+
IMAGE_WIDTH = 224
30+
MEAN = 117
31+
SCALE = 1
32+
33+
LOCAL_DIR = 'src/main/resources'
34+
35+
36+
def download():
37+
print('Downloading %s' % URL)
38+
zip_filename, _ = urllib.request.urlretrieve(URL)
39+
with zipfile.ZipFile(zip_filename) as zip:
40+
zip.extract(LABELS_FILE)
41+
zip.extract(GRAPH_FILE)
42+
os.rename(LABELS_FILE, os.path.join(LOCAL_DIR, 'labels.txt'))
43+
os.rename(GRAPH_FILE, os.path.join(LOCAL_DIR, 'graph.pb'))
44+
45+
46+
def create_graph_to_decode_and_normalize_image():
47+
"""See file docstring.
48+
49+
Returns:
50+
input: The placeholder to feed the raw bytes of an encoded image.
51+
y: A Tensor (the decoded, normalized image) to be fed to the graph.
52+
"""
53+
image = tf.placeholder(tf.string, shape=(), name='encoded_image_bytes')
54+
with tf.name_scope("preprocess"):
55+
y = tf.image.decode_image(image, channels=3)
56+
y = tf.cast(y, tf.float32)
57+
y = tf.expand_dims(y, axis=0)
58+
y = tf.image.resize_bilinear(y, (IMAGE_HEIGHT, IMAGE_WIDTH))
59+
y = (y - MEAN) / SCALE
60+
return (image, y)
61+
62+
63+
def patch_graph():
64+
"""Create graph.pb that applies the model in URL to raw image bytes."""
65+
with tf.Graph().as_default() as g:
66+
input_image, image_normalized = create_graph_to_decode_and_normalize_image()
67+
original_graph_def = tf.GraphDef()
68+
with open(os.path.join(LOCAL_DIR, 'graph.pb')) as f:
69+
original_graph_def.ParseFromString(f.read())
70+
softmax = tf.import_graph_def(
71+
original_graph_def,
72+
name='inception',
73+
input_map={GRAPH_INPUT_TENSOR: image_normalized},
74+
return_elements=[GRAPH_PROBABILITIES_TENSOR])
75+
# We're constructing a graph that accepts a single image (as opposed to a
76+
# batch of images), so might as well make the output be a vector of
77+
# probabilities, instead of a batch of vectors with batch size 1.
78+
output_probabilities = tf.squeeze(softmax, name='probabilities')
79+
# Overwrite the graph.
80+
with open(os.path.join(LOCAL_DIR, 'graph.pb'), 'w') as f:
81+
f.write(g.as_graph_def().SerializeToString())
82+
print('------------------------------------------------------------')
83+
print('MODEL GRAPH : graph.pb')
84+
print('LABELS : labels.txt')
85+
print('INPUT TENSOR : %s' % input_image.op.name)
86+
print('OUTPUT TENSOR: %s' % output_probabilities.op.name)
87+
88+
89+
if __name__ == '__main__':
90+
if not os.path.exists(LOCAL_DIR):
91+
os.makedirs(LOCAL_DIR)
92+
download()
93+
patch_graph()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
DIR="$(cd "$(dirname "$0")" && pwd -P)"
4+
docker run -it -v ${DIR}:/x -w /x --rm tensorflow/tensorflow:1.4.0 python download.py
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
DIR=$(dirname $0)
3+
mkdir -p ${DIR}/images
4+
cd ${DIR}/images
5+
6+
# Some random images
7+
curl -o "porcupine.jpg" -L "https://cdn.pixabay.com/photo/2014/11/06/12/46/porcupines-519145_960_720.jpg"
8+
curl -o "whale.jpg" -L "https://static.pexels.com/photos/417196/pexels-photo-417196.jpeg"
9+
curl -o "terrier1u.jpg" -L "https://upload.wikimedia.org/wikipedia/commons/3/34/Australian_Terrier_Melly_%282%29.JPG"
10+
curl -o "terrier2.jpg" -L "https://cdn.pixabay.com/photo/2014/05/13/07/44/yorkshire-terrier-343198_960_720.jpg"

0 commit comments

Comments
 (0)