├── .gitignore
├── LICENSE
├── README.md
├── config
├── coco.data
├── create_custom_model.sh
├── custom.data
├── yolov3-tiny.cfg
└── yolov3.cfg
├── data
├── coco.names
├── custom
│ ├── classes.names
│ ├── images
│ │ └── train.jpg
│ ├── labels
│ │ └── train.txt
│ ├── train.txt
│ └── valid.txt
├── get_coco_dataset.sh
└── samples
│ ├── dog.jpg
│ ├── eagle.jpg
│ ├── field.jpg
│ ├── giraffe.jpg
│ ├── herd_of_horses.jpg
│ ├── messi.jpg
│ ├── person.jpg
│ ├── room.jpg
│ └── street.jpg
├── image
├── 09979.jpg
├── 09981.jpg
├── 09982.jpg
├── 09983.jpg
├── 10966.jpg
├── 10969.jpg
├── 10971.jpg
├── 10976.jpg
├── anchor.jpg
├── mouse1.jpg
├── mouse2.jpg
├── mouse3.jpg
├── mouse4.jpg
├── performance.jpg
└── yolo-struct.jpg
├── models.py
├── predict.py
├── test.py
├── train.py
└── utils
├── __init__.py
├── augmentations.py
├── datasets.py
├── logger.py
├── parse_config.py
└── utils.py
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 yangbisheng2009
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # industry-mouse-detect
2 | 
3 | 
4 | 
5 | 
6 |
7 | ## 背景介绍
8 | 本工程着重识别工业环境中的老鼠,以便能够得到及时、有效的处理。本工程的难度如下(如果有条件,养只大猫咪,可能效果更佳....):
9 | - 老鼠与背景色难以区分
10 | - 老鼠经常出没在晚上,增加了识别难度
11 | - 老鼠体型较小,传统的图像处理方法和神经网络方法难以识别
12 | - 工业环境要求较高的处理速度,一般的神经网络方法难以满足
13 | ## 实现方法简介
14 | 鉴于本项目天然的难度,采用单一手段无法处理,现使用复合方法:
15 | 1. 采用YOLOV3-darknet为backbone的主干神经网络结构,以残差方减小梯度消失(YOLOV3拥有20+的FPS,能够满足高性能的工业需求)
16 | 2. 采用双帧差分监测帧与帧之间像素级别变化,并改动网络结构做重点监督训练(解决夜晚识别效果差问题、解决老鼠天然保护色问题)
17 | 3. 修改主干网络先验框尺寸,方便小物体能得到充分的学习(解决小物体检测问题)
18 |
19 | **主干网络的选择依据:**
20 | 1. 相比较 [Focal Loss for Dense Object Detection](https://arxiv.org/abs/1708.02002) 和 [Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks](https://arxiv.org/pdf/1506.01497.pdf),YOLOV3算法能够在基本满足识别效果的同时,保持高速的运算速度
21 | 2. 相比较[SSD: Single Shot MultiBox Detector](https://arxiv.org/abs/1512.02325)算法,二者处理速度相差较小,但是YOLOV3算法对小物体的识别效果更好(能够灵活改动先验anchor)
22 |
23 | **主干网络结构如下:**
24 |
25 |

26 |
27 |
28 | **自主调整先验框大小(anchor):**
29 |
30 |

31 |
32 |
33 | **backbone的性能横向对比:**
34 |
35 |

36 |
37 |
38 | ## 如何使用本项目
39 | ```shell
40 | #train
41 | python train.py --backbone darknet --epochs 90 --batch-size 16 --checkpoint ./checkpoint --data-dir ./data
42 |
43 | #test
44 | python test.py
45 |
46 | #predict
47 | python predict.py --model darknet --checkpoint ./checkpoint/x
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 | [1][YOLOv3: An Incremental Improvement](https://pjreddie.com/media/files/papers/YOLOv3.pdf)
80 | [2][Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks, 2015](https://arxiv.org/pdf/1506.01497.pdf)
81 | [3][Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning, 2016](https://arxiv.org/pdf/1602.07261.pdf)
82 | [4][YOLOv3官方原始工程](https://pjreddie.com/darknet/yolo/)
83 | [5][darknet原始主干网络](https://github.com/pjreddie/darknet)
84 |
--------------------------------------------------------------------------------
/config/coco.data:
--------------------------------------------------------------------------------
1 | classes= 80
2 | train=data/coco/trainvalno5k.txt
3 | valid=data/coco/5k.txt
4 | names=data/coco.names
5 | backup=backup/
6 | eval=coco
7 |
--------------------------------------------------------------------------------
/config/create_custom_model.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | NUM_CLASSES=$1
4 |
5 | echo "
6 | [net]
7 | # Testing
8 | #batch=1
9 | #subdivisions=1
10 | # Training
11 | batch=16
12 | subdivisions=1
13 | width=416
14 | height=416
15 | channels=3
16 | momentum=0.9
17 | decay=0.0005
18 | angle=0
19 | saturation = 1.5
20 | exposure = 1.5
21 | hue=.1
22 |
23 | learning_rate=0.001
24 | burn_in=1000
25 | max_batches = 500200
26 | policy=steps
27 | steps=400000,450000
28 | scales=.1,.1
29 |
30 | [convolutional]
31 | batch_normalize=1
32 | filters=32
33 | size=3
34 | stride=1
35 | pad=1
36 | activation=leaky
37 |
38 | # Downsample
39 |
40 | [convolutional]
41 | batch_normalize=1
42 | filters=64
43 | size=3
44 | stride=2
45 | pad=1
46 | activation=leaky
47 |
48 | [convolutional]
49 | batch_normalize=1
50 | filters=32
51 | size=1
52 | stride=1
53 | pad=1
54 | activation=leaky
55 |
56 | [convolutional]
57 | batch_normalize=1
58 | filters=64
59 | size=3
60 | stride=1
61 | pad=1
62 | activation=leaky
63 |
64 | [shortcut]
65 | from=-3
66 | activation=linear
67 |
68 | # Downsample
69 |
70 | [convolutional]
71 | batch_normalize=1
72 | filters=128
73 | size=3
74 | stride=2
75 | pad=1
76 | activation=leaky
77 |
78 | [convolutional]
79 | batch_normalize=1
80 | filters=64
81 | size=1
82 | stride=1
83 | pad=1
84 | activation=leaky
85 |
86 | [convolutional]
87 | batch_normalize=1
88 | filters=128
89 | size=3
90 | stride=1
91 | pad=1
92 | activation=leaky
93 |
94 | [shortcut]
95 | from=-3
96 | activation=linear
97 |
98 | [convolutional]
99 | batch_normalize=1
100 | filters=64
101 | size=1
102 | stride=1
103 | pad=1
104 | activation=leaky
105 |
106 | [convolutional]
107 | batch_normalize=1
108 | filters=128
109 | size=3
110 | stride=1
111 | pad=1
112 | activation=leaky
113 |
114 | [shortcut]
115 | from=-3
116 | activation=linear
117 |
118 | # Downsample
119 |
120 | [convolutional]
121 | batch_normalize=1
122 | filters=256
123 | size=3
124 | stride=2
125 | pad=1
126 | activation=leaky
127 |
128 | [convolutional]
129 | batch_normalize=1
130 | filters=128
131 | size=1
132 | stride=1
133 | pad=1
134 | activation=leaky
135 |
136 | [convolutional]
137 | batch_normalize=1
138 | filters=256
139 | size=3
140 | stride=1
141 | pad=1
142 | activation=leaky
143 |
144 | [shortcut]
145 | from=-3
146 | activation=linear
147 |
148 | [convolutional]
149 | batch_normalize=1
150 | filters=128
151 | size=1
152 | stride=1
153 | pad=1
154 | activation=leaky
155 |
156 | [convolutional]
157 | batch_normalize=1
158 | filters=256
159 | size=3
160 | stride=1
161 | pad=1
162 | activation=leaky
163 |
164 | [shortcut]
165 | from=-3
166 | activation=linear
167 |
168 | [convolutional]
169 | batch_normalize=1
170 | filters=128
171 | size=1
172 | stride=1
173 | pad=1
174 | activation=leaky
175 |
176 | [convolutional]
177 | batch_normalize=1
178 | filters=256
179 | size=3
180 | stride=1
181 | pad=1
182 | activation=leaky
183 |
184 | [shortcut]
185 | from=-3
186 | activation=linear
187 |
188 | [convolutional]
189 | batch_normalize=1
190 | filters=128
191 | size=1
192 | stride=1
193 | pad=1
194 | activation=leaky
195 |
196 | [convolutional]
197 | batch_normalize=1
198 | filters=256
199 | size=3
200 | stride=1
201 | pad=1
202 | activation=leaky
203 |
204 | [shortcut]
205 | from=-3
206 | activation=linear
207 |
208 |
209 | [convolutional]
210 | batch_normalize=1
211 | filters=128
212 | size=1
213 | stride=1
214 | pad=1
215 | activation=leaky
216 |
217 | [convolutional]
218 | batch_normalize=1
219 | filters=256
220 | size=3
221 | stride=1
222 | pad=1
223 | activation=leaky
224 |
225 | [shortcut]
226 | from=-3
227 | activation=linear
228 |
229 | [convolutional]
230 | batch_normalize=1
231 | filters=128
232 | size=1
233 | stride=1
234 | pad=1
235 | activation=leaky
236 |
237 | [convolutional]
238 | batch_normalize=1
239 | filters=256
240 | size=3
241 | stride=1
242 | pad=1
243 | activation=leaky
244 |
245 | [shortcut]
246 | from=-3
247 | activation=linear
248 |
249 | [convolutional]
250 | batch_normalize=1
251 | filters=128
252 | size=1
253 | stride=1
254 | pad=1
255 | activation=leaky
256 |
257 | [convolutional]
258 | batch_normalize=1
259 | filters=256
260 | size=3
261 | stride=1
262 | pad=1
263 | activation=leaky
264 |
265 | [shortcut]
266 | from=-3
267 | activation=linear
268 |
269 | [convolutional]
270 | batch_normalize=1
271 | filters=128
272 | size=1
273 | stride=1
274 | pad=1
275 | activation=leaky
276 |
277 | [convolutional]
278 | batch_normalize=1
279 | filters=256
280 | size=3
281 | stride=1
282 | pad=1
283 | activation=leaky
284 |
285 | [shortcut]
286 | from=-3
287 | activation=linear
288 |
289 | # Downsample
290 |
291 | [convolutional]
292 | batch_normalize=1
293 | filters=512
294 | size=3
295 | stride=2
296 | pad=1
297 | activation=leaky
298 |
299 | [convolutional]
300 | batch_normalize=1
301 | filters=256
302 | size=1
303 | stride=1
304 | pad=1
305 | activation=leaky
306 |
307 | [convolutional]
308 | batch_normalize=1
309 | filters=512
310 | size=3
311 | stride=1
312 | pad=1
313 | activation=leaky
314 |
315 | [shortcut]
316 | from=-3
317 | activation=linear
318 |
319 |
320 | [convolutional]
321 | batch_normalize=1
322 | filters=256
323 | size=1
324 | stride=1
325 | pad=1
326 | activation=leaky
327 |
328 | [convolutional]
329 | batch_normalize=1
330 | filters=512
331 | size=3
332 | stride=1
333 | pad=1
334 | activation=leaky
335 |
336 | [shortcut]
337 | from=-3
338 | activation=linear
339 |
340 |
341 | [convolutional]
342 | batch_normalize=1
343 | filters=256
344 | size=1
345 | stride=1
346 | pad=1
347 | activation=leaky
348 |
349 | [convolutional]
350 | batch_normalize=1
351 | filters=512
352 | size=3
353 | stride=1
354 | pad=1
355 | activation=leaky
356 |
357 | [shortcut]
358 | from=-3
359 | activation=linear
360 |
361 |
362 | [convolutional]
363 | batch_normalize=1
364 | filters=256
365 | size=1
366 | stride=1
367 | pad=1
368 | activation=leaky
369 |
370 | [convolutional]
371 | batch_normalize=1
372 | filters=512
373 | size=3
374 | stride=1
375 | pad=1
376 | activation=leaky
377 |
378 | [shortcut]
379 | from=-3
380 | activation=linear
381 |
382 | [convolutional]
383 | batch_normalize=1
384 | filters=256
385 | size=1
386 | stride=1
387 | pad=1
388 | activation=leaky
389 |
390 | [convolutional]
391 | batch_normalize=1
392 | filters=512
393 | size=3
394 | stride=1
395 | pad=1
396 | activation=leaky
397 |
398 | [shortcut]
399 | from=-3
400 | activation=linear
401 |
402 |
403 | [convolutional]
404 | batch_normalize=1
405 | filters=256
406 | size=1
407 | stride=1
408 | pad=1
409 | activation=leaky
410 |
411 | [convolutional]
412 | batch_normalize=1
413 | filters=512
414 | size=3
415 | stride=1
416 | pad=1
417 | activation=leaky
418 |
419 | [shortcut]
420 | from=-3
421 | activation=linear
422 |
423 |
424 | [convolutional]
425 | batch_normalize=1
426 | filters=256
427 | size=1
428 | stride=1
429 | pad=1
430 | activation=leaky
431 |
432 | [convolutional]
433 | batch_normalize=1
434 | filters=512
435 | size=3
436 | stride=1
437 | pad=1
438 | activation=leaky
439 |
440 | [shortcut]
441 | from=-3
442 | activation=linear
443 |
444 | [convolutional]
445 | batch_normalize=1
446 | filters=256
447 | size=1
448 | stride=1
449 | pad=1
450 | activation=leaky
451 |
452 | [convolutional]
453 | batch_normalize=1
454 | filters=512
455 | size=3
456 | stride=1
457 | pad=1
458 | activation=leaky
459 |
460 | [shortcut]
461 | from=-3
462 | activation=linear
463 |
464 | # Downsample
465 |
466 | [convolutional]
467 | batch_normalize=1
468 | filters=1024
469 | size=3
470 | stride=2
471 | pad=1
472 | activation=leaky
473 |
474 | [convolutional]
475 | batch_normalize=1
476 | filters=512
477 | size=1
478 | stride=1
479 | pad=1
480 | activation=leaky
481 |
482 | [convolutional]
483 | batch_normalize=1
484 | filters=1024
485 | size=3
486 | stride=1
487 | pad=1
488 | activation=leaky
489 |
490 | [shortcut]
491 | from=-3
492 | activation=linear
493 |
494 | [convolutional]
495 | batch_normalize=1
496 | filters=512
497 | size=1
498 | stride=1
499 | pad=1
500 | activation=leaky
501 |
502 | [convolutional]
503 | batch_normalize=1
504 | filters=1024
505 | size=3
506 | stride=1
507 | pad=1
508 | activation=leaky
509 |
510 | [shortcut]
511 | from=-3
512 | activation=linear
513 |
514 | [convolutional]
515 | batch_normalize=1
516 | filters=512
517 | size=1
518 | stride=1
519 | pad=1
520 | activation=leaky
521 |
522 | [convolutional]
523 | batch_normalize=1
524 | filters=1024
525 | size=3
526 | stride=1
527 | pad=1
528 | activation=leaky
529 |
530 | [shortcut]
531 | from=-3
532 | activation=linear
533 |
534 | [convolutional]
535 | batch_normalize=1
536 | filters=512
537 | size=1
538 | stride=1
539 | pad=1
540 | activation=leaky
541 |
542 | [convolutional]
543 | batch_normalize=1
544 | filters=1024
545 | size=3
546 | stride=1
547 | pad=1
548 | activation=leaky
549 |
550 | [shortcut]
551 | from=-3
552 | activation=linear
553 |
554 | ######################
555 |
556 | [convolutional]
557 | batch_normalize=1
558 | filters=512
559 | size=1
560 | stride=1
561 | pad=1
562 | activation=leaky
563 |
564 | [convolutional]
565 | batch_normalize=1
566 | size=3
567 | stride=1
568 | pad=1
569 | filters=1024
570 | activation=leaky
571 |
572 | [convolutional]
573 | batch_normalize=1
574 | filters=512
575 | size=1
576 | stride=1
577 | pad=1
578 | activation=leaky
579 |
580 | [convolutional]
581 | batch_normalize=1
582 | size=3
583 | stride=1
584 | pad=1
585 | filters=1024
586 | activation=leaky
587 |
588 | [convolutional]
589 | batch_normalize=1
590 | filters=512
591 | size=1
592 | stride=1
593 | pad=1
594 | activation=leaky
595 |
596 | [convolutional]
597 | batch_normalize=1
598 | size=3
599 | stride=1
600 | pad=1
601 | filters=1024
602 | activation=leaky
603 |
604 | [convolutional]
605 | size=1
606 | stride=1
607 | pad=1
608 | filters=$(expr 3 \* $(expr $NUM_CLASSES \+ 5))
609 | activation=linear
610 |
611 |
612 | [yolo]
613 | mask = 6,7,8
614 | anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
615 | classes=$NUM_CLASSES
616 | num=9
617 | jitter=.3
618 | ignore_thresh = .7
619 | truth_thresh = 1
620 | random=1
621 |
622 |
623 | [route]
624 | layers = -4
625 |
626 | [convolutional]
627 | batch_normalize=1
628 | filters=256
629 | size=1
630 | stride=1
631 | pad=1
632 | activation=leaky
633 |
634 | [upsample]
635 | stride=2
636 |
637 | [route]
638 | layers = -1, 61
639 |
640 |
641 |
642 | [convolutional]
643 | batch_normalize=1
644 | filters=256
645 | size=1
646 | stride=1
647 | pad=1
648 | activation=leaky
649 |
650 | [convolutional]
651 | batch_normalize=1
652 | size=3
653 | stride=1
654 | pad=1
655 | filters=512
656 | activation=leaky
657 |
658 | [convolutional]
659 | batch_normalize=1
660 | filters=256
661 | size=1
662 | stride=1
663 | pad=1
664 | activation=leaky
665 |
666 | [convolutional]
667 | batch_normalize=1
668 | size=3
669 | stride=1
670 | pad=1
671 | filters=512
672 | activation=leaky
673 |
674 | [convolutional]
675 | batch_normalize=1
676 | filters=256
677 | size=1
678 | stride=1
679 | pad=1
680 | activation=leaky
681 |
682 | [convolutional]
683 | batch_normalize=1
684 | size=3
685 | stride=1
686 | pad=1
687 | filters=512
688 | activation=leaky
689 |
690 | [convolutional]
691 | size=1
692 | stride=1
693 | pad=1
694 | filters=$(expr 3 \* $(expr $NUM_CLASSES \+ 5))
695 | activation=linear
696 |
697 |
698 | [yolo]
699 | mask = 3,4,5
700 | anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
701 | classes=$NUM_CLASSES
702 | num=9
703 | jitter=.3
704 | ignore_thresh = .7
705 | truth_thresh = 1
706 | random=1
707 |
708 |
709 |
710 | [route]
711 | layers = -4
712 |
713 | [convolutional]
714 | batch_normalize=1
715 | filters=128
716 | size=1
717 | stride=1
718 | pad=1
719 | activation=leaky
720 |
721 | [upsample]
722 | stride=2
723 |
724 | [route]
725 | layers = -1, 36
726 |
727 |
728 |
729 | [convolutional]
730 | batch_normalize=1
731 | filters=128
732 | size=1
733 | stride=1
734 | pad=1
735 | activation=leaky
736 |
737 | [convolutional]
738 | batch_normalize=1
739 | size=3
740 | stride=1
741 | pad=1
742 | filters=256
743 | activation=leaky
744 |
745 | [convolutional]
746 | batch_normalize=1
747 | filters=128
748 | size=1
749 | stride=1
750 | pad=1
751 | activation=leaky
752 |
753 | [convolutional]
754 | batch_normalize=1
755 | size=3
756 | stride=1
757 | pad=1
758 | filters=256
759 | activation=leaky
760 |
761 | [convolutional]
762 | batch_normalize=1
763 | filters=128
764 | size=1
765 | stride=1
766 | pad=1
767 | activation=leaky
768 |
769 | [convolutional]
770 | batch_normalize=1
771 | size=3
772 | stride=1
773 | pad=1
774 | filters=256
775 | activation=leaky
776 |
777 | [convolutional]
778 | size=1
779 | stride=1
780 | pad=1
781 | filters=$(expr 3 \* $(expr $NUM_CLASSES \+ 5))
782 | activation=linear
783 |
784 |
785 | [yolo]
786 | mask = 0,1,2
787 | anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
788 | classes=$NUM_CLASSES
789 | num=9
790 | jitter=.3
791 | ignore_thresh = .7
792 | truth_thresh = 1
793 | random=1
794 | " >> yolov3-custom.cfg
795 |
--------------------------------------------------------------------------------
/config/custom.data:
--------------------------------------------------------------------------------
1 | classes= 1
2 | train=data/custom/train.txt
3 | valid=data/custom/valid.txt
4 | names=data/custom/classes.names
5 |
--------------------------------------------------------------------------------
/config/yolov3-tiny.cfg:
--------------------------------------------------------------------------------
1 | [net]
2 | # Testing
3 | batch=1
4 | subdivisions=1
5 | # Training
6 | # batch=64
7 | # subdivisions=2
8 | width=416
9 | height=416
10 | channels=3
11 | momentum=0.9
12 | decay=0.0005
13 | angle=0
14 | saturation = 1.5
15 | exposure = 1.5
16 | hue=.1
17 |
18 | learning_rate=0.001
19 | burn_in=1000
20 | max_batches = 500200
21 | policy=steps
22 | steps=400000,450000
23 | scales=.1,.1
24 |
25 | # 0
26 | [convolutional]
27 | batch_normalize=1
28 | filters=16
29 | size=3
30 | stride=1
31 | pad=1
32 | activation=leaky
33 |
34 | # 1
35 | [maxpool]
36 | size=2
37 | stride=2
38 |
39 | # 2
40 | [convolutional]
41 | batch_normalize=1
42 | filters=32
43 | size=3
44 | stride=1
45 | pad=1
46 | activation=leaky
47 |
48 | # 3
49 | [maxpool]
50 | size=2
51 | stride=2
52 |
53 | # 4
54 | [convolutional]
55 | batch_normalize=1
56 | filters=64
57 | size=3
58 | stride=1
59 | pad=1
60 | activation=leaky
61 |
62 | # 5
63 | [maxpool]
64 | size=2
65 | stride=2
66 |
67 | # 6
68 | [convolutional]
69 | batch_normalize=1
70 | filters=128
71 | size=3
72 | stride=1
73 | pad=1
74 | activation=leaky
75 |
76 | # 7
77 | [maxpool]
78 | size=2
79 | stride=2
80 |
81 | # 8
82 | [convolutional]
83 | batch_normalize=1
84 | filters=256
85 | size=3
86 | stride=1
87 | pad=1
88 | activation=leaky
89 |
90 | # 9
91 | [maxpool]
92 | size=2
93 | stride=2
94 |
95 | # 10
96 | [convolutional]
97 | batch_normalize=1
98 | filters=512
99 | size=3
100 | stride=1
101 | pad=1
102 | activation=leaky
103 |
104 | # 11
105 | [maxpool]
106 | size=2
107 | stride=1
108 |
109 | # 12
110 | [convolutional]
111 | batch_normalize=1
112 | filters=1024
113 | size=3
114 | stride=1
115 | pad=1
116 | activation=leaky
117 |
118 | ###########
119 |
120 | # 13
121 | [convolutional]
122 | batch_normalize=1
123 | filters=256
124 | size=1
125 | stride=1
126 | pad=1
127 | activation=leaky
128 |
129 | # 14
130 | [convolutional]
131 | batch_normalize=1
132 | filters=512
133 | size=3
134 | stride=1
135 | pad=1
136 | activation=leaky
137 |
138 | # 15
139 | [convolutional]
140 | size=1
141 | stride=1
142 | pad=1
143 | filters=255
144 | activation=linear
145 |
146 |
147 |
148 | # 16
149 | [yolo]
150 | mask = 3,4,5
151 | anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
152 | classes=80
153 | num=6
154 | jitter=.3
155 | ignore_thresh = .7
156 | truth_thresh = 1
157 | random=1
158 |
159 | # 17
160 | [route]
161 | layers = -4
162 |
163 | # 18
164 | [convolutional]
165 | batch_normalize=1
166 | filters=128
167 | size=1
168 | stride=1
169 | pad=1
170 | activation=leaky
171 |
172 | # 19
173 | [upsample]
174 | stride=2
175 |
176 | # 20
177 | [route]
178 | layers = -1, 8
179 |
180 | # 21
181 | [convolutional]
182 | batch_normalize=1
183 | filters=256
184 | size=3
185 | stride=1
186 | pad=1
187 | activation=leaky
188 |
189 | # 22
190 | [convolutional]
191 | size=1
192 | stride=1
193 | pad=1
194 | filters=255
195 | activation=linear
196 |
197 | # 23
198 | [yolo]
199 | mask = 1,2,3
200 | anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
201 | classes=80
202 | num=6
203 | jitter=.3
204 | ignore_thresh = .7
205 | truth_thresh = 1
206 | random=1
207 |
--------------------------------------------------------------------------------
/config/yolov3.cfg:
--------------------------------------------------------------------------------
1 | [net]
2 | # Testing
3 | #batch=1
4 | #subdivisions=1
5 | # Training
6 | batch=16
7 | subdivisions=1
8 | width=416
9 | height=416
10 | channels=3
11 | momentum=0.9
12 | decay=0.0005
13 | angle=0
14 | saturation = 1.5
15 | exposure = 1.5
16 | hue=.1
17 |
18 | learning_rate=0.001
19 | burn_in=1000
20 | max_batches = 500200
21 | policy=steps
22 | steps=400000,450000
23 | scales=.1,.1
24 |
25 | [convolutional]
26 | batch_normalize=1
27 | filters=32
28 | size=3
29 | stride=1
30 | pad=1
31 | activation=leaky
32 |
33 | # Downsample
34 |
35 | [convolutional]
36 | batch_normalize=1
37 | filters=64
38 | size=3
39 | stride=2
40 | pad=1
41 | activation=leaky
42 |
43 | [convolutional]
44 | batch_normalize=1
45 | filters=32
46 | size=1
47 | stride=1
48 | pad=1
49 | activation=leaky
50 |
51 | [convolutional]
52 | batch_normalize=1
53 | filters=64
54 | size=3
55 | stride=1
56 | pad=1
57 | activation=leaky
58 |
59 | [shortcut]
60 | from=-3
61 | activation=linear
62 |
63 | # Downsample
64 |
65 | [convolutional]
66 | batch_normalize=1
67 | filters=128
68 | size=3
69 | stride=2
70 | pad=1
71 | activation=leaky
72 |
73 | [convolutional]
74 | batch_normalize=1
75 | filters=64
76 | size=1
77 | stride=1
78 | pad=1
79 | activation=leaky
80 |
81 | [convolutional]
82 | batch_normalize=1
83 | filters=128
84 | size=3
85 | stride=1
86 | pad=1
87 | activation=leaky
88 |
89 | [shortcut]
90 | from=-3
91 | activation=linear
92 |
93 | [convolutional]
94 | batch_normalize=1
95 | filters=64
96 | size=1
97 | stride=1
98 | pad=1
99 | activation=leaky
100 |
101 | [convolutional]
102 | batch_normalize=1
103 | filters=128
104 | size=3
105 | stride=1
106 | pad=1
107 | activation=leaky
108 |
109 | [shortcut]
110 | from=-3
111 | activation=linear
112 |
113 | # Downsample
114 |
115 | [convolutional]
116 | batch_normalize=1
117 | filters=256
118 | size=3
119 | stride=2
120 | pad=1
121 | activation=leaky
122 |
123 | [convolutional]
124 | batch_normalize=1
125 | filters=128
126 | size=1
127 | stride=1
128 | pad=1
129 | activation=leaky
130 |
131 | [convolutional]
132 | batch_normalize=1
133 | filters=256
134 | size=3
135 | stride=1
136 | pad=1
137 | activation=leaky
138 |
139 | [shortcut]
140 | from=-3
141 | activation=linear
142 |
143 | [convolutional]
144 | batch_normalize=1
145 | filters=128
146 | size=1
147 | stride=1
148 | pad=1
149 | activation=leaky
150 |
151 | [convolutional]
152 | batch_normalize=1
153 | filters=256
154 | size=3
155 | stride=1
156 | pad=1
157 | activation=leaky
158 |
159 | [shortcut]
160 | from=-3
161 | activation=linear
162 |
163 | [convolutional]
164 | batch_normalize=1
165 | filters=128
166 | size=1
167 | stride=1
168 | pad=1
169 | activation=leaky
170 |
171 | [convolutional]
172 | batch_normalize=1
173 | filters=256
174 | size=3
175 | stride=1
176 | pad=1
177 | activation=leaky
178 |
179 | [shortcut]
180 | from=-3
181 | activation=linear
182 |
183 | [convolutional]
184 | batch_normalize=1
185 | filters=128
186 | size=1
187 | stride=1
188 | pad=1
189 | activation=leaky
190 |
191 | [convolutional]
192 | batch_normalize=1
193 | filters=256
194 | size=3
195 | stride=1
196 | pad=1
197 | activation=leaky
198 |
199 | [shortcut]
200 | from=-3
201 | activation=linear
202 |
203 |
204 | [convolutional]
205 | batch_normalize=1
206 | filters=128
207 | size=1
208 | stride=1
209 | pad=1
210 | activation=leaky
211 |
212 | [convolutional]
213 | batch_normalize=1
214 | filters=256
215 | size=3
216 | stride=1
217 | pad=1
218 | activation=leaky
219 |
220 | [shortcut]
221 | from=-3
222 | activation=linear
223 |
224 | [convolutional]
225 | batch_normalize=1
226 | filters=128
227 | size=1
228 | stride=1
229 | pad=1
230 | activation=leaky
231 |
232 | [convolutional]
233 | batch_normalize=1
234 | filters=256
235 | size=3
236 | stride=1
237 | pad=1
238 | activation=leaky
239 |
240 | [shortcut]
241 | from=-3
242 | activation=linear
243 |
244 | [convolutional]
245 | batch_normalize=1
246 | filters=128
247 | size=1
248 | stride=1
249 | pad=1
250 | activation=leaky
251 |
252 | [convolutional]
253 | batch_normalize=1
254 | filters=256
255 | size=3
256 | stride=1
257 | pad=1
258 | activation=leaky
259 |
260 | [shortcut]
261 | from=-3
262 | activation=linear
263 |
264 | [convolutional]
265 | batch_normalize=1
266 | filters=128
267 | size=1
268 | stride=1
269 | pad=1
270 | activation=leaky
271 |
272 | [convolutional]
273 | batch_normalize=1
274 | filters=256
275 | size=3
276 | stride=1
277 | pad=1
278 | activation=leaky
279 |
280 | [shortcut]
281 | from=-3
282 | activation=linear
283 |
284 | # Downsample
285 |
286 | [convolutional]
287 | batch_normalize=1
288 | filters=512
289 | size=3
290 | stride=2
291 | pad=1
292 | activation=leaky
293 |
294 | [convolutional]
295 | batch_normalize=1
296 | filters=256
297 | size=1
298 | stride=1
299 | pad=1
300 | activation=leaky
301 |
302 | [convolutional]
303 | batch_normalize=1
304 | filters=512
305 | size=3
306 | stride=1
307 | pad=1
308 | activation=leaky
309 |
310 | [shortcut]
311 | from=-3
312 | activation=linear
313 |
314 |
315 | [convolutional]
316 | batch_normalize=1
317 | filters=256
318 | size=1
319 | stride=1
320 | pad=1
321 | activation=leaky
322 |
323 | [convolutional]
324 | batch_normalize=1
325 | filters=512
326 | size=3
327 | stride=1
328 | pad=1
329 | activation=leaky
330 |
331 | [shortcut]
332 | from=-3
333 | activation=linear
334 |
335 |
336 | [convolutional]
337 | batch_normalize=1
338 | filters=256
339 | size=1
340 | stride=1
341 | pad=1
342 | activation=leaky
343 |
344 | [convolutional]
345 | batch_normalize=1
346 | filters=512
347 | size=3
348 | stride=1
349 | pad=1
350 | activation=leaky
351 |
352 | [shortcut]
353 | from=-3
354 | activation=linear
355 |
356 |
357 | [convolutional]
358 | batch_normalize=1
359 | filters=256
360 | size=1
361 | stride=1
362 | pad=1
363 | activation=leaky
364 |
365 | [convolutional]
366 | batch_normalize=1
367 | filters=512
368 | size=3
369 | stride=1
370 | pad=1
371 | activation=leaky
372 |
373 | [shortcut]
374 | from=-3
375 | activation=linear
376 |
377 | [convolutional]
378 | batch_normalize=1
379 | filters=256
380 | size=1
381 | stride=1
382 | pad=1
383 | activation=leaky
384 |
385 | [convolutional]
386 | batch_normalize=1
387 | filters=512
388 | size=3
389 | stride=1
390 | pad=1
391 | activation=leaky
392 |
393 | [shortcut]
394 | from=-3
395 | activation=linear
396 |
397 |
398 | [convolutional]
399 | batch_normalize=1
400 | filters=256
401 | size=1
402 | stride=1
403 | pad=1
404 | activation=leaky
405 |
406 | [convolutional]
407 | batch_normalize=1
408 | filters=512
409 | size=3
410 | stride=1
411 | pad=1
412 | activation=leaky
413 |
414 | [shortcut]
415 | from=-3
416 | activation=linear
417 |
418 |
419 | [convolutional]
420 | batch_normalize=1
421 | filters=256
422 | size=1
423 | stride=1
424 | pad=1
425 | activation=leaky
426 |
427 | [convolutional]
428 | batch_normalize=1
429 | filters=512
430 | size=3
431 | stride=1
432 | pad=1
433 | activation=leaky
434 |
435 | [shortcut]
436 | from=-3
437 | activation=linear
438 |
439 | [convolutional]
440 | batch_normalize=1
441 | filters=256
442 | size=1
443 | stride=1
444 | pad=1
445 | activation=leaky
446 |
447 | [convolutional]
448 | batch_normalize=1
449 | filters=512
450 | size=3
451 | stride=1
452 | pad=1
453 | activation=leaky
454 |
455 | [shortcut]
456 | from=-3
457 | activation=linear
458 |
459 | # Downsample
460 |
461 | [convolutional]
462 | batch_normalize=1
463 | filters=1024
464 | size=3
465 | stride=2
466 | pad=1
467 | activation=leaky
468 |
469 | [convolutional]
470 | batch_normalize=1
471 | filters=512
472 | size=1
473 | stride=1
474 | pad=1
475 | activation=leaky
476 |
477 | [convolutional]
478 | batch_normalize=1
479 | filters=1024
480 | size=3
481 | stride=1
482 | pad=1
483 | activation=leaky
484 |
485 | [shortcut]
486 | from=-3
487 | activation=linear
488 |
489 | [convolutional]
490 | batch_normalize=1
491 | filters=512
492 | size=1
493 | stride=1
494 | pad=1
495 | activation=leaky
496 |
497 | [convolutional]
498 | batch_normalize=1
499 | filters=1024
500 | size=3
501 | stride=1
502 | pad=1
503 | activation=leaky
504 |
505 | [shortcut]
506 | from=-3
507 | activation=linear
508 |
509 | [convolutional]
510 | batch_normalize=1
511 | filters=512
512 | size=1
513 | stride=1
514 | pad=1
515 | activation=leaky
516 |
517 | [convolutional]
518 | batch_normalize=1
519 | filters=1024
520 | size=3
521 | stride=1
522 | pad=1
523 | activation=leaky
524 |
525 | [shortcut]
526 | from=-3
527 | activation=linear
528 |
529 | [convolutional]
530 | batch_normalize=1
531 | filters=512
532 | size=1
533 | stride=1
534 | pad=1
535 | activation=leaky
536 |
537 | [convolutional]
538 | batch_normalize=1
539 | filters=1024
540 | size=3
541 | stride=1
542 | pad=1
543 | activation=leaky
544 |
545 | [shortcut]
546 | from=-3
547 | activation=linear
548 |
549 | ######################
550 |
551 | [convolutional]
552 | batch_normalize=1
553 | filters=512
554 | size=1
555 | stride=1
556 | pad=1
557 | activation=leaky
558 |
559 | [convolutional]
560 | batch_normalize=1
561 | size=3
562 | stride=1
563 | pad=1
564 | filters=1024
565 | activation=leaky
566 |
567 | [convolutional]
568 | batch_normalize=1
569 | filters=512
570 | size=1
571 | stride=1
572 | pad=1
573 | activation=leaky
574 |
575 | [convolutional]
576 | batch_normalize=1
577 | size=3
578 | stride=1
579 | pad=1
580 | filters=1024
581 | activation=leaky
582 |
583 | [convolutional]
584 | batch_normalize=1
585 | filters=512
586 | size=1
587 | stride=1
588 | pad=1
589 | activation=leaky
590 |
591 | [convolutional]
592 | batch_normalize=1
593 | size=3
594 | stride=1
595 | pad=1
596 | filters=1024
597 | activation=leaky
598 |
599 | [convolutional]
600 | size=1
601 | stride=1
602 | pad=1
603 | filters=255
604 | activation=linear
605 |
606 |
607 | [yolo]
608 | mask = 6,7,8
609 | anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
610 | classes=80
611 | num=9
612 | jitter=.3
613 | ignore_thresh = .7
614 | truth_thresh = 1
615 | random=1
616 |
617 |
618 | [route]
619 | layers = -4
620 |
621 | [convolutional]
622 | batch_normalize=1
623 | filters=256
624 | size=1
625 | stride=1
626 | pad=1
627 | activation=leaky
628 |
629 | [upsample]
630 | stride=2
631 |
632 | [route]
633 | layers = -1, 61
634 |
635 |
636 |
637 | [convolutional]
638 | batch_normalize=1
639 | filters=256
640 | size=1
641 | stride=1
642 | pad=1
643 | activation=leaky
644 |
645 | [convolutional]
646 | batch_normalize=1
647 | size=3
648 | stride=1
649 | pad=1
650 | filters=512
651 | activation=leaky
652 |
653 | [convolutional]
654 | batch_normalize=1
655 | filters=256
656 | size=1
657 | stride=1
658 | pad=1
659 | activation=leaky
660 |
661 | [convolutional]
662 | batch_normalize=1
663 | size=3
664 | stride=1
665 | pad=1
666 | filters=512
667 | activation=leaky
668 |
669 | [convolutional]
670 | batch_normalize=1
671 | filters=256
672 | size=1
673 | stride=1
674 | pad=1
675 | activation=leaky
676 |
677 | [convolutional]
678 | batch_normalize=1
679 | size=3
680 | stride=1
681 | pad=1
682 | filters=512
683 | activation=leaky
684 |
685 | [convolutional]
686 | size=1
687 | stride=1
688 | pad=1
689 | filters=255
690 | activation=linear
691 |
692 |
693 | [yolo]
694 | mask = 3,4,5
695 | anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
696 | classes=80
697 | num=9
698 | jitter=.3
699 | ignore_thresh = .7
700 | truth_thresh = 1
701 | random=1
702 |
703 |
704 |
705 | [route]
706 | layers = -4
707 |
708 | [convolutional]
709 | batch_normalize=1
710 | filters=128
711 | size=1
712 | stride=1
713 | pad=1
714 | activation=leaky
715 |
716 | [upsample]
717 | stride=2
718 |
719 | [route]
720 | layers = -1, 36
721 |
722 |
723 |
724 | [convolutional]
725 | batch_normalize=1
726 | filters=128
727 | size=1
728 | stride=1
729 | pad=1
730 | activation=leaky
731 |
732 | [convolutional]
733 | batch_normalize=1
734 | size=3
735 | stride=1
736 | pad=1
737 | filters=256
738 | activation=leaky
739 |
740 | [convolutional]
741 | batch_normalize=1
742 | filters=128
743 | size=1
744 | stride=1
745 | pad=1
746 | activation=leaky
747 |
748 | [convolutional]
749 | batch_normalize=1
750 | size=3
751 | stride=1
752 | pad=1
753 | filters=256
754 | activation=leaky
755 |
756 | [convolutional]
757 | batch_normalize=1
758 | filters=128
759 | size=1
760 | stride=1
761 | pad=1
762 | activation=leaky
763 |
764 | [convolutional]
765 | batch_normalize=1
766 | size=3
767 | stride=1
768 | pad=1
769 | filters=256
770 | activation=leaky
771 |
772 | [convolutional]
773 | size=1
774 | stride=1
775 | pad=1
776 | filters=255
777 | activation=linear
778 |
779 |
780 | [yolo]
781 | mask = 0,1,2
782 | anchors = 10,13, 16,30, 33,23, 30,61, 62,45, 59,119, 116,90, 156,198, 373,326
783 | classes=80
784 | num=9
785 | jitter=.3
786 | ignore_thresh = .7
787 | truth_thresh = 1
788 | random=1
789 |
--------------------------------------------------------------------------------
/data/coco.names:
--------------------------------------------------------------------------------
1 | person
2 | bicycle
3 | car
4 | motorbike
5 | aeroplane
6 | bus
7 | train
8 | truck
9 | boat
10 | traffic light
11 | fire hydrant
12 | stop sign
13 | parking meter
14 | bench
15 | bird
16 | cat
17 | dog
18 | horse
19 | sheep
20 | cow
21 | elephant
22 | bear
23 | zebra
24 | giraffe
25 | backpack
26 | umbrella
27 | handbag
28 | tie
29 | suitcase
30 | frisbee
31 | skis
32 | snowboard
33 | sports ball
34 | kite
35 | baseball bat
36 | baseball glove
37 | skateboard
38 | surfboard
39 | tennis racket
40 | bottle
41 | wine glass
42 | cup
43 | fork
44 | knife
45 | spoon
46 | bowl
47 | banana
48 | apple
49 | sandwich
50 | orange
51 | broccoli
52 | carrot
53 | hot dog
54 | pizza
55 | donut
56 | cake
57 | chair
58 | sofa
59 | pottedplant
60 | bed
61 | diningtable
62 | toilet
63 | tvmonitor
64 | laptop
65 | mouse
66 | remote
67 | keyboard
68 | cell phone
69 | microwave
70 | oven
71 | toaster
72 | sink
73 | refrigerator
74 | book
75 | clock
76 | vase
77 | scissors
78 | teddy bear
79 | hair drier
80 | toothbrush
81 |
--------------------------------------------------------------------------------
/data/custom/classes.names:
--------------------------------------------------------------------------------
1 | train
2 |
--------------------------------------------------------------------------------
/data/custom/images/train.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/custom/images/train.jpg
--------------------------------------------------------------------------------
/data/custom/labels/train.txt:
--------------------------------------------------------------------------------
1 | 0 0.515 0.5 0.21694873 0.18286777
2 |
--------------------------------------------------------------------------------
/data/custom/train.txt:
--------------------------------------------------------------------------------
1 | data/custom/images/train.jpg
2 |
--------------------------------------------------------------------------------
/data/custom/valid.txt:
--------------------------------------------------------------------------------
1 | data/custom/images/train.jpg
2 |
--------------------------------------------------------------------------------
/data/get_coco_dataset.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # CREDIT: https://github.com/pjreddie/darknet/tree/master/scripts/get_coco_dataset.sh
4 |
5 | # Clone COCO API
6 | git clone https://github.com/pdollar/coco
7 | cd coco
8 |
9 | mkdir images
10 | cd images
11 |
12 | # Download Images
13 | wget -c https://pjreddie.com/media/files/train2014.zip
14 | wget -c https://pjreddie.com/media/files/val2014.zip
15 |
16 | # Unzip
17 | unzip -q train2014.zip
18 | unzip -q val2014.zip
19 |
20 | cd ..
21 |
22 | # Download COCO Metadata
23 | wget -c https://pjreddie.com/media/files/instances_train-val2014.zip
24 | wget -c https://pjreddie.com/media/files/coco/5k.part
25 | wget -c https://pjreddie.com/media/files/coco/trainvalno5k.part
26 | wget -c https://pjreddie.com/media/files/coco/labels.tgz
27 | tar xzf labels.tgz
28 | unzip -q instances_train-val2014.zip
29 |
30 | # Set Up Image Lists
31 | paste <(awk "{print \"$PWD\"}" <5k.part) 5k.part | tr -d '\t' > 5k.txt
32 | paste <(awk "{print \"$PWD\"}" trainvalno5k.txt
33 |
--------------------------------------------------------------------------------
/data/samples/dog.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/dog.jpg
--------------------------------------------------------------------------------
/data/samples/eagle.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/eagle.jpg
--------------------------------------------------------------------------------
/data/samples/field.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/field.jpg
--------------------------------------------------------------------------------
/data/samples/giraffe.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/giraffe.jpg
--------------------------------------------------------------------------------
/data/samples/herd_of_horses.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/herd_of_horses.jpg
--------------------------------------------------------------------------------
/data/samples/messi.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/messi.jpg
--------------------------------------------------------------------------------
/data/samples/person.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/person.jpg
--------------------------------------------------------------------------------
/data/samples/room.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/room.jpg
--------------------------------------------------------------------------------
/data/samples/street.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/data/samples/street.jpg
--------------------------------------------------------------------------------
/image/09979.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/09979.jpg
--------------------------------------------------------------------------------
/image/09981.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/09981.jpg
--------------------------------------------------------------------------------
/image/09982.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/09982.jpg
--------------------------------------------------------------------------------
/image/09983.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/09983.jpg
--------------------------------------------------------------------------------
/image/10966.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/10966.jpg
--------------------------------------------------------------------------------
/image/10969.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/10969.jpg
--------------------------------------------------------------------------------
/image/10971.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/10971.jpg
--------------------------------------------------------------------------------
/image/10976.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/10976.jpg
--------------------------------------------------------------------------------
/image/anchor.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/anchor.jpg
--------------------------------------------------------------------------------
/image/mouse1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/mouse1.jpg
--------------------------------------------------------------------------------
/image/mouse2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/mouse2.jpg
--------------------------------------------------------------------------------
/image/mouse3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/mouse3.jpg
--------------------------------------------------------------------------------
/image/mouse4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/mouse4.jpg
--------------------------------------------------------------------------------
/image/performance.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/performance.jpg
--------------------------------------------------------------------------------
/image/yolo-struct.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/image/yolo-struct.jpg
--------------------------------------------------------------------------------
/models.py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 |
3 | import torch
4 | import torch.nn as nn
5 | import torch.nn.functional as F
6 | from torch.autograd import Variable
7 | import numpy as np
8 |
9 | from utils.parse_config import *
10 | from utils.utils import build_targets, to_cpu, non_max_suppression
11 |
12 | import matplotlib.pyplot as plt
13 | import matplotlib.patches as patches
14 |
15 |
16 | def create_modules(module_defs):
17 | """
18 | Constructs module list of layer blocks from module configuration in module_defs
19 | """
20 | hyperparams = module_defs.pop(0)
21 | output_filters = [int(hyperparams["channels"])]
22 | module_list = nn.ModuleList()
23 | for module_i, module_def in enumerate(module_defs):
24 | modules = nn.Sequential()
25 |
26 | if module_def["type"] == "convolutional":
27 | bn = int(module_def["batch_normalize"])
28 | filters = int(module_def["filters"])
29 | kernel_size = int(module_def["size"])
30 | pad = (kernel_size - 1) // 2
31 | modules.add_module(
32 | f"conv_{module_i}",
33 | nn.Conv2d(
34 | in_channels=output_filters[-1],
35 | out_channels=filters,
36 | kernel_size=kernel_size,
37 | stride=int(module_def["stride"]),
38 | padding=pad,
39 | bias=not bn,
40 | ),
41 | )
42 | if bn:
43 | modules.add_module(f"batch_norm_{module_i}", nn.BatchNorm2d(filters, momentum=0.9, eps=1e-5))
44 | if module_def["activation"] == "leaky":
45 | modules.add_module(f"leaky_{module_i}", nn.LeakyReLU(0.1))
46 |
47 | elif module_def["type"] == "maxpool":
48 | kernel_size = int(module_def["size"])
49 | stride = int(module_def["stride"])
50 | if kernel_size == 2 and stride == 1:
51 | modules.add_module(f"_debug_padding_{module_i}", nn.ZeroPad2d((0, 1, 0, 1)))
52 | maxpool = nn.MaxPool2d(kernel_size=kernel_size, stride=stride, padding=int((kernel_size - 1) // 2))
53 | modules.add_module(f"maxpool_{module_i}", maxpool)
54 |
55 | elif module_def["type"] == "upsample":
56 | upsample = Upsample(scale_factor=int(module_def["stride"]), mode="nearest")
57 | modules.add_module(f"upsample_{module_i}", upsample)
58 |
59 | elif module_def["type"] == "route":
60 | layers = [int(x) for x in module_def["layers"].split(",")]
61 | filters = sum([output_filters[1:][i] for i in layers])
62 | modules.add_module(f"route_{module_i}", EmptyLayer())
63 |
64 | elif module_def["type"] == "shortcut":
65 | filters = output_filters[1:][int(module_def["from"])]
66 | modules.add_module(f"shortcut_{module_i}", EmptyLayer())
67 |
68 | elif module_def["type"] == "yolo":
69 | anchor_idxs = [int(x) for x in module_def["mask"].split(",")]
70 | # Extract anchors
71 | anchors = [int(x) for x in module_def["anchors"].split(",")]
72 | anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)]
73 | anchors = [anchors[i] for i in anchor_idxs]
74 | num_classes = int(module_def["classes"])
75 | img_size = int(hyperparams["height"])
76 | # Define detection layer
77 | yolo_layer = YOLOLayer(anchors, num_classes, img_size)
78 | modules.add_module(f"yolo_{module_i}", yolo_layer)
79 | # Register module list and number of output filters
80 | module_list.append(modules)
81 | output_filters.append(filters)
82 |
83 | return hyperparams, module_list
84 |
85 |
86 | class Upsample(nn.Module):
87 | """ nn.Upsample is deprecated """
88 |
89 | def __init__(self, scale_factor, mode="nearest"):
90 | super(Upsample, self).__init__()
91 | self.scale_factor = scale_factor
92 | self.mode = mode
93 |
94 | def forward(self, x):
95 | x = F.interpolate(x, scale_factor=self.scale_factor, mode=self.mode)
96 | return x
97 |
98 |
99 | class EmptyLayer(nn.Module):
100 | """Placeholder for 'route' and 'shortcut' layers"""
101 |
102 | def __init__(self):
103 | super(EmptyLayer, self).__init__()
104 |
105 |
106 | class YOLOLayer(nn.Module):
107 | """Detection layer"""
108 |
109 | def __init__(self, anchors, num_classes, img_dim=416):
110 | super(YOLOLayer, self).__init__()
111 | self.anchors = anchors
112 | self.num_anchors = len(anchors)
113 | self.num_classes = num_classes
114 | self.ignore_thres = 0.5
115 | self.mse_loss = nn.MSELoss()
116 | self.bce_loss = nn.BCELoss()
117 | self.obj_scale = 1
118 | self.noobj_scale = 100
119 | self.metrics = {}
120 | self.img_dim = img_dim
121 | self.grid_size = 0 # grid size
122 |
123 | def compute_grid_offsets(self, grid_size, cuda=True):
124 | self.grid_size = grid_size
125 | g = self.grid_size
126 | FloatTensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
127 | self.stride = self.img_dim / self.grid_size
128 | # Calculate offsets for each grid
129 | self.grid_x = torch.arange(g).repeat(g, 1).view([1, 1, g, g]).type(FloatTensor)
130 | self.grid_y = torch.arange(g).repeat(g, 1).t().view([1, 1, g, g]).type(FloatTensor)
131 | self.scaled_anchors = FloatTensor([(a_w / self.stride, a_h / self.stride) for a_w, a_h in self.anchors])
132 | self.anchor_w = self.scaled_anchors[:, 0:1].view((1, self.num_anchors, 1, 1))
133 | self.anchor_h = self.scaled_anchors[:, 1:2].view((1, self.num_anchors, 1, 1))
134 |
135 | def forward(self, x, targets=None, img_dim=None):
136 |
137 | # Tensors for cuda support
138 | FloatTensor = torch.cuda.FloatTensor if x.is_cuda else torch.FloatTensor
139 | LongTensor = torch.cuda.LongTensor if x.is_cuda else torch.LongTensor
140 | ByteTensor = torch.cuda.ByteTensor if x.is_cuda else torch.ByteTensor
141 |
142 | self.img_dim = img_dim
143 | num_samples = x.size(0)
144 | grid_size = x.size(2)
145 |
146 | prediction = (
147 | x.view(num_samples, self.num_anchors, self.num_classes + 5, grid_size, grid_size)
148 | .permute(0, 1, 3, 4, 2)
149 | .contiguous()
150 | )
151 |
152 | # Get outputs
153 | x = torch.sigmoid(prediction[..., 0]) # Center x
154 | y = torch.sigmoid(prediction[..., 1]) # Center y
155 | w = prediction[..., 2] # Width
156 | h = prediction[..., 3] # Height
157 | pred_conf = torch.sigmoid(prediction[..., 4]) # Conf
158 | pred_cls = torch.sigmoid(prediction[..., 5:]) # Cls pred.
159 |
160 | # If grid size does not match current we compute new offsets
161 | if grid_size != self.grid_size:
162 | self.compute_grid_offsets(grid_size, cuda=x.is_cuda)
163 |
164 | # Add offset and scale with anchors
165 | pred_boxes = FloatTensor(prediction[..., :4].shape)
166 | pred_boxes[..., 0] = x.data + self.grid_x
167 | pred_boxes[..., 1] = y.data + self.grid_y
168 | pred_boxes[..., 2] = torch.exp(w.data) * self.anchor_w
169 | pred_boxes[..., 3] = torch.exp(h.data) * self.anchor_h
170 |
171 | output = torch.cat(
172 | (
173 | pred_boxes.view(num_samples, -1, 4) * self.stride,
174 | pred_conf.view(num_samples, -1, 1),
175 | pred_cls.view(num_samples, -1, self.num_classes),
176 | ),
177 | -1,
178 | )
179 |
180 | if targets is None:
181 | return output, 0
182 | else:
183 | iou_scores, class_mask, obj_mask, noobj_mask, tx, ty, tw, th, tcls, tconf = build_targets(
184 | pred_boxes=pred_boxes,
185 | pred_cls=pred_cls,
186 | target=targets,
187 | anchors=self.scaled_anchors,
188 | ignore_thres=self.ignore_thres,
189 | )
190 |
191 | # Loss : Mask outputs to ignore non-existing objects (except with conf. loss)
192 | loss_x = self.mse_loss(x[obj_mask], tx[obj_mask])
193 | loss_y = self.mse_loss(y[obj_mask], ty[obj_mask])
194 | loss_w = self.mse_loss(w[obj_mask], tw[obj_mask])
195 | loss_h = self.mse_loss(h[obj_mask], th[obj_mask])
196 | loss_conf_obj = self.bce_loss(pred_conf[obj_mask], tconf[obj_mask])
197 | loss_conf_noobj = self.bce_loss(pred_conf[noobj_mask], tconf[noobj_mask])
198 | loss_conf = self.obj_scale * loss_conf_obj + self.noobj_scale * loss_conf_noobj
199 | loss_cls = self.bce_loss(pred_cls[obj_mask], tcls[obj_mask])
200 | total_loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls
201 |
202 | # Metrics
203 | cls_acc = 100 * class_mask[obj_mask].mean()
204 | conf_obj = pred_conf[obj_mask].mean()
205 | conf_noobj = pred_conf[noobj_mask].mean()
206 | conf50 = (pred_conf > 0.5).float()
207 | iou50 = (iou_scores > 0.5).float()
208 | iou75 = (iou_scores > 0.75).float()
209 | detected_mask = conf50 * class_mask * tconf
210 | precision = torch.sum(iou50 * detected_mask) / (conf50.sum() + 1e-16)
211 | recall50 = torch.sum(iou50 * detected_mask) / (obj_mask.sum() + 1e-16)
212 | recall75 = torch.sum(iou75 * detected_mask) / (obj_mask.sum() + 1e-16)
213 |
214 | self.metrics = {
215 | "loss": to_cpu(total_loss).item(),
216 | "x": to_cpu(loss_x).item(),
217 | "y": to_cpu(loss_y).item(),
218 | "w": to_cpu(loss_w).item(),
219 | "h": to_cpu(loss_h).item(),
220 | "conf": to_cpu(loss_conf).item(),
221 | "cls": to_cpu(loss_cls).item(),
222 | "cls_acc": to_cpu(cls_acc).item(),
223 | "recall50": to_cpu(recall50).item(),
224 | "recall75": to_cpu(recall75).item(),
225 | "precision": to_cpu(precision).item(),
226 | "conf_obj": to_cpu(conf_obj).item(),
227 | "conf_noobj": to_cpu(conf_noobj).item(),
228 | "grid_size": grid_size,
229 | }
230 |
231 | return output, total_loss
232 |
233 |
234 | class Darknet(nn.Module):
235 | """YOLOv3 object detection model"""
236 |
237 | def __init__(self, config_path, img_size=416):
238 | super(Darknet, self).__init__()
239 | self.module_defs = parse_model_config(config_path)
240 | self.hyperparams, self.module_list = create_modules(self.module_defs)
241 | self.yolo_layers = [layer[0] for layer in self.module_list if hasattr(layer[0], "metrics")]
242 | self.img_size = img_size
243 | self.seen = 0
244 | self.header_info = np.array([0, 0, 0, self.seen, 0], dtype=np.int32)
245 |
246 | def forward(self, x, targets=None):
247 | img_dim = x.shape[2]
248 | loss = 0
249 | layer_outputs, yolo_outputs = [], []
250 | for i, (module_def, module) in enumerate(zip(self.module_defs, self.module_list)):
251 | if module_def["type"] in ["convolutional", "upsample", "maxpool"]:
252 | x = module(x)
253 | elif module_def["type"] == "route":
254 | x = torch.cat([layer_outputs[int(layer_i)] for layer_i in module_def["layers"].split(",")], 1)
255 | elif module_def["type"] == "shortcut":
256 | layer_i = int(module_def["from"])
257 | x = layer_outputs[-1] + layer_outputs[layer_i]
258 | elif module_def["type"] == "yolo":
259 | x, layer_loss = module[0](x, targets, img_dim)
260 | loss += layer_loss
261 | yolo_outputs.append(x)
262 | layer_outputs.append(x)
263 | yolo_outputs = to_cpu(torch.cat(yolo_outputs, 1))
264 | return yolo_outputs if targets is None else (loss, yolo_outputs)
265 |
266 | def load_darknet_weights(self, weights_path):
267 | """Parses and loads the weights stored in 'weights_path'"""
268 |
269 | # Open the weights file
270 | with open(weights_path, "rb") as f:
271 | header = np.fromfile(f, dtype=np.int32, count=5) # First five are header values
272 | self.header_info = header # Needed to write header when saving weights
273 | self.seen = header[3] # number of images seen during training
274 | weights = np.fromfile(f, dtype=np.float32) # The rest are weights
275 |
276 | # Establish cutoff for loading backbone weights
277 | cutoff = None
278 | if "darknet53.conv.74" in weights_path:
279 | cutoff = 75
280 |
281 | ptr = 0
282 | for i, (module_def, module) in enumerate(zip(self.module_defs, self.module_list)):
283 | if i == cutoff:
284 | break
285 | if module_def["type"] == "convolutional":
286 | conv_layer = module[0]
287 | if module_def["batch_normalize"]:
288 | # Load BN bias, weights, running mean and running variance
289 | bn_layer = module[1]
290 | num_b = bn_layer.bias.numel() # Number of biases
291 | # Bias
292 | bn_b = torch.from_numpy(weights[ptr : ptr + num_b]).view_as(bn_layer.bias)
293 | bn_layer.bias.data.copy_(bn_b)
294 | ptr += num_b
295 | # Weight
296 | bn_w = torch.from_numpy(weights[ptr : ptr + num_b]).view_as(bn_layer.weight)
297 | bn_layer.weight.data.copy_(bn_w)
298 | ptr += num_b
299 | # Running Mean
300 | bn_rm = torch.from_numpy(weights[ptr : ptr + num_b]).view_as(bn_layer.running_mean)
301 | bn_layer.running_mean.data.copy_(bn_rm)
302 | ptr += num_b
303 | # Running Var
304 | bn_rv = torch.from_numpy(weights[ptr : ptr + num_b]).view_as(bn_layer.running_var)
305 | bn_layer.running_var.data.copy_(bn_rv)
306 | ptr += num_b
307 | else:
308 | # Load conv. bias
309 | num_b = conv_layer.bias.numel()
310 | conv_b = torch.from_numpy(weights[ptr : ptr + num_b]).view_as(conv_layer.bias)
311 | conv_layer.bias.data.copy_(conv_b)
312 | ptr += num_b
313 | # Load conv. weights
314 | num_w = conv_layer.weight.numel()
315 | conv_w = torch.from_numpy(weights[ptr : ptr + num_w]).view_as(conv_layer.weight)
316 | conv_layer.weight.data.copy_(conv_w)
317 | ptr += num_w
318 |
319 | def save_darknet_weights(self, path, cutoff=-1):
320 | """
321 | @:param path - path of the new weights file
322 | @:param cutoff - save layers between 0 and cutoff (cutoff = -1 -> all are saved)
323 | """
324 | fp = open(path, "wb")
325 | self.header_info[3] = self.seen
326 | self.header_info.tofile(fp)
327 |
328 | # Iterate through layers
329 | for i, (module_def, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
330 | if module_def["type"] == "convolutional":
331 | conv_layer = module[0]
332 | # If batch norm, load bn first
333 | if module_def["batch_normalize"]:
334 | bn_layer = module[1]
335 | bn_layer.bias.data.cpu().numpy().tofile(fp)
336 | bn_layer.weight.data.cpu().numpy().tofile(fp)
337 | bn_layer.running_mean.data.cpu().numpy().tofile(fp)
338 | bn_layer.running_var.data.cpu().numpy().tofile(fp)
339 | # Load conv bias
340 | else:
341 | conv_layer.bias.data.cpu().numpy().tofile(fp)
342 | # Load conv weights
343 | conv_layer.weight.data.cpu().numpy().tofile(fp)
344 |
345 | fp.close()
346 |
--------------------------------------------------------------------------------
/predict.py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 |
3 | from models import *
4 | from utils.utils import *
5 | from utils.datasets import *
6 |
7 | import os
8 | import sys
9 | import time
10 | import datetime
11 | import argparse
12 |
13 | from PIL import Image
14 |
15 | import torch
16 | from torch.utils.data import DataLoader
17 | from torchvision import datasets
18 | from torch.autograd import Variable
19 |
20 | import matplotlib.pyplot as plt
21 | import matplotlib.patches as patches
22 | from matplotlib.ticker import NullLocator
23 |
24 | if __name__ == "__main__":
25 | parser = argparse.ArgumentParser()
26 | parser.add_argument("--image_folder", type=str, default="data/samples", help="path to dataset")
27 | parser.add_argument("--model_def", type=str, default="config/yolov3.cfg", help="path to model definition file")
28 | parser.add_argument("--weights_path", type=str, default="weights/yolov3.weights", help="path to weights file")
29 | parser.add_argument("--class_path", type=str, default="data/coco.names", help="path to class label file")
30 | parser.add_argument("--conf_thres", type=float, default=0.8, help="object confidence threshold")
31 | parser.add_argument("--nms_thres", type=float, default=0.4, help="iou thresshold for non-maximum suppression")
32 | parser.add_argument("--batch_size", type=int, default=1, help="size of the batches")
33 | parser.add_argument("--n_cpu", type=int, default=0, help="number of cpu threads to use during batch generation")
34 | parser.add_argument("--img_size", type=int, default=416, help="size of each image dimension")
35 | parser.add_argument("--checkpoint_model", type=str, help="path to checkpoint model")
36 | opt = parser.parse_args()
37 | print(opt)
38 |
39 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
40 |
41 | os.makedirs("output", exist_ok=True)
42 |
43 | # Set up model
44 | model = Darknet(opt.model_def, img_size=opt.img_size).to(device)
45 |
46 | if opt.weights_path.endswith(".weights"):
47 | # Load darknet weights
48 | model.load_darknet_weights(opt.weights_path)
49 | else:
50 | # Load checkpoint weights
51 | model.load_state_dict(torch.load(opt.weights_path))
52 |
53 | model.eval() # Set in evaluation mode
54 |
55 | dataloader = DataLoader(
56 | ImageFolder(opt.image_folder, img_size=opt.img_size),
57 | batch_size=opt.batch_size,
58 | shuffle=False,
59 | num_workers=opt.n_cpu,
60 | )
61 |
62 | classes = load_classes(opt.class_path) # Extracts class labels from file
63 |
64 | Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
65 |
66 | imgs = [] # Stores image paths
67 | img_detections = [] # Stores detections for each image index
68 |
69 | print("\nPerforming object detection:")
70 | prev_time = time.time()
71 | for batch_i, (img_paths, input_imgs) in enumerate(dataloader):
72 | # Configure input
73 | input_imgs = Variable(input_imgs.type(Tensor))
74 |
75 | # Get detections
76 | with torch.no_grad():
77 | detections = model(input_imgs)
78 | detections = non_max_suppression(detections, opt.conf_thres, opt.nms_thres)
79 |
80 | # Log progress
81 | current_time = time.time()
82 | inference_time = datetime.timedelta(seconds=current_time - prev_time)
83 | prev_time = current_time
84 | print("\t+ Batch %d, Inference Time: %s" % (batch_i, inference_time))
85 |
86 | # Save image and detections
87 | imgs.extend(img_paths)
88 | img_detections.extend(detections)
89 |
90 | # Bounding-box colors
91 | cmap = plt.get_cmap("tab20b")
92 | colors = [cmap(i) for i in np.linspace(0, 1, 20)]
93 |
94 | print("\nSaving images:")
95 | # Iterate through images and save plot of detections
96 | for img_i, (path, detections) in enumerate(zip(imgs, img_detections)):
97 |
98 | print("(%d) Image: '%s'" % (img_i, path))
99 |
100 | # Create plot
101 | img = np.array(Image.open(path))
102 | plt.figure()
103 | fig, ax = plt.subplots(1)
104 | ax.imshow(img)
105 |
106 | # Draw bounding boxes and labels of detections
107 | if detections is not None:
108 | # Rescale boxes to original image
109 | detections = rescale_boxes(detections, opt.img_size, img.shape[:2])
110 | unique_labels = detections[:, -1].cpu().unique()
111 | n_cls_preds = len(unique_labels)
112 | bbox_colors = random.sample(colors, n_cls_preds)
113 | for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:
114 |
115 | print("\t+ Label: %s, Conf: %.5f" % (classes[int(cls_pred)], cls_conf.item()))
116 |
117 | box_w = x2 - x1
118 | box_h = y2 - y1
119 |
120 | color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]
121 | # Create a Rectangle patch
122 | bbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2, edgecolor=color, facecolor="none")
123 | # Add the bbox to the plot
124 | ax.add_patch(bbox)
125 | # Add label
126 | plt.text(
127 | x1,
128 | y1,
129 | s=classes[int(cls_pred)],
130 | color="white",
131 | verticalalignment="top",
132 | bbox={"color": color, "pad": 0},
133 | )
134 |
135 | # Save generated image with detections
136 | plt.axis("off")
137 | plt.gca().xaxis.set_major_locator(NullLocator())
138 | plt.gca().yaxis.set_major_locator(NullLocator())
139 | filename = path.split("/")[-1].split(".")[0]
140 | plt.savefig(f"output/{filename}.png", bbox_inches="tight", pad_inches=0.0)
141 | plt.close()
142 |
--------------------------------------------------------------------------------
/test.py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 |
3 | from models import *
4 | from utils.utils import *
5 | from utils.datasets import *
6 | from utils.parse_config import *
7 |
8 | import os
9 | import sys
10 | import time
11 | import datetime
12 | import argparse
13 | import tqdm
14 |
15 | import torch
16 | from torch.utils.data import DataLoader
17 | from torchvision import datasets
18 | from torchvision import transforms
19 | from torch.autograd import Variable
20 | import torch.optim as optim
21 |
22 |
23 | def evaluate(model, path, iou_thres, conf_thres, nms_thres, img_size, batch_size):
24 | model.eval()
25 |
26 | # Get dataloader
27 | dataset = ListDataset(path, img_size=img_size, augment=False, multiscale=False)
28 | dataloader = torch.utils.data.DataLoader(
29 | dataset, batch_size=batch_size, shuffle=False, num_workers=1, collate_fn=dataset.collate_fn
30 | )
31 |
32 | Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
33 |
34 | labels = []
35 | sample_metrics = [] # List of tuples (TP, confs, pred)
36 | for batch_i, (_, imgs, targets) in enumerate(tqdm.tqdm(dataloader, desc="Detecting objects")):
37 |
38 | # Extract labels
39 | labels += targets[:, 1].tolist()
40 | # Rescale target
41 | targets[:, 2:] = xywh2xyxy(targets[:, 2:])
42 | targets[:, 2:] *= img_size
43 |
44 | imgs = Variable(imgs.type(Tensor), requires_grad=False)
45 |
46 | with torch.no_grad():
47 | outputs = model(imgs)
48 | outputs = non_max_suppression(outputs, conf_thres=conf_thres, nms_thres=nms_thres)
49 |
50 | sample_metrics += get_batch_statistics(outputs, targets, iou_threshold=iou_thres)
51 |
52 | # Concatenate sample statistics
53 | true_positives, pred_scores, pred_labels = [np.concatenate(x, 0) for x in list(zip(*sample_metrics))]
54 | precision, recall, AP, f1, ap_class = ap_per_class(true_positives, pred_scores, pred_labels, labels)
55 |
56 | return precision, recall, AP, f1, ap_class
57 |
58 |
59 | if __name__ == "__main__":
60 | parser = argparse.ArgumentParser()
61 | parser.add_argument("--batch_size", type=int, default=8, help="size of each image batch")
62 | parser.add_argument("--model_def", type=str, default="config/yolov3.cfg", help="path to model definition file")
63 | parser.add_argument("--data_config", type=str, default="config/coco.data", help="path to data config file")
64 | parser.add_argument("--weights_path", type=str, default="weights/yolov3.weights", help="path to weights file")
65 | parser.add_argument("--class_path", type=str, default="data/coco.names", help="path to class label file")
66 | parser.add_argument("--iou_thres", type=float, default=0.5, help="iou threshold required to qualify as detected")
67 | parser.add_argument("--conf_thres", type=float, default=0.001, help="object confidence threshold")
68 | parser.add_argument("--nms_thres", type=float, default=0.5, help="iou thresshold for non-maximum suppression")
69 | parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
70 | parser.add_argument("--img_size", type=int, default=416, help="size of each image dimension")
71 | opt = parser.parse_args()
72 | print(opt)
73 |
74 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
75 |
76 | data_config = parse_data_config(opt.data_config)
77 | valid_path = data_config["valid"]
78 | class_names = load_classes(data_config["names"])
79 |
80 | # Initiate model
81 | model = Darknet(opt.model_def).to(device)
82 | if opt.weights_path.endswith(".weights"):
83 | # Load darknet weights
84 | model.load_darknet_weights(opt.weights_path)
85 | else:
86 | # Load checkpoint weights
87 | model.load_state_dict(torch.load(opt.weights_path))
88 |
89 | print("Compute mAP...")
90 |
91 | precision, recall, AP, f1, ap_class = evaluate(
92 | model,
93 | path=valid_path,
94 | iou_thres=opt.iou_thres,
95 | conf_thres=opt.conf_thres,
96 | nms_thres=opt.nms_thres,
97 | img_size=opt.img_size,
98 | batch_size=8,
99 | )
100 |
101 | print("Average Precisions:")
102 | for i, c in enumerate(ap_class):
103 | print(f"+ Class '{c}' ({class_names[c]}) - AP: {AP[i]}")
104 |
105 | print(f"mAP: {AP.mean()}")
106 |
--------------------------------------------------------------------------------
/train.py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 |
3 | from models import *
4 | from utils.logger import *
5 | from utils.utils import *
6 | from utils.datasets import *
7 | from utils.parse_config import *
8 | from test import evaluate
9 |
10 | from terminaltables import AsciiTable
11 |
12 | import os
13 | import sys
14 | import time
15 | import datetime
16 | import argparse
17 |
18 | import torch
19 | from torch.utils.data import DataLoader
20 | from torchvision import datasets
21 | from torchvision import transforms
22 | from torch.autograd import Variable
23 | import torch.optim as optim
24 |
25 | if __name__ == "__main__":
26 | parser = argparse.ArgumentParser()
27 | parser.add_argument("--epochs", type=int, default=100, help="number of epochs")
28 | parser.add_argument("--batch_size", type=int, default=8, help="size of each image batch")
29 | parser.add_argument("--gradient_accumulations", type=int, default=2, help="number of gradient accums before step")
30 | parser.add_argument("--model_def", type=str, default="config/yolov3.cfg", help="path to model definition file")
31 | parser.add_argument("--data_config", type=str, default="config/coco.data", help="path to data config file")
32 | parser.add_argument("--pretrained_weights", type=str, help="if specified starts from checkpoint model")
33 | parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
34 | parser.add_argument("--img_size", type=int, default=416, help="size of each image dimension")
35 | parser.add_argument("--checkpoint_interval", type=int, default=1, help="interval between saving model weights")
36 | parser.add_argument("--evaluation_interval", type=int, default=1, help="interval evaluations on validation set")
37 | parser.add_argument("--compute_map", default=False, help="if True computes mAP every tenth batch")
38 | parser.add_argument("--multiscale_training", default=True, help="allow for multi-scale training")
39 | opt = parser.parse_args()
40 | print(opt)
41 |
42 | logger = Logger("logs")
43 |
44 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
45 |
46 | os.makedirs("output", exist_ok=True)
47 | os.makedirs("checkpoints", exist_ok=True)
48 |
49 | # Get data configuration
50 | data_config = parse_data_config(opt.data_config)
51 | train_path = data_config["train"]
52 | valid_path = data_config["valid"]
53 | class_names = load_classes(data_config["names"])
54 |
55 | # Initiate model
56 | model = Darknet(opt.model_def).to(device)
57 | model.apply(weights_init_normal)
58 |
59 | # If specified we start from checkpoint
60 | if opt.pretrained_weights:
61 | if opt.pretrained_weights.endswith(".pth"):
62 | model.load_state_dict(torch.load(opt.pretrained_weights))
63 | else:
64 | model.load_darknet_weights(opt.pretrained_weights)
65 |
66 | # Get dataloader
67 | dataset = ListDataset(train_path, augment=True, multiscale=opt.multiscale_training)
68 | dataloader = torch.utils.data.DataLoader(
69 | dataset,
70 | batch_size=opt.batch_size,
71 | shuffle=True,
72 | num_workers=opt.n_cpu,
73 | pin_memory=True,
74 | collate_fn=dataset.collate_fn,
75 | )
76 |
77 | optimizer = torch.optim.Adam(model.parameters())
78 |
79 | metrics = [
80 | "grid_size",
81 | "loss",
82 | "x",
83 | "y",
84 | "w",
85 | "h",
86 | "conf",
87 | "cls",
88 | "cls_acc",
89 | "recall50",
90 | "recall75",
91 | "precision",
92 | "conf_obj",
93 | "conf_noobj",
94 | ]
95 |
96 | for epoch in range(opt.epochs):
97 | model.train()
98 | start_time = time.time()
99 | for batch_i, (_, imgs, targets) in enumerate(dataloader):
100 | batches_done = len(dataloader) * epoch + batch_i
101 |
102 | imgs = Variable(imgs.to(device))
103 | targets = Variable(targets.to(device), requires_grad=False)
104 |
105 | loss, outputs = model(imgs, targets)
106 | loss.backward()
107 |
108 | if batches_done % opt.gradient_accumulations:
109 | # Accumulates gradient before each step
110 | optimizer.step()
111 | optimizer.zero_grad()
112 |
113 | # ----------------
114 | # Log progress
115 | # ----------------
116 |
117 | log_str = "\n---- [Epoch %d/%d, Batch %d/%d] ----\n" % (epoch, opt.epochs, batch_i, len(dataloader))
118 |
119 | metric_table = [["Metrics", *[f"YOLO Layer {i}" for i in range(len(model.yolo_layers))]]]
120 |
121 | # Log metrics at each YOLO layer
122 | for i, metric in enumerate(metrics):
123 | formats = {m: "%.6f" for m in metrics}
124 | formats["grid_size"] = "%2d"
125 | formats["cls_acc"] = "%.2f%%"
126 | row_metrics = [formats[metric] % yolo.metrics.get(metric, 0) for yolo in model.yolo_layers]
127 | metric_table += [[metric, *row_metrics]]
128 |
129 | # Tensorboard logging
130 | tensorboard_log = []
131 | for j, yolo in enumerate(model.yolo_layers):
132 | for name, metric in yolo.metrics.items():
133 | if name != "grid_size":
134 | tensorboard_log += [(f"{name}_{j+1}", metric)]
135 | tensorboard_log += [("loss", loss.item())]
136 | logger.list_of_scalars_summary(tensorboard_log, batches_done)
137 |
138 | log_str += AsciiTable(metric_table).table
139 | log_str += f"\nTotal loss {loss.item()}"
140 |
141 | # Determine approximate time left for epoch
142 | epoch_batches_left = len(dataloader) - (batch_i + 1)
143 | time_left = datetime.timedelta(seconds=epoch_batches_left * (time.time() - start_time) / (batch_i + 1))
144 | log_str += f"\n---- ETA {time_left}"
145 |
146 | print(log_str)
147 |
148 | model.seen += imgs.size(0)
149 |
150 | if epoch % opt.evaluation_interval == 0:
151 | print("\n---- Evaluating Model ----")
152 | # Evaluate the model on the validation set
153 | precision, recall, AP, f1, ap_class = evaluate(
154 | model,
155 | path=valid_path,
156 | iou_thres=0.5,
157 | conf_thres=0.5,
158 | nms_thres=0.5,
159 | img_size=opt.img_size,
160 | batch_size=8,
161 | )
162 | evaluation_metrics = [
163 | ("val_precision", precision.mean()),
164 | ("val_recall", recall.mean()),
165 | ("val_mAP", AP.mean()),
166 | ("val_f1", f1.mean()),
167 | ]
168 | logger.list_of_scalars_summary(evaluation_metrics, epoch)
169 |
170 | # Print class APs and mAP
171 | ap_table = [["Index", "Class name", "AP"]]
172 | for i, c in enumerate(ap_class):
173 | ap_table += [[c, class_names[c], "%.5f" % AP[i]]]
174 | print(AsciiTable(ap_table).table)
175 | print(f"---- mAP {AP.mean()}")
176 |
177 | if epoch % opt.checkpoint_interval == 0:
178 | torch.save(model.state_dict(), f"checkpoints/yolov3_ckpt_%d.pth" % epoch)
179 |
--------------------------------------------------------------------------------
/utils/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangwenwenlili/industry-mouse-detect/68de3ab55a7878f61afbfb8693c3503178edac5e/utils/__init__.py
--------------------------------------------------------------------------------
/utils/augmentations.py:
--------------------------------------------------------------------------------
1 | import torch
2 | import torch.nn.functional as F
3 | import numpy as np
4 |
5 |
6 | def horisontal_flip(images, targets):
7 | images = torch.flip(images, [-1])
8 | targets[:, 2] = 1 - targets[:, 2]
9 | return images, targets
10 |
--------------------------------------------------------------------------------
/utils/datasets.py:
--------------------------------------------------------------------------------
1 | import glob
2 | import random
3 | import os
4 | import sys
5 | import numpy as np
6 | from PIL import Image
7 | import torch
8 | import torch.nn.functional as F
9 |
10 | from utils.augmentations import horisontal_flip
11 | from torch.utils.data import Dataset
12 | import torchvision.transforms as transforms
13 |
14 |
15 | def pad_to_square(img, pad_value):
16 | c, h, w = img.shape
17 | dim_diff = np.abs(h - w)
18 | # (upper / left) padding and (lower / right) padding
19 | pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2
20 | # Determine padding
21 | pad = (0, 0, pad1, pad2) if h <= w else (pad1, pad2, 0, 0)
22 | # Add padding
23 | img = F.pad(img, pad, "constant", value=pad_value)
24 |
25 | return img, pad
26 |
27 |
28 | def resize(image, size):
29 | image = F.interpolate(image.unsqueeze(0), size=size, mode="nearest").squeeze(0)
30 | return image
31 |
32 |
33 | def random_resize(images, min_size=288, max_size=448):
34 | new_size = random.sample(list(range(min_size, max_size + 1, 32)), 1)[0]
35 | images = F.interpolate(images, size=new_size, mode="nearest")
36 | return images
37 |
38 |
39 | class ImageFolder(Dataset):
40 | def __init__(self, folder_path, img_size=416):
41 | self.files = sorted(glob.glob("%s/*.*" % folder_path))
42 | self.img_size = img_size
43 |
44 | def __getitem__(self, index):
45 | img_path = self.files[index % len(self.files)]
46 | # Extract image as PyTorch tensor
47 | img = transforms.ToTensor()(Image.open(img_path))
48 | # Pad to square resolution
49 | img, _ = pad_to_square(img, 0)
50 | # Resize
51 | img = resize(img, self.img_size)
52 |
53 | return img_path, img
54 |
55 | def __len__(self):
56 | return len(self.files)
57 |
58 |
59 | class ListDataset(Dataset):
60 | def __init__(self, list_path, img_size=416, augment=True, multiscale=True, normalized_labels=True):
61 | with open(list_path, "r") as file:
62 | self.img_files = file.readlines()
63 |
64 | self.label_files = [
65 | path.replace("images", "labels").replace(".png", ".txt").replace(".jpg", ".txt")
66 | for path in self.img_files
67 | ]
68 | self.img_size = img_size
69 | self.max_objects = 100
70 | self.augment = augment
71 | self.multiscale = multiscale
72 | self.normalized_labels = normalized_labels
73 | self.min_size = self.img_size - 3 * 32
74 | self.max_size = self.img_size + 3 * 32
75 | self.batch_count = 0
76 |
77 | def __getitem__(self, index):
78 |
79 | # ---------
80 | # Image
81 | # ---------
82 |
83 | img_path = self.img_files[index % len(self.img_files)].rstrip()
84 |
85 | # Extract image as PyTorch tensor
86 | img = transforms.ToTensor()(Image.open(img_path).convert('RGB'))
87 |
88 | # Handle images with less than three channels
89 | if len(img.shape) != 3:
90 | img = img.unsqueeze(0)
91 | img = img.expand((3, img.shape[1:]))
92 |
93 | _, h, w = img.shape
94 | h_factor, w_factor = (h, w) if self.normalized_labels else (1, 1)
95 | # Pad to square resolution
96 | img, pad = pad_to_square(img, 0)
97 | _, padded_h, padded_w = img.shape
98 |
99 | # ---------
100 | # Label
101 | # ---------
102 |
103 | label_path = self.label_files[index % len(self.img_files)].rstrip()
104 |
105 | targets = None
106 | if os.path.exists(label_path):
107 | boxes = torch.from_numpy(np.loadtxt(label_path).reshape(-1, 5))
108 | # Extract coordinates for unpadded + unscaled image
109 | x1 = w_factor * (boxes[:, 1] - boxes[:, 3] / 2)
110 | y1 = h_factor * (boxes[:, 2] - boxes[:, 4] / 2)
111 | x2 = w_factor * (boxes[:, 1] + boxes[:, 3] / 2)
112 | y2 = h_factor * (boxes[:, 2] + boxes[:, 4] / 2)
113 | # Adjust for added padding
114 | x1 += pad[0]
115 | y1 += pad[2]
116 | x2 += pad[1]
117 | y2 += pad[3]
118 | # Returns (x, y, w, h)
119 | boxes[:, 1] = ((x1 + x2) / 2) / padded_w
120 | boxes[:, 2] = ((y1 + y2) / 2) / padded_h
121 | boxes[:, 3] *= w_factor / padded_w
122 | boxes[:, 4] *= h_factor / padded_h
123 |
124 | targets = torch.zeros((len(boxes), 6))
125 | targets[:, 1:] = boxes
126 |
127 | # Apply augmentations
128 | if self.augment:
129 | if np.random.random() < 0.5:
130 | img, targets = horisontal_flip(img, targets)
131 |
132 | return img_path, img, targets
133 |
134 | def collate_fn(self, batch):
135 | paths, imgs, targets = list(zip(*batch))
136 | # Remove empty placeholder targets
137 | targets = [boxes for boxes in targets if boxes is not None]
138 | # Add sample index to targets
139 | for i, boxes in enumerate(targets):
140 | boxes[:, 0] = i
141 | targets = torch.cat(targets, 0)
142 | # Selects new image size every tenth batch
143 | if self.multiscale and self.batch_count % 10 == 0:
144 | self.img_size = random.choice(range(self.min_size, self.max_size + 1, 32))
145 | # Resize images to input shape
146 | imgs = torch.stack([resize(img, self.img_size) for img in imgs])
147 | self.batch_count += 1
148 | return paths, imgs, targets
149 |
150 | def __len__(self):
151 | return len(self.img_files)
152 |
--------------------------------------------------------------------------------
/utils/logger.py:
--------------------------------------------------------------------------------
1 | import tensorflow as tf
2 |
3 |
4 | class Logger(object):
5 | def __init__(self, log_dir):
6 | """Create a summary writer logging to log_dir."""
7 | self.writer = tf.summary.FileWriter(log_dir)
8 |
9 | def scalar_summary(self, tag, value, step):
10 | """Log a scalar variable."""
11 | summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value)])
12 | self.writer.add_summary(summary, step)
13 |
14 | def list_of_scalars_summary(self, tag_value_pairs, step):
15 | """Log scalar variables."""
16 | summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value) for tag, value in tag_value_pairs])
17 | self.writer.add_summary(summary, step)
18 |
--------------------------------------------------------------------------------
/utils/parse_config.py:
--------------------------------------------------------------------------------
1 |
2 |
3 | def parse_model_config(path):
4 | """Parses the yolo-v3 layer configuration file and returns module definitions"""
5 | file = open(path, 'r')
6 | lines = file.read().split('\n')
7 | lines = [x for x in lines if x and not x.startswith('#')]
8 | lines = [x.rstrip().lstrip() for x in lines] # get rid of fringe whitespaces
9 | module_defs = []
10 | for line in lines:
11 | if line.startswith('['): # This marks the start of a new block
12 | module_defs.append({})
13 | module_defs[-1]['type'] = line[1:-1].rstrip()
14 | if module_defs[-1]['type'] == 'convolutional':
15 | module_defs[-1]['batch_normalize'] = 0
16 | else:
17 | key, value = line.split("=")
18 | value = value.strip()
19 | module_defs[-1][key.rstrip()] = value.strip()
20 |
21 | return module_defs
22 |
23 | def parse_data_config(path):
24 | """Parses the data configuration file"""
25 | options = dict()
26 | options['gpus'] = '0,1,2,3'
27 | options['num_workers'] = '10'
28 | with open(path, 'r') as fp:
29 | lines = fp.readlines()
30 | for line in lines:
31 | line = line.strip()
32 | if line == '' or line.startswith('#'):
33 | continue
34 | key, value = line.split('=')
35 | options[key.strip()] = value.strip()
36 | return options
37 |
--------------------------------------------------------------------------------
/utils/utils.py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 | import math
3 | import time
4 | import tqdm
5 | import torch
6 | import torch.nn as nn
7 | import torch.nn.functional as F
8 | from torch.autograd import Variable
9 | import numpy as np
10 | import matplotlib.pyplot as plt
11 | import matplotlib.patches as patches
12 |
13 |
14 | def to_cpu(tensor):
15 | return tensor.detach().cpu()
16 |
17 |
18 | def load_classes(path):
19 | """
20 | Loads class labels at 'path'
21 | """
22 | fp = open(path, "r")
23 | names = fp.read().split("\n")[:-1]
24 | return names
25 |
26 |
27 | def weights_init_normal(m):
28 | classname = m.__class__.__name__
29 | if classname.find("Conv") != -1:
30 | torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
31 | elif classname.find("BatchNorm2d") != -1:
32 | torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
33 | torch.nn.init.constant_(m.bias.data, 0.0)
34 |
35 |
36 | def rescale_boxes(boxes, current_dim, original_shape):
37 | """ Rescales bounding boxes to the original shape """
38 | orig_h, orig_w = original_shape
39 | # The amount of padding that was added
40 | pad_x = max(orig_h - orig_w, 0) * (current_dim / max(original_shape))
41 | pad_y = max(orig_w - orig_h, 0) * (current_dim / max(original_shape))
42 | # Image height and width after padding is removed
43 | unpad_h = current_dim - pad_y
44 | unpad_w = current_dim - pad_x
45 | # Rescale bounding boxes to dimension of original image
46 | boxes[:, 0] = ((boxes[:, 0] - pad_x // 2) / unpad_w) * orig_w
47 | boxes[:, 1] = ((boxes[:, 1] - pad_y // 2) / unpad_h) * orig_h
48 | boxes[:, 2] = ((boxes[:, 2] - pad_x // 2) / unpad_w) * orig_w
49 | boxes[:, 3] = ((boxes[:, 3] - pad_y // 2) / unpad_h) * orig_h
50 | return boxes
51 |
52 |
53 | def xywh2xyxy(x):
54 | y = x.new(x.shape)
55 | y[..., 0] = x[..., 0] - x[..., 2] / 2
56 | y[..., 1] = x[..., 1] - x[..., 3] / 2
57 | y[..., 2] = x[..., 0] + x[..., 2] / 2
58 | y[..., 3] = x[..., 1] + x[..., 3] / 2
59 | return y
60 |
61 |
62 | def ap_per_class(tp, conf, pred_cls, target_cls):
63 | """ Compute the average precision, given the recall and precision curves.
64 | Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
65 | # Arguments
66 | tp: True positives (list).
67 | conf: Objectness value from 0-1 (list).
68 | pred_cls: Predicted object classes (list).
69 | target_cls: True object classes (list).
70 | # Returns
71 | The average precision as computed in py-faster-rcnn.
72 | """
73 |
74 | # Sort by objectness
75 | i = np.argsort(-conf)
76 | tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
77 |
78 | # Find unique classes
79 | unique_classes = np.unique(target_cls)
80 |
81 | # Create Precision-Recall curve and compute AP for each class
82 | ap, p, r = [], [], []
83 | for c in tqdm.tqdm(unique_classes, desc="Computing AP"):
84 | i = pred_cls == c
85 | n_gt = (target_cls == c).sum() # Number of ground truth objects
86 | n_p = i.sum() # Number of predicted objects
87 |
88 | if n_p == 0 and n_gt == 0:
89 | continue
90 | elif n_p == 0 or n_gt == 0:
91 | ap.append(0)
92 | r.append(0)
93 | p.append(0)
94 | else:
95 | # Accumulate FPs and TPs
96 | fpc = (1 - tp[i]).cumsum()
97 | tpc = (tp[i]).cumsum()
98 |
99 | # Recall
100 | recall_curve = tpc / (n_gt + 1e-16)
101 | r.append(recall_curve[-1])
102 |
103 | # Precision
104 | precision_curve = tpc / (tpc + fpc)
105 | p.append(precision_curve[-1])
106 |
107 | # AP from recall-precision curve
108 | ap.append(compute_ap(recall_curve, precision_curve))
109 |
110 | # Compute F1 score (harmonic mean of precision and recall)
111 | p, r, ap = np.array(p), np.array(r), np.array(ap)
112 | f1 = 2 * p * r / (p + r + 1e-16)
113 |
114 | return p, r, ap, f1, unique_classes.astype("int32")
115 |
116 |
117 | def compute_ap(recall, precision):
118 | """ Compute the average precision, given the recall and precision curves.
119 | Code originally from https://github.com/rbgirshick/py-faster-rcnn.
120 |
121 | # Arguments
122 | recall: The recall curve (list).
123 | precision: The precision curve (list).
124 | # Returns
125 | The average precision as computed in py-faster-rcnn.
126 | """
127 | # correct AP calculation
128 | # first append sentinel values at the end
129 | mrec = np.concatenate(([0.0], recall, [1.0]))
130 | mpre = np.concatenate(([0.0], precision, [0.0]))
131 |
132 | # compute the precision envelope
133 | for i in range(mpre.size - 1, 0, -1):
134 | mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
135 |
136 | # to calculate area under PR curve, look for points
137 | # where X axis (recall) changes value
138 | i = np.where(mrec[1:] != mrec[:-1])[0]
139 |
140 | # and sum (\Delta recall) * prec
141 | ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
142 | return ap
143 |
144 |
145 | def get_batch_statistics(outputs, targets, iou_threshold):
146 | """ Compute true positives, predicted scores and predicted labels per sample """
147 | batch_metrics = []
148 | for sample_i in range(len(outputs)):
149 |
150 | if outputs[sample_i] is None:
151 | continue
152 |
153 | output = outputs[sample_i]
154 | pred_boxes = output[:, :4]
155 | pred_scores = output[:, 4]
156 | pred_labels = output[:, -1]
157 |
158 | true_positives = np.zeros(pred_boxes.shape[0])
159 |
160 | annotations = targets[targets[:, 0] == sample_i][:, 1:]
161 | target_labels = annotations[:, 0] if len(annotations) else []
162 | if len(annotations):
163 | detected_boxes = []
164 | target_boxes = annotations[:, 1:]
165 |
166 | for pred_i, (pred_box, pred_label) in enumerate(zip(pred_boxes, pred_labels)):
167 |
168 | # If targets are found break
169 | if len(detected_boxes) == len(annotations):
170 | break
171 |
172 | # Ignore if label is not one of the target labels
173 | if pred_label not in target_labels:
174 | continue
175 |
176 | iou, box_index = bbox_iou(pred_box.unsqueeze(0), target_boxes).max(0)
177 | if iou >= iou_threshold and box_index not in detected_boxes:
178 | true_positives[pred_i] = 1
179 | detected_boxes += [box_index]
180 | batch_metrics.append([true_positives, pred_scores, pred_labels])
181 | return batch_metrics
182 |
183 |
184 | def bbox_wh_iou(wh1, wh2):
185 | wh2 = wh2.t()
186 | w1, h1 = wh1[0], wh1[1]
187 | w2, h2 = wh2[0], wh2[1]
188 | inter_area = torch.min(w1, w2) * torch.min(h1, h2)
189 | union_area = (w1 * h1 + 1e-16) + w2 * h2 - inter_area
190 | return inter_area / union_area
191 |
192 |
193 | def bbox_iou(box1, box2, x1y1x2y2=True):
194 | """
195 | Returns the IoU of two bounding boxes
196 | """
197 | if not x1y1x2y2:
198 | # Transform from center and width to exact coordinates
199 | b1_x1, b1_x2 = box1[:, 0] - box1[:, 2] / 2, box1[:, 0] + box1[:, 2] / 2
200 | b1_y1, b1_y2 = box1[:, 1] - box1[:, 3] / 2, box1[:, 1] + box1[:, 3] / 2
201 | b2_x1, b2_x2 = box2[:, 0] - box2[:, 2] / 2, box2[:, 0] + box2[:, 2] / 2
202 | b2_y1, b2_y2 = box2[:, 1] - box2[:, 3] / 2, box2[:, 1] + box2[:, 3] / 2
203 | else:
204 | # Get the coordinates of bounding boxes
205 | b1_x1, b1_y1, b1_x2, b1_y2 = box1[:, 0], box1[:, 1], box1[:, 2], box1[:, 3]
206 | b2_x1, b2_y1, b2_x2, b2_y2 = box2[:, 0], box2[:, 1], box2[:, 2], box2[:, 3]
207 |
208 | # get the corrdinates of the intersection rectangle
209 | inter_rect_x1 = torch.max(b1_x1, b2_x1)
210 | inter_rect_y1 = torch.max(b1_y1, b2_y1)
211 | inter_rect_x2 = torch.min(b1_x2, b2_x2)
212 | inter_rect_y2 = torch.min(b1_y2, b2_y2)
213 | # Intersection area
214 | inter_area = torch.clamp(inter_rect_x2 - inter_rect_x1 + 1, min=0) * torch.clamp(
215 | inter_rect_y2 - inter_rect_y1 + 1, min=0
216 | )
217 | # Union Area
218 | b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1)
219 | b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1)
220 |
221 | iou = inter_area / (b1_area + b2_area - inter_area + 1e-16)
222 |
223 | return iou
224 |
225 |
226 | def non_max_suppression(prediction, conf_thres=0.5, nms_thres=0.4):
227 | """
228 | Removes detections with lower object confidence score than 'conf_thres' and performs
229 | Non-Maximum Suppression to further filter detections.
230 | Returns detections with shape:
231 | (x1, y1, x2, y2, object_conf, class_score, class_pred)
232 | """
233 |
234 | # From (center x, center y, width, height) to (x1, y1, x2, y2)
235 | prediction[..., :4] = xywh2xyxy(prediction[..., :4])
236 | output = [None for _ in range(len(prediction))]
237 | for image_i, image_pred in enumerate(prediction):
238 | # Filter out confidence scores below threshold
239 | image_pred = image_pred[image_pred[:, 4] >= conf_thres]
240 | # If none are remaining => process next image
241 | if not image_pred.size(0):
242 | continue
243 | # Object confidence times class confidence
244 | score = image_pred[:, 4] * image_pred[:, 5:].max(1)[0]
245 | # Sort by it
246 | image_pred = image_pred[(-score).argsort()]
247 | class_confs, class_preds = image_pred[:, 5:].max(1, keepdim=True)
248 | detections = torch.cat((image_pred[:, :5], class_confs.float(), class_preds.float()), 1)
249 | # Perform non-maximum suppression
250 | keep_boxes = []
251 | while detections.size(0):
252 | large_overlap = bbox_iou(detections[0, :4].unsqueeze(0), detections[:, :4]) > nms_thres
253 | label_match = detections[0, -1] == detections[:, -1]
254 | # Indices of boxes with lower confidence scores, large IOUs and matching labels
255 | invalid = large_overlap & label_match
256 | weights = detections[invalid, 4:5]
257 | # Merge overlapping bboxes by order of confidence
258 | detections[0, :4] = (weights * detections[invalid, :4]).sum(0) / weights.sum()
259 | keep_boxes += [detections[0]]
260 | detections = detections[~invalid]
261 | if keep_boxes:
262 | output[image_i] = torch.stack(keep_boxes)
263 |
264 | return output
265 |
266 |
267 | def build_targets(pred_boxes, pred_cls, target, anchors, ignore_thres):
268 |
269 | ByteTensor = torch.cuda.ByteTensor if pred_boxes.is_cuda else torch.ByteTensor
270 | FloatTensor = torch.cuda.FloatTensor if pred_boxes.is_cuda else torch.FloatTensor
271 |
272 | nB = pred_boxes.size(0)
273 | nA = pred_boxes.size(1)
274 | nC = pred_cls.size(-1)
275 | nG = pred_boxes.size(2)
276 |
277 | # Output tensors
278 | obj_mask = ByteTensor(nB, nA, nG, nG).fill_(0)
279 | noobj_mask = ByteTensor(nB, nA, nG, nG).fill_(1)
280 | class_mask = FloatTensor(nB, nA, nG, nG).fill_(0)
281 | iou_scores = FloatTensor(nB, nA, nG, nG).fill_(0)
282 | tx = FloatTensor(nB, nA, nG, nG).fill_(0)
283 | ty = FloatTensor(nB, nA, nG, nG).fill_(0)
284 | tw = FloatTensor(nB, nA, nG, nG).fill_(0)
285 | th = FloatTensor(nB, nA, nG, nG).fill_(0)
286 | tcls = FloatTensor(nB, nA, nG, nG, nC).fill_(0)
287 |
288 | # Convert to position relative to box
289 | target_boxes = target[:, 2:6] * nG
290 | gxy = target_boxes[:, :2]
291 | gwh = target_boxes[:, 2:]
292 | # Get anchors with best iou
293 | ious = torch.stack([bbox_wh_iou(anchor, gwh) for anchor in anchors])
294 | best_ious, best_n = ious.max(0)
295 | # Separate target values
296 | b, target_labels = target[:, :2].long().t()
297 | gx, gy = gxy.t()
298 | gw, gh = gwh.t()
299 | gi, gj = gxy.long().t()
300 | # Set masks
301 | obj_mask[b, best_n, gj, gi] = 1
302 | noobj_mask[b, best_n, gj, gi] = 0
303 |
304 | # Set noobj mask to zero where iou exceeds ignore threshold
305 | for i, anchor_ious in enumerate(ious.t()):
306 | noobj_mask[b[i], anchor_ious > ignore_thres, gj[i], gi[i]] = 0
307 |
308 | # Coordinates
309 | tx[b, best_n, gj, gi] = gx - gx.floor()
310 | ty[b, best_n, gj, gi] = gy - gy.floor()
311 | # Width and height
312 | tw[b, best_n, gj, gi] = torch.log(gw / anchors[best_n][:, 0] + 1e-16)
313 | th[b, best_n, gj, gi] = torch.log(gh / anchors[best_n][:, 1] + 1e-16)
314 | # One-hot encoding of label
315 | tcls[b, best_n, gj, gi, target_labels] = 1
316 | # Compute label correctness and iou at best anchor
317 | class_mask[b, best_n, gj, gi] = (pred_cls[b, best_n, gj, gi].argmax(-1) == target_labels).float()
318 | iou_scores[b, best_n, gj, gi] = bbox_iou(pred_boxes[b, best_n, gj, gi], target_boxes, x1y1x2y2=False)
319 |
320 | tconf = obj_mask.float()
321 | return iou_scores, class_mask, obj_mask, noobj_mask, tx, ty, tw, th, tcls, tconf
322 |
--------------------------------------------------------------------------------