diff --git a/models/inception_v3_ufo/README.md b/models/inception_v3_ufo/README.md new file mode 100644 index 0000000..6dd0bcb --- /dev/null +++ b/models/inception_v3_ufo/README.md @@ -0,0 +1,44 @@ +# Retrain Inception v3 Model (ImageCaptioning) + +The model in this folder has been retrained with the UFO images. The based model is from the ImageCaptioning Project that used the Inception v3 Model. + +This folder includes model checkpoint files and a freeze graph of the model. +The model checkpoint files can be used for further retraining. + +The graph (.pb) can be used to classify UFO images into five categories, i.e. UFO, Flare, Dust, Bird, Plane. + +## How to obtain the checkpoint and the graph (.pb) file? +``` +sh extract.sh +``` + +## How to integrate with Tika-Docker (USCDatascience)? +Please refers to https://wiki.apache.org/tika/ImageCaption +for downloading tika-docker and required config files. + +After downloading the config files. You have to update the "Im2txtRestDockerfile" to apply the retrain checkpoint files to the tika-docker. + +Below is the old code that we need to update +``` +RUN echo "We're downloading the checkpoint file for image captioning, the shell might look unresponsive. Please be patient." && \ + # To get rid of early EOF error + git config --global http.postBuffer 1048576000 && \ + git clone https://github.com/USCDataScience/img2text.git && \ + # Join the parts + cat img2text/models/1M_iters_ckpt_parts_* >1M_iters_ckpt.tar.gz && \ + tar -xzvf 1M_iters_ckpt.tar.gz && rm -rf 1M_iters_ckpt.tar.gz +``` + +The docker file download the inceptionv3 model checkpoint file. +However, We want to use the retrain inceptionv3 checkpoint instead so we use command +``` +COPY path/to/each/checkpoint/file /usr/share/apache-tika/models/dl/image/caption/1M_iters_ckpt/model.ckpt-1000000 +``` +This command will copy the checkpoint file to the docker container path. + +## How to use the graph model (.pb)? +You can use the "label_image.py" file to validate the model like so, + +``` +python label_image.py --graph=path/to/output_graph.pb --labels=path/to/output_labels.txt --input_layer=Placeholder --output_layer=final_result --image=path/to/image/file +``` \ No newline at end of file diff --git a/models/inception_v3_ufo/extract.sh b/models/inception_v3_ufo/extract.sh new file mode 100644 index 0000000..cad9307 --- /dev/null +++ b/models/inception_v3_ufo/extract.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cat inceptionv3_model_part_a* >inceptionv3_model.tar.gz && \ +tar -xzvf inceptionv3_model.tar.gz \ No newline at end of file diff --git a/models/inception_v3_ufo/inceptionv3_model_part_aa b/models/inception_v3_ufo/inceptionv3_model_part_aa new file mode 100644 index 0000000..8207dac Binary files /dev/null and b/models/inception_v3_ufo/inceptionv3_model_part_aa differ diff --git a/models/inception_v3_ufo/inceptionv3_model_part_ab b/models/inception_v3_ufo/inceptionv3_model_part_ab new file mode 100644 index 0000000..e59d82a Binary files /dev/null and b/models/inception_v3_ufo/inceptionv3_model_part_ab differ diff --git a/models/inception_v3_ufo/inceptionv3_model_part_ac b/models/inception_v3_ufo/inceptionv3_model_part_ac new file mode 100644 index 0000000..b6a8dc8 Binary files /dev/null and b/models/inception_v3_ufo/inceptionv3_model_part_ac differ diff --git a/models/inception_v3_ufo/inceptionv3_model_part_ad b/models/inception_v3_ufo/inceptionv3_model_part_ad new file mode 100644 index 0000000..e2f43f5 Binary files /dev/null and b/models/inception_v3_ufo/inceptionv3_model_part_ad differ diff --git a/models/inception_v3_ufo/label/output_labels.txt b/models/inception_v3_ufo/label/output_labels.txt new file mode 100644 index 0000000..d568088 --- /dev/null +++ b/models/inception_v3_ufo/label/output_labels.txt @@ -0,0 +1,5 @@ +bird +dust +flare +plane +ufo diff --git a/models/inception_v3_ufo/label_image.py b/models/inception_v3_ufo/label_image.py new file mode 100644 index 0000000..fe5e0fc --- /dev/null +++ b/models/inception_v3_ufo/label_image.py @@ -0,0 +1,140 @@ +# Copyright 2017 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. +# ============================================================================== + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import argparse + +import numpy as np +import tensorflow as tf + + +def load_graph(model_file): + graph = tf.Graph() + graph_def = tf.GraphDef() + + with open(model_file, "rb") as f: + graph_def.ParseFromString(f.read()) + with graph.as_default(): + tf.import_graph_def(graph_def) + + return graph + + +def read_tensor_from_image_file(file_name, + input_height=299, + input_width=299, + input_mean=0, + input_std=255): + input_name = "file_reader" + output_name = "normalized" + file_reader = tf.read_file(file_name, input_name) + if file_name.endswith(".png"): + image_reader = tf.image.decode_png( + file_reader, channels=3, name="png_reader") + elif file_name.endswith(".gif"): + image_reader = tf.squeeze( + tf.image.decode_gif(file_reader, name="gif_reader")) + elif file_name.endswith(".bmp"): + image_reader = tf.image.decode_bmp(file_reader, name="bmp_reader") + else: + image_reader = tf.image.decode_jpeg( + file_reader, channels=3, name="jpeg_reader") + float_caster = tf.cast(image_reader, tf.float32) + dims_expander = tf.expand_dims(float_caster, 0) + resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width]) + normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std]) + sess = tf.Session() + result = sess.run(normalized) + + return result + + +def load_labels(label_file): + label = [] + proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines() + for l in proto_as_ascii_lines: + label.append(l.rstrip()) + return label + + +if __name__ == "__main__": + file_name = "tensorflow/examples/label_image/data/grace_hopper.jpg" + model_file = \ + "tensorflow/examples/label_image/data/inception_v3_2016_08_28_frozen.pb" + label_file = "tensorflow/examples/label_image/data/imagenet_slim_labels.txt" + input_height = 299 + input_width = 299 + input_mean = 0 + input_std = 255 + input_layer = "input" + output_layer = "InceptionV3/Predictions/Reshape_1" + + parser = argparse.ArgumentParser() + parser.add_argument("--image", help="image to be processed") + parser.add_argument("--graph", help="graph/model to be executed") + parser.add_argument("--labels", help="name of file containing labels") + parser.add_argument("--input_height", type=int, help="input height") + parser.add_argument("--input_width", type=int, help="input width") + parser.add_argument("--input_mean", type=int, help="input mean") + parser.add_argument("--input_std", type=int, help="input std") + parser.add_argument("--input_layer", help="name of input layer") + parser.add_argument("--output_layer", help="name of output layer") + args = parser.parse_args() + + if args.graph: + model_file = args.graph + if args.image: + file_name = args.image + if args.labels: + label_file = args.labels + if args.input_height: + input_height = args.input_height + if args.input_width: + input_width = args.input_width + if args.input_mean: + input_mean = args.input_mean + if args.input_std: + input_std = args.input_std + if args.input_layer: + input_layer = args.input_layer + if args.output_layer: + output_layer = args.output_layer + + graph = load_graph(model_file) + t = read_tensor_from_image_file( + file_name, + input_height=input_height, + input_width=input_width, + input_mean=input_mean, + input_std=input_std) + + input_name = "import/" + input_layer + output_name = "import/" + output_layer + input_operation = graph.get_operation_by_name(input_name) + output_operation = graph.get_operation_by_name(output_name) + + with tf.Session(graph=graph) as sess: + results = sess.run(output_operation.outputs[0], { + input_operation.outputs[0]: t + }) + results = np.squeeze(results) + + top_k = results.argsort()[-5:][::-1] + labels = load_labels(label_file) + for i in top_k: + print(labels[i], results[i]) diff --git a/models/inception_v4_ufo/README.md b/models/inception_v4_ufo/README.md new file mode 100644 index 0000000..e1b0e1e --- /dev/null +++ b/models/inception_v4_ufo/README.md @@ -0,0 +1,39 @@ +# Retrain Inception v4 Model (Image Recognition Deep Learning model) + +The model in this folder has been retrained with the UFO images. The based model is from the Image Recognition Deep Learning model Project (UFO object types) that used the Inception v4 Model. + +This folder includes model checkpoint files and a freeze graph of the model. +The model checkpoint files can be used for further retraining. + +The graph (.pb) can be used to classify UFO images into five categories, i.e. UFO, Flare, Dust, Bird, Plane. + +## How to obtain the checkpoint and the graph (.pb) file? +``` +sh extract.sh +``` + +## How to integrate with Tika-Docker (USCDatascience)? +Please refers to https://wiki.apache.org/tika/TikaAndVision +for downloading tika-docker and required config files. + +After downloading the config files. You have to update the "InceptionRestDockerfile" to apply the retrain checkpoint files to the tika-docker. + +Below is the old code that we need to update +``` +RUN curl -O http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz && \ + tar -xzvf inception_v4_2016_09_09.tar.gz && rm -rf inception_v4_2016_09_09.tar.gz && \ +``` + +The docker file download the inceptionv4 model checkpoint file. +However, We want to use the retrain inceptionv4 checkpoint instead so we use command +``` +COPY path/to/each/checkpoint/file /usr/share/apache-tika/models/dl/image-video/recognition/inceptionv4.ckpt +``` +This command will copy the checkpoint file to the docker container path. + +## How to use the graph model (.pb)? +You can use the "label_image.py" file to validate the model like so, + +``` +python label_image.py --graph=path/to/output_graphv4.pb --labels=path/to/output_labelsv4.txt --image=path/to/image/file --input_height=299 --input_width=299 --input_mean=128 --input_std=128 --input_layer=InputImage --output_layer=final_result +``` \ No newline at end of file diff --git a/models/inception_v4_ufo/extract.sh b/models/inception_v4_ufo/extract.sh new file mode 100644 index 0000000..f38ec0f --- /dev/null +++ b/models/inception_v4_ufo/extract.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cat inceptionv4_model_part_a* >inceptionv4_model.tar.gz && \ +tar -xzvf inceptionv4_model.tar.gz \ No newline at end of file diff --git a/models/inception_v4_ufo/inceptionv4_model_part_aa b/models/inception_v4_ufo/inceptionv4_model_part_aa new file mode 100644 index 0000000..ffce5ad Binary files /dev/null and b/models/inception_v4_ufo/inceptionv4_model_part_aa differ diff --git a/models/inception_v4_ufo/inceptionv4_model_part_ab b/models/inception_v4_ufo/inceptionv4_model_part_ab new file mode 100644 index 0000000..3110eb4 Binary files /dev/null and b/models/inception_v4_ufo/inceptionv4_model_part_ab differ diff --git a/models/inception_v4_ufo/inceptionv4_model_part_ac b/models/inception_v4_ufo/inceptionv4_model_part_ac new file mode 100644 index 0000000..0ae6f4f Binary files /dev/null and b/models/inception_v4_ufo/inceptionv4_model_part_ac differ diff --git a/models/inception_v4_ufo/inceptionv4_model_part_ad b/models/inception_v4_ufo/inceptionv4_model_part_ad new file mode 100644 index 0000000..0ee8af6 Binary files /dev/null and b/models/inception_v4_ufo/inceptionv4_model_part_ad differ diff --git a/models/inception_v4_ufo/inceptionv4_model_part_ae b/models/inception_v4_ufo/inceptionv4_model_part_ae new file mode 100644 index 0000000..1f03199 Binary files /dev/null and b/models/inception_v4_ufo/inceptionv4_model_part_ae differ diff --git a/models/inception_v4_ufo/inceptionv4_model_part_af b/models/inception_v4_ufo/inceptionv4_model_part_af new file mode 100644 index 0000000..0e31a55 Binary files /dev/null and b/models/inception_v4_ufo/inceptionv4_model_part_af differ diff --git a/models/inception_v4_ufo/inceptionv4_model_part_ag b/models/inception_v4_ufo/inceptionv4_model_part_ag new file mode 100644 index 0000000..7fddb30 Binary files /dev/null and b/models/inception_v4_ufo/inceptionv4_model_part_ag differ diff --git a/models/inception_v4_ufo/label_image.py b/models/inception_v4_ufo/label_image.py new file mode 100644 index 0000000..fe5e0fc --- /dev/null +++ b/models/inception_v4_ufo/label_image.py @@ -0,0 +1,140 @@ +# Copyright 2017 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. +# ============================================================================== + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import argparse + +import numpy as np +import tensorflow as tf + + +def load_graph(model_file): + graph = tf.Graph() + graph_def = tf.GraphDef() + + with open(model_file, "rb") as f: + graph_def.ParseFromString(f.read()) + with graph.as_default(): + tf.import_graph_def(graph_def) + + return graph + + +def read_tensor_from_image_file(file_name, + input_height=299, + input_width=299, + input_mean=0, + input_std=255): + input_name = "file_reader" + output_name = "normalized" + file_reader = tf.read_file(file_name, input_name) + if file_name.endswith(".png"): + image_reader = tf.image.decode_png( + file_reader, channels=3, name="png_reader") + elif file_name.endswith(".gif"): + image_reader = tf.squeeze( + tf.image.decode_gif(file_reader, name="gif_reader")) + elif file_name.endswith(".bmp"): + image_reader = tf.image.decode_bmp(file_reader, name="bmp_reader") + else: + image_reader = tf.image.decode_jpeg( + file_reader, channels=3, name="jpeg_reader") + float_caster = tf.cast(image_reader, tf.float32) + dims_expander = tf.expand_dims(float_caster, 0) + resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width]) + normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std]) + sess = tf.Session() + result = sess.run(normalized) + + return result + + +def load_labels(label_file): + label = [] + proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines() + for l in proto_as_ascii_lines: + label.append(l.rstrip()) + return label + + +if __name__ == "__main__": + file_name = "tensorflow/examples/label_image/data/grace_hopper.jpg" + model_file = \ + "tensorflow/examples/label_image/data/inception_v3_2016_08_28_frozen.pb" + label_file = "tensorflow/examples/label_image/data/imagenet_slim_labels.txt" + input_height = 299 + input_width = 299 + input_mean = 0 + input_std = 255 + input_layer = "input" + output_layer = "InceptionV3/Predictions/Reshape_1" + + parser = argparse.ArgumentParser() + parser.add_argument("--image", help="image to be processed") + parser.add_argument("--graph", help="graph/model to be executed") + parser.add_argument("--labels", help="name of file containing labels") + parser.add_argument("--input_height", type=int, help="input height") + parser.add_argument("--input_width", type=int, help="input width") + parser.add_argument("--input_mean", type=int, help="input mean") + parser.add_argument("--input_std", type=int, help="input std") + parser.add_argument("--input_layer", help="name of input layer") + parser.add_argument("--output_layer", help="name of output layer") + args = parser.parse_args() + + if args.graph: + model_file = args.graph + if args.image: + file_name = args.image + if args.labels: + label_file = args.labels + if args.input_height: + input_height = args.input_height + if args.input_width: + input_width = args.input_width + if args.input_mean: + input_mean = args.input_mean + if args.input_std: + input_std = args.input_std + if args.input_layer: + input_layer = args.input_layer + if args.output_layer: + output_layer = args.output_layer + + graph = load_graph(model_file) + t = read_tensor_from_image_file( + file_name, + input_height=input_height, + input_width=input_width, + input_mean=input_mean, + input_std=input_std) + + input_name = "import/" + input_layer + output_name = "import/" + output_layer + input_operation = graph.get_operation_by_name(input_name) + output_operation = graph.get_operation_by_name(output_name) + + with tf.Session(graph=graph) as sess: + results = sess.run(output_operation.outputs[0], { + input_operation.outputs[0]: t + }) + results = np.squeeze(results) + + top_k = results.argsort()[-5:][::-1] + labels = load_labels(label_file) + for i in top_k: + print(labels[i], results[i]) diff --git a/models/inception_v4_ufo/labelv4/output_labelsv4.txt b/models/inception_v4_ufo/labelv4/output_labelsv4.txt new file mode 100644 index 0000000..09edb2d --- /dev/null +++ b/models/inception_v4_ufo/labelv4/output_labelsv4.txt @@ -0,0 +1,5 @@ +dust +flare +plane +bird +ufo