├── .gitignore
├── .idea
├── CrosswalkDetectionNet.iml
├── codeStyles
│ ├── Project.xml
│ └── codeStyleConfig.xml
├── deployment.xml
├── dictionaries
│ └── tanmenglu.xml
├── inspectionProfiles
│ └── Project_Default.xml
├── misc.xml
├── modules.xml
├── remote-mappings.xml
├── vagrant.xml
├── vcs.xml
└── workspace.xml
├── README.md
├── __init__.py
├── data
├── Detection results compare to YOLOv5.jpg
├── Detection results compare to YOLOv5_0.jpg
├── Detection results compare to YOLOv5_1.jpg
├── SHNAVI.jpg
├── cl_setting.yaml
├── crosswalk detection in complex realistic scenarios.jpg
├── crosswalk.yaml
├── graphical_abstract.jpg
├── rect.jpg
└── set_ai.png
├── detect.py
├── docs
├── DATASETS.md
├── INSTALL.md
├── MODEL_ZOO.md
├── Press_Material.md
└── README_zh_cn.md
├── evaluate.py
├── example
├── fog_image
│ ├── fogged_br0.1_th0.05.jpg
│ ├── fogged_br0.3_th0.05.jpg
│ ├── fogged_br0.5_th0.05.jpg
│ ├── fogged_br0.7_th0.01.jpg
│ ├── fogged_br0.7_th0.03.jpg
│ ├── fogged_br0.7_th0.05.jpg
│ ├── fogged_br0.7_th0.07.jpg
│ ├── fogged_br0.7_th0.09.jpg
│ ├── fogged_br0.9_th0.05.jpg
│ └── raw.jpg
├── images
│ ├── complex_scenarios
│ │ ├── clearly_visible_0760_00001.jpg
│ │ ├── damaged_0036_00846.jpg
│ │ ├── dazzling_0036_01707.jpg
│ │ ├── distorted_0760_01846.jpg
│ │ ├── occluded_by_wiper_0760_02902.jpg
│ │ ├── partialy_lost_0036_01558.jpg
│ │ ├── reflection_after_rain_0760_02711.jpg
│ │ ├── truncated_in_steering_0760_03208.jpg
│ │ ├── view_blocked_0036_05420.jpg
│ │ └── visible_at_night_0036_01127.jpg
│ ├── exmp_imgs
│ │ ├── filename_00030.jpg
│ │ ├── filename_00031.jpg
│ │ ├── filename_00032.jpg
│ │ ├── filename_00033.jpg
│ │ ├── filename_00034.jpg
│ │ ├── filename_00035.jpg
│ │ ├── filename_00036.jpg
│ │ ├── filename_00037.jpg
│ │ ├── filename_00038.jpg
│ │ ├── filename_00039.jpg
│ │ ├── filename_00040.jpg
│ │ ├── filename_00041.jpg
│ │ ├── filename_00042.jpg
│ │ ├── filename_00043.jpg
│ │ ├── filename_00044.jpg
│ │ ├── filename_00045.jpg
│ │ ├── filename_00046.jpg
│ │ ├── filename_00047.jpg
│ │ ├── filename_00048.jpg
│ │ ├── filename_00049.jpg
│ │ ├── filename_00050.jpg
│ │ ├── filename_00051.jpg
│ │ ├── filename_00052.jpg
│ │ ├── filename_00053.jpg
│ │ ├── filename_00054.jpg
│ │ ├── filename_00055.jpg
│ │ ├── filename_00056.jpg
│ │ ├── filename_00057.jpg
│ │ ├── filename_00058.jpg
│ │ └── filename_00059.jpg
│ ├── filename_00030.jpg
│ ├── filename_00031.jpg
│ ├── filename_00032.jpg
│ ├── filename_00033.jpg
│ ├── filename_00034.jpg
│ ├── filename_00035.jpg
│ ├── filename_00036.jpg
│ ├── filename_00037.jpg
│ ├── filename_00038.jpg
│ ├── filename_00039.jpg
│ ├── filename_00040.jpg
│ ├── filename_00041.jpg
│ ├── filename_00042.jpg
│ ├── filename_00043.jpg
│ ├── filename_00044.jpg
│ ├── filename_00045.jpg
│ ├── filename_00046.jpg
│ ├── filename_00047.jpg
│ ├── filename_00048.jpg
│ ├── filename_00049.jpg
│ ├── filename_00050.jpg
│ ├── filename_00051.jpg
│ ├── filename_00052.jpg
│ ├── filename_00053.jpg
│ ├── filename_00054.jpg
│ ├── filename_00055.jpg
│ ├── filename_00056.jpg
│ ├── filename_00057.jpg
│ ├── filename_00058.jpg
│ ├── filename_00059.jpg
│ └── for_overall_architecture
│ │ ├── filename1120.jpg
│ │ ├── filename1121.jpg
│ │ ├── filename1122.jpg
│ │ ├── filename1123.jpg
│ │ ├── filename1124.jpg
│ │ ├── filename1125.jpg
│ │ ├── filename1126.jpg
│ │ ├── filename1127.jpg
│ │ ├── filename1128.jpg
│ │ ├── filename1129.jpg
│ │ ├── filename1130.jpg
│ │ └── filename1131.jpg
├── out_for_overall
│ ├── detect.log
│ ├── filename1120.jpg
│ ├── filename1120.txt
│ ├── filename1121.jpg
│ ├── filename1121.txt
│ ├── filename1122.jpg
│ ├── filename1122.txt
│ ├── filename1123.jpg
│ ├── filename1123.txt
│ ├── filename1124.jpg
│ ├── filename1124.txt
│ ├── filename1125.jpg
│ ├── filename1125.txt
│ ├── filename1126.jpg
│ ├── filename1126.txt
│ ├── filename1127.jpg
│ ├── filename1127.txt
│ ├── filename1128.jpg
│ ├── filename1128.txt
│ ├── filename1129.jpg
│ ├── filename1129.txt
│ ├── filename1130.jpg
│ ├── filename1130.txt
│ ├── filename1131.jpg
│ └── filename1131.txt
└── output
│ ├── detect.log
│ ├── filename_00030.jpg
│ ├── filename_00031.jpg
│ ├── filename_00032.jpg
│ ├── filename_00033.jpg
│ ├── filename_00034.jpg
│ ├── filename_00035.jpg
│ ├── filename_00036.jpg
│ ├── filename_00036.txt
│ ├── filename_00037.jpg
│ ├── filename_00037.txt
│ ├── filename_00038.jpg
│ ├── filename_00038.txt
│ ├── filename_00039.jpg
│ ├── filename_00039.txt
│ ├── filename_00040.jpg
│ ├── filename_00040.txt
│ ├── filename_00041.jpg
│ ├── filename_00041.txt
│ ├── filename_00042.jpg
│ ├── filename_00042.txt
│ ├── filename_00043.jpg
│ ├── filename_00043.txt
│ ├── filename_00044.jpg
│ ├── filename_00045.jpg
│ ├── filename_00046.jpg
│ ├── filename_00046.txt
│ ├── filename_00047.jpg
│ ├── filename_00047.txt
│ ├── filename_00048.jpg
│ ├── filename_00048.txt
│ ├── filename_00049.jpg
│ ├── filename_00049.txt
│ ├── filename_00050.jpg
│ ├── filename_00050.txt
│ ├── filename_00051.jpg
│ ├── filename_00052.jpg
│ ├── filename_00053.jpg
│ ├── filename_00054.jpg
│ ├── filename_00055.jpg
│ ├── filename_00056.jpg
│ ├── filename_00057.jpg
│ ├── filename_00058.jpg
│ └── filename_00059.jpg
├── fog_augment.py
├── for_paper
├── CDNet_plot.py
├── data
│ └── results.yaml
├── entropy_analyse.py
└── overall_data
├── models
├── common.py
├── commonSE.py
├── experimental.py
├── yolo.py
├── yoloSE.py
└── yolov5m.yaml
├── repos
└── mmdetection
│ ├── configs
│ ├── _base_
│ │ └── datasets
│ │ │ ├── zzd_coco_detection.py
│ │ │ ├── zzd_coco_instance.py
│ │ │ ├── zzd_crosswalk_detection.py
│ │ │ └── zzd_crosswalk_instance.py
│ ├── centernet
│ │ └── zzd_centernet_resnet18_dcnv2_140e_crosswalk.py
│ ├── deformable_detr
│ │ └── zzd_deformable_detr_r50_16x2_50e_crosswalk.py
│ ├── faster_rcnn
│ │ ├── zzd_faster_rcnn_r50_fpn_1x_coco.py
│ │ └── zzd_faster_rcnn_r50_fpn_1x_crosswalk.py
│ ├── htc
│ │ ├── zzd_htc_r50_fpn_1x_crosswalk.py
│ │ └── zzd_htc_without_semantic_r50_fpn_1x_crosswalk.py
│ ├── mask_rcnn
│ │ ├── zzd_mask_rcnn_r50_fpn_1x_coco.py
│ │ └── zzd_mask_rcnn_r50_fpn_1x_crosswalk.py
│ ├── scnet
│ │ └── zzd_scnet_r50_fpn_1x_crosswalk.py
│ ├── swin
│ │ └── zzd_mask_rcnn_swin-t-p4-w7_fpn_1x_crosswalk.py
│ └── yolox
│ │ ├── zzd_yolox_m_8x8_300e_crosswalk.py
│ │ └── zzd_yolox_s_8x8_300e_crosswalk.py
│ ├── demo
│ └── zzd_inference_speed_test.py
│ ├── zzd_inference.py
│ └── zzd_train.py
├── requirements.txt
├── scripts
├── __init__.py
├── check_coco.py
├── check_dataset.py
├── get_confusing_matrix.py
├── post.bak.py
├── post.py
├── pt2wts.py
├── synthetic_fog.py
├── test.py
└── yolo2coco.py
├── train.py
└── utils
├── __init__.py
├── datasets.py
├── general.py
├── google_utils.py
└── torch_utils.py
/.gitignore:
--------------------------------------------------------------------------------
1 | /for_paper/
2 | /repos
3 | /weights
--------------------------------------------------------------------------------
/.idea/CrosswalkDetectionNet.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/deployment.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/dictionaries/tanmenglu.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/remote-mappings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.idea/vagrant.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](
2 | https://github.com/zhangzhengde0225/CDNet)
3 | [](
4 | https://github.com/zhangzhengde0225/CDNet/issues)
5 | [](
6 | https://github.com/zhangzhengde0225/CDNet/blob/master/docs/DATASETS.md)
7 | [](
8 | https://github.com/zhangzhengde0225/CDNet/archive/refs/heads/master.zip)
9 |
10 | #### English | [简体中文](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/README_zh_cn.md)
11 |
12 | Please **star this project** in the upper right corner and **cite this paper** blow
13 | if this project helps you.
14 |
15 | # CDNet
16 |
17 | This repository is the codes, datasets and tutorials for the paper
18 | "CDNet: a real-time and robust crosswalk detection network on Jetson nano based on YOLOv5".
19 |
20 | CDNet (Crosswalk Detection Network) is a specific implementation of crosswalk (zebra crossing) detection and vehicle crossing behavior analysis under the vision of vehicle-mounted camera.
21 |
22 | 
23 |
24 | Fig.1 Graphical abstract.
25 |
26 | # Highlights
27 | + A crosswalk detection and vehicle crossing behavior detection network is realized.
28 | + The accuracy and speed exceed YOLOv5 in the specific task.
29 | + High robustness in real complex scenarios such as in cloudy, sunny, rainy and at night is achieved.
30 | + Real-time detection (33.1 FPS) is implemented on Jetson nano edge-computing device.
31 | +The datasets, tutorials and source codes are available on GitHub.
32 |
33 | # Contribution
34 |
35 | + SENet (Squeeze-and-Excitation Network), F1 score up, speed slightly down
36 | + NST (Negative Samples Training), F1 score up, speed invariant
37 | + ROI (Region Of Interest), F1 score down, speed up
38 | + SSVM (Slide receptive field Short-term Vectors Memory), transfer crosswalk detection task into vehicle crossing behavior task,
39 | F1 score up, speed invariant
40 | + SFA (Synthetic Fog Augment), dataset augment, adapt to foggy weather, F1 score up, speed invariant
41 |
42 | # Installation
43 | Get CDNet code and configure the environment, please check out [docs/INSTALL.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/INSTALL.md)
44 |
45 | # Model Zoo
46 | Please check out [docs/MODEL_ZOO.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/MODEL_ZOO.md)
47 |
48 | # Datasets
49 | Download trainsets and testsets, please check out [docs/DATASETS.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/DATASETS.md)
50 |
51 | # Quick Start
52 | ## Train
53 |
54 | Once you get the CDNet code, configure the environment and download the dataset, juse type:
55 | ```
56 | python train.py --trainset_path
57 | (such as: /home/xxx/datasets/train_data_yolov5_format)
58 | ```
59 | The training results and weights will be saved in runs/expxx/ directory.
60 |
61 | The main optional arguments:
62 | ```
63 | --device "0, 1" # cpu or gpu id, "0, 1" means use two gpu to train.
64 | --img-size 640
65 | --batch-size 32
66 | --epochs 100
67 | --not-use-SE # use original YOLOv5 which not SE-module embedded if there is the flag
68 | ```
69 |
70 | ## Inference
71 | Detect the crosswalk image by image and analyze the vehicle crossing behavior.
72 | ```
73 | python detect.py
74 | ```
75 |
76 | The main optional arguments:
77 | ```
78 | --source example/images # images dir
79 | --output example/output # output dir
80 | --img-size 640 # inference model size
81 | --device "0" # use cpu or gpu(gpu id)
82 | --plot-classes ["crosswalk"] # plot classes
83 | --field-size 5 # the Slide receptive field size of SSVM
84 | --not-use-ROI # not use roi for accelerate inference speed if there is the flag
85 | --not-use-SSVM # not use ssvm method for analyse vehicle crossing behavior if there is the flag
86 | ```
87 |
88 | For more details, please refer to [docs/INSTALL.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/INSTALL.md) and [docs/DATASETS.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/DATASETS.md).
89 |
90 | ## Fogging Augment
91 | If you want to augment datasets by synthetic fog algorithm, just run:
92 | ```
93 | python fog_augment.py
94 | ```
95 | For more details, please view the source code in fog_augment.py and /scripts/synthetic_fog.py
96 |
97 | # Results
98 |
99 | 
100 |
101 | Fig.2 Performance compared to YOLOv5.
102 |
103 | **CDNet improves the score for 5.13 points and speed for 10.7 FPS on Jetson nano for detection size of 640 compared to YOLOv5.**
104 |
105 | **For detection size of 288, the improvements are 13.38 points and 13.1 FPS.**
106 |
107 |
108 | # Contributors
109 | CDNet is authored by Zhengde Zhang, Menglu Tan, Zhicai Lan, Haichun Liu, Ling Pei and Wenxian Yu.
110 |
111 | Currently, it is maintained by Zhengde Zhang (drivener@163.com).
112 |
113 | Please feel free to contact us if you have any question.
114 |
115 | The Academic homepage of Zhengde Zhang: [zhangzhengde0225.github.io](https://zhangzhengde0225.github.io).
116 |
117 | # Acknowledgement
118 |
119 | This work was supported by the National Natural
120 | Science Foundation of China [Grant Numbers: 61873163].
121 |
122 | We acknowledge the Center for High Performance Computing at
123 | Shanghai Jiao Tong University for providing computing resources.
124 |
125 | We are very grateful to the
126 | [yolov5](https://github.com/ultralytics/yolov5) project
127 | for the benchmark detection algorithm.
128 |
129 | We are very grateful to the
130 | [tensorrtx](https://github.com/wang-xinyu/tensorrtx) project
131 | for the deployment techniques to the Jetson nano.
132 |
133 | # Links
134 | Detect Video Samples:[https://www.bilibili.com/video/BV1qf4y1B7BA](https://www.bilibili.com/video/BV1qf4y1B7BA)
135 |
136 | Read Full Text of This Paper:[https://rdcu.be/cHuc8](https://rdcu.be/cHuc8)
137 |
138 | Download Full Text of this Paper:[https://doi.org/10.1007/s00521-022-07007-9](https://doi.org/10.1007/s00521-022-07007-9)
139 |
140 | Project Introduction on CSDN:[http://t.csdn.cn/Cf7c7](http://t.csdn.cn/Cf7c7)
141 |
142 | If it is helps you,
143 | please star this project in the upper right corner and cite this paper blow.
144 |
145 | # Citation
146 | ```
147 | @article{CDNet,
148 | author={Zheng-De Zhang, Meng-Lu Tan, Zhi-Cai Lan, Hai-Chun Liu, Ling Pei and Wen-Xian Yu},
149 | title={CDNet: a real-time and robust crosswalk detection network on Jetson nano based on YOLOv5},
150 | Journal={Neural Computing and Applications},
151 | Year={2022},
152 | DOI={10.1007/s00521-022-07007-9},
153 | }
154 | ```
155 |
156 | # License
157 | CDNet is freely available for free non-commercial use, and may be redistributed under these conditions. For commercial queries, please drop an e-mail at drivener@163.com. We will send the detail agreement to you.
158 |
159 |
160 |
161 |
162 |
163 |
--------------------------------------------------------------------------------
/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/__init__.py
--------------------------------------------------------------------------------
/data/Detection results compare to YOLOv5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/Detection results compare to YOLOv5.jpg
--------------------------------------------------------------------------------
/data/Detection results compare to YOLOv5_0.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/Detection results compare to YOLOv5_0.jpg
--------------------------------------------------------------------------------
/data/Detection results compare to YOLOv5_1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/Detection results compare to YOLOv5_1.jpg
--------------------------------------------------------------------------------
/data/SHNAVI.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/SHNAVI.jpg
--------------------------------------------------------------------------------
/data/cl_setting.yaml:
--------------------------------------------------------------------------------
1 | # control line setting for vehicle crossing crosswalk behavior detection
2 |
3 | # control_line in pixels for different videos
4 | general: [400, 680] # two horizontal lines parallel to the y-axis in pixels coordinates. [top, button]
5 | '01_4885': [380, 718]
6 | '02_0036': [400, 680]
7 | '03_0760': [380, 718]
8 | '04_1421': [400, 680]
9 | '05_0823': [225, 590]
10 | '06_5271': [365, 635]
11 | '13_3519': [300, 718]
12 | '14_9247': [280, 718]
13 | '15_9440': [270, 718]
14 |
15 |
16 | # red line
17 | red_line: 0.5 # proportion of diff. between red_line and top_line to diff. between button_line and top_line
18 |
19 |
--------------------------------------------------------------------------------
/data/crosswalk detection in complex realistic scenarios.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/crosswalk detection in complex realistic scenarios.jpg
--------------------------------------------------------------------------------
/data/crosswalk.yaml:
--------------------------------------------------------------------------------
1 | # PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC/
2 | # Download command: bash ./data/get_voc.sh
3 | # Train command: python train.py --data voc.yaml
4 | # Default dataset location is next to /yolov5:
5 | # /parent_folder
6 | # /VOC
7 | # /yolov5
8 |
9 |
10 | # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
11 | # train: /home/zzd/datasets/crosswalk/train_data_v5_format/images/train
12 | # val: /home/zzd/datasets/crosswalk/train_data_v5_format/images/test
13 |
14 | # number of classes
15 | nc: 2
16 |
17 | # class names
18 | names: ['crosswalk', 'guide_arrows']
19 |
--------------------------------------------------------------------------------
/data/graphical_abstract.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/graphical_abstract.jpg
--------------------------------------------------------------------------------
/data/rect.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/rect.jpg
--------------------------------------------------------------------------------
/data/set_ai.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/data/set_ai.png
--------------------------------------------------------------------------------
/detect.py:
--------------------------------------------------------------------------------
1 | import argparse
2 | import os
3 | import platform
4 | import shutil
5 | import time
6 | from pathlib import Path
7 | import numpy as np
8 | import yaml
9 |
10 | import cv2
11 | import torch
12 | import torch.backends.cudnn as cudnn
13 | from numpy import random
14 | from tqdm import tqdm
15 |
16 | from models.experimental import attempt_load
17 | from utils.datasets import LoadStreams, LoadImages
18 | from utils.general import check_img_size, non_max_suppression, apply_classifier, scale_coords, xyxy2xywh, plot_one_box
19 | from utils.torch_utils import select_device, load_classifier, time_synchronized
20 | from scripts import post
21 |
22 |
23 | def detect(opt, dp, save_img=False):
24 | out, source, weights, view_img, save_txt, imgsz = \
25 | opt.output, opt.source, opt.weights, opt.view_img, opt.save_txt, opt.img_size
26 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt')
27 |
28 | # Initialize
29 | device = select_device(opt.device)
30 | if os.path.exists(out):
31 | shutil.rmtree(out) # delete output folder
32 | os.makedirs(out) # make new output folder
33 | half = device.type != 'cpu' # half precision only supported on CUDA
34 |
35 | # Load model
36 | model = attempt_load(weights, map_location=device) # load FP32 model
37 |
38 | imgsz = check_img_size(imgsz, s=model.stride.max()) # check img_size 如果不是32的倍数,就向上取整调整至32的倍数并答应warning
39 |
40 | if half:
41 | model.half() # to FP16
42 |
43 | # Second-stage classifier
44 | classify = False
45 | if classify:
46 | modelc = load_classifier(name='resnet101', n=2) # initialize
47 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights
48 | modelc.to(device).eval()
49 |
50 | # Set Dataloader
51 | vid_path, vid_writer = None, None
52 | if opt.use_roi:
53 | # print(dp.cl)
54 | # print(dp.cl[0], dp.cl[1])
55 | # cl = opt.control_line
56 | cl = dp.cl
57 | roi_in_pixels = np.array([0, cl[0], 1280, cl[1]]) # two points coor, x1, y1, x2, y2
58 | else:
59 | roi_in_pixels = None
60 |
61 | if webcam:
62 | view_img = True
63 | cudnn.benchmark = True # set True to speed up constant image size inference
64 | dataset = LoadStreams(source, img_size=imgsz)
65 | else:
66 | save_img = True
67 | dataset = LoadImages(source, img_size=imgsz, roi=roi_in_pixels)
68 |
69 | # Get names and colors
70 | names = model.module.names if hasattr(model, 'module') else model.names # 解决GPU保存的模型多了module属性的问题
71 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] # 随机颜色,对应names,names是class
72 |
73 | # fix issue: when single cls, names = ['item'] rather than names = ['crosswalk']
74 | if 'item' in names:
75 | names = ['crosswalk']
76 |
77 | # prune
78 | # torch_utils.prune(model, 0.7)
79 | model.eval()
80 |
81 | # Run inference
82 | t0 = time.time()
83 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img
84 | _ = model(img.half() if half else img) if device.type != 'cpu' else None # run once 空跑一次,释放!!牛逼
85 |
86 | detected_img_id = 0
87 | time_list = [None] * len(dataset)
88 | bar = tqdm(dataset)
89 | for iii, (path, img, im0s, vid_cap, recover) in enumerate(bar):
90 | # print(img.shape, im0s.shape, vid_cap)
91 | # exit()
92 |
93 | # img.shape [3, 384, 640] im0s.shape [720, 1280, 3] None
94 | img = torch.from_numpy(img).to(device)
95 | img = img.half() if half else img.float() # uint8 to fp16/32
96 | img /= 255.0 # 0 - 255 to 0.0 - 1.0
97 | if img.ndimension() == 3:
98 | img = img.unsqueeze(0) # 从[3, h, w]转换为[batch_size, 3, h, w]的形式
99 |
100 | # Inference
101 | t1 = time_synchronized()
102 | # print('aug', opt.augment) # False
103 | pred = model(img, augment=opt.augment)[0]
104 | # print(pred.shape) [1, 15120, 25]
105 | # Apply NMS
106 | pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)
107 | t2 = time_synchronized()
108 | infer_time = t2 - t1
109 | time_list[iii] = t2-t1
110 |
111 | # print('\n', len(pred), pred, recover) # list 长度是bs,代表每张图, 元素tensor,代表检测到的目标,每个tensor.shape [n, 6] xy4, conf, cls
112 |
113 | # Apply Classifier
114 | if classify:
115 | pred = apply_classifier(pred, modelc, img, im0s)
116 |
117 | # Process detections
118 | for i, det in enumerate(pred): # detections per image
119 | if opt.use_roi and det is not None:
120 | small_img_shape = torch.from_numpy(np.array([recover[1], recover[0]]).astype(np.float))
121 | det[:, 0], det[:, 2] = det[:, 0] + recover[2], det[:, 2] + recover[2]
122 | det[:, 1], det[:, 3] = det[:, 1] + recover[3], det[:, 3] + recover[3]
123 | else:
124 | small_img_shape = img.shape[2::]
125 | if webcam: # batch_size >= 1
126 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy()
127 | else:
128 | p, s, im0 = path, '', im0s # im0s是原图
129 |
130 | save_path = str(Path(out) / Path(p).name) # output/filenamexxxx.jpg
131 | txt_path = str(Path(out) / Path(p).stem) + ('_%g' % dataset.frame if dataset.mode == 'video' else '')
132 | # output/filenamexxxx.txt
133 | s += '%gx%g ' % img.shape[2:] # print string, 640x640
134 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
135 | # 本来是[720, 1280, 3],重复取,变成[1280, 720, 1280, 720]
136 | if det is not None and len(det):
137 | # Rescale boxes from img_size to im0 size
138 | det[:, :4] = scale_coords(small_img_shape, det[:, :4], im0.shape).round() # 转换成原图的x1 y1 x2 y1,像素值
139 | # Print results
140 | for c in det[:, -1].unique():
141 | n = (det[:, -1] == c).sum() # detections per class
142 | s += '%g %ss, ' % (n, names[int(c)]) # add to string # i.e. 1 crosswalk
143 | # s += f'{det[:, 4].item():.4f} '
144 | # print(n)
145 |
146 | # Write results
147 | for *xyxy, conf, cls in det:
148 | if save_txt: # Write to file
149 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
150 | with open(txt_path + '.txt', 'a') as f:
151 | x, y, w, h = xywh
152 | string = f"{int(cls)} {conf.item():.4f} {x:.6f} {y:.6f} {w:.6f} {h:.6f}\n"
153 | f.write(string) # label format
154 |
155 | if save_img or view_img: # Add bbox to image
156 | label = '%s %.2f' % (names[int(cls)], conf)
157 | # print(type(im0), im0.shape) array, 720, 1280, 3
158 | if names[int(cls)] in opt.plot_classes:
159 | # color = colors[int(cls)]
160 | color = (255, 85, 33)
161 | plot_one_box(xyxy, im0, label=label, color=color, line_thickness=5)
162 |
163 | # Print time (inference + NMS)
164 | prt_str = '%sDone. (%.5fs)' % (s, t2 - t1)
165 | # print(prt_str)
166 | os.system(f'echo "{prt_str}" >> {opt.output}/detect.log')
167 |
168 | # Stream results
169 | if view_img:
170 | cv2.imshow(p, im0)
171 | if cv2.waitKey(1) == ord('q'): # q to quit
172 | raise StopIteration
173 |
174 | # Save results (image with detections)
175 | if save_img:
176 | if dataset.mode == 'images':
177 | im0 = dp.dmpost(im0, det, det_id=detected_img_id, filename=Path(p).name, names=names)
178 | cv2.imwrite(save_path, im0)
179 | else:
180 | if vid_path != save_path: # new video
181 | vid_path = save_path
182 | if isinstance(vid_writer, cv2.VideoWriter):
183 | vid_writer.release() # release previous video writer
184 |
185 | fourcc = 'mp4v' # output video codec
186 | fps = vid_cap.get(cv2.CAP_PROP_FPS)
187 | w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
188 | h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
189 | vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*fourcc), fps, (w, h))
190 | # print(detected_img_id, p, txt_path)
191 | tmp_filename = Path(txt_path).stem
192 | im0 = dp.dmpost(im0, det, det_id=detected_img_id, filename=tmp_filename, names=names)
193 | vid_writer.write(im0)
194 | detected_img_id += 1
195 | bar.set_description(f'inf_time: {infer_time*1000:.2f}ms {prt_str:<40}')
196 |
197 | if save_txt or save_img:
198 | print('Results saved to %s' % out)
199 | if platform == 'darwin' and not opt.update: # MacOS
200 | os.system('open ' + save_path)
201 |
202 | print('Done. (%.3fs)' % (time.time() - t0))
203 | time_arr = np.array(time_list)
204 | prnt = f'Done. Network mean inference time: {np.mean(time_arr)*1000:.2f}ms, Mean FPS: {1/np.mean(time_arr):.2f}.'
205 | print(f'\nModel size {opt.img_size} inference {prnt}')
206 | os.system(f'echo "{prnt}" >> {opt.output}/detect.log')
207 | os.system(f'echo "useroi {opt.img_size} {prnt}" >> detect2.log')
208 |
209 |
210 | def run(opt, dp):
211 | with torch.no_grad():
212 | if opt.update: # update all models (to fix SourceChangeWarning)
213 | for opt.weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt']:
214 | detect()
215 | strip_optimizer(opt.weights)
216 | else:
217 | detect(opt, dp)
218 |
219 |
220 | def get_opt():
221 | parser = argparse.ArgumentParser()
222 | parser.add_argument('--weights', nargs='+', type=str, required=False,
223 | default='runs/SEm_NST1_fog1_ep100/weights/best.pt',
224 | help='trained model path model.pt ddpath(s)')
225 | parser.add_argument('--source', type=str, default='example/images', help='source') # file/folder, 0 for webcam
226 | parser.add_argument('--output', type=str, default='example/output', help='output folder') # output folder
227 | parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
228 | parser.add_argument('--conf-thres', type=float, default=0.4, help='object confidence threshold')
229 | parser.add_argument('--iou-thres', type=float, default=0.5, help='IOU threshold for NMS')
230 | parser.add_argument('--device', default='0', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
231 | parser.add_argument('--view-img', action='store_true', help='display results')
232 | parser.add_argument('--save-txt', type=bool, default=True, help='save results to *.txt')
233 | parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
234 | parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
235 | parser.add_argument('--augment', action='store_true', help='augmented inference')
236 | parser.add_argument('--update', action='store_true', help='update all models')
237 | parser.add_argument('--control-line-setting', type=str, default='settings/cl_setting.yaml',
238 | help='control line setting')
239 | parser.add_argument('--select-control-line', type=str, default='general',
240 | help='select which control line. i.e. general, 0036')
241 | parser.add_argument('--field-size', type=int, default=5, help='receptive field size for post')
242 | parser.add_argument('--plot-classes', type=list, default=['crosswalk'],
243 | help='specifies which classes will be drawn')
244 | parser.add_argument('--not-use-ROI', action='store_true',
245 | help='not use roi for accelerate inference speed if there is the flag')
246 | parser.add_argument('--not-use-SSVM', action='store_true',
247 | help='not use ssvm method for analyse vehicle crossing behavior if there is the flag')
248 |
249 | opt = parser.parse_args()
250 | return opt
251 |
252 |
253 | if __name__ == '__main__':
254 | opt = get_opt()
255 | opt.weights = 'runs/m_ep300/weights/best.pt'
256 | opt.not_use_ROI = True
257 | opt.not_use_SSVM = True
258 |
259 | opt.use_roi = not opt.not_use_ROI
260 | opt.use_ssvm = not opt.not_use_SSVM
261 | for_paper = False
262 | if for_paper:
263 | exps = [
264 | 'm_ep300', 'SEm_NST0_fog0_ep100', 'SEm_NST1_fog0_ep100',
265 | 'SEm_NST1_fog0_ep300', 'SEm_NST1_fog1_ep100']
266 | exp = exps[1]
267 |
268 | roi = int(opt.use_roi)
269 | ssvm = int(opt.use_ssvm)
270 |
271 | opt.weights = f"runs/{exp}/weights/best.pt"
272 | opt.source = "/home/zzd/datasets/crosswalk/testsets_1770/Images"
273 | opt.output = f"/home/zzd/datasets/crosswalk/testsets_1770/{exp}_sz{opt.img_size}_ROI{roi}_SSVM{ssvm}"
274 |
275 | dp = post.DmPost(opt)
276 |
277 | print(opt)
278 | runtime = time.time()
279 | run(opt, dp)
280 | print(f'Total runtime: {time.time()-runtime:.5f}')
281 |
282 |
283 |
--------------------------------------------------------------------------------
/docs/DATASETS.md:
--------------------------------------------------------------------------------
1 | # DATASETS
2 |
3 | The crosswalk detection datasets contains 3080 images for train and 1770 images for test.
4 |
5 | ## Trainset
6 | + Download from [Baidu Pan](https://pan.baidu.com/s/1jAsQ_kbU7cX0AqK4wDm6LA), passwd: **hul6**
7 | + Download from [Google Drive](https://drive.google.com/file/d/1RIIpdrAUXZRuoOgHMIr-BSsJxSjqiKC8/view?usp=sharing).
8 | + Download from [SJTU Jbox](https://jbox.sjtu.edu.cn/l/Z0i6nQ).
9 | + or use wget:
10 | ```
11 | wget -c -O train_data_v5_format_3080.tar https://jbox.sjtu.edu.cn:10081/v2/delivery/data/de2ac2610893499686d095f174aa6ffd/?token=
12 | ```
13 | **Notes**
14 |
15 | Please remove proxy if download failed.
16 |
17 | Unzip:
18 | ```
19 | tar -xvf train_data_v5_format_3080.tar
20 | cd train_data_v5_format_3080
21 | rm labels/*.cache # if you are the first time to use this datasets.
22 | ```
23 | The trainsets contains 3080 images, includes training and verification set, and the file structure format is YOLOv5 likes:
24 | ```
25 | |--train_data_v5_format
26 | --|--images # the .jpg images
27 | --|--train # trainning images
28 | -- xxx.jpg
29 | -- ...
30 | --|--test # validation images
31 | --|--labels # corresponding .txt labels
32 | --|--train # trainning labels
33 | -- xxx.txt
34 | -- ...
35 | --|--test # validation labels
36 | ```
37 | Each .txt file contains annotations in the format of CLS XC YC W H in each line.
38 |
39 | CLS(Classes): crosswalk, guide_arrows
40 |
41 | XC YC W H in terms of percentage.
42 |
43 |
44 | ## Testsets
45 | + Download from [Baidu Pan](https://pan.baidu.com/s/1-AWw2AjE8zTP-iVjOoifOg), passwd: **vj6b**
46 | + Download from [Google Drive](https://drive.google.com/file/d/1DBVt81LehEyiTxuUn-BUGBoh3gXcwrEE/view?usp=sharing).
47 | + Download from [SJTU Jbox](https://jbox.sjtu.edu.cn/l/eHE7vD).
48 | + or use wget:
49 | ```
50 | wget -c -O testsets_1770.tar https://jbox.sjtu.edu.cn:10081/v2/delivery/data/a526b168356949068e6a13e8c254a71b/?token=
51 | ```
52 |
53 | Unzip:
54 | ```
55 | tar -xvf testsets_1770.tar
56 | cd testsets_1770
57 | ```
58 | The testsets contains 1770 images.
59 |
60 | Currently we do not provide the testsets for vehicle crossing behavior analysis.
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/docs/INSTALL.md:
--------------------------------------------------------------------------------
1 | # Installation
2 |
3 | This document describes how to get CNDet source code and configure the running environment.
4 |
5 | ## Requirements
6 | + Python 3.6+
7 | + Pytorch 1.6+
8 | + Linux or MacOS
9 |
10 | ## Get CDNet Code
11 |
12 | ##### use git:
13 | ```python
14 | git clone https://github.com/zhangzhengde0225/CDNet.git
15 | cd cdnet
16 | ```
17 | ##### from github web:
18 | + Browse the [CDnet] repository
19 | + Click "Code"-"Download ZIP"
20 | ```python
21 | unzip CDNet-master.zip
22 | cd CDNet_master
23 | ```
24 |
25 |
26 | ## Configure Environment
27 | [Anaconda](https://www.anaconda.com) is highly recommanded.
28 |
29 | Haven't Anaconda been installed yet? Download anaconda installer [here](https://www.anaconda.com/products/individual#Downloads) and install it:
30 | ```python
31 | chmod +x ./Anaconda3-2020.11-Linux-.sh
32 | ./Anaconda3-2020.11-Linux-.sh # install
33 | which conda # verify installation
34 | ```
35 |
36 | After having a CONDA, directly import the fully configured environment:
37 | ```python
38 | conda env creat -f conda_cdnet_env.yaml
39 | ```
40 |
41 | or Creating a environment from sratch:
42 | ```python
43 | conda create --name cdnet python=3.7 # create a env named as "cdnet"
44 | conda activate cdnet # activate the env
45 | which pip # verify pip
46 | pip install -r requirements.txt # install packages use pip
47 | # or use conda to install package
48 | conda install
49 | ```
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/docs/MODEL_ZOO.md:
--------------------------------------------------------------------------------
1 | # MODEL ZOO
2 |
3 | ## Pytorch model
4 |
5 | |Model|Detect Size|Speed (FPS)|F1 Score (%)|Pytorch Model|TensorRT Engine|
6 | |:---:|:---:|:---:|:---:|:---:|:---:|
7 | |YOLOv5m (baseline)|288x192|20.0|81.34|[Download](https://pan.baidu.com/s/1FCW5urZynjXjteR1E_2XuA) passwd: **duap**|[Download](link)
8 | |SE-YOLOv5m+NST+ROI+SSVM (CDNet)|288X192|33.1|94.72|[Download](https://pan.baidu.com/s/18p2TS-x830X7IUAVsdihow) passwd: **1cpp**|[Download](link)
9 |
10 | **Notes**
11 |
12 | Detection speed measured on **Jetson Nano** (4GB) with JetPack-4.4, cuda-10.2 and TensorRT-7.1.3.
13 |
14 | We also tested the GPU inference time on **RTX 3080** using [TensorRT](https://developer.nvidia.com/tensorRt), and the result shows that the speed is about 323 FPS (3.1 ms) per image.
15 |
16 | Note that TensorRT is optimized for specific TensorRT version, CUDA version and hardware, so the engine downloaded from this page can only be used on Jetson Nano with specific package installed.
17 |
18 | If you want to export the TensorRT engine from the pytorch weights for specific hardware and package, please refers to [here](https://github.com/wang-xinyu/tensorrtx).
--------------------------------------------------------------------------------
/docs/Press_Material.md:
--------------------------------------------------------------------------------
1 |
2 | 今天带来一个基于改进YOLOv5的斑马线和汽车过线行为检测算法,文章提出多项tricks,数据集和复现代码开源!
3 |
4 | 标题: CDNet: A Real-Time and Robust Crosswalk Detection Network on Jetson Nano Based on YOLOv5
5 | CDNet: 一个基于YOLOv5的在Jetson Nano上实时、鲁棒的斑马线检测网络
6 | 作者:Zheng-De Zhang, Meng-Lu Tan, Zhi-Cai Lan, Hai-Chun Liu, Ling Pei, Wen-Xian Yu
7 | 时间:Feb, 2022
8 | 期刊:Neural Computing & Applications, IF 5.6
9 |
10 | 图形摘要:
11 |
12 |
13 | 摘要:
14 | 在复杂场景和有限计算能力下实现实时、鲁棒的斑马线(人行横道)检测是当前智能交通管理系统(ITMS)的重要难点之一。有限的边缘计算能力和多云、晴天、雨天、雾天和夜间等真实复杂的场景同时对这项任务提出了挑战。本研究提出基于改进YOLOv5的人行横道检测网络(CDNet),实现车载摄像头视觉下快速准确的人行横道检测,并在Jetson nano设备上实现实时检测。强大的卷积神经网络特征提取器用于处理复杂环境,网络中嵌入了squeeze-and-excitation(SE)注意力机制模块,使用负样本训练(NST)方法提高准确率,利用感兴趣区域(ROI)算法进一步提高检测速度,提出了一种新的滑动感受野短时向量记忆(SSVM)算法来提高车辆交叉行为检测精度,使用合成雾增强算法允许模型适应有雾的场景。最后,在 Jetson nano 上以 33.1 FPS 的检测速度,我们在上述复杂场景中获得了 94.83% 的平均 F1 分数。对于晴天和阴天等更好的天气条件,F1 分数超过 98%。该工作为人工神经网络算法优化方法在边缘计算设备上的具体应用提供了参考,发布的模型为智能交通管理系统提供了算法支持。
15 |
16 | 贡献:
17 | + 注意力机制网络改进网络,提升精度,略微降低速度:SENet (Squeeze-and-Excitation Network)
18 | + 负样本训练,提升精度,速度不变: NST (Negative Samples Training)
19 | + 感兴趣区域,提升速度,精度下降:ROI (Region Of Interest)
20 | + 滑动感受野短时向量记忆算法,迁移斑马线检测任务到汽车过线行为检测任务,提升精度,速度不变:SSVM (Slide receptive field Short-term Vectors Memory)
21 | + 合成雾增强算法,增强数据集,适应雾天,提升精度,速度不变:SFA (Synthetic Fog Augment)
22 | + 斑马数据集:标注好的,车载摄像头视角下的,共计6868张图像。
23 | + 复现代码:见github项目主页
24 |
25 |
26 | 数据集、复现代码:https://github.com/zhangzhengde0225/CDNet
27 | 视频样例:https://www.bilibili.com/video/BV1qf4y1B7BA
28 | 阅读全文:https://rdcu.be/cHuc8
29 | 下载全文:https://doi.org/10.1007/s00521-022-07007-9
30 | 如果该项目对你有帮助,请点击github项目右上角star收藏和引用本论文,谢谢~
--------------------------------------------------------------------------------
/docs/README_zh_cn.md:
--------------------------------------------------------------------------------
1 |
2 | #### [English](https://github.com/zhangzhengde0225/CDNet) | 简体中文
3 |
4 | 如果本项目对你有帮助,请点击项目右上角star支持一下和引用下面的论文。
5 |
6 | # CDNet
7 |
8 | 这个项目是论文的《CDNet: 一个基于YOLOv5的在Jetson Nano上实时、鲁棒的斑马线检测网络》的代码、数据集和教程。
9 |
10 | CDNet (Crosswalk Detection Network) 是车载摄像头视野下检测斑马线(人行横道)和分析车辆过线行为的具体实现。
11 |
12 | 
13 |
14 | Fig.1 图形摘要
15 |
16 | # Highlights
17 | + 实现了斑马线检测和车辆过线行为检测。
18 | + 在特定任务中准确率和速度超过原生YOLOv5。
19 | + 在阴天、晴天、雨天、夜间等真实复杂场景中实现高鲁棒性。
20 | + 在 Jetson nano 边缘计算设备上实现实时检测 (33.1 FPS)。
21 | + 提供了标注好的斑马线数据集,共计6868张图 。
22 |
23 | # 贡献Contribution
24 |
25 | + 注意力机制网络改进网络,提升精度,略微降低速度:SENet (Squeeze-and-Excitation Network)
26 | + 负样本训练,提升精度,速度不变: NST (Negative Samples Training)
27 | + 感兴趣区域,提升速度,精度下降:ROI (Region Of Interest)
28 | + 滑动感受野短时向量记忆算法,迁移斑马线检测任务到汽车过线行为检测任务,提升精度,速度不变:SSVM (Slide receptive field Short-term Vectors Memory)
29 | + 合成雾增强算法,增强数据集,适应雾天,提升精度,速度不变:SFA (Synthetic Fog Augment)
30 |
31 | # 安装Installation
32 | 安装CDNet代码并配置环境,请查看:
33 | [docs/INSTALL.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/INSTALL.md)
34 |
35 | # 模型库Model Zoo
36 | 模型库请查看:
37 | [docs/MODEL_ZOO.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/MODEL_ZOO.md)
38 |
39 | # 数据集Datasets
40 | 下载训练集和测试集,请查看:
41 | [docs/DATASETS.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/DATASETS.md)
42 |
43 | # 快速开始Quick Start
44 | ## 训练Train
45 |
46 | 安装CDNet代码,配置环境和下载数据集后,输入代码训练:
47 | ```
48 | python train.py --trainset_path
49 | (such as: /home/xxx/datasets/train_data_yolov5_format)
50 | ```
51 | 训练结果和权重将保存在 runs/xxx/ 目录中。
52 |
53 | 主要的可选参数:
54 | ```
55 | --device "0, 1" # cpu or gpu id, "0, 1" means use two gpu to train.
56 | --img-size 640
57 | --batch-size 32
58 | --epochs 100
59 | --not-use-SE # use original YOLOv5 which not SE-module embedded if there is the flag
60 | ```
61 |
62 | ## 推理Inference
63 | 逐图或视频检测斑马线并分析车辆过线行为,输入代码:
64 | ```
65 | python detect.py
66 | ```
67 |
68 | 主要的可选参数:
69 | ```
70 | --source example/images # images dir
71 | --output example/output # output dir
72 | --img-size 640 # inference model size
73 | --device "0" # use cpu or gpu(gpu id)
74 | --plot-classes ["crosswalk"] # plot classes
75 | --field-size 5 # the Slide receptive field size of SSVM
76 | --not-use-ROI # not use roi for accelerate inference speed if there is the flag
77 | --not-use-SSVM # not use ssvm method for analyse vehicle crossing behavior if there is the flag
78 | ```
79 |
80 | 关于安装和数据集的更多详情请参考:
81 | [docs/INSTALL.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/INSTALL.md) and [docs/DATASETS.md](https://github.com/zhangzhengde0225/CDNet/blob/master/docs/DATASETS.md).
82 |
83 | ## 合成雾增强Fogging Augment
84 | 如果你想通过合成雾算法增加数据集,只需运行:
85 | ```
86 | python fog_augment.py
87 | ```
88 | 更多细节请查看fog_augment.py和/scripts/synthetic_fog.py中的源代码
89 |
90 | # 结果Results
91 |
92 | 
93 | Fig.2 与原生YOLOv5性能对比Performance compared to YOLOv5.
94 |
95 | **与原生YOLOv5相比,检测尺寸为640时,CDNet 在 Jetson nano 上提高了5.13%的F1分数和10.7FPS的速度,**
96 | **检测尺寸为288时,提升为13.38%的F1分数和13.1FPS。**
97 |
98 | # 贡献者Contributors
99 | CDNet的作者是: Zhengde Zhang, Menglu Tan, Zhicai Lan, Haichun Liu, Ling Pei and Wenxian Yu。
100 |
101 | 目前,CDNet由
102 | Zhengde Zhang (drivener@163.com)负责维护。
103 |
104 | 如果您有任何问题,请随时与我们联系。
105 |
106 | Zhengde Zhang的学术主页: [zhangzhengde0225.github.io](https://zhangzhengde0225.github.io).
107 |
108 | # 致谢Acknowledgement
109 |
110 | 本工作得到了国家自然科学基金会的支持[编号:61873163]。
111 |
112 | 我们非常感谢上海交通大学高性能计算中心提供计算资源。
113 |
114 | 我们非常感谢
115 | [yolov5](https://github.com/ultralytics/yolov5)
116 | 项目提供的目标检测算法基准。
117 |
118 | 我们非常感谢
119 | [tensorrtx](https://github.com/wang-xinyu/tensorrtx)
120 | 项目提供的模型模署到Jetson nano上的技术。
121 |
122 | # 链接Links
123 |
124 | B站视频样例:[https://www.bilibili.com/video/BV1qf4y1B7BA](https://www.bilibili.com/video/BV1qf4y1B7BA)
125 |
126 | 阅读论文全文:[https://rdcu.be/cHuc8](https://rdcu.be/cHuc8)
127 |
128 | 下载论文全文:[https://doi.org/10.1007/s00521-022-07007-9](https://doi.org/10.1007/s00521-022-07007-9)
129 |
130 | CSDN项目简介:[http://t.csdn.cn/Cf7c7](http://t.csdn.cn/Cf7c7)
131 |
132 | 如果对您有帮助,请为点击项目右上角的star支持一下或引用论文。
133 |
134 | # 引用Citation
135 | ```
136 | @article{CDNet,
137 | author={Zheng-De Zhang, Meng-Lu Tan, Zhi-Cai Lan, Hai-Chun Liu, Ling Pei and Wen-Xian Yu},
138 | title={CDNet: a real-time and robust crosswalk detection network on Jetson nano based on YOLOv5},
139 | Journal={Neural Computing and Applications},
140 | Year={2022},
141 | DOI={10.1007/s00521-022-07007-9},
142 | }
143 | ```
144 |
145 |
146 | # 许可License
147 | CDNet可免费用于非商业用途,并可在这些条件下重新分发。 如需商业咨询,请发送电子邮件至
148 | drivener@163.com,我们会将详细协议发送给您。
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
--------------------------------------------------------------------------------
/evaluate.py:
--------------------------------------------------------------------------------
1 | """
2 | evaluate model performance on test set
3 | (1) 指定detect后的目标文件夹,获取评估结果
4 | (2) 指定不存在的检测文件夹,需要指定权重、ROI等一系列参数,获取评估结果
5 | """
6 | import os
7 | import argparse
8 | from pathlib import Path
9 | import re
10 |
11 | import detect
12 | from scripts import post
13 | from scripts.get_confusing_matrix import GetConfusingMatrix
14 |
15 |
16 | class CDNetEvaluator(object):
17 | def __init__(self):
18 | self.gcm = GetConfusingMatrix()
19 |
20 | def __call__(self, opt):
21 | exps = sorted(os.listdir('./runs'))
22 | assert len(exps), f'Experiments not exists, please train model first'
23 | q = "\n".join([f'[{i:>2}] {x}' for i, x in enumerate(exps)])
24 | ipt = input(
25 | f'Existing experiments:\n{q}\nPlease select which model to evaluate\n'
26 | f'(0 for model0, 0,1,2 for model0,1,2): ')
27 | ipt = '0' if ipt == '' else ipt
28 | indexes = [int(x) for x in ipt.split(',')] if ',' in ipt else [int(ipt)]
29 | for idx in indexes:
30 | print('\n')
31 | exp = exps[idx]
32 | opt.weights = f'./runs/{exp}/weights/best.pt'
33 |
34 | roi = int(opt.use_roi)
35 | ssvm = int(opt.use_ssvm)
36 | tp = f'{Path(opt.source).parent}/{exp}_sz{opt.img_size}_ROI{roi}_SSVM{ssvm}{opt.fog}' # target path
37 |
38 | flag = 0 # 0: detect and evaluate, 1: direct evaluate
39 | if os.path.exists(tp):
40 | ipt = input(f'Output_path: {tp} exists, \nevaluate without detect [YES/no]: ')
41 | if ipt in ['Yes', 'y', 'Y', 'yes', '']:
42 | flag = 1
43 | elif ipt in ['No', 'N', 'n']:
44 | flag = 0
45 | else:
46 | raise NotImplementedError(f'input error: {ipt}')
47 | if flag == 0:
48 | opt.output = tp
49 | print(f'opt: {opt}')
50 | dp = post.DmPost(opt)
51 | detect.run(opt, dp)
52 |
53 | ret = self.gcm.analyse(thresh=0.5, v5_out=tp, pn_dir=Path(opt.source).parent)
54 | time, ret = self.read_time_and_fps(tp=tp)
55 |
56 | def read_time_and_fps(self, tp):
57 | with open(f'{tp}/detect.log', 'r') as f:
58 | log = f.readlines()
59 | time, fps = re.findall(f'\d+.\d+', log[-1])
60 | time = time[:-1]
61 | fps = fps[:-3]
62 | return time, fps
63 |
64 | def read_and_update_opt(eval_opt):
65 | det_opt = detect.get_opt()
66 | det_opt.source = eval_opt.source
67 | det_opt.output = eval_opt.output
68 | det_opt.not_use_ROI = eval_opt.not_use_ROI
69 | det_opt.not_use_SSVM = eval_opt.not_use_SSVM
70 | det_opt.img_size = eval_opt.img_size
71 |
72 | det_opt.use_roi = not det_opt.not_use_ROI
73 | det_opt.use_ssvm = not det_opt.not_use_SSVM
74 | return det_opt
75 |
76 |
77 | if __name__ == '__main__':
78 | parser = argparse.ArgumentParser()
79 | parser.add_argument('--source', type=str, default='example/images', help='source')
80 | parser.add_argument('--output', type=str, default='example/output', help='output folder') # output folder
81 | parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
82 | parser.add_argument('--not-use-ROI', action='store_true',
83 | help='not use roi for accelerate inference speed if there is the flag')
84 | parser.add_argument('--not-use-SSVM', action='store_true',
85 | help='not use ssvm method for analyse vehicle crossing behavior if there is the flag')
86 | opt = parser.parse_args()
87 |
88 | # opt.source = "/home/zzd/datasets/crosswalk/testsets_1770/Images"
89 | opt.source = "/home/zzd/datasets/crosswalk/testsets_1770/fogged_Images"
90 | opt = read_and_update_opt(opt)
91 | opt.fog = '_fogged' if 'fogged' in opt.source else ''
92 |
93 | CDNetE = CDNetEvaluator()
94 | CDNetE(opt)
95 |
96 |
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.1_th0.05.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.1_th0.05.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.3_th0.05.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.3_th0.05.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.5_th0.05.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.5_th0.05.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.7_th0.01.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.7_th0.01.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.7_th0.03.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.7_th0.03.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.7_th0.05.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.7_th0.05.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.7_th0.07.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.7_th0.07.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.7_th0.09.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.7_th0.09.jpg
--------------------------------------------------------------------------------
/example/fog_image/fogged_br0.9_th0.05.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/fogged_br0.9_th0.05.jpg
--------------------------------------------------------------------------------
/example/fog_image/raw.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/fog_image/raw.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/clearly_visible_0760_00001.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/clearly_visible_0760_00001.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/damaged_0036_00846.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/damaged_0036_00846.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/dazzling_0036_01707.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/dazzling_0036_01707.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/distorted_0760_01846.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/distorted_0760_01846.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/occluded_by_wiper_0760_02902.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/occluded_by_wiper_0760_02902.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/partialy_lost_0036_01558.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/partialy_lost_0036_01558.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/reflection_after_rain_0760_02711.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/reflection_after_rain_0760_02711.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/truncated_in_steering_0760_03208.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/truncated_in_steering_0760_03208.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/view_blocked_0036_05420.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/view_blocked_0036_05420.jpg
--------------------------------------------------------------------------------
/example/images/complex_scenarios/visible_at_night_0036_01127.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/complex_scenarios/visible_at_night_0036_01127.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00030.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00030.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00031.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00031.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00032.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00032.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00033.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00033.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00034.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00034.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00035.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00035.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00036.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00036.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00037.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00037.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00038.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00038.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00039.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00039.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00040.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00040.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00041.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00041.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00042.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00042.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00043.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00043.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00044.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00044.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00045.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00045.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00046.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00046.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00047.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00047.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00048.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00048.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00049.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00049.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00050.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00050.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00051.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00051.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00052.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00052.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00053.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00053.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00054.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00054.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00055.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00055.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00056.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00056.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00057.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00057.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00058.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00058.jpg
--------------------------------------------------------------------------------
/example/images/exmp_imgs/filename_00059.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/exmp_imgs/filename_00059.jpg
--------------------------------------------------------------------------------
/example/images/filename_00030.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00030.jpg
--------------------------------------------------------------------------------
/example/images/filename_00031.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00031.jpg
--------------------------------------------------------------------------------
/example/images/filename_00032.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00032.jpg
--------------------------------------------------------------------------------
/example/images/filename_00033.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00033.jpg
--------------------------------------------------------------------------------
/example/images/filename_00034.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00034.jpg
--------------------------------------------------------------------------------
/example/images/filename_00035.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00035.jpg
--------------------------------------------------------------------------------
/example/images/filename_00036.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00036.jpg
--------------------------------------------------------------------------------
/example/images/filename_00037.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00037.jpg
--------------------------------------------------------------------------------
/example/images/filename_00038.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00038.jpg
--------------------------------------------------------------------------------
/example/images/filename_00039.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00039.jpg
--------------------------------------------------------------------------------
/example/images/filename_00040.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00040.jpg
--------------------------------------------------------------------------------
/example/images/filename_00041.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00041.jpg
--------------------------------------------------------------------------------
/example/images/filename_00042.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00042.jpg
--------------------------------------------------------------------------------
/example/images/filename_00043.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00043.jpg
--------------------------------------------------------------------------------
/example/images/filename_00044.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00044.jpg
--------------------------------------------------------------------------------
/example/images/filename_00045.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00045.jpg
--------------------------------------------------------------------------------
/example/images/filename_00046.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00046.jpg
--------------------------------------------------------------------------------
/example/images/filename_00047.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00047.jpg
--------------------------------------------------------------------------------
/example/images/filename_00048.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00048.jpg
--------------------------------------------------------------------------------
/example/images/filename_00049.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00049.jpg
--------------------------------------------------------------------------------
/example/images/filename_00050.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00050.jpg
--------------------------------------------------------------------------------
/example/images/filename_00051.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00051.jpg
--------------------------------------------------------------------------------
/example/images/filename_00052.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00052.jpg
--------------------------------------------------------------------------------
/example/images/filename_00053.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00053.jpg
--------------------------------------------------------------------------------
/example/images/filename_00054.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00054.jpg
--------------------------------------------------------------------------------
/example/images/filename_00055.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00055.jpg
--------------------------------------------------------------------------------
/example/images/filename_00056.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00056.jpg
--------------------------------------------------------------------------------
/example/images/filename_00057.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00057.jpg
--------------------------------------------------------------------------------
/example/images/filename_00058.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00058.jpg
--------------------------------------------------------------------------------
/example/images/filename_00059.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/filename_00059.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1120.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1120.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1121.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1121.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1122.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1122.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1123.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1123.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1124.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1124.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1125.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1125.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1126.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1126.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1127.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1127.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1128.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1128.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1129.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1129.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1130.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1130.jpg
--------------------------------------------------------------------------------
/example/images/for_overall_architecture/filename1131.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/images/for_overall_architecture/filename1131.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/detect.log:
--------------------------------------------------------------------------------
1 | 160x640 1 crosswalks, Done. (0.323s)
2 | 160x640 1 crosswalks, Done. (0.285s)
3 | 160x640 1 guide_arrowss, Done. (0.275s)
4 | 160x640 1 guide_arrowss, Done. (0.281s)
5 | 160x640 1 guide_arrowss, Done. (0.279s)
6 | 160x640 1 crosswalks, 1 guide_arrowss, Done. (0.284s)
7 | 160x640 1 crosswalks, 1 guide_arrowss, Done. (0.275s)
8 | 160x640 1 crosswalks, 1 guide_arrowss, Done. (0.276s)
9 | 160x640 1 crosswalks, Done. (0.286s)
10 | 160x640 1 crosswalks, Done. (0.282s)
11 | 160x640 1 crosswalks, Done. (0.278s)
12 | 160x640 1 crosswalks, Done. (0.279s)
13 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1120.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1120.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1120.txt:
--------------------------------------------------------------------------------
1 | 0 0.7516 0.305469 0.609722 0.164062 0.019444
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1121.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1121.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1121.txt:
--------------------------------------------------------------------------------
1 | 0 0.5696 0.291797 0.609722 0.188281 0.019444
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1122.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1122.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1122.txt:
--------------------------------------------------------------------------------
1 | 1 0.6779 0.257031 0.644444 0.253125 0.030556
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1123.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1123.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1123.txt:
--------------------------------------------------------------------------------
1 | 1 0.8535 0.220313 0.671528 0.317187 0.040278
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1124.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1124.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1124.txt:
--------------------------------------------------------------------------------
1 | 1 0.9064 0.201562 0.716667 0.403125 0.086111
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1125.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1125.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1125.txt:
--------------------------------------------------------------------------------
1 | 1 0.9220 0.208594 0.761111 0.417188 0.150000
2 | 0 0.6014 0.218750 0.645139 0.381250 0.037500
3 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1126.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1126.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1126.txt:
--------------------------------------------------------------------------------
1 | 1 0.9169 0.220313 0.834722 0.440625 0.227778
2 | 0 0.8203 0.229297 0.656250 0.430469 0.051389
3 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1127.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1127.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1127.txt:
--------------------------------------------------------------------------------
1 | 0 0.8860 0.241016 0.665278 0.466406 0.066667
2 | 1 0.8282 0.239063 0.851389 0.478125 0.208333
3 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1128.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1128.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1128.txt:
--------------------------------------------------------------------------------
1 | 0 0.8918 0.248047 0.689583 0.496094 0.087500
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1129.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1129.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1129.txt:
--------------------------------------------------------------------------------
1 | 0 0.9231 0.273047 0.729167 0.539844 0.122222
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1130.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1130.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1130.txt:
--------------------------------------------------------------------------------
1 | 0 0.9285 0.314844 0.790278 0.629687 0.216667
2 |
--------------------------------------------------------------------------------
/example/out_for_overall/filename1131.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/out_for_overall/filename1131.jpg
--------------------------------------------------------------------------------
/example/out_for_overall/filename1131.txt:
--------------------------------------------------------------------------------
1 | 0 0.8945 0.471484 0.830556 0.942969 0.255556
2 |
--------------------------------------------------------------------------------
/example/output/detect.log:
--------------------------------------------------------------------------------
1 | 160x640 Done. (0.306s)
2 | filename_00030.jpg detect_id: 0 speed: None count: 0 status: No crosswalk
3 | 160x640 Done. (0.283s)
4 | filename_00031.jpg detect_id: 1 speed: None count: 0 status: No crosswalk
5 | 160x640 Done. (0.279s)
6 | filename_00032.jpg detect_id: 2 speed: None count: 0 status: No crosswalk
7 | 160x640 Done. (0.290s)
8 | filename_00033.jpg detect_id: 3 speed: None count: 0 status: No crosswalk
9 | 160x640 Done. (0.284s)
10 | filename_00034.jpg detect_id: 4 speed: None count: 0 status: No crosswalk
11 | 160x640 Done. (0.276s)
12 | filename_00035.jpg detect_id: 5 speed: None count: 0 status: No crosswalk
13 | 160x640 1 guide_arrowss, Done. (0.281s)
14 | filename_00036.jpg detect_id: 6 speed: None count: 0 status: No crosswalk
15 | 160x640 1 crosswalks, 1 guide_arrowss, Done. (0.276s)
16 | filename_00037.jpg detect_id: 7 speed: None count: 0 status: No crosswalk
17 | 160x640 1 crosswalks, Done. (0.291s)
18 | filename_00038.jpg detect_id: 8 speed: 19.0 count: 0 status: Crossing
19 | 160x640 1 crosswalks, Done. (0.296s)
20 | filename_00039.jpg detect_id: 9 speed: 40.0 count: 0 status: Crossing
21 | 160x640 1 crosswalks, Done. (0.285s)
22 |
23 | filename_00040.jpg count+1, conf: 0.96 count: 1
24 | filename_00040.jpg detect_id: 10 speed: 36.0 count: 1 status: Crossing
25 | 160x640 1 crosswalks, Done. (0.285s)
26 | filename_00041.jpg detect_id: 11 speed: 40.0 count: 1 status: Crossing
27 | 160x640 1 crosswalks, Done. (0.284s)
28 | filename_00042.jpg detect_id: 12 speed: 16.0 count: 1 status: Crossing
29 | 160x640 1 crosswalks, Done. (0.278s)
30 | filename_00043.jpg detect_id: 13 speed: 35.0 count: 1 status: Crossing
31 | 160x640 Done. (0.284s)
32 | filename_00044.jpg detect_id: 14 speed: None count: 1 status: Crossing
33 | 160x640 Done. (0.286s)
34 | filename_00045.jpg detect_id: 15 speed: None count: 1 status: Crossing
35 | 160x640 1 crosswalks, Done. (0.278s)
36 | filename_00046.jpg detect_id: 16 speed: -66.33333333333333 count: 1 status: No crosswalk
37 | 160x640 2 crosswalks, Done. (0.282s)
38 | filename_00047.jpg detect_id: 17 speed: 12.0 count: 1 status: Crossing
39 | 160x640 1 crosswalks, Done. (0.280s)
40 | filename_00048.jpg detect_id: 18 speed: 14.0 count: 1 status: Crossing
41 | 160x640 1 crosswalks, Done. (0.283s)
42 | filename_00049.jpg detect_id: 19 speed: 41.0 count: 1 status: Crossing
43 | 160x640 1 crosswalks, Done. (0.291s)
44 | filename_00050.jpg detect_id: 20 speed: 85.0 count: 1 status: Crossing
45 | 160x640 Done. (0.277s)
46 | filename_00051.jpg detect_id: 21 speed: None count: 1 status: Crossing
47 | 160x640 Done. (0.283s)
48 | filename_00052.jpg detect_id: 22 speed: None count: 1 status: Crossing
49 | 160x640 Done. (0.285s)
50 | filename_00053.jpg detect_id: 23 speed: None count: 1 status: Crossing
51 | 160x640 Done. (0.276s)
52 | filename_00054.jpg detect_id: 24 speed: None count: 1 status: Crossing
53 | 160x640 Done. (0.282s)
54 | filename_00055.jpg detect_id: 25 speed: None count: 1 status: No crosswalk
55 | 160x640 Done. (0.286s)
56 | filename_00056.jpg detect_id: 26 speed: None count: 1 status: No crosswalk
57 | 160x640 Done. (0.278s)
58 | filename_00057.jpg detect_id: 27 speed: None count: 1 status: No crosswalk
59 | 160x640 Done. (0.286s)
60 | filename_00058.jpg detect_id: 28 speed: None count: 1 status: No crosswalk
61 | 160x640 Done. (0.287s)
62 | filename_00059.jpg detect_id: 29 speed: None count: 1 status: No crosswalk
63 | 0.28381s 3.5234FPS
64 |
--------------------------------------------------------------------------------
/example/output/filename_00030.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00030.jpg
--------------------------------------------------------------------------------
/example/output/filename_00031.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00031.jpg
--------------------------------------------------------------------------------
/example/output/filename_00032.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00032.jpg
--------------------------------------------------------------------------------
/example/output/filename_00033.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00033.jpg
--------------------------------------------------------------------------------
/example/output/filename_00034.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00034.jpg
--------------------------------------------------------------------------------
/example/output/filename_00035.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00035.jpg
--------------------------------------------------------------------------------
/example/output/filename_00036.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00036.jpg
--------------------------------------------------------------------------------
/example/output/filename_00036.txt:
--------------------------------------------------------------------------------
1 | 1 0.8876 0.275391 0.675000 0.271094 0.061111
2 |
--------------------------------------------------------------------------------
/example/output/filename_00037.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00037.jpg
--------------------------------------------------------------------------------
/example/output/filename_00037.txt:
--------------------------------------------------------------------------------
1 | 1 0.9397 0.233203 0.803472 0.466406 0.204167
2 | 0 0.5480 0.316016 0.637500 0.289844 0.038889
3 |
--------------------------------------------------------------------------------
/example/output/filename_00038.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00038.jpg
--------------------------------------------------------------------------------
/example/output/filename_00038.txt:
--------------------------------------------------------------------------------
1 | 0 0.8900 0.309766 0.663889 0.399219 0.055556
2 |
--------------------------------------------------------------------------------
/example/output/filename_00039.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00039.jpg
--------------------------------------------------------------------------------
/example/output/filename_00039.txt:
--------------------------------------------------------------------------------
1 | 0 0.9498 0.295312 0.719444 0.585938 0.108333
2 |
--------------------------------------------------------------------------------
/example/output/filename_00040.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00040.jpg
--------------------------------------------------------------------------------
/example/output/filename_00040.txt:
--------------------------------------------------------------------------------
1 | 0 0.9563 0.336719 0.770139 0.673437 0.173611
2 |
--------------------------------------------------------------------------------
/example/output/filename_00041.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00041.jpg
--------------------------------------------------------------------------------
/example/output/filename_00041.txt:
--------------------------------------------------------------------------------
1 | 0 0.9516 0.410156 0.825000 0.820312 0.233333
2 |
--------------------------------------------------------------------------------
/example/output/filename_00042.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00042.jpg
--------------------------------------------------------------------------------
/example/output/filename_00042.txt:
--------------------------------------------------------------------------------
1 | 0 0.9410 0.412891 0.847222 0.825781 0.191667
2 |
--------------------------------------------------------------------------------
/example/output/filename_00043.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00043.jpg
--------------------------------------------------------------------------------
/example/output/filename_00043.txt:
--------------------------------------------------------------------------------
1 | 0 0.8974 0.420312 0.896528 0.806250 0.112500
2 |
--------------------------------------------------------------------------------
/example/output/filename_00044.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00044.jpg
--------------------------------------------------------------------------------
/example/output/filename_00045.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00045.jpg
--------------------------------------------------------------------------------
/example/output/filename_00046.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00046.jpg
--------------------------------------------------------------------------------
/example/output/filename_00046.txt:
--------------------------------------------------------------------------------
1 | 0 0.5608 0.361328 0.619444 0.247656 0.033333
2 |
--------------------------------------------------------------------------------
/example/output/filename_00047.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00047.jpg
--------------------------------------------------------------------------------
/example/output/filename_00047.txt:
--------------------------------------------------------------------------------
1 | 0 0.7835 0.344531 0.636111 0.329688 0.038889
2 | 0 0.5951 0.113672 0.765278 0.227344 0.202778
3 |
--------------------------------------------------------------------------------
/example/output/filename_00048.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00048.jpg
--------------------------------------------------------------------------------
/example/output/filename_00048.txt:
--------------------------------------------------------------------------------
1 | 0 0.6838 0.316797 0.655556 0.432031 0.063889
2 |
--------------------------------------------------------------------------------
/example/output/filename_00049.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00049.jpg
--------------------------------------------------------------------------------
/example/output/filename_00049.txt:
--------------------------------------------------------------------------------
1 | 0 0.9292 0.311328 0.713194 0.614844 0.106944
2 |
--------------------------------------------------------------------------------
/example/output/filename_00050.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00050.jpg
--------------------------------------------------------------------------------
/example/output/filename_00050.txt:
--------------------------------------------------------------------------------
1 | 0 0.9086 0.427734 0.830556 0.849219 0.222222
2 |
--------------------------------------------------------------------------------
/example/output/filename_00051.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00051.jpg
--------------------------------------------------------------------------------
/example/output/filename_00052.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00052.jpg
--------------------------------------------------------------------------------
/example/output/filename_00053.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00053.jpg
--------------------------------------------------------------------------------
/example/output/filename_00054.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00054.jpg
--------------------------------------------------------------------------------
/example/output/filename_00055.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00055.jpg
--------------------------------------------------------------------------------
/example/output/filename_00056.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00056.jpg
--------------------------------------------------------------------------------
/example/output/filename_00057.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00057.jpg
--------------------------------------------------------------------------------
/example/output/filename_00058.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00058.jpg
--------------------------------------------------------------------------------
/example/output/filename_00059.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/example/output/filename_00059.jpg
--------------------------------------------------------------------------------
/fog_augment.py:
--------------------------------------------------------------------------------
1 | """
2 | fogging train and test datasets using synthetic fog algorithm
3 | """
4 |
5 | import os, sys
6 | import shutil
7 | from pathlib import Path
8 | import numpy as np
9 | from tqdm import tqdm
10 | import cv2
11 | import random
12 | from copy import deepcopy
13 |
14 | from scripts.synthetic_fog import SyntheticFog
15 |
16 |
17 | class AugmentCrosswalkDataset(object):
18 | def __init__(self, source_path):
19 | self.sp = source_path # source path
20 | p = Path(self.sp)
21 | self.tp = f'{p.parent}/fogged_{p.stem}' # target path
22 |
23 | self.sf = SyntheticFog() # synthetic fog object
24 |
25 | def augment(self, show=False):
26 | """augment train and test set in YOLOv5 format"""
27 | # 逐张进行增强
28 | sp = self.sp
29 | tp = self.tp
30 | print(f'fogged data will be saved to: {tp}')
31 | if os.path.exists(self.tp):
32 |
33 | shutil.rmtree(self.tp)
34 | os.makedirs(f'{self.tp}/images/train')
35 | os.makedirs(f'{self.tp}/images/test')
36 | os.makedirs(f'{self.tp}/labels/train')
37 | os.makedirs(f'{self.tp}/labels/test')
38 |
39 | for trte in ['train', 'test']:
40 | pi = f'{sp}/images/{trte}' # path of images
41 | pl = f'{sp}/labels/{trte}'
42 | ti = f'{tp}/images/{trte}'
43 | tl = f'{tp}/labels/{trte}'
44 |
45 | imgs = [f'{x}' for x in os.listdir(pi) if x.endswith('.jpg')]
46 | print(f'transform {trte} images, total: {len(imgs)}, transformed total: {2*len(img)}.')
47 | bar = tqdm(imgs)
48 | for i, img_name in enumerate(bar):
49 | img_path = f'{pi}/{img_name}'
50 | stem = Path(img_path).stem
51 | assert os.path.exists(img_path), f'img does not exists {img_path}'
52 |
53 | # 先拷贝原始图像和标注
54 | shutil.copy(img_path, f'{ti}/{img_name}')
55 | shutil.copy(f'{pl}/{stem}.txt', f'{tl}/{stem}.txt')
56 |
57 | # fogging
58 | img = cv2.imread(img_path)
59 | h, w, c = img.shape
60 | # random brightness and thickness
61 | br = np.clip(0.2 * np.random.randn() + 0.5, 0.1, 0.9) # 0.1~0.9
62 | th = np.clip(0.01 * np.random.randn() + 0.05, 0.01, 0.09)
63 | normed_img = img.copy()/255.0
64 | fogged_img = self.sf.fogging_img(
65 | normed_img, brightness=br, thickness=th, high_efficiency=True)
66 | fogged_img = np.array(fogged_img*255, dtype=np.uint8)
67 |
68 | # save fogged images and labels
69 | cv2.imwrite(f'{ti}/fogged_{img_name}', fogged_img)
70 | shutil.copy(f'{pl}/{stem}.txt', f'{tl}/fogged_{stem}.txt')
71 |
72 | if show:
73 | print(f'img_name: {Path(img_path).name} img: {img.shape} br: {br} th: {th} max: {np.max(fogged_img)}')
74 | self.show(img, name='src_img', wait=False)
75 | self.show(fogged_img, name='fogged_img', wait=False)
76 | if cv2.waitKey(0) == ord('q'):
77 | break
78 |
79 | bar.set_description(f'Img and fogged img saved, {stem}.')
80 |
81 | def show(self, img, name='xx', wait=True):
82 | h, w, c = img.shape
83 | scale = 0.5
84 | show_img = cv2.resize(img, (int(w*scale), int(h*scale)))
85 | cv2.imshow(name, show_img)
86 | if wait:
87 | cv2.waitKey(0)
88 |
89 | def augment_testset(self, dir):
90 | """augment only test set"""
91 | self.sf.fogging_dir(sp=dir, tp=None, random_params=True, save_src_img=True)
92 |
93 |
94 | if __name__ == '__main__':
95 | source_path = '/home/zzd/datasets/crosswalk/train_data_v5_format'
96 | acd = AugmentCrosswalkDataset(source_path)
97 | acd.augment(show=False)
98 | # test_imgs_path = '/home/zzd/datasets/crosswalk/testsets_1770/Images'
99 | # acd.augment_testset(test_imgs_path)
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
--------------------------------------------------------------------------------
/for_paper/CDNet_plot.py:
--------------------------------------------------------------------------------
1 | """
2 | 绘制result的图
3 | """
4 |
5 | import os
6 | import matplotlib.pyplot as plt
7 | import yaml
8 | import numpy as np
9 | import matplotlib
10 |
11 | matplotlib.rcParams['xtick.direction'] = 'in' # 刻度线向内
12 | matplotlib.rcParams['ytick.direction'] = 'in'
13 | c = [
14 | 'lightcoral', 'limegreen', 'royalblue', 'orange', 'mediumorchid',
15 | 'orangered', 'lawngreen', 'deepskyblue', 'gold', 'fuchsia',
16 | 'sandybrown', 'cyan']
17 | c = [
18 | 'lightcoral', 'limegreen', 'royalblue', 'orange', 'mediumorchid',
19 | 'deepskyblue', 'gold', 'fuchsia',
20 | 'sandybrown', 'cyan']
21 | c = [
22 | 'black', 'lightcoral', 'limegreen', 'royalblue', 'orange', 'mediumorchid',
23 | 'deepskyblue', '#EE3B3B', 'gold', 'fuchsia',
24 | 'sandybrown', 'cyan']
25 | c = [
26 | 'black', 'lightcoral', 'limegreen', 'royalblue', 'orange', 'mediumorchid',
27 | 'deepskyblue', '#009966', '#EE3B3B', 'gold', 'fuchsia',
28 | 'sandybrown', 'cyan']
29 |
30 |
31 | class CDNetPlot(object):
32 | def __init__(self):
33 | self.data = self.load_data('data/results_3.yaml')
34 |
35 | def load_data(self, path):
36 | with open(path, 'r') as f:
37 | data = yaml.load(f, Loader=yaml.FullLoader)
38 | return data
39 |
40 | def plot(self):
41 | d = self.data
42 | for i, k in enumerate(d.keys()):
43 | v = [float(x) for x in d[k].split()]
44 | print(k, v, type(v))
45 | x = np.array([v[0], v[2]]).astype(np.float32)
46 | y = np.array([v[1], v[3]]).astype(np.float32)
47 | # 划线
48 | lb = '$\mathbf{'+k+'}$'
49 | plt.plot(
50 | x, y, ls='-', label=lb, c=c[i], linewidth=3,
51 | marker='', markersize=10)
52 | # 画点288
53 | plt.plot(v[0], v[1], marker='^', c=c[i], markersize=15)
54 | plt.plot(v[2], v[3], marker='s', c=c[i], markersize=15)
55 | # 添加额外的标签
56 | if False:
57 | if i == 0:
58 | plt.text(v[0]-12, v[1]-1.5, '288', size=16)
59 | plt.text(v[2] - 12, v[3] - 1.5, '640', size=16)
60 |
61 | fontdict = {'size': 18, 'weight': 'bold'}
62 | plt.legend(loc=2, bbox_to_anchor=(1.0, 1.0), borderaxespad=0.)
63 | plt.grid()
64 | plt.xlabel('Detection Speed (FPS)', fontdict=fontdict)
65 | plt.ylabel('F1 score (%)', fontdict=fontdict)
66 | plt.xticks(range(0, 100, 5), size=12, weight='bold')
67 | plt.xlim(1, 42)
68 | plt.yticks(range(60, 101, 5), size=12, weight='bold')
69 | plt.ylim(78, 98)
70 | # plt.xticks(range(0, 300, 50))
71 | plt.show()
72 | # plt.savefig('results.png', dpi=2000, bbox_inches='tight')
73 |
74 |
75 | if __name__ == '__main__':
76 | cdnetp = CDNetPlot()
77 | cdnetp.plot()
78 |
--------------------------------------------------------------------------------
/for_paper/data/results.yaml:
--------------------------------------------------------------------------------
1 | Base (YOLOv5m): 189 81.34 634 90.34
2 | Base+NST: 189 89.03 634 91.18
3 | Base+NST+SE: 205 86.41 650 92.10
4 | Base+NST+ROI: 74 81.27 273 87.17
5 | Base+NST+SE+ROI: 86 79.33 284 86.25
6 | Base+NST+SE+SSVM: 205 94.86 650 95.47
7 | Base+NST+ROI+SSVM: 74 93.70 273 95.31
8 | Base+NST+SE+ROI+SSVM (CDNet): 86 94.72 284 95.47
--------------------------------------------------------------------------------
/for_paper/entropy_analyse.py:
--------------------------------------------------------------------------------
1 | """
2 | 分析使用NST前后的数据集,根据信息论,从信息熵出发分析性能。
3 | """
4 | import os
5 | import numpy as np
6 |
7 |
8 | class EntropyAnalyse(object):
9 | def __init__(self):
10 | self.lb_2cls = "/home/zzd/datasets/crosswalk/train_data_v5_format/labels/train"
11 | self.lb_1cls = "/home/zzd/datasets/crosswalk/train_data_v5_format_1cls/labels/train"
12 |
13 | self.out_1cls_288 = "/home/zzd/datasets/crosswalk/testsets_1770/dense_testsize_full_1cls_640_288_output"
14 | self.out_1cls_640 = "/home/zzd/datasets/crosswalk/testsets_1770/dense_testsize_full_1cls_640_640_output"
15 | self.lb_2cls_te = "/home/zzd/datasets/crosswalk/train_data_v5_format/labels/test"
16 |
17 | def run(self):
18 |
19 | e = self.get_error_rate(out_path=self.out_1cls_640)
20 | # print(e)
21 | # exit()
22 | # 第一步,读取概率
23 | P2cls = self.get_P_from_label(path=self.lb_2cls)
24 | H2 = self.cal_information_entropy(P2cls)
25 | print(H2)
26 | exit()
27 | P1cls = self.get_P_from_label(path=self.lb_1cls)
28 |
29 |
30 | # P = np.array([0.1, 0.9, 0])
31 | H1 = self.cal_information_entropy(P1cls) # 熵
32 |
33 | print(H1, H2)
34 |
35 | # 统计在1cls中误检为crosswalk的数目,分288和640
36 |
37 |
38 |
39 | def get_error_rate(self, out_path):
40 | """
41 | 根据2cls的测试集的标注文件和1cls时的检测结果,统计在所有标注的guide_arrows中,被误检为crosswalk的错误数
42 | :return:
43 | """
44 | lb2te = self.lb_2cls_te
45 | txts = [x for x in os.listdir(lb2te) if x.endswith('.txt')]
46 | error = np.zeros(3) # 总标注数目,标注了1个同时检测为crosswalk,标注2个同时检测出2个crosswalk
47 | for i, txt in enumerate(txts):
48 | # print(txt)
49 | classes = self.read_txt(f'{lb2te}/{txt}')
50 | if '1' in classes:
51 | classes_ga = [x for x in classes if x == '1'] # 只有guide arrows
52 | both = len(classes) != len(classes_ga) # both为真时,表示该标注文件同时标注了crosswalk和guidearrows
53 | assert len(classes_ga) == 1
54 | classes2 = self.read_txt(f'{out_path}/{txt}')
55 | classes2 = [] if classes2 is None else classes2
56 | # print(classes, classes2, both)
57 | error[0] += 1
58 |
59 | if not both and '0' in classes2:
60 | error[1] += 1
61 | if both and len(classes2) >= 2:
62 | print(f'{txt} 标注了2个,且有GA,检测出大于2个 {classes} {classes2}')
63 | error[1] += 1
64 | print(f'[{i+1:>2}/{len(txts)}] {error} {out_path}/{txt}')
65 |
66 | return 0
67 |
68 |
69 | def get_P_from_label(self, path):
70 | txts = [x for x in os.listdir(path) if x.endswith('.txt')]
71 | assert len(txts) != 0
72 | """
73 | 根据yolo的目标检测算法,每张图的样本总数应该是:7x7x3
74 | """
75 | P = np.zeros(3) # crosswalk guide arrows total
76 | for i, txt in enumerate(txts):
77 | with open(f'{path}/{txt}', 'r') as f:
78 | data = f.readlines()
79 | classes = [x.split()[0] for x in data] # 标注文件中的类别
80 | assert len(classes) != 0
81 | for j, cls in enumerate(classes):
82 | if cls == '0':
83 | P[0] += 1
84 | elif cls == '1':
85 | P[1] += 1
86 | if len(classes) >=2:
87 | print(f'同时存在,{classes}')
88 | else:
89 | raise NameError(f'标注文件读取到的分类{cls}不正确,文件:{path}/{txt}')
90 | P[2] += (7*7*3 - len(classes))
91 |
92 | print(f'\r[{i+1:>2}/{len(txts)}] {P} ', end='')
93 |
94 | P = P/np.sum(P)
95 | print(P)
96 | return P
97 |
98 | def cal_information_entropy(self, P):
99 | """
100 | 输入的P是一个ndarray,长度是可能的符号数目,每个值是对应的概率Pxi
101 | H(x) = - sum(Pxi * log(Pxi))
102 | :param P:
103 | :return:
104 | """
105 | P = P[P != 0] # 筛选掉为0的项
106 | return -np.sum(np.multiply(P, np.log2(P)))
107 |
108 | def read_txt(self, txt):
109 | if os.path.exists(txt):
110 | with open(txt, 'r') as f:
111 | data = f.readlines()
112 | return [x.split()[0] for x in data]
113 | else:
114 | return None
115 |
116 |
117 | if __name__ == '__main__':
118 | ea = EntropyAnalyse()
119 | ea.run()
120 |
--------------------------------------------------------------------------------
/for_paper/overall_data:
--------------------------------------------------------------------------------
1 | ts base btime base+NST base+NST+SE base+NST+ROI base+NST+SE+ROI base+NST+SE+ROI+SSVM
2 | 128 34.53 75 48.84 22.56 46 34.19 4.38
3 | 288 81.34 189 89.03 86.41 74 81.27 79.33
4 | 448 87.98 314 90.77 90.64 169 85.30 82.41
5 | 640 90.34 634 91.18 92.10 273 87.17 86.25
6 | 832 87.98 1134 90.66 90.17 403 88.85 87.69
7 |
8 |
--------------------------------------------------------------------------------
/models/common.py:
--------------------------------------------------------------------------------
1 | # This file contains modules common to various models
2 | import math
3 |
4 | import torch
5 | import torch.nn as nn
6 |
7 |
8 | def autopad(k, p=None): # kernel, padding
9 | # Pad to 'same'
10 | if p is None:
11 | p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
12 | return p
13 |
14 |
15 | def DWConv(c1, c2, k=1, s=1, act=True):
16 | # Depthwise convolution
17 | return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
18 |
19 |
20 | class Conv(nn.Module):
21 | # Standard convolution
22 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
23 | super(Conv, self).__init__()
24 | self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
25 | self.bn = nn.BatchNorm2d(c2)
26 | self.act = nn.LeakyReLU(0.1, inplace=True) if act else nn.Identity()
27 |
28 | def forward(self, x):
29 | return self.act(self.bn(self.conv(x)))
30 |
31 | def fuseforward(self, x):
32 | return self.act(self.conv(x))
33 |
34 |
35 | class Bottleneck(nn.Module):
36 | # Standard bottleneck
37 | def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
38 | super(Bottleneck, self).__init__()
39 | c_ = int(c2 * e) # hidden channels
40 | self.cv1 = Conv(c1, c_, 1, 1)
41 | self.cv2 = Conv(c_, c2, 3, 1, g=g)
42 | self.add = shortcut and c1 == c2
43 |
44 | def forward(self, x):
45 | return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
46 |
47 |
48 | class BottleneckSE(nn.Module):
49 | """
50 | 添加了SENet的bottleneck
51 | """
52 |
53 | def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
54 | super(Bottleneck, self).__init__()
55 | c_ = int(c2 * e) # hidden channels
56 | self.cv1 = Conv(c1, c_, 1, 1)
57 | # self.cv2 = Conv(c_, c2, 3, 1, g=g)
58 |
59 | self.conv = nn.Conv2d(c_, c2, 3, 1, autopad(3, None), groups=g, bias=False)
60 | self.bn = nn.BatchNorm2d(c2)
61 |
62 | self.se = SELayer(c2, reduction=16)
63 | self.act = nn.LeakyReLU(0.1, inplace=True)
64 |
65 | self.add = shortcut and c1 == c2
66 |
67 | def forward(self, x):
68 | residual = x
69 | x = self.cv1(x)
70 | x = self.conv(x)
71 | x = self.bn(x)
72 | if self.add:
73 | x = self.se(x)
74 | x = residual + x
75 | x = self.act(x)
76 |
77 | return x
78 |
79 | # return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
80 |
81 |
82 | class SELayer(nn.Module):
83 | def __init__(self, channel, reduction=16):
84 | super(SELayer, self).__init__()
85 | self.avg_pool = nn.AdaptiveAvgPool2d(1)
86 | self.fc = nn.Sequential(
87 | nn.Linear(channel, channel // reduction, bias=False),
88 | nn.ReLU(inplace=True),
89 | nn.Linear(channel // reduction, channel, bias=False),
90 | nn.Sigmoid()
91 | )
92 |
93 | def forward(self, x):
94 | b, c, _, _ = x.size()
95 | y = self.avg_pool(x).view(b, c)
96 | y = self.fc(y).view(b, c, 1, 1)
97 | return x * y.expand_as(x)
98 |
99 |
100 | class BottleneckCSP(nn.Module):
101 | # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
102 | def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
103 | super(BottleneckCSP, self).__init__()
104 | c_ = int(c2 * e) # hidden channels
105 | self.cv1 = Conv(c1, c_, 1, 1)
106 | self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
107 | self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
108 | self.cv4 = Conv(2 * c_, c2, 1, 1)
109 | self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
110 | self.act = nn.LeakyReLU(0.1, inplace=True)
111 | self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
112 |
113 | def forward(self, x):
114 | y1 = self.cv3(self.m(self.cv1(x)))
115 | y2 = self.cv2(x)
116 | return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
117 |
118 |
119 | class SPP(nn.Module):
120 | # Spatial pyramid pooling layer used in YOLOv3-SPP
121 | def __init__(self, c1, c2, k=(5, 9, 13)):
122 | super(SPP, self).__init__()
123 | c_ = c1 // 2 # hidden channels
124 | self.cv1 = Conv(c1, c_, 1, 1)
125 | self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
126 | self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
127 |
128 | def forward(self, x):
129 | x = self.cv1(x)
130 | return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
131 |
132 |
133 | class Focus(nn.Module):
134 | # Focus wh information into c-space
135 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
136 | super(Focus, self).__init__()
137 | self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
138 |
139 | def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
140 | return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
141 |
142 |
143 | class Concat(nn.Module):
144 | # Concatenate a list of tensors along dimension
145 | def __init__(self, dimension=1):
146 | super(Concat, self).__init__()
147 | self.d = dimension
148 |
149 | def forward(self, x):
150 | return torch.cat(x, self.d)
151 |
152 |
153 | class Flatten(nn.Module):
154 | # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions
155 | @staticmethod
156 | def forward(x):
157 | return x.view(x.size(0), -1)
158 |
159 |
160 | class Classify(nn.Module):
161 | # Classification head, i.e. x(b,c1,20,20) to x(b,c2)
162 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups
163 | super(Classify, self).__init__()
164 | self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1)
165 | self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) # to x(b,c2,1,1)
166 | self.flat = Flatten()
167 |
168 | def forward(self, x):
169 | z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list
170 | return self.flat(self.conv(z)) # flatten to x(b,c2)
171 |
--------------------------------------------------------------------------------
/models/commonSE.py:
--------------------------------------------------------------------------------
1 | # This file contains modules common to various models
2 | import math
3 |
4 | import torch
5 | import torch.nn as nn
6 |
7 |
8 | def autopad(k, p=None): # kernel, padding
9 | # Pad to 'same'
10 | if p is None:
11 | p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
12 | return p
13 |
14 |
15 | def DWConv(c1, c2, k=1, s=1, act=True):
16 | # Depthwise convolution
17 | return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act)
18 |
19 |
20 | class Conv(nn.Module):
21 | # Standard convolution
22 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
23 | super(Conv, self).__init__()
24 | self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)
25 | self.bn = nn.BatchNorm2d(c2)
26 | self.act = nn.LeakyReLU(0.1, inplace=True) if act else nn.Identity()
27 |
28 | def forward(self, x):
29 | return self.act(self.bn(self.conv(x)))
30 |
31 | def fuseforward(self, x):
32 | return self.act(self.conv(x))
33 |
34 |
35 | class Bottleneck_back(nn.Module):
36 | # Standard bottleneck
37 | def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
38 | super(Bottleneck, self).__init__()
39 | c_ = int(c2 * e) # hidden channels
40 | self.cv1 = Conv(c1, c_, 1, 1)
41 | self.cv2 = Conv(c_, c2, 3, 1, g=g)
42 | self.add = shortcut and c1 == c2
43 |
44 | def forward(self, x):
45 | return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
46 |
47 |
48 | class Bottleneck(nn.Module):
49 | """
50 | 添加了SENet的bottleneck
51 | """
52 |
53 | def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
54 | super(Bottleneck, self).__init__()
55 | c_ = int(c2 * e) # hidden channels
56 | self.cv1 = Conv(c1, c_, 1, 1)
57 | # self.cv2 = Conv(c_, c2, 3, 1, g=g)
58 |
59 | self.conv = nn.Conv2d(c_, c2, 3, 1, autopad(3, None), groups=g, bias=False)
60 | self.bn = nn.BatchNorm2d(c2)
61 |
62 | self.se = SELayer(c2, reduction=16)
63 | self.act = nn.LeakyReLU(0.1, inplace=True)
64 |
65 | self.add = shortcut and c1 == c2
66 |
67 | def forward(self, x):
68 | residual = x
69 | x = self.cv1(x)
70 | x = self.conv(x)
71 | x = self.bn(x)
72 | if self.add:
73 | x = self.se(x)
74 | x = residual + x
75 | x = self.act(x)
76 |
77 | return x
78 | # return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
79 |
80 |
81 | class SELayer(nn.Module):
82 | def __init__(self, channel, reduction=16):
83 | super(SELayer, self).__init__()
84 | self.avg_pool = nn.AdaptiveAvgPool2d(1)
85 | self.fc = nn.Sequential(
86 | nn.Linear(channel, channel // reduction, bias=False),
87 | nn.ReLU(inplace=True),
88 | nn.Linear(channel // reduction, channel, bias=False),
89 | nn.Sigmoid()
90 | )
91 |
92 | def forward(self, x):
93 | b, c, _, _ = x.size()
94 | y = self.avg_pool(x).view(b, c)
95 | y = self.fc(y).view(b, c, 1, 1)
96 | return x * y.expand_as(x)
97 |
98 |
99 | class BottleneckCSP(nn.Module):
100 | # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
101 | def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
102 | super(BottleneckCSP, self).__init__()
103 | c_ = int(c2 * e) # hidden channels
104 | self.cv1 = Conv(c1, c_, 1, 1)
105 | self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
106 | self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
107 | self.cv4 = Conv(2 * c_, c2, 1, 1)
108 | self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
109 | self.act = nn.LeakyReLU(0.1, inplace=True)
110 | self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
111 |
112 | def forward(self, x):
113 | y1 = self.cv3(self.m(self.cv1(x)))
114 | y2 = self.cv2(x)
115 | return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
116 |
117 |
118 | class SPP(nn.Module):
119 | # Spatial pyramid pooling layer used in YOLOv3-SPP
120 | def __init__(self, c1, c2, k=(5, 9, 13)):
121 | super(SPP, self).__init__()
122 | c_ = c1 // 2 # hidden channels
123 | self.cv1 = Conv(c1, c_, 1, 1)
124 | self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
125 | self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
126 |
127 | def forward(self, x):
128 | x = self.cv1(x)
129 | return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
130 |
131 |
132 | class Focus(nn.Module):
133 | # Focus wh information into c-space
134 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
135 | super(Focus, self).__init__()
136 | self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
137 |
138 | def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
139 | return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
140 |
141 |
142 | class Concat(nn.Module):
143 | # Concatenate a list of tensors along dimension
144 | def __init__(self, dimension=1):
145 | super(Concat, self).__init__()
146 | self.d = dimension
147 |
148 | def forward(self, x):
149 | return torch.cat(x, self.d)
150 |
151 |
152 | class Flatten(nn.Module):
153 | # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions
154 | @staticmethod
155 | def forward(x):
156 | return x.view(x.size(0), -1)
157 |
158 |
159 | class Classify(nn.Module):
160 | # Classification head, i.e. x(b,c1,20,20) to x(b,c2)
161 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups
162 | super(Classify, self).__init__()
163 | self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1)
164 | self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) # to x(b,c2,1,1)
165 | self.flat = Flatten()
166 |
167 | def forward(self, x):
168 | z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list
169 | return self.flat(self.conv(z)) # flatten to x(b,c2)
170 |
--------------------------------------------------------------------------------
/models/experimental.py:
--------------------------------------------------------------------------------
1 | # This file contains experimental modules
2 |
3 | import numpy as np
4 | import torch
5 | import torch.nn as nn
6 |
7 | from models.common import Conv, DWConv
8 | from utils.google_utils import attempt_download
9 |
10 |
11 | class CrossConv(nn.Module):
12 | # Cross Convolution Downsample
13 | def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False):
14 | # ch_in, ch_out, kernel, stride, groups, expansion, shortcut
15 | super(CrossConv, self).__init__()
16 | c_ = int(c2 * e) # hidden channels
17 | self.cv1 = Conv(c1, c_, (1, k), (1, s))
18 | self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g)
19 | self.add = shortcut and c1 == c2
20 |
21 | def forward(self, x):
22 | return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
23 |
24 |
25 | class C3(nn.Module):
26 | # Cross Convolution CSP
27 | def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
28 | super(C3, self).__init__()
29 | c_ = int(c2 * e) # hidden channels
30 | self.cv1 = Conv(c1, c_, 1, 1)
31 | self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
32 | self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
33 | self.cv4 = Conv(2 * c_, c2, 1, 1)
34 | self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
35 | self.act = nn.LeakyReLU(0.1, inplace=True)
36 | self.m = nn.Sequential(*[CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)])
37 |
38 | def forward(self, x):
39 | y1 = self.cv3(self.m(self.cv1(x)))
40 | y2 = self.cv2(x)
41 | return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
42 |
43 |
44 | class Sum(nn.Module):
45 | # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
46 | def __init__(self, n, weight=False): # n: number of inputs
47 | super(Sum, self).__init__()
48 | self.weight = weight # apply weights boolean
49 | self.iter = range(n - 1) # iter object
50 | if weight:
51 | self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) # layer weights
52 |
53 | def forward(self, x):
54 | y = x[0] # no weight
55 | if self.weight:
56 | w = torch.sigmoid(self.w) * 2
57 | for i in self.iter:
58 | y = y + x[i + 1] * w[i]
59 | else:
60 | for i in self.iter:
61 | y = y + x[i + 1]
62 | return y
63 |
64 |
65 | class GhostConv(nn.Module):
66 | # Ghost Convolution https://github.com/huawei-noah/ghostnet
67 | def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
68 | super(GhostConv, self).__init__()
69 | c_ = c2 // 2 # hidden channels
70 | self.cv1 = Conv(c1, c_, k, s, g, act)
71 | self.cv2 = Conv(c_, c_, 5, 1, c_, act)
72 |
73 | def forward(self, x):
74 | y = self.cv1(x)
75 | return torch.cat([y, self.cv2(y)], 1)
76 |
77 |
78 | class GhostBottleneck(nn.Module):
79 | # Ghost Bottleneck https://github.com/huawei-noah/ghostnet
80 | def __init__(self, c1, c2, k, s):
81 | super(GhostBottleneck, self).__init__()
82 | c_ = c2 // 2
83 | self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw
84 | DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw
85 | GhostConv(c_, c2, 1, 1, act=False)) # pw-linear
86 | self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False),
87 | Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity()
88 |
89 | def forward(self, x):
90 | return self.conv(x) + self.shortcut(x)
91 |
92 |
93 | class MixConv2d(nn.Module):
94 | # Mixed Depthwise Conv https://arxiv.org/abs/1907.09595
95 | def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True):
96 | super(MixConv2d, self).__init__()
97 | groups = len(k)
98 | if equal_ch: # equal c_ per group
99 | i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices
100 | c_ = [(i == g).sum() for g in range(groups)] # intermediate channels
101 | else: # equal weight.numel() per group
102 | b = [c2] + [0] * groups
103 | a = np.eye(groups + 1, groups, k=-1)
104 | a -= np.roll(a, 1, axis=1)
105 | a *= np.array(k) ** 2
106 | a[0] = 1
107 | c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
108 |
109 | self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)])
110 | self.bn = nn.BatchNorm2d(c2)
111 | self.act = nn.LeakyReLU(0.1, inplace=True)
112 |
113 | def forward(self, x):
114 | return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
115 |
116 |
117 | class Ensemble(nn.ModuleList):
118 | # Ensemble of models
119 | def __init__(self):
120 | super(Ensemble, self).__init__()
121 |
122 | def forward(self, x, augment=False):
123 | y = []
124 | for module in self:
125 | y.append(module(x, augment)[0])
126 | # y = torch.stack(y).max(0)[0] # max ensemble
127 | # y = torch.cat(y, 1) # nms ensemble
128 | y = torch.stack(y).mean(0) # mean ensemble
129 | return y, None # inference, train output
130 |
131 |
132 | def attempt_load(weights, map_location=None):
133 | # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
134 | model = Ensemble()
135 | for w in weights if isinstance(weights, list) else [weights]:
136 | attempt_download(w)
137 | model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval()) # load FP32 model
138 |
139 | if len(model) == 1:
140 | return model[-1] # return model
141 | else:
142 | print('Ensemble created with %s\n' % weights)
143 | for k in ['names', 'stride']:
144 | setattr(model, k, getattr(model[-1], k))
145 | return model # return ensemble
146 |
--------------------------------------------------------------------------------
/models/yolov5m.yaml:
--------------------------------------------------------------------------------
1 | # parameters
2 | nc: 2 # number of classes
3 | depth_multiple: 0.67 # model depth multiple
4 | width_multiple: 0.75 # layer channel multiple
5 |
6 | # anchors
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, BottleneckCSP, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 9, BottleneckCSP, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, BottleneckCSP, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 1, SPP, [1024, [5, 9, 13]]],
24 | [-1, 3, BottleneckCSP, [1024, False]], # 9
25 | ]
26 |
27 | # YOLOv5 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, BottleneckCSP, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/_base_/datasets/zzd_coco_detection.py:
--------------------------------------------------------------------------------
1 | # dataset settings
2 | dataset_type = 'CocoDataset'
3 | data_root = '/home/zzd/datasets/mscoco/'
4 | img_norm_cfg = dict(
5 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
6 | train_pipeline = [
7 | dict(type='LoadImageFromFile'),
8 | dict(type='LoadAnnotations', with_bbox=True),
9 | dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
10 | dict(type='RandomFlip', flip_ratio=0.5),
11 | dict(type='Normalize', **img_norm_cfg),
12 | dict(type='Pad', size_divisor=32),
13 | dict(type='DefaultFormatBundle'),
14 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
15 | ]
16 | test_pipeline = [
17 | dict(type='LoadImageFromFile'),
18 | dict(
19 | type='MultiScaleFlipAug',
20 | img_scale=(1333, 800),
21 | flip=False,
22 | transforms=[
23 | dict(type='Resize', keep_ratio=True),
24 | dict(type='RandomFlip'),
25 | dict(type='Normalize', **img_norm_cfg),
26 | dict(type='Pad', size_divisor=32),
27 | dict(type='ImageToTensor', keys=['img']),
28 | dict(type='Collect', keys=['img']),
29 | ])
30 | ]
31 | data = dict(
32 | samples_per_gpu=2,
33 | workers_per_gpu=2,
34 | train=dict(
35 | type=dataset_type,
36 | ann_file=data_root + 'annotations/instances_train2017.json',
37 | img_prefix=data_root + 'train2017/',
38 | pipeline=train_pipeline),
39 | val=dict(
40 | type=dataset_type,
41 | ann_file=data_root + 'annotations/instances_val2017.json',
42 | img_prefix=data_root + 'val2017/',
43 | pipeline=test_pipeline),
44 | test=dict(
45 | type=dataset_type,
46 | ann_file=data_root + 'annotations/instances_val2017.json',
47 | img_prefix=data_root + 'val2017/',
48 | pipeline=test_pipeline))
49 | evaluation = dict(interval=1, metric='bbox')
50 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/_base_/datasets/zzd_coco_instance.py:
--------------------------------------------------------------------------------
1 | # dataset settings
2 | dataset_type = 'CocoDataset'
3 | data_root = '/home/zzd/datasets/mscoco/'
4 | img_norm_cfg = dict(
5 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
6 | train_pipeline = [
7 | dict(type='LoadImageFromFile'),
8 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
9 | dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
10 | dict(type='RandomFlip', flip_ratio=0.5),
11 | dict(type='Normalize', **img_norm_cfg),
12 | dict(type='Pad', size_divisor=32),
13 | dict(type='DefaultFormatBundle'),
14 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']),
15 | ]
16 | test_pipeline = [
17 | dict(type='LoadImageFromFile'),
18 | dict(
19 | type='MultiScaleFlipAug',
20 | img_scale=(1333, 800),
21 | flip=False,
22 | transforms=[
23 | dict(type='Resize', keep_ratio=True),
24 | dict(type='RandomFlip'),
25 | dict(type='Normalize', **img_norm_cfg),
26 | dict(type='Pad', size_divisor=32),
27 | dict(type='ImageToTensor', keys=['img']),
28 | dict(type='Collect', keys=['img']),
29 | ])
30 | ]
31 | data = dict(
32 | samples_per_gpu=2,
33 | workers_per_gpu=2,
34 | train=dict(
35 | type=dataset_type,
36 | ann_file=data_root + 'annotations/instances_train2017.json',
37 | img_prefix=data_root + 'train2017/',
38 | pipeline=train_pipeline),
39 | val=dict(
40 | type=dataset_type,
41 | ann_file=data_root + 'annotations/instances_val2017.json',
42 | img_prefix=data_root + 'val2017/',
43 | pipeline=test_pipeline),
44 | test=dict(
45 | type=dataset_type,
46 | ann_file=data_root + 'annotations/instances_val2017.json',
47 | img_prefix=data_root + 'val2017/',
48 | pipeline=test_pipeline))
49 | evaluation = dict(metric=['bbox', 'segm'])
50 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/_base_/datasets/zzd_crosswalk_detection.py:
--------------------------------------------------------------------------------
1 | # dataset settings
2 | dataset_type = 'CocoDataset'
3 | classes = ('crosswalk', 'guide_arrows')
4 | data_root = '/home/zzd/datasets/crosswalk/fogged_train_data_coco_format/'
5 | img_norm_cfg = dict(
6 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
7 | train_pipeline = [
8 | dict(type='LoadImageFromFile'),
9 | dict(type='LoadAnnotations', with_bbox=True),
10 | dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
11 | dict(type='RandomFlip', flip_ratio=0.5),
12 | dict(type='Normalize', **img_norm_cfg),
13 | dict(type='Pad', size_divisor=32),
14 | dict(type='DefaultFormatBundle'),
15 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
16 | ]
17 | test_pipeline = [
18 | dict(type='LoadImageFromFile'),
19 | dict(
20 | type='MultiScaleFlipAug',
21 | img_scale=(1333, 800),
22 | flip=False,
23 | transforms=[
24 | dict(type='Resize', keep_ratio=True),
25 | dict(type='RandomFlip'),
26 | dict(type='Normalize', **img_norm_cfg),
27 | dict(type='Pad', size_divisor=32),
28 | dict(type='ImageToTensor', keys=['img']),
29 | dict(type='Collect', keys=['img']),
30 | ])
31 | ]
32 | data = dict(
33 | samples_per_gpu=2,
34 | workers_per_gpu=2,
35 | train=dict(
36 | type=dataset_type,
37 | ann_file=data_root + 'annotations/instances_train2017.json',
38 | img_prefix=data_root + 'train2017/',
39 | classes=classes,
40 | pipeline=train_pipeline),
41 | val=dict(
42 | type=dataset_type,
43 | ann_file=data_root + 'annotations/instances_val2017.json',
44 | img_prefix=data_root + 'val2017/',
45 | classes=classes,
46 | pipeline=test_pipeline),
47 | test=dict(
48 | type=dataset_type,
49 | ann_file=data_root + 'annotations/instances_val2017.json',
50 | img_prefix=data_root + 'val2017/',
51 | classes=classes,
52 | pipeline=test_pipeline))
53 | evaluation = dict(interval=1, metric='bbox')
54 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/_base_/datasets/zzd_crosswalk_instance.py:
--------------------------------------------------------------------------------
1 | # dataset settings
2 | dataset_type = 'CocoDataset'
3 | classes = ('crosswalk', 'guide_arrows')
4 | data_root = '/home/zzd/datasets/crosswalk/fogged_train_data_coco_format/'
5 | img_norm_cfg = dict(
6 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
7 | train_pipeline = [
8 | dict(type='LoadImageFromFile'),
9 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
10 | dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
11 | dict(type='RandomFlip', flip_ratio=0.5),
12 | dict(type='Normalize', **img_norm_cfg),
13 | dict(type='Pad', size_divisor=32),
14 | dict(type='DefaultFormatBundle'),
15 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']),
16 | ]
17 | test_pipeline = [
18 | dict(type='LoadImageFromFile'),
19 | dict(
20 | type='MultiScaleFlipAug',
21 | img_scale=(1333, 800),
22 | flip=False,
23 | transforms=[
24 | dict(type='Resize', keep_ratio=True),
25 | dict(type='RandomFlip'),
26 | dict(type='Normalize', **img_norm_cfg),
27 | dict(type='Pad', size_divisor=32),
28 | dict(type='ImageToTensor', keys=['img']),
29 | dict(type='Collect', keys=['img']),
30 | ])
31 | ]
32 | data = dict(
33 | samples_per_gpu=2,
34 | workers_per_gpu=2,
35 | train=dict(
36 | type=dataset_type,
37 | ann_file=data_root + 'annotations/instances_train2017.json',
38 | img_prefix=data_root + 'train2017/',
39 | classes=classes,
40 | pipeline=train_pipeline),
41 | val=dict(
42 | type=dataset_type,
43 | ann_file=data_root + 'annotations/instances_val2017.json',
44 | img_prefix=data_root + 'val2017/',
45 | classes=classes,
46 | pipeline=test_pipeline),
47 | test=dict(
48 | type=dataset_type,
49 | ann_file=data_root + 'annotations/instances_val2017.json',
50 | img_prefix=data_root + 'val2017/',
51 | classes=classes,
52 | pipeline=test_pipeline))
53 | evaluation = dict(metric=['bbox', 'segm'])
54 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/centernet/zzd_centernet_resnet18_dcnv2_140e_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/datasets/zzd_crosswalk_detection.py',
3 | '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
4 | ]
5 |
6 | model = dict(
7 | type='CenterNet',
8 | backbone=dict(
9 | type='ResNet',
10 | depth=18,
11 | norm_eval=False,
12 | norm_cfg=dict(type='BN'),
13 | init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet18')),
14 | neck=dict(
15 | type='CTResNetNeck',
16 | in_channel=512,
17 | num_deconv_filters=(256, 128, 64),
18 | num_deconv_kernels=(4, 4, 4),
19 | use_dcn=True),
20 | bbox_head=dict(
21 | type='CenterNetHead',
22 | num_classes=2,
23 | in_channel=64,
24 | feat_channel=64,
25 | loss_center_heatmap=dict(type='GaussianFocalLoss', loss_weight=1.0),
26 | loss_wh=dict(type='L1Loss', loss_weight=0.1),
27 | loss_offset=dict(type='L1Loss', loss_weight=1.0)),
28 | train_cfg=None,
29 | test_cfg=dict(topk=100, local_maximum_kernel=3, max_per_img=100))
30 |
31 | # We fixed the incorrect img_norm_cfg problem in the source code.
32 | img_norm_cfg = dict(
33 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
34 |
35 | train_pipeline = [
36 | dict(type='LoadImageFromFile', to_float32=True, color_type='color'),
37 | dict(type='LoadAnnotations', with_bbox=True),
38 | dict(
39 | type='PhotoMetricDistortion',
40 | brightness_delta=32,
41 | contrast_range=(0.5, 1.5),
42 | saturation_range=(0.5, 1.5),
43 | hue_delta=18),
44 | dict(
45 | type='RandomCenterCropPad',
46 | crop_size=(512, 512),
47 | ratios=(0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3),
48 | mean=[0, 0, 0],
49 | std=[1, 1, 1],
50 | to_rgb=True,
51 | test_pad_mode=None),
52 | dict(type='Resize', img_scale=(512, 512), keep_ratio=True),
53 | dict(type='RandomFlip', flip_ratio=0.5),
54 | dict(type='Normalize', **img_norm_cfg),
55 | dict(type='DefaultFormatBundle'),
56 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels'])
57 | ]
58 | test_pipeline = [
59 | dict(type='LoadImageFromFile', to_float32=True),
60 | dict(
61 | type='MultiScaleFlipAug',
62 | scale_factor=1.0,
63 | flip=False,
64 | transforms=[
65 | dict(type='Resize', keep_ratio=True),
66 | dict(
67 | type='RandomCenterCropPad',
68 | ratios=None,
69 | border=None,
70 | mean=[0, 0, 0],
71 | std=[1, 1, 1],
72 | to_rgb=True,
73 | test_mode=True,
74 | test_pad_mode=['logical_or', 31],
75 | test_pad_add_pix=1),
76 | dict(type='RandomFlip'),
77 | dict(type='Normalize', **img_norm_cfg),
78 | dict(type='DefaultFormatBundle'),
79 | dict(
80 | type='Collect',
81 | meta_keys=('filename', 'ori_shape', 'img_shape', 'pad_shape',
82 | 'scale_factor', 'flip', 'flip_direction',
83 | 'img_norm_cfg', 'border'),
84 | keys=['img'])
85 | ])
86 | ]
87 |
88 | classes = ('crosswalk', 'guide_arrows')
89 | dataset_type = 'CocoDataset'
90 | # data_root = 'data/coco/'
91 | data_root = '/home/zzd/datasets/crosswalk/fogged_train_data_coco_format/'
92 |
93 | # Use RepeatDataset to speed up training
94 | data = dict(
95 | samples_per_gpu=8,
96 | workers_per_gpu=2,
97 | train=dict(
98 | _delete_=True,
99 | type='RepeatDataset',
100 | times=5,
101 | dataset=dict(
102 | type=dataset_type,
103 | ann_file=data_root + 'annotations/instances_train2017.json',
104 | img_prefix=data_root + 'train2017/',
105 | pipeline=train_pipeline, classes=classes)),
106 | val=dict(pipeline=test_pipeline, classes=classes),
107 | test=dict(pipeline=test_pipeline, classes=classes))
108 |
109 | # optimizer
110 | # Based on the default settings of modern detectors, the SGD effect is better
111 | # than the Adam in the source code, so we use SGD default settings and
112 | # if you use adam+lr5e-4, the map is 29.1.
113 | optimizer_config = dict(
114 | _delete_=True, grad_clip=dict(max_norm=35, norm_type=2))
115 |
116 | # learning policy
117 | # Based on the default settings of modern detectors, we added warmup settings.
118 | lr_config = dict(
119 | policy='step',
120 | warmup='linear',
121 | warmup_iters=1000,
122 | warmup_ratio=1.0 / 1000,
123 | step=[18, 24]) # the real step is [18*5, 24*5]
124 | runner = dict(max_epochs=28) # the real epoch is 28*5=140
125 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/deformable_detr/zzd_deformable_detr_r50_16x2_50e_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/datasets/zzd_crosswalk_detection.py', '../_base_/default_runtime.py'
3 | ]
4 | model = dict(
5 | type='DeformableDETR',
6 | backbone=dict(
7 | type='ResNet',
8 | depth=50,
9 | num_stages=4,
10 | out_indices=(1, 2, 3),
11 | frozen_stages=1,
12 | norm_cfg=dict(type='BN', requires_grad=False),
13 | norm_eval=True,
14 | style='pytorch',
15 | init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')),
16 | neck=dict(
17 | type='ChannelMapper',
18 | in_channels=[512, 1024, 2048],
19 | kernel_size=1,
20 | out_channels=256,
21 | act_cfg=None,
22 | norm_cfg=dict(type='GN', num_groups=32),
23 | num_outs=4),
24 | bbox_head=dict(
25 | type='DeformableDETRHead',
26 | num_query=300,
27 | num_classes=2,
28 | in_channels=2048,
29 | sync_cls_avg_factor=True,
30 | as_two_stage=False,
31 | transformer=dict(
32 | type='DeformableDetrTransformer',
33 | encoder=dict(
34 | type='DetrTransformerEncoder',
35 | num_layers=6,
36 | transformerlayers=dict(
37 | type='BaseTransformerLayer',
38 | attn_cfgs=dict(
39 | type='MultiScaleDeformableAttention', embed_dims=256),
40 | feedforward_channels=1024,
41 | ffn_dropout=0.1,
42 | operation_order=('self_attn', 'norm', 'ffn', 'norm'))),
43 | decoder=dict(
44 | type='DeformableDetrTransformerDecoder',
45 | num_layers=6,
46 | return_intermediate=True,
47 | transformerlayers=dict(
48 | type='DetrTransformerDecoderLayer',
49 | attn_cfgs=[
50 | dict(
51 | type='MultiheadAttention',
52 | embed_dims=256,
53 | num_heads=8,
54 | dropout=0.1),
55 | dict(
56 | type='MultiScaleDeformableAttention',
57 | embed_dims=256)
58 | ],
59 | feedforward_channels=1024,
60 | ffn_dropout=0.1,
61 | operation_order=('self_attn', 'norm', 'cross_attn', 'norm',
62 | 'ffn', 'norm')))),
63 | positional_encoding=dict(
64 | type='SinePositionalEncoding',
65 | num_feats=128,
66 | normalize=True,
67 | offset=-0.5),
68 | loss_cls=dict(
69 | type='FocalLoss',
70 | use_sigmoid=True,
71 | gamma=2.0,
72 | alpha=0.25,
73 | loss_weight=2.0),
74 | loss_bbox=dict(type='L1Loss', loss_weight=5.0),
75 | loss_iou=dict(type='GIoULoss', loss_weight=2.0)),
76 | # training and testing settings
77 | train_cfg=dict(
78 | assigner=dict(
79 | type='HungarianAssigner',
80 | cls_cost=dict(type='FocalLossCost', weight=2.0),
81 | reg_cost=dict(type='BBoxL1Cost', weight=5.0, box_format='xywh'),
82 | iou_cost=dict(type='IoUCost', iou_mode='giou', weight=2.0))),
83 | test_cfg=dict(max_per_img=100))
84 | img_norm_cfg = dict(
85 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
86 | # train_pipeline, NOTE the img_scale and the Pad's size_divisor is different
87 | # from the default setting in mmdet.
88 | train_pipeline = [
89 | dict(type='LoadImageFromFile'),
90 | dict(type='LoadAnnotations', with_bbox=True),
91 | dict(type='RandomFlip', flip_ratio=0.5),
92 | dict(
93 | type='AutoAugment',
94 | policies=[
95 | [
96 | dict(
97 | type='Resize',
98 | img_scale=[(480, 1333), (512, 1333), (544, 1333),
99 | (576, 1333), (608, 1333), (640, 1333),
100 | (672, 1333), (704, 1333), (736, 1333),
101 | (768, 1333), (800, 1333)],
102 | multiscale_mode='value',
103 | keep_ratio=True)
104 | ],
105 | [
106 | dict(
107 | type='Resize',
108 | # The radio of all image in train dataset < 7
109 | # follow the original impl
110 | img_scale=[(400, 4200), (500, 4200), (600, 4200)],
111 | multiscale_mode='value',
112 | keep_ratio=True),
113 | dict(
114 | type='RandomCrop',
115 | crop_type='absolute_range',
116 | crop_size=(384, 600),
117 | allow_negative_crop=True),
118 | dict(
119 | type='Resize',
120 | img_scale=[(480, 1333), (512, 1333), (544, 1333),
121 | (576, 1333), (608, 1333), (640, 1333),
122 | (672, 1333), (704, 1333), (736, 1333),
123 | (768, 1333), (800, 1333)],
124 | multiscale_mode='value',
125 | override=True,
126 | keep_ratio=True)
127 | ]
128 | ]),
129 | dict(type='Normalize', **img_norm_cfg),
130 | dict(type='Pad', size_divisor=1),
131 | dict(type='DefaultFormatBundle'),
132 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels'])
133 | ]
134 | # test_pipeline, NOTE the Pad's size_divisor is different from the default
135 | # setting (size_divisor=32). While there is little effect on the performance
136 | # whether we use the default setting or use size_divisor=1.
137 | test_pipeline = [
138 | dict(type='LoadImageFromFile'),
139 | dict(
140 | type='MultiScaleFlipAug',
141 | img_scale=(1333, 800),
142 | flip=False,
143 | transforms=[
144 | dict(type='Resize', keep_ratio=True),
145 | dict(type='RandomFlip'),
146 | dict(type='Normalize', **img_norm_cfg),
147 | dict(type='Pad', size_divisor=1),
148 | dict(type='ImageToTensor', keys=['img']),
149 | dict(type='Collect', keys=['img'])
150 | ])
151 | ]
152 |
153 | classes = ('crosswalk', 'guide_arrows')
154 | data = dict(
155 | samples_per_gpu=1,
156 | workers_per_gpu=2,
157 | train=dict(filter_empty_gt=False, pipeline=train_pipeline, classes=classes),
158 | val=dict(pipeline=test_pipeline, classes=classes),
159 | test=dict(pipeline=test_pipeline, classes=classes))
160 | # optimizer
161 | optimizer = dict(
162 | type='Adam',
163 | lr=2e-4,
164 | weight_decay=0.0001,
165 | paramwise_cfg=dict(
166 | custom_keys={
167 | 'backbone': dict(lr_mult=0.1),
168 | 'sampling_offsets': dict(lr_mult=0.1),
169 | 'reference_points': dict(lr_mult=0.1)
170 | }))
171 | optimizer_config = dict(grad_clip=dict(max_norm=0.1, norm_type=2))
172 | # learning policy
173 | lr_config = dict(policy='step', step=[40])
174 | runner = dict(type='EpochBasedRunner', max_epochs=50)
175 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/faster_rcnn/zzd_faster_rcnn_r50_fpn_1x_coco.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/models/faster_rcnn_r50_fpn.py',
3 | '../_base_/datasets/zzd_crosswalk_detection.py',
4 | '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
5 | ]
6 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/faster_rcnn/zzd_faster_rcnn_r50_fpn_1x_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/models/faster_rcnn_r50_fpn.py',
3 | '../_base_/datasets/zzd_crosswalk_detection.py',
4 | '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
5 | ]
6 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/htc/zzd_htc_r50_fpn_1x_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = './zzd_htc_without_semantic_r50_fpn_1x_crosswalk.py'
2 | model = dict(
3 | roi_head=dict(
4 | semantic_roi_extractor=dict(
5 | type='SingleRoIExtractor',
6 | roi_layer=dict(type='RoIAlign', output_size=14, sampling_ratio=0),
7 | out_channels=256,
8 | featmap_strides=[8]),
9 | semantic_head=dict(
10 | type='FusedSemanticHead',
11 | num_ins=5,
12 | fusion_level=1,
13 | num_convs=4,
14 | in_channels=256,
15 | conv_out_channels=256,
16 | num_classes=183,
17 | loss_seg=dict(
18 | type='CrossEntropyLoss', ignore_index=255, loss_weight=0.2))))
19 | data_root = 'data/coco/'
20 | img_norm_cfg = dict(
21 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
22 | train_pipeline = [
23 | dict(type='LoadImageFromFile'),
24 | dict(
25 | type='LoadAnnotations', with_bbox=True, with_mask=True, with_seg=True),
26 | dict(type='Resize', img_scale=(1333, 800), keep_ratio=True),
27 | dict(type='RandomFlip', flip_ratio=0.5),
28 | dict(type='Normalize', **img_norm_cfg),
29 | dict(type='Pad', size_divisor=32),
30 | dict(type='SegRescale', scale_factor=1 / 8),
31 | dict(type='DefaultFormatBundle'),
32 | dict(
33 | type='Collect',
34 | keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks', 'gt_semantic_seg']),
35 | ]
36 | test_pipeline = [
37 | dict(type='LoadImageFromFile'),
38 | dict(
39 | type='MultiScaleFlipAug',
40 | img_scale=(1333, 800),
41 | flip=False,
42 | transforms=[
43 | dict(type='Resize', keep_ratio=True),
44 | dict(type='RandomFlip', flip_ratio=0.5),
45 | dict(type='Normalize', **img_norm_cfg),
46 | dict(type='Pad', size_divisor=32),
47 | dict(type='ImageToTensor', keys=['img']),
48 | dict(type='Collect', keys=['img']),
49 | ])
50 | ]
51 | data = dict(
52 | train=dict(
53 | seg_prefix=data_root + 'stuffthingmaps/train2017/',
54 | pipeline=train_pipeline),
55 | val=dict(pipeline=test_pipeline),
56 | test=dict(pipeline=test_pipeline))
57 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/htc/zzd_htc_without_semantic_r50_fpn_1x_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/datasets/zzd_crosswalk_instance.py',
3 | '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
4 | ]
5 | # model settings
6 | model = dict(
7 | type='HybridTaskCascade',
8 | backbone=dict(
9 | type='ResNet',
10 | depth=50,
11 | num_stages=4,
12 | out_indices=(0, 1, 2, 3),
13 | frozen_stages=1,
14 | norm_cfg=dict(type='BN', requires_grad=True),
15 | norm_eval=True,
16 | style='pytorch',
17 | init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')),
18 | neck=dict(
19 | type='FPN',
20 | in_channels=[256, 512, 1024, 2048],
21 | out_channels=256,
22 | num_outs=5),
23 | rpn_head=dict(
24 | type='RPNHead',
25 | in_channels=256,
26 | feat_channels=256,
27 | anchor_generator=dict(
28 | type='AnchorGenerator',
29 | scales=[8],
30 | ratios=[0.5, 1.0, 2.0],
31 | strides=[4, 8, 16, 32, 64]),
32 | bbox_coder=dict(
33 | type='DeltaXYWHBBoxCoder',
34 | target_means=[.0, .0, .0, .0],
35 | target_stds=[1.0, 1.0, 1.0, 1.0]),
36 | loss_cls=dict(
37 | type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),
38 | loss_bbox=dict(type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.0)),
39 | roi_head=dict(
40 | type='HybridTaskCascadeRoIHead',
41 | interleaved=True,
42 | mask_info_flow=True,
43 | num_stages=3,
44 | stage_loss_weights=[1, 0.5, 0.25],
45 | bbox_roi_extractor=dict(
46 | type='SingleRoIExtractor',
47 | roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),
48 | out_channels=256,
49 | featmap_strides=[4, 8, 16, 32]),
50 | bbox_head=[
51 | dict(
52 | type='Shared2FCBBoxHead',
53 | in_channels=256,
54 | fc_out_channels=1024,
55 | roi_feat_size=7,
56 | num_classes=2,
57 | bbox_coder=dict(
58 | type='DeltaXYWHBBoxCoder',
59 | target_means=[0., 0., 0., 0.],
60 | target_stds=[0.1, 0.1, 0.2, 0.2]),
61 | reg_class_agnostic=True,
62 | loss_cls=dict(
63 | type='CrossEntropyLoss',
64 | use_sigmoid=False,
65 | loss_weight=1.0),
66 | loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
67 | loss_weight=1.0)),
68 | dict(
69 | type='Shared2FCBBoxHead',
70 | in_channels=256,
71 | fc_out_channels=1024,
72 | roi_feat_size=7,
73 | num_classes=2,
74 | bbox_coder=dict(
75 | type='DeltaXYWHBBoxCoder',
76 | target_means=[0., 0., 0., 0.],
77 | target_stds=[0.05, 0.05, 0.1, 0.1]),
78 | reg_class_agnostic=True,
79 | loss_cls=dict(
80 | type='CrossEntropyLoss',
81 | use_sigmoid=False,
82 | loss_weight=1.0),
83 | loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
84 | loss_weight=1.0)),
85 | dict(
86 | type='Shared2FCBBoxHead',
87 | in_channels=256,
88 | fc_out_channels=1024,
89 | roi_feat_size=7,
90 | num_classes=2,
91 | bbox_coder=dict(
92 | type='DeltaXYWHBBoxCoder',
93 | target_means=[0., 0., 0., 0.],
94 | target_stds=[0.033, 0.033, 0.067, 0.067]),
95 | reg_class_agnostic=True,
96 | loss_cls=dict(
97 | type='CrossEntropyLoss',
98 | use_sigmoid=False,
99 | loss_weight=1.0),
100 | loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0))
101 | ],
102 | mask_roi_extractor=dict(
103 | type='SingleRoIExtractor',
104 | roi_layer=dict(type='RoIAlign', output_size=14, sampling_ratio=0),
105 | out_channels=256,
106 | featmap_strides=[4, 8, 16, 32]),
107 | mask_head=[
108 | dict(
109 | type='HTCMaskHead',
110 | with_conv_res=False,
111 | num_convs=4,
112 | in_channels=256,
113 | conv_out_channels=256,
114 | num_classes=2,
115 | loss_mask=dict(
116 | type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)),
117 | dict(
118 | type='HTCMaskHead',
119 | num_convs=4,
120 | in_channels=256,
121 | conv_out_channels=256,
122 | num_classes=2,
123 | loss_mask=dict(
124 | type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)),
125 | dict(
126 | type='HTCMaskHead',
127 | num_convs=4,
128 | in_channels=256,
129 | conv_out_channels=256,
130 | num_classes=2,
131 | loss_mask=dict(
132 | type='CrossEntropyLoss', use_mask=True, loss_weight=1.0))
133 | ]),
134 | # model training and testing settings
135 | train_cfg=dict(
136 | rpn=dict(
137 | assigner=dict(
138 | type='MaxIoUAssigner',
139 | pos_iou_thr=0.7,
140 | neg_iou_thr=0.3,
141 | min_pos_iou=0.3,
142 | ignore_iof_thr=-1),
143 | sampler=dict(
144 | type='RandomSampler',
145 | num=256,
146 | pos_fraction=0.5,
147 | neg_pos_ub=-1,
148 | add_gt_as_proposals=False),
149 | allowed_border=0,
150 | pos_weight=-1,
151 | debug=False),
152 | rpn_proposal=dict(
153 | nms_pre=2000,
154 | max_per_img=2000,
155 | nms=dict(type='nms', iou_threshold=0.7),
156 | min_bbox_size=0),
157 | rcnn=[
158 | dict(
159 | assigner=dict(
160 | type='MaxIoUAssigner',
161 | pos_iou_thr=0.5,
162 | neg_iou_thr=0.5,
163 | min_pos_iou=0.5,
164 | ignore_iof_thr=-1),
165 | sampler=dict(
166 | type='RandomSampler',
167 | num=512,
168 | pos_fraction=0.25,
169 | neg_pos_ub=-1,
170 | add_gt_as_proposals=True),
171 | mask_size=28,
172 | pos_weight=-1,
173 | debug=False),
174 | dict(
175 | assigner=dict(
176 | type='MaxIoUAssigner',
177 | pos_iou_thr=0.6,
178 | neg_iou_thr=0.6,
179 | min_pos_iou=0.6,
180 | ignore_iof_thr=-1),
181 | sampler=dict(
182 | type='RandomSampler',
183 | num=512,
184 | pos_fraction=0.25,
185 | neg_pos_ub=-1,
186 | add_gt_as_proposals=True),
187 | mask_size=28,
188 | pos_weight=-1,
189 | debug=False),
190 | dict(
191 | assigner=dict(
192 | type='MaxIoUAssigner',
193 | pos_iou_thr=0.7,
194 | neg_iou_thr=0.7,
195 | min_pos_iou=0.7,
196 | ignore_iof_thr=-1),
197 | sampler=dict(
198 | type='RandomSampler',
199 | num=512,
200 | pos_fraction=0.25,
201 | neg_pos_ub=-1,
202 | add_gt_as_proposals=True),
203 | mask_size=28,
204 | pos_weight=-1,
205 | debug=False)
206 | ]),
207 | test_cfg=dict(
208 | rpn=dict(
209 | nms_pre=1000,
210 | max_per_img=1000,
211 | nms=dict(type='nms', iou_threshold=0.7),
212 | min_bbox_size=0),
213 | rcnn=dict(
214 | score_thr=0.001,
215 | nms=dict(type='nms', iou_threshold=0.5),
216 | max_per_img=100,
217 | mask_thr_binary=0.5)))
218 | img_norm_cfg = dict(
219 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
220 | test_pipeline = [
221 | dict(type='LoadImageFromFile'),
222 | dict(
223 | type='MultiScaleFlipAug',
224 | img_scale=(1333, 800),
225 | flip=False,
226 | transforms=[
227 | dict(type='Resize', keep_ratio=True),
228 | dict(type='RandomFlip', flip_ratio=0.5),
229 | dict(type='Normalize', **img_norm_cfg),
230 | dict(type='Pad', size_divisor=32),
231 | dict(type='ImageToTensor', keys=['img']),
232 | dict(type='Collect', keys=['img']),
233 | ])
234 | ]
235 | data = dict(
236 | val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline))
237 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/mask_rcnn/zzd_mask_rcnn_r50_fpn_1x_coco.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/models/mask_rcnn_r50_fpn.py',
3 | '../_base_/datasets/zzd_coco_instance.py',
4 | '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
5 | ]
6 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/mask_rcnn/zzd_mask_rcnn_r50_fpn_1x_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/models/mask_rcnn_r50_fpn.py',
3 | '../_base_/datasets/zzd_crosswalk_instance.py',
4 | '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
5 | ]
6 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/scnet/zzd_scnet_r50_fpn_1x_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = '../htc/zzd_htc_r50_fpn_1x_crosswalk.py'
2 | # model settings
3 | model = dict(
4 | type='SCNet',
5 | roi_head=dict(
6 | _delete_=True,
7 | type='SCNetRoIHead',
8 | num_stages=3,
9 | stage_loss_weights=[1, 0.5, 0.25],
10 | bbox_roi_extractor=dict(
11 | type='SingleRoIExtractor',
12 | roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),
13 | out_channels=256,
14 | featmap_strides=[4, 8, 16, 32]),
15 | bbox_head=[
16 | dict(
17 | type='SCNetBBoxHead',
18 | num_shared_fcs=2,
19 | in_channels=256,
20 | fc_out_channels=1024,
21 | roi_feat_size=7,
22 | num_classes=2,
23 | bbox_coder=dict(
24 | type='DeltaXYWHBBoxCoder',
25 | target_means=[0., 0., 0., 0.],
26 | target_stds=[0.1, 0.1, 0.2, 0.2]),
27 | reg_class_agnostic=True,
28 | loss_cls=dict(
29 | type='CrossEntropyLoss',
30 | use_sigmoid=False,
31 | loss_weight=1.0),
32 | loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
33 | loss_weight=1.0)),
34 | dict(
35 | type='SCNetBBoxHead',
36 | num_shared_fcs=2,
37 | in_channels=256,
38 | fc_out_channels=1024,
39 | roi_feat_size=7,
40 | num_classes=2,
41 | bbox_coder=dict(
42 | type='DeltaXYWHBBoxCoder',
43 | target_means=[0., 0., 0., 0.],
44 | target_stds=[0.05, 0.05, 0.1, 0.1]),
45 | reg_class_agnostic=True,
46 | loss_cls=dict(
47 | type='CrossEntropyLoss',
48 | use_sigmoid=False,
49 | loss_weight=1.0),
50 | loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
51 | loss_weight=1.0)),
52 | dict(
53 | type='SCNetBBoxHead',
54 | num_shared_fcs=2,
55 | in_channels=256,
56 | fc_out_channels=1024,
57 | roi_feat_size=7,
58 | num_classes=2,
59 | bbox_coder=dict(
60 | type='DeltaXYWHBBoxCoder',
61 | target_means=[0., 0., 0., 0.],
62 | target_stds=[0.033, 0.033, 0.067, 0.067]),
63 | reg_class_agnostic=True,
64 | loss_cls=dict(
65 | type='CrossEntropyLoss',
66 | use_sigmoid=False,
67 | loss_weight=1.0),
68 | loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0))
69 | ],
70 | mask_roi_extractor=dict(
71 | type='SingleRoIExtractor',
72 | roi_layer=dict(type='RoIAlign', output_size=14, sampling_ratio=0),
73 | out_channels=256,
74 | featmap_strides=[4, 8, 16, 32]),
75 | mask_head=dict(
76 | type='SCNetMaskHead',
77 | num_convs=12,
78 | in_channels=256,
79 | conv_out_channels=256,
80 | num_classes=2,
81 | conv_to_res=True,
82 | loss_mask=dict(
83 | type='CrossEntropyLoss', use_mask=True, loss_weight=1.0)),
84 | semantic_roi_extractor=dict(
85 | type='SingleRoIExtractor',
86 | roi_layer=dict(type='RoIAlign', output_size=14, sampling_ratio=0),
87 | out_channels=256,
88 | featmap_strides=[8]),
89 | semantic_head=dict(
90 | type='SCNetSemanticHead',
91 | num_ins=5,
92 | fusion_level=1,
93 | num_convs=4,
94 | in_channels=256,
95 | conv_out_channels=256,
96 | num_classes=183,
97 | loss_seg=dict(
98 | type='CrossEntropyLoss', ignore_index=255, loss_weight=0.2),
99 | conv_to_res=True),
100 | glbctx_head=dict(
101 | type='GlobalContextHead',
102 | num_convs=4,
103 | in_channels=256,
104 | conv_out_channels=256,
105 | num_classes=2,
106 | loss_weight=3.0,
107 | conv_to_res=True),
108 | feat_relay_head=dict(
109 | type='FeatureRelayHead',
110 | in_channels=1024,
111 | out_conv_channels=256,
112 | roi_feat_size=7,
113 | scale_factor=2)))
114 |
115 | # uncomment below code to enable test time augmentations
116 | # img_norm_cfg = dict(
117 | # mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
118 | # test_pipeline = [
119 | # dict(type='LoadImageFromFile'),
120 | # dict(
121 | # type='MultiScaleFlipAug',
122 | # img_scale=[(600, 900), (800, 1200), (1000, 1500), (1200, 1800),
123 | # (1400, 2100)],
124 | # flip=True,
125 | # transforms=[
126 | # dict(type='Resize', keep_ratio=True),
127 | # dict(type='RandomFlip', flip_ratio=0.5),
128 | # dict(type='Normalize', **img_norm_cfg),
129 | # dict(type='Pad', size_divisor=32),
130 | # dict(type='ImageToTensor', keys=['img']),
131 | # dict(type='Collect', keys=['img']),
132 | # ])
133 | # ]
134 | # data = dict(
135 | # val=dict(pipeline=test_pipeline),
136 | # test=dict(pipeline=test_pipeline))
137 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/swin/zzd_mask_rcnn_swin-t-p4-w7_fpn_1x_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = [
2 | '../_base_/models/mask_rcnn_r50_fpn.py',
3 | '../_base_/datasets/zzd_crosswalk_instance.py',
4 | '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
5 | ]
6 | pretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth' # noqa
7 | # pretrained = None
8 | model = dict(
9 | type='MaskRCNN',
10 | backbone=dict(
11 | _delete_=True,
12 | type='SwinTransformer',
13 | embed_dims=96,
14 | depths=[2, 2, 6, 2],
15 | num_heads=[3, 6, 12, 24],
16 | window_size=7,
17 | mlp_ratio=4,
18 | qkv_bias=True,
19 | qk_scale=None,
20 | drop_rate=0.,
21 | attn_drop_rate=0.,
22 | drop_path_rate=0.2,
23 | patch_norm=True,
24 | out_indices=(0, 1, 2, 3),
25 | with_cp=False,
26 | convert_weights=True,
27 | init_cfg=dict(type='Pretrained', checkpoint=pretrained)),
28 | neck=dict(in_channels=[96, 192, 384, 768]))
29 |
30 | optimizer = dict(
31 | _delete_=True,
32 | type='AdamW',
33 | lr=0.0001,
34 | betas=(0.9, 0.999),
35 | weight_decay=0.05,
36 | paramwise_cfg=dict(
37 | custom_keys={
38 | 'absolute_pos_embed': dict(decay_mult=0.),
39 | 'relative_position_bias_table': dict(decay_mult=0.),
40 | 'norm': dict(decay_mult=0.)
41 | }))
42 | lr_config = dict(warmup_iters=1000, step=[8, 11])
43 | runner = dict(max_epochs=12)
44 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/yolox/zzd_yolox_m_8x8_300e_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = './zzd_yolox_s_8x8_300e_crosswalk.py'
2 |
3 | # model settings
4 | model = dict(
5 | backbone=dict(deepen_factor=0.67, widen_factor=0.75),
6 | neck=dict(in_channels=[192, 384, 768], out_channels=192, num_csp_blocks=2),
7 | bbox_head=dict(in_channels=192, feat_channels=192),
8 | )
9 |
--------------------------------------------------------------------------------
/repos/mmdetection/configs/yolox/zzd_yolox_s_8x8_300e_crosswalk.py:
--------------------------------------------------------------------------------
1 | _base_ = ['../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py']
2 |
3 | # model settings
4 | model = dict(
5 | type='YOLOX',
6 | backbone=dict(type='CSPDarknet', deepen_factor=0.33, widen_factor=0.5),
7 | neck=dict(
8 | type='YOLOXPAFPN',
9 | in_channels=[128, 256, 512],
10 | out_channels=128,
11 | num_csp_blocks=1),
12 | bbox_head=dict(
13 | type='YOLOXHead', num_classes=2, in_channels=128, feat_channels=128),
14 | train_cfg=dict(assigner=dict(type='SimOTAAssigner', center_radius=2.5)),
15 | # In order to align the source code, the threshold of the val phase is
16 | # 0.01, and the threshold of the test phase is 0.001.
17 | test_cfg=dict(score_thr=0.01, nms=dict(type='nms', iou_threshold=0.65)))
18 |
19 | # dataset settings
20 | # data_root = 'data/coco/'
21 | dataset_type = 'CocoDataset'
22 | classes = ('crosswalk', 'guide_arrows')
23 | data_root = '/home/zzd/datasets/crosswalk/fogged_train_data_coco_format/'
24 |
25 | img_norm_cfg = dict(
26 | mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
27 |
28 | img_scale = (640, 640)
29 |
30 | train_pipeline = [
31 | dict(type='Mosaic', img_scale=img_scale, pad_val=114.0),
32 | dict(
33 | type='RandomAffine',
34 | scaling_ratio_range=(0.1, 2),
35 | border=(-img_scale[0] // 2, -img_scale[1] // 2)),
36 | dict(
37 | type='MixUp',
38 | img_scale=img_scale,
39 | ratio_range=(0.8, 1.6),
40 | pad_val=114.0),
41 | dict(
42 | type='PhotoMetricDistortion',
43 | brightness_delta=32,
44 | contrast_range=(0.5, 1.5),
45 | saturation_range=(0.5, 1.5),
46 | hue_delta=18),
47 | dict(type='RandomFlip', flip_ratio=0.5),
48 | dict(type='Resize', keep_ratio=True),
49 | dict(type='Pad', pad_to_square=True, pad_val=114.0),
50 | dict(type='Normalize', **img_norm_cfg),
51 | dict(type='DefaultFormatBundle'),
52 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels'])
53 | ]
54 |
55 | train_dataset = dict(
56 | type='MultiImageMixDataset',
57 | dataset=dict(
58 | type=dataset_type,
59 | ann_file=data_root + 'annotations/instances_train2017.json',
60 | img_prefix=data_root + 'train2017/',
61 | classes=classes,
62 | pipeline=[
63 | dict(type='LoadImageFromFile', to_float32=True),
64 | dict(type='LoadAnnotations', with_bbox=True)
65 | ],
66 | filter_empty_gt=False,
67 | ),
68 | pipeline=train_pipeline,
69 | dynamic_scale=img_scale)
70 |
71 | test_pipeline = [
72 | dict(type='LoadImageFromFile'),
73 | dict(
74 | type='MultiScaleFlipAug',
75 | img_scale=img_scale,
76 | flip=False,
77 | transforms=[
78 | dict(type='Resize', keep_ratio=True),
79 | dict(type='RandomFlip'),
80 | dict(type='Pad', size=img_scale, pad_val=114.0),
81 | dict(type='Normalize', **img_norm_cfg),
82 | dict(type='DefaultFormatBundle'),
83 | dict(type='Collect', keys=['img'])
84 | ])
85 | ]
86 |
87 | data = dict(
88 | samples_per_gpu=4,
89 | workers_per_gpu=2,
90 | train=train_dataset,
91 | val=dict(
92 | type=dataset_type,
93 | ann_file=data_root + 'annotations/instances_val2017.json',
94 | img_prefix=data_root + 'val2017/',
95 | classes=classes,
96 | pipeline=test_pipeline),
97 | test=dict(
98 | type=dataset_type,
99 | ann_file=data_root + 'annotations/instances_val2017.json',
100 | img_prefix=data_root + 'val2017/',
101 | classes=classes,
102 | pipeline=test_pipeline))
103 |
104 | # optimizer
105 | # default 8 gpu
106 | optimizer = dict(
107 | type='SGD',
108 | lr=0.01,
109 | momentum=0.9,
110 | weight_decay=5e-4,
111 | nesterov=True,
112 | paramwise_cfg=dict(norm_decay_mult=0., bias_decay_mult=0.))
113 | optimizer_config = dict(grad_clip=None)
114 |
115 | # learning policy
116 | lr_config = dict(
117 | _delete_=True,
118 | policy='YOLOX',
119 | warmup='exp',
120 | by_epoch=False,
121 | warmup_by_epoch=True,
122 | warmup_ratio=1,
123 | warmup_iters=5, # 5 epoch
124 | num_last_epochs=15,
125 | min_lr_ratio=0.05)
126 | runner = dict(type='EpochBasedRunner', max_epochs=100)
127 |
128 | resume_from = None
129 | interval = 10
130 |
131 | custom_hooks = [
132 | dict(type='YOLOXModeSwitchHook', num_last_epochs=15, priority=48),
133 | dict(
134 | type='SyncRandomSizeHook',
135 | ratio_range=(14, 26),
136 | img_scale=img_scale,
137 | priority=48),
138 | dict(
139 | type='SyncNormHook',
140 | num_last_epochs=15,
141 | interval=interval,
142 | priority=48),
143 | dict(type='ExpMomentumEMAHook', resume_from=resume_from, priority=49)
144 | ]
145 | checkpoint_config = dict(interval=interval)
146 | evaluation = dict(interval=interval, metric='bbox')
147 | log_config = dict(interval=50)
148 |
--------------------------------------------------------------------------------
/repos/mmdetection/demo/zzd_inference_speed_test.py:
--------------------------------------------------------------------------------
1 | # Copyright (c) OpenMMLab. All rights reserved.
2 | import os
3 | import asyncio
4 | import numpy as np
5 | from pathlib import Path
6 | from argparse import ArgumentParser
7 |
8 | from mmdet.apis import (async_inference_detector, inference_detector,
9 | init_detector, show_result_pyplot)
10 |
11 |
12 | def parse_args():
13 | parser = ArgumentParser()
14 | parser.add_argument('--img', help='Image file')
15 | parser.add_argument('--config', help='Config file')
16 | parser.add_argument('--checkpoint', help='Checkpoint file')
17 | parser.add_argument(
18 | '--device', default='cuda:0', help='Device used for inference')
19 | parser.add_argument(
20 | '--score-thr', type=float, default=0.3, help='bbox score threshold')
21 | parser.add_argument(
22 | '--async-test',
23 | action='store_true',
24 | help='whether to set async options for async inference.')
25 | args = parser.parse_args()
26 |
27 | path = '../../example/images'
28 | imgs = sorted([f'{path}/{x}' for x in os.listdir(path) if x.endswith('jpg')])
29 | args.img = imgs
30 |
31 | ck_dict = dict(mask_rcnn='zzd_mask_rcnn_r50_fpn_1x_crosswalk/epoch_12.pth',
32 | faster_rcnn='zzd_faster_rcnn_r50_fpn_1x_crosswalk/epoch_12.pth',
33 | yolox='zzd_yolox_m_8x8_300e_crosswalk/epoch_100.pth',
34 | swin='zzd_mask_rcnn_swin-t-p4-w7_fpn_1x_crosswalk/epoch_12.pth')
35 |
36 | model = 'mask_rcnn'
37 | model = 'faster_rcnn'
38 | model = 'yolox'
39 | model = 'swin'
40 | args.config = f'./configs/{model}/{ck_dict[model].split("/")[0]}.py'
41 | args.checkpoint = f'work_dirs/{ck_dict[model]}'
42 | return args
43 |
44 |
45 | def main(args):
46 | # build the model from a config file and a checkpoint file
47 | model = init_detector(args.config, args.checkpoint, device=args.device)
48 | # test a single image
49 | t = []
50 | for i, img in enumerate(args.img):
51 | result, st = inference_detector(model, img) # spent time
52 | print(f'{i:>2} spent time: {st}')
53 | if i != 0:
54 | t.append(st)
55 | t = np.array(t)
56 | print(f'total time : {np.sum(t):.4f}s mean: {np.mean(t)*1000:.2f}ms FPS: {1/np.mean(t):.2f}')
57 | # show the results
58 | # show_result_pyplot(model, args.img, result, score_thr=args.score_thr)
59 |
60 |
61 | async def async_main(args):
62 | # build the model from a config file and a checkpoint file
63 | model = init_detector(args.config, args.checkpoint, device=args.device)
64 | # test a single image
65 | tasks = asyncio.create_task(async_inference_detector(model, args.img))
66 | result = await asyncio.gather(tasks)
67 | # show the results
68 | show_result_pyplot(model, args.img, result[0], score_thr=args.score_thr)
69 |
70 |
71 | if __name__ == '__main__':
72 | args = parse_args()
73 | if args.async_test:
74 | asyncio.run(async_main(args))
75 | else:
76 | main(args)
77 |
--------------------------------------------------------------------------------
/repos/mmdetection/zzd_inference.py:
--------------------------------------------------------------------------------
1 | import os
2 | import subprocess
3 |
4 | img = 'demo/demo.jpg'
5 | # cfg = './configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
6 | # checkpoint = './checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
7 |
8 | img = '../../example/images/filename_00038.jpg'
9 | cfg = './configs/mask_rcnn/zzd_mask_rcnn_r50_fpn_1x_crosswalk.py'
10 | checkpoint = 'work_dirs/zzd_mask_rcnn_r50_fpn_1x_crosswalk/epoch_12.pth'
11 |
12 | code = f'python demo/image_demo.py {img}' \
13 | f' {cfg} {checkpoint} --device cpu'
14 | print(code)
15 | # subprocess.call(code)
16 | os.system(code)
17 |
--------------------------------------------------------------------------------
/repos/mmdetection/zzd_train.py:
--------------------------------------------------------------------------------
1 | import os
2 | import subprocess
3 |
4 | cfg = "./configs/mask_rcnn/zzd_mask_rcnn_r50_fpn_1x_crosswalk.py"
5 | cfg = "./configs/faster_rcnn/zzd_faster_rcnn_r50_fpn_1x_crosswalk.py"
6 | cfg = "./configs/centernet/zzd_centernet_resnet18_dcnv2_140e_crosswalk.py"
7 | cfg = './configs/swin/zzd_mask_rcnn_swin-t-p4-w7_fpn_1x_crosswalk.py'
8 | cfg = './configs/yolox/zzd_yolox_m_8x8_300e_crosswalk.py'
9 |
10 | code = f'python tools/train.py {cfg}'
11 | # subprocess.call(code)
12 | os.system(code)
13 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | # pip install -r requirements.txt
2 |
3 | Cython
4 | matplotlib>=3.2.2
5 | numpy>=1.18.5
6 | opencv-python>=3.4.2
7 | Pillow
8 | pytorch>=1.6.0
9 | torchvision>=0.7.0
10 | PyYAML>=5.3
11 | scipy>=1.4.1
12 | tensorboard>=2.2
13 | torch>=1.7.0
14 | torchvision>=0.8.1
15 | tqdm>=4.41.0
16 | opencv_python
17 |
18 |
--------------------------------------------------------------------------------
/scripts/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/scripts/__init__.py
--------------------------------------------------------------------------------
/scripts/check_coco.py:
--------------------------------------------------------------------------------
1 | """
2 | 检查coco数据集
3 | """
4 | import os, sys
5 | import argparse
6 |
7 | sys.path.append(f'/home/zzd/PycharmProject/damei')
8 | import damei as dm
9 |
10 | # parser = argparse.ArgumentParser()
11 | # parser.add_argument('--json_path', )
12 |
13 |
14 | dp = "/home/zzd/datasets/crosswalk/fogged_train_data_coco_format"
15 | jp = f'{dp}/annotations/instances_train2017.json'
16 | # jp = "/home/zzd/datasets/mscoco/annotations/instances_train2017.json"
17 | dm.tools.check_coco(json_path=jp)
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/scripts/check_dataset.py:
--------------------------------------------------------------------------------
1 | """
2 | Check datasets
3 | """
4 | import os, sys
5 | import numpy as np
6 | import cv2
7 | from pathlib import Path
8 | import random
9 | import argparse
10 |
11 | try:
12 | from utils.datasets import LoadImages, LoadImagesAndLabels
13 | from utils.general import plot_one_box, xywh2xyxy
14 | except:
15 | sys.path.append('..')
16 | from utils.datasets import LoadImages, LoadImagesAndLabels
17 | from utils.general import plot_one_box, xywh2xyxy
18 |
19 |
20 | class CheckDataset(object):
21 | def __init__(self, dp):
22 | self.dp = dp
23 | # self.colors = [[random.randint(0, 255) for _ in range(3)] for __ in range(100)]
24 | # self.colors = ['#009966', '#EE3B3B']
25 | self.colors = [[102, 153, 0], [59, 59, 238]]
26 | self.names = ['crosswalk', 'guide_arrows']
27 |
28 | def __call__(self, trte='test', cf=False):
29 | p = f'{self.dp}/images/{trte}'
30 |
31 | imgs = [f'{self.dp}/images/{trte}/{x}' for x in os.listdir(p) if x.endswith('.jpg')]
32 | imgs = sorted(imgs)
33 | for i, imgp in enumerate(imgs):
34 | stem = Path(imgp).stem
35 | labelp = f'{self.dp}/labels/{trte}/{Path(imgp).stem}.txt'
36 | img = cv2.imread(imgp)
37 | h, w, c = img.shape
38 |
39 | with open(labelp, 'r') as f:
40 | label = f.readlines()
41 | label = np.array([x.split() for x in label], dtype=np.float32)
42 |
43 | classes = label[:, 0]
44 | bboxes = label[:, 1::]
45 | bboxes = xywh2xyxy(bboxes)
46 | for j in range(len(label)):
47 | cls = classes[j]
48 | bbox = bboxes[j]
49 | bbox[0] *= w
50 | bbox[1] *= h
51 | bbox[2] *= w
52 | bbox[3] *= h
53 | plot_one_box(bbox, img, label=f'{self.names[int(cls)]}', color=self.colors[int(cls)])
54 |
55 | print(f'imgs: {len(imgs)} stem: {stem} img_shape: {img.shape} lb: {label}')
56 | # cr = np.any(label[:, 0] == 1)
57 | crit = 'fogged' in stem if cf else True
58 | if crit:
59 | cv2.imshow('xx', img)
60 | if cv2.waitKey(0) == ord('q'):
61 | exit()
62 |
63 |
64 | if __name__ == '__main__':
65 | parser = argparse.ArgumentParser()
66 | parser.add_argument('-dp', '--dataset-path', type=str,
67 | help='path to dataset need to be checked. such as: xxx/train_data_v5_format',
68 | default='/home/zzd/datasets/crosswalk/train_data_v5_format')
69 | parser.add_argument('-trte', '--train_or_test', type=str,
70 | help='specify check train or test of YOLOv5 format',
71 | default='train')
72 | parser.add_argument('-cf', '--only-check-fog', action='store_true', help='only check fogged dataset')
73 |
74 | opt = parser.parse_args()
75 | cd = CheckDataset(dp=opt.dataset_path)
76 | cd(trte=opt.train_or_test, cf=opt.only_check_fog)
77 |
--------------------------------------------------------------------------------
/scripts/get_confusing_matrix.py:
--------------------------------------------------------------------------------
1 | """
2 | 针对crosswalk 1770测试集,计算混淆矩阵
3 | """
4 |
5 | import os
6 | import numpy as np
7 |
8 | import time
9 | from PIL import Image
10 | from pathlib import Path
11 | import re
12 |
13 |
14 | class GetConfusingMatrix(object):
15 | """
16 | for crosswalk detection.
17 | """
18 | def __init__(self, thresh=0.8, img_suffix='jpg'):
19 | self.thresh = thresh
20 | self.img_suffix = img_suffix
21 | self.classes = ['crosswalk']
22 |
23 | def analyse(self, thresh=None, v5_out=None, pn_dir=None):
24 | print(f"{Path(v5_out).stem:-^80}")
25 |
26 | self.thresh = thresh if thresh is not None else self.thresh
27 | # 获取detect data
28 | dt_data = self.get_detect_data(v5_out=v5_out)
29 | gt_data = self.get_ground_truth(pn_dir, num_dt=dt_data.shape[0]) # array [n, 2] 2:filestem, cls, cls0: 无斑马线,cls1: 有斑马线
30 | assert dt_data.shape[0] == gt_data.shape[0], \
31 | f'dt shape: {dt_data.shape[0]} is not compatible with gt shape: {gt_data.shape[0]}'
32 | # array [n_files, 100, 3] 100: 同一张图最大检测到的目标数目,3: filename, cls, conf
33 | assert len(self.classes) == 1
34 | for i, cls in enumerate(self.classes):
35 | # gt = gt_data[gt_data[:, 1] == str(i)]
36 | dt = np.array(dt_data[np.where(dt_data[:, :, 1] == str(i))], dtype=np.str_) # (n, 7) # 筛选出该类
37 | dt = dt[np.array(dt[:, 2], dtype=np.float32) > self.thresh]
38 | # new_dt = np.zeros((dt_data.shape[0], dt_data.shape[2])).astype(np.str) # (1770, 7)
39 | ret = self.cal_TPFPTNFN(cal_cls=self.classes[i], dt_data=dt, gt_data=gt_data)
40 | return ret
41 |
42 | def cal_TPFPTNFN(self, cal_cls, dt_data, gt_data):
43 | """
44 | 计算混淆矩阵。
45 | :param cal_cls: 需要计算的类别名称
46 | :param dt_data: 检测结果:[n, 7] n: 所有检测的图,7: img, cls, conf, xc, yc, w, h
47 | :param gt_data: 真值
48 | :return:
49 | """
50 | TP_count, FP_count, TN_count, FN_count = 0, 0, 0, 0
51 |
52 | dt_filenames = np.unique(dt_data[:, 0]).tolist()
53 | for i, (filename, gtcls) in enumerate(gt_data): #
54 | gtcls = int(gtcls)
55 | if gtcls == 1: # 1是有斑马线
56 | gt = True
57 | else:
58 | gt = False
59 |
60 | # filename, cls, conf = dt_data[i, :3]
61 | if filename in dt_filenames: # 检测到了,有斑马线
62 | dt = True
63 | else:
64 | dt = False
65 |
66 | if gt and dt:
67 | TP_count += 1
68 | elif gt and not dt:
69 | FN_count += 1
70 | elif not gt and dt:
71 | FP_count += 1
72 | elif not gt and not dt:
73 | TN_count += 1
74 | else:
75 | raise Exception(f'ERROR, gt:{gt} dt:{dt}')
76 | # print(filename, gt, dt)
77 | # if i > 100:
78 | # break
79 | confusing_matrix = np.array([TP_count, FP_count, FN_count, TN_count]).astype(np.int32)
80 | # print(confusing_matrix)
81 | # exit()
82 |
83 | # confusing_matrix = confusing_matrix/len(data)
84 | # print(confusing_matrix)
85 | alpha = 1e-10
86 |
87 | ACC = (TP_count + TN_count) / (TP_count + TN_count + FP_count + FN_count+alpha) # 准确率
88 | PPV = (TP_count) / (TP_count + FP_count+alpha) # 精确率
89 | TPR = (TP_count) / (TP_count + FN_count+alpha) # 召回率
90 | TNR = (TN_count) / (TN_count + FP_count+alpha) # 特异度
91 | F1_score = 2 * PPV * TPR / (PPV + TPR+alpha)
92 |
93 | cm = confusing_matrix
94 | string1 = f"{'cls':^15}{'ALL':<5}{'TP':<5}{'FP':<5}{'FN':<5}{'TN':<5}\n" \
95 | f"{cal_cls:^15}{len(dt_data):<5}{cm[0]:<5}{cm[1]:<5}{cm[2]:<5}{cm[3]:<5}"
96 | # print(f"ALL TP FP FN TN")
97 | # print(f"{len(dt_data)} {confusing_matrix}")
98 | print(string1)
99 | string2 = f'Thresh: {self.thresh} Accuracy: {ACC*100:.2f}% Precision: {PPV*100:.2f}% Recall: {TPR*100:.2f}% ' \
100 | f'Specificity: {TNR*100:.2f}% F1_score: {F1_score*100:.2f}%'
101 | print(string2)
102 | pnts3 = f"{len(dt_data):<5}{cm[0]:<5}{cm[1]:<5}{cm[2]:<5}{cm[3]:<5} {self.thresh} {ACC*100:.2f} " \
103 | f"{PPV*100:.2f} {TPR*100:.2f} {TNR*100:.2f} {F1_score*100:.2f}"
104 | print(pnts3)
105 | return pnts3
106 |
107 | def get_ground_truth(self, pn_dir, num_dt):
108 | with open(f'{pn_dir}/positive.txt', 'r') as f:
109 | positive = f.readlines()
110 | with open(f'{pn_dir}/negative.txt', 'r') as f:
111 | negative = f.readlines()
112 | positive = [x.strip('\n') for x in positive]
113 | negative = [x.strip('\n') for x in negative]
114 | gt0 = [[x, 0] for x in negative]
115 | gt1 = [[x, 1] for x in positive]
116 | gt = gt0+gt1
117 | if len(gt) == num_dt:
118 | pass
119 | elif len(gt) != num_dt and 2*len(gt) == num_dt:
120 | gt_fogged = [[f'fogged_{x}', cls] for x, cls in gt]
121 | gt += gt_fogged
122 | else:
123 | raise NameError(f'num_gt: {len(gt)} is not compatible with num_dt: {num_dt}')
124 |
125 | gt_data = np.array(gt)
126 | return gt_data
127 |
128 | def get_detect_data(self, v5_out=None):
129 | """
130 | :param v5_out:
131 | :return: dt_data, an array, shape(nfiles, 50, 2)
132 | nfiles: number of output files, 100 represents a maximum of 100 targets in an image.
133 | 2 represents class and confidence.
134 | dt_data[dt_data[:, :, 1] != -2],筛选有目标的jpg,filename会重复。shape, (n_detected_files, 3) 检测到的目标的图片数目。
135 | """
136 | files = os.listdir(v5_out)
137 | imgs = sorted([tmp.strip('\n') for tmp in files if tmp.endswith(self.img_suffix)])
138 | txts = [tmp.strip('\n') for tmp in files if tmp.endswith('.txt')]
139 |
140 | dt_data = np.zeros((len(imgs), 100, 7), dtype=np.object_) # 7: img cls conf bbox(xcycwh_percent)
141 | # 最多100个目标
142 | dt_data[:, :, :] = -2
143 | cls_c = np.zeros(100).astype(np.int32) # 分类count
144 | for i, img in enumerate(imgs):
145 | file_stem = Path(img).stem
146 | txt_file = f'{v5_out}/{file_stem}.txt'
147 | if os.path.exists(txt_file):
148 | with open(txt_file, 'r') as f:
149 | txt_data = f.readlines()
150 | # if len(txt_data) > 1:
151 | # print(img)
152 | for j, t in enumerate(txt_data): #
153 | cls, conf, xc, yc, w, h = t.split()
154 | # tmp = [img, int(cls), float(conf)]
155 | tmp = [img, int(cls), float(conf), float(xc), float(yc), float(w), float(h)]
156 | dt_data[i, j, :] = np.array(tmp)
157 | cls_c[int(cls)] += 1
158 | else:
159 | dt_data[i, 0, :] = np.array([img, -2, -2, -2, -2, -2, -2])
160 |
161 | cls_c = cls_c[cls_c[:] != 0]
162 | print(
163 | f'detected output: {len(imgs)} imgs[.{self.img_suffix}], {len(txts)} txts. {np.sum(cls_c)} detected targets.')
164 | prnts = '\n'.join([f'class{i}: {count} targets detected' for i, count in enumerate(cls_c)])
165 | print(prnts)
166 | return dt_data
167 |
168 | def detect(self):
169 | self.net.eval()
170 | os.system(r'rm -rf detect_ret/detect.txt')
171 | for i, filename in enumerate(self.cfg.filenames):
172 | t = time.time()
173 | img_path = f"{self.cfg.infer_img_dir}/{filename}"
174 | img = Image.open(img_path)
175 | img, outs = self.infer(img)
176 | # img.save(f'/Users/tanmenglu/Downloads/AutoDrive/ret/{filename}')
177 | print(f"{filename} time: {time.time()-t:.2f} FPS: {1/(time.time()-t):.2f}")
178 | strs = ''
179 | for ot in outs:
180 | strs += ' ' + ','.join(ot)
181 | os.system(f'echo "{filename}{strs}" >>detect_ret/detect.txt')
182 |
183 |
184 | def plot():
185 | import matplotlib.pyplot as plt
186 | threshes = [i / 100 for i in range(10, 91, 2)]
187 | xs = []
188 | ys = []
189 | for thresh in threshes:
190 | ret = GCM(thresh=thresh, v5_out=v5_out)
191 | print(ret)
192 | ret = ret.split('\n')[-2].split()
193 | xs.append((ret[1]))
194 | ys.append([ret[3].strip('%'), ret[5].strip('%'), ret[7].strip('%'), ret[9].strip('%'), ret[11].strip('%')])
195 |
196 | xs = np.array(xs).astype(np.float).reshape((-1, 1))
197 | xs = np.repeat(xs, 5, axis=1)
198 | ys = np.array(ys).astype(np.float)
199 | print(xs.shape, ys.shape)
200 | print(xs, ys)
201 | lbs = ['ACC', 'PPV', 'RECALL', 'TNR', 'F1_score']
202 | colors = [[np.random.randint(0, 255) for _ in range(3)] for _ in range(xs.shape[1])]
203 | colors = [[tmp/255 for tmp in tmp2] for tmp2 in colors]
204 | for i in range(xs.shape[1]):
205 | x = xs[:, i]
206 | y = ys[:, i]
207 | print(colors[i])
208 | plt.plot(x, y, label=lbs[i], color=colors[i])
209 |
210 | # 标出max:
211 | ys_max = np.max(ys, axis=0) # 每一列是一个种类,对每一列求最大值m
212 | ys_max_i = np.argmax(ys, axis=0) # [c1,c2,c3,c4,c5]
213 | xs_max = xs[:, 0][ys_max_i]
214 | print(xs_max, ys_max_i, ys_max)
215 | for i in range(len(xs_max)):
216 | x = xs_max[i]
217 | y = ys_max[i]
218 | plt.text(x, y, f"{lbs[i]} {x} {y}", color=colors[i])
219 | plt.plot(x, y, 'o', color=colors[i])
220 |
221 | plt.xticks()
222 | plt.legend(loc='best')
223 | plt.xlabel('thresh')
224 | plt.ylabel('precent/%')
225 | plt.savefig('xxx.png', bbox_inches='tight')
226 |
227 |
228 | if __name__ == "__main__":
229 | GCM = GetConfusingMatrix()
230 |
231 | path = "/home/zzd/datasets/crosswalk/testsets_1770"
232 | ret_all = []
233 | head = f"trs tes {'ALL':<5}{'TP':<5}{'FP':<5}{'FN':<5}{'TN':<5} thr acc P R TNR F1 {'time':<7} fps\n"
234 | ret_all.append(head)
235 | train_sizes = [640]
236 | test_sizes = range(128, 160, 32)
237 |
238 | exps = [
239 | 'm_ep300', 'SEm_NST0_fog0_ep100', 'SEm_NST1_fog0_ep100',
240 | 'SEm_NST1_fog0_ep300', 'SEm_NST1_fog1_ep100']
241 |
242 | for trs in train_sizes: # train_size
243 | for tes in test_sizes: # test_size
244 | v5_out = f"{path}/dense_ts_roi_SE300epoch_{trs}_{tes}_output"
245 | v5_out = f'{path}/SEv5m_300EP_no_roi'
246 | # v5_out = f'{path}/SEYOLOv5m_ep100_exp'
247 | # v5_out = f'{path}/m_ep300_sz640_ROI1_SSVM1'
248 | v5_out = f'{path}/SEm_NST0_fog0_ep100_sz640_ROI1_SSVM1'
249 |
250 | # v5_out = f'{path}/SEYOLOv5m_ep100_exp_sz640_roi1_ssvm1'
251 | # v5_out = f'{path}/SEYOLOv5m_ep100_exp_sz640_roi1_ssvm1'
252 | ret = GCM.analyse(thresh=0.5, v5_out=v5_out, pn_dir=path)
253 | # 读取inference时间
254 | with open(f'{v5_out}/detect.log', 'r') as f:
255 | log = f.readlines()
256 | time, fps = re.findall(f'\d+.\d+', log[-1])
257 | time = time[:-1]
258 | fps = fps[:-3]
259 | ret_all.append(f'{trs} {tes} {ret} {time} {fps}\n')
260 | with open('confusing_matrix_dense_roi_SE300epoch.txt', 'w') as f:
261 | f.writelines(ret_all)
262 |
--------------------------------------------------------------------------------
/scripts/post.bak.py:
--------------------------------------------------------------------------------
1 | """
2 | post for crosswalk detection, the vector crossing method used to detect the vehicle crossing crosswalk behavior
3 | 这个bak是添加抖动保护前的备份。20200929
4 | """
5 |
6 | import os
7 | import cv2
8 | import numpy as np
9 | import yaml
10 |
11 | __author__ = 'zhangzhengde@mail.sjtu.edu.cn'
12 | __date__ = '20200929'
13 |
14 |
15 | class DmPost(object):
16 | """damei post"""
17 | def __init__(self, opt):
18 | self.opt = opt
19 | self.crossout = self.init_crossout()
20 | self.vetors = self.init_vectors()
21 | self.logo = self.init_logo()
22 | self.rect = self.init_rect()
23 | self.t_inter = self.init_others()
24 | self.cl, self.rl = self.init_clrl()
25 |
26 | def init_crossout(self):
27 | crossout = np.zeros(
28 | (self.opt.field_size, 8))
29 | # 5 rows. 8 columns :out_index, detect_id, fileorder, is crosswalk exists, xc, yc, count,recording_flag
30 | crossout[:, :] = -2 # -2 represents undetected
31 | crossout[:, 0] = range(len(crossout)) # init out_index
32 | crossout[:, 6:8] = 0 # init count and recording_flag to 0
33 | return crossout
34 |
35 | def init_vectors(self):
36 | vector_size = 600 * 30 # maximun 600 seconds, sampling ratio 30。
37 | vectors = np.zeros((vector_size, 2)) # 2: store xc, yc
38 | vectors[:, :] = -2 # init
39 | return vectors
40 |
41 | def init_logo(self):
42 | logo_path = "others/SHNAVI.jpg"
43 | logo = cv2.imread(logo_path)
44 | imgs = [x for x in os.listdir(self.opt.source) if x.endswith('jpg') or x.endswith('png')]
45 | if len(imgs) == 0:
46 | raise NameError(f'did not get imgs from {self.opt.source}, support suffix: .jpg .png')
47 | img_size = cv2.imread(f'{self.opt.source}/{imgs[0]}').shape[:2]
48 | logo_size = logo.shape[:2] # 248, 1217
49 | scale_factor = 3.5
50 | resized_logo_size = (
51 | int(img_size[1] / scale_factor), int(img_size[1] / scale_factor * logo_size[0] / logo_size[1]))
52 | logo = cv2.resize(logo, resized_logo_size, interpolation=cv2.INTER_LINEAR)
53 | return logo
54 |
55 | def init_rect(self):
56 | rect = cv2.imread("others/rect.jpg")
57 | rect_resized_size = (self.logo.shape[1], 200)
58 | rect = cv2.resize(rect, rect_resized_size, interpolation=cv2.INTER_LINEAR)
59 | return rect
60 |
61 | def init_clrl(self):
62 | # get opt
63 | opt = self.opt
64 | with open(opt.control_line_setting, 'r') as f:
65 | conls = yaml.load(f, Loader=yaml.FullLoader)
66 | print(conls)
67 | cl = conls[opt.select_control_line] # control line
68 | rl = conls['red_line'] # red_line
69 | return cl, rl
70 |
71 | def init_others(self):
72 | FPS = 30 # 1s 有25张图
73 | sampling_ratio = 30 # 每4张采样1张
74 | sampling_rate = FPS / sampling_ratio # 采样率,1秒5张
75 | base_time = 0 # 基准时间,检测的图片超过chunk大小时,基准时间要增加
76 | t_inter = 1 / sampling_rate # time interpolation 每张0.2秒
77 | return t_inter
78 |
79 | def dmpost(self, img, det, det_id, filename, names, cls='crosswalk'):
80 | """
81 | :param img:
82 | :param det: None或tensor,shape:(n, 6), n是n个目标,6是已经scale到原图的x1,y1,x2,y2和confidence、class
83 | :param filename: filename0001.jpg获取order,或者输入0036_1,0036是视频名,1是第1帧。
84 | :param names: 模型中的类名
85 | :param cls: 后处理筛选的分类,默认是crosswalk
86 | :return:
87 | """
88 | opt = self.opt
89 | crossout = self.crossout
90 | vectors = self.vetors
91 |
92 | # 0000 0001 0002 0003 and so on # 1 2 3 4 ....
93 | fileorder = int(filename.split('.')[0][-4::]) if filename.endswith('jpg') else int(filename.split('_')[-1])
94 |
95 | # 绘制上下控制线
96 | # cl = opt.control_line # control line, [360, 700]
97 | cl = self.cl
98 | rl = self.rl
99 | lt = round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line thickness
100 | tf = max(lt - 1, 1) # 2
101 | h, w = img.shape[:2]
102 | top = [(0, cl[0]), (w, cl[0])]
103 | button = [(0, cl[1]), (w, cl[1])]
104 | middle = [(int(w / 4), int(cl[0]+(cl[1]-cl[0])*rl)), (int(3 * w / 4), int(cl[0]+(cl[1]-cl[0])*rl))]
105 | arrow = [(int(w / 2), middle[0][1]), (int(w / 2), middle[0][1] + 50)]
106 | textpos = (top[0][0] + 10, top[0][1] + 30)
107 |
108 | cv2.line(img, top[0], top[1], color=(30, 30, 224), thickness=lt)
109 | cv2.line(img, button[0], button[1], color=(30, 30, 224), thickness=lt)
110 | cv2.line(img, middle[0], middle[1], color=(120, 85, 220), thickness=lt) # 粉色
111 | cv2.arrowedLine(img, arrow[0], arrow[1], (120, 85, 220), 5, 8, 0, 0.3)
112 | cv2.putText(img, 'Control Line', textpos, 0, lt / 3, (30, 30, 224), thickness=tf, lineType=cv2.LINE_AA)
113 |
114 | cls_idx = names.index(cls) # class index i.e. 0 for crosswalk
115 | det_include_class = cls_idx in det[:, 5] if det is not None else False
116 | if det is not None and det_include_class:
117 | det = det.cpu().numpy()
118 | crosswalk = np.zeros((det[det[:, 5] == 0].shape[0], 8))
119 | # [n, 6]变成[n, 8],去掉cls。8分别是x1,y1,x2,y2,conf,cx,cy,is_in_control_line
120 | # print(det.shape, det[:, 5] == 1)
121 | crosswalk[:, :5] = det[det[:, 5] == 0][:, :5] # 筛选只要crosswalk,默认起序号为0, 0 1 2 3 4
122 | cx = (crosswalk[:, 0] + crosswalk[:, 2]) // 2
123 | cy = (crosswalk[:, 1] + crosswalk[:, 3]) // 2
124 | crosswalk[:, 5] = cx
125 | crosswalk[:, 6] = cy
126 | is_in_cl = (crosswalk[:, 6] > cl[0]) | (crosswalk[:, 6] < cl[1])
127 | crosswalk[:, 7] = is_in_cl
128 | if crosswalk.shape[0] > 1:
129 | # 同时检测到多条斑马线,根据之前记录的crossout,确定选择哪一条
130 | # lastxc, lastyc = crossout
131 | last_co = crossout[crossout[:, 3] != -2] # last crossout
132 | # print(last_co, last_co.shape)
133 | if len(last_co) == 0: # 都是空的,,那就使用第一条
134 | valid_idx = 0
135 | else: # 计算检测到的斑马线的中心与上次记录的中心哪个接近就用哪个
136 | lastcxy = last_co[-1][4:6]
137 | currentcxy = crosswalk[:, 5:7]
138 | # print(lastcxy, lastcxy.shape, currentcxy, currentcxy.shape)
139 | distances = np.sum(np.square(currentcxy - lastcxy), axis=1) # 距离
140 | valid_idx = np.argmin(distances)
141 | print(f'WANING: detect.py post, the detected crosswalk is more than one, use the {valid_idx+1} one')
142 | crosswalk = crosswalk[valid_idx, :].reshape(1, -1)
143 | det = det[valid_idx, :].reshape(1, -1)
144 | else:
145 | crosswalk = np.zeros((1, 8))
146 |
147 | # print(crosswalk.shape)
148 | # print(det.shape)
149 |
150 | if det_id < crossout.shape[0]: # detected_img id < 5
151 | # 该列更新值 n列:out_index, detect_id, fileorder, 有无斑马线, xc, yc
152 | crossout[det_id, 1] = det_id
153 | crossout[det_id, 2] = fileorder
154 | crossout[det_id, 3] = crosswalk[0, 7]
155 | crossout[det_id, 4:6] = crosswalk[0, 5: 7] # xc, yc
156 | index = det_id
157 | else:
158 | crossout[0:-1:, 1::] = crossout[1::, 1::] # 除了序号列的所有行向上平移一格
159 | # 最后一列更新值
160 | crossout[-1, 1] = det_id
161 | crossout[-1, 2] = fileorder
162 | crossout[-1, 3] = crosswalk[0, 7]
163 | crossout[-1, 4:6] = crosswalk[0, 5: 7] # xc, yc
164 | index = len(crossout) - 1
165 |
166 | # print(crossout[:11, :])
167 | exist, vector, scale = self.decode_crossout(crossout, index)
168 | recording = crossout[index, 7]
169 |
170 | if recording == 1 and vector is not None:
171 | vectors[opt.v_idx, :] = vector[0]
172 | vectors[opt.v_idx + 1, :] = vector[1]
173 | opt.v_idx += 1
174 | elif recording == 1 and vector is None:
175 | pass
176 | else:
177 | vectors[:, :] = -2
178 | opt.v_idx = 0
179 |
180 | speed = None if scale is None else float((vector[1][1] - vector[0][1]) / (self.t_inter * scale))
181 | # 向量的y距离除以scale再除以time interpolate
182 |
183 | # 绘制logo和结果
184 | # logo_pos = (int(img.shape[1]/2-logo.shape[1]/2), cl[1]-logo.shape[0])
185 | logo_pos = (20, 20) # w h
186 | img = self.imgAdd(self.logo, img, logo_pos[1], logo_pos[0], alpha=0.5)
187 | rect_pos = (20, 20 + 5 + self.logo.shape[0])
188 | img = self.imgAdd(self.rect, img, rect_pos[1], rect_pos[0], alpha=0.5)
189 |
190 | pos = (20 + 20, 20 + 5 + self.logo.shape[0] + 20 + 30)
191 | self.imgputText(img, f'crosswalk: {exist}', pos, lt, tf)
192 | pos = (20 + 20, 20 + 5 + self.logo.shape[0] + 20 + 30 + 40)
193 | self.imgputText(img, f'speed: {speed:.2f}', pos, lt, tf) if speed is not None else None
194 | self.imgputText(img, f'speed: {speed}', pos, lt, tf) if speed is None else None
195 |
196 | vt = vectors[vectors[:, 0] != -2].astype(np.int) # 筛选t
197 | self.imgplotVectors(img, vt) if vector is not None else None
198 |
199 | # 计数,vectors有值、向量经过中间线、当前vector不为None时,count+1
200 | crossout[index, 6] = crossout[index - 1, 6] # 先同步count与前一个相同
201 | if vt.shape[0] != 0:
202 | # print(vt.shape)
203 | if vt[0, 1] < np.mean(cl):
204 | intersect = vt[vt[:, 1] > middle[0][1]]
205 | # print('inter', intersect)
206 | if intersect.shape[0] == 1 and vector is not None:
207 | crossout[index, 6] += 1 # count+1
208 | prt_str = f'\n{filename} count+1, conf: {crosswalk[0, 4]:.2f} count: {int(crossout[index, 6])}'
209 | print(prt_str)
210 | os.system(f'echo "{prt_str}" >> {opt.output}/detect.log')
211 | count = int(crossout[index, 6])
212 |
213 | pos = (20 + 20, 20 + 5 + self.logo.shape[0] + 20 + 30 + 40 + 40)
214 | self.imgputText(img, f'count: {count}', pos, lt, tf)
215 |
216 | # 打印状态:有vectors时就是crossing
217 | pos = (20 + 20, 20 + 5 + self.logo.shape[0] + 20 + 30 + 40 + 40 + 40)
218 | status = 'No crosswalk' if vt.shape[0] == 0 else 'Crossing'
219 | self.imgputText(img, f'status: {status}', pos, lt, tf)
220 |
221 | prt_str = f'{filename} detect_id: {det_id} speed: {speed} count: {count} status: {status}'
222 | print(prt_str)
223 | os.system(f'echo "{prt_str}" >> {opt.output}/detect.log')
224 |
225 | return img
226 |
227 | def decode_crossout(self, crossout, index, vector_threash=20, vector_max_threash=600):
228 | """
229 | 解码crossout, 输出当前图是否有斑马线,斑马线位移矢量,时间尺度(索引的间距)
230 | count计数算法:该矢量经过中心线时,计数+1。
231 | :param crossout:
232 | :index: 当前位置索引
233 | :recursive_bits: 向前递归位数
234 | :vector_threash: 向量的模的阈值,大于该阈值且y的变化为负数时,设置recording为0,断开count。
235 | :return:
236 | """
237 | exist = crossout[index, 3]
238 | co = crossout[crossout[:, 3] == 1] # 所有存在的行
239 | if exist == 0:
240 | if co.shape[0] == 0: # 感受野范围内都没有斑马线
241 | crossout[:, 7] = 0 # recording置0
242 | return False, None, None
243 | else:
244 | if co.shape[0] == 1: # 只有最后一行有
245 | crossout[:, 7] = 0 # recording置0
246 | return False, None, None
247 | else:
248 | scale = co[-1, 1] - co[-2, 1] # detected_id的差
249 | vector = [co[-2, 4:6], co[-1, 4:6]] # 2个点 ((xc1, yc1), (xc2, yc2))
250 | vector2 = vector[1] - vector[0] # (x2-x1) (y2-y1)
251 | length = np.sqrt(vector2[0] ** 2 + vector2[1] ** 2)
252 | y_shift = vector2[1]
253 | # print(vector2, vector2/1280, vector2/720, length, y_shift)
254 | if length > vector_threash and y_shift < 0:
255 | crossout[:, 7] = 0 # recording置0
256 | elif length > vector_max_threash: # 有时候会出现超级长的大于300像素,筛掉,约((680-400)*2/3)**2 680和400是控制线
257 | crossout[:, 7] = 0
258 | else:
259 | crossout[:, 7] = 1 # recording置1
260 | # print(crossout[:, 7], length)
261 | return True, vector, scale
262 |
263 | def imgAdd(self, small_img, big_image, x, y, alpha=0.5):
264 | """
265 | 把小图贴到大图的xy位置,透明度设置为0.5
266 | """
267 | row, col = small_img.shape[:2]
268 | if small_img.shape[0] > big_image.shape[0] or small_img.shape[1] > big_image.shape[1]:
269 | raise NameError(f'imgAdd, the size of small img bigger than big img.')
270 | roi = big_image[x:x + row, y:y + col, :]
271 | roi = cv2.addWeighted(small_img, alpha, roi, 1 - alpha, 0)
272 | big_image[x:x + row, y:y + col] = roi
273 | return big_image
274 |
275 | def imgputText(self, img, txt, pos, lt, tf):
276 | cv2.putText(img, txt, pos, 0, lt / 3, (30, 30, 224), thickness=tf, lineType=cv2.LINE_AA)
277 |
278 | def imgplotVectors(self, img, vt):
279 | if vt.shape[0] == 0:
280 | return
281 | for i in range(vt.shape[0] - 1):
282 | pt1, pt2 = tuple(vt[i]), tuple(vt[i + 1])
283 | cv2.arrowedLine(img, pt1, pt2, (0, 0, 255), 5, 8, 0, 0.3)
284 |
--------------------------------------------------------------------------------
/scripts/pt2wts.py:
--------------------------------------------------------------------------------
1 | """
2 | convert xxx.pt to xxx.wts
3 |
4 | need install damei library:
5 | pip install damei -i https://pypi.Python.org/simple
6 | """
7 | import damei as dm
8 | import argparse
9 |
10 | parser = argparse.ArgumentParser()
11 | parser.add_argument(
12 | 'weights', type=str, default='runs/exp0/weights/best.pt',
13 | help='path to weights [xx.pt]')
14 | parser.add_argument('--output_dir', type=str, default=None)
15 | opt = parser.parse_args()
16 |
17 | dm.post.pt2wts(opt.weights, opt.output_dir)
18 |
19 |
--------------------------------------------------------------------------------
/scripts/synthetic_fog.py:
--------------------------------------------------------------------------------
1 | """
2 | 直接运行程序可以测试合成雾气效果
3 | Produced by: zhangzhengde@sjtu.edu.cn
4 | """
5 | import os
6 | import math
7 | import cv2
8 | import time
9 | from pathlib import Path
10 | import numpy as np
11 | import matplotlib.pyplot as plt
12 | from tqdm import tqdm
13 | import shutil
14 |
15 |
16 | class SyntheticFog(object):
17 | def __init__(self):
18 | pass
19 |
20 | def __call__(self, show=False):
21 | img_path = '../example/fog_image/raw.jpg'
22 | # img_path = '../sources/IMG_6685.JPG'
23 | assert os.path.exists(img_path), f'error: img does not exists, {img_path}'
24 | img = cv2.imread(img_path)
25 | print(img.shape)
26 | img = img/255.0
27 | print(f'fogging...')
28 | t0 = time.time()
29 | br = 0.7
30 | th = 0.05
31 | fogged_img = self.fogging_img(
32 | img, brightness=br, thickness=th,
33 | high_efficiency=True)
34 | print(f'fogging time: {(time.time()-t0)*1000:.4f}ms')
35 | rf = 1 # resize factor
36 | img = cv2.resize(img, (int(img.shape[1]*rf), int(img.shape[0]*rf)))
37 | fogged_img = cv2.resize(fogged_img, ((int(fogged_img.shape[1]*rf)), (int(fogged_img.shape[0]*rf))))
38 | fogged_img = np.array(fogged_img*255, dtype=np.uint8)
39 | if show:
40 | cv2.imshow('src', img)
41 | cv2.imshow('fogged', fogged_img)
42 | cv2.waitKey(0)
43 | cv2.imwrite(f'../example/fog_image/fogged_br{br}_th{th}.jpg', fogged_img)
44 |
45 | def fogging_dir(self, sp, tp=None, random_params=True, brightness=None, thickness=None, save_src_img=False):
46 | """
47 | fogging images in a directory
48 | :param sp: str, source dir path
49 | :param tp: str, target dir path, tp is fogged_{sp} by default
50 | :param random_params: bool, use random brightness and fog thickness params if True
51 | :param brightness: float, 0.1 to 0.9, gray of synthetic fog, pure white fog if 1, dark fog if 0.
52 | :param thickness: float, 0.01 to 0.09, thickness of synthetic fog, the larger the value, the thicker the fog.
53 | :param save_src_img: save source image at the same time i.e. copy source imgs to tp
54 | :return: None, all fogged images will be saved to target dir path.
55 | """
56 | tp = tp if tp is not None else f'{Path(sp).parent}/fogged_{Path(sp).name}'
57 | if os.path.exists(tp):
58 | ipt = input(f'Target dir: {tp} exists, do you want to remove it and continue. [Yes]/No: ')
59 | if ipt in ['', 'Yes', 'Y', 'yes']:
60 | shutil.rmtree(tp)
61 | else:
62 | print('do nothing')
63 | exit()
64 | os.makedirs(f'{tp}')
65 |
66 | imgs = [x for x in os.listdir(sp) if str(Path(x).suffix).lower() in ['.jpg', '.bmp']]
67 | print(f'Fogging {len(imgs)} images in dir {sp}, \nfogged images will be save to {tp}.')
68 | bar = tqdm(imgs)
69 | for i, img_name in enumerate(bar):
70 | img_path = f'{sp}/{img_name}'
71 | # stem = Path(img_path).stem
72 | # suffix = Path(img_path).suffix
73 |
74 | if save_src_img: # save source img
75 | shutil.copy(f'{sp}/{img_name}', f'{tp}/{img_name}')
76 |
77 | img = cv2.imread(img_path)
78 | h, w, c = img.shape
79 | normed_img = img.copy()/255.0
80 |
81 | if random_params:
82 | br = np.clip(0.2 * np.random.randn() + 0.5, 0.1, 0.9) # 0.1~0.9
83 | th = np.clip(0.01 * np.random.randn() + 0.05, 0.01, 0.09)
84 | else:
85 | br = brightness
86 | th = thickness
87 | assert br is not None
88 | assert th is not None
89 | fogged_img = self.fogging_img(normed_img, br, th, high_efficiency=True)
90 | fogged_img = np.array(fogged_img * 255, dtype=np.uint8)
91 | cv2.imwrite(f'{tp}/fogged_{img_name}', fogged_img)
92 |
93 | bar.set_description(f'Fogged image saved, fogged_{img_name}')
94 |
95 | def fogging_img(self, img, brightness=0.7, thickness=0.05, high_efficiency=True):
96 | """
97 | fogging single image
98 | :param img: src img
99 | :param brightness: brightness
100 | :param thickness: fog thickness, without fog when 0, max 0.1,
101 | :param high_efficiency: use matrix to improve fogging speed when high_efficiency is True, else use loops
102 | low efficiency: about 4000ms, high efficiency: about 80ms, tested in (864, 1152, 3) img
103 | :return: fogged image
104 | """
105 | assert 0 <= brightness <= 1
106 | assert 0 <= thickness <= 0.1
107 | fogged_img = img.copy()
108 | h, w, c = fogged_img.shape
109 | if not high_efficiency: # use default loop to fogging, low efficiency
110 | size = np.sqrt(np.max(fogged_img.shape[:2])) # 雾化尺寸
111 | center = (h // 2, w // 2) # 雾化中心
112 | # print(f'shape: {img.shape} center: {center} size: {size}') # 33
113 | # d_list = []
114 | for j in range(h):
115 | for l in range(w):
116 | d = -0.04 * math.sqrt((j - center[0]) ** 2 + (l - center[1]) ** 2) + size
117 | # print(f'd {d}')
118 | td = math.exp(-thickness * d)
119 | # d_list.append(td)
120 | fogged_img[j][l][:] = fogged_img[j][l][:] * td + brightness * (1 - td)
121 | # x = np.arange(len(d_list))
122 | # plt.plot(x, d_list, 'o')
123 | # if j == 5:
124 | # break
125 | else: # use matrix # TODO: 直接使用像素坐标,距离参数不适用于大分辨率图像,会变成鱼眼镜头的样子. done.
126 | use_pixel = True
127 | size = np.sqrt(np.max(fogged_img.shape[:2])) if use_pixel else 1 # 雾化尺寸
128 | h, w, c = fogged_img.shape
129 | hc, wc = h // 2, w // 2
130 | mask = self.get_mask(h=h, w=w, hc=hc, wc=wc, pixel=use_pixel) # (h, w, 2)
131 | d = -0.04 * np.linalg.norm(mask, axis=2) + size
132 |
133 | td = np.exp(-thickness * d)
134 |
135 | for cc in range(c):
136 | fogged_img[..., cc] = fogged_img[..., cc] * td + brightness*(1-td)
137 |
138 | # a = np.linalg.norm(mask, axis=2)
139 | # print(f'size: {fogged_img.shape} a: {a} max: {np.max(fogged_img)} {np.min(fogged_img)}')
140 |
141 | fogged_img = np.clip(fogged_img, 0, 1) # 解决黑白噪点的问题
142 | # print(f'mask: {mask[:, :, 1]} {mask.shape}')
143 | # print(f'd: {d} {d.shape}')
144 |
145 | return fogged_img
146 |
147 | def get_mask(self, h, w, hc, wc, pixel=True):
148 | mask = np.zeros((h, w, 2), dtype=np.float32)
149 | if pixel:
150 | mask[:, :, 0] = np.repeat(np.arange(h).reshape((h, 1)), w, axis=1) - hc
151 | mask[:, :, 1] = np.repeat(np.arange(w).reshape((1, w)), h, axis=0) - wc
152 | else:
153 | mask[:, :, 0] = np.repeat(np.linspace(0, 1, h).reshape(h, 1), w, axis=1) - 0.5
154 | mask[:, :, 1] = np.repeat(np.linspace(0, 1, w).reshape((1, w)), h, axis=0) - 0.5
155 | return mask
156 |
157 |
158 | if __name__ == '__main__':
159 | synf = SyntheticFog()
160 | synf(show=True)
161 |
162 |
163 |
164 |
--------------------------------------------------------------------------------
/scripts/yolo2coco.py:
--------------------------------------------------------------------------------
1 | """
2 | 转换训练集,YOLOv5 format to COCO format
3 | """
4 | import os, sys
5 | sys.path.append(f'/home/zzd/PycharmProject/damei')
6 | import damei as dm
7 |
8 | sp = '/home/zzd/datasets/crosswalk/fogged_train_data_v5_format'
9 | tp = "/home/zzd/datasets/crosswalk/fogged_train_data_coco_format"
10 | dm.tools.yolo2coco(sp=sp, tp=tp)
11 |
12 |
--------------------------------------------------------------------------------
/utils/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangzhengde0225/CDNet/6ffc9a6a28398ecea78c05bd38cc37dc4ce6a252/utils/__init__.py
--------------------------------------------------------------------------------
/utils/google_utils.py:
--------------------------------------------------------------------------------
1 | # This file contains google utils: https://cloud.google.com/storage/docs/reference/libraries
2 | # pip install --upgrade google-cloud-storage
3 | # from google.cloud import storage
4 |
5 | import os
6 | import time
7 | from pathlib import Path
8 |
9 |
10 | def attempt_download(weights):
11 | # Attempt to download pretrained weights if not found locally
12 | weights = weights.strip().replace("'", '')
13 | msg = weights + ' missing, try downloading from https://github.com/zhangzhengde0225/CDNet/blob/master/docs/MODEL_ZOO.md'
14 |
15 | r = 1 # return
16 | if len(weights) > 0 and not os.path.isfile(weights):
17 | d = {'yolov3-spp.pt': '1mM67oNw4fZoIOL1c8M3hHmj66d8e-ni_', # yolov3-spp.yaml
18 | 'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO', # yolov5s.yaml
19 | 'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr', # yolov5m.yaml
20 | 'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV', # yolov5l.yaml
21 | 'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS', # yolov5x.yaml
22 | }
23 |
24 | file = Path(weights).name
25 | if file in d:
26 | r = gdrive_download(id=d[file], name=weights)
27 |
28 | if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
29 | os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
30 | s = "curl -L -o %s 'storage.googleapis.com/ultralytics/yolov5/ckpt/%s'" % (weights, file)
31 | r = os.system(s) # execute, capture return values
32 |
33 | # Error check
34 | if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
35 | os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
36 | raise Exception(msg)
37 |
38 |
39 | def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'):
40 | # Downloads a file from Google Drive, accepting presented query
41 | # from utils.google_utils import *; gdrive_download()
42 | t = time.time()
43 |
44 | print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='')
45 | os.remove(name) if os.path.exists(name) else None # remove existing
46 | os.remove('cookie') if os.path.exists('cookie') else None
47 |
48 | # Attempt file download
49 | os.system("curl -c ./cookie -s -L \"drive.google.com/uc?export=download&id=%s\" > /dev/null" % id)
50 | if os.path.exists('cookie'): # large file
51 | s = "curl -Lb ./cookie \"drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=%s\" -o %s" % (
52 | id, name)
53 | else: # small file
54 | s = 'curl -s -L -o %s "drive.google.com/uc?export=download&id=%s"' % (name, id)
55 | r = os.system(s) # execute, capture return values
56 | os.remove('cookie') if os.path.exists('cookie') else None
57 |
58 | # Error check
59 | if r != 0:
60 | os.remove(name) if os.path.exists(name) else None # remove partial
61 | print('Download error ') # raise Exception('Download error')
62 | return r
63 |
64 | # Unzip if archive
65 | if name.endswith('.zip'):
66 | print('unzipping... ', end='')
67 | os.system('unzip -q %s' % name) # unzip
68 | os.remove(name) # remove zip to free space
69 |
70 | print('Done (%.1fs)' % (time.time() - t))
71 | return r
72 |
73 |
74 | # def upload_blob(bucket_name, source_file_name, destination_blob_name):
75 | # # Uploads a file to a bucket
76 | # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
77 | #
78 | # storage_client = storage.Client()
79 | # bucket = storage_client.get_bucket(bucket_name)
80 | # blob = bucket.blob(destination_blob_name)
81 | #
82 | # blob.upload_from_filename(source_file_name)
83 | #
84 | # print('File {} uploaded to {}.'.format(
85 | # source_file_name,
86 | # destination_blob_name))
87 | #
88 | #
89 | # def download_blob(bucket_name, source_blob_name, destination_file_name):
90 | # # Uploads a blob from a bucket
91 | # storage_client = storage.Client()
92 | # bucket = storage_client.get_bucket(bucket_name)
93 | # blob = bucket.blob(source_blob_name)
94 | #
95 | # blob.download_to_filename(destination_file_name)
96 | #
97 | # print('Blob {} downloaded to {}.'.format(
98 | # source_blob_name,
99 | # destination_file_name))
100 |
--------------------------------------------------------------------------------
/utils/torch_utils.py:
--------------------------------------------------------------------------------
1 | import math
2 | import os
3 | import time
4 | from copy import deepcopy
5 |
6 | import torch
7 | import torch.backends.cudnn as cudnn
8 | import torch.nn as nn
9 | import torch.nn.functional as F
10 | import torchvision.models as models
11 |
12 |
13 | def init_seeds(seed=0):
14 | torch.manual_seed(seed)
15 |
16 | # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html
17 | if seed == 0: # slower, more reproducible
18 | cudnn.deterministic = True
19 | cudnn.benchmark = False
20 | else: # faster, less reproducible
21 | cudnn.deterministic = False
22 | cudnn.benchmark = True
23 |
24 |
25 | def select_device(device='', batch_size=None):
26 | # device = 'cpu' or '0' or '0,1,2,3'
27 | cpu_request = device.lower() == 'cpu'
28 | if device and not cpu_request: # if device requested other than 'cpu'
29 | os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
30 | assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device # check availablity
31 |
32 | cuda = False if cpu_request else torch.cuda.is_available()
33 | if cuda:
34 | c = 1024 ** 2 # bytes to MB
35 | ng = torch.cuda.device_count()
36 | # zzd k80
37 | # ng = 2
38 | if ng > 1 and batch_size: # check that batch_size is compatible with device_count
39 | assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng)
40 | x = [torch.cuda.get_device_properties(i) for i in range(ng)]
41 | s = 'Using CUDA '
42 | for i in range(0, ng):
43 | if i == 1:
44 | s = ' ' * len(s)
45 | print("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" %
46 | (s, i, x[i].name, x[i].total_memory / c))
47 | else:
48 | print('Using CPU')
49 |
50 | print('') # skip a line
51 | return torch.device('cuda:0' if cuda else 'cpu')
52 |
53 |
54 | def time_synchronized():
55 | torch.cuda.synchronize() if torch.cuda.is_available() else None
56 | return time.time()
57 |
58 |
59 | def is_parallel(model):
60 | # is model is parallel with DP or DDP
61 | return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel)
62 |
63 |
64 | def initialize_weights(model):
65 | for m in model.modules():
66 | t = type(m)
67 | if t is nn.Conv2d:
68 | pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
69 | elif t is nn.BatchNorm2d:
70 | m.eps = 1e-3
71 | m.momentum = 0.03
72 | elif t in [nn.LeakyReLU, nn.ReLU, nn.ReLU6]:
73 | m.inplace = True
74 |
75 |
76 | def find_modules(model, mclass=nn.Conv2d):
77 | # finds layer indices matching module class 'mclass'
78 | return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)]
79 |
80 |
81 | def sparsity(model):
82 | # Return global model sparsity
83 | a, b = 0., 0.
84 | for p in model.parameters():
85 | a += p.numel()
86 | b += (p == 0).sum()
87 | return b / a
88 |
89 |
90 | def prune(model, amount=0.3):
91 | # Prune model to requested global sparsity
92 | import torch.nn.utils.prune as prunes
93 | print('Pruning model... ', end='')
94 | for name, m in model.named_modules():
95 | if isinstance(m, nn.Conv2d):
96 | prune.l1_unstructured(m, name='weight', amount=amount) # prune
97 | prune.remove(m, 'weight') # make permanent
98 | print(' %.3g global sparsity' % sparsity(model))
99 |
100 |
101 | def fuse_conv_and_bn(conv, bn):
102 | # https://tehnokv.com/posts/fusing-batchnorm-and-conv/
103 | with torch.no_grad():
104 | # init
105 | fusedconv = nn.Conv2d(conv.in_channels,
106 | conv.out_channels,
107 | kernel_size=conv.kernel_size,
108 | stride=conv.stride,
109 | padding=conv.padding,
110 | bias=True).to(conv.weight.device)
111 |
112 | # prepare filters
113 | w_conv = conv.weight.clone().view(conv.out_channels, -1)
114 | w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var)))
115 | fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size()))
116 |
117 | # prepare spatial bias
118 | b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias
119 | b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps))
120 | fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn)
121 |
122 | return fusedconv
123 |
124 |
125 | def model_info(model, verbose=False):
126 | # Plots a line-by-line description of a PyTorch model
127 | n_p = sum(x.numel() for x in model.parameters()) # number parameters
128 | n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients
129 | if verbose:
130 | print('%5s %40s %9s %12s %20s %10s %10s' % ('layer', 'name', 'gradient', 'parameters', 'shape', 'mu', 'sigma'))
131 | for i, (name, p) in enumerate(model.named_parameters()):
132 | name = name.replace('module_list.', '')
133 | print('%5g %40s %9s %12g %20s %10.3g %10.3g' %
134 | (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std()))
135 |
136 | try: # FLOPS
137 | from thop import profile
138 | flops = profile(deepcopy(model), inputs=(torch.zeros(1, 3, 64, 64),), verbose=False)[0] / 1E9 * 2
139 | fs = ', %.1f GFLOPS' % (flops * 100) # 640x640 FLOPS
140 | except:
141 | fs = ''
142 |
143 | print('Model Summary: %g layers, %g parameters, %g gradients%s' % (len(list(model.parameters())), n_p, n_g, fs))
144 |
145 |
146 | def load_classifier(name='resnet101', n=2):
147 | # Loads a pretrained model reshaped to n-class output
148 | model = models.__dict__[name](pretrained=True)
149 |
150 | # Display model properties
151 | input_size = [3, 224, 224]
152 | input_space = 'RGB'
153 | # input_space = ['RGB']
154 | input_range = [0, 1]
155 | mean = [0.485, 0.456, 0.406]
156 | std = [0.229, 0.224, 0.225]
157 | for x in [input_size, input_space, input_range, mean, std]:
158 | print(x + ' =', eval(x))
159 |
160 | # Reshape output to n classes
161 | filters = model.fc.weight.shape[1]
162 | model.fc.bias = nn.Parameter(torch.zeros(n), requires_grad=True)
163 | model.fc.weight = nn.Parameter(torch.zeros(n, filters), requires_grad=True)
164 | model.fc.out_features = n
165 | return model
166 |
167 |
168 | def scale_img(img, ratio=1.0, same_shape=False): # img(16,3,256,416), r=ratio
169 | # scales img(bs,3,y,x) by ratio
170 | if ratio == 1.0:
171 | return img
172 | else:
173 | h, w = img.shape[2:]
174 | s = (int(h * ratio), int(w * ratio)) # new size
175 | img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize
176 | if not same_shape: # pad/crop img
177 | gs = 32 # (pixels) grid size
178 | h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)]
179 | return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean
180 |
181 |
182 | def copy_attr(a, b, include=(), exclude=()):
183 | # Copy attributes from b to a, options to only include [...] and to exclude [...]
184 | for k, v in b.__dict__.items():
185 | if (len(include) and k not in include) or k.startswith('_') or k in exclude:
186 | continue
187 | else:
188 | setattr(a, k, v)
189 |
190 |
191 | class ModelEMA:
192 | """ Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models
193 | Keep a moving average of everything in the model state_dict (parameters and buffers).
194 | This is intended to allow functionality like
195 | https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage
196 | A smoothed version of the weights is necessary for some training schemes to perform well.
197 | This class is sensitive where it is initialized in the sequence of model init,
198 | GPU assignment and distributed training wrappers.
199 | """
200 |
201 | def __init__(self, model, decay=0.9999, updates=0):
202 | # Create EMA
203 | self.ema = deepcopy(model.module if is_parallel(model) else model).eval() # FP32 EMA
204 | # if next(model.parameters()).device.type != 'cpu':
205 | # self.ema.half() # FP16 EMA
206 | self.updates = updates # number of EMA updates
207 | self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) # decay exponential ramp (to help early epochs)
208 | for p in self.ema.parameters():
209 | p.requires_grad_(False)
210 |
211 | def update(self, model):
212 | # Update EMA parameters
213 | with torch.no_grad():
214 | self.updates += 1
215 | d = self.decay(self.updates)
216 |
217 | msd = model.module.state_dict() if is_parallel(model) else model.state_dict() # model state_dict
218 | for k, v in self.ema.state_dict().items():
219 | if v.dtype.is_floating_point:
220 | v *= d
221 | v += (1. - d) * msd[k].detach()
222 |
223 | def update_attr(self, model, include=(), exclude=('process_group', 'reducer')):
224 | # Update EMA attributes
225 | copy_attr(self.ema, model, include, exclude)
226 |
--------------------------------------------------------------------------------