首页 文章

对于宠物示例,tensorflow对象检测失败

提问于
浏览
0

american_pit_bull_terrier_26

basset_hound_3

beagle_166

bengal_191


系统信息

  • What is the top-level directory of the model you are using :tensorflow / models

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow) :没有

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04) :Windows 10 x64

  • TensorFlow installed from (source or binary) :二进制

  • TensorFlow version (use command below) :1.3.0

  • Bazel version (if compiling from source)

  • CUDA/cuDNN version

  • GPU model and memory

  • Exact command to reproduce :python object_detection \ eval.py --logtostderr --checkpoint_dir = train --eval_dir = eval --pipeline_config_path = ssd_mobilenet_v1.config&python detect.py

描述问题

从COCO集中使用微调检查点训练Oxford-IIIT Pet Dataset时,运行eval.py脚本时出现以下错误消息:

损坏的JPEG数据:标记0xd9之前的245个外部字节

在评估失败后运行detect.py脚本时,我得到附加的图像,没有任何检测框 .

源代码/日志

detect.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]))

1 回答

  • 1

    检测失败,因为培训需要很多步骤 . 我开始在2000步获得结果 . jpeg损坏错误可能与牛津数据集有关,但我仍然可以测试错误 .

    enter image description here

相关问题