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

Fixed to work with keras2onnx-made models #76

Open
wants to merge 1 commit 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
26 changes: 25 additions & 1 deletion onnx2keras/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ def onnx_to_keras(onnx_model, input_names,

logger.debug('Gathering weights to dictionary.')
weights = {}

for onnx_w in onnx_weights:
# Simpler solution. Seems to work with all keras2onnx-made models (July 28 2020)
# Unsure if it works with others. See below alternate solution
onnx_extracted_weights_name = onnx_w.ListFields()[-1][1]
weights[onnx_extracted_weights_name] = numpy_helper.to_array(onnx_w)
# Previous code, does not work for models from keras2onnx as of July 28 2020, and likely earlier
'''
try:
if len(onnx_w.ListFields()) < 4:
onnx_extracted_weights_name = onnx_w.ListFields()[1][1]
Expand All @@ -86,7 +93,24 @@ def onnx_to_keras(onnx_model, input_names,
except:
onnx_extracted_weights_name = onnx_w.ListFields()[3][1]
weights[onnx_extracted_weights_name] = numpy_helper.to_array(onnx_w)

'''
# Alternate (ugly) solution, based on previous code
'''
try:
if len(onnx_w.ListFields()) < 4:
onnx_extracted_weights_name = onnx_w.ListFields()[1][1]
else:
onnx_extracted_weights_name = onnx_w.ListFields()[2][1]
weights[onnx_extracted_weights_name] = numpy_helper.to_array(onnx_w)
except:
# Ugly fix
try:
onnx_extracted_weights_name = onnx_w.ListFields()[3][1]
weights[onnx_extracted_weights_name] = numpy_helper.to_array(onnx_w)
except:
onnx_extracted_weights_name = onnx_w.ListFields()[-1][1]
weights[onnx_extracted_weights_name] = numpy_helper.to_array(onnx_w)
'''
logger.debug('Found weight {0} with shape {1}.'.format(
onnx_extracted_weights_name,
weights[onnx_extracted_weights_name].shape))
Expand Down
18 changes: 13 additions & 5 deletions onnx2keras/operation_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ def convert_clip(node, params, layers, lambda_func, node_name, keras_name):
assert AttributeError('More than 1 input for clip layer.')

input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)

if params['min'] == 0:
logger.debug("Using ReLU({0}) instead of clip".format(params['max']))
layer = keras.layers.ReLU(max_value=params['max'], name=keras_name)
clip_min = None
clip_max = None
for key in layers.keys():
if 'clip_min' in key:
clip_min = layers[key]
elif 'clip_max' in key:
clip_max = layers[key]
if clip_min == 0:
logger.debug("Using ReLU({0}) instead of clip".format(clip_max))
layer = keras.layers.ReLU(max_value=clip_max)
# previous code, caused scope name errors for keras2onnx-made models
#layer = keras.layers.ReLU(max_value=clip_max, name=keras_name)
else:
def target_layer(x, vmin=params['min'], vmax=params['max']):
def target_layer(x, vmin=clip_min, vmax=clip_max):
import tensorflow as tf
return tf.clip_by_value(x, vmin, vmax)
layer = keras.layers.Lambda(target_layer, name=keras_name)
Expand Down