How to compile multi ouput keras models?


#1

Hey guys!!
I’m working on a keras model with multi ouput (3-outputs). I successfully compiled and load the model as in keras example tutorial.
But I’m only able to load only one output i.e.first output only to numpy empty array. Whereas I tried to load output shapes of the second output as:

out_shape_1 = [1,19,19,255]
out_1 = module.get_output(1, out=tvm.nd.empty(out_shape_!))

I’m getting the following tvm error:
TVMError: [18:57:28] /home/prnv/tvm/tvm/src/runtime/graph/graph_runtime.cc:121: Check failed: static_cast<size_t>(index) < outputs_.size() (1 vs. 1)

It seems like the output of the model was compiled to single output. How to solve this issue?

Thanks.


Wrong output in keras model
#2

I think multi output is not supported for the Keras frontend now.

Can you share your code so that we can reproduce the problem easily? I’ll try to implement a multi output support.


#3

@kazum thanks for your reply.
I’m trying to compile yolov3 keras model since there is no direct support for yolov3 in tvm.

import nnvm
import tvm
import keras
import numpy as np

weight_file = ‘yolo.h5’
keras_yolo = keras.models.load_model(weight_file)
data = np.zeros([1,3,416,416]).astype(np.float32)
sym, params = nnvm.frontend.from_keras(keras_yolo)
target = ‘opencl’
shape_dict = {‘input_1’: data.shape}
with nnvm.compiler.build_config(opt_level=2):
graph, lib, params = nnvm.compiler.build(sym, target, shape_dict, params=params)

#Inference
from tvm.contrib import graph_runtime
ctx = tvm.opencl()
ctx.device_type = 4
m = graph_runtime.create(graph, lib, ctx)

m.set_input(‘input_1’, tvm.nd.array(data.astype(‘float32’)))
m.set_input(**params)

m.run()
out_shape = [dim.value if dim.value is not None else 1 for dim in keras_yolo._output_layers[0].output.shape]
tvm_out_1 = m.get_output(0, tvm.nd.empty(out_shape, ‘float32’)).asnumpy()

out_shape_2 = [dim.value if dim.value is not None else 1 for dim in keras_yolo._output_layers[1].output.shape]
tvm_out_2 = m.get_output(1, tvm.nd.empty(out_shape_1, ‘float32’)).asnumpy()

out_shape_3 = [dim.value if dim.value is not None else 1 for dim in keras_yolo._output_layers[2].output.shape]
tvm_out_3 = m.get_output(2, tvm.nd.empty(out_shape_1, ‘float32’)).asnumpy()

I’m getting error at tvm_out_2/3


#4

Multiple output support for the Keras frontend has been merged.

Can you give it a try?


#5

Hey @kazum,
I tried with new commit, it is working well. But I’m getting wrong ouputs;(, may be because of different opt_value and build params on opencl.
Thank you so much!!


#6

does the tvm support multi outputs for tensforflow frontend now? thanks!