当前位置:网站首页>Yolov5 practice: teach object detection by hand
Yolov5 practice: teach object detection by hand
2022-07-01 17:50:00 【Huawei cloud developer community】
Abstract :YOLOv5 It's not a single model , It's a family of models , It includes YOLOv5s、YOLOv5m、YOLO...
This article is shared from Huawei cloud community 《YoloV5 actual combat : Teach object detection by hand ——YoloV5》, author : AI Ho .
Abstract
YOLOV5 Strictly speaking, it's not YOLO The fifth version of , Because it didn't get YOLO The father of Joe Redmon Recognition , But the overall performance of the test data is good . The details are as follows
YOLOv5 It's not a single model , It's a family of models , It includes YOLOv5s、YOLOv5m、YOLOv5l、YOLOv5x、YOLOv5x+TTA, It's a bit like this EfficientDet. Because I couldn't find it V5 The paper of , We can only learn it from the code . In general and YOLOV4 almost , Think of it as YOLOV5 The enhanced .
Project address :GitHub - ultralytics/yolov5: YOLOv5 in PyTorch > ONNX > CoreML > TFLite
Training
1、 Download code
Project address :https://github.com/ultralytics/YOLOv5, Recently, the author has updated some code .
2、 Configuration environment
matplotlib>=3.2.2numpy>=1.18.5opencv-python>=4.1.2pillowPyYAML>=5.3scipy>=1.4.1tensorboard>=2.2torch>=1.6.0torchvision>=0.7.0tqdm>=4.41.0
3、 Prepare the dataset
The dataset uses Labelme The data format of the annotation , Dataset from RSOD Two kinds of data sets, aircraft and oil tank, are obtained from the data set , And turn it into Labelme Annotated datasets .
The address of the dataset : https://pan.baidu.com/s/1iTUpvA9_cwx1qiH8zbRmDg
Extraction code :gr6g
perhaps :LabelmeData.zip_yolov5 actual combat - Deep learning document resources -CSDN download
Decompress the downloaded data set and put it in the root directory of the project . Prepare for the next step of generating test data sets . Here's the picture :
4、 Generate data set
YoloV5 The data set of is not the same as that of previous versions , Let's take a look at the converted dataset first .
The data structure is as follows :
images Folder storage train and val Pictures of the
labels Inside the store train and val Object data for , Each of them txt Document and images The pictures inside are one-to-one .
txt The contents of the document are as follows :
Format : Object category x y w h
Are coordinates real coordinates , It's calculated by dividing the coordinates by the width and height , It's the ratio of width to height .
Let's write the code to generate the data set , newly build LabelmeToYoloV5.py, Then write the following code .
import osimport numpy as npimport jsonfrom glob import globimport cv2from sklearn.model_selection import train_test_splitfrom os import getcwdclasses = ["aircraft", "oiltank"]# 1. Tag path labelme_path = "LabelmeData/"isUseTest = True # Whether to create test Set # 3. Get the pending file files = glob(labelme_path + "*.json")files = [i.replace("\\", "/").split("/")[-1].split(".json")[0] for i in files]print(files)if isUseTest: trainval_files, test_files = train_test_split(files, test_size=0.1, random_state=55)else: trainval_files = files# splittrain_files, val_files = train_test_split(trainval_files, test_size=0.1, random_state=55)def convert(size, box): dw = 1. / (size[0]) dh = 1. / (size[1]) x = (box[0] + box[1]) / 2.0 - 1 y = (box[2] + box[3]) / 2.0 - 1 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)wd = getcwd()print(wd)def ChangeToYolo5(files, txt_Name): if not os.path.exists('tmp/'): os.makedirs('tmp/') list_file = open('tmp/%s.txt' % (txt_Name), 'w') for json_file_ in files: json_filename = labelme_path + json_file_ + ".json" imagePath = labelme_path + json_file_ + ".jpg" list_file.write('%s/%s\n' % (wd, imagePath)) out_file = open('%s/%s.txt' % (labelme_path, json_file_), 'w') json_file = json.load(open(json_filename, "r", encoding="utf-8")) height, width, channels = cv2.imread(labelme_path + json_file_ + ".jpg").shape for multi in json_file["shapes"]: points = np.array(multi["points"]) xmin = min(points[:, 0]) if min(points[:, 0]) > 0 else 0 xmax = max(points[:, 0]) if max(points[:, 0]) > 0 else 0 ymin = min(points[:, 1]) if min(points[:, 1]) > 0 else 0 ymax = max(points[:, 1]) if max(points[:, 1]) > 0 else 0 label = multi["label"] if xmax <= xmin: pass elif ymax <= ymin: pass else: cls_id = classes.index(label) b = (float(xmin), float(xmax), float(ymin), float(ymax)) bb = convert((width, height), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') print(json_filename, xmin, ymin, xmax, ymax, cls_id)ChangeToYolo5(train_files, "train")ChangeToYolo5(val_files, "val")ChangeToYolo5(test_files, "test")
This code will be executed in LabelmeData Generate... For each image txt Annotation data , At the same time tmp Create the training set under the folder 、 Verification set and test set txt,txt It records the path of the image , Generate for next step YoloV5 Data sets for training and testing . stay tmp New under the folder MakeData.py file , Generate the final result , The directory structure is shown in the figure below :
open MakeData.py, Write the following code .
import shutilimport osfile_List = ["train", "val", "test"]for file in file_List: if not os.path.exists('../VOC/images/%s' % file): os.makedirs('../VOC/images/%s' % file) if not os.path.exists('../VOC/labels/%s' % file): os.makedirs('../VOC/labels/%s' % file) print(os.path.exists('../tmp/%s.txt' % file)) f = open('../tmp/%s.txt' % file, 'r') lines = f.readlines() for line in lines: print(line) line = "/".join(line.split('/')[-5:]).strip() shutil.copy(line, "../VOC/images/%s" % file) line = line.replace('JPEGImages', 'labels') line = line.replace('jpg', 'txt') shutil.copy(line, "../VOC/labels/%s/" % file)
After execution, it can generate YoloV5 The data set used for training . give the result as follows :
5、 Modify configuration parameters
open voc.yaml file , Modify the configuration parameters inside train: VOC/images/train/ # The path of the training set picture val: VOC/images/val/ # Verify the path of the image set # number of classesnc: 2 # Category of detection , This dataset has two categories, so write 2# class namesnames: ["aircraft", "oiltank"]# The name of the category , And the transformation of data sets list Corresponding
6、 modify train.py Parameters of
cfg Parameter is YoloV5 The configuration file for the model , The model files are stored in models Under the folder , Fill in different documents as required .weights Parameter is YoloV5 Pre training model of , and cfg Corresponding , example :cfg The configuration is yolov5s.yaml,weights We need to configure yolov5s.ptdata Is the configuration file for the configuration dataset , We chose voc.yaml, So configuration data/voc.yaml Modify the above three parameters to start training , Other parameters are modified according to their own needs . The modified parameter configuration is as follows :parser.add_argument('--weights', type=str, default='yolov5s.pt', help='initial weights path')parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml path')parser.add_argument('--data', type=str, default='data/voc.yaml', help='data.yaml path')
After the modification is completed , You can start training . As shown in the figure below :
7、 See the training results
After going through 300epoch After training , We will be in runs Find the training weight file and some files of the training process under the folder . Pictured :
test
The first thing you need to do is voc.yaml Add the path of test set in , open voc.yaml, stay val Add... After the field test: tmp/test.txt This line of code , Pictured :
modify test.py Parameters in , The following parameters need to be modified .
parser = argparse.ArgumentParser(prog='test.py')
parser.add_argument('--weights', nargs='+', type=str, default='runs/exp7/weights/best.pt', help='model.pt path(s)')
parser.add_argument('--data', type=str, default='data/voc.yaml', help='*.data path')
parser.add_argument('--batch-size', type=int, default=2, help='size of each image batch')
parser.add_argument('--save-txt', default='True', action='store_true', help='save results to *.txt')
stay 275 That's ok modify test Methods , Add a path to save test results . So that when the test is done, it can be done in inference\images See the picture of the test , stay inference\output You can see the saved test results in .
Pictured :
Here are the results of the run :
Click to follow , The first time to learn about Huawei's new cloud technology ~
边栏推荐
- Can hero sports go public against the wind?
- Encryption and decryption of tinyurl in leetcode
- 中国生物降解塑料市场预测与投资战略报告(2022版)
- The latest intelligent factory MES management system software solution
- Petrv2: a unified framework for 3D perception of multi camera images
- 中国一次性卫生用品生产设备行业深度调研报告(2022版)
- MySQL -- explain performance optimization
- Alibaba cloud Li Feifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- ACL 2022 | decomposed meta learning small sample named entity recognition
猜你喜欢
String的trim()和substring()详解
Data warehouse (3) star model and dimension modeling of data warehouse modeling
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
(1) CNN network structure
Intelligent operation and maintenance practice: banking business process and single transaction tracking
Rotation order and universal lock of unity panel
How to write good code - Defensive Programming Guide
【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
ISO 27001 Information Security Management System Certification
Sword finger offer 20 String representing numeric value
随机推荐
2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
Nielseniq found that 60% of the re launched products had poor returns
期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
Vulnhub range hacker_ Kid-v1.0.1
Penetration practice vulnhub range Tornado
Sword finger offer II 105 Maximum area of the island
MySQL + JSON = King fried
Why should you consider using prism
(十七)DAC转换实验
LeetCode中等题之TinyURL 的加密与解密
Develop those things: easycvr cluster device management page function display optimization
transform. Forward and vector3 Differences in the use of forward
(12) About time-consuming printing
An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
Reflective XSS vulnerability
[C supplement] [string] display the schedule of a month by date
In depth Research Report on China's disposable sanitary products production equipment industry (2022 Edition)
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say