From db24afbf83fb17b10ac056b012c8d2c36bffd1cb Mon Sep 17 00:00:00 2001 From: Tossy0423 Date: Sun, 19 Sep 2021 17:16:31 +0900 Subject: [PATCH] Merged from Alexey/darknet --- README.md | 8 ++++- darknet.py | 66 +++++++++++++++++++++++++++++++++++++++++ darknet_video.py | 5 ++-- scripts/deploy-cuda.ps1 | 16 +++++----- scripts/deploy-cuda.sh | 19 ++++++------ scripts/setup.ps1 | 6 ++-- scripts/setup.sh | 12 ++++---- 7 files changed, 102 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index bb545cf097a..65b518726a3 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Paper YOLO v4: https://arxiv.org/abs/2004.10934 -Paper Scaled YOLO v4: https://arxiv.org/abs/2011.08036 use to reproduce results: [ScaledYOLOv4](https://github.com/WongKinYiu/ScaledYOLOv4) +Paper Scaled YOLO v4: * **[CVPR 2021](https://openaccess.thecvf.com/content/CVPR2021/html/Wang_Scaled-YOLOv4_Scaling_Cross_Stage_Partial_Network_CVPR_2021_paper.html)**: use to reproduce results: [ScaledYOLOv4](https://github.com/WongKinYiu/ScaledYOLOv4) More details in articles on medium: @@ -152,6 +152,12 @@ FPS on RTX 2070 (R) and Tesla V100 (V): - [yolov4-p5.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-p5.cfg) - 896x896 - **70.0% mAP@0.5 (51.6% AP@0.5:0.95) - 43(V) FPS** - xxx BFlops (xxx FMA) - 271 MB: [yolov4-p5.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-p5.weights) - pre-trained weights for training: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-p5.conv.232 +- [yolov4-csp-x-swish.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-csp-x-swish.cfg) - 640x640 - **69.9% mAP@0.5 (51.5% AP@0.5:0.95) - 23(R) FPS / 50(V) FPS** - 221 BFlops (110 FMA) - 381 MB: [yolov4-csp-x-swish.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp-x-swish.weights) + - pre-trained weights for training: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp-x-swish.conv.192 + +- [yolov4-csp-swish.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-csp-swish.cfg) - 640x640 - **68.7% mAP@0.5 (50.0% AP@0.5:0.95) - 70(V) FPS** - 120 (60 FMA) - 202 MB: [yolov4-csp-swish.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp-swish.weights) + - pre-trained weights for training: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp-swish.conv.164 + - [yolov4x-mish.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4x-mish.cfg) - 640x640 - **68.5% mAP@0.5 (50.1% AP@0.5:0.95) - 23(R) FPS / 50(V) FPS** - 221 BFlops (110 FMA) - 381 MB: [yolov4x-mish.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4x-mish.weights) - pre-trained weights for training: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4x-mish.conv.166 diff --git a/darknet.py b/darknet.py index 698f0469ad5..ebb0eede210 100644 --- a/darknet.py +++ b/darknet.py @@ -24,6 +24,7 @@ class BOX(Structure): class DETECTION(Structure): _fields_ = [("bbox", BOX), ("classes", c_int), + ("best_class_idx", c_int), ("prob", POINTER(c_float)), ("mask", POINTER(c_float)), ("objectness", c_float), @@ -133,6 +134,56 @@ def decode_detection(detections): decoded.append((str(label), confidence, bbox)) return decoded +# https://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/ +# Malisiewicz et al. +def non_max_suppression_fast(detections, overlap_thresh): + boxes = [] + for detection in detections: + _, _, _, (x, y, w, h) = detection + x1 = x - w / 2 + y1 = y - h / 2 + x2 = x + w / 2 + y2 = y + h / 2 + boxes.append(np.array([x1, y1, x2, y2])) + boxes_array = np.array(boxes) + + # initialize the list of picked indexes + pick = [] + # grab the coordinates of the bounding boxes + x1 = boxes_array[:, 0] + y1 = boxes_array[:, 1] + x2 = boxes_array[:, 2] + y2 = boxes_array[:, 3] + # compute the area of the bounding boxes and sort the bounding + # boxes by the bottom-right y-coordinate of the bounding box + area = (x2 - x1 + 1) * (y2 - y1 + 1) + idxs = np.argsort(y2) + # keep looping while some indexes still remain in the indexes + # list + while len(idxs) > 0: + # grab the last index in the indexes list and add the + # index value to the list of picked indexes + last = len(idxs) - 1 + i = idxs[last] + pick.append(i) + # find the largest (x, y) coordinates for the start of + # the bounding box and the smallest (x, y) coordinates + # for the end of the bounding box + xx1 = np.maximum(x1[i], x1[idxs[:last]]) + yy1 = np.maximum(y1[i], y1[idxs[:last]]) + xx2 = np.minimum(x2[i], x2[idxs[:last]]) + yy2 = np.minimum(y2[i], y2[idxs[:last]]) + # compute the width and height of the bounding box + w = np.maximum(0, xx2 - xx1 + 1) + h = np.maximum(0, yy2 - yy1 + 1) + # compute the ratio of overlap + overlap = (w * h) / area[idxs[:last]] + # delete all indexes from the index list that have + idxs = np.delete(idxs, np.concatenate(([last], + np.where(overlap > overlap_thresh)[0]))) + # return only the bounding boxes that were picked using the + # integer data type + return [detections[i] for i in pick] def remove_negatives(detections, class_names, num): """ @@ -148,6 +199,21 @@ def remove_negatives(detections, class_names, num): return predictions +def remove_negatives_faster(detections, class_names, num): + """ + Faster version of remove_negatives (very useful when using yolo9000) + """ + predictions = [] + for j in range(num): + if detections[j].best_class_idx == -1: + continue + name = class_names[detections[j].best_class_idx] + bbox = detections[j].bbox + bbox = (bbox.x, bbox.y, bbox.w, bbox.h) + predictions.append((name, detections[j].prob[detections[j].best_class_idx], bbox)) + return predictions + + def detect_image(network, class_names, image, thresh=.5, hier_thresh=.5, nms=.45): """ Returns a list with highest confidence class and their bbox diff --git a/darknet_video.py b/darknet_video.py index 04895133f97..5e7036203a7 100644 --- a/darknet_video.py +++ b/darknet_video.py @@ -136,7 +136,7 @@ def inference(darknet_image_queue, detections_queue, fps_queue): def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors - video = set_saved_video(cap, args.out_filename, (darknet_width, darknet_height)) + video = set_saved_video(cap, args.out_filename, (video_width, video_height)) while cap.isOpened(): frame = frame_queue.get() detections = detections_queue.get() @@ -149,7 +149,6 @@ def drawing(frame_queue, detections_queue, fps_queue): image = darknet.draw_boxes(detections_adjusted, frame, class_colors) if not args.dont_show: cv2.imshow('Inference', image) - image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.out_filename is not None: video.write(image) if cv2.waitKey(fps) == 27: @@ -177,6 +176,8 @@ def drawing(frame_queue, detections_queue, fps_queue): darknet_height = darknet.network_height(network) input_path = str2int(args.input) cap = cv2.VideoCapture(input_path) + video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) Thread(target=video_capture, args=(frame_queue, darknet_image_queue)).start() Thread(target=inference, args=(darknet_image_queue, detections_queue, fps_queue)).start() Thread(target=drawing, args=(frame_queue, detections_queue, fps_queue)).start() diff --git a/scripts/deploy-cuda.ps1 b/scripts/deploy-cuda.ps1 index 83b64d29559..78ce6e507f4 100644 --- a/scripts/deploy-cuda.ps1 +++ b/scripts/deploy-cuda.ps1 @@ -1,18 +1,18 @@ #!/usr/bin/env pwsh -$url = 'https://developer.download.nvidia.com/compute/cuda/11.3.0/network_installers/cuda_11.3.0_win10_network.exe' +$url = 'https://developer.download.nvidia.com/compute/cuda/11.4.0/network_installers/cuda_11.4.0_win10_network.exe' -$CudaFeatures = 'nvcc_11.3 cuobjdump_11.3 nvprune_11.3 cupti_11.3 memcheck_11.3 nvdisasm_11.3 nvprof_11.3 ' + ` - 'visual_studio_integration_11.3 visual_profiler_11.3 visual_profiler_11.3 cublas_11.3 cublas_dev_11.3 ' + ` - 'cudart_11.3 cufft_11.3 cufft_dev_11.3 curand_11.3 curand_dev_11.3 cusolver_11.3 cusolver_dev_11.3 ' + ` - 'cusparse_11.3 cusparse_dev_11.3 npp_11.3 npp_dev_11.3 nvrtc_11.3 nvrtc_dev_11.3 nvml_dev_11.3 ' + ` - 'occupancy_calculator_11.3 ' +$CudaFeatures = 'nvcc_11.4 cuobjdump_11.4 nvprune_11.4 cupti_11.4 memcheck_11.4 nvdisasm_11.4 nvprof_11.4 ' + ` + 'visual_studio_integration_11.4 visual_profiler_11.4 visual_profiler_11.4 cublas_11.4 cublas_dev_11.4 ' + ` + 'cudart_11.4 cufft_11.4 cufft_dev_11.4 curand_11.4 curand_dev_11.4 cusolver_11.4 cusolver_dev_11.4 ' + ` + 'cusparse_11.4 cusparse_dev_11.4 npp_11.4 npp_dev_11.4 nvrtc_11.4 nvrtc_dev_11.4 nvml_dev_11.4 ' + ` + 'occupancy_calculator_11.4 ' try { Write-Host 'Downloading CUDA...' - Invoke-WebRequest -Uri $url -OutFile "cuda_11.3.0_win10_network.exe" + Invoke-WebRequest -Uri $url -OutFile "cuda_11.4.0_win10_network.exe" Write-Host 'Installing CUDA...' - $proc = Start-Process -PassThru -FilePath "./cuda_11.3.0_win10_network.exe" -ArgumentList @('-s ' + $CudaFeatures) + $proc = Start-Process -PassThru -FilePath "./cuda_11.4.0_win10_network.exe" -ArgumentList @('-s ' + $CudaFeatures) $proc.WaitForExit() $exitCode = $proc.ExitCode if ($exitCode -eq 0) { diff --git a/scripts/deploy-cuda.sh b/scripts/deploy-cuda.sh index 65f173aabaf..92da66c0b16 100755 --- a/scripts/deploy-cuda.sh +++ b/scripts/deploy-cuda.sh @@ -7,17 +7,16 @@ else sudo apt-get update sudo apt-get install build-essential g++ sudo apt-get install apt-transport-https ca-certificates gnupg software-properties-common wget - wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.2.89-1_amd64.deb - sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub - sudo dpkg -i cuda-repo-ubuntu1804_10.2.89-1_amd64.deb - wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb - sudo dpkg -i nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb + sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin + sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub + sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" + sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/ /" sudo apt-get update sudo apt-get dist-upgrade -y - sudo apt-get install -y --no-install-recommends cuda-compiler-10-2 cuda-libraries-dev-10-2 cuda-driver-dev-10-2 cuda-cudart-dev-10-2 cuda-curand-dev-10-2 - sudo apt-get install -y --no-install-recommends libcudnn7-dev + sudo apt-get install -y --no-install-recommends cuda-compiler-11-4 cuda-libraries-dev-11-4 cuda-driver-dev-11-4 cuda-cudart-dev-11-4 + sudo apt-get install -y --no-install-recommends libcudnn8-dev sudo rm -rf /usr/local/cuda - sudo ln -s /usr/local/cuda-10.2 /usr/local/cuda + sudo ln -s /usr/local/cuda-11.4 /usr/local/cuda elif [[ $(cut -f2 <<< $(lsb_release -r)) == "20.04" ]]; then sudo apt-get update sudo apt-get install build-essential g++ @@ -28,10 +27,10 @@ else sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" sudo apt-get update sudo apt-get dist-upgrade -y - sudo apt-get install -y --no-install-recommends cuda-compiler-11-2 cuda-libraries-dev-11-2 cuda-driver-dev-11-2 cuda-cudart-dev-11-2 + sudo apt-get install -y --no-install-recommends cuda-compiler-11-4 cuda-libraries-dev-11-4 cuda-driver-dev-11-4 cuda-cudart-dev-11-4 sudo apt-get install -y --no-install-recommends libcudnn8-dev sudo rm -rf /usr/local/cuda - sudo ln -s /usr/local/cuda-11.2 /usr/local/cuda + sudo ln -s /usr/local/cuda-11.4 /usr/local/cuda else echo "Unable to deploy CUDA on this Linux version, please wait for a future script update" fi diff --git a/scripts/setup.ps1 b/scripts/setup.ps1 index ca54dba9754..1ebd9f02eed 100755 --- a/scripts/setup.ps1 +++ b/scripts/setup.ps1 @@ -16,9 +16,9 @@ Push-Location $PSScriptRoot if ($InstallCUDA) { & $PSScriptRoot/deploy-cuda.ps1 - $env:CUDA_PATH="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.3" - $env:CUDA_TOOLKIT_ROOT_DIR="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.3" - $env:CUDACXX="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.3\\bin\\nvcc.exe" + $env:CUDA_PATH="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.4" + $env:CUDA_TOOLKIT_ROOT_DIR="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.4" + $env:CUDACXX="C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.4\\bin\\nvcc.exe" $CUDAisAvailable = $true } else { diff --git a/scripts/setup.sh b/scripts/setup.sh index 51d641bcea4..67aa744871b 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -51,9 +51,9 @@ if [ "$install_tools" = true ] ; then sudo apt-get install -y cmake sudo apt-get install -y powershell if [ "$bypass_driver_installation" = true ] ; then - sudo ln -s /usr/local/cuda-10.2/lib64/stubs/libcuda.so /usr/local/cuda-11.2/lib64/stubs/libcuda.so.1 - sudo ln -s /usr/local/cuda-10.2/lib64/stubs/libcuda.so /usr/local/cuda-11.2/lib64/libcuda.so.1 - sudo ln -s /usr/local/cuda-10.2/lib64/stubs/libcuda.so /usr/local/cuda-11.2/lib64/libcuda.so + sudo ln -s /usr/local/cuda-11.4/lib64/stubs/libcuda.so /usr/local/cuda-11.4/lib64/stubs/libcuda.so.1 + sudo ln -s /usr/local/cuda-11.4/lib64/stubs/libcuda.so /usr/local/cuda-11.4/lib64/libcuda.so.1 + sudo ln -s /usr/local/cuda-11.4/lib64/stubs/libcuda.so /usr/local/cuda-11.4/lib64/libcuda.so fi export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH @@ -75,9 +75,9 @@ if [ "$install_tools" = true ] ; then sudo apt-get install -y cmake sudo apt-get install -y powershell if [ "$bypass_driver_installation" = true ] ; then - sudo ln -s /usr/local/cuda-11.2/lib64/stubs/libcuda.so /usr/local/cuda-11.2/lib64/stubs/libcuda.so.1 - sudo ln -s /usr/local/cuda-11.2/lib64/stubs/libcuda.so /usr/local/cuda-11.2/lib64/libcuda.so.1 - sudo ln -s /usr/local/cuda-11.2/lib64/stubs/libcuda.so /usr/local/cuda-11.2/lib64/libcuda.so + sudo ln -s /usr/local/cuda-11.4/lib64/stubs/libcuda.so /usr/local/cuda-11.4/lib64/stubs/libcuda.so.1 + sudo ln -s /usr/local/cuda-11.4/lib64/stubs/libcuda.so /usr/local/cuda-11.4/lib64/libcuda.so.1 + sudo ln -s /usr/local/cuda-11.4/lib64/stubs/libcuda.so /usr/local/cuda-11.4/lib64/libcuda.so fi export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH