繼續上篇部落格介紹的
【Tensorflow】SSD_Mobilenet_v2實作目标檢測(一):環境配置+訓練
接下來SSD_Mobilenet_v2實作目标檢測之訓練後實作測試。
訓練後會在指定的檔案夾内生成如下檔案

1. 可視化訓練過程
tensorboard --logdir=C:\Users\znjt\Desktop\loss # 儲存.tfevents的路徑
将獲得的網址複制到火狐或谷歌浏覽器進行檢視:
根據以上的資訊,可以選擇最佳的模型,筆者簡單測試了6000epoch,是以訓練結果并不好,可以增加epoch的數量以及适當調整訓練參數。
具體的可視化過程可以參考:【tensorboard】可視化events.out.tfevents檔案
2. 導出模型檔案
在object detection目錄下的export_inference_graph.py,使用如下指令導出模型結果:
python export_inference_graph.py --pipeline_config_path=/home/lianlirong/models-master/research/object_detection/data/hengfeng/ssd_mobilenet_v2_coco.config --trained_checkpoint_prefix /home/lianlirong/models-master/logs/hengfeng/model.ckpt-60000 --output_directory /home/lianlirong/models-master/logs/hengfeng/model
執行完指令後,在model檔案夾下生成如下檔案
其中,frozen_inference_graph.pb就是我們以後将要使用的模型結果。
3. 擷取測試圖檔
在Annotations的同級目錄下建立一個檔案夾test(用于儲存測試檔案)以及python檔案get_test.py(用于擷取測試圖檔),代碼如下:
"""
2020.09.23:alian
tensorflow-ssd-test-img
"""
from PIL import Image
import os.path
import glob
annotations_test_dir = "/home/lianlirong/models-master/research/object_detection/images/VOC2007-hengfeng/Annotations/test/" # 訓練時生成的用于測試劃分出來的xml檔案
Images_dir = "/home/lianlirong/models-master/research/object_detection/images/VOC2007-hengfeng/JPEGImages" # 原圖路徑
test_images_dir = "/home/lianlirong/models-master/research/object_detection/images/VOC2007-hengfeng/test" # 空檔案夾,儲存擷取的測試圖檔
i = 0
for xmlfile in os.listdir(annotations_test_dir):
(filepath, tempfilename) = os.path.split(xmlfile)
(shotname, extension) = os.path.splitext(tempfilename)
xmlname = shotname
for pngfile in os.listdir(Images_dir):
(filepath, tempfilename) = os.path.split(pngfile)
(pngname, extension) = os.path.splitext(tempfilename)
if pngname == xmlname:
img = Image.open(Images_dir+"/" + pngname + ".jpg")
img.save(os.path.join(test_images_dir, os.path.basename(pngfile)))
print(pngname)
i += 1
print(i)
運作代碼,在test檔案夾下獲得測試圖檔
4. 測試圖檔并儲存測試結果
在./logs/hengfeng/(即model的同級目錄)下建立results_imgs檔案夾;在/models-master/research/object_detection/檔案夾下get_results.py檔案并加入如下代碼,我們将使用前面訓練出的模型批量測試test檔案夾中的圖檔并儲存到results_imgs檔案夾中。
"""
2020.09.23:alian
獲得測試結果
"""
# -*- coding: utf-8 -*-
import os
from PIL import Image
import time
import tensorflow as tf
from PIL import Image
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import zipfile
import time
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
# plt.switch_backend('Agg')
from utils import label_map_util
from utils import visualization_utils as vis_util
PATH_TO_TEST_IMAGES = "/home/lianlirong/models-master/research/object_detection/images/VOC2007-hengfeng/JPEGImages/" # 測試圖檔的路徑
MODEL_NAME = '/home/lianlirong/models-master/logs/hengfeng/model' # 模型的儲存路徑
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb' # 模型檔案
PATH_TO_LABELS = '/home/lianlirong/models-master/research/object_detection/data/hengfeng/hengfeng_label_map.pbtxt' # 标簽檔案的路徑
NUM_CLASSES = 2 # 檢測的目标數
PATH_TO_RESULTS = "/home/lianlirong/models-master/logs/hengfeng/results_imgs/" # 測試結果圖的儲存路徑
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)
def save_object_detection_result():
IMAGE_SIZE = (12, 8)
# Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
# loading ckpt file to graph
with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: ## tf.gfile.GFile更新為tf.io.gfile.GFile,否則會出錯‘utf8’
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
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
start = time.time()
for test_image in os.listdir(PATH_TO_TEST_IMAGES):
image = Image.open(PATH_TO_TEST_IMAGES + test_image)
# 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)
final_score = np.squeeze(scores)
count = 0
for i in range(100):
if scores is None or final_score[i] > 0.5:
count = count + 1
print()
print("the count of objects is: ", count)
(im_width, im_height) = image.size
for i in range(count):
# print(boxes[0][i])
y_min = boxes[0][i][0] * im_height
x_min = boxes[0][i][1] * im_width
y_max = boxes[0][i][2] * im_height
x_max = boxes[0][i][3] * im_width
x = int((x_min + x_max) / 2)
y = int((y_min + y_max) / 2)
if category_index[classes[0][i]]['name'] == "tower":
print("this image has a tower!")
y = int((y_max - y_min) / 4 * 3 + y_min)
print("object{0}: {1}".format(i, category_index[classes[0][i]]['name']),
',Center_X:', x, ',Center_Y:', y)
# print(x_min,y_min,x_max,y_max)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
picName = test_image.split('/')[-1]
# print(picName)
plt.savefig(PATH_TO_RESULTS + picName)
print(test_image + ' succeed')
end = time.time()
seconds = end - start
print("Time taken : {0} seconds".format(seconds))
save_object_detection_result()
運作代碼,獲得測試結果圖。
檔案夾樹形結構如下:
├──models-master (tensorflow項目檔案)
├── logs # 筆者存放訓練模型的目錄
├──hengfeng
├──model # 轉換生成的模型檔案
├──results_imgs # 儲存結果圖檔
├── research
├──generate_tfrecord.py # 生成tfrecord檔案的代碼
├──object_detection
├──ssd_mobilenet_v2_coco_2018_03_29
├──model.ckpt # 預訓練模型檔案
├──model_main.py # 訓練代碼檔案
├──get_redults.py # 測試運作代碼
├──images
├──hengfeng # 目标資料集
├──Annotations # xml檔案
├──JPEGImages # 原圖檔
├──train_test_split.py # 劃分訓練測試集的代碼
├──xml_to_csv.py # 生成csv的代碼檔案
├──others_object …# 其他目标的資料集
├──get_test.py # 擷取測試圖檔
├──test # 儲存測試圖檔的檔案夾
├──data
├──hengfeng # 目标訓練的必要檔案
├──hengfeng_label_map.pbtxt # 目标标簽檔案
├──hengfeng_train.tfrecord #生成的tfrecord檔案
├──hengfeng_val.tfrecord
├──hengfeng_test.tfrecord
├──hengfeng_train_labels.csv # 生成的csv檔案
├──hengfeng_val_labels.csv
├──hengfeng_test_labels.csv
├──ssd_mobilenet_v2_coco.config # 訓練的配置檔案
├──others_object #其他目标
以上是基于tensorflow +SSD_Mobilenet_v2實作目标檢測的全部内容