YOLOV3 Training Your own data

Source: Internet
Author: User

1. Download the YOLOv3 of the official website and open the terminal input: Git clone https://github.com/pjreddie/darknet

After the download is complete, enter: CD darknet, and then enter: Make,

When make is finished, download the pre-trained weights file by typing: wget https://pjreddie.com/media/files/yolov3.weights in the terminal, then you can run the detector and enter it in the terminal:. Darknet Detect cfg/yolov3.cfg yolov3.weights data/doa.jpg (This command runs in the darknet directory), you get the result:



2. Start training your own data

(1) Create a new VOC named folder under the Darknet directory, create a new Vocdevkit folder in the VOC folder, create a new VOC2018 folder in the Vocdevkit folder, create a new VOC2018 under the annotations folder, Imagesets,jpegimages,segmentationclass,segmentationobject these five folders, create a new main folder under the Imagesets folder. where annotations store all the image of the XML file, Jpegimages folder to store all the pictures, main in Train.txt and Test.txt, as for Segmentationclass, Segmentationobject these two folders I didn't use.

(2) Image renaming, using VOC naming method, this: 000012. Rename the code as follows, modify it according to your own path to use:

#-*-Coding:utf-8-*-
Import OS
Path = "/home/f/image/aft_original_crack_dataset_second"
filelist = Os.listdir (path) #该文件夹下所有的文件 (including folders)
Count=0
For file in FileList:
Print (file)
For file in FileList: #遍历所有文件
Olddir=os.path.join (Path,file) #原来的文件路径
If Os.path.isdir (olddir): #如果是文件夹则跳过
Continue
Filename=os.path.splitext (file) [0] #文件名
Filetype=os.path.splitext (file) [1] #文件扩展名
Newdir=os.path.join (Path,str (count). Zfill (6) +filetype) #用字符串函数zfill the number of digits required to complete the 0 completion
Os.rename (Olddir,newdir) #重命名
Count+=1

(3) Mark the picture, I use the labelimg. Download URL: https://github.com/tzutalin/labelImg

After the download is complete, install it according to the installation method in this URL (I am executing this last statement: Python labelimg.py times is wrong, then add sudo python labelimg.py execution is OK), and then you can begin to annotate the picture, the labeling method see: 80211220.

(4) Generate Train.txt and test.txt, the content is such: 000606, there is no suffix, my generation is used to test the image will be displayed in the train.txt inside the 001234.J, the same way for the training of the picture in the Test.txt will also be shown, we just need to put this Two. txt files all. J is deleted, otherwise an error will be followed. The Python code follows, depending on your path and the number of photos you have modified, you can use:

#-*-Coding:utf-8-*-
Import OS
From OS import Listdir, GETCWD
From Os.path Import Join
if __name__ = = ' __main__ ':
Source_folder= '/home/f/darknet/voc/vocdevkit/voc2018/jpegimages/' #地址是所有图片的保存地点
dest= '/home/f/darknet/voc/vocdevkit/voc2018/imagesets/main/train.txt ' #保存train. txt address
Dest2= '/home/f/darknet/voc/vocdevkit/voc2018/imagesets/main/test.txt ' #保存test. txt address
File_list=os.listdir (Source_folder) #赋值图片所在文件夹的文件列表
Train_file=open (dest, ' a ') #打开文件
Test_file=open (Dest2, ' a ') #打开文件
For file_obj in File_list: #访问文件列表中的每一个文件
File_path=os.path.join (Source_folder,file_obj)
#file_path保存每一个文件的完整路径
File_name,file_extend=os.path.splitext (File_obj)
#file_name Save the file name, File_extend save the file extension
File_num=int (file_name)
#把每一个文件命str转换为 numeric int Type each file name is made up of four digits, such as 0201 for 201 high 0
if (file_num<1000): #保留1000个文件用于训练


#print File_num
Train_file.write (file_name+ ' \ n ') #用于训练前149个的图片路径保存在train. txt inside, ending with a carriage return and newline
else:
Test_file.write (file_name+ ' \ n ') #其余的文件保存在test. txt inside
Train_file.close () #关闭文件
Test_file.close ()

(5) Download and modify voc_label.py

Download: wget https://pjreddie.com/media/files/voc_label.py

Modify:

Import Xml.etree.ElementTree as ET
Import Pickle
Import OS
From OS import Listdir, GETCWD
From Os.path Import Join

sets=[(' 2018 ', ' Train '), (' 2018 ', ' Test ')] #根据自己的数据修改

classes = ["Bridgecrack"] #根据自己的类别进行修改


def convert (size, box):
DW = 1./size[0]
DH = 1./size[1]
x = (Box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
W = box[1]-box[0]
h = box[3]-box[2]
x = X*DW
W = W*DW
y = Y*dh
H = H*dh
Return (X,Y,W,H)

Def convert_annotation (year, image_id):
In_file = open ('/home/f/darknet/voc/vocdevkit/voc%s/annotations/%s.xml '% (year, image_id)) #根据自己的路径修改
Out_file = open (' Vocdevkit/voc%s/labels/%s.txt '% (year, image_id), ' W ')
Tree=et.parse (In_file)
root = Tree.getroot ()
Size = Root.find (' size ')
w = Int (size.find (' width '). Text)
h = Int (size.find (' height '). Text)

For obj in Root.iter (' object '):
Difficult = Obj.find (' difficult '). Text
CLS = Obj.find (' name '). Text
If CLS not in classes or int (difficult) = = 1:
Continue
cls_id = Classes.index (CLS)
Xmlbox = Obj.find (' Bndbox ')
b = (Float (xmlbox.find (' xmin '). Text), Float (xmlbox.find (' Xmax '). Text), Float (xmlbox.find (' ymin '). Text), float ( Xmlbox.find (' Ymax '). Text))
bb = Convert ((w,h), B)
Out_file.write (str (cls_id) + "+" ". Join ([Str (a) for a in BB]) + ' \ n ')

WD = GETCWD ()

For year, Image_set in sets:
If not os.path.exists (' vocdevkit/voc%s/labels/' percent (year)):
Os.makedirs (' vocdevkit/voc%s/labels/'% (year))
Image_ids = open ('/home/f/darknet/voc/vocdevkit/voc%s/imagesets/main/%s.txt '% (year, image_set)). Read (). Strip (). Split () #根据自己的路径修改
List_file = open ('%s_%s.txt '% (year, Image_set), ' W ')
For image_id in Image_ids:
List_file.write ('%s/vocdevkit/voc%s/jpegimages/%s.jpg\n '% (WD, year, image_id))
Convert_annotation (year, image_id)
List_file.close ()

Run: Enter in Terminal: Python voc_label.py will generate 2018_train.txt and 2018_ in the main folder Test.txt, as well as the folder Vocdevkit (which used the train and test text files under Main, make the XML and JPG file one by one correspond, and generate the final picture path. )

(6) Download pre-training model: Enter in Terminal: Wget https://pjreddie.com/media/files/darknet53.conv.74

(7) Modify Cfg/voc.data

classes= 1 #根据自己的类型修改
Train =/home/f/darknet/voc/2018_train.txt #根据自己的路径修改
valid =/home/f/darknet/voc/2018_test.txt #根据自己的路径修改
names =/home/f/darknet/data/voc.names #根据自己的路径修改
Backup = Backup

(8) Modify Data/voc.names

It's good to list all of your categories:

Car

Cat

(9) Modify Cfg/yolov3-voc.cfg

Altogether modifies three filters,classes, finds each YOLO above filters, as well as YOLO below the classes modifies for own can, modifies according to: Calsses is the classification number, filters=3* (classes+5), Random=0 is to turn off multi-scale training.

The meaning of each parameter, see here: 80907040

(10) Start training

./darknet Detector Train Cfg/voc.data cfg/yolov3-voc.cfg darknet53.conv.74

Training, my newspaper cannot load image error, because my 2018_train.txt inside the picture path there is no photo, so I just press the path inside my picture moved to the corresponding path, there is no error.

(11) test data

Enter in Terminal:./darknet detector test Cfg/voc.data cfg/yolov3-voc.cfg backup/yolov3-voc_600.weights data/001335.jpg

Turn from:
---------------------
Fjy_sunshine
Source: CSDN
Original: 82590440
Copyright NOTICE: This article is for bloggers original article, reprint please attach blog link!

YOLOV3 Training Your own data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.