Error when building model from Keras


#1

Hi all,

I’d appriciate if someone can help me with the following problem:

I have a Keras model:

seq_size = 30
class_num = 2
lr = 0.001
input_shape = (seq_size, 2)
ip = Input(shape=input_shape)
x = LSTM(64, return_sequences=True, dropout=0.1)(ip)
x = LSTM(32, return_sequences=True, dropout=0.1)(x)
x = Flatten()(x)
x = Dropout(0.2)(x)
x = Dense(128, activation=‘relu’)(x)
x = Dense(class_num, activation=‘softmax’)(x)
mymodel = Model(inputs=ip, outputs=x)
mymodel.compile(loss=categorical_crossentropy, optimizer=Adam(lr=lr), metrics=[categorical_accuracy])
mymodel.load_weights(h5_file)

I load the weights from a trained model in the h5_file

I’m trying to compile a NNVM model as described in the tutorial:

import nnvm
sym, params = nnvm.frontend.from_keras(mymodel)
print(sym)
target = ‘cuda’
shape_dict = {‘input_1’: input_shape}
with nnvm.compiler.build_config(opt_level=3):
graph, lib, params = nnvm.compiler.build(sym, target, shape_dict, params=params)

However, I’m getting the following error:


NNVMError Traceback (most recent call last)
in
5 shape_dict = {‘input_1’: input_shape}
6 with nnvm.compiler.build_config(opt_level=3):
----> 7 graph, lib, params = nnvm.compiler.build(sym, target, shape_dict, params=params)

~/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/compiler/build_module.py in build(graph, target, shape, dtype, params, target_host, layout)
279 # Apply optimization
280 with target:
–> 281 graph = optimize(graph, shape, dtype, layout)
282
283 # Clear extra params without nodes.

~/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/compiler/build_module.py in optimize(graph, shape, dtype, layout)
178 if cfg.pass_enabled(“FoldScaleAxis”):
179 graph = graph_attr.set_shape_inputs(graph, shape)
–> 180 graph = graph.apply([“InferShape”, “FoldScaleAxis”])
181 return graph
182

~/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/graph.py in apply(self, passes)
232 ghandle = GraphHandle()
233 npass = nn_uint(len(passes))
–> 234 check_call(_LIB.NNGraphApplyPasses(self.handle, npass, cpass, ctypes.byref(ghandle)))
235 return Graph(ghandle)
236

~/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/_base.py in check_call(ret)
73 “”"
74 if ret != 0:
—> 75 raise NNVMError(py_str(_LIB.NNGetLastError()))
76
77 def c_str(string):

NNVMError: [12:22:45] /home/vasco/work/tvm/nnvm/src/compiler/fold_scale_axis.cc:91: Check failed: oshape.ndim() != 0 (0 vs. 0)

Stack trace returned 10 entries:
[bt] (0) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/site-packages/tvm-0.5.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(dmlc::StackTrace[abi:cxx11](unsigned long)+0x1f5) [0x7f7ef7e180f5]
[bt] (1) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/site-packages/tvm-0.5.dev0-py3.6-linux-x86_64.egg/tvm/libtvm.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x3e) [0x7f7ef7e18d7e]
[bt] (2) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/libnnvm_compiler.so(nnvm::compiler::DetectScaleAxis(nnvm::IndexedGraph const&, unsigned int, std::vector<nnvm::TShape, std::allocatornnvm::TShape > const&, std::vector<unsigned int, std::allocator > const&, bool, std::vector<nnvm::compiler::FoldChainEntry, std::allocatornnvm::compiler::FoldChainEntry >)+0x53a) [0x7f7ece9f5a6a]
[bt] (3) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/libnnvm_compiler.so(nnvm::compiler::FoldScaleAxis(nnvm::Graph)+0x5df) [0x7f7ece9f8f0f]
[bt] (4) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/libnnvm_compiler.so(std::_Function_handler<nnvm::Graph (nnvm::Graph), nnvm::Graph (
)(nnvm::Graph)>::_M_invoke(std::_Any_data const&, nnvm::Graph&&)+0x10e) [0x7f7ece997cee]
[bt] (5) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/libnnvm_compiler.so(nnvm::ApplyPasses(nnvm::Graph, std::vector<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::allocator<std::__cxx11::basic_string<char, std::char_traits, std::allocator > > > const&)+0x127d) [0x7f7ece97fe5d]
[bt] (6) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/site-packages/nnvm-0.8.0-py3.6.egg/nnvm/libnnvm_compiler.so(NNGraphApplyPasses+0x320) [0x7f7ece95dc80]
[bt] (7) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call_unix64+0x4c) [0x7f7f233b9dae]
[bt] (8) /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_call+0x22f) [0x7f7f233b971f]
[bt] (9) /home/vasco/work/tf/tensorflow1.12/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(_ctypes_callproc+0x2b4) [0x7f7f235cd524]

I am able to run the examples from the tutorials, including the “keras_s2s_translate.py”, but no luck with my model above.
Anyone has an idea how can I fix this?

Thanks in advance!


#2

Hey I ran into the same problem.

The stranger thing is that I know my models used to work in the older versions of TVM before I updated.

It’s hard to say exactly when it broke, but I forked on August 2, so it was some point after that.