Skip to content

Commit

Permalink
Fix Issue 1: add support for BodyPix and Posenet RESNET50 models
Browse files Browse the repository at this point in the history
  • Loading branch information
patlevin committed Jan 11, 2020
1 parent a596cb8 commit b6fcd32
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
48 changes: 34 additions & 14 deletions tfjs_graph_converter/api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
# Copyright 2019 Patrick Levin. 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.
# ==============================================================================
# SPDX-License-Identifier: MIT
# Copyright © 2020 Patrick Levin

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
Expand Down Expand Up @@ -70,6 +59,36 @@ def _convert_string_attrs(node):

return

def _fix_dilation_attrs(node):
"""
Search dilations-attribute and convert
misaligned dilation rates if necessary see
https://github.com/patlevin/tfjs-to-tf/issues/1
"""
path = ['attr', 'dilations', 'list']
values = node
for key in path:
if key in values:
values = values[key]
else:
values = None
break

# if dilations are present, they're stored in 'values' now
ints = common.TFJS_ATTR_INT_VALUE_KEY
if values is not None and ints in values and isinstance(values[ints], list):
v = values[ints]
if len(v) is not 4:
# must be NCHW-formatted 4D tensor or else TF can't handle it
raise ValueError(
"Unsupported 'dilations'-attribute in node {}".format(node[
common.TFJS_NAME_KEY]))
# check for [>1,>1,1,1], which is likely a mistranslated [1,>1,>1,1]
if int(v[0], 10) > 1:
values[ints] = ['1', v[0], v[1], '1']

return

def _convert_attr_values(message_dict):
"""
Node attributes in deserialised JSON contain strings as lists of ascii codes.
Expand All @@ -80,6 +99,7 @@ def _convert_attr_values(message_dict):
nodes = message_dict[common.TFJS_NODE_KEY]
for node in nodes:
_convert_string_attrs(node)
_fix_dilation_attrs(node)

return message_dict

Expand Down
28 changes: 8 additions & 20 deletions tfjs_graph_converter/common.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
# Copyright (c) 2019 Patrick Levin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# SPDX-License-Identifier: MIT
# Copyright © 2020 Patrick Levin
# ==============================================================================

# Keys in the model.json file
Expand All @@ -30,6 +13,7 @@
TFJS_ATTR_SHAPE_KEY = 'shape'
TFJS_ATTR_VALUE_KEY = 'value'
TFJS_ATTR_STRING_VALUE_KEY = 's'
TFJS_ATTR_INT_VALUE_KEY = 'i'

TFJS_NAME_KEY = 'name'
TFJS_DATA_KEY = 'data'
Expand All @@ -42,4 +26,8 @@
CLI_VERSION = 'version'
CLI_SAVED_MODEL = 'tf_saved_model'
CLI_FROZEN_MODEL = 'tf_frozen_model'
CLI_SILENT_MODE = 'silent'
CLI_SILENT_MODE = 'silent'
CLI_OPTIMIZATION = 'optimization'
CLI_OPTIMIZATION_NONE = 'none'
CLI_OPTIMIZATION_LITE = 'lite'
CLI_OPTIMIZATION_FULL = 'full'
27 changes: 14 additions & 13 deletions tfjs_graph_converter/converter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
# Copyright 2019 Patrick Levin. 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.
# SPDX-License-Identifier: MIT
# Copyright © 2020 Patrick Levin
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
Expand Down Expand Up @@ -81,6 +70,12 @@ def get_arg_parser():
return parser

def convert(arguments):
"""
Convert a TensorflowJS-model to a TensorFlow-model.
Args:
arguments: List of command-line arguments
"""
args = get_arg_parser().parse_args(arguments)
if args.show_version:
print("\ntfjs_graph_converter {}\n".format(version.VERSION))
Expand Down Expand Up @@ -132,6 +127,12 @@ def pip_main():
main([' '.join(sys.argv[1:])])

def main(argv):
"""
Entry point for debugging and running the script directly
Args:
argv: Command-line arguments as a single, space-separated string
"""
try:
convert(argv[0].split(' '))
except ValueError as ex:
Expand Down
2 changes: 1 addition & 1 deletion tfjs_graph_converter/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION = "0.1.0"
VERSION = "0.2.0"
__version__ = VERSION

0 comments on commit b6fcd32

Please sign in to comment.