|
| 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() |
0 commit comments