Tensorflow 对象检测 API 教程错误 [英] Tensorflow object detection API tutorial error

查看:58
本文介绍了Tensorflow 对象检测 API 教程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解决了 Tensorflow 2.00 和对象检测 API 之间的兼容性问题之后,我降级到 Tensorflow 1.15 以便能够训练我自己的模型.完成培训后,我修改了 Tensorflow 对象中包含的 jupyter 笔记本检测 API 存储库在我自己的图像上进行测试,但我不断收到此错误:

After struggling with compatibility issues between Tensorflow 2.00 and the object detection API, I downgraded to Tensorflow 1.15 to be able to train my own model. after completing the training I modified the jupyter notebook included in the Tensorflow object detection API repo to test on my own images but I keep getting this error:

回溯(最近一次调用最后一次): 中的文件object_detection_tutorial_converted.py",第 254 行显示推理(检测模型,图像路径)文件object_detection_tutorial_converted.py",第 235 行,在 show_inferenceoutput_dict = run_inference_for_single_image(模型,image_np)文件object_detection_tutorial_converted.py",第 203 行,在 run_inference_for_single_image 中num_detections = int(output_dict.pop('num_detections'))类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是张量"

这是我修改后的 jupyter 笔记本

Here's my modified jupyter notebook

import os
import pathlib
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile

from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import display
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util


# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1

# Patch the location of gfile
tf.gfile = tf.io.gfile


def load_model(model_name):
  model_dir = pathlib.Path(model_name)/"saved_model"
  model = model = tf.compat.v2.saved_model.load(str(model_dir), None)
  model = model.signatures['serving_default']
  return model


# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = 'training/label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)


TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('test_images')
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS



model_name = 'devices_graph'
detection_model = load_model(model_name)


print(detection_model.inputs)


detection_model.output_dtypes


detection_model.output_shapes


def run_inference_for_single_image(model, image):
  image = np.asarray(image)
  # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
  input_tensor = tf.convert_to_tensor(image)
  # The model expects a batch of images, so add an axis with `tf.newaxis`.
  input_tensor = input_tensor[tf.newaxis,...]

  # Run inference
  output_dict = model(input_tensor)
  # All outputs are batches tensors.
  # Convert to numpy arrays, and take index [0] to remove the batch dimension.
  # We're only interested in the first num_detections.
  num_detections = int(output_dict.pop('num_detections'))
  output_dict = {key:value[0, :num_detections].numpy() 
                 for key,value in output_dict.items()}
  output_dict['num_detections'] = num_detections

  # detection_classes should be ints.
  output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

  # Handle models with masks:
  if 'detection_masks' in output_dict:
    # Reframe the the bbox mask to the image size.
    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
              output_dict['detection_masks'], output_dict['detection_boxes'],
               image.shape[0], image.shape[1])      
    detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
                                       tf.uint8)
    output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()

  return output_dict


# Run it on each test image and show the results:


def show_inference(model, 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 = np.array(Image.open(image_path))
  # Actual detection.
  output_dict = run_inference_for_single_image(model, image_np)
  # 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_reframed', None),
      use_normalized_coordinates=True,
      line_thickness=8)

  display(Image.fromarray(image_np))



for image_path in TEST_IMAGE_PATHS:
  show_inference(detection_model, image_path)

推荐答案

很奇怪这对我来说适用于 tensorflow 2.0.0.你能发送控制台日志吗

That's strange this worked for tensorflow 2.0.0 for me. Can you send console log

这篇关于Tensorflow 对象检测 API 教程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆