diff --git a/CMakeLists.txt b/CMakeLists.txt index 5669a470..e2831afb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,28 @@ endif() project(darknet) +# To enable CUDA, launch cmake with -DENABLE_CUDA. Also it is advisable to set the highest compute model from below, depending on your specific card capabilities +# If CUDA is not required, this setting is completely ignored. +#set(COMPUTE_MODEL "75") #Tesla T4, RTX 2080 +#set(COMPUTE_MODEL "72") #Jetson Xavier +#set(COMPUTE_MODEL "70") #Tesla V100 +#set(COMPUTE_MODEL "62") #Jetson TX2 +#set(COMPUTE_MODEL "61") #Telsa P40 +set(COMPUTE_MODEL "60") #Telsa P100 +#set(COMPUTE_MODEL "53") #Jetson TX1 +#set(COMPUTE_MODEL "52") #Tesla M40/M60 +#set(COMPUTE_MODEL "37") #Tesla K80 +#set(COMPUTE_MODEL "35") #Tesla K20/K40 +#set(COMPUTE_MODEL "30") #Tesla K10, Quadro K4000 +### +if(FORCE_CUSTOM_LIBS) + include_directories(${CMAKE_CURRENT_LIST_DIR}/3rdparty/include) + set(PTHREADS_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/3rdparty/include) + set(PTHREADS_LIBRARY ${CMAKE_CURRENT_LIST_DIR}/3rdparty/lib/x64/pthreadVC2.lib) + add_definitions(-D_TIMESPEC_DEFINED) +endif() +### + enable_language(C) enable_language(CXX) if(ENABLE_CUDA) @@ -57,10 +79,12 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") string(REGEX REPLACE "-O3" "-Ofast" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) endif() -if(APPLE) - find_package(OpenCV PATHS "/usr/local/Cellar/opencv@3/3.4.5" NO_DEFAULT_PATH) -else() - find_package(OpenCV) +if(NOT FORCE_CUSTOM_LIBS) + if(APPLE) + find_package(OpenCV PATHS "/usr/local/Cellar/opencv@3/3.4.5" NO_DEFAULT_PATH) + else() + find_package(OpenCV) + endif() endif() if(OpenCV_FOUND) @@ -70,24 +94,18 @@ endif() if(ENABLE_CUDA) add_definitions(-DGPU) - #set(COMPUTE_MODEL "75") #Tesla T4, RTX 2080 - #set(COMPUTE_MODEL "72") #Jetson Xavier - #set(COMPUTE_MODEL "70") #Tesla V100 - #set(COMPUTE_MODEL "62") #Jetson TX2 - #set(COMPUTE_MODEL "61") #Telsa P40 - #set(COMPUTE_MODEL "60") #Telsa P100 - #set(COMPUTE_MODEL "53") #Jetson TX1 - #set(COMPUTE_MODEL "52") #Tesla M40/M60 - #set(COMPUTE_MODEL "37") #Tesla K80 - #set(COMPUTE_MODEL "35") #Tesla K20/K40 - set(COMPUTE_MODEL "30") #Tesla K10, Quadro K4000 - if(COMPUTE_MODEL VERSION_GREATER_EQUAL 53) - add_definitions(-DCUDNN_HALF) + if(COMPUTE_MODEL VERSION_GREATER_EQUAL 75 AND CMAKE_CUDA_COMPILER_VERSION VERSION_LESS_EQUAL 9) + message(STATUS "CUDA 10 is required for Compute Model 7.5 - Lowering settings to 7.0") + set(COMPUTE_MODEL "70") endif() find_package(CUDNN) if(CUDNN_FOUND) add_definitions(-DCUDNN) include_directories(${CUDNN_INCLUDE_DIR}) + if(COMPUTE_MODEL VERSION_GREATER_EQUAL 53) + add_definitions(-DCUDNN_HALF) + message(STATUS "Enabled CUDNN_HALF") + endif() endif() if (MSVC) if(CUDNN_FOUND) diff --git a/CMakeSettings.json b/CMakeSettings.json new file mode 100644 index 00000000..1c8184e4 --- /dev/null +++ b/CMakeSettings.json @@ -0,0 +1,40 @@ +{ + "configurations": [ + { + "buildCommandArgs": "-m -v:minimal", + "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", + "cmakeCommandArgs": "", + "configurationType": "Debug", + "generator": "Visual Studio 15 2017 Win64", + "name": "x64-Debug", + "variables": [ + { + "name": "CMAKE_TOOLCHAIN_FILE", + "value": "${env.VCPKG_ROOT}\\scripts\\buildsystems\\vcpkg.cmake" + }, + { + "name": "VCPKG_TARGET_TRIPLET", + "value": "${env.VCPKG_DEFAULT_TRIPLET}" + } + ] + }, + { + "buildCommandArgs": "-m -v:minimal", + "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}", + "cmakeCommandArgs": "", + "configurationType": "Release", + "generator": "Visual Studio 15 2017 Win64", + "name": "x64-Release", + "variables": [ + { + "name": "CMAKE_TOOLCHAIN_FILE", + "value": "${env.VCPKG_ROOT}\\scripts\\buildsystems\\vcpkg.cmake" + }, + { + "name": "VCPKG_TARGET_TRIPLET", + "value": "${env.VCPKG_DEFAULT_TRIPLET}" + } + ] + } + ] +} diff --git a/README.md b/README.md index 3dba89d1..983ee17e 100644 --- a/README.md +++ b/README.md @@ -6,21 +6,20 @@ * [Requirements](#requirements) * [Pre-trained models](#pre-trained-models) * [Explanations in issues](https://github.com/AlexeyAB/darknet/issues?q=is%3Aopen+is%3Aissue+label%3AExplanations) -0. [Improvements in this repository](#improvements-in-this-repository) -1. [How to use](#how-to-use) -2. [How to compile on Linux](#how-to-compile-on-linux) -3. [How to compile on Windows](#how-to-compile-on-windows) -4. [How to train (Pascal VOC Data)](#how-to-train-pascal-voc-data) -5. [How to train (to detect your custom objects)](#how-to-train-to-detect-your-custom-objects) -6. [When should I stop training](#when-should-i-stop-training) -7. [How to calculate mAP on PascalVOC 2007](#how-to-calculate-map-on-pascalvoc-2007) -8. [How to improve object detection](#how-to-improve-object-detection) -9. [How to mark bounded boxes of objects and create annotation files](#how-to-mark-bounded-boxes-of-objects-and-create-annotation-files) + +0. [Improvements in this repository](#improvements-in-this-repository) +1. [How to use](#how-to-use) +2. [How to compile on Linux](#how-to-compile-on-linux) +3. [How to compile on Windows](#how-to-compile-on-windows) +4. [How to train (Pascal VOC Data)](#how-to-train-pascal-voc-data) +5. [How to train (to detect your custom objects)](#how-to-train-to-detect-your-custom-objects) +6. [When should I stop training](#when-should-i-stop-training) +7. [How to calculate mAP on PascalVOC 2007](#how-to-calculate-map-on-pascalvoc-2007) +8. [How to improve object detection](#how-to-improve-object-detection) +9. [How to mark bounded boxes of objects and create annotation files](#how-to-mark-bounded-boxes-of-objects-and-create-annotation-files) 10. [Using Yolo9000](#using-yolo9000) 11. [How to use Yolo as DLL and SO libraries](#how-to-use-yolo-as-dll-and-so-libraries) - - | ![Darknet Logo](http://pjreddie.com/media/files/darknet-black-small.png) |   ![map_time](https://user-images.githubusercontent.com/4096485/52151356-e5d4a380-2683-11e9-9d7d-ac7bc192c477.jpg) mAP@0.5 (AP50) https://pjreddie.com/media/files/papers/YOLOv3.pdf | |---|---| @@ -29,9 +28,9 @@ * Yolo v2 on Pascal VOC 2007: https://hsto.org/files/a24/21e/068/a2421e0689fb43f08584de9d44c2215f.jpg * Yolo v2 on Pascal VOC 2012 (comp4): https://hsto.org/files/3a6/fdf/b53/3a6fdfb533f34cee9b52bdd9bb0b19d9.jpg +## "You Only Look Once: Unified, Real-Time Object Detection (versions 2 & 3)" -# "You Only Look Once: Unified, Real-Time Object Detection (versions 2 & 3)" -A Yolo cross-platform Windows and Linux version (for object detection). Contributtors: https://github.com/AlexeyAB/darknet/graphs/contributors +A Yolo cross-platform Windows and Linux version (for object detection). Contributors: https://github.com/AlexeyAB/darknet/graphs/contributors This repository is forked from Linux-version: https://github.com/pjreddie/darknet @@ -45,15 +44,14 @@ This repository supports: * CUDA >= 7.5 * also create SO-library on Linux and DLL-library on Windows -##### Requirements: -* **Linux GCC>=4.9 or Windows MS Visual Studio 2015 (v140)**: https://go.microsoft.com/fwlink/?LinkId=532606&clcid=0x409 (or offline [ISO image](https://go.microsoft.com/fwlink/?LinkId=615448&clcid=0x409)) +### Requirements + +* **CMake >= 3.8** for modern CUDA support: https://cmake.org/download/ * **CUDA 10.0**: https://developer.nvidia.com/cuda-toolkit-archive (on Linux do [Post-installation Actions](https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#post-installation-actions)) -* **OpenCV 3.3.0**: https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.3.0/opencv-3.3.0-vc14.exe/download -* **or OpenCV 2.4.13**: https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.13/opencv-2.4.13.2-vc14.exe/download - - OpenCV allows to show image or video detection in the window and store result to file that specified in command line `-out_filename res.avi` +* **OpenCV < 4.0**: use your preferred package manager (brew, apt) or build from source using [vcpkg](https://github.com/Microsoft/vcpkg) * **GPU with CC >= 3.0**: https://en.wikipedia.org/wiki/CUDA#GPUs_supported -##### Pre-trained models +#### Pre-trained models There are weights-file for different cfg-files (smaller size -> faster speed & lower accuracy: @@ -71,7 +69,7 @@ Put it near compiled: darknet.exe You can get cfg-files by path: `darknet/cfg/` -##### Examples of results: +##### Examples of results [![Everything Is AWESOME](http://img.youtube.com/vi/VOC3huqHrss/0.jpg)](https://www.youtube.com/watch?v=VOC3huqHrss "Everything Is AWESOME") @@ -102,10 +100,7 @@ And added manual - [How to train Yolo v3/v2 (to detect your custom objects)](#ho Also, you might be interested in using a simplified repository where is implemented INT8-quantization (+30% speedup and -1% mAP reduced): https://github.com/AlexeyAB/yolo2_light -### How to use: - - -##### How to use on the command line: +#### How to use on the command line On Linux use `./darknet` instead of `darknet.exe`, like this:`./darknet detector test ./cfg/coco.data ./cfg/yolov3.cfg ./yolov3.weights` @@ -133,11 +128,10 @@ On Linux find executable file `./darknet` in the root directory, while on Window * To check accuracy mAP@IoU=50: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` * To check accuracy mAP@IoU=75: `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights -iou_thresh 0.75` -##### For using network video-camera mjpeg-stream with any Android smartphone: +##### For using network video-camera mjpeg-stream with any Android smartphone 1. Download for Android phone mjpeg-stream soft: IP Webcam / Smart WebCam - * Smart WebCam - preferably: https://play.google.com/store/apps/details?id=com.acontech.android.SmartWebCam2 * IP Webcam: https://play.google.com/store/apps/details?id=com.pas.webcam @@ -145,14 +139,13 @@ On Linux find executable file `./darknet` in the root directory, while on Window 3. Start Smart WebCam on your phone 4. Replace the address below, on shown in the phone application (Smart WebCam) and launch: - * Yolo v3 COCO-model: `darknet.exe detector demo data/coco.data yolov3.cfg yolov3.weights http://192.168.0.80:8080/video?dummy=param.mjpg -i 0` - -### How to compile on Linux: +### How to compile on Linux Just do `make` in the darknet directory. Before make, you can set such options in the `Makefile`: [link](https://github.com/AlexeyAB/darknet/blob/9c1b9a2cf6363546c152251be578a21f3c3caec6/Makefile#L1) + * `GPU=1` to build with CUDA to accelerate by using GPU (CUDA should be in `/usr/local/cuda`) * `CUDNN=1` to build with cuDNN v5-v7 to accelerate training by using GPU (cuDNN should be in `/usr/local/cudnn`) * `CUDNN_HALF=1` to build for Tensor Cores (on Titan V / Tesla V100 / DGX-2 and later) speedup Detection 3x, Training 2x @@ -164,7 +157,32 @@ Before make, you can set such options in the `Makefile`: [link](https://github.c To run Darknet on Linux use examples from this article, just use `./darknet` instead of `darknet.exe`, i.e. use this command: `./darknet detector test ./cfg/coco.data ./cfg/yolov3.cfg ./yolov3.weights` -### How to compile on Windows: +### How to compile on Windows (using `vcpkg`) + +1. Install or update Visual Studio to at least version 2017, making sure to have it fully patched (run again the installer if not sure to automatically update to latest version). If you need to install from scratch, download VS from here: [Visual Studio 2017 Community](http://visualstudio.com) + +2. Install CUDA and cuDNN + +3. Install `git` and `cmake`. Make sure they are on the Path at least for the current account + +4. Install [vcpkg](https://github.com/Microsoft/vcpkg) and try to install a test library to make sure everything is working, for example `vcpkg install opengl` + +5. Define an environment variables, `VCPKG_ROOT`, pointing to the install path of `vcpkg` + +6. Define another environment variable, with name `VCPKG_DEFAULT_TRIPLET` and value `x64-windows` + +7. Open a Powershell (as a standard user) and type (the last command requires a confirmation and is used to clean up unnecessary files) + +```PowerShell +PS \> cd $env:VCPKG_ROOT +PS Code\vcpkg> .\vcpkg install pthreads opencv #replace with opencv[cuda] in case you want to use cuda-accelerated openCV +``` + +8. [necessary only with CUDA] Customize the CMakeLists.txt with the preferred compute capability + +9. Build with the Powershell script `build.ps1` or use the "Open Folder" functionality of Visual Studio 2017. In the first option, if you want to use Visual Studio, you will find a custom solution created for you by CMake after the build containing all the appropriate config flags for your system. + +### How to compile on Windows (legacy way) 1. If you have **MSVS 2015, CUDA 10.0, cuDNN 7.4 and OpenCV 3.x** (with paths: `C:\opencv_3.0\opencv\build\include` & `C:\opencv_3.0\opencv\build\x64\vc14\lib`), then start MSVS, open `build\darknet\darknet.sln`, set **x64** and **Release** https://hsto.org/webt/uh/fk/-e/uhfk-eb0q-hwd9hsxhrikbokd6u.jpeg and do the: Build -> Build darknet. Also add Windows system variable `cudnn` with path to CUDNN: https://hsto.org/files/a49/3dc/fc4/a493dcfc4bd34a1295fd15e0e2e01f26.jpg **NOTE:** If installing OpenCV, use OpenCV 3.4.0 or earlier. This is a bug in OpenCV 3.4.1 in the C API (see [#500](https://github.com/AlexeyAB/darknet/issues/500)). diff --git a/build.ps1 b/build.ps1 new file mode 100755 index 00000000..f1080ab7 --- /dev/null +++ b/build.ps1 @@ -0,0 +1,46 @@ +#!/usr/bin/env pwsh + +pushd "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools" +cmd /c "VsDevCmd.bat -arch=x64 & set" | +foreach { + if ($_ -match "=") { + $v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])" + } +} +popd +Write-Host "Visual Studio 2017 Command Prompt variables set.`n" -ForegroundColor Yellow + +# CPU ONLY, DEBUG +#New-Item -Path .\build_win_debug -ItemType directory -Force +#Set-Location build_win_debug +#cmake -G "Visual Studio 15 2017" -T "host=x64" -A "x64" "-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$env:VCPKG_DEFAULT_TRIPLET" "-DCMAKE_BUILD_TYPE=Debug" .. +#cmake --build . --config Debug +#Set-Location .. + +# CPU ONLY, RELEASE +#New-Item -Path .\build_win_release -ItemType directory -Force +#Set-Location build_win_release +#cmake -G "Visual Studio 15 2017" -T "host=x64" -A "x64" "-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$env:VCPKG_DEFAULT_TRIPLET" "-DCMAKE_BUILD_TYPE=Release" .. +#cmake --build . --config Release +#Set-Location .. + +# CUDA, DEBUG +#New-Item -Path .\build_win_debug_cuda -ItemType directory -Force +#Set-Location build_win_debug_cuda +#cmake -G "Visual Studio 15 2017" -T "host=x64" -A "x64" "-DENABLE_CUDA:BOOL=TRUE" "-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$env:VCPKG_DEFAULT_TRIPLET" "-DCMAKE_BUILD_TYPE=Debug" .. +#cmake --build . --config Debug +#Set-Location .. + +# CUDA, RELEASE +#New-Item -Path .\build_win_release_cuda -ItemType directory -Force +#Set-Location build_win_release_cuda +#cmake -G "Visual Studio 15 2017" -T "host=x64" -A "x64" "-DENABLE_CUDA:BOOL=TRUE" "-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$env:VCPKG_DEFAULT_TRIPLET" "-DCMAKE_BUILD_TYPE=Release" .. +#cmake --build . --config Release +#Set-Location .. + +# CPU ONLY, USE LOCAL PTHREAD LIB, NO VCPKG: remember to use "vcpkg.exe integrate remove" in case you had enable user-wide vcpkg integration +New-Item -Path .\build_win_release_cuda_custom_libs -ItemType directory -Force +Set-Location build_win_release_cuda_custom_libs +cmake -G "Visual Studio 15 2017" -T "host=x64" -A "x64" "-DFORCE_CUSTOM_LIBS:BOOL=TRUE" "-DENABLE_CUDA:BOOL=TRUE" .. +cmake --build . --config Release +Set-Location .. diff --git a/cmake/Modules/FindCUDNN.cmake b/cmake/Modules/FindCUDNN.cmake index bc130ea4..c5f28c53 100644 --- a/cmake/Modules/FindCUDNN.cmake +++ b/cmake/Modules/FindCUDNN.cmake @@ -30,13 +30,17 @@ include(FindPackageHandleStandardArgs) #set(CUDNN_ROOT_DIR "" CACHE PATH "Folder contains NVIDIA cuDNN") -find_path(CUDNN_INCLUDE_DIR cudnn.h +if(NOT CUDNN_INCLUDE_DIR) + find_path(CUDNN_INCLUDE_DIR cudnn.h HINTS ${CUDNN_ROOT_DIR} ${CUDA_HOME} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES cuda/include include) +endif() -find_library(CUDNN_LIBRARY cudnn +if(NOT CUDNN_LIBRARY) + find_library(CUDNN_LIBRARY cudnn HINTS ${CUDNN_ROOT_DIR} ${CUDA_HOME} ${CUDA_TOOLKIT_ROOT_DIR} PATH_SUFFIXES lib lib64 cuda/lib cuda/lib64 lib/x64) +endif() find_package_handle_standard_args( CUDNN DEFAULT_MSG CUDNN_INCLUDE_DIR CUDNN_LIBRARY) diff --git a/cmake/Modules/FindPThreads_windows.cmake b/cmake/Modules/FindPThreads_windows.cmake index 4f6a9f24..2b7d76a4 100644 --- a/cmake/Modules/FindPThreads_windows.cmake +++ b/cmake/Modules/FindPThreads_windows.cmake @@ -25,7 +25,9 @@ include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) include(${CMAKE_ROOT}/Modules/SelectLibraryConfigurations.cmake) -find_path(PTHREADS_INCLUDE_DIR NAMES pthread.h) +if(NOT PTHREADS_INCLUDE_DIR) + find_path(PTHREADS_INCLUDE_DIR NAMES pthread.h) +endif() # Allow libraries to be set manually if(NOT PTHREADS_LIBRARY)