-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path教女朋友学会用win10+yolov3+python训练自己的模型.html
857 lines (641 loc) · 67.4 KB
/
教女朋友学会用win10+yolov3+python训练自己的模型.html
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="keywords" content="python,matlab,yolo,yolov3,java,go,c++,c#" />
<meta name="description" content="孙全胜의Space" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>
教女朋友学会用win10+yolov3+python训练自己的模型 | 孙全胜의Space
</title>
<meta name="generator" content="hexo-theme-ayer">
<link rel="shortcut icon" href="/bitbug_favicon.ico" />
<link rel="stylesheet" href="/dist/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
<link rel="stylesheet" href="/css/custom.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/pace.min.js"></script>
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-168079169-1', 'auto');
ga('send', 'pageview');
</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?6101cdb197f4b6300cb90fa7e51accec";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<link rel="alternate" href="/atom.xml" title="孙全胜의Space" type="application/atom+xml">
</head>
</html>
<body>
<canvas width="1777" height="841" style="position: fixed; left: 0px; top: 0px; z-index: 999; pointer-events: none;"></canvas>
<div id="app">
<main class="content on">
<section class="outer">
<article id="post-教女朋友学会用win10+yolov3+python训练自己的模型" class="article article-type-post" itemscope
itemprop="blogPost" data-scroll-reveal>
<div class="article-inner">
<header class="article-header">
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
教女朋友学会用win10+yolov3+python训练自己的模型
</h1>
</header>
<div class="article-meta">
<a href="/%E6%95%99%E5%A5%B3%E6%9C%8B%E5%8F%8B%E5%AD%A6%E4%BC%9A%E7%94%A8win10+yolov3+python%E8%AE%AD%E7%BB%83%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A8%A1%E5%9E%8B.html" class="article-date">
<time datetime="2020-06-17T11:18:54.000Z" itemprop="datePublished">2020-06-17</time>
</a>
<div class="article-category">
<a class="article-category-link" href="/categories/python/">python</a> / <a class="article-category-link" href="/categories/python/%E6%9C%BA%E5%99%A8%E8%A7%86%E8%A7%89/">机器视觉</a>
</div>
<div class="word_count">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="ri-quill-pen-line"></i>
<span class="post-meta-item-text"> 字数统计:</span>
<span class="post-count">2.5k</span>
</span>
</span>
<span class="post-time">
|
<span class="post-meta-item-icon">
<i class="ri-book-open-line"></i>
<span class="post-meta-item-text"> 阅读时长≈</span>
<span class="post-count">11 分钟</span>
</span>
</span>
</div>
</div>
<div class="tocbot"></div>
<div class="article-entry" itemprop="articleBody">
<blockquote>
<p>times:2020/3/23<br>操作系统:win10<br>环境:python 3.6<br>因为我之前把所有内容写在一篇文章里非常的乱,所以本文主线是训练自己的 yolo.h5 去识别图像中的人,所有小细节的操作,我都在文中添加了链接,新手的话需要注意看一下。<br>// 有任何的问题都可以直接评论,还有资料的话直接留言邮箱,说明问题//<br>//也可以评论下加下微信询问//<br>大家一起加油学习yolo,之后我会再出一篇详细介绍yolo代码的文章<br>新创建的小小交流群:977947271 想要资料的也可以在群里要哦<br>2020/5/30:</p>
</blockquote>
<p>如果你是 yolo 小白,或者环境配置等一直报错,请先参阅上一篇博文:<a href="https://blog.csdn.net/qq_45504119/article/details/105033492" target="_blank" rel="noopener">keras-yolov3目标检测详解——适合新手</a> (环境配置、用官方权重识别自己的图片)</p>
<a id="more"></a>
<h2 id="本文目的:"><a href="#本文目的:" class="headerlink" title="本文目的:"></a>本文目的:</h2><p>前面有篇文章说的是利用官方的权重直接识别自己的图片,我也展示了识别的效果。</p>
<p>今天我介绍一下如何创建自己的数据集去训练属于自己的 <code>model</code></p>
<h2 id="前提准备:"><a href="#前提准备:" class="headerlink" title="前提准备:"></a>前提准备:</h2><p><strong>1、配置好环境的 python、anaconda 或 pycharm</strong></p>
<p><strong>2、labelimg 软件:下载方法:</strong> <a href="https://blog.csdn.net/qq_45504119/article/details/105038483" target="_blank" rel="noopener">labelimg的下载与使用</a></p>
<p><strong>3、准备一些图片,创建训练需要的 VOC 文件</strong></p>
<p><strong>(1)</strong> 官方的VOC2007下载链接:<a href="http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar" target="_blank" rel="noopener">voc2007下载链接</a>,可以从这里找需要的图片,或者一些有基础的朋友可以写爬虫去爬一些图片</p>
<p><strong>(2)</strong> voc2007百度网盘下载链接:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">链接:https:<span class="comment">//pan.baidu.com/s/18wqRTZDSz5NQEtvq0u0a1g </span></span><br><span class="line">提取码:hexy</span><br></pre></td></tr></table></figure>
<p><strong>(3)</strong> 可以自己准备图片,不过最好准备多一点<br>.<br>.</p>
<h2 id="正式训练步骤:"><a href="#正式训练步骤:" class="headerlink" title="正式训练步骤:"></a>正式训练步骤:</h2><p><strong>一、准备自己的voc2007数据集</strong></p>
<p>先用 pycharm 或 spyder 打开 keras-yolo3 文件夹,用 pycharm 或 spyder 是为了看文件夹更方便,直接在 anaconda 里运行也是可以的</p>
<p><strong>1、打开文件夹</strong></p>
<p>先按照这篇文章的步骤操作:<a href="https://blog.csdn.net/qq_45504119/article/details/105033492" target="_blank" rel="noopener">keras-yolov3目标检测详解——适合新手</a></p>
<p>完成后打开的文件夹应该是这样的:</p>
<p><img src="https://img-blog.csdnimg.cn/20200323190223242.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>2、新建voc2007数据集(存放自己的图片及标注信息)</strong></p>
<p>新建的文件夹:如下</p>
<p><img src="https://img-blog.csdnimg.cn/20200323194549165.png" alt="在这里插入图片描述"></p>
<p><code>ImageSets</code> 文件夹下还有个名为 <code>Main</code> 的小文件夹</p>
<p><img src="https://img-blog.csdnimg.cn/20200323201423401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">VOCdevkit{</span><br><span class="line"> VOC2007{ Annotations</span><br><span class="line"> ImageSets{main}</span><br><span class="line"> JPEGImages } </span><br><span class="line"> }</span><br><span class="line">虽然表达的很丑,但是上面有图,应该还是可以看明白的</span><br><span class="line">注意:文件夹的名称必须和上面展示的一样,这是 yolo 默认的</span><br><span class="line"> 不然还需要改代码才行</span><br></pre></td></tr></table></figure>
<p><strong>3、用labelimg软件对自己的图片进行信息标注</strong></p>
<p>—-<strong>labelimg 的使用方法:</strong><a href="https://blog.csdn.net/qq_45504119/article/details/105038483" target="_blank" rel="noopener">labelimg 下载和标注 xlm 文件</a></p>
<blockquote>
<p>想要训练自己的模型就要学会 labelimg 的使用,实在不想学的…就评论一下邮箱,我直接把我标注好的 VOC2007 文件夹打包发给你们吧</p>
</blockquote>
<p>(1)需要训练的图片放在 <code>JPEGImages</code> 里面:</p>
<p><img src="https://img-blog.csdnimg.cn/20200323195853955.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/20200323200108855.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>(2)labelimg 标注的 xlm 文件放在 <code>Annotations</code> 里面:</p>
<p><img src="https://img-blog.csdnimg.cn/20200323200132768.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/20200323200159422.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>4、在 VOC2007 里新建一个 py 文件</strong></p>
<p><strong>我这里取名 voc.py</strong></p>
<p><img src="https://img-blog.csdnimg.cn/20200323201211208.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>voc.py 的代码:</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line">trainval_percent = <span class="number">0.2</span> <span class="comment">#测试集占0.2</span></span><br><span class="line">train_percent = <span class="number">0.8</span> <span class="comment">#训练集占0.8</span></span><br><span class="line">xmlfilepath = <span class="string">'Annotations'</span></span><br><span class="line">txtsavepath = <span class="string">'ImageSets\Main'</span></span><br><span class="line">total_xml = os.listdir(xmlfilepath)</span><br><span class="line"></span><br><span class="line">num = len(total_xml)</span><br><span class="line">list = range(num)</span><br><span class="line">tv = int(num * trainval_percent)</span><br><span class="line">tr = int(tv * train_percent)</span><br><span class="line">trainval = random.sample(list, tv)</span><br><span class="line">train = random.sample(trainval, tr)</span><br><span class="line"></span><br><span class="line">ftrainval = open(<span class="string">'ImageSets/Main/trainval.txt'</span>, <span class="string">'w'</span>)</span><br><span class="line">ftest = open(<span class="string">'ImageSets/Main/test.txt'</span>, <span class="string">'w'</span>)</span><br><span class="line">ftrain = open(<span class="string">'ImageSets/Main/train.txt'</span>, <span class="string">'w'</span>)</span><br><span class="line">fval = open(<span class="string">'ImageSets/Main/val.txt'</span>, <span class="string">'w'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> list:</span><br><span class="line"> name = total_xml[i][:<span class="number">-4</span>] + <span class="string">'\n'</span></span><br><span class="line"> <span class="keyword">if</span> i <span class="keyword">in</span> trainval:</span><br><span class="line"> ftrainval.write(name)</span><br><span class="line"> <span class="keyword">if</span> i <span class="keyword">in</span> train:</span><br><span class="line"> ftest.write(name)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> fval.write(name)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> ftrain.write(name)</span><br><span class="line"></span><br><span class="line">ftrainval.close()</span><br><span class="line">ftrain.close()</span><br><span class="line">fval.close()</span><br><span class="line">ftest.close()</span><br><span class="line">直接复制以上代码即可</span><br></pre></td></tr></table></figure>
<p><strong>然后运行 voc.py 文件</strong></p>
<p><strong>运行成功的话 mian 文件夹里会多了四个 txt 文件</strong></p>
<p><img src="https://img-blog.csdnimg.cn/2020032320220362.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>完成以上所有步骤,VOC 的处理就完成了</strong></p>
<p>.</p>
<p><strong>二、进行训练前的最后准备</strong></p>
<p><strong>1、修改 <code>voc_annotation.py</code> 文件并运行</strong></p>
<p>更改这里的 classes 的数量,你voc2007里标注了哪几种,你就留哪几种就行</p>
<p><img src="https://img-blog.csdnimg.cn/20200323203028861.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>比如我的 voc 中只标注了 “person”,那我只留下“person”,然后再运行一下就行</p>
<p><img src="https://img-blog.csdnimg.cn/20200323203328943.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>运行完成后会多出这几个 txt 文件</p>
<p><img src="https://img-blog.csdnimg.cn/20200323213806877.png" alt="在这里插入图片描述"></p>
<p><strong>2、修改 model_data</strong></p>
<p>将 coco_classes.txt 和 voc_classes.txt 中也只留下VOC2007 中所标注的那个类型</p>
<p><img src="https://img-blog.csdnimg.cn/20200323203712506.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>比如我标注的只有 “person”</p>
<p>那我只留下“person”</p>
<p><img src="https://img-blog.csdnimg.cn/20200323203936905.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/20200323203955552.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>3、修改 yolo3.cfg</strong></p>
<p>大概在 610、696 和 783 行的位置,把 classes 的数值都改为 1</p>
<p><img src="https://img-blog.csdnimg.cn/20200323205031301.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/202003232052127.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/20200323205103807.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br><strong>4、添加官方权重</strong></p>
<p>按照上篇博文步骤进行的朋友应该下载好了 <code>yolov3.weights</code> 文件并转为了 <code>yolo.h5</code> 文件</p>
<p>附上上篇博文的链接(里面有下载链接和转化方法):<a href="https://blog.csdn.net/qq_45504119/article/details/105033492" target="_blank" rel="noopener">keras-yolov3目标检测详解——适合新手</a></p>
<p>将 <code>yolo.h5</code> 改名为 <code>yolo_weights.h5</code></p>
<p><img src="https://img-blog.csdnimg.cn/20200323213125140.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/20200323213046688.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>5、新建 logs 文件夹存放训练的 权重文件</strong></p>
<p><img src="https://img-blog.csdnimg.cn/2020032321335393.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>6、开始训练</strong></p>
<p>在 <code>keras-yolo3-master</code> 文件夹下新建 一个名为 <code>trainyolo.py</code> 的文件</p>
<p>为什么不用 源文件中的train.py呢??(因为我运行的时候一直出现库的报错…建议按照我的方法来)</p>
<p><code>trainyolo.py</code> 的代码(直接复制即可):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> tensorflow <span class="keyword">as</span> tf</span><br><span class="line"><span class="keyword">import</span> keras.backend <span class="keyword">as</span> K</span><br><span class="line"><span class="keyword">from</span> keras.layers <span class="keyword">import</span> Input, Lambda</span><br><span class="line"><span class="keyword">from</span> keras.models <span class="keyword">import</span> Model</span><br><span class="line"><span class="keyword">from</span> keras.optimizers <span class="keyword">import</span> Adam</span><br><span class="line"><span class="keyword">from</span> keras.callbacks <span class="keyword">import</span> TensorBoard, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping</span><br><span class="line"><span class="keyword">from</span> yolo3.model <span class="keyword">import</span> yolo_body</span><br><span class="line"><span class="keyword">from</span> yolo3.model <span class="keyword">import</span> yolo_loss</span><br><span class="line"><span class="keyword">from</span> keras.backend.tensorflow_backend <span class="keyword">import</span> set_session</span><br><span class="line"><span class="keyword">from</span> yolo3.utils <span class="keyword">import</span> get_random_data</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_classes</span><span class="params">(classes_path)</span>:</span></span><br><span class="line"> <span class="string">'''loads the classes'''</span></span><br><span class="line"> <span class="keyword">with</span> open(classes_path) <span class="keyword">as</span> f:</span><br><span class="line"> class_names = f.readlines()</span><br><span class="line"> class_names = [c.strip() <span class="keyword">for</span> c <span class="keyword">in</span> class_names]</span><br><span class="line"> <span class="keyword">return</span> class_names</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_anchors</span><span class="params">(anchors_path)</span>:</span></span><br><span class="line"> <span class="string">'''loads the anchors from a file'''</span></span><br><span class="line"> <span class="keyword">with</span> open(anchors_path) <span class="keyword">as</span> f:</span><br><span class="line"> anchors = f.readline()</span><br><span class="line"> anchors = [float(x) <span class="keyword">for</span> x <span class="keyword">in</span> anchors.split(<span class="string">','</span>)]</span><br><span class="line"> <span class="keyword">return</span> np.array(anchors).reshape(<span class="number">-1</span>, <span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">data_generator</span><span class="params">(annotation_lines, batch_size, input_shape, anchors, num_classes)</span>:</span></span><br><span class="line"> <span class="string">'''data generator for fit_generator'''</span></span><br><span class="line"> n = len(annotation_lines)</span><br><span class="line"> i = <span class="number">0</span></span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> image_data = []</span><br><span class="line"> box_data = []</span><br><span class="line"> <span class="keyword">for</span> b <span class="keyword">in</span> range(batch_size):</span><br><span class="line"> <span class="keyword">if</span> i==<span class="number">0</span>:</span><br><span class="line"> np.random.shuffle(annotation_lines)</span><br><span class="line"> image, box = get_random_data(annotation_lines[i], input_shape, random=<span class="literal">True</span>)</span><br><span class="line"> image_data.append(image)</span><br><span class="line"> box_data.append(box)</span><br><span class="line"> i = (i+<span class="number">1</span>) % n</span><br><span class="line"> image_data = np.array(image_data)</span><br><span class="line"> box_data = np.array(box_data)</span><br><span class="line"> y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)</span><br><span class="line"> <span class="keyword">yield</span> [image_data, *y_true], np.zeros(batch_size)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">preprocess_true_boxes</span><span class="params">(true_boxes, input_shape, anchors, num_classes)</span>:</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">assert</span> (true_boxes[..., <span class="number">4</span>]<num_classes).all(), <span class="string">'class id must be less than num_classes'</span></span><br><span class="line"> </span><br><span class="line"> num_layers = len(anchors)//<span class="number">3</span> </span><br><span class="line"> anchor_mask = [[<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>], [<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>], [<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>]] <span class="keyword">if</span> num_layers==<span class="number">3</span> <span class="keyword">else</span> [[<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>], [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]]</span><br><span class="line"></span><br><span class="line"> true_boxes = np.array(true_boxes, dtype=<span class="string">'float32'</span>)</span><br><span class="line"> input_shape = np.array(input_shape, dtype=<span class="string">'int32'</span>) <span class="comment"># 416,416</span></span><br><span class="line"> boxes_xy = (true_boxes[..., <span class="number">0</span>:<span class="number">2</span>] + true_boxes[..., <span class="number">2</span>:<span class="number">4</span>]) // <span class="number">2</span></span><br><span class="line"> boxes_wh = true_boxes[..., <span class="number">2</span>:<span class="number">4</span>] - true_boxes[..., <span class="number">0</span>:<span class="number">2</span>]</span><br><span class="line"> true_boxes[..., <span class="number">0</span>:<span class="number">2</span>] = boxes_xy/input_shape[:]</span><br><span class="line"> true_boxes[..., <span class="number">2</span>:<span class="number">4</span>] = boxes_wh/input_shape[:]</span><br><span class="line"></span><br><span class="line"> m = true_boxes.shape[<span class="number">0</span>]</span><br><span class="line"> grid_shapes = [input_shape//{<span class="number">0</span>:<span class="number">32</span>, <span class="number">1</span>:<span class="number">16</span>, <span class="number">2</span>:<span class="number">8</span>}[l] <span class="keyword">for</span> l <span class="keyword">in</span> range(num_layers)]</span><br><span class="line"> y_true = [np.zeros((m,grid_shapes[l][<span class="number">0</span>],grid_shapes[l][<span class="number">1</span>],len(anchor_mask[l]),<span class="number">5</span>+num_classes),</span><br><span class="line"> dtype=<span class="string">'float32'</span>) <span class="keyword">for</span> l <span class="keyword">in</span> range(num_layers)]</span><br><span class="line"> anchors = np.expand_dims(anchors, <span class="number">0</span>)</span><br><span class="line"> anchor_maxes = anchors / <span class="number">2.</span></span><br><span class="line"> anchor_mins = -anchor_maxes</span><br><span class="line"> valid_mask = boxes_wh[..., <span class="number">0</span>]><span class="number">0</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> b <span class="keyword">in</span> range(m):</span><br><span class="line"> wh = boxes_wh[b, valid_mask[b]]</span><br><span class="line"> <span class="keyword">if</span> len(wh)==<span class="number">0</span>: <span class="keyword">continue</span></span><br><span class="line"> wh = np.expand_dims(wh, <span class="number">-2</span>)</span><br><span class="line"> box_maxes = wh / <span class="number">2.</span></span><br><span class="line"> box_mins = -box_maxes</span><br><span class="line"></span><br><span class="line"> intersect_mins = np.maximum(box_mins, anchor_mins)</span><br><span class="line"> intersect_maxes = np.minimum(box_maxes, anchor_maxes)</span><br><span class="line"> intersect_wh = np.maximum(intersect_maxes - intersect_mins, <span class="number">0.</span>)</span><br><span class="line"> intersect_area = intersect_wh[..., <span class="number">0</span>] * intersect_wh[..., <span class="number">1</span>]</span><br><span class="line"> box_area = wh[..., <span class="number">0</span>] * wh[..., <span class="number">1</span>]</span><br><span class="line"> anchor_area = anchors[..., <span class="number">0</span>] * anchors[..., <span class="number">1</span>]</span><br><span class="line"> iou = intersect_area / (box_area + anchor_area - intersect_area)</span><br><span class="line"> best_anchor = np.argmax(iou, axis=<span class="number">-1</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> t, n <span class="keyword">in</span> enumerate(best_anchor):</span><br><span class="line"> <span class="keyword">for</span> l <span class="keyword">in</span> range(num_layers):</span><br><span class="line"> <span class="keyword">if</span> n <span class="keyword">in</span> anchor_mask[l]:</span><br><span class="line"> i = np.floor(true_boxes[b,t,<span class="number">0</span>]*grid_shapes[l][<span class="number">1</span>]).astype(<span class="string">'int32'</span>)</span><br><span class="line"> j = np.floor(true_boxes[b,t,<span class="number">1</span>]*grid_shapes[l][<span class="number">0</span>]).astype(<span class="string">'int32'</span>)</span><br><span class="line"> k = anchor_mask[l].index(n)</span><br><span class="line"> c = true_boxes[b,t, <span class="number">4</span>].astype(<span class="string">'int32'</span>)</span><br><span class="line"> y_true[l][b, j, i, k, <span class="number">0</span>:<span class="number">4</span>] = true_boxes[b,t, <span class="number">0</span>:<span class="number">4</span>]</span><br><span class="line"> y_true[l][b, j, i, k, <span class="number">4</span>] = <span class="number">1</span></span><br><span class="line"> y_true[l][b, j, i, k, <span class="number">5</span>+c] = <span class="number">1</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> y_true</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">config = tf.ConfigProto()</span><br><span class="line">config.gpu_options.allocator_type = <span class="string">'BFC'</span></span><br><span class="line">config.gpu_options.per_process_gpu_memory_fraction = <span class="number">0.7</span></span><br><span class="line">config.gpu_options.allow_growth = <span class="literal">True</span></span><br><span class="line">set_session(tf.Session(config=config)) </span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line"> annotation_path = <span class="string">'2007_train.txt'</span></span><br><span class="line"> classes_path = <span class="string">'model_data/voc_classes.txt'</span> </span><br><span class="line"> anchors_path = <span class="string">'model_data/yolo_anchors.txt'</span></span><br><span class="line"> weights_path = <span class="string">'model_data/yolo_weights.h5'</span></span><br><span class="line"> class_names = get_classes(classes_path)</span><br><span class="line"> anchors = get_anchors(anchors_path)</span><br><span class="line"> num_classes = len(class_names)</span><br><span class="line"> num_anchors = len(anchors)</span><br><span class="line"> log_dir = <span class="string">'logs/'</span></span><br><span class="line"> input_shape = (<span class="number">416</span>,<span class="number">416</span>)</span><br><span class="line"></span><br><span class="line"> K.clear_session()</span><br><span class="line"></span><br><span class="line"> image_input = Input(shape=(<span class="literal">None</span>, <span class="literal">None</span>, <span class="number">3</span>))</span><br><span class="line"> h, w = input_shape</span><br><span class="line"></span><br><span class="line"> print(<span class="string">'Create YOLOv3 model with {} anchors and {} classes.'</span>.format(num_anchors, num_classes))</span><br><span class="line"> model_body = yolo_body(image_input, num_anchors//<span class="number">3</span>, num_classes)</span><br><span class="line"> </span><br><span class="line"> print(<span class="string">'Load weights {}.'</span>.format(weights_path))</span><br><span class="line"> model_body.load_weights(weights_path, by_name=<span class="literal">True</span>, skip_mismatch=<span class="literal">True</span>)</span><br><span class="line"> </span><br><span class="line"> y_true = [Input(shape=(h//{<span class="number">0</span>:<span class="number">32</span>, <span class="number">1</span>:<span class="number">16</span>, <span class="number">2</span>:<span class="number">8</span>}[l], w//{<span class="number">0</span>:<span class="number">32</span>, <span class="number">1</span>:<span class="number">16</span>, <span class="number">2</span>:<span class="number">8</span>}[l], \</span><br><span class="line"> num_anchors//<span class="number">3</span>, num_classes+<span class="number">5</span>)) <span class="keyword">for</span> l <span class="keyword">in</span> range(<span class="number">3</span>)]</span><br><span class="line"></span><br><span class="line"> loss_input = [*model_body.output, *y_true]</span><br><span class="line"> model_loss = Lambda(yolo_loss, output_shape=(<span class="number">1</span>,), name=<span class="string">'yolo_loss'</span>,</span><br><span class="line"> arguments={<span class="string">'anchors'</span>: anchors, <span class="string">'num_classes'</span>: num_classes, <span class="string">'ignore_thresh'</span>: <span class="number">0.5</span>})(loss_input)</span><br><span class="line"></span><br><span class="line"> model = Model([model_body.input, *y_true], model_loss)</span><br><span class="line"></span><br><span class="line"> freeze_layers = <span class="number">249</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> range(freeze_layers): model_body.layers[i].trainable = <span class="literal">False</span></span><br><span class="line"> print(<span class="string">'Freeze the first {} layers of total {} layers.'</span>.format(freeze_layers, len(model_body.layers)))</span><br><span class="line"></span><br><span class="line"> logging = TensorBoard(log_dir=log_dir)</span><br><span class="line"> checkpoint = ModelCheckpoint(log_dir + <span class="string">'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'</span>,</span><br><span class="line"> monitor=<span class="string">'val_loss'</span>, save_weights_only=<span class="literal">True</span>, save_best_only=<span class="literal">False</span>, period=<span class="number">2</span>)</span><br><span class="line"> reduce_lr = ReduceLROnPlateau(monitor=<span class="string">'val_loss'</span>, factor=<span class="number">0.5</span>, patience=<span class="number">2</span>, verbose=<span class="number">1</span>)</span><br><span class="line"> early_stopping = EarlyStopping(monitor=<span class="string">'val_loss'</span>, min_delta=<span class="number">0</span>, patience=<span class="number">6</span>, verbose=<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"> val_split = <span class="number">0.2</span></span><br><span class="line"> <span class="keyword">with</span> open(annotation_path) <span class="keyword">as</span> f:</span><br><span class="line"> lines = f.readlines()</span><br><span class="line"> np.random.seed(<span class="number">10101</span>)</span><br><span class="line"> np.random.shuffle(lines)</span><br><span class="line"> np.random.seed(<span class="literal">None</span>)</span><br><span class="line"> num_val = int(len(lines)*val_split)</span><br><span class="line"> num_train = len(lines) - num_val</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span> <span class="literal">True</span>:</span><br><span class="line"> model.compile(optimizer=Adam(lr=<span class="number">1e-3</span>), loss={</span><br><span class="line"> <span class="string">'yolo_loss'</span>: <span class="keyword">lambda</span> y_true, y_pred: y_pred})</span><br><span class="line"></span><br><span class="line"> batch_size = <span class="number">1</span></span><br><span class="line"> print(<span class="string">'Train on {} samples, val on {} samples, with batch size {}.'</span>.format(num_train, num_val, batch_size))</span><br><span class="line"> model.fit_generator(data_generator(lines[:num_train], batch_size, input_shape, anchors, num_classes),</span><br><span class="line"> steps_per_epoch=max(<span class="number">1</span>, num_train//batch_size),</span><br><span class="line"> validation_data=data_generator(lines[num_train:], batch_size, input_shape, anchors, num_classes),</span><br><span class="line"> validation_steps=max(<span class="number">1</span>, num_val//batch_size),</span><br><span class="line"> epochs=<span class="number">50</span>,</span><br><span class="line"> initial_epoch=<span class="number">0</span>,</span><br><span class="line"> callbacks=[logging, checkpoint])</span><br><span class="line"> model.save_weights(log_dir + <span class="string">'trained_weights_stage_1.h5'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> range(freeze_layers): model_body.layers[i].trainable = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="literal">True</span>:</span><br><span class="line"> model.compile(optimizer=Adam(lr=<span class="number">1e-4</span>), loss={</span><br><span class="line"> <span class="string">'yolo_loss'</span>: <span class="keyword">lambda</span> y_true, y_pred: y_pred})</span><br><span class="line"></span><br><span class="line"> batch_size = <span class="number">1</span></span><br><span class="line"> print(<span class="string">'Train on {} samples, val on {} samples, with batch size {}.'</span>.format(num_train, num_val, batch_size))</span><br><span class="line"> model.fit_generator(data_generator(lines[:num_train], batch_size, input_shape, anchors, num_classes),</span><br><span class="line"> steps_per_epoch=max(<span class="number">1</span>, num_train//batch_size),</span><br><span class="line"> validation_data=data_generator(lines[num_train:], batch_size, input_shape, anchors, num_classes),</span><br><span class="line"> validation_steps=max(<span class="number">1</span>, num_val//batch_size),</span><br><span class="line"> epochs=<span class="number">100</span>,</span><br><span class="line"> initial_epoch=<span class="number">50</span>,</span><br><span class="line"> callbacks=[logging, checkpoint])</span><br><span class="line"> model.save_weights(log_dir + <span class="string">'last1.h5'</span>)</span><br></pre></td></tr></table></figure>
<p>然后运行<code>trainyolo.py</code> 的代码</p>
<p><img src="https://img-blog.csdnimg.cn/2020032321563349.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>成功开始训练,训练时间比较长,需要耐心等待</strong></p>
<p>.<br>.</p>
<h2 id="训练完成:"><a href="#训练完成:" class="headerlink" title="训练完成:"></a>训练完成:</h2><p>因为我的电脑配置比较差,所以训练了很长很长时间,这是我前几天训练的结果</p>
<p><img src="https://img-blog.csdnimg.cn/20200323220016562.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>训练好的权重都放在 logs 文件夹下的 000 文件夹里:</p>
<p><img src="https://img-blog.csdnimg.cn/20200323220244528.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>按理说训练会经过两个阶段,且自动从一堆 loss 中选则出 loss最低的文件(应该是 earlystop函数的作用):</p>
<p>应该就是下面的框选的这两个 h5文件,都可以使用</p>
<p><img src="https://img-blog.csdnimg.cn/20200323220350578.png" alt="在这里插入图片描述"></p>
<p>使用的方法就和前面那篇博文操作一样了(用这个h5权重模型去识别自己图片),下面给大家展示一下我训练的模型的结果吧</p>
<p><img src="https://img-blog.csdnimg.cn/20200323221119292.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/20200323221200911.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NTA0MTE5,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>对比使用官方的权重文件来说,我的模型可以仅仅识别出图片中的人体,而且识别效果还不赖,感觉还不错吧 哈哈哈</p>
<h2 id="总结:"><a href="#总结:" class="headerlink" title="总结:"></a>总结:</h2><p>以上过程皆为刚刚我亲自操作,全程没有任何问题</p>
<p>如果有朋友对这篇文章的任何内容感到不明白的请及时评论提出问题,我怕时间一久我也会忘,想要资料的直接评论你们的联系方式就行,我有时间会回复的。</p>
<p>这个暂时告一段落,接下来我会在 matlab 中找一些算法去实现同样的功能,有兴趣的朋友可以评论一下一起探讨。</p>
<!-- reward -->
</div>
<!-- copyright -->
<div class="declare">
<ul class="post-copyright">
<li>
<i class="ri-copyright-line"></i>
<strong>版权声明: </strong>
本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
</li>
</ul>
</div>
<footer class="article-footer">
<div class="share-btn">
<span class="share-sns share-outer">
<i class="ri-share-forward-line"></i>
分享
</span>
<div class="share-wrap">
<i class="arrow"></i>
<div class="share-icons">
<a class="weibo share-sns" href="javascript:;" data-type="weibo">
<i class="ri-weibo-fill"></i>
</a>
<a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
<i class="ri-wechat-fill"></i>
</a>
<a class="qq share-sns" href="javascript:;" data-type="qq">
<i class="ri-qq-fill"></i>
</a>
<a class="douban share-sns" href="javascript:;" data-type="douban">
<i class="ri-douban-line"></i>
</a>
<!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
<i class="icon icon-qzone"></i>
</a> -->
<a class="facebook share-sns" href="javascript:;" data-type="facebook">
<i class="ri-facebook-circle-fill"></i>
</a>
<a class="twitter share-sns" href="javascript:;" data-type="twitter">
<i class="ri-twitter-fill"></i>
</a>
<a class="google share-sns" href="javascript:;" data-type="google">
<i class="ri-google-fill"></i>
</a>
</div>
</div>
</div>
<div class="wx-share-modal">
<a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
<p>扫一扫,分享到微信</p>
<div class="wx-qrcode">
<img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://squansh.top/%E6%95%99%E5%A5%B3%E6%9C%8B%E5%8F%8B%E5%AD%A6%E4%BC%9A%E7%94%A8win10+yolov3+python%E8%AE%AD%E7%BB%83%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A8%A1%E5%9E%8B.html" alt="微信分享二维码">
</div>
</div>
<div id="share-mask"></div>
<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%9C%BA%E5%99%A8%E8%A7%86%E8%A7%89/" rel="tag">机器视觉</a></li></ul>
</footer>
</div>
<nav class="article-nav">
<a href="/python%E7%BB%9D%E5%AF%B9%E5%85%BC%E5%AE%B9%E7%9A%84%E5%BA%93%E9%85%8D%E7%BD%AE%E2%80%94%E2%80%94%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0&%E7%88%AC%E8%99%AB.html" class="article-nav-link">
<strong class="article-nav-caption">上一篇</strong>
<div class="article-nav-title">
python绝对兼容的库配置——机器学习&爬虫
</div>
</a>
<a href="/python%E7%BB%9D%E5%AF%B9%E5%85%BC%E5%AE%B9%E7%9A%84%E5%BA%93%E9%85%8D%E7%BD%AE%E2%80%94%E2%80%94%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0.html" class="article-nav-link">
<strong class="article-nav-caption">下一篇</strong>
<div class="article-nav-title">python绝对兼容的库配置——机器学习</div>
</a>
</nav>
<!-- valine评论 -->
<div id="vcomments-box">
<div id="vcomments">
</div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/Valine.min.js'></script>
<script>
new Valine({
el: '#vcomments',
app_id: 'bMSQYcOX9RQgHRwGmc2xl2iY-9Nh9j0Va',
app_key: 'fhWrk4asGBlMT45CQdjPoEx4',
path: window.location.pathname,
notify: true,
verify: true,
avatar: 'monsterid',
placeholder: '给我的文章加点评论吧~',
recordIP: true
});
const infoEle = document.querySelector('#vcomments .info');
if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
infoEle.childNodes.forEach(function (item) {
item.parentNode.removeChild(item);
});
}
</script>
<style>
#vcomments-box {
padding: 5px 30px;
}
@media screen and (max-width: 800px) {
#vcomments-box {
padding: 5px 0px;
}
}
#vcomments-box #vcomments {
background-color: #fff;
}
.v .vlist .vcard .vh {
padding-right: 20px;
}
.v .vlist .vcard {
padding-left: 10px;
}
</style>
</article>
</section>
<footer class="footer">
<div class="outer">
<ul>
<li>
<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date();
function createtime() {
var grt= new Date("06/01/2020 00:00:00");//在此处修改你的建站时间,格式:月/日/年 时:分:秒
now.setTime(now.getTime()+250);
days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 ";
document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()",250);
</script>
</li>
</ul>
<ul>
<li>
Copyrights ©
2020
<i class="ri-heart-fill heart_icon"></i> squansh
</li>
</ul>
<ul>
<li>
<span>
<span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
<span class="division">|</span>
<span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</li>
</ul>
<ul>
</ul>
<ul>
<li>
<!-- cnzz统计 -->
<script type="text/javascript" src="https://s4.cnzz.com/z_stat.php?id=1278953980&web_id=1278953980"></script>
</li>
</ul>
</div>
</footer>
<div class="float_btns">
<div class="totop" id="totop">
<i class="ri-arrow-up-line"></i>
</div>
<div class="todark" id="todark">
<i class="ri-moon-line"></i>
</div>
</div>
</main>
<aside class="sidebar on">
<button class="navbar-toggle"></button>
<nav class="navbar">
<div class="logo">
<a href="/"><img src="/images/photo_side.svg" alt="孙全胜의Space"></a>
</div>
<ul class="nav nav-main">
<li class="nav-item">
<a class="nav-item-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="/archives">Archives</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="/categories/">Categories</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="/tags">Tags</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="https://changyuqi.top/" target="_blank" rel="noopener">Sister</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="http://121.41.228.97/" target="_blank" rel="noopener">Bolo</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="https://quan_sheng_sun.gitee.io/resume" target="_blank" rel="noopener">Resume</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="https://jq.qq.com/?_wv=1027&k=KyptBBmy" target="_blank" rel="noopener">Qgroup</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="https://www.zhihu.com/people/jes331" target="_blank" rel="noopener">Zhi-h</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="https://weibo.com/5600672195" target="_blank" rel="noopener">Wei-b</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="https://blog.csdn.net/qq_45504119" target="_blank" rel="noopener">CSDN</a>
</li>
<li class="nav-item">
<a class="nav-item-link" href="mailto:[email protected]">E-mail</a>
</li>
</ul>
</nav>
<nav class="navbar navbar-bottom">
<ul class="nav">
<li class="nav-item">
<a class="nav-item-link nav-item-search" title="搜索">
<i class="ri-search-line"></i>
</a>
<a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
<i class="ri-rss-line"></i>
</a>
</li>
</ul>
</nav>
<div class="search-form-wrap">
<div class="local-search local-search-plugin">
<input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
<div id="local-search-result" class="local-search-result"></div>
</div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5uyif1eoo9j&m=0&c=ff0000&cr1=ffffff&f=arial&l=33&s=341&bv=85" async="async"></script>
</aside>
<!--浏览器搞笑标题-->
<script type="text/javascript" src="/js/FunnyTitle.js"></script>
<script>
if (window.matchMedia("(max-width: 768px)").matches) {
document.querySelector('.content').classList.remove('on');
document.querySelector('.sidebar').classList.remove('on');
}
</script>
<div id="mask"></div>
<!-- #reward -->
<div id="reward">
<span class="close"><i class="ri-close-line"></i></span>
<p class="reward-p"><i class="ri-cup-line"></i></p>
<div class="reward-box">
</div>
</div>
<script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>
<!-- Subtitle -->
<script>
try {
var typed = new Typed("#subtitle", {
strings: ['夜深了', '是否明天会悄然忘记', '夜却深了'],
startDelay: 0,
typeSpeed: 400,
loop: true,
backSpeed: 150,
showCursor: true
});
} catch (err) {
console.log(err)
}
</script>
<!-- Tocbot -->
<script src="/js/tocbot.min.js"></script>
<script>
tocbot.init({
tocSelector: '.tocbot',
contentSelector: '.article-entry',
headingSelector: 'h1, h2, h3, h4, h5, h6',
hasInnerContainers: true,
scrollSmooth: true,
scrollContainer: 'main',
positionFixedSelector: '.tocbot',
positionFixedClass: 'is-position-fixed',
fixedSidebarOffset: 'auto'
});
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/jquery.justifiedGallery.min.js"></script>
<script src="/dist/main.js"></script>
<!-- ImageViewer -->
<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
<!-- Background of PhotoSwipe.
It's a separate element as animating opacity is faster than rgba(). -->
<div class="pswp__bg"></div>
<!-- Slides wrapper with overflow:hidden. -->
<div class="pswp__scroll-wrap">
<!-- Container that holds slides.
PhotoSwipe keeps only 3 of them in the DOM to save memory.
Don't modify these 3 pswp__item elements, data is added later on. -->
<div class="pswp__container">
<div class="pswp__item"></div>
<div class="pswp__item"></div>
<div class="pswp__item"></div>
</div>
<!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
<div class="pswp__ui pswp__ui--hidden">
<div class="pswp__top-bar">
<!-- Controls are self-explanatory. Order can be changed. -->
<div class="pswp__counter"></div>
<button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
<button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>
<button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
<button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
<!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
<!-- element will get class pswp__preloader--active when preloader is running -->
<div class="pswp__preloader">
<div class="pswp__preloader__icn">
<div class="pswp__preloader__cut">
<div class="pswp__preloader__donut"></div>
</div>
</div>
</div>
</div>
<div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
<div class="pswp__share-tooltip"></div>
</div>
<button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
</button>
<button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
</button>
<div class="pswp__caption">
<div class="pswp__caption__center"></div>
</div>
</div>
</div>
</div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/photoswipe-ui-default.min.js"></script>
<script>
function viewer_init() {
let pswpElement = document.querySelectorAll('.pswp')[0];
let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))
$imgArr.forEach(($em, i) => {
$em.onclick = () => {
// slider展开状态
// todo: 这样不好,后面改成状态
if (document.querySelector('.left-col.show')) return
let items = []
$imgArr.forEach(($em2, i2) => {
let img = $em2.getAttribute('data-idx', i2)
let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
let title = $em2.getAttribute('alt')
// 获得原图尺寸
const image = new Image()
image.src = src
items.push({
src: src,
w: image.width || $em2.width,
h: image.height || $em2.height,
title: title
})
})
var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
index: parseInt(i)
});
gallery.init()
}
})
}
viewer_init()
</script>
<!-- MathJax -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
var ayerConfig = {
mathjax: true
}
</script>
<!-- Katex -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"></script>
<!-- busuanzi -->
<script src="/js/busuanzi-2.3.pure.min.js"></script>
<!-- ClickLove -->
<script src="/js/clickLove.js"></script>
<!-- ClickBoom -->
<script src="/js/clickBoom.js"></script>
<!-- CodeCopy -->
<link rel="stylesheet" href="/css/clipboard.css">
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
function wait(callback, seconds) {
var timelag = null;
timelag = window.setTimeout(callback, seconds);
}
!function (e, t, a) {
var initCopyCode = function(){
var copyHtml = '';
copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
copyHtml += '</button>';
$(".highlight .code pre").before(copyHtml);
$(".article pre code").before(copyHtml);
var clipboard = new ClipboardJS('.btn-copy', {
target: function(trigger) {
return trigger.nextElementSibling;
}
});
clipboard.on('success', function(e) {
let $btn = $(e.trigger);
$btn.addClass('copied');
let $icon = $($btn.find('i'));
$icon.removeClass('ri-file-copy-2-line');
$icon.addClass('ri-checkbox-circle-line');
let $span = $($btn.find('span'));
$span[0].innerText = 'COPIED';
wait(function () { // 等待两秒钟后恢复
$icon.removeClass('ri-checkbox-circle-line');
$icon.addClass('ri-file-copy-2-line');
$span[0].innerText = 'COPY';
}, 2000);
});
clipboard.on('error', function(e) {
e.clearSelection();
let $btn = $(e.trigger);
$btn.addClass('copy-failed');
let $icon = $($btn.find('i'));
$icon.removeClass('ri-file-copy-2-line');
$icon.addClass('ri-time-line');
let $span = $($btn.find('span'));
$span[0].innerText = 'COPY FAILED';
wait(function () { // 等待两秒钟后恢复
$icon.removeClass('ri-time-line');
$icon.addClass('ri-file-copy-2-line');
$span[0].innerText = 'COPY';
}, 2000);
});
}
initCopyCode();
}(window, document);
</script>
</div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/haruto.model.json"},"display":{"position":"right","width":260,"height":520},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>