https://github.com/YunYang1994/tensorflow-yolov3
This is the big brother of the source code can be used to directly, you first need two data sets, one test, one is used for training.
Then we generate some documents on these two data sets, mainly because some documents core / config file inside needed
First voc_train.txt, the format of the file path name of the class left corner of the lower right corner coordinates
We first need to generate a class file name
You need a script to read the annotations folder name generated class file is saved in the data / classes directory
import os
import re
import xml.etree.cElementTree as et
annotations_path = "D:/Tensorflow/VOCtest_06-Nov-2007/VOCdevkit/VOC2007/Annotations/"
save_all_class_name = []
if __name__ == "__main__":
for filanama in os.listdir(annotations_path):
filename = annotations_path + filanama
e = et.parse(filename).getroot()
str_one_line = "D:/Tensorflow/VOCtest_06-Nov-2007/VOCdevkit/VOC2007/JPEGImages/"
for atype_2 in e.findall('object'):
for atype_3 in atype_2.findall("name"):
if atype_3.text not in save_all_class_name:
save_all_class_name.append(atype_3.text)
with open('D:/Tensorflow/tensorflow-yolov3-master/data/classes/my_voc.names', 'a+') as f_names:
for i in save_all_class_name:
f_names.writelines(i+"\n")
f_names.close()
My_voc_train generate a file, read voc2007 data set of annotations inside a folder with a script, then train marked file
annotations_path = "D:/Tensorflow/VOCtest_06-Nov-2007/VOCdevkit/VOC2007/Annotations/"
save_all_class_name = []
all_class_name = []
dict_all_class_name = {}
str_all_line = []
with open('D:/Tensorflow/tensorflow-yolov3-master/data/classes/voc_2007_nov/my_voc.names', 'r') as f:
while True:
buffer_text = f.readline()
buffer_text = buffer_text.rstrip("\n")
if buffer_text != '':
all_class_name.append(buffer_text)
if not buffer_text:
break
count = 0
for i in all_class_name:
dict_all_class_name[i] = count
count += 1
print(dict_all_class_name)
if __name__ == "__main__":
for filanama in os.listdir(annotations_path):
filename = annotations_path + filanama
e = et.parse(filename).getroot()
str_one_line = "D:/Tensorflow/VOCtest_06-Nov-2007/VOCdevkit/VOC2007/JPEGImages/"
for atype in e.findall('filename'):
str_one_line += atype.text
print(atype.text)
str_one_line += " "
for atype_2 in e.findall('object'):
for atype_4 in atype_2.findall("bndbox"):
atype_xmin = atype_4.findall("xmin")[0].text
atype_ymin = atype_4.findall("ymin")[0].text
atype_xmax = atype_4.findall("xmax")[0].text
atype_ymax = atype_4.findall("ymax")[0].text
str_one_line += atype_xmin
str_one_line += ","
str_one_line += atype_ymin
str_one_line += ","
str_one_line += atype_xmax
str_one_line += ","
str_one_line += atype_ymax
str_one_line += ","
for atype_3 in atype_2.findall("name"):
str_one_line += str(dict_all_class_name[atype_3.text])
str_one_line += " "
if atype_3.text not in save_all_class_name:
save_all_class_name.append(atype_3.text)
str_all_line.append(str_one_line)
with open("D:/Tensorflow/tensorflow-yolov3-master/data/dataset/my_voc_test.txt", 'a+') as ff:
for uu in str_all_line:
ff.writelines(uu)
ff.writelines("\n")
ff.close()
The same test suite used to generate annotation files, named after the name of self-generated content as follows
If you want to test to see whether the generated file labeled correctly, you can use a script to test
import cv2
import os
import re
all_class_name = []
dict_all_class_name = {}
with open('D:/Tensorflow/tensorflow-yolov3-master/data/classes/voc_2007_nov/my_voc.names', 'r') as f:
while True:
buffer_text = f.readline()
buffer_text = buffer_text.rstrip("\n")
if buffer_text != '':
all_class_name.append(buffer_text)
if not buffer_text:
break
count = 0
for i in all_class_name:
dict_all_class_name[count] = i
count += 1
print(dict_all_class_name)
with open('D:/Tensorflow/tensorflow-yolov3-master/data/dataset/my_voc_test.txt', 'r') as f:
while True:
a = f.readline()
pattern = re.compile('(\d+.jpg).+', re.S)
result = re.findall(pattern, a)
#每张图片
if len(result) != 0:
image = cv2.imread("D:/Tensorflow/VOCtest_06-Nov-2007/VOCdevkit/VOC2007/JPEGImages/"
+ result[0])
pattert_2 = re.compile('\s?(\d+,\d+,\d+,\d+),\d+', re.S)
pattert_2_2 = re.compile('\s?\d+,\d+,\d+,\d+,(\d+)', re.S)
result_2 = re.findall(pattert_2, a)
result_2_2 = re.findall(pattert_2_2, a)
pic_count = 0
for pt_data in result_2:
pattert_3 = re.compile('(\d+,\d+)', re.S)
oop = re.findall(pattert_3, pt_data)
#每两个点
pt1_x = int(oop[0].split(',')[0])
pt1_y = int(oop[0].split(',')[1])
pt2_x = int(oop[1].split(',')[0])
pt2_y = int(oop[1].split(',')[1])
# pt2 = oop.split(',')[1]
in_class = int(result_2_2[pic_count])
buffer_class = dict_all_class_name[in_class]
pic_count += 1
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image, buffer_class, (50, 300), font, 0.8, (0, 0, 255), 2)
cv2.rectangle(image, (pt1_x, pt1_y), (pt2_x, pt2_y), (0, 255, 0), 2)
cv2.imshow(str(result[0]), image)
cv2.waitKey(2000)
cv2.destroyAllWindows()
else:
pass
if not a:
break
Results are as follows
, Confirmed that no problem, so that you can be trained to a data set, do not want to change the network model, then directly open train.py start training, we need to change it in the config file before training
These are previously generated, according to their own needs additional graphics change it batchsize, for voc data sets and darknet, my 2080batchsize 6
Then they can train, and about half a day and more time, to see their own graphics and data sets.
After the train will generate some ckpt file
Pb need to ckpt documents into files, call
Form
When used directly pb can be used to identify a file, use the time to num_class into their own data sets num_class
The feeling of speed is not fast, but also for the large oak prime target detection industry is not good, being tested shufflenet2 replace darknet53, for 5000 * 5000 pixels, but since there is no marked staff only another day to write a