Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the ImageCaptioning (inception_V3) and Image Recognition (inception_V4) Model (TEAM13) #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions models/inception_v3_ufo/README.md
Original file line number Diff line number Diff line change
@@ -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
```
3 changes: 3 additions & 0 deletions models/inception_v3_ufo/extract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cat inceptionv3_model_part_a* >inceptionv3_model.tar.gz && \
tar -xzvf inceptionv3_model.tar.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added models/inception_v3_ufo/inceptionv3_model_part_ad
Binary file not shown.
5 changes: 5 additions & 0 deletions models/inception_v3_ufo/label/output_labels.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bird
dust
flare
plane
ufo
140 changes: 140 additions & 0 deletions models/inception_v3_ufo/label_image.py
Original file line number Diff line number Diff line change
@@ -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])
39 changes: 39 additions & 0 deletions models/inception_v4_ufo/README.md
Original file line number Diff line number Diff line change
@@ -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
```
3 changes: 3 additions & 0 deletions models/inception_v4_ufo/extract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cat inceptionv4_model_part_a* >inceptionv4_model.tar.gz && \
tar -xzvf inceptionv4_model.tar.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added models/inception_v4_ufo/inceptionv4_model_part_ag
Binary file not shown.
140 changes: 140 additions & 0 deletions models/inception_v4_ufo/label_image.py
Original file line number Diff line number Diff line change
@@ -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])
5 changes: 5 additions & 0 deletions models/inception_v4_ufo/labelv4/output_labelsv4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dust
flare
plane
bird
ufo