Python 例如,tensorflow对象检测失败
Python 例如,tensorflow对象检测失败,python,tensorflow,object-detection,Python,Tensorflow,Object Detection,系统信息 您正在使用的模型的顶级目录是什么:tensorflow/models 我是否编写了自定义代码(而不是使用TensorFlow中提供的股票示例脚本):否 操作系统平台和发行版(如Linux Ubuntu 16.04):Windows 10 x64 TensorFlow安装自(源代码或二进制文件):二进制文件 TensorFlow版本(使用下面的命令):1.3.0 Bazel版本(如果从源代码处编译): CUDA/cuDNN版本: GPU型号和内存: 复制的精确命令:python对象检测
系统信息
- 您正在使用的模型的顶级目录是什么:tensorflow/models
- 我是否编写了自定义代码(而不是使用TensorFlow中提供的股票示例脚本):否
- 操作系统平台和发行版(如Linux Ubuntu 16.04):Windows 10 x64
- TensorFlow安装自(源代码或二进制文件):二进制文件
- TensorFlow版本(使用下面的命令):1.3.0
- Bazel版本(如果从源代码处编译):
- CUDA/cuDNN版本:
- GPU型号和内存:
- 复制的精确命令:python对象检测\eval.py--logtostderr--checkpoint\u dir=train--eval\u dir=eval--pipeline\u config\u path=ssd\u mobilenet\u v1.config & python.py
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tensorflow as tf
import zipfile
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
PATH_TO_CKPT = os.path.join('inference_graphs', 'frozen_inference_graph.pb')
PATH_TO_LABELS = 'pet_label_map.pbtxt'
PATH_TO_TEST_IMAGES_DIR = os.path.join('test')
NUM_CLASSES = 37
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR,'{}'.format(file)) for file in os.listdir(PATH_TO_TEST_IMAGES_DIR)]
print(TEST_IMAGE_PATHS)
#TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image#{}.jpg'.format(i)) for i in range(1, 3) ]
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
def write_jpeg(data, filepath):
g = tf.Graph()
with g.as_default():
data_t = tf.placeholder(tf.uint8)
op = tf.image.encode_jpeg(data_t, format='rgb', quality=100)
init = tf.initialize_all_variables()
with tf.Session(graph=g) as sess:
sess.run(init)
data_np = sess.run(op, feed_dict={ data_t: data })
with open(filepath, 'wb') as fd:
fd.write(data_np)
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
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)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
write_jpeg(image_np, os.path.join(os.path.dirname(image_path),'inferred', os.path.basename(image_path)))
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
plt.show()
print(image_path)
print(boxes)
print(classes)
print(scores)
#write_jpeg(image_np, os.path.join(os.path.dirname(image_path),os.path.splitext(os.path.basename(image_path))[1]))
检测失败,因为培训需要很多步骤。我从2000步开始得到结果。jpeg损坏错误可能与牛津数据集有关,但我仍然能够使用该错误进行测试
哪个文件会导致此错误?你能手动打开吗?当我浏览它们时,所有的图像文件看起来都很好。我认为这个错误是随机的。评估在另一个检查点之后再次开始&错误重复。我的问题有两个。一个是评估,另一个是检测。即使评估失败,当我使用训练集中的图像时,检测也应该成功。但我甚至没有一个边界框。。。