Skip to main content
 首页 » 编程设计

python之运行 export_inference_graph.py 给出类型差异错误

2024年10月01日8itcoder

我正在尝试通过执行 export_inference_graph.py 脚本导出模型。

我尝试使用训练有素的 model.ckpt 和 ssd_mobilenet_v1_pets 的官方示例文件。

在 cmd 中输入:

python export_inference_graph.py \ --input_type image_tensor \ --pipeline_config_path training/ssd_mobilenet_v1_pets.config \ --trained_checkpoint_prefix training/model.ckpt-2453 \ --output_directory heart_graph 

我使用的是 TensorFlow 1.4,但我总是收到以下错误:

Traceback (most recent call last): 
  File "export_inference_graph.py", line 119, in <module> 
    tf.app.run() 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run 
    _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 
  File "export_inference_graph.py", line 115, in main 
    FLAGS.output_directory, input_shape) 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\Lib\site-packages\tensorflow\models\research\object_detection\exporter.py", line 427, in export_inference_graph 
    input_shape, optimize_graph, output_collection_name) 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\Lib\site-packages\tensorflow\models\research\object_detection\exporter.py", line 353, in _export_inference_graph 
    postprocessed_tensors = detection_model.postprocess(output_tensors) 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\Lib\site-packages\tensorflow\models\research\object_detection\meta_architectures\ssd_meta_arch.py", line 405, in postprocess 
    class_predictions_without_background) 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\Lib\site-packages\tensorflow\models\research\object_detection\builders\post_processing_builder.py", line 94, in score_converter_fn 
    scaled_logits = tf.divide(logits, logit_scale, name='scale_logits') 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 309, in divide 
    return DivideDelegateWithName(x, name) / y 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 294, in __truediv__ 
    return _truediv_python3(self.x, y, self.name) 
  File "C:\Users\<Name>\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 981, in _truediv_python3 
    (x_dtype, y_dtype)) 
TypeError: x and y must have the same dtype, got tf.float32 != tf.int32 

问题出在哪里,如何解决?

请您参考如下方法:

最近用object detection做了一些有趣的事情,也出现了这个错误,我在github上查了一下issues,找到了解决方案。

在https://github.com/tensorflow/models/issues/2774中,有改源码的方案,我试试。有用!!

您可以找到 post_processing_builder.py 并将函数更改为 def_score_converter_fn_with_logit_scale(tf_score_converter_fn, logit_scale): """创建一个函数来缩放 logits,然后应用一个 Tensorflow 函数。""" def score_converter_fn(logits): cr = logit_scale cr = tf.constant([[cr]],tf.float32) 打印(logit_scale) 打印(登录) scaled_logits = tf.divide(logits, cr, name='scale_logits') #改变 logit_scale 返回 tf_score_converter_fn(scaled_logits, name='convert_scores') score_converter_fn.__name__ = '%s_with_logit_scale' % ( tf_score_converter_fn.__name__) 返回 score_converter_fn

然后进入research文件夹,运行

python setup.py 安装

那就OK了!

顺便说一句,我不知道你是否应该重新安装research文件夹中的slim,你最好也重新安装它。