导出推理图后,ssd_mobilenet_v2_coco无法检测到自定义训练对象

西蒙

我正在尝试使用张量流api检测万寿菊。我已经使用faster_rcnn_inception模型进行了尝试,该模型效果很好,但是我打算在raspi4上运行检测,因为它太重了。

所以我尝试了ssd_mobilenet_v2_coco。在训练中,一切工作正常,它几乎可以检测到每朵花,但是当我尝试在同一张图片上使用导出的推理图时,它什么也检测不到。

从张量板训练期间在评估中检测到花朵

用于推断的代码:

######## Image Object Detection Using Tensorflow-trained Classifier #########
#
# Author: Evan Juras
# Date: 1/15/18
# Description: 
# This program uses a TensorFlow-trained neural network to perform object detection.
# It loads the classifier and uses it to perform object detection on an image.
# It draws boxes, scores, and labels around the objects of interest in the image.

## Some of the code is copied from Google's example at
## https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb

## and some is copied from Dat Tran's example at
## https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py

## but I changed it to make it more understandable to me.

# Import packages
import os
import cv2
import numpy as np
import tensorflow as tf
import sys

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

# Import utilites
from utils import label_map_util
from utils import visualization_utils as vis_util

# Name of the directory containing the object detection module we're using
MODEL_NAME = 'model'
IMAGE_NAME = 'test2.jpg'

# Grab path to current working directory
CWD_PATH = os.getcwd()

# Path to frozen detection graph .pb file, which contains the model that is used
# for object detection.
PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')

# Path to label map file
PATH_TO_LABELS = os.path.join(CWD_PATH,'labelmap.pbtxt')

# Path to image
PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)

# Number of classes the object detector can identify
NUM_CLASSES = 1

# Load the label map.
# Label maps map indices to category names, so that when our convolution
# network predicts `5`, we know that this corresponds to `king`.
# Here we use internal utility functions, but anything that returns a
# dictionary mapping integers to appropriate string labels would be fine
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)

# Load the Tensorflow model into memory.
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='')

    sess = tf.Session(graph=detection_graph)

# Define input and output tensors (i.e. data) for the object detection classifier

# Input tensor is the image
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

# Output tensors are the detection boxes, scores, and classes
# 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 represents level of confidence for each of the objects.
# The 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')

# Number of objects detected
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# Load image using OpenCV and
# expand image dimensions to have shape: [1, None, None, 3]
# i.e. a single-column array, where each item in the column has the pixel RGB value
image = cv2.imread(PATH_TO_IMAGE)
image_expanded = np.expand_dims(image, axis=0)

# Perform the actual detection by running the model with the image as input
(boxes, scores, classes, num) = sess.run(
    [detection_boxes, detection_scores, detection_classes, num_detections],
    feed_dict={image_tensor: image_expanded})

# Draw the results of the detection (aka 'visulaize the results')

vis_util.visualize_boxes_and_labels_on_image_array(
    image,
    np.squeeze(boxes),
    np.squeeze(classes).astype(np.int32),
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True,
    line_thickness=8,
    min_score_thresh=0.60)

# All the results have been drawn on image. Now display the image.
cv2.imshow('Object detector', image)

# Press any key to close the image
cv2.waitKey(0)

# Clean up
cv2.destroyAllWindows()
威丹舒

您没有将BGR转换为RGB。OpenCV以BGR格式读取图像,并且以RGB格式训练Tensorflow。因此,只需执行以下操作:

image = cv2.imread(PATH_TO_IMAGE)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # <== Will convert BGR to RGB
image_expanded = np.expand_dims(image, axis=0)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

model_main.py 无法训练 mobilenet ssd v2 - tensorflow 对象检测 api

使用带有 OpenCV DNN 模块的自定义 SSD Mobilenet 模型没有检测到

重新训练MobileNet SSD V1 COCO后,Tensorflow的pb和pbtxt文件无法与OpenCV一起使用

DarkNet-未检测到自定义训练数据

无法检测到自定义框架代码

如何为自定义类构建tensorflow对象检测模型,以及如何包括SSD Mobilenet模型包含的90个类

在Tensorlfow对象检测API中导出推理图时出错

未检测到自定义SpringVaadinServlet

如何在Android应用中使用ssd_mobilenet_v2_mnasfpn_coco

用于对象检测和分割的 Mask R-CNN [训练自定义数据集]

TF Slim:在自定义数据集上微调mobilenet v2

无法检测到 ROS 中的自定义消息子目录(即 package.msg)

Kubernetes HPA无法从Stackdriver检测到成功发布的自定义指标

对象检测:导出/导入推理时出错

ssd_mobilenet_v1_coco SSD是如何用mobilenet实现的

在Tensorflow对象检测API中顺序导出两个不同的推理图时出错

包属性未检测到Log4j2(2.1)自定义插件

关闭电源2天后未检测到SSD

我的自定义mobilenet训练模型未显示任何结果。我究竟做错了什么?

Android Studio 未检测到自定义 SDK

自定义布局未在mainActivity类中检测到?

迁移未检测到 Django 自定义字段更改

在Chrome自定义标签中检测到内存泄漏

自定义springboot自动配置未检测到bean

Resharper未检测到自定义插件

Xcode 未检测到自定义字体

在单一类型的对象上训练时使用tensorflow进行自定义对象检测的问题

tensorflow openvino ssd-mobilnet coco自定义数据集错误输入层

WIX工具集-自定义操作命令无法正确检测到Windows 10版本