Tensorflow的对象分离api不工作。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tensorflow的对象分离api不工作。相关的知识,希望对你有一定的参考价值。
我一直在尝试使用tensorflow的对象检测api。经过一番周折,我已经安装了所有必要的模块。然后当我运行"对象检测_教程"从 模型研究对象_检测 我在检测部分出现了如下的错误。
检测
加载一个对象检测模型。
model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
detection_model = load_model(model_name)
tensorflow:Saver未被创建,因为图中没有变量可以恢复。
print(detection_model.inputs)
tf.Tensor 'image_tensor:0' shape=(?, ?, ?, 3) dtype=uint8。
我又在下面一行中得到了错误的信息。
for image_path in TEST_IMAGE_PATHS:
show_inference(detection_model, image_path)
错误: *在run_inference_for_ingle_image(model, image)中
* 在run_inference_for_single_image(model, image)中。
12 # Convert to numpy arrays, and take index [0] to remove the batch dimension.
13 # We're only interested in the first num_detections.
---> 14 num_detections = int(output_dict.pop('num_detections'))
15 output_dict = {key:value[0, :num_detections].numpy()
16 for key,value in output_dict.items()}
类型错误:int()参数必须是一个字符串、一个类似字节的对象或一个数字,而不是'Tensor'*。
请大家帮帮我,为什么我会出现这个错误。我是tensorflow新手,只想成功运行这个教程。我已经安装了 "Tensorflow 2.2.0 "和所有的模块。我也用Tensorflow的低版本做了同样的工作,但是同样的问题。
试试下面的代码。
import tensorflow as tf
import cv2
import numpy as np
import os
import random
from IPython import embed
IMAGE_PATH = "/Users/vedanshu/tfrecord/dataset"
input_names = ['image_tensor']
def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
font_scale=0.5, thickness=2):
size = cv2.getTextSize(label, font, font_scale, thickness)[0]
x, y = point
cv2.rectangle(image, (x, y - size[1]),
(x + size[0], y), (255, 0, 0), cv2.FILLED)
cv2.putText(image, label, point, font, font_scale,
(255, 255, 255), thickness)
detection_graph1 = tf.Graph()
with detection_graph1.as_default():
tf_sess1 = tf.Session(graph=detection_graph1)
model = tf.saved_model.loader.load(tf_sess1, ["serve"], "/Users/vedanshu/saved_model")
tf_input = tf_sess1.graph.get_tensor_by_name(input_names[0] + ':0')
tf_scores = tf_sess1.graph.get_tensor_by_name('detection_scores:0')
tf_boxes = tf_sess1.graph.get_tensor_by_name('detection_boxes:0')
tf_classes = tf_sess1.graph.get_tensor_by_name('detection_classes:0')
tf_num_detections = tf_sess1.graph.get_tensor_by_name('num_detections:0')
for img in os.listdir(IMAGE_PATH):
if img.endswith("jpg"):
image = os.path.join(IMAGE_PATH, img)
image = cv2.imread(image)
_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
scores, boxes, classes, num_detections = tf_sess1.run([tf_scores, tf_boxes, tf_classes, tf_num_detections], feed_dict={tf_input: _img[None, ...]})
boxes = boxes[0] # index by 0 to remove batch dimension
scores = scores[0]
classes = classes[0]
num_detections = int(num_detections[0])
# embed()
boxes_pixels = []
for i in range(num_detections):
# scale box to image coordinates
box = boxes[i] * np.array([image.shape[0], image.shape[1], image.shape[0], image.shape[1]])
box = np.round(box).astype(int)
boxes_pixels.append(box)
boxes_pixels = np.array(boxes_pixels)
for i in range(num_detections):
if scores[i] > 0.15:
box = boxes_pixels[i]
box = np.round(box).astype(int)
(startY, startX, endY, endX) = box.astype("int")
boxW = endX - startX
boxH = endY - startY
image = cv2.rectangle(image, (box[1], box[0]), (box[3], box[2]), (0, 255, 0), 2)
label = "{}:{:.2f}".format(int(classes[i]), scores[i])
draw_label(image, (box[1], box[0]), label)
cv2.imwrite("/Users/vedanshu/tfrecord/out/"+img, image)
你需要提供你的 saved_model.pb
和图像的位置。
以上是关于Tensorflow的对象分离api不工作。的主要内容,如果未能解决你的问题,请参考以下文章
如何从代码运行 tensorflow 对象检测 api (model_main_tf2)?
Spring MVC @Controller中转发或者重定向到其他页面的信息怎么携带和传递(Servlet API对象)HttpServletRequestHttpServletRespose(代码片