I tried the following code and no assertions were raised. It looks like the outputs of Keras and TVM are at least close. If you get errors with my example, can you share your yolo.h5 file?
import keras
import numpy as np
import nnvm
import tvm
keras_model = keras.models.load_model('yolo.h5')
in_shapes = []
for layer in keras_model._input_layers:
in_shapes.append([1 if dim is None else dim for dim in layer.input_shape])
out_shapes = []
for layer in keras_model._output_layers:
out_shapes.append([1 if dim is None else dim for dim in layer.output_shape])
def get_tvm_output(xs):
def to_channels_last(shape):
return [shape[0]] + list(shape[2:]) + [shape[1]]
def to_channels_first(shape):
return [shape[0], shape[-1]] + list(shape[1:-1])
dtype='float32'
xs = [x.transpose(to_channels_first(range(x.ndim))) for x in xs]
sym, params = nnvm.frontend.from_keras(keras_model)
shape_dict = {name: x.shape for (name, x) in zip(keras_model.input_names, xs)}
graph, lib, params = nnvm.compiler.build(sym, "llvm", shape_dict, params=params)
m = tvm.contrib.graph_runtime.create(graph, lib, tvm.cpu())
for name, x in zip(keras_model.input_names, xs):
m.set_input(name, tvm.nd.array(x.astype(dtype)))
m.set_input(**params)
m.run()
tvm_out = []
for i, shape in enumerate(out_shapes):
out = m.get_output(i, tvm.nd.empty(to_channels_first(shape), dtype)).asnumpy()
out = out.transpose(to_channels_last(range(out.ndim)))
tvm_out.append(out)
return tvm_out
xs = [np.random.uniform(size=shape) for shape in in_shapes]
keras_out = keras_model.predict(xs)
tvm_out = get_tvm_output(xs)
for a, b in zip(keras_out, tvm_out):
np.testing.assert_allclose(a, b, rtol=1e-4, atol=1e-4)