当前位置:网站首页>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.03、 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 ~
边栏推荐
- EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
- Yyds dry inventory MySQL RC transaction isolation level implementation
- Irradiance, Joule energy, exercise habits
- China PBAT resin Market Forecast and Strategic Research Report (2022 Edition)
- In aks, use secret in CSI driver mount key vault
- RadHat搭建内网YUM源服务器
- Is the software of futures pioneer formal and safe? Which futures company is safer to choose?
- 中国PBAT树脂市场预测及战略研究报告(2022版)
- Develop those things: easycvr cluster device management page function display optimization
- Sword finger offer II 105 Maximum area of the island
猜你喜欢

SQL injection vulnerability (MySQL and MSSQL features)

2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers

Penetration practice vulnhub range Keyring

Apache iceberg source code analysis: schema evolution

Mysql database - Advanced SQL statement (2)

Sword finger offer 20 String representing numeric value

Good looking UI mall source code has been scanned, no back door, no encryption

Intelligent operation and maintenance practice: banking business process and single transaction tracking

Euler function: find the number of numbers less than or equal to N and coprime with n

Detailed explanation of string's trim() and substring()
随机推荐
Source code of new campus errand / campus task platform on mutual station
Euler function: find the number of numbers less than or equal to N and coprime with n
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
整形数组合并【JS】
Petrv2: a unified framework for 3D perception of multi camera images
People help ant help task platform repair source code
The reviewboard has 500 errors when submitting a review. Solutions
Encryption and decryption of tinyurl in leetcode
Product service, operation characteristics
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
中国乙腈市场预测与战略咨询研究报告(2022版)
Why should you consider using prism
[PHP foundation] realize the connection between PHP and SQL database
Openlayers 自定义气泡框以及定位到气泡框
網上股票開戶安全嗎?是否可靠?
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
RadHat搭建内网YUM源服务器
反射型XSS漏洞
Key points on February 15, 2022