TensorFlow Object Detection API版本适配困惑及TF1.15运行报错求助
解答你的TensorFlow Object Detection API版本与推理问题
Hey there! Let's work through your confusion and errors one by one:
一、关于TensorFlow版本冲突的困惑
其实这个问题是因为Object Detection API的版本迭代导致的文档滞后:
- 早期的API版本确实只支持TensorFlow 1.x,但现在官方已经完成了TF2.x的适配,并且当前官方主推的就是TF2.x版本。
- 你看到的“暂不支持TF2.x”的页面应该是旧文档,而Demo要求安装TF2.x是因为它基于最新的TF2分支API。建议你直接使用TensorFlow 2.x版本搭配最新的Object Detection API,这样版本匹配,后续也能获得更好的支持。
二、TF1.15运行Demo时的TypeError错误
从你给出的模型调用输出可以看到,所有返回的结果都是Tensor对象(比如num_detections是<tf.Tensor 'StatefulPartitionedCall:4' shape=(?,) dtype=float32>),而不是实际的数值。这是因为:
- 在TensorFlow 1.x中,默认是图执行模式,Tensor只是计算图中的节点,需要通过会话(Session)来运行才能得到具体数值;而你可能用了TF2风格的模型加载/调用方式,导致返回的是未计算的Tensor。
修复方法(针对TF1.15)
修改你的run_inference_for_single_image函数,添加会话运行的逻辑:
def run_inference_for_single_image(image, graph): with graph.as_default(): with tf.Session() as sess: # Get handles to input and output tensors ops = tf.get_default_graph().get_operations() all_tensor_names = {output.name for op in ops for output in op.outputs} tensor_dict = {} for key in [ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks' ]: tensor_name = key + ':0' if tensor_name in all_tensor_names: tensor_dict[key] = tf.get_default_graph().get_tensor_by_name( tensor_name) if 'detection_masks' in tensor_dict: # The following processing is only for single image detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0]) detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0]) # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size. real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32) detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1]) detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks( detection_masks, detection_boxes, image.shape[0], image.shape[1]) detection_masks_reframed = tf.cast( tf.greater(detection_masks_reframed, 0.5), tf.uint8) # Follow the convention by adding back the batch dimension tensor_dict['detection_masks'] = tf.expand_dims( detection_masks_reframed, 0) image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0') # Run inference output_dict = sess.run(tensor_dict, feed_dict={image_tensor: np.expand_dims(image, 0)}) # All outputs are float32 numpy arrays, so convert types as appropriate output_dict['num_detections'] = int(output_dict['num_detections'][0]) output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.uint8) output_dict['detection_boxes'] = output_dict['detection_boxes'][0] output_dict['detection_scores'] = output_dict['detection_scores'][0] if 'detection_masks' in output_dict: output_dict['detection_masks'] = output_dict['detection_masks'][0] return output_dict
核心是用sess.run()来执行Tensor计算,得到实际的数值数组,再从中提取需要的结果。
更推荐的方案
直接切换到TensorFlow 2.x版本,使用官方最新的Object Detection API。TF2默认开启 eager execution,不需要手动管理会话,推理代码会更简洁,也能和Demo的要求匹配,避免版本不兼容的问题。
内容的提问来源于stack exchange,提问作者Aakash
相关产品推荐
相关产品推荐

