手把手教物體檢測——yolov3
目錄

訓練
下載算法
下載.weights結尾的預訓練模型,并將weight文件轉為h5文件
修改類別。
Labelme標注的數據集轉為yolov3訓練的數據集。
執行Kmeans.py文件計算anchors。
修改train.py文件。
測試
修改yolo.py
修改yolo_video.py
摘要
YOLOv3是YOLO (You Only Look Once)系列目標檢測算法中的第三版,相比之前的算法,尤其是針對小目標,精度有顯著提升。在Pascal Titan X上處理608x608圖像速度達到20FPS,在 COCO test-dev 上 mAP@0.5 達到 57.9%,與RetinaNet(FocalLoss論文所提出的單階段網絡)的結果相近,并且速度快4倍。
相比YOLOV2,YOLOV3的改進之處主要有兩點:
多尺度預測 (類FPN)
更好的基礎分類網絡(類ResNet)和分類器
關于YOLOV3 的理解可以參照這兩篇文章:
1、yolo系列之yolo v3【深度解析】
yolo系列之yolo v3【深度解析】_木盞-CSDN博客_yolo3
。
YOLOV3的整體結構
2、目標檢測網絡之 YOLOv3
目標檢測網絡之 YOLOv3 - 康行天下 - 博客園
訓練
本地環境:TensorFlow 1.15.3
Python 3.7
Keras 2.1.5
下載算法
yolo v3 的算法版本比較多,我建議大家選用是qqwweee的keras版本,復現比較容易,代碼相對來說比較容易理解。
github地址:
https://github.com/qqwweee/keras-yolo3
下載.weights結尾的預訓練模型,并將weight文件轉為h5文件
-:https://pjreddie.com/media/files/yolov3.weights。
新建weight文件夾,將下載的模型放進去。然后修改convert.py文件
將config_path、weightsPath和output_path這個三個參數刪除。如下圖:
修改main函數中的路徑。
def _main(args): config_path = "yolov3.cfg" weights_path = "weight/yolov3.weights" assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format( config_path) assert weights_path.endswith( '.weights'), '{} is not a .weights file'.format(weights_path) output_path = "weight/yolov3.h5"
修改完成后點擊運行。
修改類別。
yolo默認使用的類別文件是coco_classes.txt,所以我們需要將此文件的類別修改為數據集的類別。本例使用的數據集有兩個類別,分別是aircraft和oiltank。
Labelme標注的數據集轉為yolov3訓練的數據集。
增加labelme2txt.py文件
from os import getcwd import os import json import glob wd = getcwd() "labelme標注的json 數據集轉為keras 版yolov3的訓練集" classes = ["aircraft","oiltank"] image_ids = glob.glob(r"LabelmeData/*jpg") print(image_ids) list_file = open('train.txt', 'w') def convert_annotation(image_id, list_file): jsonfile=open('%s.json' % (image_id)) in_file = json.load(jsonfile) for i in range(0,len(in_file["shapes"])): object=in_file["shapes"][i] cls=object["label"] points=object["points"] xmin=int(points[0][0]) ymin=int(points[0][1]) xmax=int(points[1][0]) ymax=int(points[1][1]) if cls not in classes: print("cls not in classes") continue cls_id = classes.index(cls) b = (xmin, ymin, xmax, ymax) list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id)) jsonfile.close() for image_id in image_ids: list_file.write('%s.jpg' % (image_id.split('.')[0])) convert_annotation(image_id.split('.')[0], list_file) list_file.write('\n') list_file.close()
生成的train.txt內容如下:
每張圖片是x1,y1,x2,y2 class 組成的字符串。
執行Kmeans.py文件計算anchors。
打開Kmeans.py文件,修改self.filename = "train.txt",然后運行,計算的結果會直接覆蓋到yolo_anchors.txt
修改train.py文件。
annotation_path = 'train.txt' classes_path = 'model_data/coco_classes.txt' anchors_path = 'model_data/yolo_anchors.txt'
model = create_model(input_shape, anchors, num_classes, freeze_body=2, weights_path='weight/yolov3.h5')
這幾個文件的路徑按照上面文件的存放位置和名稱修改。
注意57行和76行的batch_size按照電腦的配置去修改。
完成上面的步驟就可以開始訓練了,點擊run,開始訓練。
測試
修改yolo.py
mode_path 修改為最終模型的路徑:
"model_path": 'logs/000/trained_weights_final.h5',
修改yolo_video.py
刪除以下參數
parser.add_argument( '--model', type=str, help='path to model weight file, default ' + YOLO.get_defaults("model_path") ) parser.add_argument( '--anchors', type=str, help='path to anchor definitions, default ' + YOLO.get_defaults("anchors_path") ) parser.add_argument( '--classes', type=str, help='path to class definitions, default ' + YOLO.get_defaults("classes_path") ) parser.add_argument( '--gpu_num', type=int, help='Number of GPU to use, default ' + str(YOLO.get_defaults("gpu_num")) )
將image參數改為true
parser.add_argument( '--image', default=True, action="store_true", help='Image detection mode, will ignore all positional arguments' )
修改detect_img的img路徑 def detect_img(yolo): while True: img ="D:\keras-yolo3-master\LabelmeData/aircraft_4.jpg"
測試結果
本文實例:keras-yolo3-master.rar-深度學習文檔類資源-CSDN下載
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。