tensorflow利用预训练模型进行目标检测(一):预训练模型的使用

一、运行样例

官网链接:https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb  但是一直有问题,没有运行起来,所以先使用一个别人写好的代码

上一个在ubuntu下可用的代码链接:https://gitee.com/bubbleit/JianDanWuTiShiBie  使用python2运行,python3可能会有问题

该代码由https://gitee.com/talengu/JianDanWuTiShiBie/tree/master而来,经过我部分的调整与修改,代码包含在ODtest.py文件中,/ssd_mobilenet_v1_coco_11_06_2017中存储的是预训练模型

原始代码如下

import numpy as np
from matplotlib import pyplot as plt
import os
import tensorflow as tf
from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util

import datetime
# 关闭tensorflow警告
os.environ[‘TF_CPP_MIN_LOG_LEVEL‘]=‘3‘

detection_graph = tf.Graph()

# 加载模型数据-------------------------------------------------------------------------------------------------------
def loading():

    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        PATH_TO_CKPT = ‘ssd_mobilenet_v1_coco_11_06_2017‘ + ‘/frozen_inference_graph.pb‘
        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=‘‘)
    return detection_graph

# Detection检测-------------------------------------------------------------------------------------------------------
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)
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join(‘data‘, ‘mscoco_label_map.pbtxt‘)
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

def Detection(image_path="images/image1.jpg"):
    loading()
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # 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)
            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.
            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.
            scores = detection_graph.get_tensor_by_name(‘detection_scores:0‘)
            classes = detection_graph.get_tensor_by_name(‘detection_classes:0‘)
            num_detections = detection_graph.get_tensor_by_name(‘num_detections:0‘)

            # Actual detection.
            (boxes, scores, classes, num_detections) = sess.run(
                [boxes, scores, 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)
            # output result输出
            for i in range(3):
                if classes[0][i] in category_index.keys():
                    class_name = category_index[classes[0][i]][‘name‘]
                else:
                    class_name = ‘N/A‘
                print("物体:%s 概率:%s" % (class_name, scores[0][i]))

            # matplotlib输出图片
            # Size, in inches, of the output images.
            IMAGE_SIZE = (20, 12)
            plt.figure(figsize=IMAGE_SIZE)
            plt.imshow(image_np)
            plt.show()

# 运行
Detection()

git clone到本地后执行有几个错误

问题1

报错信息: UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe5 in position 1: ordinal not in range(128)

solution:参考:https://www.cnblogs.com/QuLory/p/3615584.html

主要错误是上面最后一行的Unicode解码问题,网上搜索说是读取文件时使用的编码默认时ascii而不是utf8,导致的错误;

在代码中加上如下几句即可。

import sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)

问题1

报错信息:_tkinter.TclError: no display name and no $DISPLAY environment variable 详情:

Traceback (most recent call last):
  File "ODtest.py", line 103, in <module>
    Detection()
  File "ODtest.py", line 96, in Detection
    plt.figure(figsize=IMAGE_SIZE)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 533, in figure
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 161, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/_backend_tk.py", line 1046, in new_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1822, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

solution:参考:https://blog.csdn.net/qq_22194315/article/details/77984423

纯代码解决方案

这也是大部分人在网上诸如stackoverflow的问答平台得到的解决方案,在引入pyplot、pylab之前,要先更改matplotlib的后端模式为”Agg”。直接贴代码吧!

# do this before importing pylab or pyplot
Import matplotlib
matplotlib.use(‘Agg‘)
import matplotlib.pyplot asplt

修改之后代码为:

#!usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib
matplotlib.use(‘Agg‘)
import matplotlib.pyplot
from matplotlib import pyplot as plt
import os
import tensorflow as tf
from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util

import datetime
# 关闭tensorflow警告
import sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)

os.environ[‘TF_CPP_MIN_LOG_LEVEL‘]=‘3‘

detection_graph = tf.Graph()

# 加载模型数据-------------------------------------------------------------------------------------------------------
def loading():

    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        PATH_TO_CKPT = ‘ssd_mobilenet_v1_coco_11_06_2017‘ + ‘/frozen_inference_graph.pb‘
        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=‘‘)
    return detection_graph

# Detection检测-------------------------------------------------------------------------------------------------------
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)
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join(‘data‘, ‘mscoco_label_map.pbtxt‘)
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

def Detection(image_path="images/image1.jpg"):
    loading()
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # 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)
            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.
            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.
            scores = detection_graph.get_tensor_by_name(‘detection_scores:0‘)
            classes = detection_graph.get_tensor_by_name(‘detection_classes:0‘)
            num_detections = detection_graph.get_tensor_by_name(‘num_detections:0‘)

            # Actual detection.
            (boxes, scores, classes, num_detections) = sess.run(
                [boxes, scores, 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)
            # output result输出
            for i in range(3):
                if classes[0][i] in category_index.keys():
                    class_name = category_index[classes[0][i]][‘name‘]
                else:
                    class_name = ‘N/A‘
                print("object:%s gailv:%s" % (class_name, scores[0][i]))

            # matplotlib输出图片
            # Size, in inches, of the output images.
            IMAGE_SIZE = (20, 12)
            plt.figure(figsize=IMAGE_SIZE)
            plt.imshow(image_np)
            plt.show()

# 运行
Detection()

运行结果:

如无意外,加上时间统计函数,调用已下载好的预训练模型即可

二、使用与训练模型

aa

原文地址:https://www.cnblogs.com/vactor/p/9820604.html

时间: 2024-10-31 08:32:27

tensorflow利用预训练模型进行目标检测(一):预训练模型的使用的相关文章

在opencv3中利用SVM进行图像目标检测和分类

采用鼠标事件,手动选择样本点,包括目标样本和背景样本.组成训练数据进行训练 1.主函数 #include "stdafx.h" #include "opencv2/opencv.hpp" using namespace cv; using namespace cv::ml; Mat img,image; Mat targetData, backData; bool flag = true; string wdname = "image"; voi

基于Intel OpenVINO的搭建及应用,包含分类,目标检测,及分割,超分辨

PART I: 搭建环境OPENVINO+Tensorflow1.12.0 I: l_openvino_toolkit_p_2019.1.094 第一步常规安装参考链接:https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 第二步编译Inference Engine Samples: cd /PATH/TO/deployment_tools/inference_eng

HoG SVM 目标检测分析

前一段时间开始了解HoG跟SVM行人识别,看了很多包括Dalal得前辈的文章及经验分享,对HoG理论有了些初步的认识. HoG 的全称是 Histogram of Oriented Gradient, 直译过来也就是梯度方向直方图. 就是计算各像素的梯度方向,统计成为直方图来作为特征表示目标. 下面简述一下利用HoG + SVM 实现目标检测的简要步骤 Step1:获取正样本集并用hog计算特征得到hog特征描述子.例如进行行人检测,可用IRINA等行人样本集,提取出行人的描述子. Step2:

利用 ImageAI 在 COCO 上学习目标检测

ImageAI是一个python库,旨在使开发人员能够使用简单的几行代码构建具有包含深度学习和计算机视觉功能的应用程序和系统. 这个 AI Commons 项目https://commons.specpal.science 由 Moses Olafenwa 和 John Olafenwa 开发和维护.为了更好的使用 ImageAI,我将其 Fork 到 CodeXZone/ImageAI.同时,ImageAI 也提供了中文手册:imageai.下面我将借助该教程一步一步的学习目标检测. 利用 c

Tensorflow实战目标检测

首先到github下载相应的Tensorflow模型,以及配置好环境.具体的可参考这篇博客 或者参考Github上,TensorFlow models/research/object_detection里的安装教程. 这里给出一个视频里面的目标检测代码: import os import time import argparse import multiprocessing import numpy as np import tensorflow as tf import tarfile fro

目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as np import os import tensorflow as tf from PIL import Image classes = ["aeroplane", "bicycle", "bird", "boat", &quo

利用更快的r-cnn深度学习进行目标检测

此示例演示如何使用名为"更快r-cnn(具有卷积神经网络的区域)"的深度学习技术来训练对象探测器. 概述 此示例演示如何训练用于检测车辆的更快r-cnn对象探测器.更快的r-nnn [1]是r-cnn [2]和快速r-nnn [3]对象检测技术的引伸.所有这三种技术都使用卷积神经网络(cnn).它们之间的区别在于它们如何选择要处理的区域以及如何对这些区域进行分类.r-cnn和快速r-概算在运行美国有线电视新闻网之前使用区域建议算法作为预处理步骤.提议算法通常是技术例如edgox [4]

第三十一节,使用谷歌Object Detection API进行目标检测

Object Detection API是谷歌开放的一个内部使用的物体识别系统.2016年 10月,该系统在COCO识别挑战中名列第一.它支持当前最佳的实物检测模型,能够在单个图像中定位和识别多个对象.该系统不仅用于谷歌于自身的产品和服务,还被推广至整个研究社区. 一.代码位置与内置的模型 1.Object Detection Object Detection模块的位置与slim的位置相近,同在github.com 中TensorFlow 的models\research目录下.类似slim,

第三十六节,目标检测之yolo源码解析

在一个月前,我就已经介绍了yolo目标检测的原理,后来也把tensorflow实现代码仔细看了一遍.但是由于这个暑假事情比较大,就一直搁浅了下来,趁今天有时间,就把源码解析一下.关于yolo目标检测的原理请参考前面一篇文章:第三十五节,目标检测之YOLO算法详解 在讲解源码之前,我们需要做一些准备工作: 下载源码,本文所使用的yolo源码来源于网址:https://github.com/hizhangp/yolo_tensorflow 下载训练所使用的数据集,我们仍然使用以VOC 2012数据集