本工程包含AI模型训练,预测的自动机器学习库
先看效果
lol英雄目标检测
Ai框架请下载
基础环境
复制代码
1
2pip install -r requirements.txt
准备数据集
数据标注工具
- 分类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 数据集结构如下 # main_directory/ # ...class_a/ # ......a_image_1.jpg # ......a_image_2.jpg # ...class_b/ # ......b_image_1.jpg # ......b_image_2.jpg
- 目标检测
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20# 数据集结构如下 # main_directory/ # ...images/ # ......a_image_1.jpg # ......a_image_2.jpg # ...labels/ # ......b_image_1.txt # ......b_image_2.txt # ...classes.txt # ...notes.json
训练任务
参考上面准备数据集
拉取配置
复制代码
1
2
3
4
5$ python3 -m utils.automlUtils.base get {dataset_path:例如./main_directory} class # 拉取分类任务的配置信息 $ python3 -m utils.automlUtils.base get {dataset_path:例如./main_directory} detectioon # 拉取目标检测任务的配置信息 config.yaml 请参考配置文件修改或者添加配置信息
训练任务
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100import cv2 import numpy as np def 图像分类预测(): from utils.automlUtils import Config, ImageClassifierConfig, ImageClassifierPredictor conf = ImageClassifierConfig(**(Config.json())) print(conf) import cv2 modeler = ImageClassifierPredictor(config=conf) import time for url in ["http://imgservice.suning.cn/uimg1/b2c/image/i3sVxZZHr9SbY4TpH1SmvQ.jpg"]: start_time = time.time() img = modeler.url2numpy(url=url) img = cv2.resize(img, (256, 256)) predict_result = modeler.predict(x=np.array([img])) print("inference time: ", time.time() - start_time) print("predict_result:", predict_result) post_result = modeler.post(predict_result)[0] print("post_result", post_result) print("-" * 20) # print(label, post_result, label == post_result) # inference # time: 0.7593982219696045 # predict_result: [[0.38690686 0.23805034 0.26550755 0.10953525]] # post_result # {'class': 'BabyPants', 'confidence': 0.38690686} for path in ["./img.png"]: start_time = time.time() img = modeler.file2numpy(path=path) img = cv2.resize(img, (256, 256)) predict_result = modeler.predict(x=np.array([img])) print("inference time: ", time.time() - start_time) print("predict_result:", predict_result) post_result = modeler.post(predict_result)[0] print("post_result", post_result) print("-" * 20) # print(label, post_result, label == post_result) # inference # time: 0.0785212516784668 # predict_result: [[0.02243473 0.00355711 0.9482294 0.02577879]] # post_result # {'class': 'womencasualshoes', 'confidence': 0.9482294} def 图片分类训练(): """ 数据集下载地址:https://autogluon.s3.amazonaws.com/datasets/shopee-iet.zip """ from utils.automlUtils import Config, ImageClassifierConfig, ImageClassifierPredictor conf = ImageClassifierConfig(**(Config.json())) print(conf) ImageClassifierPredictor(config=conf).train_and_export() def 图片检测训练(): from utils.automlUtils import Config, ImageDetectorPredictor, ImageDetectorConfig conf = ImageDetectorConfig(**(Config.json())) modeler = ImageDetectorPredictor(config=conf) modeler.train_and_export() def 图片检测预测(): from utils.automlUtils import Config, ImageDetectorPredictor, ImageDetectorConfig conf = ImageDetectorConfig(**(Config.json())) modeler = ImageDetectorPredictor(config=conf) import time for url in ["http://n.sinaimg.cn/spider2020824/264/w1080h784/20200824/8508-iyaiihm7246958.jpg"]: img = modeler.url2Image(url=url) start_time = time.time() predict_result = modeler.predict(x=[img], size=640) print("inference time: ", time.time() - start_time) print("predict_result:", predict_result) post_result = modeler.post(predict_result) post_result[0].update({"name": "行人"}) img = modeler.draw(img, pred_val=post_result, size=40, show=True) print("post_result", post_result) print("-" * 20) def show(path="/Users/rockontrol/Desktop/lol1.mp4"): from utils.automlUtils import Config, ImageDetectorPredictor, ImageDetectorConfig conf = ImageDetectorConfig(**(Config.json())) modeler = ImageDetectorPredictor(config=conf) video = cv2.VideoCapture(path) import time while video.isOpened(): ret, frame = video.read() if not ret: break img = modeler.numpy2Image(img=frame) start_time = time.time() predict_result = modeler.predict(x=[img], size=640) print("inference time: ", time.time() - start_time) print("predict_result:", predict_result) post_result = modeler.post(predict_result) img = modeler.draw(img, pred_val=post_result, size=40, show=False) frame = np.array(img) cv2.imshow('frame', frame) # 若没有按下q键,则每1毫秒显示一帧 if cv2.waitKey(1) & 0xFF == ord('q'): break # 所有操作结束后不要忘记释放 video.release() cv2.destroyWindow("frame") if __name__ == '__main__': import fire fire.Fire()
- 训练
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286$ python3 main.py 图片分类训练 ... $ python3 main.py 图像分类预测 ... $ python3 main.py 图片检测训练 2 -1 1 18816 yolov5.models.common.C3 [64, 64, 1] 3 -1 1 73984 yolov5.models.common.Conv [64, 128, 3, 2] 4 -1 2 115712 yolov5.models.common.C3 [128, 128, 2] 5 -1 1 295424 yolov5.models.common.Conv [128, 256, 3, 2] 6 -1 3 625152 yolov5.models.common.C3 [256, 256, 3] 7 -1 1 1180672 yolov5.models.common.Conv [256, 512, 3, 2] 8 -1 1 1182720 yolov5.models.common.C3 [512, 512, 1] 9 -1 1 656896 yolov5.models.common.SPPF [512, 512, 5] 10 -1 1 131584 yolov5.models.common.Conv [512, 256, 1, 1] 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 12 [-1, 6] 1 0 yolov5.models.common.Concat [1] 13 -1 1 361984 yolov5.models.common.C3 [512, 256, 1, False] 14 -1 1 33024 yolov5.models.common.Conv [256, 128, 1, 1] 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 16 [-1, 4] 1 0 yolov5.models.common.Concat [1] 17 -1 1 90880 yolov5.models.common.C3 [256, 128, 1, False] 18 -1 1 147712 yolov5.models.common.Conv [128, 128, 3, 2] 19 [-1, 14] 1 0 yolov5.models.common.Concat [1] 20 -1 1 296448 yolov5.models.common.C3 [256, 256, 1, False] 21 -1 1 590336 yolov5.models.common.Conv [256, 256, 3, 2] 22 [-1, 10] 1 0 yolov5.models.common.Concat [1] 23 -1 1 1182720 yolov5.models.common.C3 [512, 512, 1, False] 24 [17, 20, 23] 1 18879 yolov5.models.yolo.Detect [2, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]] Model Summary: 270 layers, 7025023 parameters, 7025023 gradients, 15.9 GFLOPs ... Starting training for 500 epochs... Epoch gpu_mem box obj cls labels img_size 0/499 2.12G 0.07039 0.01503 0.01063 132 640: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:07<00:00, 1.27s/it] Class Images Labels P R mAP@.5 mAP@.5:.95: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:01<00:00, 1.65it/s] all 91 366 0.0024 0.0533 0.00101 0.00015 Epoch gpu_mem box obj cls labels img_size 1/499 2.12G 0.07097 0.01468 0.01075 100 640: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:01<00:00, 3.75it/s] Class Images Labels P R mAP@.5 mAP@.5:.95: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:01<00:00, 2.38it/s] all 91 366 0.00211 0.0266 0.000798 0.000117 Epoch gpu_mem box obj cls labels img_size 2/499 2.12G 0.07008 0.01568 0.01056 143 640: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:01<00:00, 3.57it/s] Class Images Labels P R mAP@.5 mAP@.5:.95: Epoch gpu_mem box obj cls labels img_size 197/499 2.12G 0.03268 0.01033 0.003047 111 640: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:01<00:00, 4.10it/s] Class Images Labels P R mAP@.5 mAP@.5:.95: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:00<00:00, 4.10it/s] all 91 366 0.804 0.867 0.829 0.253 Epoch gpu_mem box obj cls labels img_size 198/499 2.12G 0.02963 0.009621 0.003108 141 640: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:01<00:00, 3.65it/s] Class Images Labels P R mAP@.5 mAP@.5:.95: $ python3 main.py 图片检测预测 inference time: 0.20399999618530273 post_result: [{'bbox': [385.8732604980469, 107.14449310302734, 447.9578857421875, 170.21142578125], 'confidence': 0.38369208574295044, 'name': '英雄', 'cls': 0}, {'bbox': [537.8407592773438, 118.21878814697266, 611.421142578125, 1813377075195312], 'confidence': 0.38793814182281494, 'name': '小兵', 'cls': 1}, {'bbox': [366.82672119140625, 226.30960083007812, 450.38079833984375, 297.9781188964844], 'confidence': 0.46867531538009644, 'name': '小兵', 'cls': 1}, {'': [383.04864501953125, 105.42671966552734, 450.8124084472656, 167.3153533935547], 'confidence': 0.5011225342750549, 'name': '小兵', 'cls': 1}, {'bbox': [486.0379638671875, 162.92074584960938, 650.8978881835938, 348.2691955566406], onfidence': 0.6519156098365784, 'name': '英雄', 'cls': 0}] /Users/rockontrol/Desktop/python_code/venv/lib/python3.8/site-packages/torch/autocast_mode.py:141: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling warnings.warn('User provided device_type of 'cuda', but CUDA is not available. Disabling') inference time: 0.19359803199768066 post_result: [{'bbox': [386.54046630859375, 105.60713195800781, 448.0963439941406, 171.9403076171875], 'confidence': 0.32672685384750366, 'name': '英雄', 'cls': 0}, {'bbox': [537.49169921875, 118.83441925048828, 611.8263549804688, 1.37303161621094], 'confidence': 0.4074070155620575, 'name': '小兵', 'cls': 1}, {'bbox': [366.115966796875, 226.2453155517578, 449.3259582519531, 295.8804626464844], 'confidence': 0.41025012731552124, 'name': '小兵', 'cls': 1}, {'bbo[383.3480529785156, 105.75922393798828, 450.8968200683594, 167.15966796875], 'confidence': 0.5280213356018066, 'name': '小兵', 'cls': 1}, {'bbox': [486.89239501953125, 163.3772430419922, 648.9190673828125, 344.3421325683594], 'confince': 0.6505584716796875, 'name': '英雄', 'cls': 0}] ...
建议训练时,使用GPU训练
训练时用到的数据集链接: https://pan.baidu.com/s/1VO1CxCiWBRnvNXW1HgNgjg 密码: sea7
最后
以上就是彩色羊最近收集整理的关于自动机器学习实战本工程包含AI模型训练,预测的自动机器学习库先看效果Ai框架请下载基础环境准备数据集训练任务的全部内容,更多相关自动机器学习实战本工程包含AI模型训练,预测内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复