tensorflow / models

Models and examples built with TensorFlow
Other
76.97k stars 45.79k forks source link

detection fails in pet detection example #2746

Closed meongithub closed 6 years ago

meongithub commented 6 years ago

american_pit_bull_terrier_26 basset_hound_3 beagle_166 bengal_191


System information

Describe the problem

When training the Oxford-IIIT Pet Dataset from with finetune checkpoint from COCO set, I get the following error message while running the eval.py script:

Corrupt JPEG data: 245 extraneous bytes before marker 0xd9

While running the detect.py script after the failed evaluation I get the attached images without any detection boxes.

Source code / logs

code for 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]))
bignamehyp commented 6 years ago

This question is better asked on StackOverflow since it is not a bug or feature request. There is also a larger community that reads questions there. Thanks!