記錄使用tensorflow的物體檢測預處理模型時所遇到的各種坑
python文件我使用的博客地址為:
Tensorflow object detection API訓練自己的目標檢測模型(檢測圖片中和視頻中的物體)
由於原博主的tensorflow版本是tensorflow1.14.0
版本,為了適配2.0版本人做了部分修改.
代碼如下:
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
import matplotlib
from PIL import Image
# # This is needed to display the images.
# %matplotlib inline
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
# from utils import label_map_util
# from utils import visualization_utils as vis_util
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
# What model to download.
MODEL_NAME = 'other_library_files/ssd_mobilenet_v1_coco_2017_11_17'
# MODEL_FILE = MODEL_NAME + '.tar.gz'
# DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
# 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')
PATH_TO_LABELS = 'other_library_files/models-master/research/object_detection/data/mscoco_label_map.pbtxt'
NUM_CLASSES = 90
# download model
# opener = urllib.request.URLopener()
# 下載模型,如果已經下載好了下面這句代碼可以注釋掉
# opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
# tar_file = tarfile.open(MODEL_FILE)
# for file in tar_file.getmembers():
# file_name = os.path.basename(file.name)
# if 'frozen_inference_graph.pb' in file_name:
# tar_file.extract(file, os.getcwd())
# Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.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='')
# # Loading label map
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)
# Helper code
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)
# For the sake of simplicity we will use only 2 images:
# image1.jpg
# image2.jpg
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = 'other_library_files/models-master/research/object_detection/test_images' # 測試圖片文件夾
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 6)] # 遍歷測試圖片文件夾的圖片
# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
# 圖像識別
with detection_graph.as_default():
with tf.compat.v1.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]
# 由於模型期望圖像具有形狀,因此請擴展尺寸:[1 ,無,無,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) # line_thickness 畫線框的線條粗細
# 用於顯示
matplotlib.use('TkAgg')
matplotlib.pyplot.figure(figsize=IMAGE_SIZE)
matplotlib.pyplot.imshow(image_np)
matplotlib.pyplot.show()
所遇到的問題:
1.缺少object_dection
模塊
ModuleNotFoundError: No module named ‘object_detection’
在我們使用tensorflow的預處理模型時需要用到tensorflow的api,它包含了很多的工具方法,包括圖像分類、檢測、自然語言處理NLP、視頻預測、圖像理解等等,我們需要的對象檢測API也包括在這里面。直接在github下載zip包,然后如果你的tensorflow是在Anaconda的一個開發環境中,就直接在anaconda的Lib的site-packages目錄下(Anaconda本身只是一個包管理器)新建一個xx.pth的文件,比如新建一個tensorflow_model.pth文件,添加你的models的3個路徑:
E:\models-master\research
E:\models-master\research\slim
E:\models-master\research\object_detection
如果你安裝時是單獨安裝的python沒有依靠anaconda,只需要在python的Lib的site-packages目錄下按照上面的一樣操作即可。
問題就解決了
2.無法導入string_int_label_map_pb2
具體錯誤為:
tensorflow object-detection ImportError: cannot import name 'string_int_label_map_pb2'
解決方法:
1.下載protoc-3.6.1-win32
2.解壓后將bin里面的protoc.exe的路徑加到電腦的環境變量的PATH中
3.打開cmd,在/model/research/
目錄下執行命令
protoc object_detection/protos/*.proto --python_out=.
發現出錯
object_detection/protos/*.proto: No such file or directory
這是因為*.”在windows系統無法識別
。這時就可以使用git命令,不要用CMD命令,當然這需要你Windows系統安裝了git了,Git for Windows下載 安裝完后,在/model/research/
目錄下使用git命令重試
protoc object_detection/protos/*.proto --python_out=.
問題解決
3.tensorflow缺失GraphDef
屬性
具體錯誤為:
Error: module 'tensorflow' has no attribute 'GraphDef'
這是因為tensorflow的1和2的版本的差異
解決辦法:
# 原來語句
graph_def = tf.GraphDef.FromString(file_handle.read())
修改為
# 修改后語句
graph_def = tf.compat.v1.GraphDef.FromString(file_handle.read())
4.tensorflow缺失gfile
屬性
具體錯誤為:
AttributeError: module 'tensorflow' has no attribute 'gfile'
運行如下代碼
if not tf.gfile.exists(DATA_DIRECTORY):
tf.gfile.makedirs(DATA_DIRECTORY)
with tf.gfile.GFile(filepath) as f:
會出現如下問題:
AttributeError: module ‘tensorflow’ has no attribute ‘gfile’
這是因為在當前的版本中,gfile已經定義在io包的file_io.py中。
解決辦法:
所以只要改為下面的即可:
if not tf.io.gfile.exists(DATA_DIRECTORY):
tf.io.gfile.makedirs(DATA_DIRECTORY)
with tf.io.gfile.GFile(filepath) as f:
5.tensorflow無法顯示Matplotlib的ui界面
具體錯誤為:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
這是因為Matplotlib默認中使用的Agg是一個沒有圖形顯示界面的終端,常用的有圖形界面顯示的終端有TkAgg等。,所以我們選擇更換為TKAgg
解決辦法:
在程序中增加一條語句:
import matplotlib
matplotlib.use('TkAgg') (增加這條語句)
6.ERROR:無效的連續字節
具體錯誤為:
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xca in position 0: invalid continuation byte
博主的原因與網上其他造成這種錯誤的原因不同,博主是因為博主項目中的資源路徑和所參考的文獻中的資源路徑不同,代碼中的PATH_TO_LABELS
的文本地址沒有修改導致引用的文本不存在從而產生的錯誤
解決辦法:
修改為正確的路徑即可。
最后
所參考的所有文獻地址如下: