训练目标检测模型
目錄
?
前言:
一、VOC數據集的制作
1、數據的標注工具:labelIImg ----我也是在github上下載的,這里我提供我的鏈接
2、數據集的文件夾:由于我的只涉及目標檢測,故只需以下幾個文件目錄:
二、實現
1、Pascal_label_map.pbtxt文件格式:
2、將數據集轉換為tfrecord格式,書中提供了create_pascal_tf_record.py,在這里,需要對書中的代碼修改部分。如下:
3、voc.config文件:這個按照書中的方式添加參數即可,下面是我的:
4、最后配置完后的文件目錄:
5、開始訓練:train.py
6、導出模型:export_inference_graph.py
前言:
? ? ?由于畢業設計涉及到圖像識別的目標檢測,故需要訓練一個識別家具的目標檢測模型。而訓練模型的代碼是參考《21個項目玩轉深度學習》中的第五章的目標檢測代碼。但是書中代碼的環境是python2.x版本的,而我的是python3.6。故需要在他的代碼基礎上做些修改,才可以正確訓練,書中已經大致介紹了模型訓練的整個過程,而我會在他的基礎簡單補充一下我的步驟以及部分代碼的修改。
一、VOC數據集的制作
? ??書中的代碼是在VOC數據集的基礎上訓練模型的,但是由于我的畢業設計要識別是室內家具等,故我要制作自己的VOC格式的數據集。而制作voc數據集的方法步驟網上很多,我也是按照他們的步驟制作的。
附上書的鏈接以及書中的代碼:
? ? 鏈接:https://pan.baidu.com/s/1RGlJkxfqE9ba71kUGmwfWA?
? ? 提取碼:3mfx
可以參考一下:https://blog.csdn.net/weixin_38385446/article/details/81081172?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3
1、數據的標注工具:labelIImg ----我也是在github上下載的,這里我提供我的鏈接
? ? 鏈接:https://pan.baidu.com/s/17oAJU2_tfp5X5yjrFI4v1g
? ? 提取碼:kqdw
2、數據集的文件夾:由于我的只涉及目標檢測,故只需以下幾個文件目錄:
? ??
- ? ? 首先將訓練圖片放到JPEGImages,然后將圖片重命名為VOC2007的“000001.jpg”形式
- ? ? 圖片批量重名代碼參考:http://blog.csdn.net/u011574296/article/details/72956446
- ? ? Annotations文件夾中存放的是每張圖片對應的標注文件,注意:其中標注的標簽名最好是小寫英文字母,大寫的可能會報錯。
- ? ? 最后是ImageSets文件夾,該文件夾下存放的是4個txt,分別是:
? ? test.txt是測試集
? ?train.txt是訓練集
? ?val.txt是驗證集
? ?trainval.txt是訓練和驗證集
? 如圖:
?
?
而這四個txt可以用代碼生成:
import os import randomtrainval_percent = 0.8 train_percent = 0.8 xmlfilepath = 'D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/Annotations' txtsavepath = 'D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main' total_xml = os.listdir(xmlfilepath)num = len(total_xml) list = range(num) tv = int(num * trainval_percent) tr = int(tv * train_percent) trainval = random.sample(list, tv) train = random.sample(trainval, tr)ftrainval = open('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main/trainval.txt', 'w') ftest = open('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main/test.txt', 'w') ftrain = open('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main/train.txt', 'w') fval = open('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main/val.txt', 'w')for i in list:name = total_xml[i][:-4] + '\n'if i in trainval:ftrainval.write(name)if i in train:ftrain.write(name)else:fval.write(name)else:ftest.write(name)ftrainval.close() ftrain.close() fval.close() ftest.close()至此數據集已經制作完成
二、實現
1、Pascal_label_map.pbtxt文件格式:
2、將數據集轉換為tfrecord格式,書中提供了create_pascal_tf_record.py,在這里,需要對書中的代碼修改部分。如下:
第84、85行需要修改,這是書的代碼:
?修改為:
?
第162、163行代碼也需要修改,書中代碼:
修改為:
?
然后在create_pascal_tf_record.py中還需添加以下文件路徑:
在生成pascal_train.record時:
在生成pascal_val.record時:
隨后便會生成相應的兩個tfrecord文件:
?
3、voc.config文件:這個按照書中的方式添加參數即可,下面是我的:
?
?
?
?
?
4、最后配置完后的文件目錄:
?
5、開始訓練:train.py
添加兩行參數:
?
注:當你在訓練時,出現內存不足的錯誤時,可以調整voc.config中的:
分別減小一半。
6、導出模型:export_inference_graph.py
添加一些參數:
注意:第83行中模型的步數是你實際訓練的步數。
參考代碼:
import numpy as np import os import six.moves.urllib as urllib import sys import tarfile import tensorflow as tf import pandas as pd import zipfile import cv2 from collections import defaultdict from io import StringIO from matplotlib import pyplot as plt 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 research.object_detection.utils import label_map_util from research.object_detection.utils import visualization_utils as vis_utilMODEL_NAME = 'frozen_inference_graph.pb'# # Path to frozen detection graph. This is the actual model that is used for the object detection. PATH_TO_CKPT = r'voc\export\frozen_inference_graph.pb' # # # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = r'voc\pascal_label_map.pbtxt' NUM_CLASSES = 12# 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.GraphDef()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='') # 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.sizereturn np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)# For the sake of simplicity we will use only 2 images: # 這里說明測試圖片的命名規則為imagen.jpg, 遵守規則即可 # image1.jpg # image.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 = r'test_images' # 存放測試圖片的路徑 imagename = 'test.jpg' TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, imagename)] # 修改測試圖片的張數range(1, n + 1), 為測試圖片的張數# Size, in inches, of the output images. IMAGE_SIZE = (12, 8)with detection_graph.as_default():with tf.Session(graph=detection_graph) as sess: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')for image_path in TEST_IMAGE_PATHS:image = Image.open(image_path)width,height = image.size# 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)# 開始檢測(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections],feed_dict={image_tensor: image_np_expanded})# 可視化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)plt.figure(figsize=IMAGE_SIZE)plt.imshow(image_np)plt.show()print(num_detections)plt.imsave('save_image/' + imagename,image_np)我的檢測效果:
?
總結
- 上一篇: [附源码]计算机毕业设计基于spring
- 下一篇: 欧盟RoHS指令连续发布豁免条款,202