Skip to content

Commit 9db8f0d

Browse files
committed
Fixed critical converter bug (non-unique scope name).
1 parent 6540b61 commit 9db8f0d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

pytorch2keras/converter.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def _optimize_trace(trace, aten):
5050
torch._C._jit_pass_lint(trace)
5151

5252

53+
def get_node_id(node):
54+
import re
55+
node_id = re.search(r"[\d]+", node.__str__())[0]
56+
return node_id
57+
58+
5359
def pytorch_to_keras(
5460
model, args, input_shape,
5561
change_ordering=False, training=False, verbose=False
@@ -84,6 +90,9 @@ def pytorch_to_keras(
8490

8591
_optimize_trace(trace, False)
8692

93+
if verbose:
94+
print(trace.graph())
95+
8796
if verbose:
8897
print(list(trace.graph().outputs()))
8998

@@ -115,14 +124,16 @@ def pytorch_to_keras(
115124
node_input_names = []
116125
for node_input in node_inputs:
117126
if node_input.node().scopeName():
118-
node_input_names.append(node_input.node().scopeName())
127+
node_input_names.append(get_node_id(node_input.node()))
119128

120129
if len(node_input_names) == 0:
121130
node_input_names.append('input')
122131

123132
node_type = node.kind()
133+
# print(dir(node))
134+
124135
node_scope_name = node.scopeName()
125-
node_id = re.search(r"[\d]+", node.__str__())[0]
136+
node_id = get_node_id(node)
126137
node_weights_name = '.'.join(
127138
re.findall(r'\[([\w\d.]+)\]', node_scope_name)
128139
)
@@ -145,12 +156,12 @@ def pytorch_to_keras(
145156
print('is_terminal:', node_id in graph_outputs)
146157
AVAILABLE_CONVERTERS[node_type](
147158
node_attrs,
148-
node_weights_name, node_scope_name,
159+
node_weights_name, node_id,
149160
node_input_names,
150161
layers, state_dict
151162
)
152163
if node_id in graph_outputs:
153-
outputs.append(layers[node_scope_name])
164+
outputs.append(layers[node_id])
154165

155166
model = keras.models.Model(inputs=layers['input'], outputs=outputs)
156167

0 commit comments

Comments
 (0)