├── Proposed Algorithm ├── utils │ ├── __init__.py │ ├── __pycache__ │ │ ├── utils.cpython-37.pyc │ │ ├── utils.cpython-38.pyc │ │ ├── __init__.cpython-35.pyc │ │ ├── __init__.cpython-37.pyc │ │ ├── __init__.cpython-38.pyc │ │ ├── datasets.cpython-35.pyc │ │ ├── datasets.cpython-37.pyc │ │ ├── datasets.cpython-38.pyc │ │ ├── torch_utils.cpython-37.pyc │ │ ├── torch_utils.cpython-38.pyc │ │ ├── google_utils.cpython-37.pyc │ │ └── google_utils.cpython-38.pyc │ ├── activations.py │ ├── google_utils.py │ └── torch_utils.py ├── __pycache__ │ ├── test.cpython-37.pyc │ └── test.cpython-38.pyc ├── inference │ └── prev │ │ ├── Japan_000007.jpg │ │ ├── Japan_000016.jpg │ │ ├── Japan_000021.jpg │ │ ├── Japan_000047.jpg │ │ ├── Japan_000052.jpg │ │ ├── Japan_000057.jpg │ │ ├── Japan_000060.jpg │ │ ├── Japan_000061.jpg │ │ ├── Japan_000094.jpg │ │ ├── Japan_000134.jpg │ │ ├── Japan_000141.jpg │ │ ├── Japan_000147.jpg │ │ ├── Japan_000148.jpg │ │ ├── Japan_000149.jpg │ │ ├── Japan_000157.jpg │ │ ├── Japan_000159.jpg │ │ ├── Japan_000161.jpg │ │ ├── Japan_000173.jpg │ │ ├── Japan_000174.jpg │ │ ├── Japan_000185.jpg │ │ ├── Japan_000195.jpg │ │ ├── Japan_000206.jpg │ │ ├── Japan_000223.jpg │ │ ├── Japan_000226.jpg │ │ ├── Japan_000236.jpg │ │ ├── Japan_000239.jpg │ │ ├── Japan_000248.jpg │ │ ├── Japan_000249.jpg │ │ ├── Japan_000280.jpg │ │ ├── Japan_000288.jpg │ │ ├── Japan_000293.jpg │ │ ├── Japan_000299.jpg │ │ ├── Japan_000321.jpg │ │ ├── Japan_000342.jpg │ │ ├── Japan_000344.jpg │ │ ├── Japan_000346.jpg │ │ ├── Japan_000367.jpg │ │ ├── Japan_000411.jpg │ │ ├── Japan_000417.jpg │ │ ├── Japan_000443.jpg │ │ ├── Japan_000451.jpg │ │ ├── Japan_000470.jpg │ │ ├── Japan_000475.jpg │ │ ├── Japan_000479.jpg │ │ ├── Japan_000482.jpg │ │ ├── Japan_000497.jpg │ │ ├── Japan_000501.jpg │ │ ├── Japan_000514.jpg │ │ ├── Japan_000524.jpg │ │ ├── Japan_000541.jpg │ │ ├── Japan_000542.jpg │ │ ├── Japan_000543.jpg │ │ ├── Japan_000549.jpg │ │ ├── Japan_000563.jpg │ │ ├── Japan_000569.jpg │ │ ├── Japan_000595.jpg │ │ ├── Japan_000606.jpg │ │ ├── Japan_000621.jpg │ │ ├── Japan_000624.jpg │ │ ├── Japan_000640.jpg │ │ ├── Japan_000647.jpg │ │ ├── Japan_000674.jpg │ │ ├── Japan_000678.jpg │ │ ├── Japan_000686.jpg │ │ ├── Japan_000714.jpg │ │ ├── Japan_000715.jpg │ │ ├── Japan_000730.jpg │ │ ├── Japan_000746.jpg │ │ ├── Japan_000747.jpg │ │ ├── Japan_000753.jpg │ │ ├── Japan_000768.jpg │ │ └── Japan_000769.jpg ├── models │ ├── __pycache__ │ │ ├── yolo.cpython-37.pyc │ │ ├── yolo.cpython-38.pyc │ │ ├── common.cpython-37.pyc │ │ ├── common.cpython-38.pyc │ │ ├── experimental.cpython-37.pyc │ │ └── experimental.cpython-38.pyc │ ├── yolov5l.yaml │ ├── yolov5l_pave.yaml │ ├── yolov5m.yaml │ ├── yolov5s.yaml │ ├── yolov5s_di.yaml │ ├── yolov5x.yaml │ ├── yolov5m_pave.yaml │ ├── yolov5s_cctv.yaml │ ├── yolov5x_india.yaml │ ├── yolov5x_pave.yaml │ ├── onnx_export.py │ ├── yolov3-spp.yaml │ ├── experimental.py │ ├── common.py │ └── yolo.py ├── main_run_di.py ├── data │ ├── cctv.yaml │ ├── cctv (copy).yaml │ ├── pave_india.yaml │ ├── pave.yaml │ ├── di.yaml │ ├── get_coco2017.sh │ ├── coco128.yaml │ └── coco.yaml ├── run_train.py ├── data_proc.py ├── hubconf.py ├── detect_di.py ├── submissions.py ├── getDetectionAll.py ├── detect.py ├── trial_india.py ├── trial_india2.py ├── trial2.py ├── trial.py ├── trial_Czech.py ├── trial_Czech2.py ├── getDetections.py ├── accuracy.py ├── automl.py └── di_automl.py ├── requirements.txt └── README.md /Proposed Algorithm/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Proposed Algorithm/__pycache__/test.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/__pycache__/test.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/__pycache__/test.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/__pycache__/test.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000007.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000016.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000016.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000021.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000021.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000047.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000047.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000052.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000052.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000057.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000057.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000060.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000060.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000061.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000061.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000094.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000094.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000134.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000134.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000141.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000141.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000147.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000147.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000148.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000148.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000149.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000149.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000157.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000157.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000159.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000159.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000161.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000161.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000173.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000173.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000174.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000174.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000185.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000185.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000195.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000195.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000206.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000206.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000223.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000223.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000226.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000226.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000236.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000236.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000239.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000239.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000248.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000248.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000249.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000249.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000280.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000280.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000288.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000288.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000293.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000293.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000299.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000299.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000321.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000321.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000342.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000342.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000344.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000344.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000346.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000346.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000367.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000367.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000411.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000411.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000417.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000417.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000443.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000443.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000451.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000451.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000470.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000470.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000475.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000475.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000479.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000479.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000482.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000482.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000497.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000497.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000501.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000501.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000514.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000514.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000524.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000524.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000541.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000541.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000542.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000542.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000543.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000543.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000549.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000549.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000563.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000563.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000569.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000569.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000595.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000595.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000606.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000606.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000621.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000621.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000624.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000624.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000640.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000640.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000647.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000647.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000674.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000674.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000678.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000678.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000686.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000686.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000714.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000714.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000715.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000715.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000730.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000730.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000746.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000746.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000747.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000747.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000753.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000753.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000768.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000768.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/inference/prev/Japan_000769.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/inference/prev/Japan_000769.jpg -------------------------------------------------------------------------------- /Proposed Algorithm/models/__pycache__/yolo.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/models/__pycache__/yolo.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/models/__pycache__/yolo.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/models/__pycache__/yolo.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/utils.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/utils.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/utils.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/models/__pycache__/common.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/models/__pycache__/common.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/models/__pycache__/common.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/models/__pycache__/common.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/__init__.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/__init__.cpython-35.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/datasets.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/datasets.cpython-35.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/datasets.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/datasets.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/datasets.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/datasets.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/torch_utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/torch_utils.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/torch_utils.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/torch_utils.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/models/__pycache__/experimental.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/models/__pycache__/experimental.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/models/__pycache__/experimental.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/models/__pycache__/experimental.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/google_utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/google_utils.cpython-37.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/utils/__pycache__/google_utils.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/titanmu/RoadCrackDetection/HEAD/Proposed Algorithm/utils/__pycache__/google_utils.cpython-38.pyc -------------------------------------------------------------------------------- /Proposed Algorithm/main_run_di.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # os.system('python detect.py --source d3.mp4 --weights weights/di/best.pt --view-img') 4 | os.system('python detect.py --source d3.mp4 --weights weights/yolov5l.pt --view-img') 5 | # os.system('python detect.py --source d3.mp4 --weights weights/di/last.pt --view-img') 6 | # os.system('python detect.py --source d3.mp4 --weights weights/yolov5s.pt --view-img') 7 | # os.system('python di_automl.py --source d8.mp4 --weights weights/yolov5s.pt --view-img') -------------------------------------------------------------------------------- /Proposed Algorithm/data/cctv.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org - first 128 training images 2 | # Download command: python -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" 3 | # Train command: python train.py --data ./data/coco128.yaml 4 | # Dataset should be placed next to yolov5 folder: 5 | # /parent_folder 6 | # /coco128 7 | # /yolov5 8 | 9 | 10 | # train and val datasets (image directory or *.txt file with image paths) 11 | train: /media/ai/New Volume/dev/aicity/yolov5/training/ 12 | val: /media/ai/New Volume/dev/aicity/yolov5/training/ 13 | 14 | # number of classes 15 | nc: 4 16 | 17 | # class names 18 | names: ['car','motorcycle','truck','single-unit'] 19 | -------------------------------------------------------------------------------- /Proposed Algorithm/data/cctv (copy).yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org - first 128 training images 2 | # Download command: python -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" 3 | # Train command: python train.py --data ./data/coco128.yaml 4 | # Dataset should be placed next to yolov5 folder: 5 | # /parent_folder 6 | # /coco128 7 | # /yolov5 8 | 9 | 10 | # train and val datasets (image directory or *.txt file with image paths) 11 | train: /media/ai/New Volume/dev/aicity/yolov5/training/ 12 | val: /media/ai/New Volume/dev/aicity/yolov5/training/ 13 | 14 | # number of classes 15 | nc: 4 16 | 17 | # class names 18 | names: ['car','motorcycle','truck','single-unit'] 19 | -------------------------------------------------------------------------------- /Proposed Algorithm/data/pave_india.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org - first 128 training images 2 | # Download command: python -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" 3 | # Train command: python train.py --data ./data/coco128.yaml 4 | # Dataset should be placed next to yolov5 folder: 5 | # /parent_folder 6 | # /coco128 7 | # /yolov5 8 | 9 | 10 | # train and val datasets (image directory or *.txt file with image paths) 11 | train: /home/ai/bigDataComp/Trainer/tdata/indiayolov5/ 12 | val: /home/ai/bigDataComp/Trainer/tdata/indiayolov5/ 13 | 14 | # number of classes 15 | nc: 7 16 | 17 | # class names 18 | names: ['D00','D10','D20','D40','D43','D44','D50'] 19 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # pip install -U -r requirements.txt 2 | Cython 3 | matplotlib>=3.2.2 4 | numpy>=1.18.5 5 | opencv-python>=4.1.2 6 | pillow 7 | # pycocotools>=2.0 8 | PyYAML>=5.3 9 | scipy>=1.4.1 10 | tensorboard>=2.2 11 | torch>=1.6.0 12 | torchvision>=0.7.0 13 | tqdm>=4.41.0 14 | 15 | # Conda commands (in place of pip) --------------------------------------------- 16 | # conda update -yn base -c defaults conda 17 | # conda install -yc anaconda numpy opencv matplotlib tqdm pillow ipython 18 | # conda install -yc conda-forge scikit-image pycocotools tensorboard 19 | # conda install -yc spyder-ide spyder-line-profiler 20 | # conda install -yc pytorch pytorch torchvision 21 | # conda install -yc conda-forge protobuf numpy && pip install onnx==1.6.0 # https://github.com/onnx/onnx#linux-and-macos 22 | -------------------------------------------------------------------------------- /Proposed Algorithm/data/pave.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org - first 128 training images 2 | # Download command: python -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" 3 | # Train command: python train.py --data ./data/coco128.yaml 4 | # Dataset should be placed next to yolov5 folder: 5 | # /parent_folder 6 | # /coco128 7 | # /yolov5 8 | 9 | 10 | # train and val datasets (image directory or *.txt file with image paths) 11 | train: /home/ai/bigDataComp/Trainer/tdata/yolov5/ 12 | val: /home/ai/bigDataComp/Trainer/tdata/yolov5/ 13 | 14 | # number of classes 15 | nc: 7 16 | #nc: 4 17 | 18 | # class names 19 | names: ['D00','D10','D20','D40','D43','D44','D50'] 20 | #names: ['D00','D10','D20','D40'] 21 | -------------------------------------------------------------------------------- /Proposed Algorithm/data/di.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org - first 128 training images 2 | # Download command: python -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" 3 | # Train command: python train.py --data ./data/coco128.yaml 4 | # Dataset should be placed next to yolov5 folder: 5 | # /parent_folder 6 | # /coco128 7 | # /yolov5 8 | 9 | 10 | # train and val datasets (image directory or *.txt file with image paths) 11 | train: /media/ai/New Volume/dev/deep_insight/training_data_prep/training_data/yolov5f/ 12 | val: /media/ai/New Volume/dev/deep_insight/training_data_prep/training_data/yolov5f/ 13 | 14 | # number of classes 15 | nc: 3 16 | 17 | # class names 18 | names: ['person','car','traffic_light'] 19 | -------------------------------------------------------------------------------- /Proposed Algorithm/run_train.py: -------------------------------------------------------------------------------- 1 | import os 2 | # os.system('python train_main.py --data data/pave.yaml --cfg models/yolov5x_pave.yaml --batch-size 8 --resume') 3 | # os.system('python train_main.py --data data/pave.yaml --cfg models/yolov5x_pave.yaml --batch-size 6 --device 0 --resume') #Japan_Czech 4 | 5 | os.system('python train_main.py --data data/pave.yaml --cfg models/yolov5m_pave.yaml --batch-size 8 --device 0 --resume' ) #Japan_Czech 6 | 7 | # os.system('python train_main.py --data data/pave_india.yaml --cfg models/yolov5x_india.yaml --batch-size 6 --device 0 --resume') #India 8 | 9 | # os.system('python train_main.py -pyth-data data/pave.yaml --cfg models/yolov5x_pave.yaml --weights '' --batch-size 8') 10 | 11 | ## test model 12 | # run submissions.py 13 | # os.system('python detect.py --weights weights/pave/last.pt --source ../Trainer/test1/Japan/images --conf-thres 0.2') -------------------------------------------------------------------------------- /Proposed Algorithm/data_proc.py: -------------------------------------------------------------------------------- 1 | import os, cv2 2 | import pandas as pd 3 | 4 | dets_df = pd.read_csv('dets.csv') 5 | vid_path = '1x.avi' 6 | # print (dets_df.head()) 7 | cap = cv2.VideoCapture(vid_path) 8 | while cap.isOpened(): 9 | ret, frame = cap.read() 10 | if ret: 11 | cur_frame = int(cap.get(cv2.CAP_PROP_POS_FRAMES)) 12 | # get all detections on current frame 13 | cur_dets = dets_df[dets_df['frame'] == cur_frame].values 14 | for det in cur_dets: 15 | [x1,y1,x2,y2,cls,frm] = det 16 | ## to crop image use this code 17 | # crop_img = frame[y1:y2, x1:x2] 18 | 19 | ## plot detections on image 20 | frame = cv2.rectangle(frame,(x1,y1),(x2,y2),(0,0,255),3) 21 | cv2.imshow('',frame) 22 | if cv2.waitKey(100) == ord('q'): 23 | break 24 | # print (det) 25 | cap.release() 26 | cv2.destroyAllWindows() -------------------------------------------------------------------------------- /Proposed Algorithm/data/get_coco2017.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Zip coco folder 3 | # zip -r coco.zip coco 4 | # tar -czvf coco.tar.gz coco 5 | 6 | # Download labels from Google Drive, accepting presented query 7 | filename="coco2017labels.zip" 8 | fileid="1cXZR_ckHki6nddOmcysCuuJFM--T-Q6L" 9 | curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null 10 | curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename} 11 | rm ./cookie 12 | 13 | # Unzip labels 14 | unzip -q ${filename} # for coco.zip 15 | # tar -xzf ${filename} # for coco.tar.gz 16 | rm ${filename} 17 | 18 | # Download and unzip images 19 | cd coco/images 20 | f="train2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f # 19G, 118k images 21 | f="val2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f # 1G, 5k images 22 | # f="test2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f # 7G, 41k images 23 | 24 | # cd out 25 | cd ../.. 26 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5l.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 80 # number of classes 3 | depth_multiple: 1.0 # model depth multiple 4 | width_multiple: 1.0 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5l_pave.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 7 # number of classes 3 | depth_multiple: 1.0 # model depth multiple 4 | width_multiple: 1.0 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5m.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 80 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5s.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 80 # number of classes 3 | depth_multiple: 0.33 # model depth multiple 4 | width_multiple: 0.50 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5s_di.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 3 # number of classes 3 | depth_multiple: 0.33 # model depth multiple 4 | width_multiple: 0.50 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5x.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 80 # number of classes 3 | depth_multiple: 1.33 # model depth multiple 4 | width_multiple: 1.25 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5m_pave.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 7 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5s_cctv.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 4 # number of classes 3 | depth_multiple: 0.33 # model depth multiple 4 | width_multiple: 0.50 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5x_india.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 7 # number of classes 3 | depth_multiple: 1.33 # model depth multiple 4 | width_multiple: 1.25 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov5x_pave.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 7 # number of classes 3 | depth_multiple: 1.33 # model depth multiple 4 | width_multiple: 1.25 # 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]], # 1-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 2-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 4-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 6-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 10 25 | ] 26 | 27 | # yolov5 head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 11 30 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) 31 | 32 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, BottleneckCSP, [512, False]], 36 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) 37 | 38 | [-2, 1, nn.Upsample, [None, 2, 'nearest']], 39 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 40 | [-1, 1, Conv, [256, 1, 1]], 41 | [-1, 3, BottleneckCSP, [256, False]], 42 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) 43 | 44 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 45 | ] 46 | -------------------------------------------------------------------------------- /Proposed Algorithm/data/coco128.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org - first 128 training images 2 | # Download command: python -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" 3 | # Train command: python train.py --data ./data/coco128.yaml 4 | # Dataset should be placed next to yolov5 folder: 5 | # /parent_folder 6 | # /coco128 7 | # /yolov5 8 | 9 | 10 | # train and val datasets (image directory or *.txt file with image paths) 11 | train: ../coco128/images/train2017/ 12 | val: ../coco128/images/train2017/ 13 | 14 | # number of classes 15 | nc: 80 16 | 17 | # class names 18 | names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 19 | 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 20 | 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 21 | 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 22 | 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 23 | 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 24 | 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 25 | 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 26 | 'hair drier', 'toothbrush'] -------------------------------------------------------------------------------- /Proposed Algorithm/data/coco.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org 2 | # Download command: bash yolov5/data/get_coco2017.sh 3 | # Train command: python train.py --data ./data/coco.yaml 4 | # Dataset should be placed next to yolov5 folder: 5 | # /parent_folder 6 | # /coco 7 | # /yolov5 8 | 9 | 10 | # train and val datasets (image directory or *.txt file with image paths) 11 | train: ../coco/train2017.txt # 118k images 12 | val: ../coco/val2017.txt # 5k images 13 | test: ../coco/test-dev2017.txt # 20k images for submission to https://competitions.codalab.org/competitions/20794 14 | 15 | # number of classes 16 | nc: 80 17 | 18 | # class names 19 | names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 20 | 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 21 | 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 22 | 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 23 | 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 24 | 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 25 | 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 26 | 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 27 | 'hair drier', 'toothbrush'] 28 | 29 | # Print classes 30 | # with open('data/coco.yaml') as f: 31 | # d = yaml.load(f, Loader=yaml.FullLoader) # dict 32 | # for i, x in enumerate(d['names']): 33 | # print(i, x) -------------------------------------------------------------------------------- /Proposed Algorithm/models/onnx_export.py: -------------------------------------------------------------------------------- 1 | """Exports a pytorch *.pt model to *.onnx format 2 | 3 | Usage: 4 | import torch 5 | $ export PYTHONPATH="$PWD" && python models/onnx_export.py --weights ./weights/yolov5s.pt --img 640 --batch 1 6 | """ 7 | 8 | import argparse 9 | 10 | import onnx 11 | 12 | from models.common import * 13 | 14 | if __name__ == '__main__': 15 | parser = argparse.ArgumentParser() 16 | parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path') 17 | parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') 18 | parser.add_argument('--batch-size', type=int, default=1, help='batch size') 19 | opt = parser.parse_args() 20 | print(opt) 21 | 22 | # Parameters 23 | f = opt.weights.replace('.pt', '.onnx') # onnx filename 24 | img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size, (1, 3, 320, 192) iDetection 25 | 26 | # Load pytorch model 27 | google_utils.attempt_download(opt.weights) 28 | model = torch.load(opt.weights)['model'] 29 | model.eval() 30 | model.fuse() 31 | 32 | # Export to onnx 33 | model.model[-1].export = True # set Detect() layer export=True 34 | _ = model(img) # dry run 35 | torch.onnx.export(model, img, f, verbose=False, opset_version=11, input_names=['images'], 36 | output_names=['output']) # output_names=['classes', 'boxes'] 37 | 38 | # Check onnx model 39 | model = onnx.load(f) # load onnx model 40 | onnx.checker.check_model(model) # check onnx model 41 | print(onnx.helper.printable_graph(model.graph)) # print a human readable representation of the graph 42 | print('Export complete. ONNX model saved to %s\nView with https://github.com/lutzroeder/netron' % f) 43 | -------------------------------------------------------------------------------- /Proposed Algorithm/utils/activations.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.functional as F 3 | import torch.nn as nn 4 | 5 | 6 | # Swish ------------------------------------------------------------------------ 7 | class SwishImplementation(torch.autograd.Function): 8 | @staticmethod 9 | def forward(ctx, x): 10 | ctx.save_for_backward(x) 11 | return x * torch.sigmoid(x) 12 | 13 | @staticmethod 14 | def backward(ctx, grad_output): 15 | x = ctx.saved_tensors[0] 16 | sx = torch.sigmoid(x) 17 | return grad_output * (sx * (1 + x * (1 - sx))) 18 | 19 | 20 | class MemoryEfficientSwish(nn.Module): 21 | @staticmethod 22 | def forward(x): 23 | return SwishImplementation.apply(x) 24 | 25 | 26 | class HardSwish(nn.Module): # https://arxiv.org/pdf/1905.02244.pdf 27 | @staticmethod 28 | def forward(x): 29 | return x * F.hardtanh(x + 3, 0., 6., True) / 6. 30 | 31 | 32 | class Swish(nn.Module): 33 | @staticmethod 34 | def forward(x): 35 | return x * torch.sigmoid(x) 36 | 37 | 38 | # Mish ------------------------------------------------------------------------ 39 | class MishImplementation(torch.autograd.Function): 40 | @staticmethod 41 | def forward(ctx, x): 42 | ctx.save_for_backward(x) 43 | return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) 44 | 45 | @staticmethod 46 | def backward(ctx, grad_output): 47 | x = ctx.saved_tensors[0] 48 | sx = torch.sigmoid(x) 49 | fx = F.softplus(x).tanh() 50 | return grad_output * (fx + x * sx * (1 - fx * fx)) 51 | 52 | 53 | class MemoryEfficientMish(nn.Module): 54 | @staticmethod 55 | def forward(x): 56 | return MishImplementation.apply(x) 57 | 58 | 59 | class Mish(nn.Module): # https://github.com/digantamisra98/Mish 60 | @staticmethod 61 | def forward(x): 62 | return x * F.softplus(x).tanh() 63 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolov3-spp.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | #nc: 80 # number of classes 3 | nc: 7 4 | depth_multiple: 1.0 # expand model depth 5 | width_multiple: 1.0 # expand layer channels 6 | 7 | # anchors 8 | anchors: 9 | - [10,13, 16,30, 33,23] # P3/8 10 | - [30,61, 62,45, 59,119] # P4/16 11 | - [116,90, 156,198, 373,326] # P5/32 12 | 13 | # darknet53 backbone 14 | backbone: 15 | # [from, number, module, args] 16 | [[-1, 1, Conv, [32, 3, 1]], # 0 17 | [-1, 1, Conv, [64, 3, 2]], # 1-P1/2 18 | [-1, 1, Bottleneck, [64]], 19 | [-1, 1, Conv, [128, 3, 2]], # 3-P2/4 20 | [-1, 2, Bottleneck, [128]], 21 | [-1, 1, Conv, [256, 3, 2]], # 5-P3/8 22 | [-1, 8, Bottleneck, [256]], 23 | [-1, 1, Conv, [512, 3, 2]], # 7-P4/16 24 | [-1, 8, Bottleneck, [512]], 25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P5/32 26 | [-1, 4, Bottleneck, [1024]], # 10 27 | ] 28 | 29 | # yolov3-spp head 30 | # na = len(anchors[0]) 31 | head: 32 | [[-1, 1, Bottleneck, [1024, False]], # 11 33 | [-1, 1, SPP, [512, [5, 9, 13]]], 34 | [-1, 1, Conv, [1024, 3, 1]], 35 | [-1, 1, Conv, [512, 1, 1]], 36 | [-1, 1, Conv, [1024, 3, 1]], 37 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 16 (P5/32-large) 38 | 39 | [-3, 1, Conv, [256, 1, 1]], 40 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 41 | [[-1, 8], 1, Concat, [1]], # cat backbone P4 42 | [-1, 1, Bottleneck, [512, False]], 43 | [-1, 1, Bottleneck, [512, False]], 44 | [-1, 1, Conv, [256, 1, 1]], 45 | [-1, 1, Conv, [512, 3, 1]], 46 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 24 (P4/16-medium) 47 | 48 | [-3, 1, Conv, [128, 1, 1]], 49 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 50 | [[-1, 6], 1, Concat, [1]], # cat backbone P3 51 | [-1, 1, Bottleneck, [256, False]], 52 | [-1, 2, Bottleneck, [256, False]], 53 | [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 30 (P3/8-small) 54 | 55 | [[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 56 | ] 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Automatic Pavement Crack Detection for Japan, Czech Republic and India 2 | 3 | Step 1: Download the complete repository 4 | 5 | Step 2: Install all the dependencies from requirements.txt 6 |
7 | $ pip install -r requirements.txt 8 | 9 | Step 3: Download 3 different models one each for every country [HERE](https://drive.google.com/drive/folders/1__WPNp2PFkZf0pbGwnCValb58Mc4DKYD) 10 | 11 | Step 4: Create a new folder within the parent directory and place each of those models in it for testing. 12 | 13 | # Testing 14 | 15 | The train, test1 and test2 datasets can be downloaded from [HERE](https://github.com/sekilab/RoadDamageDetector). 16 | 17 | Since, you already downloaded the models in Step 3. Proceed to Step 5-6 for Testing those models on test1 and test2 datasets. 18 | 19 | # test1 20 | 21 | Step 5: Run the following python files separately by passing individual models (downloaded from Step 3) and by inserting the correct path of the models within these files: 22 | 23 | trial.py: jpvl54.pt 24 |
25 | 26 | trial_Czech.py: jc66.pt 27 |
28 | 29 | trial_india.py: last_india_new.pt 30 |
31 | 32 | After running these files, an output .txt file would be generated with the name: tttt3_new.txt. It contains the results for test1. 33 |
34 | 35 | Note: Pass the appropriate model weights path in line 31 and the appropriate test datasets filenames/path in line 160. 36 | 37 | # test2 38 | 39 | Step 6: Now, you will need to pass different models into the following files to test on 'test2' dataset. Here are the following files and you will need to change the models as per the information detailed below: 40 | 41 | trial2.py: jc66.pt 42 |
43 | 44 | trial_Czech2.py: jpvl54.pt 45 |
46 | 47 | trial_india2.py: last_india_new.pt 48 |
49 | 50 | After running these files, an output .txt file would be generated with the name: ttt3_test2.txt. It contains the results for test2. 51 |
52 | 53 | Note: Pass the appropriate model weights path in line 31 and the appropriate test datasets filenames/path in line 160. 54 | 55 | # Citation 56 | 57 | The methods and the applicability of this research is further explained in our [PAPER](https://arxiv.org/abs/2010.10681) 58 |
59 | 60 | If you find this project useful for your research, please use the following BibTeX entry to cite our work: 61 |
62 | 63 | @article{mandal2020deep, 64 | title={Deep Learning Frameworks for Pavement Distress Classification: A Comparative Analysis}, 65 | author={Mandal, Vishal and Mussah, Abdul Rashid and Adu-Gyamfi, Yaw}, 66 | journal={arXiv preprint arXiv:2010.10681}, 67 | year={2020} 68 | } 69 | 70 | # Contact 71 | 72 | For general inquiries, contact either Vishal Mandal (vmghv@mail.missouri.edu) or, Yaw Adu-Gyamfi (adugyamfiy@missouri.edu) 73 | 74 | 75 | -------------------------------------------------------------------------------- /Proposed Algorithm/hubconf.py: -------------------------------------------------------------------------------- 1 | """File for accessing YOLOv5 via PyTorch Hub https://pytorch.org/hub/ 2 | 3 | Usage: 4 | import torch 5 | model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, channels=3, classes=80) 6 | """ 7 | 8 | dependencies = ['torch', 'yaml'] 9 | import torch 10 | 11 | from models.yolo import Model 12 | from utils import google_utils 13 | 14 | 15 | def create(name, pretrained, channels, classes): 16 | """Creates a specified YOLOv5 model 17 | 18 | Arguments: 19 | name (str): name of model, i.e. 'yolov5s' 20 | pretrained (bool): load pretrained weights into the model 21 | channels (int): number of input channels 22 | classes (int): number of model classes 23 | 24 | Returns: 25 | pytorch model 26 | """ 27 | model = Model('models/%s.yaml' % name, channels, classes) 28 | if pretrained: 29 | ckpt = '%s.pt' % name # checkpoint filename 30 | google_utils.attempt_download(ckpt) # download if not found locally 31 | state_dict = torch.load(ckpt)['model'].state_dict() 32 | state_dict = {k: v for k, v in state_dict.items() if model.state_dict()[k].numel() == v.numel()} # filter 33 | model.load_state_dict(state_dict, strict=False) # load 34 | return model 35 | 36 | 37 | def yolov5s(pretrained=False, channels=3, classes=80): 38 | """YOLOv5-small model from https://github.com/ultralytics/yolov5 39 | 40 | Arguments: 41 | pretrained (bool): load pretrained weights into the model, default=False 42 | channels (int): number of input channels, default=3 43 | classes (int): number of model classes, default=80 44 | 45 | Returns: 46 | pytorch model 47 | """ 48 | return create('yolov5s', pretrained, channels, classes) 49 | 50 | 51 | def yolov5m(pretrained=False, channels=3, classes=80): 52 | """YOLOv5-medium model from https://github.com/ultralytics/yolov5 53 | 54 | Arguments: 55 | pretrained (bool): load pretrained weights into the model, default=False 56 | channels (int): number of input channels, default=3 57 | classes (int): number of model classes, default=80 58 | 59 | Returns: 60 | pytorch model 61 | """ 62 | return create('yolov5m', pretrained, channels, classes) 63 | 64 | 65 | def yolov5l(pretrained=False, channels=3, classes=80): 66 | """YOLOv5-large model from https://github.com/ultralytics/yolov5 67 | 68 | Arguments: 69 | pretrained (bool): load pretrained weights into the model, default=False 70 | channels (int): number of input channels, default=3 71 | classes (int): number of model classes, default=80 72 | 73 | Returns: 74 | pytorch model 75 | """ 76 | return create('yolov5l', pretrained, channels, classes) 77 | 78 | 79 | def yolov5x(pretrained=False, channels=3, classes=80): 80 | """YOLOv5-xlarge model from https://github.com/ultralytics/yolov5 81 | 82 | Arguments: 83 | pretrained (bool): load pretrained weights into the model, default=False 84 | channels (int): number of input channels, default=3 85 | classes (int): number of model classes, default=80 86 | 87 | Returns: 88 | pytorch model 89 | """ 90 | return create('yolov5x', pretrained, channels, classes) 91 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/experimental.py: -------------------------------------------------------------------------------- 1 | from models.common import * 2 | 3 | 4 | class Sum(nn.Module): 5 | # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 6 | def __init__(self, n, weight=False): # n: number of inputs 7 | super(Sum, self).__init__() 8 | self.weight = weight # apply weights boolean 9 | self.iter = range(n - 1) # iter object 10 | if weight: 11 | self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) # layer weights 12 | 13 | def forward(self, x): 14 | y = x[0] # no weight 15 | if self.weight: 16 | w = torch.sigmoid(self.w) * 2 17 | for i in self.iter: 18 | y = y + x[i + 1] * w[i] 19 | else: 20 | for i in self.iter: 21 | y = y + x[i + 1] 22 | return y 23 | 24 | 25 | class GhostConv(nn.Module): 26 | # Ghost Convolution https://github.com/huawei-noah/ghostnet 27 | def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups 28 | super(GhostConv, self).__init__() 29 | c_ = c2 // 2 # hidden channels 30 | self.cv1 = Conv(c1, c_, k, s, g, act) 31 | self.cv2 = Conv(c_, c_, 5, 1, c_, act) 32 | 33 | def forward(self, x): 34 | y = self.cv1(x) 35 | return torch.cat([y, self.cv2(y)], 1) 36 | 37 | 38 | class GhostBottleneck(nn.Module): 39 | # Ghost Bottleneck https://github.com/huawei-noah/ghostnet 40 | def __init__(self, c1, c2, k, s): 41 | super(GhostBottleneck, self).__init__() 42 | c_ = c2 // 2 43 | self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw 44 | DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw 45 | GhostConv(c_, c2, 1, 1, act=False)) # pw-linear 46 | self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), 47 | Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity() 48 | 49 | def forward(self, x): 50 | return self.conv(x) + self.shortcut(x) 51 | 52 | 53 | class ConvPlus(nn.Module): 54 | # Plus-shaped convolution 55 | def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): # ch_in, ch_out, kernel, stride, groups 56 | super(ConvPlus, self).__init__() 57 | self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias=bias) 58 | self.cv2 = nn.Conv2d(c1, c2, (1, k), s, (0, k // 2), groups=g, bias=bias) 59 | 60 | def forward(self, x): 61 | return self.cv1(x) + self.cv2(x) 62 | 63 | 64 | class MixConv2d(nn.Module): 65 | # Mixed Depthwise Conv https://arxiv.org/abs/1907.09595 66 | def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): 67 | super(MixConv2d, self).__init__() 68 | groups = len(k) 69 | if equal_ch: # equal c_ per group 70 | i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices 71 | c_ = [(i == g).sum() for g in range(groups)] # intermediate channels 72 | else: # equal weight.numel() per group 73 | b = [c2] + [0] * groups 74 | a = np.eye(groups + 1, groups, k=-1) 75 | a -= np.roll(a, 1, axis=1) 76 | a *= np.array(k) ** 2 77 | a[0] = 1 78 | c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b 79 | 80 | self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)]) 81 | self.bn = nn.BatchNorm2d(c2) 82 | self.act = nn.LeakyReLU(0.1, inplace=True) 83 | 84 | def forward(self, x): 85 | return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1))) 86 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/common.py: -------------------------------------------------------------------------------- 1 | # This file contains modules common to various models 2 | 3 | 4 | from utils.utils import * 5 | 6 | 7 | def DWConv(c1, c2, k=1, s=1, act=True): 8 | # Depthwise convolution 9 | return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act) 10 | 11 | 12 | class Conv(nn.Module): 13 | # Standard convolution 14 | def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups 15 | super(Conv, self).__init__() 16 | self.conv = nn.Conv2d(c1, c2, k, s, k // 2, groups=g, bias=False) 17 | self.bn = nn.BatchNorm2d(c2) 18 | self.act = nn.LeakyReLU(0.1, inplace=True) if act else nn.Identity() 19 | 20 | def forward(self, x): 21 | return self.act(self.bn(self.conv(x))) 22 | 23 | def fuseforward(self, x): 24 | return self.act(self.conv(x)) 25 | 26 | 27 | class Bottleneck(nn.Module): 28 | # Standard bottleneck 29 | def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion 30 | super(Bottleneck, self).__init__() 31 | c_ = int(c2 * e) # hidden channels 32 | self.cv1 = Conv(c1, c_, 1, 1) 33 | self.cv2 = Conv(c_, c2, 3, 1, g=g) 34 | self.add = shortcut and c1 == c2 35 | 36 | def forward(self, x): 37 | return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) 38 | 39 | 40 | class BottleneckCSP(nn.Module): 41 | # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks 42 | def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion 43 | super(BottleneckCSP, self).__init__() 44 | c_ = int(c2 * e) # hidden channels 45 | self.cv1 = Conv(c1, c_, 1, 1) 46 | self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) 47 | self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) 48 | self.cv4 = Conv(c2, c2, 1, 1) 49 | self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) 50 | self.act = nn.LeakyReLU(0.1, inplace=True) 51 | self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) 52 | 53 | def forward(self, x): 54 | y1 = self.cv3(self.m(self.cv1(x))) 55 | y2 = self.cv2(x) 56 | return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) 57 | 58 | 59 | class SPP(nn.Module): 60 | # Spatial pyramid pooling layer used in YOLOv3-SPP 61 | def __init__(self, c1, c2, k=(5, 9, 13)): 62 | super(SPP, self).__init__() 63 | c_ = c1 // 2 # hidden channels 64 | self.cv1 = Conv(c1, c_, 1, 1) 65 | self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) 66 | self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) 67 | 68 | def forward(self, x): 69 | x = self.cv1(x) 70 | return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) 71 | 72 | 73 | class Flatten(nn.Module): 74 | # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions 75 | def forward(self, x): 76 | return x.view(x.size(0), -1) 77 | 78 | 79 | class Focus(nn.Module): 80 | # Focus wh information into c-space 81 | def __init__(self, c1, c2, k=1): 82 | super(Focus, self).__init__() 83 | self.conv = Conv(c1 * 4, c2, k, 1) 84 | 85 | def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) 86 | return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) 87 | 88 | 89 | class Concat(nn.Module): 90 | # Concatenate a list of tensors along dimension 91 | def __init__(self, dimension=1): 92 | super(Concat, self).__init__() 93 | self.d = dimension 94 | 95 | def forward(self, x): 96 | return torch.cat(x, self.d) 97 | -------------------------------------------------------------------------------- /Proposed Algorithm/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() 13 | msg = weights + ' missing, try downloading from https://drive.google.com/drive/folders/1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J' 14 | 15 | r = 1 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 | # Error check 29 | if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB 30 | os.system('rm ' + weights) # remove partial downloads 31 | raise Exception(msg) 32 | 33 | 34 | def gdrive_download(id='1HaXkef9z6y5l4vUnCYgdmEAj61c6bfWO', name='coco.zip'): 35 | # https://gist.github.com/tanaikech/f0f2d122e05bf5f971611258c22c110f 36 | # Downloads a file from Google Drive, accepting presented query 37 | # from utils.google_utils import *; gdrive_download() 38 | t = time.time() 39 | 40 | print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='') 41 | os.remove(name) if os.path.exists(name) else None # remove existing 42 | os.remove('cookie') if os.path.exists('cookie') else None 43 | 44 | # Attempt file download 45 | os.system("curl -c ./cookie -s -L \"https://drive.google.com/uc?export=download&id=%s\" > /dev/null" % id) 46 | if os.path.exists('cookie'): # large file 47 | s = "curl -Lb ./cookie \"https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=%s\" -o %s" % ( 48 | id, name) 49 | else: # small file 50 | s = "curl -s -L -o %s 'https://drive.google.com/uc?export=download&id=%s'" % (name, id) 51 | r = os.system(s) # execute, capture return values 52 | os.remove('cookie') if os.path.exists('cookie') else None 53 | 54 | # Error check 55 | if r != 0: 56 | os.remove(name) if os.path.exists(name) else None # remove partial 57 | print('Download error ') # raise Exception('Download error') 58 | return r 59 | 60 | # Unzip if archive 61 | if name.endswith('.zip'): 62 | print('unzipping... ', end='') 63 | os.system('unzip -q %s' % name) # unzip 64 | os.remove(name) # remove zip to free space 65 | 66 | print('Done (%.1fs)' % (time.time() - t)) 67 | return r 68 | 69 | # def upload_blob(bucket_name, source_file_name, destination_blob_name): 70 | # # Uploads a file to a bucket 71 | # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python 72 | # 73 | # storage_client = storage.Client() 74 | # bucket = storage_client.get_bucket(bucket_name) 75 | # blob = bucket.blob(destination_blob_name) 76 | # 77 | # blob.upload_from_filename(source_file_name) 78 | # 79 | # print('File {} uploaded to {}.'.format( 80 | # source_file_name, 81 | # destination_blob_name)) 82 | # 83 | # 84 | # def download_blob(bucket_name, source_blob_name, destination_file_name): 85 | # # Uploads a blob from a bucket 86 | # storage_client = storage.Client() 87 | # bucket = storage_client.get_bucket(bucket_name) 88 | # blob = bucket.blob(source_blob_name) 89 | # 90 | # blob.download_to_filename(destination_file_name) 91 | # 92 | # print('Blob {} downloaded to {}.'.format( 93 | # source_blob_name, 94 | # destination_file_name)) 95 | -------------------------------------------------------------------------------- /Proposed Algorithm/detect_di.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | 7 | def vis_box(x, img, path, df, color=None, label=None, line_thickness=None): 8 | # Plots one bounding box on image img 9 | 10 | img_name = os.path.basename(path) 11 | # clss = ['person','car','traffic_light'] 12 | clss = ['car', 'motorcycle', 'truck','single-unit'] 13 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 14 | color = color or [random.randint(0, 255) for _ in range(3)] 15 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 16 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 17 | df.loc[len(df)] = [img_name, x1,y1,x2,y2,clss.index(label.split(' ')[0])] 18 | cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 19 | if label: 20 | tf = max(tl - 1, 1) # font thickness 21 | t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 22 | c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 23 | # print (c1,c2) 24 | cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 25 | cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 26 | # cv2.imshow('',img) 27 | # cv2.waitKey(1) 28 | return df 29 | 30 | def detect(source, save_img=True): 31 | 32 | weights = 'weights/best.pt' 33 | view_img = True 34 | save_txt = False 35 | imgsz = 640 36 | device = '' 37 | half = False 38 | augment=False 39 | iou_thres = 0.5 40 | classes = None;conf_thres = 0.4;agnostic_nms = False 41 | 42 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 43 | 44 | # Initialize 45 | device = torch_utils.select_device(device) 46 | 47 | # Load model 48 | google_utils.attempt_download(weights) 49 | model = torch.load(weights, map_location=device)['model'] 50 | # torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning 51 | # model.fuse() 52 | model.to(device).eval() 53 | 54 | # Second-stage classifier 55 | classify = False 56 | if classify: 57 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 58 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 59 | modelc.to(device).eval() 60 | 61 | # Half precision 62 | half = half and device.type != 'cpu' # half precision only supported on CUDA 63 | if half: 64 | model.half() 65 | 66 | # Set Dataloader 67 | vid_path, vid_writer = None, None 68 | if webcam: 69 | view_img = True 70 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 71 | dataset = LoadStreams(source, img_size=imgsz) 72 | else: 73 | save_img = True 74 | dataset = LoadImages(source, img_size=imgsz) 75 | 76 | # Get names and colors 77 | names = model.names if hasattr(model, 'names') else model.modules.names 78 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 79 | 80 | # Run inference 81 | t0 = time.time() 82 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 83 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 84 | df=pd.DataFrame(columns=['filename','x1','y1','x2','y2','cls']) 85 | for path, img, im0s, vid_cap in dataset: 86 | # print (path) 87 | img = torch.from_numpy(img).to(device) 88 | img = img.half() if half else img.float() # uint8 to fp16/32 89 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 90 | if img.ndimension() == 3: 91 | img = img.unsqueeze(0) 92 | 93 | # Inference 94 | t1 = torch_utils.time_synchronized() 95 | pred = model(img, augment=augment)[0] 96 | t2 = torch_utils.time_synchronized() 97 | 98 | # to float 99 | if half: 100 | pred = pred.float() 101 | 102 | # Apply NMS 103 | pred = non_max_suppression(pred, conf_thres, iou_thres, 104 | fast=True, classes=classes, agnostic=agnostic_nms) 105 | 106 | # Apply Classifier 107 | if classify: 108 | pred = apply_classifier(pred, modelc, img, im0s) 109 | 110 | # Process detections 111 | for i, det in enumerate(pred): # detections per image 112 | if webcam: # batch_size >= 1 113 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 114 | else: 115 | p, s, im0 = path, '', im0s 116 | 117 | s += '%gx%g ' % img.shape[2:] # print string 118 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 119 | if det is not None and len(det): 120 | # Rescale boxes from img_size to im0 size 121 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 122 | 123 | # Print results 124 | for c in det[:, -1].unique(): 125 | n = (det[:, -1] == c).sum() # detections per class 126 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 127 | 128 | # Write results 129 | for *xyxy, conf, cls in det: 130 | if save_txt: # Write to file 131 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 132 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 133 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 134 | 135 | if save_img or view_img: # Add bbox to image 136 | label = '%s %.2f' % (names[int(cls)], conf) 137 | df = vis_box(xyxy, im0, path, df, label=label, color=colors[int(cls)], line_thickness=3) 138 | return df 139 | # with torch.no_grad(): 140 | # detect('d3.mp4') 141 | -------------------------------------------------------------------------------- /Proposed Algorithm/submissions.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | 7 | 8 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 9 | 10 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 11 | print (clss.index(label.split(' ')[0])) 12 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 13 | color = color or [random.randint(0, 255) for _ in range(3)] 14 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 15 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 16 | # np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 17 | np_array.append([x1,y1,x2,y2,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 18 | cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 19 | if label: 20 | tf = max(tl - 1, 1) # font thickness 21 | t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 22 | c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 23 | cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 24 | cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 25 | # cv2.imshow('',img) 26 | # cv2.waitKey(1000) 27 | return np_array,img 28 | 29 | def detect(source, save_img=True): 30 | 31 | weights = 'weights/pave/last.pt' 32 | view_img = True 33 | save_txt = False 34 | imgsz = 640 35 | device = '' 36 | half = False 37 | augment=False 38 | iou_thres = 0.5 39 | classes = None;conf_thres = 0.2;agnostic_nms = False 40 | 41 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 42 | 43 | # Initialize 44 | device = torch_utils.select_device(device) 45 | 46 | # Load model 47 | google_utils.attempt_download(weights) 48 | model = torch.load(weights, map_location=device)['model'] 49 | model.to(device).eval() 50 | 51 | # Second-stage classifier 52 | classify = False 53 | if classify: 54 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 55 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 56 | modelc.to(device).eval() 57 | 58 | # Half precision 59 | half = half and device.type != 'cpu' # half precision only supported on CUDA 60 | if half: 61 | model.half() 62 | 63 | # Set Dataloader 64 | vid_path, vid_writer = None, None 65 | if webcam: 66 | view_img = True 67 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 68 | dataset = LoadStreams(source, img_size=imgsz) 69 | else: 70 | save_img = True 71 | dataset = LoadImages(source, img_size=imgsz) 72 | 73 | # Get names and colors 74 | names = model.names if hasattr(model, 'names') else model.modules.names 75 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 76 | 77 | # Run inference 78 | t0 = time.time() 79 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 80 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 81 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 82 | np_array = [] 83 | for path, img, im0s,vc in dataset: 84 | 85 | # print (ftime) 86 | img = torch.from_numpy(img).to(device) 87 | img = img.half() if half else img.float() # uint8 to fp16/32 88 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 89 | if img.ndimension() == 3: 90 | img = img.unsqueeze(0) 91 | 92 | # Inference 93 | t1 = torch_utils.time_synchronized() 94 | pred = model(img, augment=augment)[0] 95 | t2 = torch_utils.time_synchronized() 96 | 97 | # to float 98 | if half: 99 | pred = pred.float() 100 | 101 | # Apply NMS 102 | pred = non_max_suppression(pred, conf_thres, iou_thres, 103 | fast=True, classes=classes, agnostic=agnostic_nms) 104 | 105 | # Apply Classifier 106 | if classify: 107 | pred = apply_classifier(pred, modelc, img, im0s) 108 | 109 | # Process detections 110 | for i, det in enumerate(pred): # detections per image 111 | if webcam: # batch_size >= 1 112 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 113 | else: 114 | p, s, im0 = path, '', im0s 115 | 116 | s += '%gx%g ' % img.shape[2:] # print string 117 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 118 | if det is not None and len(det): 119 | # Rescale boxes from img_size to im0 size 120 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 121 | 122 | # Print results 123 | for c in det[:, -1].unique(): 124 | n = (det[:, -1] == c).sum() # detections per class 125 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 126 | 127 | # Write results 128 | for *xyxy, conf, cls in det: 129 | if save_txt: # Write to file 130 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 131 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 132 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 133 | 134 | if save_img or view_img: # Add bbox to image 135 | label = '%s %.2f' % (names[int(cls)], conf) 136 | np_array,imgd = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 137 | else: 138 | imgd = im0 139 | 140 | return np_array,imgd 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | with torch.no_grad(): 151 | 152 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 153 | out_cls = ['D00','D10','D20','D40'] 154 | img_source = '../Trainer/test2' 155 | imgs = os.listdir(img_source) 156 | for imgcur in imgs: 157 | vid_source = os.path.join(img_source,imgcur) 158 | np_array, imgd = detect(vid_source) 159 | annts_list = [] 160 | for annts in np_array: 161 | if clss[annts[5]] in out_cls: 162 | annts_list.append([annts[5]+1,annts[0],annts[1],annts[2],annts[3]]) 163 | 164 | if len(annts_list)>0: 165 | sub_list = [imgcur]+np.concatenate(annts_list).tolist() 166 | print (np.array(sub_list)) 167 | # filename1=np.array(sub_list #vm 168 | # write to submission.txt 169 | 170 | # converter() #vm 171 | 172 | else: 173 | print ([imgcur]) 174 | # write to submission.txt 175 | 176 | 177 | # converter(): #vm 178 | cv2.imshow('',imgd) 179 | cv2.waitKey(1000) 180 | -------------------------------------------------------------------------------- /Proposed Algorithm/getDetectionAll.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | 7 | def vis_box(x, img, path, np_array, vid_cap, color=None, label=None, line_thickness=None): 8 | # Plots one bounding box on image img 9 | ftime = vid_cap.get(cv2.CAP_PROP_POS_MSEC)/1000 10 | fnum = vid_cap.get(cv2.CAP_PROP_POS_FRAMES) 11 | # print (fnum) 12 | img_name = os.path.basename(path) 13 | clss = ['car','motorcycle','truck','single-unit'] 14 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 15 | color = color or [random.randint(0, 255) for _ in range(3)] 16 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 17 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 18 | # df.loc[len(df)] = [x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),fnum,ftime] 19 | np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),fnum,ftime]) 20 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 21 | # if label: 22 | # tf = max(tl - 1, 1) # font thickness 23 | # t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 24 | # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 25 | # # print (c1,c2) 26 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 27 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 28 | # cv2.imshow('',img) 29 | # cv2.waitKey(1) 30 | return np_array 31 | 32 | def detect(source, save_img=True): 33 | 34 | weights = 'weights/cctv/best.pt' 35 | view_img = True 36 | save_txt = False 37 | imgsz = 640 38 | device = '' 39 | half = False 40 | augment=False 41 | iou_thres = 0.5 42 | classes = None;conf_thres = 0.4;agnostic_nms = False 43 | 44 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 45 | 46 | # Initialize 47 | device = torch_utils.select_device(device) 48 | 49 | # Load model 50 | google_utils.attempt_download(weights) 51 | model = torch.load(weights, map_location=device)['model'] 52 | model.to(device).eval() 53 | 54 | # Second-stage classifier 55 | classify = False 56 | if classify: 57 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 58 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 59 | modelc.to(device).eval() 60 | 61 | # Half precision 62 | half = half and device.type != 'cpu' # half precision only supported on CUDA 63 | if half: 64 | model.half() 65 | 66 | # Set Dataloader 67 | vid_path, vid_writer = None, None 68 | if webcam: 69 | view_img = True 70 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 71 | dataset = LoadStreams(source, img_size=imgsz) 72 | else: 73 | save_img = True 74 | dataset = LoadImages(source, img_size=imgsz) 75 | 76 | # Get names and colors 77 | names = model.names if hasattr(model, 'names') else model.modules.names 78 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 79 | 80 | # Run inference 81 | t0 = time.time() 82 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 83 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 84 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 85 | np_array = [] 86 | for path, img, im0s, vid_cap in dataset: 87 | 88 | 89 | # print (ftime) 90 | img = torch.from_numpy(img).to(device) 91 | img = img.half() if half else img.float() # uint8 to fp16/32 92 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 93 | if img.ndimension() == 3: 94 | img = img.unsqueeze(0) 95 | 96 | # Inference 97 | t1 = torch_utils.time_synchronized() 98 | pred = model(img, augment=augment)[0] 99 | t2 = torch_utils.time_synchronized() 100 | 101 | # to float 102 | if half: 103 | pred = pred.float() 104 | 105 | # Apply NMS 106 | pred = non_max_suppression(pred, conf_thres, iou_thres, 107 | fast=True, classes=classes, agnostic=agnostic_nms) 108 | 109 | # Apply Classifier 110 | if classify: 111 | pred = apply_classifier(pred, modelc, img, im0s) 112 | 113 | # Process detections 114 | for i, det in enumerate(pred): # detections per image 115 | if webcam: # batch_size >= 1 116 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 117 | else: 118 | p, s, im0 = path, '', im0s 119 | 120 | s += '%gx%g ' % img.shape[2:] # print string 121 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 122 | if det is not None and len(det): 123 | # Rescale boxes from img_size to im0 size 124 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 125 | 126 | # Print results 127 | for c in det[:, -1].unique(): 128 | n = (det[:, -1] == c).sum() # detections per class 129 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 130 | 131 | # Write results 132 | for *xyxy, conf, cls in det: 133 | if save_txt: # Write to file 134 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 135 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 136 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 137 | 138 | if save_img or view_img: # Add bbox to image 139 | label = '%s %.2f' % (names[int(cls)], conf) 140 | np_array = vis_box(xyxy, im0, path, np_array, vid_cap, label=label, color=colors[int(cls)], line_thickness=3) 141 | 142 | # print (df.tail()) 143 | # print (len(np_array)) 144 | return np_array 145 | 146 | with torch.no_grad(): 147 | cur_det_folder = os.path.join('../../paper/output', 'detections','yolov5') 148 | if not (os.path.isdir(cur_det_folder)): 149 | os.makedirs(cur_det_folder) 150 | fldrs_path = '../../paper/output/videos' 151 | fldrs = os.listdir(fldrs_path) 152 | for fldr in fldrs: 153 | print (fldr) 154 | det_folder = os.path.join(cur_det_folder,fldr) 155 | if not (os.path.isdir(det_folder)): 156 | os.makedirs(det_folder) 157 | cur_vid_path = os.path.join(fldrs_path,fldr) 158 | vidfiles = os.listdir(cur_vid_path) 159 | for vidname in vidfiles: 160 | 161 | vid_source = os.path.join(cur_vid_path,vidname) 162 | # print (vid_source) 163 | np_array = detect(vid_source) 164 | df=pd.DataFrame(np_array, columns=['x','y','w','h','score','cls','frame','time']) 165 | 166 | df.to_csv(os.path.join(det_folder, vidname+'.csv'),index=False,header=True) 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /Proposed Algorithm/detect.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | from utils.utils import * 5 | 6 | 7 | def detect(save_img=False): 8 | out, source, weights, half, view_img, save_txt, imgsz = \ 9 | opt.output, opt.source, opt.weights, opt.half, opt.view_img, opt.save_txt, opt.img_size 10 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 11 | 12 | # Initialize 13 | device = torch_utils.select_device(opt.device) 14 | if os.path.exists(out): 15 | shutil.rmtree(out) # delete output folder 16 | os.makedirs(out) # make new output folder 17 | 18 | # Load model 19 | google_utils.attempt_download(weights) 20 | model = torch.load(weights, map_location=device)['model'] 21 | # torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning 22 | # model.fuse() 23 | model.to(device).eval() 24 | 25 | # Second-stage classifier 26 | classify = False 27 | if classify: 28 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 29 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 30 | modelc.to(device).eval() 31 | 32 | # Half precision 33 | half = half and device.type != 'cpu' # half precision only supported on CUDA 34 | if half: 35 | model.half() 36 | 37 | # Set Dataloader 38 | vid_path, vid_writer = None, None 39 | if webcam: 40 | view_img = True 41 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 42 | dataset = LoadStreams(source, img_size=imgsz) 43 | else: 44 | save_img = True 45 | dataset = LoadImages(source, img_size=imgsz) 46 | 47 | # Get names and colors 48 | names = model.names if hasattr(model, 'names') else model.modules.names 49 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 50 | 51 | # Run inference 52 | t0 = time.time() 53 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 54 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 55 | for path, img, im0s, vid_cap in dataset: 56 | img = torch.from_numpy(img).to(device) 57 | img = img.half() if half else img.float() # uint8 to fp16/32 58 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 59 | if img.ndimension() == 3: 60 | img = img.unsqueeze(0) 61 | 62 | # Inference 63 | t1 = torch_utils.time_synchronized() 64 | pred = model(img, augment=opt.augment)[0] 65 | t2 = torch_utils.time_synchronized() 66 | 67 | # to float 68 | if half: 69 | pred = pred.float() 70 | 71 | # Apply NMS 72 | pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, 73 | fast=True, classes=opt.classes, agnostic=opt.agnostic_nms) 74 | 75 | # Apply Classifier 76 | if classify: 77 | pred = apply_classifier(pred, modelc, img, im0s) 78 | 79 | # Process detections 80 | for i, det in enumerate(pred): # detections per image 81 | if webcam: # batch_size >= 1 82 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 83 | else: 84 | p, s, im0 = path, '', im0s 85 | 86 | save_path = str(Path(out) / Path(p).name) 87 | s += '%gx%g ' % img.shape[2:] # print string 88 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 89 | if det is not None and len(det): 90 | # Rescale boxes from img_size to im0 size 91 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 92 | 93 | # Print results 94 | for c in det[:, -1].unique(): 95 | n = (det[:, -1] == c).sum() # detections per class 96 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 97 | 98 | # Write results 99 | for *xyxy, conf, cls in det: 100 | print (cls) 101 | if save_txt: # Write to file 102 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 103 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 104 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 105 | 106 | if save_img or view_img: # Add bbox to image 107 | label = '%s %.2f' % (names[int(cls)], conf) 108 | plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3) 109 | 110 | # Print time (inference + NMS) 111 | print('%sDone. (%.3fs)' % (s, t2 - t1)) 112 | 113 | # Stream results 114 | if view_img: 115 | cv2.imshow(p, im0) 116 | if cv2.waitKey(1) == ord('q'): # q to quit 117 | raise StopIteration 118 | 119 | # Save results (image with detections) 120 | if save_img: 121 | if dataset.mode == 'images': 122 | cv2.imwrite(save_path, im0) 123 | else: 124 | if vid_path != save_path: # new video 125 | vid_path = save_path 126 | if isinstance(vid_writer, cv2.VideoWriter): 127 | vid_writer.release() # release previous video writer 128 | 129 | fps = vid_cap.get(cv2.CAP_PROP_FPS) 130 | w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 131 | h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 132 | vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h)) 133 | vid_writer.write(im0) 134 | 135 | if save_txt or save_img: 136 | print('Results saved to %s' % os.getcwd() + os.sep + out) 137 | if platform == 'darwin': # MacOS 138 | os.system('open ' + save_path) 139 | 140 | print('Done. (%.3fs)' % (time.time() - t0)) 141 | 142 | 143 | if __name__ == '__main__': 144 | parser = argparse.ArgumentParser() 145 | parser.add_argument('--weights', type=str, default='weights/yolov5s.pt', help='model.pt path') 146 | parser.add_argument('--source', type=str, default='inference/images', help='source') # file/folder, 0 for webcam 147 | parser.add_argument('--output', type=str, default='inference/output', help='output folder') # output folder 148 | parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') 149 | parser.add_argument('--conf-thres', type=float, default=0.4, help='object confidence threshold') 150 | parser.add_argument('--iou-thres', type=float, default=0.5, help='IOU threshold for NMS') 151 | parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)') 152 | parser.add_argument('--half', action='store_true', help='half precision FP16 inference') 153 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 154 | parser.add_argument('--view-img', action='store_true', help='display results') 155 | parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') 156 | parser.add_argument('--classes', nargs='+', type=int, help='filter by class') 157 | parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') 158 | parser.add_argument('--augment', action='store_true', help='augmented inference') 159 | opt = parser.parse_args() 160 | print(opt) 161 | 162 | with torch.no_grad(): 163 | detect() 164 | -------------------------------------------------------------------------------- /Proposed Algorithm/trial_india.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | 7 | import os 8 | 9 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 10 | 11 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 12 | print (clss.index(label.split(' ')[0])) 13 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 14 | color = color or [random.randint(0, 255) for _ in range(3)] 15 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 16 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 17 | # np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 18 | np_array.append([x1,y1,x2,y2,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 19 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 20 | # if label: 21 | # tf = max(tl - 1, 1) # font thickness 22 | # t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 23 | # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 24 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 25 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 26 | # cv2.imshow('',img) 27 | # cv2.waitKey(1000) 28 | return np_array,img 29 | 30 | def detect(source, save_img=True): 31 | weights = '/home/ai/bigDataComp/yolov5/weights/pave/test_prev/last_india_new.pt' 32 | view_img = True 33 | save_txt = False 34 | imgsz = 640 35 | device = '' 36 | half = False 37 | augment=False 38 | iou_thres = 0.5 39 | # classes = None;conf_thres = 0.2;agnostic_nms = False 40 | classes = None;conf_thres = 0.25;agnostic_nms = False #VM 41 | 42 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 43 | 44 | # Initialize 45 | device = torch_utils.select_device(device) 46 | 47 | # Load model 48 | google_utils.attempt_download(weights) 49 | model = torch.load(weights, map_location=device)['model'] 50 | model.to(device).eval() 51 | 52 | # Second-stage classifier 53 | classify = False 54 | if classify: 55 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 56 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 57 | modelc.to(device).eval() 58 | 59 | # Half precision 60 | half = half and device.type != 'cpu' # half precision only supported on CUDA 61 | if half: 62 | model.half() 63 | 64 | # Set Dataloader 65 | vid_path, vid_writer = None, None 66 | if webcam: 67 | view_img = True 68 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 69 | dataset = LoadStreams(source, img_size=imgsz) 70 | else: 71 | save_img = True 72 | dataset = LoadImages(source, img_size=imgsz) 73 | 74 | # Get names and colors 75 | names = model.names if hasattr(model, 'names') else model.modules.names 76 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 77 | 78 | # Run inference 79 | t0 = time.time() 80 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 81 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 82 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 83 | np_array = [] 84 | for path, img, im0s,vc in dataset: 85 | 86 | # print (ftime) 87 | img = torch.from_numpy(img).to(device) 88 | img = img.half() if half else img.float() # uint8 to fp16/32 89 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 90 | if img.ndimension() == 3: 91 | img = img.unsqueeze(0) 92 | 93 | # Inference 94 | t1 = torch_utils.time_synchronized() 95 | pred = model(img, augment=augment)[0] 96 | t2 = torch_utils.time_synchronized() 97 | 98 | # to float 99 | if half: 100 | pred = pred.float() 101 | 102 | # Apply NMS 103 | pred = non_max_suppression(pred, conf_thres, iou_thres, 104 | fast=True, classes=classes, agnostic=agnostic_nms) 105 | 106 | # Apply Classifier 107 | if classify: 108 | pred = apply_classifier(pred, modelc, img, im0s) 109 | 110 | # Process detections 111 | for i, det in enumerate(pred): # detections per image 112 | if webcam: # batch_size >= 1 113 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 114 | else: 115 | p, s, im0 = path, '', im0s 116 | 117 | s += '%gx%g ' % img.shape[2:] # print string 118 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 119 | if det is not None and len(det): 120 | # Rescale boxes from img_size to im0 size 121 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 122 | 123 | # Print results 124 | for c in det[:, -1].unique(): 125 | n = (det[:, -1] == c).sum() # detections per class 126 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 127 | 128 | # Write results 129 | for *xyxy, conf, cls in det: 130 | if save_txt: # Write to file 131 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 132 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 133 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 134 | 135 | if save_img or view_img: # Add bbox to image 136 | label = '%s %.2f' % (names[int(cls)], conf) 137 | np_array,imgd = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 138 | else: 139 | imgd = im0 140 | 141 | return np_array,imgd 142 | 143 | 144 | # def filename1(file1): 145 | # with open ('submissions.txt', 'w') as file2: 146 | # file2.write() 147 | 148 | 149 | 150 | 151 | 152 | 153 | with torch.no_grad(): 154 | 155 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 156 | out_cls = ['D00','D10','D20','D40'] 157 | # cnts = ['India','Japan','Czech'] 158 | cnts = ['India'] 159 | for cnt in cnts: 160 | img_source =os.path.join('../Trainer/test1',cnt,'images') 161 | # img_source = '../Trainer/test1/India/images' 162 | imgs = os.listdir(img_source) 163 | for imgcur in imgs: 164 | vid_source = os.path.join(img_source,imgcur) 165 | np_array, imgd = detect(vid_source) 166 | annts_list = [] 167 | for annts in np_array: 168 | if clss[annts[5]] in out_cls: 169 | annts_list.append([annts[5]+1,annts[0],annts[1],annts[2],annts[3]]) 170 | 171 | if len(annts_list)>0: 172 | sub_list = [imgcur]+np.concatenate(annts_list).tolist() 173 | # print ("final output:",np.array(sub_list)) 174 | # print("LENGTH", len(sub_list)) 175 | with open('tttt3_new.txt','a+') as writer: 176 | txt=str(sub_list[0])+"," 177 | for i in range(1,len(sub_list)): 178 | txt+=str(sub_list[i])+' ' 179 | txt+='\n' 180 | writer.write(txt) 181 | # filename1=np.array(sub_list #vm 182 | # write to submission.txt 183 | 184 | # converter() #vm 185 | 186 | else: 187 | print ("IMGCURL:",[imgcur]) 188 | with open('tttt3_new.txt','a+') as writer: 189 | txt=str(imgcur)+","+"\n" 190 | writer.write(txt) 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /Proposed Algorithm/trial_india2.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | 7 | import os 8 | 9 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 10 | 11 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 12 | print (clss.index(label.split(' ')[0])) 13 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 14 | color = color or [random.randint(0, 255) for _ in range(3)] 15 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 16 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 17 | # np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 18 | np_array.append([x1,y1,x2,y2,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 19 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 20 | # if label: 21 | # tf = max(tl - 1, 1) # font thickness 22 | # t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 23 | # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 24 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 25 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 26 | # cv2.imshow('',img) 27 | # cv2.waitKey(1000) 28 | return np_array,img 29 | 30 | def detect(source, save_img=True): 31 | weights = '/home/ai/bigDataComp/yolov5/weights/pave/test_prev/last_india_new.pt' #Pass the correct path of the model 32 | view_img = True 33 | save_txt = False 34 | imgsz = 640 35 | device = '' 36 | half = False 37 | augment=False 38 | iou_thres = 0.5 39 | # classes = None;conf_thres = 0.2;agnostic_nms = False 40 | classes = None;conf_thres = 0.15;agnostic_nms = False #VM 41 | 42 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 43 | 44 | # Initialize 45 | device = torch_utils.select_device(device) 46 | 47 | # Load model 48 | google_utils.attempt_download(weights) 49 | model = torch.load(weights, map_location=device)['model'] 50 | model.to(device).eval() 51 | 52 | # Second-stage classifier 53 | classify = False 54 | if classify: 55 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 56 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 57 | modelc.to(device).eval() 58 | 59 | # Half precision 60 | half = half and device.type != 'cpu' # half precision only supported on CUDA 61 | if half: 62 | model.half() 63 | 64 | # Set Dataloader 65 | vid_path, vid_writer = None, None 66 | if webcam: 67 | view_img = True 68 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 69 | dataset = LoadStreams(source, img_size=imgsz) 70 | else: 71 | save_img = True 72 | dataset = LoadImages(source, img_size=imgsz) 73 | 74 | # Get names and colors 75 | names = model.names if hasattr(model, 'names') else model.modules.names 76 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 77 | 78 | # Run inference 79 | t0 = time.time() 80 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 81 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 82 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 83 | np_array = [] 84 | for path, img, im0s,vc in dataset: 85 | 86 | # print (ftime) 87 | img = torch.from_numpy(img).to(device) 88 | img = img.half() if half else img.float() # uint8 to fp16/32 89 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 90 | if img.ndimension() == 3: 91 | img = img.unsqueeze(0) 92 | 93 | # Inference 94 | t1 = torch_utils.time_synchronized() 95 | pred = model(img, augment=augment)[0] 96 | t2 = torch_utils.time_synchronized() 97 | 98 | # to float 99 | if half: 100 | pred = pred.float() 101 | 102 | # Apply NMS 103 | pred = non_max_suppression(pred, conf_thres, iou_thres, 104 | fast=True, classes=classes, agnostic=agnostic_nms) 105 | 106 | # Apply Classifier 107 | if classify: 108 | pred = apply_classifier(pred, modelc, img, im0s) 109 | 110 | # Process detections 111 | for i, det in enumerate(pred): # detections per image 112 | if webcam: # batch_size >= 1 113 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 114 | else: 115 | p, s, im0 = path, '', im0s 116 | 117 | s += '%gx%g ' % img.shape[2:] # print string 118 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 119 | if det is not None and len(det): 120 | # Rescale boxes from img_size to im0 size 121 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 122 | 123 | # Print results 124 | for c in det[:, -1].unique(): 125 | n = (det[:, -1] == c).sum() # detections per class 126 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 127 | 128 | # Write results 129 | for *xyxy, conf, cls in det: 130 | if save_txt: # Write to file 131 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 132 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 133 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 134 | 135 | if save_img or view_img: # Add bbox to image 136 | label = '%s %.2f' % (names[int(cls)], conf) 137 | np_array,imgd = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 138 | else: 139 | imgd = im0 140 | 141 | return np_array,imgd 142 | 143 | 144 | # def filename1(file1): 145 | # with open ('submissions.txt', 'w') as file2: 146 | # file2.write() 147 | 148 | 149 | 150 | 151 | 152 | 153 | with torch.no_grad(): 154 | 155 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 156 | out_cls = ['D00','D10','D20','D40'] 157 | # cnts = ['India','Japan','Czech'] 158 | cnts = ['India'] 159 | for cnt in cnts: 160 | img_source =os.path.join('../Trainer/test2',cnt,'images') #Pass the Path of the test2 dataset 161 | # img_source = '../Trainer/test1/India/images' 162 | imgs = os.listdir(img_source) 163 | for imgcur in imgs: 164 | vid_source = os.path.join(img_source,imgcur) 165 | np_array, imgd = detect(vid_source) 166 | annts_list = [] 167 | for annts in np_array: 168 | if clss[annts[5]] in out_cls: 169 | annts_list.append([annts[5]+1,annts[0],annts[1],annts[2],annts[3]]) 170 | 171 | if len(annts_list)>0: 172 | sub_list = [imgcur]+np.concatenate(annts_list).tolist() 173 | # print ("final output:",np.array(sub_list)) 174 | # print("LENGTH", len(sub_list)) 175 | with open('ttt3_test2.txt','a+') as writer: 176 | txt=str(sub_list[0])+"," 177 | for i in range(1,len(sub_list)): 178 | txt+=str(sub_list[i])+' ' 179 | txt+='\n' 180 | writer.write(txt) 181 | # filename1=np.array(sub_list #vm 182 | # write to submission.txt 183 | 184 | # converter() #vm 185 | 186 | else: 187 | print ("IMGCURL:",[imgcur]) 188 | with open('ttt3_test2.txt','a+') as writer: 189 | txt=str(imgcur)+","+"\n" 190 | writer.write(txt) 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /Proposed Algorithm/trial2.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | import os 7 | 8 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 9 | 10 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 11 | print (clss.index(label.split(' ')[0])) 12 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 13 | color = color or [random.randint(0, 255) for _ in range(3)] 14 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 15 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 16 | # np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 17 | np_array.append([x1,y1,x2,y2,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 18 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 19 | # if label: 20 | # tf = max(tl - 1, 1) # font thickness 21 | # t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 22 | # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 23 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 24 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 25 | # cv2.imshow('',img) 26 | # cv2.waitKey(1000) 27 | return np_array,img 28 | 29 | def detect(source, save_img=True): 30 | 31 | weights = 'weights/pave/Vishal/66JC/last.pt' #Pass the correct path of the model 32 | view_img = True 33 | save_txt = False 34 | imgsz = 640 35 | device = '' 36 | half = False 37 | augment=False 38 | iou_thres = 1 39 | # classes = None;conf_thres = 0.01;agnostic_nms = False 40 | classes = None;conf_thres = 0.20;agnostic_nms = False #Vm 41 | 42 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 43 | 44 | # Initialize 45 | device = torch_utils.select_device(device) 46 | 47 | # Load model 48 | google_utils.attempt_download(weights) 49 | model = torch.load(weights, map_location=device)['model'] 50 | model.to(device).eval() 51 | 52 | # Second-stage classifier 53 | classify = False 54 | if classify: 55 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 56 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 57 | modelc.to(device).eval() 58 | 59 | # Half precision 60 | half = half and device.type != 'cpu' # half precision only supported on CUDA 61 | if half: 62 | model.half() 63 | 64 | # Set Dataloader 65 | vid_path, vid_writer = None, None 66 | if webcam: 67 | view_img = True 68 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 69 | dataset = LoadStreams(source, img_size=imgsz) 70 | else: 71 | save_img = True 72 | dataset = LoadImages(source, img_size=imgsz) 73 | 74 | # Get names and colors 75 | names = model.names if hasattr(model, 'names') else model.modules.names 76 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 77 | 78 | # Run inference 79 | t0 = time.time() 80 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 81 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 82 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 83 | np_array = [] 84 | for path, img, im0s,vc in dataset: 85 | 86 | # print (ftime) 87 | img = torch.from_numpy(img).to(device) 88 | img = img.half() if half else img.float() # uint8 to fp16/32 89 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 90 | if img.ndimension() == 3: 91 | img = img.unsqueeze(0) 92 | 93 | # Inference 94 | t1 = torch_utils.time_synchronized() 95 | pred = model(img, augment=augment)[0] 96 | t2 = torch_utils.time_synchronized() 97 | 98 | # to float 99 | if half: 100 | pred = pred.float() 101 | 102 | # Apply NMS 103 | pred = non_max_suppression(pred, conf_thres, iou_thres, 104 | fast=True, classes=classes, agnostic=agnostic_nms) 105 | 106 | # Apply Classifier 107 | if classify: 108 | pred = apply_classifier(pred, modelc, img, im0s) 109 | 110 | # Process detections 111 | for i, det in enumerate(pred): # detections per image 112 | if webcam: # batch_size >= 1 113 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 114 | else: 115 | p, s, im0 = path, '', im0s 116 | 117 | s += '%gx%g ' % img.shape[2:] # print string 118 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 119 | if det is not None and len(det): 120 | # Rescale boxes from img_size to im0 size 121 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 122 | 123 | # Print results 124 | for c in det[:, -1].unique(): 125 | n = (det[:, -1] == c).sum() # detections per class 126 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 127 | 128 | # Write results 129 | for *xyxy, conf, cls in det: 130 | if save_txt: # Write to file 131 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 132 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 133 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 134 | 135 | if save_img or view_img: # Add bbox to image 136 | label = '%s %.2f' % (names[int(cls)], conf) 137 | np_array,imgd = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 138 | else: 139 | imgd = im0 140 | 141 | return np_array,imgd 142 | 143 | 144 | # def filename1(file1): 145 | # with open ('submissions.txt', 'w') as file2: 146 | # file2.write() 147 | 148 | 149 | 150 | 151 | 152 | 153 | with torch.no_grad(): 154 | 155 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 156 | out_cls = ['D00','D10','D20','D40'] 157 | # cnts = ['Japan','Czech'] 158 | cnts = ['Japan'] #VM 159 | for cnt in cnts: 160 | img_source =os.path.join('../Trainer/test2',cnt,'images') #Pass the correct path of the test2 dataset 161 | # img_source = '../Trainer/test1/India/images' 162 | imgs = os.listdir(img_source) 163 | for imgcur in imgs: 164 | vid_source = os.path.join(img_source,imgcur) 165 | np_array, imgd = detect(vid_source) 166 | annts_list = [] 167 | for annts in np_array: 168 | if clss[annts[5]] in out_cls: 169 | annts_list.append([annts[5]+1,annts[0],annts[1],annts[2],annts[3]]) 170 | 171 | if len(annts_list)>0: 172 | sub_list = [imgcur]+np.concatenate(annts_list).tolist() 173 | # print ("final output:",np.array(sub_list)) 174 | # print("LENGTH", len(sub_list)) 175 | with open('ttt3_test2.txt','a+') as writer: 176 | txt=str(sub_list[0])+"," 177 | for i in range(1,len(sub_list)): 178 | txt+=str(sub_list[i])+' ' 179 | txt+='\n' 180 | writer.write(txt) 181 | # filename1=np.array(sub_list #vm 182 | # write to submission.txt 183 | 184 | # converter() #vm 185 | 186 | else: 187 | print ("IMGCURL:",[imgcur]) 188 | with open('ttt3_test2.txt','a+') as writer: 189 | txt=str(imgcur)+","+"\n" 190 | writer.write(txt) 191 | 192 | # write to submission.txt 193 | 194 | 195 | # converter(): #vm 196 | # cv2.imshow('',imgd) 197 | 198 | # cv2.waitKey(1) 199 | 200 | 201 | -------------------------------------------------------------------------------- /Proposed Algorithm/trial.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | import os 7 | 8 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 9 | 10 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 11 | print (clss.index(label.split(' ')[0])) 12 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 13 | color = color or [random.randint(0, 255) for _ in range(3)] 14 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 15 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 16 | # np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 17 | np_array.append([x1,y1,x2,y2,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 18 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 19 | # if label: 20 | # tf = max(tl - 1, 1) # font thickness 21 | # t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 22 | # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 23 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 24 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 25 | # cv2.imshow('',img) 26 | # cv2.waitKey(1000) 27 | return np_array,img 28 | 29 | def detect(source, save_img=True): 30 | 31 | weights = 'weights/pave/Vishal/yolo_vl/54jc/last.pt' #Pass the correct path of the model 32 | view_img = True 33 | save_txt = False 34 | imgsz = 640 35 | device = '' 36 | half = False 37 | augment=False 38 | iou_thres = 1 39 | # classes = None;conf_thres = 0.01;agnostic_nms = False 40 | classes = None;conf_thres = 0.17;agnostic_nms = False #Vm 41 | 42 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 43 | 44 | # Initialize 45 | device = torch_utils.select_device(device) 46 | 47 | # Load model 48 | google_utils.attempt_download(weights) 49 | model = torch.load(weights, map_location=device)['model'] 50 | model.to(device).eval() 51 | 52 | # Second-stage classifier 53 | classify = False 54 | if classify: 55 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 56 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 57 | modelc.to(device).eval() 58 | 59 | # Half precision 60 | half = half and device.type != 'cpu' # half precision only supported on CUDA 61 | if half: 62 | model.half() 63 | 64 | # Set Dataloader 65 | vid_path, vid_writer = None, None 66 | if webcam: 67 | view_img = True 68 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 69 | dataset = LoadStreams(source, img_size=imgsz) 70 | else: 71 | save_img = True 72 | dataset = LoadImages(source, img_size=imgsz) 73 | 74 | # Get names and colors 75 | names = model.names if hasattr(model, 'names') else model.modules.names 76 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 77 | 78 | # Run inference 79 | t0 = time.time() 80 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 81 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 82 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 83 | np_array = [] 84 | for path, img, im0s,vc in dataset: 85 | 86 | # print (ftime) 87 | img = torch.from_numpy(img).to(device) 88 | img = img.half() if half else img.float() # uint8 to fp16/32 89 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 90 | if img.ndimension() == 3: 91 | img = img.unsqueeze(0) 92 | 93 | # Inference 94 | t1 = torch_utils.time_synchronized() 95 | pred = model(img, augment=augment)[0] 96 | t2 = torch_utils.time_synchronized() 97 | 98 | # to float 99 | if half: 100 | pred = pred.float() 101 | 102 | # Apply NMS 103 | pred = non_max_suppression(pred, conf_thres, iou_thres, 104 | fast=True, classes=classes, agnostic=agnostic_nms) 105 | 106 | # Apply Classifier 107 | if classify: 108 | pred = apply_classifier(pred, modelc, img, im0s) 109 | 110 | # Process detections 111 | for i, det in enumerate(pred): # detections per image 112 | if webcam: # batch_size >= 1 113 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 114 | else: 115 | p, s, im0 = path, '', im0s 116 | 117 | s += '%gx%g ' % img.shape[2:] # print string 118 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 119 | if det is not None and len(det): 120 | # Rescale boxes from img_size to im0 size 121 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 122 | 123 | # Print results 124 | for c in det[:, -1].unique(): 125 | n = (det[:, -1] == c).sum() # detections per class 126 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 127 | 128 | # Write results 129 | for *xyxy, conf, cls in det: 130 | if save_txt: # Write to file 131 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 132 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 133 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 134 | 135 | if save_img or view_img: # Add bbox to image 136 | label = '%s %.2f' % (names[int(cls)], conf) 137 | np_array,imgd = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 138 | else: 139 | imgd = im0 140 | 141 | return np_array,imgd 142 | 143 | 144 | # def filename1(file1): 145 | # with open ('submissions.txt', 'w') as file2: 146 | # file2.write() 147 | 148 | 149 | 150 | 151 | 152 | 153 | with torch.no_grad(): 154 | 155 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 156 | out_cls = ['D00','D10','D20','D40'] 157 | # cnts = ['Japan','Czech'] 158 | cnts = ['Japan'] #VM 159 | for cnt in cnts: 160 | img_source =os.path.join('../Trainer/test1',cnt,'images') #Pass the correct path of the test2 dataset 161 | # img_source = '../Trainer/test1/India/images' 162 | imgs = os.listdir(img_source) 163 | for imgcur in imgs: 164 | vid_source = os.path.join(img_source,imgcur) 165 | np_array, imgd = detect(vid_source) 166 | annts_list = [] 167 | for annts in np_array: 168 | if clss[annts[5]] in out_cls: 169 | annts_list.append([annts[5]+1,annts[0],annts[1],annts[2],annts[3]]) 170 | 171 | if len(annts_list)>0: 172 | sub_list = [imgcur]+np.concatenate(annts_list).tolist() 173 | # print ("final output:",np.array(sub_list)) 174 | # print("LENGTH", len(sub_list)) 175 | with open('tttt3_new.txt','a+') as writer: 176 | txt=str(sub_list[0])+"," 177 | for i in range(1,len(sub_list)): 178 | txt+=str(sub_list[i])+' ' 179 | txt+='\n' 180 | writer.write(txt) 181 | # filename1=np.array(sub_list #vm 182 | # write to submission.txt 183 | 184 | # converter() #vm 185 | 186 | else: 187 | print ("IMGCURL:",[imgcur]) 188 | with open('tttt3_new.txt','a+') as writer: 189 | txt=str(imgcur)+","+"\n" 190 | writer.write(txt) 191 | 192 | # write to submission.txt 193 | 194 | 195 | # converter(): #vm 196 | # cv2.imshow('',imgd) 197 | 198 | # cv2.waitKey(1) 199 | 200 | 201 | -------------------------------------------------------------------------------- /Proposed Algorithm/trial_Czech.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | import os 7 | 8 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 9 | 10 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 11 | print (clss.index(label.split(' ')[0])) 12 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 13 | color = color or [random.randint(0, 255) for _ in range(3)] 14 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 15 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 16 | # np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 17 | np_array.append([x1,y1,x2,y2,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 18 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 19 | # if label: 20 | # tf = max(tl - 1, 1) # font thickness 21 | # t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 22 | # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 23 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 24 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 25 | # cv2.imshow('',img) 26 | # cv2.waitKey(1000) 27 | return np_array,img 28 | 29 | def detect(source, save_img=True): 30 | 31 | weights = 'weights/pave/Vishal/66JC/last.pt' #Pass the correct path of the model 32 | view_img = True 33 | save_txt = False 34 | imgsz = 640 35 | device = '' 36 | half = False 37 | augment=False 38 | iou_thres = 0.5 39 | # classes = None;conf_thres = 0.01;agnostic_nms = False 40 | classes = None;conf_thres = 0.30;agnostic_nms = False #Vm 41 | 42 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 43 | 44 | # Initialize 45 | device = torch_utils.select_device(device) 46 | 47 | # Load model 48 | google_utils.attempt_download(weights) 49 | model = torch.load(weights, map_location=device)['model'] 50 | model.to(device).eval() 51 | 52 | # Second-stage classifier 53 | classify = False 54 | if classify: 55 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 56 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 57 | modelc.to(device).eval() 58 | 59 | # Half precision 60 | half = half and device.type != 'cpu' # half precision only supported on CUDA 61 | if half: 62 | model.half() 63 | 64 | # Set Dataloader 65 | vid_path, vid_writer = None, None 66 | if webcam: 67 | view_img = True 68 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 69 | dataset = LoadStreams(source, img_size=imgsz) 70 | else: 71 | save_img = True 72 | dataset = LoadImages(source, img_size=imgsz) 73 | 74 | # Get names and colors 75 | names = model.names if hasattr(model, 'names') else model.modules.names 76 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 77 | 78 | # Run inference 79 | t0 = time.time() 80 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 81 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 82 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 83 | np_array = [] 84 | for path, img, im0s,vc in dataset: 85 | 86 | # print (ftime) 87 | img = torch.from_numpy(img).to(device) 88 | img = img.half() if half else img.float() # uint8 to fp16/32 89 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 90 | if img.ndimension() == 3: 91 | img = img.unsqueeze(0) 92 | 93 | # Inference 94 | t1 = torch_utils.time_synchronized() 95 | pred = model(img, augment=augment)[0] 96 | t2 = torch_utils.time_synchronized() 97 | 98 | # to float 99 | if half: 100 | pred = pred.float() 101 | 102 | # Apply NMS 103 | pred = non_max_suppression(pred, conf_thres, iou_thres, 104 | fast=True, classes=classes, agnostic=agnostic_nms) 105 | 106 | # Apply Classifier 107 | if classify: 108 | pred = apply_classifier(pred, modelc, img, im0s) 109 | 110 | # Process detections 111 | for i, det in enumerate(pred): # detections per image 112 | if webcam: # batch_size >= 1 113 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 114 | else: 115 | p, s, im0 = path, '', im0s 116 | 117 | s += '%gx%g ' % img.shape[2:] # print string 118 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 119 | if det is not None and len(det): 120 | # Rescale boxes from img_size to im0 size 121 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 122 | 123 | # Print results 124 | for c in det[:, -1].unique(): 125 | n = (det[:, -1] == c).sum() # detections per class 126 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 127 | 128 | # Write results 129 | for *xyxy, conf, cls in det: 130 | if save_txt: # Write to file 131 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 132 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 133 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 134 | 135 | if save_img or view_img: # Add bbox to image 136 | label = '%s %.2f' % (names[int(cls)], conf) 137 | np_array,imgd = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 138 | else: 139 | imgd = im0 140 | 141 | return np_array,imgd 142 | 143 | 144 | # def filename1(file1): 145 | # with open ('submissions.txt', 'w') as file2: 146 | # file2.write() 147 | 148 | 149 | 150 | 151 | 152 | 153 | with torch.no_grad(): 154 | 155 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 156 | out_cls = ['D00','D10','D20','D40'] 157 | # cnts = ['Japan','Czech'] 158 | cnts = ['Czech'] #VM 159 | for cnt in cnts: 160 | img_source =os.path.join('../Trainer/test1',cnt,'images') #Pass the correct path of the test1 dataset 161 | # img_source = '../Trainer/test1/India/images' 162 | imgs = os.listdir(img_source) 163 | for imgcur in imgs: 164 | vid_source = os.path.join(img_source,imgcur) 165 | np_array, imgd = detect(vid_source) 166 | annts_list = [] 167 | for annts in np_array: 168 | if clss[annts[5]] in out_cls: 169 | annts_list.append([annts[5]+1,annts[0],annts[1],annts[2],annts[3]]) 170 | 171 | if len(annts_list)>0: 172 | sub_list = [imgcur]+np.concatenate(annts_list).tolist() 173 | # print ("final output:",np.array(sub_list)) 174 | # print("LENGTH", len(sub_list)) 175 | with open('tttt3_new.txt','a+') as writer: 176 | txt=str(sub_list[0])+"," 177 | for i in range(1,len(sub_list)): 178 | txt+=str(sub_list[i])+' ' 179 | txt+='\n' 180 | writer.write(txt) 181 | # filename1=np.array(sub_list #vm 182 | # write to submission.txt 183 | 184 | # converter() #vm 185 | 186 | else: 187 | print ("IMGCURL:",[imgcur]) 188 | with open('tttt3_new.txt','a+') as writer: 189 | txt=str(imgcur)+","+"\n" 190 | writer.write(txt) 191 | 192 | # write to submission.txt 193 | 194 | 195 | # converter(): #vm 196 | # cv2.imshow('',imgd) 197 | 198 | # cv2.waitKey(1) 199 | 200 | 201 | -------------------------------------------------------------------------------- /Proposed Algorithm/trial_Czech2.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | import os 7 | 8 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 9 | 10 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 11 | print (clss.index(label.split(' ')[0])) 12 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 13 | color = color or [random.randint(0, 255) for _ in range(3)] 14 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 15 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 16 | # np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 17 | np_array.append([x1,y1,x2,y2,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 18 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 19 | # if label: 20 | # tf = max(tl - 1, 1) # font thickness 21 | # t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 22 | # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 23 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 24 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 25 | # cv2.imshow('',img) 26 | # cv2.waitKey(1000) 27 | return np_array,img 28 | 29 | def detect(source, save_img=True): 30 | 31 | weights = 'weights/pave/Vishal/yolo_vl/54jc/last.pt' #Pass the correct path of the model 32 | view_img = True 33 | save_txt = False 34 | imgsz = 640 35 | device = '' 36 | half = False 37 | augment=False 38 | iou_thres = 0.5 39 | # classes = None;conf_thres = 0.01;agnostic_nms = False 40 | classes = None;conf_thres = 0.05;agnostic_nms = False #Vm 41 | 42 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 43 | 44 | # Initialize 45 | device = torch_utils.select_device(device) 46 | 47 | # Load model 48 | google_utils.attempt_download(weights) 49 | model = torch.load(weights, map_location=device)['model'] 50 | model.to(device).eval() 51 | 52 | # Second-stage classifier 53 | classify = False 54 | if classify: 55 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 56 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 57 | modelc.to(device).eval() 58 | 59 | # Half precision 60 | half = half and device.type != 'cpu' # half precision only supported on CUDA 61 | if half: 62 | model.half() 63 | 64 | # Set Dataloader 65 | vid_path, vid_writer = None, None 66 | if webcam: 67 | view_img = True 68 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 69 | dataset = LoadStreams(source, img_size=imgsz) 70 | else: 71 | save_img = True 72 | dataset = LoadImages(source, img_size=imgsz) 73 | 74 | # Get names and colors 75 | names = model.names if hasattr(model, 'names') else model.modules.names 76 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 77 | 78 | # Run inference 79 | t0 = time.time() 80 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 81 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 82 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 83 | np_array = [] 84 | for path, img, im0s,vc in dataset: 85 | 86 | # print (ftime) 87 | img = torch.from_numpy(img).to(device) 88 | img = img.half() if half else img.float() # uint8 to fp16/32 89 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 90 | if img.ndimension() == 3: 91 | img = img.unsqueeze(0) 92 | 93 | # Inference 94 | t1 = torch_utils.time_synchronized() 95 | pred = model(img, augment=augment)[0] 96 | t2 = torch_utils.time_synchronized() 97 | 98 | # to float 99 | if half: 100 | pred = pred.float() 101 | 102 | # Apply NMS 103 | pred = non_max_suppression(pred, conf_thres, iou_thres, 104 | fast=True, classes=classes, agnostic=agnostic_nms) 105 | 106 | # Apply Classifier 107 | if classify: 108 | pred = apply_classifier(pred, modelc, img, im0s) 109 | 110 | # Process detections 111 | for i, det in enumerate(pred): # detections per image 112 | if webcam: # batch_size >= 1 113 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 114 | else: 115 | p, s, im0 = path, '', im0s 116 | 117 | s += '%gx%g ' % img.shape[2:] # print string 118 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 119 | if det is not None and len(det): 120 | # Rescale boxes from img_size to im0 size 121 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 122 | 123 | # Print results 124 | for c in det[:, -1].unique(): 125 | n = (det[:, -1] == c).sum() # detections per class 126 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 127 | 128 | # Write results 129 | for *xyxy, conf, cls in det: 130 | if save_txt: # Write to file 131 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 132 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 133 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 134 | 135 | if save_img or view_img: # Add bbox to image 136 | label = '%s %.2f' % (names[int(cls)], conf) 137 | np_array,imgd = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 138 | else: 139 | imgd = im0 140 | 141 | return np_array,imgd 142 | 143 | 144 | # def filename1(file1): 145 | # with open ('submissions.txt', 'w') as file2: 146 | # file2.write() 147 | 148 | 149 | 150 | 151 | 152 | 153 | with torch.no_grad(): 154 | 155 | clss = ['D00','D10','D20','D40','D43','D44','D50'] 156 | out_cls = ['D00','D10','D20','D40'] 157 | # cnts = ['Japan','Czech'] 158 | cnts = ['Czech'] #VM 159 | for cnt in cnts: 160 | img_source =os.path.join('../Trainer/test2',cnt,'images') #Pass the Path of the test2 dataset 161 | # img_source = '../Trainer/test1/India/images' 162 | imgs = os.listdir(img_source) 163 | for imgcur in imgs: 164 | vid_source = os.path.join(img_source,imgcur) 165 | np_array, imgd = detect(vid_source) 166 | annts_list = [] 167 | for annts in np_array: 168 | if clss[annts[5]] in out_cls: 169 | annts_list.append([annts[5]+1,annts[0],annts[1],annts[2],annts[3]]) 170 | 171 | if len(annts_list)>0: 172 | sub_list = [imgcur]+np.concatenate(annts_list).tolist() 173 | # print ("final output:",np.array(sub_list)) 174 | # print("LENGTH", len(sub_list)) 175 | with open('ttt3_test2.txt','a+') as writer: 176 | txt=str(sub_list[0])+"," 177 | for i in range(1,len(sub_list)): 178 | txt+=str(sub_list[i])+' ' 179 | txt+='\n' 180 | writer.write(txt) 181 | # filename1=np.array(sub_list #vm 182 | # write to submission.txt 183 | 184 | # converter() #vm 185 | 186 | else: 187 | print ("IMGCURL:",[imgcur]) 188 | with open('ttt3_test2.txt','a+') as writer: 189 | txt=str(imgcur)+","+"\n" 190 | writer.write(txt) 191 | 192 | # write to submission.txt 193 | 194 | 195 | # converter(): #vm 196 | # cv2.imshow('',imgd) 197 | 198 | # cv2.waitKey(1) 199 | 200 | 201 | -------------------------------------------------------------------------------- /Proposed Algorithm/getDetections.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from utils.datasets import * 4 | import pandas as pd 5 | from utils.utils import * 6 | 7 | 8 | def show_annotations(input_folder,cimg): 9 | cimg_path = os.path.join(input_folder,cimg) 10 | cannt_path = os.path.join(input_folder,cimg.replace('.jpg','.txt')) 11 | if os.path.isfile(cannt_path): 12 | img_file__ = cv2.imread(cimg_path); 13 | annt_file = np.loadtxt(cannt_path) 14 | 15 | img_h, img_w, _ = img_file__.shape 16 | img_w, img_h 17 | dims = annt_file.ndim 18 | annt_list = [] 19 | if dims > 1: 20 | for cbox in annt_file: 21 | # print (cbox) 22 | clsv = cbox[0] 23 | cbox = cbox[1:] 24 | 25 | x11, y11 = int((cbox[0] - cbox[2]/2)*img_w), int((cbox[1] - cbox[3]/2)*img_h) 26 | x22, y22 = int((cbox[0] + cbox[2]/2)*img_w), int((cbox[1] + cbox[3]/2)*img_h) 27 | img_file__ = cv2.rectangle(img_file__, (x11, y11), (x22, y22), (0,255,0), 3) 28 | annt_list.append([clsv,x11,y11,x22,y22]) 29 | else: 30 | clsv=annt_file[0] 31 | cbox = annt_file[1:] 32 | x11, y11 = int((cbox[0] - cbox[2]/2)*img_w), int((cbox[1] - cbox[3]/2)*img_h) 33 | x22, y22 = int((cbox[0] + cbox[2]/2)*img_w), int((cbox[1] + cbox[3]/2)*img_h) 34 | annt_list.append([clsv,x11,y11,x22,y22]) 35 | # img_file__ = cv2.rectangle(img_file__, (x11, y11), (x22, y22), (0,255,0), 3) 36 | # cv2.imshow('',img_file__) 37 | # cv2.waitKey(0) 38 | else: 39 | annt_list = [] 40 | return annt_list 41 | 42 | def vis_box(x, img, path, np_array, color=None, label=None, line_thickness=None): 43 | # Plots one bounding box on image img 44 | 45 | # img_name = os.path.basename(path) 46 | # ann_list = show_annotations('training_img_annt',img_name) 47 | # for cann in ann_list: 48 | # cv2.rectangle(img, (cann[1],cann[2]), (cann[3],cann[4]), (255,255,255), thickness=5, lineType=cv2.LINE_AA) 49 | 50 | clss = ['D00','D10','D20','D40','D44'] 51 | print (clss.index(label.split(' ')[0])) 52 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 53 | color = color or [random.randint(0, 255) for _ in range(3)] 54 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 55 | x1,y1,x2,y2 = int(x[0]), int(x[1]), int(x[2]), int(x[3]) 56 | np_array.append([x1,y1,x2-x1,y2-y1,label.split(' ')[-1],clss.index(label.split(' ')[0]),1,1]) 57 | cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 58 | if label: 59 | tf = max(tl - 1, 1) # font thickness 60 | t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 61 | c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 62 | cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 63 | cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 64 | cv2.imshow('',img) 65 | cv2.waitKey(500) 66 | return np_array 67 | 68 | def detect(source, save_img=True): 69 | 70 | weights = 'weights/pave/best.pt' 71 | view_img = True 72 | save_txt = False 73 | imgsz = 640 74 | device = '' 75 | half = False 76 | augment=False 77 | iou_thres = 0.1 78 | classes = None;conf_thres = 0.2;agnostic_nms = False 79 | 80 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 81 | 82 | # Initialize 83 | device = torch_utils.select_device(device) 84 | 85 | # Load model 86 | google_utils.attempt_download(weights) 87 | model = torch.load(weights, map_location=device)['model'] 88 | model.to(device).eval() 89 | 90 | # Second-stage classifier 91 | classify = False 92 | if classify: 93 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 94 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 95 | modelc.to(device).eval() 96 | 97 | # Half precision 98 | half = half and device.type != 'cpu' # half precision only supported on CUDA 99 | if half: 100 | model.half() 101 | 102 | # Set Dataloader 103 | vid_path, vid_writer = None, None 104 | if webcam: 105 | view_img = True 106 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 107 | dataset = LoadStreams(source, img_size=imgsz) 108 | else: 109 | save_img = True 110 | dataset = LoadImages(source, img_size=imgsz) 111 | 112 | # Get names and colors 113 | names = model.names if hasattr(model, 'names') else model.modules.names 114 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 115 | 116 | # Run inference 117 | t0 = time.time() 118 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 119 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 120 | # df=pd.DataFrame(columns=['x','y','w','h','score','cls','frame','time']) 121 | np_array = [] 122 | for path, img, im0s,vc in dataset: 123 | 124 | # print (ftime) 125 | img = torch.from_numpy(img).to(device) 126 | img = img.half() if half else img.float() # uint8 to fp16/32 127 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 128 | if img.ndimension() == 3: 129 | img = img.unsqueeze(0) 130 | 131 | # Inference 132 | t1 = torch_utils.time_synchronized() 133 | pred = model(img, augment=augment)[0] 134 | t2 = torch_utils.time_synchronized() 135 | 136 | # to float 137 | if half: 138 | pred = pred.float() 139 | 140 | # Apply NMS 141 | pred = non_max_suppression(pred, conf_thres, iou_thres, 142 | fast=True, classes=classes, agnostic=agnostic_nms) 143 | 144 | # Apply Classifier 145 | if classify: 146 | pred = apply_classifier(pred, modelc, img, im0s) 147 | 148 | # Process detections 149 | for i, det in enumerate(pred): # detections per image 150 | if webcam: # batch_size >= 1 151 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 152 | else: 153 | p, s, im0 = path, '', im0s 154 | 155 | s += '%gx%g ' % img.shape[2:] # print string 156 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 157 | if det is not None and len(det): 158 | # Rescale boxes from img_size to im0 size 159 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 160 | 161 | # Print results 162 | for c in det[:, -1].unique(): 163 | n = (det[:, -1] == c).sum() # detections per class 164 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 165 | 166 | # Write results 167 | for *xyxy, conf, cls in det: 168 | if save_txt: # Write to file 169 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 170 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 171 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 172 | 173 | if save_img or view_img: # Add bbox to image 174 | label = '%s %.2f' % (names[int(cls)], conf) 175 | # print ('saving images') 176 | np_array = vis_box(xyxy, im0, path, np_array, label=label, color=colors[int(cls)], line_thickness=3) 177 | 178 | # print (df.tail()) 179 | # print (len(np_array)) 180 | return np_array 181 | 182 | with torch.no_grad(): 183 | # cur_det_folder = os.path.join('../../paper/output', 'detections','yolov5') 184 | # if not (os.path.isdir(cur_det_folder)): 185 | # os.makedirs(cur_det_folder) 186 | 187 | img_source = '/media/ai/New Volume1/dev/aicity/pavement/test1/Japan/images' 188 | # img_source = 'vids' 189 | imgs = os.listdir(img_source) 190 | for imgcur in imgs: 191 | vid_source = os.path.join(img_source,imgcur) 192 | np_array = detect(vid_source) 193 | # df=pd.DataFrame(np_array, columns=['x','y','w','h','score','cls','frame','time']) 194 | 195 | # df.to_csv(os.path.join(det_folder, vidname+'.csv'),index=False,header=True) 196 | 197 | 198 | -------------------------------------------------------------------------------- /Proposed Algorithm/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 | 11 | 12 | def init_seeds(seed=0): 13 | torch.manual_seed(seed) 14 | 15 | # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html 16 | if seed == 0: # slower, more reproducible 17 | cudnn.deterministic = True 18 | cudnn.benchmark = False 19 | else: # faster, less reproducible 20 | cudnn.deterministic = False 21 | cudnn.benchmark = True 22 | 23 | 24 | def select_device(device='', apex=False, batch_size=None): 25 | # device = 'cpu' or '0' or '0,1,2,3' 26 | cpu_request = device.lower() == 'cpu' 27 | if device and not cpu_request: # if device requested other than 'cpu' 28 | os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable 29 | assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device # check availablity 30 | 31 | cuda = False if cpu_request else torch.cuda.is_available() 32 | if cuda: 33 | c = 1024 ** 2 # bytes to MB 34 | ng = torch.cuda.device_count() 35 | if ng > 1 and batch_size: # check that batch_size is compatible with device_count 36 | assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng) 37 | x = [torch.cuda.get_device_properties(i) for i in range(ng)] 38 | s = 'Using CUDA ' + ('Apex ' if apex else '') # apex for mixed precision https://github.com/NVIDIA/apex 39 | for i in range(0, ng): 40 | if i == 1: 41 | s = ' ' * len(s) 42 | print("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" % 43 | (s, i, x[i].name, x[i].total_memory / c)) 44 | else: 45 | print('Using CPU') 46 | 47 | print('') # skip a line 48 | return torch.device('cuda:0' if cuda else 'cpu') 49 | 50 | 51 | def time_synchronized(): 52 | torch.cuda.synchronize() if torch.cuda.is_available() else None 53 | return time.time() 54 | 55 | 56 | def initialize_weights(model): 57 | for m in model.modules(): 58 | t = type(m) 59 | if t is nn.Conv2d: 60 | pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') 61 | elif t is nn.BatchNorm2d: 62 | m.eps = 1e-4 63 | m.momentum = 0.03 64 | elif t in [nn.LeakyReLU, nn.ReLU, nn.ReLU6]: 65 | m.inplace = True 66 | 67 | 68 | def find_modules(model, mclass=nn.Conv2d): 69 | # finds layer indices matching module class 'mclass' 70 | return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] 71 | 72 | 73 | def fuse_conv_and_bn(conv, bn): 74 | # https://tehnokv.com/posts/fusing-batchnorm-and-conv/ 75 | with torch.no_grad(): 76 | # init 77 | fusedconv = torch.nn.Conv2d(conv.in_channels, 78 | conv.out_channels, 79 | kernel_size=conv.kernel_size, 80 | stride=conv.stride, 81 | padding=conv.padding, 82 | bias=True) 83 | 84 | # prepare filters 85 | w_conv = conv.weight.clone().view(conv.out_channels, -1) 86 | w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) 87 | fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size())) 88 | 89 | # prepare spatial bias 90 | if conv.bias is not None: 91 | b_conv = conv.bias 92 | else: 93 | b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) 94 | b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) 95 | fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) 96 | 97 | return fusedconv 98 | 99 | 100 | def model_info(model, verbose=False): 101 | # Plots a line-by-line description of a PyTorch model 102 | n_p = sum(x.numel() for x in model.parameters()) # number parameters 103 | n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients 104 | if verbose: 105 | print('%5s %40s %9s %12s %20s %10s %10s' % ('layer', 'name', 'gradient', 'parameters', 'shape', 'mu', 'sigma')) 106 | for i, (name, p) in enumerate(model.named_parameters()): 107 | name = name.replace('module_list.', '') 108 | print('%5g %40s %9s %12g %20s %10.3g %10.3g' % 109 | (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) 110 | 111 | try: # FLOPS 112 | from thop import profile 113 | macs, _ = profile(model, inputs=(torch.zeros(1, 3, 480, 640),), verbose=False) 114 | fs = ', %.1f GFLOPS' % (macs / 1E9 * 2) 115 | except: 116 | fs = '' 117 | 118 | print('Model Summary: %g layers, %g parameters, %g gradients%s' % (len(list(model.parameters())), n_p, n_g, fs)) 119 | 120 | 121 | def load_classifier(name='resnet101', n=2): 122 | # Loads a pretrained model reshaped to n-class output 123 | import pretrainedmodels # https://github.com/Cadene/pretrained-models.pytorch#torchvision 124 | model = pretrainedmodels.__dict__[name](num_classes=1000, pretrained='imagenet') 125 | 126 | # Display model properties 127 | for x in ['model.input_size', 'model.input_space', 'model.input_range', 'model.mean', 'model.std']: 128 | print(x + ' =', eval(x)) 129 | 130 | # Reshape output to n classes 131 | filters = model.last_linear.weight.shape[1] 132 | model.last_linear.bias = torch.nn.Parameter(torch.zeros(n)) 133 | model.last_linear.weight = torch.nn.Parameter(torch.zeros(n, filters)) 134 | model.last_linear.out_features = n 135 | return model 136 | 137 | 138 | def scale_img(img, ratio=1.0, same_shape=False): # img(16,3,256,416), r=ratio 139 | # scales img(bs,3,y,x) by ratio 140 | h, w = img.shape[2:] 141 | s = (int(h * ratio), int(w * ratio)) # new size 142 | img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize 143 | if not same_shape: # pad/crop img 144 | gs = 32 # (pixels) grid size 145 | h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)] 146 | return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean 147 | 148 | 149 | class ModelEMA: 150 | """ Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models 151 | Keep a moving average of everything in the model state_dict (parameters and buffers). 152 | This is intended to allow functionality like 153 | https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage 154 | A smoothed version of the weights is necessary for some training schemes to perform well. 155 | E.g. Google's hyper-params for training MNASNet, MobileNet-V3, EfficientNet, etc that use 156 | RMSprop with a short 2.4-3 epoch decay period and slow LR decay rate of .96-.99 requires EMA 157 | smoothing of weights to match results. Pay attention to the decay constant you are using 158 | relative to your update count per epoch. 159 | To keep EMA from using GPU resources, set device='cpu'. This will save a bit of memory but 160 | disable validation of the EMA weights. Validation will have to be done manually in a separate 161 | process, or after the training stops converging. 162 | This class is sensitive where it is initialized in the sequence of model init, 163 | GPU assignment and distributed training wrappers. 164 | I've tested with the sequence in my own train.py for torch.DataParallel, apex.DDP, and single-GPU. 165 | """ 166 | 167 | def __init__(self, model, decay=0.9999, device=''): 168 | # make a copy of the model for accumulating moving average of weights 169 | self.ema = deepcopy(model) 170 | self.ema.eval() 171 | self.updates = 0 # number of EMA updates 172 | self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) # decay exponential ramp (to help early epochs) 173 | self.device = device # perform ema on different device from model if set 174 | if device: 175 | self.ema.to(device=device) 176 | for p in self.ema.parameters(): 177 | p.requires_grad_(False) 178 | 179 | def update(self, model): 180 | self.updates += 1 181 | d = self.decay(self.updates) 182 | with torch.no_grad(): 183 | if type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel): 184 | msd, esd = model.module.state_dict(), self.ema.module.state_dict() 185 | else: 186 | msd, esd = model.state_dict(), self.ema.state_dict() 187 | 188 | for k, v in esd.items(): 189 | if v.dtype.is_floating_point: 190 | v *= d 191 | v += (1. - d) * msd[k].detach() 192 | 193 | def update_attr(self, model): 194 | # Assign attributes (which may change during training) 195 | for k in model.__dict__.keys(): 196 | if not k.startswith('_'): 197 | setattr(self.ema, k, getattr(model, k)) 198 | -------------------------------------------------------------------------------- /Proposed Algorithm/accuracy.py: -------------------------------------------------------------------------------- 1 | import os, cv2, shutil, random, toml, json, math 2 | import pandas as pd 3 | import xml.etree.ElementTree as ET 4 | import numpy as np 5 | from detect_di import detect 6 | from scipy import ndimage 7 | from skimage import transform 8 | 9 | 10 | def testGauss(x_in, y_in): 11 | 12 | def gauss(): 13 | n = round(len(x_in)/3) 14 | sigma = 10 15 | r = range(-int(n / 2), int(n / 2) + 1) 16 | return [1 / (sigma * sqrt(2 * pi)) * exp(-float(x) ** 2 / (2 * sigma ** 2)) for x in r] 17 | 18 | b = gauss() 19 | x_filt = filters.convolve1d(x_in, np.array(b)/sum(b)) 20 | y_filt = filters.convolve1d(y_in, np.array(b) / sum(b)) 21 | return x_filt, y_filt 22 | 23 | def get_iou(a, b, epsilon=1e-5): 24 | x1 = max(a[0], b[0]) 25 | y1 = max(a[1], b[1]) 26 | x2 = min(a[2], b[2]) 27 | y2 = min(a[3], b[3]) 28 | 29 | # AREA OF OVERLAP - Area where the boxes intersect 30 | width = (x2 - x1) 31 | height = (y2 - y1) 32 | # handle case where there is NO overlap 33 | if (width<0) or (height <0): 34 | return 0.0 35 | area_overlap = width * height 36 | 37 | # COMBINED AREA 38 | area_a = (a[2] - a[0]) * (a[3] - a[1]) 39 | area_b = (b[2] - b[0]) * (b[3] - b[1]) 40 | area_combined = area_a + area_b - area_overlap 41 | 42 | # RATIO OF AREA OF OVERLAP OVER COMBINED AREA 43 | iou = area_overlap / (area_combined+epsilon) 44 | return iou 45 | 46 | def get_gt(input_folder,cimg): 47 | cimg_path = os.path.join(input_folder,cimg) 48 | cannt_path = os.path.join(input_folder,cimg.replace('.jpg','.txt')) 49 | img_file__ = cv2.imread(cimg_path) 50 | annt_file = np.loadtxt(cannt_path) 51 | img_h, img_w, _ = img_file__.shape 52 | img_w, img_h 53 | dims = annt_file.ndim 54 | dets_info = [] 55 | if dims > 1: 56 | for cbox in annt_file: 57 | cls=cbox[0] 58 | cbox = cbox[1:] 59 | x11, y11 = int((cbox[0] - cbox[2]/2)*img_w), int((cbox[1] - cbox[3]/2)*img_h) 60 | x22, y22 = int((cbox[0] + cbox[2]/2)*img_w), int((cbox[1] + cbox[3]/2)*img_h) 61 | dets_info.append([x11,y11,x22,y22,cls]) 62 | else: 63 | cls=annt_file[0] 64 | cbox = annt_file[1:] 65 | x11, y11 = int((cbox[0] - cbox[2]/2)*img_w), int((cbox[1] - cbox[3]/2)*img_h) 66 | x22, y22 = int((cbox[0] + cbox[2]/2)*img_w), int((cbox[1] + cbox[3]/2)*img_h) 67 | img_file__ = cv2.rectangle(img_file__, (x11, y11), (x22, y22), (0,255,0), 3) 68 | dets_info.append([x11,y11,x22,y22,cls]) 69 | return dets_info 70 | 71 | def get_tfps(dets_vals, dets_gt): 72 | tps_gt = []; tps_dets = []; fps_dets = [] 73 | for cur_det in dets_vals: 74 | box_info = []; iou_val = []; box_info_gt = [] 75 | for cur_gt in dets_gt: 76 | cur_iou = get_iou(cur_det, cur_gt) 77 | if cur_iou>0.4 and (cur_det[-1]==cur_gt[-1]): 78 | # if len(iou_val)>0: 79 | if iou_val: 80 | if cur_iou>iou_val: 81 | iou_val = cur_iou 82 | box_info = cur_det 83 | box_info_gt = cur_gt 84 | else: 85 | iou_val = cur_iou 86 | box_info = cur_det 87 | box_info_gt = cur_gt 88 | tps_dets.append(box_info) 89 | tps_gt.append(box_info_gt) 90 | if not(box_info): 91 | fps_dets.append(cur_det) 92 | # print (iou_val) 93 | return tps_dets, tps_gt, fps_dets 94 | 95 | def get_fns(dets_vals, dets_gt): 96 | tps_gt = []; tps_dets = []; fns_dets = [] 97 | for cur_det in dets_gt: 98 | box_info = []; iou_val = []; box_info_gt = [] 99 | for cur_gt in dets_vals: 100 | cur_iou = get_iou(cur_det, cur_gt) 101 | if cur_iou>0.4 and (cur_det[-1]==cur_gt[-1]): 102 | iou_val = cur_iou 103 | box_info = cur_det 104 | box_info_gt = cur_gt 105 | tps_dets.append(box_info) 106 | tps_gt.append(box_info_gt) 107 | if not(box_info): 108 | # print ('false_negative') 109 | fns_dets.append(cur_det) 110 | return fns_dets 111 | 112 | def vis_acc(df): 113 | df_imgs = df['filename'].unique() 114 | for df_img in df_imgs: 115 | df_filt = df[df['filename']==df_img] 116 | fn = df_filt.values[0][0] 117 | # print (fn) 118 | frame = cv2.imread(os.path.join(imgs,fn)) 119 | for data in df_filt.values: 120 | img_file, x1,y1,x2,y2, clsv = data 121 | cv2.rectangle(frame,(x1,y1),(x2,y2),(0,0,255),3) 122 | cv2.imshow('',frame) 123 | cv2.waitKey(1) 124 | 125 | def plot_heatmap(image,coords_fns, title): 126 | 127 | frame = cv2.imread(image) 128 | x = np.zeros((frame.shape[0], frame.shape[1])) 129 | font = cv2.FONT_HERSHEY_SIMPLEX 130 | cv2.putText(frame,title,(50,20), font, 1,(0,0,0), 1, 0) 131 | for cur_coord in coords_fns: 132 | # print (cur_coord) 133 | if not (math.isnan(cur_coord[1])): 134 | # x[ int(cur_coord[1]):int(cur_coord[3]),int(cur_coord[0]):int(cur_coord[2])] = 1 135 | x[ int(cur_coord[1]):int(cur_coord[3]),int(cur_coord[0]):int(cur_coord[2])] += 1 136 | heat_map = ndimage.filters.gaussian_filter(x, sigma=3) 137 | height = frame.shape[0] 138 | width = frame.shape[1] 139 | # resize heat map 140 | heat_map_resized = transform.resize(heat_map, (height, width)) 141 | # normalize heat map 142 | max_value = np.max(heat_map_resized) 143 | min_value = np.min(heat_map_resized) 144 | normalized_heat_map = (heat_map_resized - min_value) / (max_value - min_value) 145 | 146 | output = frame.copy() 147 | alpha = 0.7 148 | 149 | normalized_heat_map = 255*normalized_heat_map 150 | normalized_heat_map = cv2.applyColorMap(normalized_heat_map.astype(np.uint8),cv2.COLORMAP_JET) 151 | frame = cv2.addWeighted(frame, alpha, normalized_heat_map, 1 - alpha, 0, normalized_heat_map) 152 | return frame 153 | 154 | 155 | def display(preds, imgs,img): 156 | obj_list = ['car', 'motorcycle', 'truck','singl-unit'] 157 | det_list = [] 158 | for i in range(len(imgs)): 159 | if len(preds[i]['rois']) == 0: 160 | return imgs[i] 161 | 162 | for j in range(len(preds[i]['rois'])): 163 | (x1, y1, x2, y2) = preds[i]['rois'][j].astype(np.int) 164 | obj = obj_list[preds[i]['class_ids'][j]] 165 | score = float(preds[i]['scores'][j]) 166 | 167 | cur_row = [img, x1,y1,x2,y2,obj_list.index(obj)] 168 | det_list.append(cur_row) 169 | # df.loc[len(df)] = cur_row 170 | 171 | return det_list 172 | 173 | def detec_cars(imgs,img_path): 174 | # df = pd.DataFrame(columns = ['x','y','w','h','score','cls']) 175 | df=pd.DataFrame(columns=['filename','x1','y1','x2','y2','cls']) 176 | # df.to_csv(os.path.join(det_folder, vidname+'.csv'),index=False,header=True) 177 | for img in imgs: 178 | frame = os.path.join(img_path,img) 179 | frame = cv2.imread(frame) 180 | ori_imgs, framed_imgs, framed_metas = preprocess_video(frame, max_size=input_size) 181 | 182 | if use_cuda: 183 | x = torch.stack([torch.from_numpy(fi).cuda() for fi in framed_imgs], 0) 184 | else: 185 | x = torch.stack([torch.from_numpy(fi) for fi in framed_imgs], 0) 186 | 187 | x = x.to(torch.float32 if not use_float16 else torch.float16).permute(0, 3, 1, 2) 188 | 189 | # model predict 190 | with torch.no_grad(): 191 | features, regression, classification, anchors = model(x) 192 | 193 | out = postprocess(x, 194 | anchors, regression, classification, 195 | regressBoxes, clipBoxes, 196 | threshold, iou_threshold) 197 | 198 | # result 199 | out = invert_affine(framed_metas, out) 200 | df_list = display(out, ori_imgs,img) 201 | for clist in df_list: 202 | if len(clist) == 6: 203 | df.loc[len(df)] = clist 204 | return df 205 | 206 | 207 | 208 | input_folder = '../../paper/eval/test_data' 209 | img_path = '../../paper/eval/imgs' 210 | all_fldrs = os.listdir(img_path) 211 | # print (all_fldrs) 212 | for fldr in all_fldrs: 213 | test_imgs = os.listdir(os.path.join(img_path,fldr)) 214 | # print (fldr) 215 | df=pd.DataFrame(columns=['filename','x1','y1','x2','y2','cls']) 216 | for cur_img in test_imgs: 217 | df_cur = detect(os.path.join(img_path,fldr, cur_img)) 218 | # print (df) 219 | df = pd.concat([df,df_cur],axis=0) 220 | u_images = df['filename'].unique() 221 | all_fps = []; all_fns = []; all_tps = [] 222 | 223 | for fname in u_images: 224 | # print (fname) 225 | df_filt = df[df['filename']==fname] 226 | dets_vals = df_filt[['x1','y1','x2','y2','cls']].values.tolist() 227 | dets_gt = get_gt(input_folder,fname) 228 | tps_dets, tps_gt,fps_dets = get_tfps(dets_vals, dets_gt) 229 | if len(fps_dets)>0: 230 | for fps_det in fps_dets: 231 | all_fps.append([fname] +fps_det) 232 | fns_dets = get_fns(dets_vals, dets_gt) 233 | if len(fns_dets)>0: 234 | for fns_det in fns_dets: 235 | all_fns.append([fname] +fns_det) 236 | if len(tps_dets)>0: 237 | for tps_det in tps_dets: 238 | all_tps.append([fname] +tps_det) 239 | # print (tps_det) 240 | ## compute iou --> get tps, fps, fns, etc 241 | df_fps = pd.DataFrame(all_fps,columns = ['filename','x1','y1','x2','y2','cls']) 242 | df_fns = pd.DataFrame(all_fns,columns = ['filename','x1','y1','x2','y2','cls']) 243 | df_tps = pd.DataFrame(all_tps,columns = ['filename','x1','y1','x2','y2','cls']) 244 | # vis_acc(df_fns) 245 | # df_fps.to_csv('test.csv',index=False) 246 | coords_fns = df_fns[['x1','y1','x2','y2']].values 247 | coords_fps = df_fps[['x1','y1','x2','y2']].values 248 | coords_tps = df_tps[['x1','y1','x2','y2']].values 249 | image = 'eval/imgs_/br-cam-008.streams_1cae233e-d5b9-4f1a-9407-d6b398604c30_02757.jpg' 250 | image = os.path.join(img_path,fldr,test_imgs[0]) 251 | # print (coords) 252 | img_fns = plot_heatmap(image,coords_fns,'false negatives') 253 | img_fps = plot_heatmap(image,coords_fps, 'false positives') 254 | img_tps = plot_heatmap(image,coords_tps, 'true positives') 255 | final_img = cv2.hconcat((img_fns, img_fps, img_tps)) 256 | cv2.imshow('Final', final_img) 257 | cv2.waitKey(1) 258 | cv2.destroyAllWindows() 259 | cv2.imwrite(fldr + '_heatmap.jpg',final_img) 260 | 261 | 262 | -------------------------------------------------------------------------------- /Proposed Algorithm/models/yolo.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import yaml 4 | 5 | from models.experimental import * 6 | 7 | 8 | class Detect(nn.Module): 9 | def __init__(self, nc=80, anchors=()): # detection layer 10 | super(Detect, self).__init__() 11 | self.stride = None # strides computed during build 12 | self.nc = nc # number of classes 13 | self.no = nc + 5 # number of outputs per anchor 14 | self.nl = len(anchors) # number of detection layers 15 | self.na = len(anchors[0]) // 2 # number of anchors 16 | self.grid = [torch.zeros(1)] * self.nl # init grid 17 | a = torch.tensor(anchors).float().view(self.nl, -1, 2) 18 | self.register_buffer('anchors', a) # shape(nl,na,2) 19 | self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) 20 | self.export = False # onnx export 21 | 22 | def forward(self, x): 23 | # x = x.copy() # for profiling 24 | z = [] # inference output 25 | self.training |= self.export 26 | for i in range(self.nl): 27 | bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) 28 | x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() 29 | 30 | if not self.training: # inference 31 | if self.grid[i].shape[2:4] != x[i].shape[2:4]: 32 | self.grid[i] = self._make_grid(nx, ny).to(x[i].device) 33 | 34 | y = x[i].sigmoid() 35 | y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i].to(x[i].device)) * self.stride[i] # xy 36 | y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh 37 | z.append(y.view(bs, -1, self.no)) 38 | 39 | return x if self.training else (torch.cat(z, 1), x) 40 | 41 | @staticmethod 42 | def _make_grid(nx=20, ny=20): 43 | yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) 44 | return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() 45 | 46 | 47 | class Model(nn.Module): 48 | def __init__(self, model_cfg='yolov5s.yaml', ch=3, nc=None): # model, input channels, number of classes 49 | super(Model, self).__init__() 50 | if type(model_cfg) is dict: 51 | self.md = model_cfg # model dict 52 | else: # is *.yaml 53 | with open(model_cfg) as f: 54 | self.md = yaml.load(f, Loader=yaml.FullLoader) # model dict 55 | 56 | # Define model 57 | if nc: 58 | self.md['nc'] = nc # override yaml value 59 | self.model, self.save = parse_model(self.md, ch=[ch]) # model, savelist, ch_out 60 | # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))]) 61 | 62 | # Build strides, anchors 63 | m = self.model[-1] # Detect() 64 | m.stride = torch.tensor([64 / x.shape[-2] for x in self.forward(torch.zeros(1, ch, 64, 64))]) # forward 65 | m.anchors /= m.stride.view(-1, 1, 1) 66 | self.stride = m.stride 67 | 68 | # Init weights, biases 69 | torch_utils.initialize_weights(self) 70 | self._initialize_biases() # only run once 71 | torch_utils.model_info(self) 72 | print('') 73 | 74 | def forward(self, x, augment=False, profile=False): 75 | if augment: 76 | img_size = x.shape[-2:] # height, width 77 | s = [0.83, 0.67] # scales 78 | y = [] 79 | for i, xi in enumerate((x, 80 | torch_utils.scale_img(x.flip(3), s[0]), # flip-lr and scale 81 | torch_utils.scale_img(x, s[1]), # scale 82 | )): 83 | # cv2.imwrite('img%g.jpg' % i, 255 * xi[0].numpy().transpose((1, 2, 0))[:, :, ::-1]) 84 | y.append(self.forward_once(xi)[0]) 85 | 86 | y[1][..., :4] /= s[0] # scale 87 | y[1][..., 0] = img_size[1] - y[1][..., 0] # flip lr 88 | y[2][..., :4] /= s[1] # scale 89 | return torch.cat(y, 1), None # augmented inference, train 90 | else: 91 | return self.forward_once(x, profile) # single-scale inference, train 92 | 93 | def forward_once(self, x, profile=False): 94 | y, dt = [], [] # outputs 95 | for m in self.model: 96 | if m.f != -1: # if not from previous layer 97 | x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers 98 | 99 | if profile: 100 | import thop 101 | o = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # FLOPS 102 | t = torch_utils.time_synchronized() 103 | for _ in range(10): 104 | _ = m(x) 105 | dt.append((torch_utils.time_synchronized() - t) * 100) 106 | print('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt[-1], m.type)) 107 | 108 | x = m(x) # run 109 | y.append(x if m.i in self.save else None) # save output 110 | 111 | if profile: 112 | print('%.1fms total' % sum(dt)) 113 | return x 114 | 115 | def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency 116 | # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. 117 | m = self.model[-1] # Detect() module 118 | for f, s in zip(m.f, m.stride): #  from 119 | mi = self.model[f % m.i] 120 | b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) 121 | b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) 122 | b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls 123 | mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) 124 | 125 | def _print_biases(self): 126 | m = self.model[-1] # Detect() module 127 | for f in sorted([x % m.i for x in m.f]): #  from 128 | b = self.model[f].bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85) 129 | print(('%g Conv2d.bias:' + '%10.3g' * 6) % (f, *b[:5].mean(1).tolist(), b[5:].mean())) 130 | 131 | # def _print_weights(self): 132 | # for m in self.model.modules(): 133 | # if type(m) is Bottleneck: 134 | # print('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights 135 | 136 | def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers 137 | print('Fusing layers...') 138 | for m in self.model.modules(): 139 | if type(m) is Conv: 140 | m.conv = torch_utils.fuse_conv_and_bn(m.conv, m.bn) # update conv 141 | m.bn = None # remove batchnorm 142 | m.forward = m.fuseforward # update forward 143 | torch_utils.model_info(self) 144 | 145 | 146 | def parse_model(md, ch): # model_dict, input_channels(3) 147 | print('\n%3s%15s%3s%10s %-40s%-30s' % ('', 'from', 'n', 'params', 'module', 'arguments')) 148 | anchors, nc, gd, gw = md['anchors'], md['nc'], md['depth_multiple'], md['width_multiple'] 149 | na = (len(anchors[0]) // 2) # number of anchors 150 | no = na * (nc + 5) # number of outputs = anchors * (classes + 5) 151 | 152 | layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out 153 | for i, (f, n, m, args) in enumerate(md['backbone'] + md['head']): # from, number, module, args 154 | m = eval(m) if isinstance(m, str) else m # eval strings 155 | for j, a in enumerate(args): 156 | try: 157 | args[j] = eval(a) if isinstance(a, str) else a # eval strings 158 | except: 159 | pass 160 | 161 | n = max(round(n * gd), 1) if n > 1 else n # depth gain 162 | if m in [nn.Conv2d, Conv, Bottleneck, SPP, DWConv, MixConv2d, Focus, ConvPlus, BottleneckCSP]: 163 | c1, c2 = ch[f], args[0] 164 | 165 | # Normal 166 | # if i > 0 and args[0] != no: # channel expansion factor 167 | # ex = 1.75 # exponential (default 2.0) 168 | # e = math.log(c2 / ch[1]) / math.log(2) 169 | # c2 = int(ch[1] * ex ** e) 170 | # if m != Focus: 171 | c2 = make_divisible(c2 * gw, 8) if c2 != no else c2 172 | 173 | # Experimental 174 | # if i > 0 and args[0] != no: # channel expansion factor 175 | # ex = 1 + gw # exponential (default 2.0) 176 | # ch1 = 32 # ch[1] 177 | # e = math.log(c2 / ch1) / math.log(2) # level 1-n 178 | # c2 = int(ch1 * ex ** e) 179 | # if m != Focus: 180 | # c2 = make_divisible(c2, 8) if c2 != no else c2 181 | 182 | args = [c1, c2, *args[1:]] 183 | if m is BottleneckCSP: 184 | args.insert(2, n) 185 | n = 1 186 | elif m is nn.BatchNorm2d: 187 | args = [ch[f]] 188 | elif m is Concat: 189 | c2 = sum([ch[-1 if x == -1 else x + 1] for x in f]) 190 | elif m is Detect: 191 | f = f or list(reversed([(-1 if j == i else j - 1) for j, x in enumerate(ch) if x == no])) 192 | else: 193 | c2 = ch[f] 194 | 195 | m_ = nn.Sequential(*[m(*args) for _ in range(n)]) if n > 1 else m(*args) # module 196 | t = str(m)[8:-2].replace('__main__.', '') # module type 197 | np = sum([x.numel() for x in m_.parameters()]) # number params 198 | m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params 199 | print('%3s%15s%3s%10.0f %-40s%-30s' % (i, f, n, np, t, args)) # print 200 | save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist 201 | layers.append(m_) 202 | ch.append(c2) 203 | return nn.Sequential(*layers), sorted(save) 204 | 205 | 206 | if __name__ == '__main__': 207 | parser = argparse.ArgumentParser() 208 | parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml') 209 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 210 | opt = parser.parse_args() 211 | opt.cfg = glob.glob('./**/' + opt.cfg, recursive=True)[0] # find file 212 | device = torch_utils.select_device(opt.device) 213 | 214 | # Create model 215 | model = Model(opt.cfg).to(device) 216 | model.train() 217 | 218 | # Profile 219 | # img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 640, 640).to(device) 220 | # y = model(img, profile=True) 221 | # print([y[0].shape] + [x.shape for x in y[1]]) 222 | 223 | # ONNX export 224 | # model.model[-1].export = True 225 | # torch.onnx.export(model, img, f.replace('.yaml', '.onnx'), verbose=True, opset_version=11) 226 | 227 | # Tensorboard 228 | # from torch.utils.tensorboard import SummaryWriter 229 | # tb_writer = SummaryWriter() 230 | # print("Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/") 231 | # tb_writer.add_graph(model.model, img) # add model to tensorboard 232 | # tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard 233 | -------------------------------------------------------------------------------- /Proposed Algorithm/automl.py: -------------------------------------------------------------------------------- 1 | import argparse, uuid, toml 2 | 3 | from utils.datasets import * 4 | from utils.utils import * 5 | import pandas as pd 6 | 7 | 8 | def save_box_info(x, img, config, toml_name, img_name, cur_frame, df, color=None, label=None, line_thickness=None): 9 | 10 | toml_path_images = 'automl/images' 11 | toml_path_annt = 'automl/annotations' 12 | 13 | # Plots one bounding box on image img 14 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 15 | color = color or [random.randint(0, 255) for _ in range(3)] 16 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 17 | df.loc[len(df)] = [c1[0],c1[1],c2[0],c2[1],label,cur_frame] 18 | if not (os.path.isfile('dets.csv')): 19 | df.to_csv('dets.csv',index=False) 20 | else: 21 | df.to_csv('dets.csv',mode = 'a+', index=False, header=False) 22 | # df = pd.DataFrame(columns = ['tlx','tly','brx','bry','cls','frame']) 23 | # print (df.shape) 24 | cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 25 | return df 26 | 27 | 28 | 29 | def plot_one_box_(x, img, config, toml_name, img_name, color=None, label=None, line_thickness=None): 30 | 31 | toml_path_images = 'automl/images' 32 | toml_path_annt = 'automl/annotations' 33 | 34 | # Plots one bounding box on image img 35 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 36 | color = color or [random.randint(0, 255) for _ in range(3)] 37 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 38 | # imc = img.copy() 39 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 40 | 41 | if label: 42 | config['objects'] = [{'xmin': c1[0], 'ymin': c1[1],'ymax': c2[1], 'xmax': c2[0],'class': label}] 43 | tf = max(tl - 1, 1) # font thickness 44 | t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 45 | c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 46 | # print (label, c1, c2) 47 | 48 | out_path = os.path.join(toml_path_annt, toml_name) 49 | cv2.imwrite(os.path.join(toml_path_images,img_name),img) 50 | p = open(out_path, 'a+') 51 | p.write(toml.dumps(config)) 52 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 53 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 54 | 55 | 56 | def detect(save_img=False): 57 | out, source, weights, half, view_img, save_txt, imgsz = \ 58 | opt.output, opt.source, opt.weights, opt.half, opt.view_img, opt.save_txt, opt.img_size 59 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 60 | 61 | # Initialize 62 | device = torch_utils.select_device(opt.device) 63 | if os.path.exists(out): 64 | shutil.rmtree(out) # delete output folder 65 | os.makedirs(out) # make new output folder 66 | 67 | # Load model 68 | google_utils.attempt_download(weights) 69 | model = torch.load(weights, map_location=device)['model'] 70 | # torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning 71 | # model.fuse() 72 | model.to(device).eval() 73 | 74 | # Second-stage classifier 75 | classify = False 76 | if classify: 77 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 78 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 79 | modelc.to(device).eval() 80 | 81 | # Half precision 82 | half = half and device.type != 'cpu' # half precision only supported on CUDA 83 | if half: 84 | model.half() 85 | 86 | # Set Dataloader 87 | vid_path, vid_writer = None, None 88 | if webcam: 89 | view_img = True 90 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 91 | dataset = LoadStreams(source, img_size=imgsz) 92 | else: 93 | save_img = True 94 | dataset = LoadImages(source, img_size=imgsz) 95 | 96 | # Get names and colors 97 | names = model.names if hasattr(model, 'names') else model.modules.names 98 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 99 | 100 | # Run inference 101 | t0 = time.time() 102 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 103 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 104 | cnt=0 105 | df = pd.DataFrame(columns = ['tlx','tly','brx','bry','cls','frame']) 106 | for path, img, im0s, vid_cap in dataset: 107 | cur_frame = int(vid_cap.get(cv2.CAP_PROP_POS_FRAMES)) 108 | cnt = cur_frame 109 | img = torch.from_numpy(img).to(device) 110 | img = img.half() if half else img.float() # uint8 to fp16/32 111 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 112 | if img.ndimension() == 3: 113 | img = img.unsqueeze(0) 114 | 115 | # Inference 116 | t1 = torch_utils.time_synchronized() 117 | pred = model(img, augment=opt.augment)[0] 118 | t2 = torch_utils.time_synchronized() 119 | 120 | # to float 121 | if half: 122 | pred = pred.float() 123 | 124 | # Apply NMS 125 | pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, 126 | fast=True, classes=opt.classes, agnostic=opt.agnostic_nms) 127 | 128 | # Apply Classifier 129 | if classify: 130 | pred = apply_classifier(pred, modelc, img, im0s) 131 | 132 | # Process detections 133 | config = dict() 134 | # print (im0s) 135 | # config['width'] = im0s[0].shape[1] 136 | # config['height'] = im0s[0].shape[0] 137 | config['width'] = im0s.shape[1] 138 | config['height'] = im0s.shape[0] 139 | 140 | 141 | gen_name = 'br-cam-008.streams_' + str(uuid.uuid4()) +'_' +str(cnt).zfill(5) 142 | img_name = gen_name + '.jpg' 143 | toml_name = gen_name + '.toml' 144 | # print (gen_name,img_name,toml_name) 145 | for i, det in enumerate(pred): # detections per image 146 | if webcam: # batch_size >= 1 147 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 148 | else: 149 | p, s, im0 = path, '', im0s 150 | 151 | save_path = str(Path(out) / Path(p).name) 152 | s += '%gx%g ' % img.shape[2:] # print string 153 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 154 | if det is not None and len(det): 155 | # Rescale boxes from img_size to im0 size 156 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 157 | 158 | # Print results 159 | all_cls = [] 160 | for c in det[:, -1].unique(): 161 | n = (det[:, -1] == c).sum() # detections per class 162 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 163 | all_cls.append(names[int(c)]) 164 | # print (all_cls) 165 | # Write results 166 | # print (det[1]) 167 | for *xyxy, conf, cls in det: 168 | # print (cls) 169 | if save_txt: # Write to file 170 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 171 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 172 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 173 | 174 | if save_img or view_img: # Add bbox to image 175 | # label = '%s %.2f' % (names[int(cls)], conf) 176 | label = names[int(cls)] 177 | df = save_box_info(xyxy, im0, config, toml_name, img_name,cur_frame, 178 | df, label=label, 179 | color=colors[int(cls)], 180 | line_thickness=3) 181 | df = pd.DataFrame(columns = ['tlx','tly','brx','bry','cls','frame']) 182 | # if 'motorcycle' in all_cls: 183 | plot_one_box_(xyxy, im0, config, toml_name, img_name, label=label, color=colors[int(cls)], 184 | line_thickness=3) 185 | 186 | # Print time (inference + NMS) 187 | # print('%sDone. (%.3fs)' % (s, t2 - t1)) 188 | 189 | # Stream results 190 | if view_img: 191 | cv2.imshow(p, im0) 192 | if cv2.waitKey(1) == ord('q'): # q to quit 193 | raise StopIteration 194 | 195 | # Save results (image with detections) 196 | if save_img: 197 | if dataset.mode == 'images': 198 | cv2.imwrite(save_path, im0) 199 | else: 200 | if vid_path != save_path: # new video 201 | vid_path = save_path 202 | if isinstance(vid_writer, cv2.VideoWriter): 203 | vid_writer.release() # release previous video writer 204 | 205 | fps = vid_cap.get(cv2.CAP_PROP_FPS) 206 | w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 207 | h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 208 | vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h)) 209 | vid_writer.write(im0) 210 | 211 | if save_txt or save_img: 212 | # print('Results saved to %s' % os.getcwd() + os.sep + out) 213 | if platform == 'darwin': # MacOS 214 | os.system('open ' + save_path) 215 | 216 | # print('Done. (%.3fs)' % (time.time() - t0)) 217 | 218 | 219 | if __name__ == '__main__': 220 | parser = argparse.ArgumentParser() 221 | parser.add_argument('--weights', type=str, default='weights/yolov5s.pt', help='model.pt path') 222 | parser.add_argument('--source', type=str, default='inference/images', help='source') # file/folder, 0 for webcam 223 | parser.add_argument('--output', type=str, default='inference/output', help='output folder') # output folder 224 | parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') 225 | parser.add_argument('--conf-thres', type=float, default=0.4, help='object confidence threshold') 226 | parser.add_argument('--iou-thres', type=float, default=0.5, help='IOU threshold for NMS') 227 | parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)') 228 | parser.add_argument('--half', action='store_true', help='half precision FP16 inference') 229 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 230 | parser.add_argument('--view-img', action='store_true', help='display results') 231 | parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') 232 | parser.add_argument('--classes', nargs='+', type=int, help='filter by class') 233 | parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') 234 | parser.add_argument('--augment', action='store_true', help='augmented inference') 235 | opt = parser.parse_args() 236 | # print(opt) 237 | 238 | with torch.no_grad(): 239 | detect() 240 | -------------------------------------------------------------------------------- /Proposed Algorithm/di_automl.py: -------------------------------------------------------------------------------- 1 | import argparse, uuid, toml 2 | 3 | from utils.datasets import * 4 | from utils.utils import * 5 | import pandas as pd 6 | 7 | 8 | def save_box_info(x, img, config, toml_name, img_name, cur_frame, df, color=None, label=None, line_thickness=None): 9 | 10 | toml_path_images = 'automl/images' 11 | toml_path_annt = 'automl/annotations' 12 | 13 | # Plots one bounding box on image img 14 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 15 | color = color or [random.randint(0, 255) for _ in range(3)] 16 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 17 | df.loc[len(df)] = [c1[0],c1[1],c2[0],c2[1],label,cur_frame] 18 | if not (os.path.isfile('dets.csv')): 19 | df.to_csv('dets.csv',index=False) 20 | else: 21 | df.to_csv('dets.csv',mode = 'a+', index=False, header=False) 22 | # df = pd.DataFrame(columns = ['tlx','tly','brx','bry','cls','frame']) 23 | # print (df.shape) 24 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 25 | return df 26 | 27 | 28 | 29 | def plot_one_box_(x, img, config, toml_name, img_name, color=None, label=None, line_thickness=None): 30 | 31 | toml_path_images = 'automl/images' 32 | toml_path_annt = 'automl/annotations' 33 | cls_names = ['person','car','traffic_light','stop_sign'] 34 | cls_names_ori = ['person','car','traffic light','stop sign'] 35 | 36 | # Plots one bounding box on image img 37 | tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness 38 | color = color or [random.randint(0, 255) for _ in range(3)] 39 | c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) 40 | # imc = img.copy() 41 | # cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) 42 | if label == 'truck': 43 | label = 'car' 44 | 45 | if label and (label in cls_names_ori): 46 | lidx = cls_names_ori.index(label) 47 | config['objects'] = [{'xmin': c1[0], 'ymin': c1[1],'ymax': c2[1], 'xmax': c2[0],'class': cls_names[lidx]}] 48 | tf = max(tl - 1, 1) # font thickness 49 | t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] 50 | c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 51 | # print (label, c1, c2) 52 | 53 | out_path = os.path.join(toml_path_annt, toml_name) 54 | cv2.imwrite(os.path.join(toml_path_images,img_name),img) 55 | p = open(out_path, 'a+') 56 | p.write(toml.dumps(config)) 57 | # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled 58 | # cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) 59 | 60 | 61 | def detect(save_img=False): 62 | out, source, weights, half, view_img, save_txt, imgsz = \ 63 | opt.output, opt.source, opt.weights, opt.half, opt.view_img, opt.save_txt, opt.img_size 64 | webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') 65 | 66 | # Initialize 67 | device = torch_utils.select_device(opt.device) 68 | if os.path.exists(out): 69 | shutil.rmtree(out) # delete output folder 70 | os.makedirs(out) # make new output folder 71 | 72 | # Load model 73 | google_utils.attempt_download(weights) 74 | model = torch.load(weights, map_location=device)['model'] 75 | # torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning 76 | # model.fuse() 77 | model.to(device).eval() 78 | 79 | # Second-stage classifier 80 | classify = False 81 | if classify: 82 | modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize 83 | modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights 84 | modelc.to(device).eval() 85 | 86 | # Half precision 87 | half = half and device.type != 'cpu' # half precision only supported on CUDA 88 | if half: 89 | model.half() 90 | 91 | # Set Dataloader 92 | vid_path, vid_writer = None, None 93 | if webcam: 94 | view_img = True 95 | torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference 96 | dataset = LoadStreams(source, img_size=imgsz) 97 | else: 98 | save_img = True 99 | dataset = LoadImages(source, img_size=imgsz) 100 | 101 | # Get names and colors 102 | names = model.names if hasattr(model, 'names') else model.modules.names 103 | colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] 104 | 105 | # Run inference 106 | t0 = time.time() 107 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 108 | _ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once 109 | cnt=0 110 | df = pd.DataFrame(columns = ['tlx','tly','brx','bry','cls','frame']) 111 | for path, img, im0s, vid_cap in dataset: 112 | cur_frame = int(vid_cap.get(cv2.CAP_PROP_POS_FRAMES)) 113 | cnt = cur_frame 114 | img = torch.from_numpy(img).to(device) 115 | img = img.half() if half else img.float() # uint8 to fp16/32 116 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 117 | if img.ndimension() == 3: 118 | img = img.unsqueeze(0) 119 | 120 | # Inference 121 | t1 = torch_utils.time_synchronized() 122 | pred = model(img, augment=opt.augment)[0] 123 | t2 = torch_utils.time_synchronized() 124 | 125 | # to float 126 | if half: 127 | pred = pred.float() 128 | 129 | # Apply NMS 130 | pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, 131 | fast=True, classes=opt.classes, agnostic=opt.agnostic_nms) 132 | 133 | # Apply Classifier 134 | if classify: 135 | pred = apply_classifier(pred, modelc, img, im0s) 136 | 137 | # Process detections 138 | config = dict() 139 | # print (im0s) 140 | # config['width'] = im0s[0].shape[1] 141 | # config['height'] = im0s[0].shape[0] 142 | config['width'] = im0s.shape[1] 143 | config['height'] = im0s.shape[0] 144 | 145 | 146 | gen_name = 'br-cam-008.streams_' + str(uuid.uuid4()) +'_' +str(cnt).zfill(5) 147 | img_name = gen_name + '.jpg' 148 | toml_name = gen_name + '.toml' 149 | # print (gen_name,img_name,toml_name) 150 | for i, det in enumerate(pred): # detections per image 151 | if webcam: # batch_size >= 1 152 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 153 | else: 154 | p, s, im0 = path, '', im0s 155 | 156 | save_path = str(Path(out) / Path(p).name) 157 | s += '%gx%g ' % img.shape[2:] # print string 158 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] #  normalization gain whwh 159 | if det is not None and len(det): 160 | # Rescale boxes from img_size to im0 size 161 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 162 | 163 | # Print results 164 | all_cls = [] 165 | for c in det[:, -1].unique(): 166 | n = (det[:, -1] == c).sum() # detections per class 167 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 168 | all_cls.append(names[int(c)]) 169 | # print (all_cls) 170 | # Write results 171 | # print (det[1]) 172 | for *xyxy, conf, cls in det: 173 | # print (cls) 174 | if save_txt: # Write to file 175 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 176 | with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: 177 | file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 178 | 179 | if save_img or view_img: # Add bbox to image 180 | # label = '%s %.2f' % (names[int(cls)], conf) 181 | label = names[int(cls)] 182 | df = save_box_info(xyxy, im0, config, toml_name, img_name,cur_frame, 183 | df, label=label, 184 | color=colors[int(cls)], 185 | line_thickness=3) 186 | df = pd.DataFrame(columns = ['tlx','tly','brx','bry','cls','frame']) 187 | # print (all_cls) 188 | # if 'traffic light' in all_cls: 189 | if ('stop sign' in all_cls) or ('traffic light' in all_cls): 190 | plot_one_box_(xyxy, im0, config, toml_name, img_name, label=label, color=colors[int(cls)], 191 | line_thickness=3) 192 | 193 | # Print time (inference + NMS) 194 | # print('%sDone. (%.3fs)' % (s, t2 - t1)) 195 | 196 | # Stream results 197 | if view_img: 198 | cv2.imshow(p, im0) 199 | if cv2.waitKey(1) == ord('q'): # q to quit 200 | raise StopIteration 201 | 202 | # Save results (image with detections) 203 | if save_img: 204 | if dataset.mode == 'images': 205 | cv2.imwrite(save_path, im0) 206 | else: 207 | if vid_path != save_path: # new video 208 | vid_path = save_path 209 | if isinstance(vid_writer, cv2.VideoWriter): 210 | vid_writer.release() # release previous video writer 211 | 212 | fps = vid_cap.get(cv2.CAP_PROP_FPS) 213 | w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 214 | h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 215 | vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h)) 216 | vid_writer.write(im0) 217 | 218 | if save_txt or save_img: 219 | # print('Results saved to %s' % os.getcwd() + os.sep + out) 220 | if platform == 'darwin': # MacOS 221 | os.system('open ' + save_path) 222 | 223 | # print('Done. (%.3fs)' % (time.time() - t0)) 224 | 225 | 226 | if __name__ == '__main__': 227 | parser = argparse.ArgumentParser() 228 | parser.add_argument('--weights', type=str, default='weights/yolov5s.pt', help='model.pt path') 229 | parser.add_argument('--source', type=str, default='inference/images', help='source') # file/folder, 0 for webcam 230 | parser.add_argument('--output', type=str, default='inference/output', help='output folder') # output folder 231 | parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') 232 | parser.add_argument('--conf-thres', type=float, default=0.4, help='object confidence threshold') 233 | parser.add_argument('--iou-thres', type=float, default=0.5, help='IOU threshold for NMS') 234 | parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)') 235 | parser.add_argument('--half', action='store_true', help='half precision FP16 inference') 236 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 237 | parser.add_argument('--view-img', action='store_true', help='display results') 238 | parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') 239 | parser.add_argument('--classes', nargs='+', type=int, help='filter by class') 240 | parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') 241 | parser.add_argument('--augment', action='store_true', help='augmented inference') 242 | opt = parser.parse_args() 243 | # print(opt) 244 | 245 | with torch.no_grad(): 246 | detect() 247 | --------------------------------------------------------------------------------