Repository: opencv
Issue: import Tensorflow 2.8 saved model via readNetFromTensorflow DNN api 
Labels: category: dnn


##### System information (version)
OS Platform: Windows 10 64 bits
Tensorflow Version: 2.8
Python version: 3.9.7
OpenCV version: 4.5.5

##### Detailed description

I have applied transfer learning to ssd_resnet50 TensorFlow 2 model on my dataset. I have followed the TensorFlow 2 object detection API tutorial 
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/2.2.0/
The training was done using the `model_main_tf2.py` script then we exported this model using the script `exporter_main_v2.py`
The exported model 'saved_model' was tested and worked well with the TF script. I am now trying to deploy my model; for this purpose, I want to read my trained network using OpenCV. However, SavedModel is not currently supported by OpenCV.
To overcome this issue, I tried to get frozen_graph.pb from my saved_model using the script below, found in  https://github.com/opencv/opencv/issues/16879
https://github.com/opencv/opencv/issues/16879#issuecomment-603815872
Trying to read the generated frozen model using dnn.readNet returns the following error
```
File "C:\Tensorflow\testFrozengraphTF2.py", line 14, in <module>
    output = net.forward()
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\dnn.cpp:621: error: (-2:Unspecified error) Can't create layer "StatefulPartitionedCall" of type "StatefulPartitionedCall" in function 'cv::dnn::dnn4_v20211220::LayerData::getLayerInstance'
```
Could someone help me convert TensorFlow 2 saved model to be used in OpenCV, please.
I really appreciate any help you can provide. 
##### Steps to reproduce

Using the code below, I generated the frozen_graph. The code can be retrieved from https://github.com/opencv/opencv/issues/16879#issuecomment-603815872.

```
import tensorflow as tf
print(tf.__version__)

from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

loaded = tf.saved_model.load('models/mnist_test')
infer = loaded.signatures['serving_default']

f = tf.function(infer).get_concrete_function(input_tensor=tf.TensorSpec(shape=[None, 640, 640, 3], dtype=tf.float32))
f2 = convert_variables_to_constants_v2(f)
graph_def = f2.graph.as_graph_def()

# Export frozen graph
with tf.io.gfile.GFile('frozen_graph.pb', 'wb') as f:
   f.write(graph_def.SerializeToString())
```

Then, I tried to generate the text graph representation (graph.pbtxt) using [tf_text_graph_ssd.py](https://github.com/opencv/opencv/blob/master/samples/dnn/tf_text_graph_ssd.py) found in https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API

`python tf_text_graph_ssd.py --input path2frozen_graph.pb --config path2pipeline.config --output path2graph.pbtxt`

The execution of this script returns the following error:

```
Traceback (most recent call last):
  File "C:\Tensorflow\generatepBtxtgraph\tf_text_graph_common.py", line 317, in writeTextGraph
    cv.dnn.writeTextGraph(modelPath, outputPath)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\tensorflow\tf_graph_simplifier.cpp:1052: error: (-215:Assertion failed) permIds.size() == net.node_size() in function 'cv::dnn::dnn4_v20211220::sortByExecutionOrder'
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Tensorflow\generatepBtxtgraph\tf_text_graph_ssd.py", line 413, in <module>
    createSSDGraph(args.input, args.config, args.output)
  File "C:\Tensorflow\generatepBtxtgraph\tf_text_graph_ssd.py", line 127, in createSSDGraph
    writeTextGraph(modelPath, outputPath, outNames)
  File "C:\Tensorflow\generatepBtxtgraph\tf_text_graph_common.py", line 320, in writeTextGraph
    from tensorflow.tools.graph_transforms import TransformGraph
ModuleNotFoundError: No module named 'tensorflow.tools.graph_transforms'
```

Trying to read the generated frozen model without graph.pb using dnn.readNet  the code below

```
import cv2
import numpy as np
 
image = cv2.imread("img002500.jpg")
if image is None:
	print("image emplty")
image_height, image_width, _ = image.shape
net = cv2.dnn.readNet('frozen_graph_centernet.pb')
image = image.astype(np.float32)
# create blob from image (opencv dnn way of pre-processing)
input_blob = cv2.dnn.blobFromImage(image, 1, (640,640), 0, swapRB=False, crop=False)
net.setInput(input_blob)
output = net.forward()
```
Give the error below. 
```
Traceback (most recent call last):
  File "C:\Tensorflow\testFrozengraphTF2.py", line 14, in <module>
    output = net.forward()
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\dnn.cpp:621: error: (-2:Unspecified error) Can't create layer "StatefulPartitionedCall" of type "StatefulPartitionedCall" in function 'cv::dnn::dnn4_v20211220::LayerData::getLayerInstance'
```
I understand that OpenCV doesn't import models with StatefulPartitionedCall (TF Eager mode). So the script found to export my saved model to frozen_graph did not work.

###my saved model
https://www.dropbox.com/s/liw5ff87rz7v5n5/my_model.zip?dl=0