热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:tensorflow利用预训练模型进行目标检测:预训练模型的使用

本文由编程笔记#小编为大家整理,主要介绍了tensorflow利用预训练模型进行目标检测:预训练模型的使用相关的知识,希望对你有一定的参考价值。一、运行样例
本文由编程笔记#小编为大家整理,主要介绍了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()


View Code

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

问题1

报错信息: UnicodeDecodeError: ascii codec cant 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
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


View Code

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


View Code

修改之后代码为:


技术分享图片技术分享图片

#!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()


View Code

运行结果:

技术分享图片

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

 


二、使用与训练模型

aa



推荐阅读
  • 详解 Python 的二元算术运算,为什么说减法只是语法糖?[Python常见问题]
    原题|UnravellingbinaryarithmeticoperationsinPython作者|BrettCannon译者|豌豆花下猫(“Python猫 ... [详细]
  • 开源Keras Faster RCNN模型介绍及代码结构解析
    本文介绍了开源Keras Faster RCNN模型的环境需求和代码结构,包括FasterRCNN源码解析、RPN与classifier定义、data_generators.py文件的功能以及损失计算。同时提供了该模型的开源地址和安装所需的库。 ... [详细]
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • EzPP 0.2发布,新增YAML布局渲染功能
    EzPP发布了0.2.1版本,新增了YAML布局渲染功能,可以将YAML文件渲染为图片,并且可以复用YAML作为模版,通过传递不同参数生成不同的图片。这个功能可以用于绘制Logo、封面或其他图片,让用户不需要安装或卸载Photoshop。文章还提供了一个入门例子,介绍了使用ezpp的基本渲染方法,以及如何使用canvas、text类元素、自定义字体等。 ... [详细]
  • 通过Anaconda安装tensorflow,并安装运行spyder编译器的完整教程
    本文提供了一个完整的教程,介绍了如何通过Anaconda安装tensorflow,并安装运行spyder编译器。文章详细介绍了安装Anaconda、创建tensorflow环境、安装GPU版本tensorflow、安装和运行Spyder编译器以及安装OpenCV等步骤。该教程适用于Windows 8操作系统,并提供了相关的网址供参考。通过本教程,读者可以轻松地安装和配置tensorflow环境,以及运行spyder编译器进行开发。 ... [详细]
  • Python15行代码实现免费发送手机短信,推送消息「建议收藏」
    Python15行代码实现免费发 ... [详细]
  • 我用Tkinter制作了一个图形用户界面,有两个主按钮:“开始”和“停止”。请您就如何使用“停止”按钮终止“开始”按钮为以下代码调用的已运行功能提供建议 ... [详细]
  • 由于同源策略的限制,满足同源的脚本才可以获取资源。虽然这样有助于保障网络安全,但另一方面也限制了资源的使用。那么如何实现跨域呢,以下是实现跨域的一些方法。 ... [详细]
author-avatar
赵娜supergirl
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有