code: def verify_keras_frontend(
      keras_model, need_transpose=True, layout="NCHW"
  ):
      """Generic function to generate and compare Keras and TVM output"""
      # Keras frontend currently supports tensorflow backend only.
      assert keras.backend.backend() == "tensorflow"
  
      if layout != "NCHW":
          need_transpose = False
  
      in_shapes = []
      for layer in keras_model._input_layers:
          if tf.executing_eagerly():
              in_shapes.append(
                  tuple(
                      dim if dim is not None else 1
                      for dim in layer.input.shape
                  )
              )
          else:
              in_shapes.append(
                  tuple(
                      dim.value if dim.value is not None else 1
                      for dim in layer.input.shape
                  )
              )
  
      def get_keras_output(in_data):
          return keras_model.predict(in_data)
  
      def get_tvm_output(in_data, target, dev, dtype="float32"):
          shape_dict = {
              name: x.shape
              for (name, x) in zip(keras_model.input_names, in_data)
          }
          mod, params = relay.frontend.from_keras(
              keras_model, shape_dict, layout=layout
          )
          with tvm.transform.PassContext(opt_level=2):
              lib = relay.build(mod, target, params=params)
          m = graph_executor.GraphModule(lib["default"](dev))
          for name, x in zip(keras_model.input_names, in_data):
              m.set_input(name, tvm.nd.array(x.astype(dtype)))
          m.run()
          return [
              m.get_output(i).numpy()
              for i in range(m.get_num_outputs())
          ]
  
      def to_channels_first(arr):
          return arr.transpose([0, -1] + list(range(1, arr.ndim - 1)))
  
      def to_channels_last(arr):
          return arr.transpose([0] + list(range(2, arr.ndim)) + [1])
  
      in_data = [
          np.random.uniform(size=shape, low=-1.0, high=1.0)
          for shape in in_shapes
      ]
      keras_out = get_keras_output(in_data)
      keras_out = (
          keras_out if isinstance(keras_out, list) else [keras_out]
      )
      for target, dev in tvm.testing.enabled_targets():
          inputs = (
              [to_channels_first(x) for x in in_data]
              if need_transpose
              else in_data
          )
          tvm_out = get_tvm_output(inputs, target, dev)
          for kout, tout in zip(keras_out, tvm_out):
              if need_transpose:
                  tout = to_channels_last(tout)
              tvm.testing.assert_allclose(
                  kout, tout, rtol=1e-5, atol=1e-5
              )
  
imports: from packaging import version
  import numpy
  from tensorflow import keras
  import keras
  import pytest
  import tvm
  from tvm import relay
  from tvm.contrib import graph_executor
  import tvm.testing
label_desc: Use image node_id 2 for calling a global function with example usage: verify_keras_frontend(keras_model, need_transpose, layout) and returns: keras_model, unknown, arr, arr