Delete duplicate label_image script.
The version in examples/label_image is more complete (with image size and normalization options), so it can be used with `mobilenets`. Also: removed bazel from main tutorial instructions. PiperOrigin-RevId: 174212674
This commit is contained in:
parent
7a5b81c290
commit
6c4a769ab5
@ -14,10 +14,11 @@ laptop, without requiring a GPU. This tutorial will show you how to run the
|
|||||||
example script on your own images, and will explain some of the options you have
|
example script on your own images, and will explain some of the options you have
|
||||||
to help control the training process.
|
to help control the training process.
|
||||||
|
|
||||||
Note: This version of the tutorial mainly uses bazel. A bazel free version is
|
Note: A version of this tutorial is also available
|
||||||
also available
|
|
||||||
[as a codelab](https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0).
|
[as a codelab](https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0).
|
||||||
|
|
||||||
|
Before you start, you must @{$install$install tensorflow}.
|
||||||
|
|
||||||
[TOC]
|
[TOC]
|
||||||
|
|
||||||
## Training on Flowers
|
## Training on Flowers
|
||||||
@ -38,26 +39,25 @@ curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
|
|||||||
tar xzf flower_photos.tgz
|
tar xzf flower_photos.tgz
|
||||||
```
|
```
|
||||||
|
|
||||||
Once you have the images, you can build the retrainer like this, from the root
|
Once you have the images, you can clone the tensorflow repository using the
|
||||||
of your TensorFlow source directory:
|
following command (these examples are not included in the installation):
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
bazel build tensorflow/examples/image_retraining:retrain
|
git clone https://github.com/tensorflow/tensorflow
|
||||||
|
|
||||||
|
cd tensorflow
|
||||||
```
|
```
|
||||||
|
|
||||||
If you have a machine which supports
|
In the simplest cases the retrainer can then be run like this:
|
||||||
[the AVX instruction set](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)
|
|
||||||
(common in x86 CPUs produced in the last few years) you can improve the running
|
|
||||||
speed of the retraining by building for that architecture, like this (after choosing appropriate options in `configure`):
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
bazel build --config opt tensorflow/examples/image_retraining:retrain
|
python tensorflow/examples/image_retraining/retrain.py --image_dir ~/flower_photos
|
||||||
```
|
```
|
||||||
|
|
||||||
The retrainer can then be run like this:
|
The script has many other options. You can get a full listing with:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
bazel-bin/tensorflow/examples/image_retraining/retrain --image_dir ~/flower_photos
|
python tensorflow/examples/image_retraining/retrain.py -h
|
||||||
```
|
```
|
||||||
|
|
||||||
This script loads the pre-trained Inception v3 model, removes the old top layer,
|
This script loads the pre-trained Inception v3 model, removes the old top layer,
|
||||||
@ -149,26 +149,28 @@ can read in, so you can start using your new model immediately. Since you've
|
|||||||
replaced the top layer, you will need to specify the new name in the script, for
|
replaced the top layer, you will need to specify the new name in the script, for
|
||||||
example with the flag `--output_layer=final_result` if you're using label_image.
|
example with the flag `--output_layer=final_result` if you're using label_image.
|
||||||
|
|
||||||
Here's an example of how to build and run the label_image example with your
|
Here's an example of how to run the label_image example with your
|
||||||
retrained graphs:
|
retrained graphs:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
bazel build tensorflow/examples/image_retraining:label_image && \
|
python tensorflow/examples/label_image/label_image.py \
|
||||||
bazel-bin/tensorflow/examples/image_retraining/label_image \
|
|
||||||
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
|
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
|
||||||
--output_layer=final_result:0 \
|
--input_layer=Mul \
|
||||||
|
--output_layer=final_result \
|
||||||
|
--input_mean=128 --input_std=128 \
|
||||||
--image=$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg
|
--image=$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg
|
||||||
```
|
```
|
||||||
|
|
||||||
You should see a list of flower labels, in most cases with daisy on top
|
You should see a list of flower labels, in most cases with daisy on top
|
||||||
(though each retrained model may be slightly different). You can replace the
|
(though each retrained model may be slightly different). You can replace the
|
||||||
`--image` parameter with your own images to try those out, and use the C++ code
|
`--image` parameter with your own images to try those out.
|
||||||
as a template to integrate with your own applications.
|
|
||||||
|
|
||||||
If you'd like to use the retrained model in your own Python program, then the
|
If you'd like to use the retrained model in your own Python program, then the
|
||||||
above
|
above
|
||||||
[`label_image` script](https://www.tensorflow.org/code/tensorflow/examples/image_retraining/label_image.py)
|
[`label_image` script](https://www.tensorflow.org/code/tensorflow/examples/label_image/label_image.py)
|
||||||
is a reasonable starting point.
|
is a reasonable starting point. The `label_image`
|
||||||
|
directory also contains C++ code which you can use as a template to integrate
|
||||||
|
tensorflow with your own applications.
|
||||||
|
|
||||||
If you find the default Inception v3 model is too large or slow for your
|
If you find the default Inception v3 model is too large or slow for your
|
||||||
application, take a look at the [Other Model Architectures section](/tutorials/image_retraining#other_model_architectures)
|
application, take a look at the [Other Model Architectures section](/tutorials/image_retraining#other_model_architectures)
|
||||||
@ -372,3 +374,18 @@ programs, you'll need to feed in an image of the specified size converted to a
|
|||||||
float range into the 'input' tensor. Typically 24-bit images are in the range
|
float range into the 'input' tensor. Typically 24-bit images are in the range
|
||||||
[0,255], and you must convert them to the [-1,1] float range expected by the
|
[0,255], and you must convert them to the [-1,1] float range expected by the
|
||||||
model with the formula `(image - 128.)/128.`.
|
model with the formula `(image - 128.)/128.`.
|
||||||
|
|
||||||
|
The default arguments for the `label_image` script are set for Inception V3.
|
||||||
|
To use it with a MobileNet, specify the above normalization parameters as
|
||||||
|
`input_mean` and `input_std` on the command line. You also must specify the
|
||||||
|
image size that your model expects, as follows:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python tensorflow/examples/label_image/label_image.py \
|
||||||
|
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
|
||||||
|
--input_layer=input \
|
||||||
|
--output_layer=final_result:0 \
|
||||||
|
--input_height=224 --input_width=224 \
|
||||||
|
--input_mean=128 --input_std=128 \
|
||||||
|
--image=$HOME/flower_photos/daisy/21652746_cc379e0eea_m.jpg
|
||||||
|
```
|
||||||
|
@ -25,23 +25,10 @@ py_binary(
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
py_binary(
|
|
||||||
name = "label_image",
|
|
||||||
srcs = [
|
|
||||||
"label_image.py",
|
|
||||||
],
|
|
||||||
srcs_version = "PY2AND3",
|
|
||||||
visibility = ["//tensorflow:__subpackages__"],
|
|
||||||
deps = [
|
|
||||||
"//tensorflow:tensorflow_py",
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
py_test(
|
py_test(
|
||||||
name = "retrain_test",
|
name = "retrain_test",
|
||||||
size = "small",
|
size = "small",
|
||||||
srcs = [
|
srcs = [
|
||||||
"label_image.py",
|
|
||||||
"retrain.py",
|
"retrain.py",
|
||||||
"retrain_test.py",
|
"retrain_test.py",
|
||||||
],
|
],
|
||||||
@ -51,7 +38,6 @@ py_test(
|
|||||||
],
|
],
|
||||||
srcs_version = "PY2AND3",
|
srcs_version = "PY2AND3",
|
||||||
deps = [
|
deps = [
|
||||||
":label_image",
|
|
||||||
":retrain",
|
":retrain",
|
||||||
"//tensorflow:tensorflow_py",
|
"//tensorflow:tensorflow_py",
|
||||||
"//tensorflow/python:framework_test_lib",
|
"//tensorflow/python:framework_test_lib",
|
||||||
|
@ -1,147 +0,0 @@
|
|||||||
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# You may obtain a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
# See the License for the specific language governing permissions and
|
|
||||||
# limitations under the License.
|
|
||||||
# ==============================================================================
|
|
||||||
"""Simple image classification with Inception.
|
|
||||||
|
|
||||||
Run image classification with your model.
|
|
||||||
|
|
||||||
This script is usually used with retrain.py found in this same
|
|
||||||
directory.
|
|
||||||
|
|
||||||
This program creates a graph from a saved GraphDef protocol buffer,
|
|
||||||
and runs inference on an input JPEG image. You are required
|
|
||||||
to pass in the graph file and the txt file.
|
|
||||||
|
|
||||||
It outputs human readable strings of the top 5 predictions along with
|
|
||||||
their probabilities.
|
|
||||||
|
|
||||||
Change the --image_file argument to any jpg image to compute a
|
|
||||||
classification of that image.
|
|
||||||
|
|
||||||
Example usage:
|
|
||||||
python label_image.py --graph=retrained_graph.pb
|
|
||||||
--labels=retrained_labels.txt
|
|
||||||
--image=flower_photos/daisy/54377391_15648e8d18.jpg
|
|
||||||
|
|
||||||
NOTE: To learn to use this file and retrain.py, please see:
|
|
||||||
|
|
||||||
https://codelabs.developers.google.com/codelabs/tensorflow-for-poets
|
|
||||||
"""
|
|
||||||
from __future__ import absolute_import
|
|
||||||
from __future__ import division
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import tensorflow as tf
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument(
|
|
||||||
'--image', required=True, type=str, help='Absolute path to image file.')
|
|
||||||
parser.add_argument(
|
|
||||||
'--num_top_predictions',
|
|
||||||
type=int,
|
|
||||||
default=5,
|
|
||||||
help='Display this many predictions.')
|
|
||||||
parser.add_argument(
|
|
||||||
'--graph',
|
|
||||||
required=True,
|
|
||||||
type=str,
|
|
||||||
help='Absolute path to graph file (.pb)')
|
|
||||||
parser.add_argument(
|
|
||||||
'--labels',
|
|
||||||
required=True,
|
|
||||||
type=str,
|
|
||||||
help='Absolute path to labels file (.txt)')
|
|
||||||
parser.add_argument(
|
|
||||||
'--output_layer',
|
|
||||||
type=str,
|
|
||||||
default='final_result:0',
|
|
||||||
help='Name of the result operation')
|
|
||||||
parser.add_argument(
|
|
||||||
'--input_layer',
|
|
||||||
type=str,
|
|
||||||
default='DecodeJpeg/contents:0',
|
|
||||||
help='Name of the input operation')
|
|
||||||
|
|
||||||
|
|
||||||
def load_image(filename):
|
|
||||||
"""Read in the image_data to be classified."""
|
|
||||||
return tf.gfile.FastGFile(filename, 'rb').read()
|
|
||||||
|
|
||||||
|
|
||||||
def load_labels(filename):
|
|
||||||
"""Read in labels, one label per line."""
|
|
||||||
return [line.rstrip() for line in tf.gfile.GFile(filename)]
|
|
||||||
|
|
||||||
|
|
||||||
def load_graph(filename):
|
|
||||||
"""Unpersists graph from file as default graph."""
|
|
||||||
with tf.gfile.FastGFile(filename, 'rb') as f:
|
|
||||||
graph_def = tf.GraphDef()
|
|
||||||
graph_def.ParseFromString(f.read())
|
|
||||||
tf.import_graph_def(graph_def, name='')
|
|
||||||
|
|
||||||
|
|
||||||
def run_graph(image_data, labels, input_layer_name, output_layer_name,
|
|
||||||
num_top_predictions):
|
|
||||||
with tf.Session() as sess:
|
|
||||||
# Feed the image_data as input to the graph.
|
|
||||||
# predictions will contain a two-dimensional array, where one
|
|
||||||
# dimension represents the input image count, and the other has
|
|
||||||
# predictions per class
|
|
||||||
softmax_tensor = sess.graph.get_tensor_by_name(output_layer_name)
|
|
||||||
predictions, = sess.run(softmax_tensor, {input_layer_name: image_data})
|
|
||||||
|
|
||||||
# Sort to show labels in order of confidence
|
|
||||||
top_k = predictions.argsort()[-num_top_predictions:][::-1]
|
|
||||||
for node_id in top_k:
|
|
||||||
human_string = labels[node_id]
|
|
||||||
score = predictions[node_id]
|
|
||||||
print('%s (score = %.5f)' % (human_string, score))
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
|
||||||
"""Runs inference on an image."""
|
|
||||||
if argv[1:]:
|
|
||||||
raise ValueError('Unused Command Line Args: %s' % argv[1:])
|
|
||||||
|
|
||||||
if not tf.gfile.Exists(FLAGS.image):
|
|
||||||
tf.logging.fatal('image file does not exist %s', FLAGS.image)
|
|
||||||
|
|
||||||
if not tf.gfile.Exists(FLAGS.labels):
|
|
||||||
tf.logging.fatal('labels file does not exist %s', FLAGS.labels)
|
|
||||||
|
|
||||||
if not tf.gfile.Exists(FLAGS.graph):
|
|
||||||
tf.logging.fatal('graph file does not exist %s', FLAGS.graph)
|
|
||||||
|
|
||||||
# load image
|
|
||||||
image_data = load_image(FLAGS.image)
|
|
||||||
|
|
||||||
# load labels
|
|
||||||
labels = load_labels(FLAGS.labels)
|
|
||||||
|
|
||||||
# load graph, which is stored in the default session
|
|
||||||
load_graph(FLAGS.graph)
|
|
||||||
|
|
||||||
run_graph(image_data, labels, FLAGS.input_layer, FLAGS.output_layer,
|
|
||||||
FLAGS.num_top_predictions)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
FLAGS, unparsed = parser.parse_known_args()
|
|
||||||
tf.app.run(main=main, argv=sys.argv[:1]+unparsed)
|
|
@ -21,7 +21,6 @@ from __future__ import print_function
|
|||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from tensorflow.examples.image_retraining import label_image
|
|
||||||
from tensorflow.examples.image_retraining import retrain
|
from tensorflow.examples.image_retraining import retrain
|
||||||
from tensorflow.python.framework import test_util
|
from tensorflow.python.framework import test_util
|
||||||
|
|
||||||
@ -83,36 +82,6 @@ class ImageRetrainingTest(test_util.TensorFlowTestCase):
|
|||||||
gt = tf.placeholder(tf.float32, [1], name='gt')
|
gt = tf.placeholder(tf.float32, [1], name='gt')
|
||||||
self.assertIsNotNone(retrain.add_evaluation_step(final, gt))
|
self.assertIsNotNone(retrain.add_evaluation_step(final, gt))
|
||||||
|
|
||||||
def testLabelImage(self):
|
|
||||||
|
|
||||||
image_filename = ('../label_image/data/grace_hopper.jpg')
|
|
||||||
|
|
||||||
# Load some default data
|
|
||||||
label_path = os.path.join(tf.resource_loader.get_data_files_path(),
|
|
||||||
'data/labels.txt')
|
|
||||||
labels = label_image.load_labels(label_path)
|
|
||||||
self.assertEqual(len(labels), 3)
|
|
||||||
|
|
||||||
image_path = os.path.join(tf.resource_loader.get_data_files_path(),
|
|
||||||
image_filename)
|
|
||||||
|
|
||||||
image = label_image.load_image(image_path)
|
|
||||||
self.assertEqual(len(image), 61306)
|
|
||||||
|
|
||||||
# Create trivial graph; note that the two nodes don't meet
|
|
||||||
with tf.Graph().as_default():
|
|
||||||
jpeg = tf.constant(image)
|
|
||||||
# Input node that doesn't lead anywhere.
|
|
||||||
tf.image.decode_jpeg(jpeg, name='DecodeJpeg')
|
|
||||||
|
|
||||||
# Output node, that always outputs a constant.
|
|
||||||
tf.constant([[10, 30, 5]], name='final')
|
|
||||||
|
|
||||||
# As label_image outputs via print, we assume that
|
|
||||||
# if it returns, everything is OK.
|
|
||||||
result = label_image.run_graph(image, labels, jpeg, 'final:0', 3)
|
|
||||||
self.assertEqual(result, 0)
|
|
||||||
|
|
||||||
def testAddJpegDecoding(self):
|
def testAddJpegDecoding(self):
|
||||||
with tf.Graph().as_default():
|
with tf.Graph().as_default():
|
||||||
jpeg_data, mul_image = retrain.add_jpeg_decoding(10, 10, 3, 0, 255)
|
jpeg_data, mul_image = retrain.add_jpeg_decoding(10, 10, 3, 0, 255)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user