title: Using Tensorflow 2.X model on OpenCV
body: <p>I have to use a Tensorflow 2.X model with the OpenCV framework (v.4.X with C++).</p>
  <p>To do this, I need a single <em>.pb</em> file or a <em>.pb</em> and a <em>.pbtxt</em> file, instead of a Tensorflow Saved Model like the one I have.</p>
  <p>So my question is: <strong>Is there a way to convert a Saved Model in a format that OpenCV could read?</strong> Like, maybe, a caffe model?</p>
  <p>I tried with <a href="https://github.com/microsoft/MMdnn" rel="nofollow noreferrer">MMdnn</a> but it gives me a strange error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/usr/local/bin/mmconvert&quot;, line 8, in &lt;module&gt;
      sys.exit(_main())
    File &quot;/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/convert.py&quot;, line 102, in _main
      ret = convertToIR._convert(ir_args)
    File &quot;/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/convertToIR.py&quot;, line 62, in _convert
      from mmdnn.conversion.tensorflow.tensorflow_parser import TensorflowParser
    File &quot;/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/tensorflow/tensorflow_parser.py&quot;, line 15, in &lt;module&gt;
      from tensorflow.tools.graph_transforms import TransformGraph
  ImportError: No module named 'tensorflow.tools.graph_transforms'
  </code></pre>
  <p>And I suppose it is because it was developed and tested with Tensorflow 1.X.</p>
  <hr />
  <p>Edit: I also have the relative Keras model (now that it is integrated with Tensorflow 2), but it is incompatible with OpenCV DNN framework too. Trying converting it with MMdnn I get this error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/usr/local/bin/mmconvert&quot;, line 8, in &lt;module&gt;
      sys.exit(_main())
    File &quot;/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/convert.py&quot;, line 102, in _main
      ret = convertToIR._convert(ir_args)
    File &quot;/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/_script/convertToIR.py&quot;, line 46, in _convert
      parser = Keras2Parser(model)
    File &quot;/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/keras/keras2_parser.py&quot;, line 126, in __init__
      model = self._load_model(model[0], model[1])
    File &quot;/usr/local/lib/python3.5/dist-packages/mmdnn/conversion/keras/keras2_parser.py&quot;, line 78, in _load_model
      'DepthwiseConv2D': layers.DepthwiseConv2D})
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py&quot;, line 664, in model_from_json
      return deserialize(config, custom_objects=custom_objects)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py&quot;, line 168, in deserialize
      printable_module_name='layer')
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py&quot;, line 147, in deserialize_keras_object
      list(custom_objects.items())))
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/engine/network.py&quot;, line 1056, in from_config
      process_layer(layer_data)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/engine/network.py&quot;, line 1042, in process_layer
      custom_objects=custom_objects)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py&quot;, line 168, in deserialize
      printable_module_name='layer')
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py&quot;, line 149, in deserialize_keras_object
      return cls.from_config(config['config'])
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py&quot;, line 1179, in from_config
      return cls(**config)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py&quot;, line 91, in wrapper
      return func(*args, **kwargs)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/layers/convolutional.py&quot;, line 484, in __init__
      **kwargs)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/layers/convolutional.py&quot;, line 117, in __init__
      self.kernel_initializer = initializers.get(kernel_initializer)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/initializers.py&quot;, line 515, in get
      return deserialize(identifier)
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/initializers.py&quot;, line 510, in deserialize
      printable_module_name='initializer')
    File &quot;/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py&quot;, line 140, in deserialize_keras_object
      ': ' + class_name)
  ValueError: Unknown initializer: GlorotUniform
  </code></pre>
  <hr />
  <p>Edit 04/2021: Now the ONNX converter mentioned in the comments works properly with OpenCV 4.5.1 (Version 4.5.0 has a bug with some ONNX networks).</p>