使用SSD網絡模型進行Tensorflow物體檢測?(V1.0圖像檢測)
文章目錄
- 使用SSD網絡模型進行Tensorflow物體檢測?(V1.0圖像檢測)
-
- 1.模型的加載和utils庫環境的配置?
- 2.模型的導入和使用?
-
- 1.import 使用的相關的庫
-
- 關于庫的安裝和解除安裝更新問題?
- 2.使用import_graph_def()和load_labelmap()對模型檔案的導入
- 3.對label_map進行轉換得到category_index分類索引檔案
- 4.自定義将image圖檔轉換為numpy數組的函數
- 5.定義測試圖檔路徑,以及使用tf.Graph()預設圖進行測試
- 6.在預設圖tf.Graph()中添加測試圖檔的可視化效果
- 3.模型的測試與評估?
1.模型的加載和utils庫環境的配置?
1.準備好ssd_mobilenet_v1網絡模型的frozen_inference_graph.pb和mscoco_label_map.pbtxt檔案。(文中所涉及檔案和代碼均已上傳,連結提取碼:mztu)

2.配置好utils庫檔案以及pycache環境,将準備好的label_map_util.py和visualization_utils.py檔案以及對應的label_map_util.cpython-36.pyc和visualization_utils.cpython-36.pyc複制粘貼到運作環境目錄下。
2.模型的導入和使用?
1.import 使用的相關的庫
import cv2
import numpy as np
import tensorflow as tf
from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util
關于庫的安裝和解除安裝更新問題?
提升下載下傳速度的方法:
指定源:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imageio(指定的庫)
指定版本的方法:
指定版本:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imageio==1.0.2(指定的源+庫版本)
更新庫時,解除安裝原來的庫失敗:
忽略installed的庫:pip install --ignore-installed -i https://pypi.tuna.tsinghua.edu.cn/simple imageio(指定的庫)
2.使用import_graph_def()和load_labelmap()對模型檔案的導入
PATH_TO_CKPT = "E:/DeepLearn/github項目/PART1/11 TensorFlow物體檢測/ssd_mobilenet_v1_coco_2017_11_17/" \
"frozen_inference_graph.pb"
PATH_TO_LABELS = "E:/DeepLearn/github項目/PART1/11 TensorFlow物體檢測/ssd_mobilenet_v1_coco_2017_11_17/" \
"mscoco_label_map.pbtxt"
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
od_graph_def.ParseFromString(fid.read())
tf.import_graph_def(od_graph_def, name='')#這個地方容易報錯,name不要填寫名稱,多調試幾次即可運作。
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
3.對label_map進行轉換得到category_index分類索引檔案
NUM_CLASSES = 90#這裡使用的ssd網絡共90種分類
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)
4.自定義将image圖檔轉換為numpy數組的函數
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)#将圖檔的格式重組
5.定義測試圖檔路徑,以及使用tf.Graph()預設圖進行測試
TEST_IMAGE_PATHS = ['E:/DeepLearn/github項目/PART1/11 TensorFlow物體檢測/test_images/image1.jpg']
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')#輸入層,0是序号
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')#檢測的候選區域層
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)
image_np = load_image_into_numpy_array(image)#将圖檔轉換為numpy數組
image_np_expanded = np.expand_dims(image_np, axis=0)#将圖檔的三維數組變成四維,第一個次元是序号
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})#喂資料得到相應的結果(boxes, scores, classes, num)
6.在預設圖tf.Graph()中添加測試圖檔的可視化效果
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)#将候選區域和标簽列印在圖檔上
image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
cv2.imshow('object detection', image_np)
cv2.imwrite("E:\DeepLearn\dogggy.jpg", image_np)
cv2.waitKey(0)
需要注意的是,這一部分代碼包含在for循環裡,不要放在with detection_graph.as_default():的外面!
3.模型的測試與評估?
人:
狗:
對于距離适中的物體類别檢測還是具有較高的識别精度,但是模型的細膩度還是有欠缺的,也存在錯誤識别,未識别的問題。SSD網絡模型作為一個可以動态識别物體類别的模型,識别圖檔的内容類别整體效果還是不錯的,SSD_V1模型也僅僅隻有20多MB,可以供初學者上手學習作為計算機視覺入門的案例。