Python 将Tensorflow冻结推理图加载到OpenCV DNN时出错

Python 将Tensorflow冻结推理图加载到OpenCV DNN时出错,python,tensorflow,opencv,google-colaboratory,roboflow,Python,Tensorflow,Opencv,Google Colaboratory,Roboflow,我已经使用Tensorflow API训练了一个对象检测模型,下面是一个基于Roboflow的Google Colaboratory笔记本的示例。 到目前为止,一切都很好,我已经成功地将经过训练的模型提取为推理图,同样遵循相同的笔记本: import re import numpy as np output_directory = './fine_tuned_model' lst = os.listdir(model_dir) lst = [l for l in lst if 'model

我已经使用Tensorflow API训练了一个对象检测模型,下面是一个基于Roboflow的Google Colaboratory笔记本的示例。

到目前为止,一切都很好,我已经成功地将经过训练的模型提取为推理图,同样遵循相同的笔记本:

import re
import numpy as np

output_directory = './fine_tuned_model'

lst = os.listdir(model_dir)
lst = [l for l in lst if 'model.ckpt-' in l and '.meta' in l]
steps=np.array([int(re.findall('\d+', l)[0]) for l in lst])
last_model = lst[steps.argmax()].replace('.meta', '')

last_model_path = os.path.join(model_dir, last_model)
print(last_model_path)
!python /content/models/research/object_detection/export_inference_graph.py \
    --input_type=image_tensor \
    --pipeline_config_path={pipeline_fname} \
    --output_directory={output_directory} \
    --trained_checkpoint_prefix={last_model_path}
这给了我一个
freezed\u expression\u graph.pb
文件,我可以用它在OpenCV DNN中制作我的目标检测程序。同样遵循这个示例,我准备了一个模型和管道配置的.pbtxt文件,作为
cv2.dnn.readNetFromTensorflow
函数的第二个参数。以下是足以重现我所犯错误的代码:

model = cv2.dnn.readNetFromTensorflow('models/trained/frozen_inference_graph.pb', 
                                      'models/trained/output.pbtxt')
当我使用经过预训练的SSD MobileNet V2 COCO模型时,该代码成功运行,
SSD\u MobileNet\u V2\u COCO\u 2018\u 03\u 29.pbtxt

但是,使用经过培训的.pbtxt文件,它将抛出以下错误:

C:\Users\Satria\Desktop\ExploreOpencvDnn-master>python trainedmodel_video.py -i test1.mp4 -o test1result.mp4
Traceback (most recent call last):                                                                                                                            
File "trainedmodel_video.py", line 48, in <module> 'models/trained/output.pbtxt') cv2.error:
OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:544:error:
(-2:Unspecified error) Input layer not found: FeatureExtractor/MobilenetV2/Conv/weights in function
'cv::dnn::dnn4_v20190621::`anonymous-namespace'::TFImporter::connect' 
这非常奇怪,因为我的电脑里根本没有这个目录。 我尝试检查我和示例SSD mobilenet模型的pbtxt和配置文件,但我找不到任何地方使用的特定目录的任何实例,甚至它们内部也没有目录路径

这是由于使用谷歌Colab进行培训造成的吗? 我可以在OpenCV DNN中使用Colab训练的Tensorflow模型吗


提前谢谢

在我自己生成的pbtxt文件中添加额外的输入节点后解决

有人建议我使用的OpenCV版本4.11已经过时了。 我更新到4.30,仍然不工作,但是现在它允许我使用FusedBatchNormav3,这在将来非常重要

现在,在仔细查看示例中的diffcheck和生成的pbtxt之后

在sample.pbtxt文件
ssd\u mobilenet\u v2\u coco\u 2018\u 03\u 29.pbtxt中,第30行之后

node {  
  name: "Preprocessor/mul"  
  op: "Mul" 
  input: "image_tensor" 
  input: "Preprocessor/mul/x"   
}   
node {  
  name: "Preprocessor/sub"  
  op: "Sub" 
  input: "Preprocessor/mul" 
  input: "Preprocessor/sub/y"   
}   
node {  
  name: "FeatureExtractor/MobilenetV2/Conv/Conv2D"  
  op: "Conv2D"  
  input: "Preprocessor/sub" 
  input: "FeatureExtractor/MobilenetV2/Conv/weights"    
它有一个额外的输入节点,使用
预处理器
,而不仅仅是
FeatureExtractor/MobilenetV2/Conv/Conv2D

同时,在生成的pbtxt上,它只有以下内容

node {
  name: "FeatureExtractor/MobilenetV2/Conv/Conv2D"
  op: "Conv2D"
  input: "FeatureExtractor/MobilenetV2/Conv/weights"
我将sample.pbtxt的输入节点复制到我自己生成的.pbtxt中,它成功了

node {
  name: "FeatureExtractor/MobilenetV2/Conv/Conv2D"
  op: "Conv2D"
  input: "FeatureExtractor/MobilenetV2/Conv/weights"