成功解决(wait)KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operatio

简介: 成功解决(wait)KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operatio

解决问题


KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."




全部代码

#run_inference_for_single_image(image, graph) #函数实现单张图像的推断

def run_inference_for_single_image(image, graph):

 with graph.as_default():

#     detection_graph = tf.Graph()

   with tf.Session() as sess:  #graph=detection_graph

     # 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'

     ]: #score每个检测结果的confidence、classes每个框所对应的类别、num_detections框检测的个数

       tensor_name = key + ':0'

       if tensor_name in all_tensor_names:

         tensor_dict[key] = detection_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]) #boxes变量存放了所有检测框

       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)

     print(tf.get_default_graph())  

     image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0') #image_tensor:0

     # Run inference 使用sess.run真正开始计算

     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

#6、for循环实现预测并绘制检测后的图片

for image_path in TEST_IMAGE_PATHS:

 image = Image.open(image_path)

 # the array based representation of the image will be used later in order to prepare the

 # result image with boxes and labels on it.

 image_np = load_image_into_numpy_array(image) #将图片草转换为numpy形式

 # Expand dimensions since the model expects images to have shape: [1, None, None, 3]

 image_np_expanded = np.expand_dims(image_np, axis=0) #将图片扩展一个维度,最后进入神经网络的格式应该为[1,?,?,3]

 # Actual detection.

 output_dict = run_inference_for_single_image(image_np, detection_graph)

 # Visualization of the results of a detection.

 vis_util.visualize_boxes_and_labels_on_image_array(

     image_np,

     output_dict['detection_boxes'],

     output_dict['detection_classes'],

     output_dict['detection_scores'],

     category_index,

     instance_masks=output_dict.get('detection_masks'),

     use_normalized_coordinates=True,

     line_thickness=8)

 plt.figure(figsize=IMAGE_SIZE)

 plt.title('TFOD API Official Case Tutorial——Jason Niu')

 plt.imshow(image_np)


相关文章
|
Ubuntu
ubuntu 替换清华源遇到的问题-不能更新,无法拉取 https 源解决
ubuntu 替换清华源遇到的问题-不能更新,无法拉取 https 源解决
6021 0
ubuntu 替换清华源遇到的问题-不能更新,无法拉取 https 源解决
|
Windows
mathtype7产品激活密钥最新
MathType是强大的数学公式编辑器,MathType公式编辑器可以说是专门为理科生准备的软件,它可以帮助用户快速的在各种文档中插入符号和公式,不论是简单的公式和符号,还是复杂的都可以非常轻松的输入,并且在与office文档结合使用时,表现的非常完美,是非常好的一款软件,与常见的文字处理软件和演示程序配合使用,能够在各种文档中加入复杂的数学公式和符号,可用在编辑数学试卷、书籍、报刊、论文、幻灯演示等方面,是编辑数学资料的得力工具。
52111 0
|
存储 数据采集 传感器
一文多图搞懂KITTI数据集下载及解析
一文多图搞懂KITTI数据集下载及解析
15973 3
一文多图搞懂KITTI数据集下载及解析
|
并行计算 PyTorch 算法框架/工具
【pytorch】解决pytorch:Torch not compiled with CUDA enabled
【pytorch】解决pytorch:Torch not compiled with CUDA enabled
9856 0
|
Ubuntu Linux 时序数据库
sudo apt-get update提示E: 仓库 “http://mirrors.aliyun.com/ubuntu eoan Release” 没有 Release 文件。亲试解决办法
将自己亲身解决这个办法进行分享,希望朋友们可以少走弯路。
10257 1
|
传感器 机器人 测试技术
ROS相机内参标定详细步骤指南
本文是关于ROS相机内参标定的详细步骤指南,包括了标定的目的、原理、所需材料、具体操作流程以及标定结果的分析。文章以Ubuntu20.04和ROS1 Noetic为测试环境,适用于单目RGB相机的内参标定,使用ros-noetic-camera-calibration工具包进行操作,并提供了标定过程中的注意事项和建议。
1567 1
ROS相机内参标定详细步骤指南
惊呆了、老铁。CSDN竟然有GitHub的加速功能????
这篇文章介绍了几种加速访问GitHub的方法,包括使用镜像网站、代理网站下载、利用CDN加速以及转入Gitee平台进行加速。作者建议,对于较大的项目推荐使用代理网站或Gitee下载,而对于较小的项目,使用CDN加速即可满足需求。
惊呆了、老铁。CSDN竟然有GitHub的加速功能????
|
数据可视化 IDE Linux
【Python篇】PyQt5 超详细教程——由入门到精通(序篇)
【Python篇】PyQt5 超详细教程——由入门到精通(序篇)
2584 3
|
算法 计算机视觉 Python
python利用opencv进行相机标定获取参数,并根据畸变参数修正图像附有全部代码(流畅无痛版)
该文章详细介绍了使用Python和OpenCV进行相机标定以获取畸变参数,并提供了修正图像畸变的全部代码,包括生成棋盘图、拍摄标定图像、标定过程和畸变矫正等步骤。
python利用opencv进行相机标定获取参数,并根据畸变参数修正图像附有全部代码(流畅无痛版)
|
Shell Linux 开发工具
Anaconda安装后报错 -bash: conda: command not found 如何处理
【6月更文挑战第26天】Anaconda安装后报错 -bash: conda: command not found 如何处理
4503 4