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

[WIP] Export to Tensorflow Lite for Edge TPU #137

Open
wants to merge 3 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
5 changes: 3 additions & 2 deletions aocr/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def process_args(args, defaults):
% (defaults.EXPORT_PATH)))
parser_export.add_argument('--format', dest="format",
type=str, default=defaults.EXPORT_FORMAT,
choices=['frozengraph', 'savedmodel'],
choices=['frozengraph', 'savedmodel', 'lite'],
help=('export format'
' (default: %s)'
% (defaults.EXPORT_FORMAT)))
Expand Down Expand Up @@ -273,7 +273,8 @@ def main(args=None):
logging.info('Result: OK. %s %s', '{:.2f}'.format(probability), text)
elif parameters.phase == 'export':
exporter = Exporter(model)
exporter.save(parameters.export_path, parameters.format)
exporter.save(parameters.export_path, parameters.format,
parameters.max_width, parameters.max_height, parameters.channels)
return
else:
raise NotImplementedError
Expand Down
22 changes: 21 additions & 1 deletion aocr/util/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Exporter(object):
def __init__(self, model):
self.model = model

def save(self, path, model_format):
def save(self, path, model_format, max_width, max_height, channels):
if model_format == "savedmodel":
logging.info("Creating a SavedModel.")

Expand Down Expand Up @@ -51,3 +51,23 @@ def save(self, path, model_format):
outfile.write(output_graph_def.SerializeToString())

logging.info("Exported as %s", path + '/frozen_graph.pb')
elif model_format == "lite":
logging.info("Creating Tensorflow Lite graph.")

if not os.path.exists(path):
os.makedirs(path)

graph = self.model.sess.graph

input = graph.get_tensor_by_name('input_image_as_bytes:0')
shape = [1, max_height, max_width, channels]
input.set_shape(shape)
prediction_output = graph.get_tensor_by_name('prediction:0')
probability_output = graph.get_tensor_by_name('probability:0')
output = [prediction_output, probability_output]

converter = tf.lite.TFLiteConverter.from_session(self.model.sess, [input], output)
tflite_model = converter.convert()
open("saved_model.tflite", "wb").write(tflite_model)

logging.info("Exported as %s", path + '/saved_model.tflite')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup

REQUIRED_PACKAGES = ['distance', 'numpy', 'six', 'pillow']
VERSION = '0.7.6'
VERSION = '0.7.7'
try:
import pypandoc
README = pypandoc.convert('README.md', 'rst')
Expand Down