BackendIsNotSupposedToImplementIt Error: Converting ONNX to Tensorflow
Published: 2023-04-13 02:23:23

When i run this code to convert onnx to tensorflow im getting error in google colab. I need to convert this onnx file to tensorflow lite so i can use it in android app.

`from onnx_tf.backend import prepare
import onnx
onnx_model_path = 'onnx_example.onnx'
tf_model_path = 'model_tf'
onnx_model = onnx.load(onnx_model_path)
tf_rep = prepare(onnx_model)
tf_rep.export_graph(tf_model_path)`

Getting below error

```

--------------------------------------------------------------------------- BackendIsNotSupposedToImplementIt Traceback (most recent call last) in <cell line: 9>() 7 onnx_model = onnx.load(onnx_model_path) 8 tf_rep = prepare(onnx_model) ----> 9 tf_rep.export_graph(tf_model_path)

31 frames /usr/local/lib/python3.9/dist-packages/onnx_tf/backend_rep.py in export_graph(self, path) 141 self.tf_module, 142 path, --> 143 signatures=self.tf_module.call.get_concrete_function( 144 **self.signatures)) 145 self.tf_module.is_export = False

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in get_concrete_function(self, *args, **kwargs) 1256 def get_concrete_function(self, *args, **kwargs): 1257 # Implements GenericFunction.get_concrete_function. -> 1258 concrete = self._get_concrete_function_garbage_collected(*args, **kwargs) 1259 concrete._garbage_collector.release() # pylint: disable=protected-access 1260 return concrete

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in _get_concrete_function_garbage_collected(self, *args, **kwargs)
1236 if self._variable_creation_fn is None: 1237
initializers = [] -> 1238 self._initialize(args, kwargs, add_initializers_to=initializers) 1239
self._initialize_uninitialized_variables(initializers) 1240

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in _initialize(self, args, kwds, add_initializers_to) 761 self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph) 762 self._concrete_variable_creation_fn = ( --> 763 self._variable_creation_fn # pylint: disable=protected-access 764 ._get_concrete_function_internal_garbage_collected( 765 *args, **kwds))

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs) 169 """Returns a concrete function which cleans up its graph function.""" 170 with self._lock: --> 171 concrete_function, _ = self._maybe_define_concrete_function(args, kwargs) 172 return concrete_function 173

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py in _maybe_define_concrete_function(self, args, kwargs) 164 kwargs = {} 165 --> 166 return self._maybe_define_function(args, kwargs) 167 168 def _get_concrete_function_internal_garbage_collected(self, *args, **kwargs):

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py in _maybe_define_function(self, args, kwargs) 394 kwargs = placeholder_bound_args.kwargs 395 --> 396 concrete_function = self._create_concrete_function( 397 args, kwargs, func_graph) 398

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py in _create_concrete_function(self, args, kwargs, func_graph) 298 299 concrete_function = monomorphic_function.ConcreteFunction( --> 300 func_graph_module.func_graph_from_py_func( 301 self._name, 302 self._python_function,

/usr/local/lib/python3.9/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, create_placeholders, acd_record_initial_resource_uses) 1212
_, original_func = tf_decorator.unwrap(python_func) 1213 -> 1214 func_outputs = python_func(*func_args, **func_kwargs) 1215 1216 # invariant: func_outputs contains only Tensors, CompositeTensors,

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in wrapped_fn(*args, **kwds) 665 # the function a weak reference to itself to avoid a reference cycle. 666 with OptionalXlaContext(compile_with_xla): --> 667 out = weak_wrapped_fn().wrapped(*args, **kwds) 668 return out 669

/usr/local/lib/python3.9/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py in bound_method_wrapper(*args, **kwargs) 482 # However, the replacer is still responsible for attaching self properly. 483 # TODO(mdan): Is it possible to do it here instead? --> 484 return wrapped_fn(*args, **kwargs) 485 486 weak_bound_method_wrapper = weakref.ref(bound_method_wrapper)

/usr/local/lib/python3.9/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs) 1198 except Exception as e: # pylint:disable=broad-except 1199 if hasattr(e, "ag_error_metadata"): -> 1200 raise e.ag_error_metadata.to_exception(e) 1201 else: 1202 raise

/usr/local/lib/python3.9/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs) 1187 # TODO(mdan): Push this block higher in tf.function's call stack. 1188
try: -> 1189 return autograph.converted_call( 1190 original_func, 1191 args,

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options) 437 try: 438 if kwargs is not None: --> 439 result = converted_f(*effective_args, **kwargs) 440 else: 441 result = converted_f(*effective_args)

/usr/local/lib/python3.9/dist-packages/onnx_tf/backend_tf_module.py in tf____call__(self, **kwargs) 28 output_ops = ag__.Undefined('output_ops') 29 onnx_node = ag__.Undefined('onnx_node') ---> 30 ag__.for_stmt(ag__.ld(self).graph_def.node, None, loop_body, get_state, set_state, (), {'iterate_names': 'node'}) 31 outputs = ag__.converted_call(ag__.ld(dict), (), None, fscope) 32

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in for_stmt(iter_, extra_test, body, get_state, set_state, symbol_names, opts) 449 for_fn = tf_distributed_iterable_for_stmt 450 --> 451 for_fn(iter, extra_test, body, get_state, set_state, symbol_names, opts) 452 453

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in py_for_stmt(failed resolving arguments) 500 else: 501 for target in iter: --> 502 body(target) 503 504

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in protected_body(protected_iter) 466 original_body = body 467 def protected_body(protected_iter): --> 468 original_body(protected_iter) 469 after_iteration() 470 before_iteration()

/usr/local/lib/python3.9/dist-packages/onnx_tf/backend_tf_module.py in loop_body(itr) 21 node = itr 22 onnx_node = ag__.converted_call(ag__.ld(OnnxNode), (ag__.ld(node),), None, fscope) ---> 23 output_ops = ag__.converted_call(ag__.ld(self).backend.onnx_node_to_tensorflow_op, (ag_.ld(onnx_node), ag__.ld(tensor_dict), ag__.ld(self).handlers), dict(opset=ag__.ld(self).opset, strict=ag__.ld(self).strict), fscope) 24 curr_node_output_map = ag__.converted_call(ag__.ld(dict), (ag__.converted_call(ag__.ld(zip), (ag__.ld(onnx_node).outputs, ag__.ld(output_ops)), None, fscope),), None, fscope) 25 ag__.converted_call(ag__.ld(tensor_dict).update, (ag__.ld(curr_node_output_map),), None, fscope)

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options) 437 try: 438 if kwargs is not None: --> 439 result = converted_f(*effective_args, **kwargs) 440 else: 441 result = converted_f(*effective_args)

/usr/local/lib/python3.9/dist-packages/onnx_tf/backend.py in tf___onnx_node_to_tensorflow_op(cls, node, tensor_dict, handlers, opset, strict) 60 pass 61 handler = ag__.Undefined('handler') ---> 62 ag__.if_stmt(ag__.ld(handlers), if_body_1, else_body_1, get_state_1, set_state_1, ('do_return', 'retval_'), 2) 63 64 def get_state_2():

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in if_stmt(cond, body, orelse, get_state, set_state, symbol_names, nouts) 1264 _tf_if_stmt(cond, body, orelse, get_state, set_state, symbol_names, nouts) 1265 else: -> 1266 _py_if_stmt(cond, body, orelse) 1267 1268

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in _py_if_stmt(cond, body, orelse) 1317 def _py_if_stmt(cond, body, orelse): 1318 """Overload of if_stmt that executes a Python if statement.""" -> 1319 return body() if cond else orelse()

/usr/local/lib/python3.9/dist-packages/onnx_tf/backend.py in if_body_1() 54 nonlocal retval_, do_return 55 pass ---> 56 ag__.if_stmt(ag__.ld(handler), if_body, else_body, get_state, set_state, ('do_return', 'retval_'), 2) 57 58 def else_body_1():

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in if_stmt(cond, body, orelse, get_state, set_state, symbol_names, nouts) 1264 _tf_if_stmt(cond, body, orelse, get_state, set_state, symbol_names, nouts) 1265 else: -> 1266 _py_if_stmt(cond, body, orelse) 1267 1268

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in _py_if_stmt(cond, body, orelse) 1317 def _py_if_stmt(cond, body, orelse): 1318 """Overload of if_stmt that executes a Python if statement.""" -> 1319 return body() if cond else orelse()

/usr/local/lib/python3.9/dist-packages/onnx_tf/backend.py in if_body() 46 try: 47 do_return = True ---> 48 retval_ = ag__.converted_call(ag__.ld(handler).handle, (ag__.ld(node),), dict(tensor_dict=ag__.ld(tensor_dict), strict=ag__.ld(strict)), fscope) 49 except: 50 do_return = False

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options) 437 try: 438 if kwargs is not None: --> 439 result = converted_f(*effective_args, **kwargs) 440 else: 441 result = converted_f(*effective_args)

/usr/local/lib/python3.9/dist-packages/onnx_tf/handlers/handler.py in tf__handle(cls, node, **kwargs) 39 nonlocal retval_, do_return 40 raise ag__.converted_call(ag__.ld(BackendIsNotSupposedToImplementIt), (ag__.converted_call('{} version {} is not implemented.'.format, (ag__.ld(node).op_type, ag__.ld(cls).SINCE_VERSION), None, fscope),), None, fscope) ---> 41 ag__.if_stmt(ag__.ld(ver_handle), if_body, else_body, get_state, set_state, ('do_return', 'retval_'), 2) 42 return fscope.ret(retval_, do_return) 43 return tf__handle

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in if_stmt(cond, body, orelse, get_state, set_state, symbol_names, nouts) 1264 _tf_if_stmt(cond, body, orelse, get_state, set_state, symbol_names, nouts) 1265 else: -> 1266 _py_if_stmt(cond, body, orelse) 1267 1268

/usr/local/lib/python3.9/dist-packages/tensorflow/python/autograph/operators/control_flow.py in _py_if_stmt(cond, body, orelse) 1317 def _py_if_stmt(cond, body, orelse): 1318 """Overload of if_stmt that executes a Python if statement.""" -> 1319 return body() if cond else orelse()

/usr/local/lib/python3.9/dist-packages/onnx_tf/handlers/handler.py in else_body() 38 def else_body(): 39 nonlocal retval_, do_return ---> 40 raise ag__.converted_call(ag__.ld(BackendIsNotSupposedToImplementIt), (ag__.converted_call('{} version {} is not implemented.'.format, (ag__.ld(node).op_type, ag__.ld(cls).SINCE_VERSION), None, fscope),), None, fscope) 41 ag__.if_stmt(ag__.ld(ver_handle), if_body, else_body, get_state, set_state, ('do_return', 'retval_'), 2) 42 return fscope.ret(retval_, do_return)

BackendIsNotSupposedToImplementIt: in user code:

File "/usr/local/lib/python3.9/dist-packages/onnx_tf/backend_tf_module.py",
line 99, in call * output_ops = self.backend._onnx_node_to_tensorflow_op(onnx_node, File "/usr/local/lib/python3.9/dist-packages/onnx_tf/backend.py", line 347, in _onnx_node_to_tensorflow_op * return handler.handle(node, tensor_dict=tensor_dict, strict=strict) File "/usr/local/lib/python3.9/dist-packages/onnx_tf/handlers/handler.py", line 61, in handle * raise BackendIsNotSupposedToImplementIt("{} version {} is not implemented.".format(node.op_type, cls.SINCE_VERSION))

BackendIsNotSupposedToImplementIt: Unsqueeze version 13 is not implemented.
```
I tried to see the opset version like below

`from onnx.defs import onnx_opset_version
onnx_opset_version()`
It output

```
18
```

Other things for your reference:

`import onnx

# Load the ONNX model
model = onnx.load("onnx_example.onnx")

# Check that the IR is well formed
onnx.checker.check_model(model)

# Print a Human readable representation of the graph
onnx.helper.printable_graph(model.graph)`

```

graph torch_jit (\n %image_embeddings[FLOAT, 1x256x64x64]\n %point_coords[FLOAT, 1xnum_pointsx2]\n %point_labels[FLOAT, 1xnum_points]\n %mask_input[FLOAT, 1x1x256x256]\n %has_mask_input[FLOAT, 1]\n %orig_im_size[FLOAT, 2]\n) initializers (\n %mask_decoder.transformer.layers.0.self_attn.q_proj.bias[FLOAT, 256]\n %mask_decoder.transformer.layers.0.self_attn.k_proj.bias[FLOAT, 256]\n %mask_decoder.transformer.layers.0.self_attn.v_proj.bias[FLOAT, 256]\n %mask_decoder.transformer.layers.0.self_attn.out_proj.bias[FLOAT, 256]\n %mask_decoder.transformer.layers.0.norm1.weight[FLOAT, 256]\n %mask_decoder.transformer.layers.0.norm1.bias[FLOAT, 256]\n %mask_decoder.transformer.layers.0.cross_attn_token_to_image.q_proj.bias[FLOAT, 128]\n %mask_decoder.transformer.layers.0.cross_attn_token_to_image.k_proj.bias[FLOAT, 128]\n %mask_decoder.transformer.layers.0.cross_attn_token_to_image.v_proj.bias[FLOAT, 128]\n %mask_decoder.transformer.layers.0.cross_attn_token_to_image.out_proj.bia...

```
How to solve this? I found this github issue which they solved, but im not to able to find the solution https://github.com/onnx/onnx-tensorflow/pull/1022




Reference:
https://stackoverflow.com/questions/75969364/backendisnotsupposedtoimplementit-error-converting-onnx-to-tensorflow