第十一节,利用yolov3训练自己的数据集
?
1、環境配置
tensorflow1.12.0
Opencv3.4.2
keras
pycharm
2、配置yolov3
- 下載yolov3代碼:https://github.com/qqwweee/keras-yolo3
- 下載權重:https://pjreddie.com/media/files/yolov3.weights,并將權重文件放在keras-yolo3-master文件下
-
執行如下命令將darknet下的yolov3配置文件轉換成keras適用的h5文件。
? ? ? ??? ? ?python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5
更改了一下代碼:重新編寫了一個測試代碼object_detection_yolo.py
# This code is written at BigVision LLC. It is based on the OpenCV project. It is subject to the license terms in the LICENSE file found in this distribution and at http://opencv.org/license.html# Usage example: python3 object_detection_yolo.py --video=run.mp4 # python3 object_detection_yolo.py --image=bird.jpgimport cv2 as cv import argparse import sys import numpy as np import os.path# Initialize the parameters confThreshold = 0.5 # Confidence threshold nmsThreshold = 0.4 #Non-maximum suppression threshold inpWidth = 416 #Width of network's input image inpHeight = 416 #Height of network's input image parser = argparse.ArgumentParser(description='Object Detection using YOLO in OPENCV') parser.add_argument('--image', help='Path to image file.') parser.add_argument('--video', help='Path to video file.') args = parser.parse_args()# Load names of classes classesFile = "model_data/coco_classes.txt"; classes = None # with open(classesFile, 'rt') as f: # classes = f.read().rstrip('\n').split('\n') classes_path = os.path.expanduser(classesFile) with open(classes_path) as f:class_names = f.readlines()classes = [c.strip() for c in class_names]# Give the configuration and weight files for the model and load the network using them. modelConfiguration = "yolov3.cfg"; modelWeights = "yolov3.weights";net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights) net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV) net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)# Get the names of the output layers def getOutputsNames(net):# Get the names of all the layers in the networklayersNames = net.getLayerNames()# Get the names of the output layers, i.e. the layers with unconnected outputsreturn [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]# Draw the predicted bounding box def drawPred(classId, conf, left, top, right, bottom):# Draw a bounding box.cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)label = '%.2f' % conf# Get the label for the class name and its confidenceif classes:assert(classId < len(classes))label = '%s:%s' % (classes[classId], label)#Display the label at the top of the bounding boxlabelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)top = max(top, labelSize[1])cv.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine), (255, 255, 255), cv.FILLED)cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0), 1)# Remove the bounding boxes with low confidence using non-maxima suppression def postprocess(frame, outs):frameHeight = frame.shape[0]frameWidth = frame.shape[1]classIds = []confidences = []boxes = []# Scan through all the bounding boxes output from the network and keep only the# ones with high confidence scores. Assign the box's class label as the class with the highest score.classIds = []confidences = []boxes = []for out in outs:for detection in out:scores = detection[5:]classId = np.argmax(scores)confidence = scores[classId]if confidence > confThreshold:center_x = int(detection[0] * frameWidth)center_y = int(detection[1] * frameHeight)width = int(detection[2] * frameWidth)height = int(detection[3] * frameHeight)left = int(center_x - width / 2)top = int(center_y - height / 2)classIds.append(classId)confidences.append(float(confidence))boxes.append([left, top, width, height])# Perform non maximum suppression to eliminate redundant overlapping boxes with# lower confidences.indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)for i in indices:i = i[0]box = boxes[i]left = box[0]top = box[1]width = box[2]height = box[3]drawPred(classIds[i], confidences[i], left, top, left + width, top + height)# Process inputs winName = 'Deep learning object detection in OpenCV' #cv.namedWindow(winName, cv.WINDOW_NORMAL) outputFile = "yolo_out_py.avi" if (args.image):# Open the image fileif not os.path.isfile(args.image):print("Input image file ", args.image, " doesn't exist")sys.exit(1)cap = cv.VideoCapture(args.image)outputFile = args.image[:-4]+'_yolo_out_py.jpg' elif (args.video):# Open the video fileif not os.path.isfile(args.video):print("Input video file ", args.video, " doesn't exist")sys.exit(1)cap = cv.VideoCapture(args.video)outputFile = args.video[:-4]+'_yolo_out_py.avi' else:# Webcam inputcap = cv.VideoCapture(0)# Get the video writer initialized to save the output video if (not args.image):vid_writer = cv.VideoWriter(outputFile, cv.VideoWriter_fourcc('M','J','P','G'), 30, (round(cap.get(cv.CAP_PROP_FRAME_WIDTH)),round(cap.get(cv.CAP_PROP_FRAME_HEIGHT))))while cv.waitKey(1) < 0:# get frame from the videohasFrame, frame = cap.read()# Stop the program if reached end of videoif not hasFrame:print("Done processing !!!")print("Output file is stored as ", outputFile)cv.waitKey(3000)break# Create a 4D blob from a frame.blob = cv.dnn.blobFromImage(frame, 1/255, (inpWidth, inpHeight), [0,0,0], 1, crop=False)# Sets the input to the network net.setInput(blob)# Runs the forward pass to get output of the output layersouts = net.forward(getOutputsNames(net))# Remove the bounding boxes with low confidence postprocess(frame, outs)# Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)t, _ = net.getPerfProfile()label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))# Write the frame with the detection boxesif (args.image):cv.imwrite(outputFile, frame.astype(np.uint8));else:vid_writer.write(frame.astype(np.uint8))#cv.imshow(winName, frame) View Code?
?
3、用自己的數據集訓練
- 在工程下新建一個文件夾VOCdevkit,結構與VOC數據集格式保持一致,目錄結構如下所示:
將自己的數據圖片放入JPEFImages文件中,
-
生成Annotation下的文件,安裝工具labelImg。安裝過程可參照:
https://blog.csdn.net/u012746060/article/details/81016993,結果如下圖:
- 生成ImageSet/Main/4個文件,在VOC2007下新建一個test.py文件:
?運行代碼之后,生成如下文件,VOC2007數據集制作完成。
-
生成yolo3所需的train.txt,val.txt,test.txt
生成的數據集不能供yolov3直接使用。需要運行voc_annotation.py(遷移項目時必須重新運行,涉及路徑問題) ,classes以檢測兩個類為例(redlight和greenlight),在voc_annotation.py需改你的數據集為:
運行之后,生成如下三個文件:
?文件內容如圖所示:
?
-
修改參數文件yolo3.cfg
? ? ? ? ? ? ? ? ?打開yolo3.cfg文件。搜索yolo(共出現三次),每次按下圖都要修改:
filter:3*(5+len(classes))
? ? ? ? ? ? ? ? ? classes:你要訓練的類別數(我這里是訓練兩類)?
? ? ? ? ? ? ? ? ? random:原來是1,顯存小改為0
- 修改model_data下的voc_classes.txt為自己訓練的類別
- ?修改train.py代碼(用下面代碼直接替換原來的代碼)
?
?替換完成后,千萬千萬值得注意的是,因為程序中有logs/000/目錄,你需要創建這樣一個目錄,這個目錄的作用就是存放自己的數據集訓練得到的模型。不然程序運行到最后會因為找不到該路徑而發生錯誤。
轉載于:https://www.cnblogs.com/wyx501/p/10644248.html
總結
以上是生活随笔為你收集整理的第十一节,利用yolov3训练自己的数据集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 踩坑rosbag --clock
- 下一篇: vue 进行ajax请求,使用axios