Compile Keras Model issue report and solution included

Hi, I’m new to tvm and I have been trying to compile some deep learning models with tvm. I tried to compile Keras model on the tutorial page, and I ran into some issues. Given that there is few reports on this specific issue, I’m posting the issues that I ran into and how I fixed them.

My laptop and python package settings are as follows:
macOS 10.13.4
Keras 2.2.0
Theano 1.0.2

First error:
AttributeError: module ‘keras.engine’ has no attribute ‘topology’

I modified line 477 and 495 at /path/to/tvm/nnvm/python/nnvm/frontend/keras.py into:
if isinstance(keras_layer, keras.engine.input_layer.InputLayer):
if isinstance(pred, keras.engine.input_layer.InputLayer):

I’m not sure why keras.engine doesn’t recognize topology.py, but InputLayer is defined in input_layer.py.

Second error:
Traceback (most recent call last):
File “nnvm_quick_test.py”, line 44, in
graph, params = nnvm.frontend.from_keras(keras_resnet50)
File “/Users/luzijie/tvm/nnvm/python/nnvm/frontend/keras.py”, line 505, in from_keras
outsym = symtab.get_var(model.output_layers[0].name + ‘:0’)
AttributeError: ‘Model’ object has no attribute ‘output_layers’

I changed the corresponding line into
outsym = symtab.get_var(model.layers[-1].name + ‘:0’)

layers[-1] corresponds to the last layer of a keras model.

Hope this helps :slight_smile:

IIUC, the current version of TVM only supports Keras with the Tensorflow backend. We need more effort to use the Theano backend.
For example, we have to convert kernel weights in conv2d(), I think.
https://github.com/keras-team/keras/wiki/Converting-convolution-kernels-from-Theano-to-TensorFlow-and-vice-versa