diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index dd8a98dd856..21ff07296fb 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -1,6 +1,10 @@ name: Darknet Continuous Integration -on: [push, workflow_dispatch] +on: + push: + workflow_dispatch: + schedule: + - cron: '0 0 * * *' env: VCPKG_BINARY_SOURCES: 'clear;nuget,vcpkgbinarycache,readwrite' @@ -17,24 +21,13 @@ jobs: run: sudo apt install libopencv-dev - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' run: | - sudo apt update - sudo apt-get dist-upgrade -y - sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" - 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 libcudnn8-dev - sudo rm -rf /usr/local/cuda 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.2 /usr/local/cuda - export PATH=/usr/local/cuda/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH - nvcc --version - gcc --version - name: 'LIBSO=1 GPU=0 CUDNN=0 OPENCV=0' run: | @@ -72,37 +65,26 @@ jobs: make clean - ubuntu-vcpkg-cuda: + ubuntu-vcpkg-opencv4-cuda: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 + - uses: lukka/get-cmake@latest + - name: Update apt run: sudo apt update - name: Install dependencies run: sudo apt install yasm nasm - - uses: lukka/get-cmake@latest - - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' run: | - sudo apt update - sudo apt-get dist-upgrade -y - sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" - 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 libcudnn8-dev - sudo rm -rf /usr/local/cuda 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.2 /usr/local/cuda - export PATH=/usr/local/cuda/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH - nvcc --version - gcc --version - name: 'Setup vcpkg and NuGet artifacts backend' shell: bash @@ -123,7 +105,7 @@ jobs: CUDA_PATH: "/usr/local/cuda" CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" - run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -DisableInteractive -DoNotUpdateDARKNET + run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -DisableInteractive -DoNotUpdateDARKNET - uses: actions/upload-artifact@v2 with: @@ -143,6 +125,92 @@ jobs: path: ${{ github.workspace }}/uselib* + ubuntu-vcpkg-opencv3-cuda: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - uses: lukka/get-cmake@latest + + - name: Update apt + run: sudo apt update + - name: Install dependencies + run: sudo apt install yasm nasm + + - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' + run: | + 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 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) + setapikey ${{ secrets.BAGET_API_KEY }} + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Build' + shell: pwsh + env: + CUDACXX: "/usr/local/cuda/bin/nvcc" + CUDA_PATH: "/usr/local/cuda" + CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" + LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" + run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -ForceOpenCVVersion 3 -DisableInteractive -DoNotUpdateDARKNET + + + ubuntu-vcpkg-opencv2-cuda: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - uses: lukka/get-cmake@latest + + - name: Update apt + run: sudo apt update + - name: Install dependencies + run: sudo apt install yasm nasm + + - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' + run: | + 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 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) + setapikey ${{ secrets.BAGET_API_KEY }} + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Build' + shell: pwsh + env: + CUDACXX: "/usr/local/cuda/bin/nvcc" + CUDA_PATH: "/usr/local/cuda" + CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" + LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" + run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -ForceOpenCVVersion 2 -DisableInteractive -DoNotUpdateDARKNET + + ubuntu: runs-on: ubuntu-20.04 steps: @@ -195,24 +263,13 @@ jobs: - uses: lukka/get-cmake@latest - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' run: | - sudo apt update - sudo apt-get dist-upgrade -y - sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" - 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 libcudnn8-dev - sudo rm -rf /usr/local/cuda 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.2 /usr/local/cuda - export PATH=/usr/local/cuda/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH - nvcc --version - gcc --version - name: 'Build' shell: pwsh @@ -221,7 +278,7 @@ jobs: CUDA_PATH: "/usr/local/cuda" CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" - run: ./build.ps1 -EnableOPENCV -EnableCUDA -DisableInteractive -DoNotUpdateDARKNET + run: ./build.ps1 -EnableOPENCV -EnableCUDA -EnableCUDNN -DisableInteractive -DoNotUpdateDARKNET - uses: actions/upload-artifact@v2 with: @@ -253,6 +310,28 @@ jobs: run: ./build.ps1 -ForceCPP -DisableInteractive -DoNotUpdateDARKNET + ubuntu-setup-sh: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) + setapikey ${{ secrets.BAGET_API_KEY }} + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Setup' + shell: bash + run: ./scripts/setup.sh -InstallCUDA -BypassDRIVER + + osx-vcpkg: runs-on: macos-latest steps: @@ -419,6 +498,28 @@ jobs: path: ${{ github.workspace }}/uselib* + win-setup-ps1: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json ; + $(./vcpkg/vcpkg fetch nuget | tail -n 1) + setapikey ${{ secrets.BAGET_API_KEY }} + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Setup' + shell: pwsh + run: ./scripts/setup.ps1 -InstallCUDA + + win-intlibs-cpp: runs-on: windows-latest steps: @@ -431,6 +532,18 @@ jobs: run: ./build.ps1 -ForceCPP -DisableInteractive -DoNotUpdateDARKNET + win-csharp: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + + - uses: lukka/get-cmake@latest + + - name: 'Build' + shell: pwsh + run: ./build.ps1 -EnableCSharpWrapper -DisableInteractive -DoNotUpdateDARKNET + + win-intlibs-cuda: runs-on: windows-latest steps: diff --git a/.github/workflows/on_pr.yml b/.github/workflows/on_pr.yml index 198d84fc4e0..6b6aface5b8 100644 --- a/.github/workflows/on_pr.yml +++ b/.github/workflows/on_pr.yml @@ -17,24 +17,13 @@ jobs: run: sudo apt install libopencv-dev - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' run: | - sudo apt update - sudo apt-get dist-upgrade -y - sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" - 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 libcudnn8-dev - sudo rm -rf /usr/local/cuda 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.2 /usr/local/cuda - export PATH=/usr/local/cuda/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH - nvcc --version - gcc --version - name: 'LIBSO=1 GPU=0 CUDNN=0 OPENCV=0' run: | @@ -72,37 +61,26 @@ jobs: make clean - ubuntu-vcpkg-cuda: + ubuntu-vcpkg-opencv4-cuda: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 + - uses: lukka/get-cmake@latest + - name: Update apt run: sudo apt update - name: Install dependencies run: sudo apt install yasm nasm - - uses: lukka/get-cmake@latest - - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' run: | - sudo apt update - sudo apt-get dist-upgrade -y - sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" - 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 libcudnn8-dev - sudo rm -rf /usr/local/cuda 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.2 /usr/local/cuda - export PATH=/usr/local/cuda/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH - nvcc --version - gcc --version - name: 'Setup vcpkg and NuGet artifacts backend' shell: bash @@ -120,7 +98,7 @@ jobs: CUDA_PATH: "/usr/local/cuda" CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" - run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -DisableInteractive -DoNotUpdateDARKNET + run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -DisableInteractive -DoNotUpdateDARKNET - uses: actions/upload-artifact@v2 with: @@ -140,6 +118,86 @@ jobs: path: ${{ github.workspace }}/uselib* + ubuntu-vcpkg-opencv3-cuda: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - uses: lukka/get-cmake@latest + + - name: Update apt + run: sudo apt update + - name: Install dependencies + run: sudo apt install yasm nasm + + - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' + run: | + 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 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Build' + shell: pwsh + env: + CUDACXX: "/usr/local/cuda/bin/nvcc" + CUDA_PATH: "/usr/local/cuda" + CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" + LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" + run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -ForceOpenCVVersion 3 -DisableInteractive -DoNotUpdateDARKNET + + + ubuntu-vcpkg-opencv2-cuda: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - uses: lukka/get-cmake@latest + + - name: Update apt + run: sudo apt update + - name: Install dependencies + run: sudo apt install yasm nasm + + - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' + run: | + 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 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Build' + shell: pwsh + env: + CUDACXX: "/usr/local/cuda/bin/nvcc" + CUDA_PATH: "/usr/local/cuda" + CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" + LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" + run: ./build.ps1 -UseVCPKG -DoNotUpdateVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -ForceOpenCVVersion 2 -DisableInteractive -DoNotUpdateDARKNET + + ubuntu: runs-on: ubuntu-20.04 steps: @@ -192,24 +250,13 @@ jobs: - uses: lukka/get-cmake@latest - name: 'Install CUDA' + run: ./scripts/deploy-cuda.sh + + - name: 'Create softlinks for CUDA' run: | - sudo apt update - sudo apt-get dist-upgrade -y - sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" - 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 libcudnn8-dev - sudo rm -rf /usr/local/cuda 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.2 /usr/local/cuda - export PATH=/usr/local/cuda/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH - nvcc --version - gcc --version - name: 'Build' shell: pwsh @@ -218,7 +265,7 @@ jobs: CUDA_PATH: "/usr/local/cuda" CUDA_TOOLKIT_ROOT_DIR: "/usr/local/cuda" LD_LIBRARY_PATH: "/usr/local/cuda/lib64:/usr/local/cuda/lib64/stubs:$LD_LIBRARY_PATH" - run: ./build.ps1 -EnableOPENCV -EnableCUDA -DisableInteractive -DoNotUpdateDARKNET + run: ./build.ps1 -EnableOPENCV -EnableCUDA -EnableCUDNN -DisableInteractive -DoNotUpdateDARKNET - uses: actions/upload-artifact@v2 with: @@ -250,6 +297,25 @@ jobs: run: ./build.ps1 -ForceCPP -DisableInteractive -DoNotUpdateDARKNET + ubuntu-setup-sh: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + mono $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Setup' + shell: bash + run: ./scripts/setup.sh -InstallCUDA -BypassDRIVER + + osx-vcpkg: runs-on: macos-latest steps: @@ -410,6 +476,25 @@ jobs: path: ${{ github.workspace }}/uselib* + win-setup-ps1: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + + - name: 'Setup vcpkg and NuGet artifacts backend' + shell: bash + run: > + git clone https://github.com/microsoft/vcpkg ; + ./vcpkg/bootstrap-vcpkg.sh ; + $(./vcpkg/vcpkg fetch nuget | tail -n 1) sources add + -Name "vcpkgbinarycache" + -Source http://93.49.111.10:5555/v3/index.json + + - name: 'Setup' + shell: pwsh + run: ./scripts/setup.ps1 -InstallCUDA + + win-intlibs-cpp: runs-on: windows-latest steps: @@ -422,6 +507,18 @@ jobs: run: ./build.ps1 -ForceCPP -DisableInteractive -DoNotUpdateDARKNET + win-csharp: + runs-on: windows-latest + steps: + - uses: actions/checkout@v2 + + - uses: lukka/get-cmake@latest + + - name: 'Build' + shell: pwsh + run: ./build.ps1 -EnableCSharpWrapper -DisableInteractive -DoNotUpdateDARKNET + + win-intlibs-cuda: runs-on: windows-latest steps: diff --git a/.gitignore b/.gitignore index 174f0b5a378..916cfb88461 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,8 @@ cfg/ temp/ build/darknet/* build_*/ +ninja/ +ninja.zip vcpkg_installed/ !build/darknet/YoloWrapper.cs .fuse* @@ -36,6 +38,7 @@ build/.ninja_deps build/.ninja_log build/Makefile */vcpkg-manifest-install.log +build.log # OS Generated # .DS_Store* diff --git a/.travis.yml b/.travis.yml index 447a72a179d..f208498dbcd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,32 +16,6 @@ matrix: - additional_defines=" -DENABLE_CUDA=OFF -DENABLE_CUDNN=OFF -DENABLE_OPENCV=OFF" - MATRIX_EVAL="" - - os: osx - compiler: gcc - name: macOS - gcc (llvm backend) - opencv@2 - osx_image: xcode12.3 - env: - - OpenCV_DIR="/usr/local/opt/opencv@2/" - - additional_defines="-DOpenCV_DIR=${OpenCV_DIR} -DENABLE_CUDA=OFF" - - MATRIX_EVAL="brew install opencv@2" - - - os: osx - compiler: gcc - name: macOS - gcc (llvm backend) - opencv@3 - osx_image: xcode12.3 - env: - - OpenCV_DIR="/usr/local/opt/opencv@3/" - - additional_defines="-DOpenCV_DIR=${OpenCV_DIR} -DENABLE_CUDA=OFF" - - MATRIX_EVAL="brew install opencv@3" - - - os: osx - compiler: gcc - name: macOS - gcc (llvm backend) - opencv(latest) - osx_image: xcode12.3 - env: - - additional_defines=" -DENABLE_CUDA=OFF" - - MATRIX_EVAL="brew install opencv" - - os: osx compiler: clang name: macOS - clang @@ -58,40 +32,6 @@ matrix: - additional_defines="-DBUILD_AS_CPP:BOOL=TRUE -DENABLE_CUDA=OFF -DENABLE_CUDNN=OFF -DENABLE_OPENCV=OFF" - MATRIX_EVAL="" - - os: osx - compiler: clang - name: macOS - clang - opencv@2 - osx_image: xcode12.3 - env: - - OpenCV_DIR="/usr/local/opt/opencv@2/" - - additional_defines="-DOpenCV_DIR=${OpenCV_DIR} -DENABLE_CUDA=OFF" - - MATRIX_EVAL="brew install opencv@2" - - - os: osx - compiler: clang - name: macOS - clang - opencv@3 - osx_image: xcode12.3 - env: - - OpenCV_DIR="/usr/local/opt/opencv@3/" - - additional_defines="-DOpenCV_DIR=${OpenCV_DIR} -DENABLE_CUDA=OFF" - - MATRIX_EVAL="brew install opencv@3" - - - os: osx - compiler: clang - name: macOS - clang - opencv(latest) - osx_image: xcode12.3 - env: - - additional_defines=" -DENABLE_CUDA=OFF" - - MATRIX_EVAL="brew install opencv" - - - os: osx - compiler: clang - name: macOS - clang - opencv(latest) - libomp - osx_image: xcode12.3 - env: - - additional_defines=" -DENABLE_CUDA=OFF" - - MATRIX_EVAL="brew install opencv libomp" - - os: linux compiler: clang dist: bionic diff --git a/CMakeLists.txt b/CMakeLists.txt index 0029abe78ee..0e1abf32d9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ set(Darknet_PATCH_VERSION 5) set(Darknet_TWEAK_VERSION 4) set(Darknet_VERSION ${Darknet_MAJOR_VERSION}.${Darknet_MINOR_VERSION}.${Darknet_PATCH_VERSION}.${Darknet_TWEAK_VERSION}) +message("Darknet_VERSION: ${Darknet_VERSION}") + option(CMAKE_VERBOSE_MAKEFILE "Create verbose makefile" ON) option(CUDA_VERBOSE_BUILD "Create verbose CUDA build" ON) option(BUILD_SHARED_LIBS "Create dark as a shared library" ON) @@ -19,20 +21,50 @@ option(ENABLE_CUDNN "Enable CUDNN" ON) option(ENABLE_CUDNN_HALF "Enable CUDNN Half precision" ON) option(ENABLE_ZED_CAMERA "Enable ZED Camera support" ON) option(ENABLE_VCPKG_INTEGRATION "Enable VCPKG integration" ON) +option(ENABLE_CSHARP_WRAPPER "Enable building a csharp wrapper" OFF) option(VCPKG_BUILD_OPENCV_WITH_CUDA "Build OpenCV with CUDA extension integration" ON) +option(VCPKG_USE_OPENCV2 "Use legacy OpenCV 2" OFF) +option(VCPKG_USE_OPENCV3 "Use legacy OpenCV 3" OFF) +option(VCPKG_USE_OPENCV4 "Use OpenCV 4" ON) -if(VCPKG_BUILD_OPENCV_WITH_CUDA AND NOT APPLE) - list(APPEND VCPKG_MANIFEST_FEATURES "opencv-cuda") +if(VCPKG_USE_OPENCV4 AND VCPKG_USE_OPENCV2) + message(STATUS "You required vcpkg feature related to OpenCV 2 but forgot to turn off those for OpenCV 4, doing that for you") + set(VCPKG_USE_OPENCV4 OFF CACHE BOOL "Use OpenCV 4" FORCE) +endif() +if(VCPKG_USE_OPENCV4 AND VCPKG_USE_OPENCV3) + message(STATUS "You required vcpkg feature related to OpenCV 3 but forgot to turn off those for OpenCV 4, doing that for you") + set(VCPKG_USE_OPENCV4 OFF CACHE BOOL "Use OpenCV 4" FORCE) +endif() +if(VCPKG_USE_OPENCV2 AND VCPKG_USE_OPENCV3) + message(STATUS "You required vcpkg features related to both OpenCV 2 and OpenCV 3. Impossible to satisfy, keeping only OpenCV 3") + set(VCPKG_USE_OPENCV2 OFF CACHE BOOL "Use legacy OpenCV 2" FORCE) endif() + if(ENABLE_CUDA AND NOT APPLE) list(APPEND VCPKG_MANIFEST_FEATURES "cuda") endif() -if(ENABLE_OPENCV) - list(APPEND VCPKG_MANIFEST_FEATURES "opencv-base") -endif() if(ENABLE_CUDNN AND ENABLE_CUDA AND NOT APPLE) list(APPEND VCPKG_MANIFEST_FEATURES "cudnn") endif() +if(ENABLE_OPENCV) + if(VCPKG_BUILD_OPENCV_WITH_CUDA AND NOT APPLE) + if(VCPKG_USE_OPENCV4) + list(APPEND VCPKG_MANIFEST_FEATURES "opencv-cuda") + elseif(VCPKG_USE_OPENCV3) + list(APPEND VCPKG_MANIFEST_FEATURES "opencv3-cuda") + elseif(VCPKG_USE_OPENCV2) + list(APPEND VCPKG_MANIFEST_FEATURES "opencv2-cuda") + endif() + else() + if(VCPKG_USE_OPENCV4) + list(APPEND VCPKG_MANIFEST_FEATURES "opencv-base") + elseif(VCPKG_USE_OPENCV3) + list(APPEND VCPKG_MANIFEST_FEATURES "opencv3-base") + elseif(VCPKG_USE_OPENCV2) + list(APPEND VCPKG_MANIFEST_FEATURES "opencv2-base") + endif() + endif() +endif() if(NOT CMAKE_HOST_SYSTEM_PROCESSOR AND NOT WIN32) execute_process(COMMAND "uname" "-m" OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR OUTPUT_STRIP_TRAILING_WHITESPACE) @@ -235,17 +267,19 @@ set(CMAKE_CXX_FLAGS "${ADDITIONAL_CXX_FLAGS} ${SHAREDLIB_CXX_FLAGS} ${CMAKE_CXX_ set(CMAKE_C_FLAGS "${ADDITIONAL_C_FLAGS} ${SHAREDLIB_C_FLAGS} ${CMAKE_C_FLAGS}") if(OpenCV_FOUND) - if(ENABLE_CUDA AND NOT OpenCV_CUDA_VERSION) - set(BUILD_USELIB_TRACK "FALSE" CACHE BOOL "Build uselib_track" FORCE) - message(STATUS " -> darknet is fine for now, but uselib_track has been disabled!") - message(STATUS " -> Please rebuild OpenCV from sources with CUDA support to enable it") - elseif(ENABLE_CUDA AND OpenCV_CUDA_VERSION) + if(ENABLE_CUDA AND OpenCV_CUDA_VERSION) if(TARGET opencv_cudaoptflow) list(APPEND OpenCV_LINKED_COMPONENTS "opencv_cudaoptflow") endif() if(TARGET opencv_cudaimgproc) list(APPEND OpenCV_LINKED_COMPONENTS "opencv_cudaimgproc") endif() + elseif(ENABLE_CUDA AND NOT OpenCV_CUDA_VERSION) + set(BUILD_USELIB_TRACK "FALSE" CACHE BOOL "Build uselib_track" FORCE) + message(STATUS " -> darknet is fine for now, but uselib_track has been disabled!") + message(STATUS " -> Please rebuild OpenCV from sources with CUDA support to enable it") + else() + set(BUILD_USELIB_TRACK "FALSE" CACHE BOOL "Build uselib_track" FORCE) endif() endif() @@ -543,3 +577,7 @@ install(FILES "${PROJECT_BINARY_DIR}/DarknetConfigVersion.cmake" DESTINATION "${INSTALL_CMAKE_DIR}" ) + +if(ENABLE_CSHARP_WRAPPER) + add_subdirectory(src/csharp) +endif() diff --git a/README.md b/README.md index 4a4a67f49f8..337d095d44f 100644 --- a/README.md +++ b/README.md @@ -6,16 +6,18 @@ 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) -More details in articles on medium: - * [Scaled_YOLOv4](https://alexeyab84.medium.com/scaled-yolo-v4-is-the-best-neural-network-for-object-detection-on-ms-coco-dataset-39dfa22fa982?source=friends_link&sk=c8553bfed861b1a7932f739d26f487c8) - * [YOLOv4](https://medium.com/@alexeyab84/yolov4-the-most-accurate-real-time-neural-network-on-ms-coco-dataset-73adfd3602fe?source=friends_link&sk=6039748846bbcf1d960c3061542591d7) +More details in articles on medium: + +- [Scaled_YOLOv4](https://alexeyab84.medium.com/scaled-yolo-v4-is-the-best-neural-network-for-object-detection-on-ms-coco-dataset-39dfa22fa982?source=friends_link&sk=c8553bfed861b1a7932f739d26f487c8) +- [YOLOv4](https://medium.com/@alexeyab84/yolov4-the-most-accurate-real-time-neural-network-on-ms-coco-dataset-73adfd3602fe?source=friends_link&sk=6039748846bbcf1d960c3061542591d7) Manual: https://github.com/AlexeyAB/darknet/wiki -Discussion: - - [Reddit](https://www.reddit.com/r/MachineLearning/comments/gydxzd/p_yolov4_the_most_accurate_realtime_neural/) - - [Google-groups](https://groups.google.com/forum/#!forum/darknet) - - [Discord](https://discord.gg/zSq8rtW) +Discussion: + +- [Reddit](https://www.reddit.com/r/MachineLearning/comments/gydxzd/p_yolov4_the_most_accurate_realtime_neural/) +- [Google-groups](https://groups.google.com/forum/#!forum/darknet) +- [Discord](https://discord.gg/zSq8rtW) About Darknet framework: http://pjreddie.com/darknet/ @@ -29,73 +31,72 @@ About Darknet framework: http://pjreddie.com/darknet/ [![colab](https://user-images.githubusercontent.com/4096485/86174089-b2709f80-bb29-11ea-9faf-3d8dc668a1a5.png)](https://colab.research.google.com/drive/12QusaaRj_lUwCGDvQNfICpa7kA7_a2dE) [![colab](https://user-images.githubusercontent.com/4096485/86174097-b56b9000-bb29-11ea-9240-c17f6bacfc34.png)](https://colab.research.google.com/drive/1_GdoqCJWXsChrOiY8sZMr_zbr_fH-0Fg) - -* [YOLOv4 model zoo](https://github.com/AlexeyAB/darknet/wiki/YOLOv4-model-zoo) -* [Requirements (and how to install dependencies)](#requirements) -* [Pre-trained models](#pre-trained-models) -* [FAQ - frequently asked questions](https://github.com/AlexeyAB/darknet/wiki/FAQ---frequently-asked-questions) -* [Explanations in issues](https://github.com/AlexeyAB/darknet/issues?q=is%3Aopen+is%3Aissue+label%3AExplanations) -* [Yolo v4 in other frameworks (TensorRT, TensorFlow, PyTorch, OpenVINO, OpenCV-dnn, TVM,...)](#yolo-v4-in-other-frameworks) -* [Datasets](#datasets) +- [YOLOv4 model zoo](https://github.com/AlexeyAB/darknet/wiki/YOLOv4-model-zoo) +- [Requirements (and how to install dependencies)](#requirements) +- [Pre-trained models](#pre-trained-models) +- [FAQ - frequently asked questions](https://github.com/AlexeyAB/darknet/wiki/FAQ---frequently-asked-questions) +- [Explanations in issues](https://github.com/AlexeyAB/darknet/issues?q=is%3Aopen+is%3Aissue+label%3AExplanations) +- [Yolo v4 in other frameworks (TensorRT, TensorFlow, PyTorch, OpenVINO, OpenCV-dnn, TVM,...)](#yolo-v4-in-other-frameworks) +- [Datasets](#datasets) - [Yolo v4, v3 and v2 for Windows and Linux](#yolo-v4-v3-and-v2-for-windows-and-linux) - [(neural networks for object detection)](#neural-networks-for-object-detection) - - [GeForce RTX 2080 Ti:](#geforce-rtx-2080-ti) + - [GeForce RTX 2080 Ti](#geforce-rtx-2080-ti) - [Youtube video of results](#youtube-video-of-results) - [How to evaluate AP of YOLOv4 on the MS COCO evaluation server](#how-to-evaluate-ap-of-yolov4-on-the-ms-coco-evaluation-server) - [How to evaluate FPS of YOLOv4 on GPU](#how-to-evaluate-fps-of-yolov4-on-gpu) - [Pre-trained models](#pre-trained-models) - - [Requirements](#requirements) + - [Requirements for Windows, Linux and macOS](#requirements-for-windows-linux-and-macos) - [Yolo v4 in other frameworks](#yolo-v4-in-other-frameworks) - [Datasets](#datasets) - [Improvements in this repository](#improvements-in-this-repository) - [How to use on the command line](#how-to-use-on-the-command-line) - [For using network video-camera mjpeg-stream with any Android smartphone](#for-using-network-video-camera-mjpeg-stream-with-any-android-smartphone) - [How to compile on Linux/macOS (using `CMake`)](#how-to-compile-on-linuxmacos-using-cmake) - - [Using `vcpkg`](#using-vcpkg) - - [Using libraries manually provided](#using-libraries-manually-provided) + - [Using also PowerShell](#using-also-powershell) - [How to compile on Linux (using `make`)](#how-to-compile-on-linux-using-make) - [How to compile on Windows (using `CMake`)](#how-to-compile-on-windows-using-cmake) - [How to compile on Windows (using `vcpkg`)](#how-to-compile-on-windows-using-vcpkg) - [How to train with multi-GPU](#how-to-train-with-multi-gpu) - [How to train (to detect your custom objects)](#how-to-train-to-detect-your-custom-objects) - - [How to train tiny-yolo (to detect your custom objects):](#how-to-train-tiny-yolo-to-detect-your-custom-objects) - - [When should I stop training:](#when-should-i-stop-training) - - [Custom object detection:](#custom-object-detection) - - [How to improve object detection:](#how-to-improve-object-detection) - - [How to mark bounded boxes of objects and create annotation files:](#how-to-mark-bounded-boxes-of-objects-and-create-annotation-files) + - [How to train tiny-yolo (to detect your custom objects)](#how-to-train-tiny-yolo-to-detect-your-custom-objects) + - [When should I stop training](#when-should-i-stop-training) + - [Custom object detection](#custom-object-detection) + - [How to improve object detection](#how-to-improve-object-detection) + - [How to mark bounded boxes of objects and create annotation files](#how-to-mark-bounded-boxes-of-objects-and-create-annotation-files) - [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) +![Darknet Logo](http://pjreddie.com/media/files/darknet-black-small.png) ![scaled_yolov4](https://user-images.githubusercontent.com/4096485/112776361-281d8380-9048-11eb-8083-8728b12dcd55.png) AP50:95 - FPS (Tesla V100) Paper: https://arxiv.org/abs/2011.08036 ---- -![modern_gpus](https://user-images.githubusercontent.com/4096485/82835867-f1c62380-9ecd-11ea-9134-1598ed2abc4b.png) AP50:95 / AP50 - FPS (Tesla V100) Paper: https://arxiv.org/abs/2004.10934 - +![modern_gpus](https://user-images.githubusercontent.com/4096485/82835867-f1c62380-9ecd-11ea-9134-1598ed2abc4b.png) AP50:95 / AP50 - FPS (Tesla V100) Paper: https://arxiv.org/abs/2004.10934 tkDNN-TensorRT accelerates YOLOv4 **~2x** times for batch=1 and **3x-4x** times for batch=4. -* tkDNN: https://github.com/ceccocats/tkDNN -* OpenCV: https://gist.github.com/YashasSamaga/48bdb167303e10f4d07b754888ddbdcf - -#### GeForce RTX 2080 Ti: -| Network Size | Darknet, FPS (avg)| tkDNN TensorRT FP32, FPS | tkDNN TensorRT FP16, FPS | OpenCV FP16, FPS | tkDNN TensorRT FP16 batch=4, FPS | OpenCV FP16 batch=4, FPS | tkDNN Speedup | -|:-----:|:--------:|--------:|--------:|--------:|--------:|--------:|------:| -|320 | 100 | 116 | **202** | 183 | 423 | **430** | **4.3x** | -|416 | 82 | 103 | **162** | 159 | 284 | **294** | **3.6x** | -|512 | 69 | 91 | 134 | **138** | 206 | **216** | **3.1x** | -|608 | 53 | 62 | 103 | **115**| 150 | **150** | **2.8x** | -|Tiny 416 | 443 | 609 | **790** | 773 | **1774** | 1353 | **3.5x** | -|Tiny 416 CPU Core i7 7700HQ | 3.4 | - | - | 42 | - | 39 | **12x** | - -* Yolo v4 Full comparison: [map_fps](https://user-images.githubusercontent.com/4096485/80283279-0e303e00-871f-11ea-814c-870967d77fd1.png) -* Yolo v4 tiny comparison: [tiny_fps](https://user-images.githubusercontent.com/4096485/85734112-6e366700-b705-11ea-95d1-fcba0de76d72.png) -* CSPNet: [paper](https://arxiv.org/abs/1911.11929) and [map_fps](https://user-images.githubusercontent.com/4096485/71702416-6645dc00-2de0-11ea-8d65-de7d4b604021.png) comparison: https://github.com/WongKinYiu/CrossStagePartialNetworks -* Yolo v3 on MS COCO: [Speed / Accuracy (mAP@0.5) chart](https://user-images.githubusercontent.com/4096485/52151356-e5d4a380-2683-11e9-9d7d-ac7bc192c477.jpg) -* Yolo v3 on MS COCO (Yolo v3 vs RetinaNet) - Figure 3: https://arxiv.org/pdf/1804.02767v1.pdf -* 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 + +- tkDNN: https://github.com/ceccocats/tkDNN +- OpenCV: https://gist.github.com/YashasSamaga/48bdb167303e10f4d07b754888ddbdcf + +### GeForce RTX 2080 Ti + +| Network Size | Darknet, FPS (avg) | tkDNN TensorRT FP32, FPS | tkDNN TensorRT FP16, FPS | OpenCV FP16, FPS | tkDNN TensorRT FP16 batch=4, FPS | OpenCV FP16 batch=4, FPS | tkDNN Speedup | +|:--------------------------:|:------------------:|-------------------------:|-------------------------:|-----------------:|---------------------------------:|-------------------------:|--------------:| +|320 | 100 | 116 | **202** | 183 | 423 | **430** | **4.3x** | +|416 | 82 | 103 | **162** | 159 | 284 | **294** | **3.6x** | +|512 | 69 | 91 | 134 | **138** | 206 | **216** | **3.1x** | +|608 | 53 | 62 | 103 | **115** | 150 | **150** | **2.8x** | +|Tiny 416 | 443 | 609 | **790** | 773 | **1774** | 1353 | **3.5x** | +|Tiny 416 CPU Core i7 7700HQ | 3.4 | - | - | 42 | - | 39 | **12x** | + +- Yolo v4 Full comparison: [map_fps](https://user-images.githubusercontent.com/4096485/80283279-0e303e00-871f-11ea-814c-870967d77fd1.png) +- Yolo v4 tiny comparison: [tiny_fps](https://user-images.githubusercontent.com/4096485/85734112-6e366700-b705-11ea-95d1-fcba0de76d72.png) +- CSPNet: [paper](https://arxiv.org/abs/1911.11929) and [map_fps](https://user-images.githubusercontent.com/4096485/71702416-6645dc00-2de0-11ea-8d65-de7d4b604021.png) comparison: https://github.com/WongKinYiu/CrossStagePartialNetworks +- Yolo v3 on MS COCO: [Speed / Accuracy (mAP@0.5) chart](https://user-images.githubusercontent.com/4096485/52151356-e5d4a380-2683-11e9-9d7d-ac7bc192c477.jpg) +- Yolo v3 on MS COCO (Yolo v3 vs RetinaNet) - Figure 3: https://arxiv.org/pdf/1804.02767v1.pdf +- 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 #### Youtube video of results @@ -132,9 +133,9 @@ eval=coco 3. Get any .avi/.mp4 video file (preferably not more than 1920x1080 to avoid bottlenecks in CPU performance) 4. Run one of two commands and look at the AVG FPS: -* include video_capturing + NMS + drawing_bboxes: +- include video_capturing + NMS + drawing_bboxes: `./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -dont_show -ext_output` -* exclude video_capturing + NMS + drawing_bboxes: +- exclude video_capturing + NMS + drawing_bboxes: `./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -benchmark` #### Pre-trained models @@ -143,52 +144,52 @@ There are weights-file for different cfg-files (trained for MS COCO dataset): FPS on RTX 2070 (R) and Tesla V100 (V): -* [yolov4x-mish.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4x-mish.cfg) - 640x640 - **67.9% mAP@0.5 (49.4% 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 +- [yolov4x-mish.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4x-mish.cfg) - 640x640 - **67.9% mAP@0.5 (49.4% 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 -* [yolov4-csp.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-csp.cfg) - 202 MB: [yolov4-csp.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp.weights) paper [Scaled Yolo v4](https://arxiv.org/abs/2011.08036) +- [yolov4-csp.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-csp.cfg) - 202 MB: [yolov4-csp.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp.weights) paper [Scaled Yolo v4](https://arxiv.org/abs/2011.08036) just change `width=` and `height=` parameters in `yolov4-csp.cfg` file and use the same `yolov4-csp.weights` file for all cases: - * `width=640 height=640` in cfg: **66.2% mAP@0.5 (47.5% AP@0.5:0.95) - 70(V) FPS** - 120 (60 FMA) BFlops - * `width=512 height=512` in cfg: **64.8% mAP@0.5 (46.2% AP@0.5:0.95) - 93(V) FPS** - 77 (39 FMA) BFlops - * pre-trained weights for training: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp.conv.142 - -* [yolov4.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg) - 245 MB: [yolov4.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights) (Google-drive mirror [yolov4.weights](https://drive.google.com/open?id=1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT) ) paper [Yolo v4](https://arxiv.org/abs/2004.10934) + - `width=640 height=640` in cfg: **66.2% mAP@0.5 (47.5% AP@0.5:0.95) - 70(V) FPS** - 120 (60 FMA) BFlops + - `width=512 height=512` in cfg: **64.8% mAP@0.5 (46.2% AP@0.5:0.95) - 93(V) FPS** - 77 (39 FMA) BFlops + - pre-trained weights for training: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-csp.conv.142 + +- [yolov4.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg) - 245 MB: [yolov4.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights) (Google-drive mirror [yolov4.weights](https://drive.google.com/open?id=1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT) ) paper [Yolo v4](https://arxiv.org/abs/2004.10934) just change `width=` and `height=` parameters in `yolov4.cfg` file and use the same `yolov4.weights` file for all cases: - * `width=608 height=608` in cfg: **65.7% mAP@0.5 (43.5% AP@0.5:0.95) - 34(R) FPS / 62(V) FPS** - 128.5 BFlops - * `width=512 height=512` in cfg: **64.9% mAP@0.5 (43.0% AP@0.5:0.95) - 45(R) FPS / 83(V) FPS** - 91.1 BFlops - * `width=416 height=416` in cfg: **62.8% mAP@0.5 (41.2% AP@0.5:0.95) - 55(R) FPS / 96(V) FPS** - 60.1 BFlops - * `width=320 height=320` in cfg: **60% mAP@0.5 ( 38% AP@0.5:0.95) - 63(R) FPS / 123(V) FPS** - 35.5 BFlops + - `width=608 height=608` in cfg: **65.7% mAP@0.5 (43.5% AP@0.5:0.95) - 34(R) FPS / 62(V) FPS** - 128.5 BFlops + - `width=512 height=512` in cfg: **64.9% mAP@0.5 (43.0% AP@0.5:0.95) - 45(R) FPS / 83(V) FPS** - 91.1 BFlops + - `width=416 height=416` in cfg: **62.8% mAP@0.5 (41.2% AP@0.5:0.95) - 55(R) FPS / 96(V) FPS** - 60.1 BFlops + - `width=320 height=320` in cfg: **60% mAP@0.5 ( 38% AP@0.5:0.95) - 63(R) FPS / 123(V) FPS** - 35.5 BFlops -* [yolov4-tiny.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny.cfg) - **40.2% mAP@0.5 - 371(1080Ti) FPS / 330(RTX2070) FPS** - 6.9 BFlops - 23.1 MB: [yolov4-tiny.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights) +- [yolov4-tiny.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny.cfg) - **40.2% mAP@0.5 - 371(1080Ti) FPS / 330(RTX2070) FPS** - 6.9 BFlops - 23.1 MB: [yolov4-tiny.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights) -* [enet-coco.cfg (EfficientNetB0-Yolov3)](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/enet-coco.cfg) - **45.5% mAP@0.5 - 55(R) FPS** - 3.7 BFlops - 18.3 MB: [enetb0-coco_final.weights](https://drive.google.com/file/d/1FlHeQjWEQVJt0ay1PVsiuuMzmtNyv36m/view) +- [enet-coco.cfg (EfficientNetB0-Yolov3)](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/enet-coco.cfg) - **45.5% mAP@0.5 - 55(R) FPS** - 3.7 BFlops - 18.3 MB: [enetb0-coco_final.weights](https://drive.google.com/file/d/1FlHeQjWEQVJt0ay1PVsiuuMzmtNyv36m/view) -* [yolov3-openimages.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-openimages.cfg) - 247 MB - 18(R) FPS - OpenImages dataset: [yolov3-openimages.weights](https://pjreddie.com/media/files/yolov3-openimages.weights) +- [yolov3-openimages.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-openimages.cfg) - 247 MB - 18(R) FPS - OpenImages dataset: [yolov3-openimages.weights](https://pjreddie.com/media/files/yolov3-openimages.weights)
CLICK ME - Yolo v3 models -* [csresnext50-panet-spp-original-optimal.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/csresnext50-panet-spp-original-optimal.cfg) - **65.4% mAP@0.5 (43.2% AP@0.5:0.95) - 32(R) FPS** - 100.5 BFlops - 217 MB: [csresnext50-panet-spp-original-optimal_final.weights](https://drive.google.com/open?id=1_NnfVgj0EDtb_WLNoXV8Mo7WKgwdYZCc) +- [csresnext50-panet-spp-original-optimal.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/csresnext50-panet-spp-original-optimal.cfg) - **65.4% mAP@0.5 (43.2% AP@0.5:0.95) - 32(R) FPS** - 100.5 BFlops - 217 MB: [csresnext50-panet-spp-original-optimal_final.weights](https://drive.google.com/open?id=1_NnfVgj0EDtb_WLNoXV8Mo7WKgwdYZCc) -* [yolov3-spp.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-spp.cfg) - **60.6% mAP@0.5 - 38(R) FPS** - 141.5 BFlops - 240 MB: [yolov3-spp.weights](https://pjreddie.com/media/files/yolov3-spp.weights) +- [yolov3-spp.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-spp.cfg) - **60.6% mAP@0.5 - 38(R) FPS** - 141.5 BFlops - 240 MB: [yolov3-spp.weights](https://pjreddie.com/media/files/yolov3-spp.weights) -* [csresnext50-panet-spp.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/csresnext50-panet-spp.cfg) - **60.0% mAP@0.5 - 44 FPS** - 71.3 BFlops - 217 MB: [csresnext50-panet-spp_final.weights](https://drive.google.com/file/d/1aNXdM8qVy11nqTcd2oaVB3mf7ckr258-/view?usp=sharing) +- [csresnext50-panet-spp.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/csresnext50-panet-spp.cfg) - **60.0% mAP@0.5 - 44 FPS** - 71.3 BFlops - 217 MB: [csresnext50-panet-spp_final.weights](https://drive.google.com/file/d/1aNXdM8qVy11nqTcd2oaVB3mf7ckr258-/view?usp=sharing) -* [yolov3.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3.cfg) - **55.3% mAP@0.5 - 66(R) FPS** - 65.9 BFlops - 236 MB: [yolov3.weights](https://pjreddie.com/media/files/yolov3.weights) +- [yolov3.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3.cfg) - **55.3% mAP@0.5 - 66(R) FPS** - 65.9 BFlops - 236 MB: [yolov3.weights](https://pjreddie.com/media/files/yolov3.weights) -* [yolov3-tiny.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-tiny.cfg) - **33.1% mAP@0.5 - 345(R) FPS** - 5.6 BFlops - 33.7 MB: [yolov3-tiny.weights](https://pjreddie.com/media/files/yolov3-tiny.weights) +- [yolov3-tiny.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-tiny.cfg) - **33.1% mAP@0.5 - 345(R) FPS** - 5.6 BFlops - 33.7 MB: [yolov3-tiny.weights](https://pjreddie.com/media/files/yolov3-tiny.weights) -* [yolov3-tiny-prn.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-tiny-prn.cfg) - **33.1% mAP@0.5 - 370(R) FPS** - 3.5 BFlops - 18.8 MB: [yolov3-tiny-prn.weights](https://drive.google.com/file/d/18yYZWyKbo4XSDVyztmsEcF9B_6bxrhUY/view?usp=sharing) +- [yolov3-tiny-prn.cfg](https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-tiny-prn.cfg) - **33.1% mAP@0.5 - 370(R) FPS** - 3.5 BFlops - 18.8 MB: [yolov3-tiny-prn.weights](https://drive.google.com/file/d/18yYZWyKbo4XSDVyztmsEcF9B_6bxrhUY/view?usp=sharing)
CLICK ME - Yolo v2 models -* `yolov2.cfg` (194 MB COCO Yolo v2) - requires 4 GB GPU-RAM: https://pjreddie.com/media/files/yolov2.weights -* `yolo-voc.cfg` (194 MB VOC Yolo v2) - requires 4 GB GPU-RAM: http://pjreddie.com/media/files/yolo-voc.weights -* `yolov2-tiny.cfg` (43 MB COCO Yolo v2) - requires 1 GB GPU-RAM: https://pjreddie.com/media/files/yolov2-tiny.weights -* `yolov2-tiny-voc.cfg` (60 MB VOC Yolo v2) - requires 1 GB GPU-RAM: http://pjreddie.com/media/files/yolov2-tiny-voc.weights -* `yolo9000.cfg` (186 MB Yolo9000-model) - requires 4 GB GPU-RAM: http://pjreddie.com/media/files/yolo9000.weights +- `yolov2.cfg` (194 MB COCO Yolo v2) - requires 4 GB GPU-RAM: https://pjreddie.com/media/files/yolov2.weights +- `yolo-voc.cfg` (194 MB VOC Yolo v2) - requires 4 GB GPU-RAM: http://pjreddie.com/media/files/yolo-voc.weights +- `yolov2-tiny.cfg` (43 MB COCO Yolo v2) - requires 1 GB GPU-RAM: https://pjreddie.com/media/files/yolov2-tiny.weights +- `yolov2-tiny-voc.cfg` (60 MB VOC Yolo v2) - requires 1 GB GPU-RAM: http://pjreddie.com/media/files/yolov2-tiny-voc.weights +- `yolo9000.cfg` (186 MB Yolo9000-model) - requires 4 GB GPU-RAM: http://pjreddie.com/media/files/yolo9000.weights
@@ -196,76 +197,77 @@ Put it near compiled: darknet.exe You can get cfg-files by path: `darknet/cfg/` -### Requirements +### Requirements for Windows, Linux and macOS -* **CMake >= 3.18**: https://cmake.org/download/ -* **Powershell** (already installed on windows): https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell -* **CUDA >= 10.2**: 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 >= 2.4**: use your preferred package manager (brew, apt), build from source using [vcpkg](https://github.com/Microsoft/vcpkg) or download from [OpenCV official site](https://opencv.org/releases.html) (on Windows set system variable `OpenCV_DIR` = `C:\opencv\build` - where are the `include` and `x64` folders [image](https://user-images.githubusercontent.com/4096485/53249516-5130f480-36c9-11e9-8238-a6e82e48c6f2.png)) -* **cuDNN >= 8.0.2** https://developer.nvidia.com/rdp/cudnn-archive (on **Linux** copy `cudnn.h`,`libcudnn.so`... as described here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installlinux-tar , on **Windows** copy `cudnn.h`,`cudnn64_7.dll`, `cudnn64_7.lib` as described here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installwindows ) -* **GPU with CC >= 3.0**: https://en.wikipedia.org/wiki/CUDA#GPUs_supported +- **CMake >= 3.18**: https://cmake.org/download/ +- **Powershell** (already installed on windows): https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell +- **CUDA >= 10.2**: 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 >= 2.4**: use your preferred package manager (brew, apt), build from source using [vcpkg](https://github.com/Microsoft/vcpkg) or download from [OpenCV official site](https://opencv.org/releases.html) (on Windows set system variable `OpenCV_DIR` = `C:\opencv\build` - where are the `include` and `x64` folders [image](https://user-images.githubusercontent.com/4096485/53249516-5130f480-36c9-11e9-8238-a6e82e48c6f2.png)) +- **cuDNN >= 8.0.2** https://developer.nvidia.com/rdp/cudnn-archive (on **Linux** copy `cudnn.h`,`libcudnn.so`... as described here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installlinux-tar , on **Windows** copy `cudnn.h`,`cudnn64_7.dll`, `cudnn64_7.lib` as described here https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#installwindows ) +- **GPU with CC >= 3.0**: https://en.wikipedia.org/wiki/CUDA#GPUs_supported ### Yolo v4 in other frameworks -* **Pytorch - Scaled-YOLOv4:** https://github.com/WongKinYiu/ScaledYOLOv4 -* **TensorFlow:** `pip install yolov4` YOLOv4 on TensorFlow 2.0 / TFlite / Android: https://github.com/hunglc007/tensorflow-yolov4-tflite +- **Pytorch - Scaled-YOLOv4:** https://github.com/WongKinYiu/ScaledYOLOv4 +- **TensorFlow:** `pip install yolov4` YOLOv4 on TensorFlow 2.0 / TFlite / Android: https://github.com/hunglc007/tensorflow-yolov4-tflite Official TF models: https://github.com/tensorflow/models/tree/master/official/vision/beta/projects/yolo For YOLOv4 - convert `yolov4.weights`/`cfg` files to `yolov4.pb` by using [TNTWEN](https://github.com/TNTWEN/OpenVINO-YOLOV4) project, and to `yolov4.tflite` [TensorFlow-lite](https://www.tensorflow.org/lite/guide/get_started#2_convert_the_model_format) -* **OpenCV** the fastest implementation of YOLOv4 for CPU (x86/ARM-Android), OpenCV can be compiled with [OpenVINO-backend](https://github.com/opencv/opencv/wiki/Intel's-Deep-Learning-Inference-Engine-backend) for running on (Myriad X / USB Neural Compute Stick / Arria FPGA), use `yolov4.weights`/`cfg` with: [C++ example](https://github.com/opencv/opencv/blob/8c25a8eb7b10fb50cda323ee6bec68aa1a9ce43c/samples/dnn/object_detection.cpp#L192-L221) or [Python example](https://github.com/opencv/opencv/blob/8c25a8eb7b10fb50cda323ee6bec68aa1a9ce43c/samples/dnn/object_detection.py#L129-L150) -* **Intel OpenVINO 2021.2:** supports YOLOv4 (NPU Myriad X / USB Neural Compute Stick / Arria FPGA): https://devmesh.intel.com/projects/openvino-yolov4-49c756 read this [manual](https://github.com/TNTWEN/OpenVINO-YOLOV4) (old [manual](https://software.intel.com/en-us/articles/OpenVINO-Using-TensorFlow#converting-a-darknet-yolo-model) ) (for [Scaled-YOLOv4](https://github.com/WongKinYiu/ScaledYOLOv4/tree/yolov4-large) models use https://github.com/Chen-MingChang/pytorch_YOLO_OpenVINO_demo ) -* **PyTorch > ONNX**: - * [WongKinYiu/PyTorch_YOLOv4](https://github.com/WongKinYiu/PyTorch_YOLOv4) - * [maudzung/3D-YOLOv4](https://github.com/maudzung/Complex-YOLOv4-Pytorch) - * [Tianxiaomo/pytorch-YOLOv4](https://github.com/Tianxiaomo/pytorch-YOLOv4) - * [YOLOv5](https://github.com/ultralytics/yolov5) -* **ONNX** on Jetson for YOLOv4: https://developer.nvidia.com/blog/announcing-onnx-runtime-for-jetson/ -* **TensorRT+tkDNN**: https://github.com/ceccocats/tkDNN#fps-results -* **Deepstream 5.0 / TensorRT for YOLOv4** https://github.com/NVIDIA-AI-IOT/yolov4_deepstream or https://github.com/marcoslucianops/DeepStream-Yolo read [Yolo is natively supported in DeepStream 4.0](https://news.developer.nvidia.com/deepstream-sdk-4-now-available/) and [PDF](https://docs.nvidia.com/metropolis/deepstream/Custom_YOLO_Model_in_the_DeepStream_YOLO_App.pdf). Additionally [jkjung-avt/tensorrt_demos](https://github.com/jkjung-avt/tensorrt_demos) or [wang-xinyu/tensorrtx](https://github.com/wang-xinyu/tensorrtx) -* **Triton Inference Server / TensorRT** https://github.com/isarsoft/yolov4-triton-tensorrt -* **DirectML** https://github.com/microsoft/DirectML/tree/master/Samples/yolov4 -* **OpenCL** (Intel, AMD, Mali GPUs for macOS & GNU/Linux) https://github.com/sowson/darknet -* **HIP** for Training and Detection on AMD GPU https://github.com/os-hackathon/darknet -* **ROS** (Robot Operating System) https://github.com/engcang/ros-yolo-sort -* **Xilinx Zynq Ultrascale+ Deep Learning Processor (DPU) ZCU102/ZCU104:** https://github.com/Xilinx/Vitis-In-Depth-Tutorial/tree/master/Machine_Learning/Design_Tutorials/07-yolov4-tutorial -* **Amazon Neurochip / Amazon EC2 Inf1 instances** 1.85 times higher throughput and 37% lower cost per image for TensorFlow based YOLOv4 model, using Keras [URL](https://aws.amazon.com/ru/blogs/machine-learning/improving-performance-for-deep-learning-based-object-detection-with-an-aws-neuron-compiled-yolov4-model-on-aws-inferentia/) -* **TVM** - compilation of deep learning models (Keras, MXNet, PyTorch, Tensorflow, CoreML, DarkNet) into minimum deployable modules on diverse hardware backend (CPUs, GPUs, FPGA, and specialized accelerators): https://tvm.ai/about -* **Tencent/ncnn:** the fastest inference of YOLOv4 on mobile phone CPU: https://github.com/Tencent/ncnn -* **OpenDataCam** - It detects, tracks and counts moving objects by using YOLOv4: https://github.com/opendatacam/opendatacam#-hardware-pre-requisite -* **Netron** - Visualizer for neural networks: https://github.com/lutzroeder/netron +- **OpenCV** the fastest implementation of YOLOv4 for CPU (x86/ARM-Android), OpenCV can be compiled with [OpenVINO-backend](https://github.com/opencv/opencv/wiki/Intel's-Deep-Learning-Inference-Engine-backend) for running on (Myriad X / USB Neural Compute Stick / Arria FPGA), use `yolov4.weights`/`cfg` with: [C++ example](https://github.com/opencv/opencv/blob/8c25a8eb7b10fb50cda323ee6bec68aa1a9ce43c/samples/dnn/object_detection.cpp#L192-L221) or [Python example](https://github.com/opencv/opencv/blob/8c25a8eb7b10fb50cda323ee6bec68aa1a9ce43c/samples/dnn/object_detection.py#L129-L150) +- **Intel OpenVINO 2021.2:** supports YOLOv4 (NPU Myriad X / USB Neural Compute Stick / Arria FPGA): https://devmesh.intel.com/projects/openvino-yolov4-49c756 read this [manual](https://github.com/TNTWEN/OpenVINO-YOLOV4) (old [manual](https://software.intel.com/en-us/articles/OpenVINO-Using-TensorFlow#converting-a-darknet-yolo-model) ) (for [Scaled-YOLOv4](https://github.com/WongKinYiu/ScaledYOLOv4/tree/yolov4-large) models use https://github.com/Chen-MingChang/pytorch_YOLO_OpenVINO_demo ) +- **PyTorch > ONNX**: + - [WongKinYiu/PyTorch_YOLOv4](https://github.com/WongKinYiu/PyTorch_YOLOv4) + - [maudzung/3D-YOLOv4](https://github.com/maudzung/Complex-YOLOv4-Pytorch) + - [Tianxiaomo/pytorch-YOLOv4](https://github.com/Tianxiaomo/pytorch-YOLOv4) + - [YOLOv5](https://github.com/ultralytics/yolov5) +- **ONNX** on Jetson for YOLOv4: https://developer.nvidia.com/blog/announcing-onnx-runtime-for-jetson/ +- **nVidia Transfer Learning Toolkit (TLT>=3.0)** Training and Detection https://docs.nvidia.com/metropolis/TLT/tlt-user-guide/text/object_detection/yolo_v4.html +- **TensorRT+tkDNN**: https://github.com/ceccocats/tkDNN#fps-results +- **Deepstream 5.0 / TensorRT for YOLOv4** https://github.com/NVIDIA-AI-IOT/yolov4_deepstream or https://github.com/marcoslucianops/DeepStream-Yolo read [Yolo is natively supported in DeepStream 4.0](https://news.developer.nvidia.com/deepstream-sdk-4-now-available/) and [PDF](https://docs.nvidia.com/metropolis/deepstream/Custom_YOLO_Model_in_the_DeepStream_YOLO_App.pdf). Additionally [jkjung-avt/tensorrt_demos](https://github.com/jkjung-avt/tensorrt_demos) or [wang-xinyu/tensorrtx](https://github.com/wang-xinyu/tensorrtx) +- **Triton Inference Server / TensorRT** https://github.com/isarsoft/yolov4-triton-tensorrt +- **DirectML** https://github.com/microsoft/DirectML/tree/master/Samples/yolov4 +- **OpenCL** (Intel, AMD, Mali GPUs for macOS & GNU/Linux) https://github.com/sowson/darknet +- **HIP** for Training and Detection on AMD GPU https://github.com/os-hackathon/darknet +- **ROS** (Robot Operating System) https://github.com/engcang/ros-yolo-sort +- **Xilinx Zynq Ultrascale+ Deep Learning Processor (DPU) ZCU102/ZCU104:** https://github.com/Xilinx/Vitis-In-Depth-Tutorial/tree/master/Machine_Learning/Design_Tutorials/07-yolov4-tutorial +- **Amazon Neurochip / Amazon EC2 Inf1 instances** 1.85 times higher throughput and 37% lower cost per image for TensorFlow based YOLOv4 model, using Keras [URL](https://aws.amazon.com/ru/blogs/machine-learning/improving-performance-for-deep-learning-based-object-detection-with-an-aws-neuron-compiled-yolov4-model-on-aws-inferentia/) +- **TVM** - compilation of deep learning models (Keras, MXNet, PyTorch, Tensorflow, CoreML, DarkNet) into minimum deployable modules on diverse hardware backend (CPUs, GPUs, FPGA, and specialized accelerators): https://tvm.ai/about +- **Tencent/ncnn:** the fastest inference of YOLOv4 on mobile phone CPU: https://github.com/Tencent/ncnn +- **OpenDataCam** - It detects, tracks and counts moving objects by using YOLOv4: https://github.com/opendatacam/opendatacam#-hardware-pre-requisite +- **Netron** - Visualizer for neural networks: https://github.com/lutzroeder/netron #### Datasets -* MS COCO: use `./scripts/get_coco_dataset.sh` to get labeled MS COCO detection dataset -* OpenImages: use `python ./scripts/get_openimages_dataset.py` for labeling train detection dataset -* Pascal VOC: use `python ./scripts/voc_label.py` for labeling Train/Test/Val detection datasets -* ILSVRC2012 (ImageNet classification): use `./scripts/get_imagenet_train.sh` (also `imagenet_label.sh` for labeling valid set) -* German/Belgium/Russian/LISA/MASTIF Traffic Sign Datasets for Detection - use this parsers: https://github.com/angeligareta/Datasets2Darknet#detection-task -* List of other datasets: https://github.com/AlexeyAB/darknet/tree/master/scripts#datasets +- MS COCO: use `./scripts/get_coco_dataset.sh` to get labeled MS COCO detection dataset +- OpenImages: use `python ./scripts/get_openimages_dataset.py` for labeling train detection dataset +- Pascal VOC: use `python ./scripts/voc_label.py` for labeling Train/Test/Val detection datasets +- ILSVRC2012 (ImageNet classification): use `./scripts/get_imagenet_train.sh` (also `imagenet_label.sh` for labeling valid set) +- German/Belgium/Russian/LISA/MASTIF Traffic Sign Datasets for Detection - use this parsers: https://github.com/angeligareta/Datasets2Darknet#detection-task +- List of other datasets: https://github.com/AlexeyAB/darknet/tree/master/scripts#datasets ### Improvements in this repository -* developed State-of-the-Art object detector YOLOv4 -* added State-of-Art models: CSP, PRN, EfficientNet -* added layers: [conv_lstm], [scale_channels] SE/ASFF/BiFPN, [local_avgpool], [sam], [Gaussian_yolo], [reorg3d] (fixed [reorg]), fixed [batchnorm] -* added the ability for training recurrent models (with layers conv-lstm`[conv_lstm]`/conv-rnn`[crnn]`) for accurate detection on video -* added data augmentation: `[net] mixup=1 cutmix=1 mosaic=1 blur=1`. Added activations: SWISH, MISH, NORM_CHAN, NORM_CHAN_SOFTMAX -* added the ability for training with GPU-processing using CPU-RAM to increase the mini_batch_size and increase accuracy (instead of batch-norm sync) -* improved binary neural network performance **2x-4x times** for Detection on CPU and GPU if you trained your own weights by using this XNOR-net model (bit-1 inference) : https://github.com/AlexeyAB/darknet/blob/master/cfg/yolov3-tiny_xnor.cfg -* improved neural network performance **~7%** by fusing 2 layers into 1: Convolutional + Batch-norm -* improved performance: Detection **2x times**, on GPU Volta/Turing (Tesla V100, GeForce RTX, ...) using Tensor Cores if `CUDNN_HALF` defined in the `Makefile` or `darknet.sln` -* improved performance **~1.2x** times on FullHD, **~2x** times on 4K, for detection on the video (file/stream) using `darknet detector demo`... -* improved performance **3.5 X times** of data augmentation for training (using OpenCV SSE/AVX functions instead of hand-written functions) - removes bottleneck for training on multi-GPU or GPU Volta -* improved performance of detection and training on Intel CPU with AVX (Yolo v3 **~85%**) -* optimized memory allocation during network resizing when `random=1` -* optimized GPU initialization for detection - we use batch=1 initially instead of re-init with batch=1 -* added correct calculation of **mAP, F1, IoU, Precision-Recall** using command `darknet detector map`... -* added drawing of chart of average-Loss and accuracy-mAP (`-map` flag) during training -* run `./darknet detector demo ... -json_port 8070 -mjpeg_port 8090` as JSON and MJPEG server to get results online over the network by using your soft or Web-browser -* added calculation of anchors for training -* added example of Detection and Tracking objects: https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp -* run-time tips and warnings if you use incorrect cfg-file or dataset -* added support for Windows -* many other fixes of code... +- developed State-of-the-Art object detector YOLOv4 +- added State-of-Art models: CSP, PRN, EfficientNet +- added layers: [conv_lstm], [scale_channels] SE/ASFF/BiFPN, [local_avgpool], [sam], [Gaussian_yolo], [reorg3d] (fixed [reorg]), fixed [batchnorm] +- added the ability for training recurrent models (with layers conv-lstm`[conv_lstm]`/conv-rnn`[crnn]`) for accurate detection on video +- added data augmentation: `[net] mixup=1 cutmix=1 mosaic=1 blur=1`. Added activations: SWISH, MISH, NORM_CHAN, NORM_CHAN_SOFTMAX +- added the ability for training with GPU-processing using CPU-RAM to increase the mini_batch_size and increase accuracy (instead of batch-norm sync) +- improved binary neural network performance **2x-4x times** for Detection on CPU and GPU if you trained your own weights by using this XNOR-net model (bit-1 inference) : https://github.com/AlexeyAB/darknet/blob/master/cfg/yolov3-tiny_xnor.cfg +- improved neural network performance **~7%** by fusing 2 layers into 1: Convolutional + Batch-norm +- improved performance: Detection **2x times**, on GPU Volta/Turing (Tesla V100, GeForce RTX, ...) using Tensor Cores if `CUDNN_HALF` defined in the `Makefile` or `darknet.sln` +- improved performance **~1.2x** times on FullHD, **~2x** times on 4K, for detection on the video (file/stream) using `darknet detector demo`... +- improved performance **3.5 X times** of data augmentation for training (using OpenCV SSE/AVX functions instead of hand-written functions) - removes bottleneck for training on multi-GPU or GPU Volta +- improved performance of detection and training on Intel CPU with AVX (Yolo v3 **~85%**) +- optimized memory allocation during network resizing when `random=1` +- optimized GPU initialization for detection - we use batch=1 initially instead of re-init with batch=1 +- added correct calculation of **mAP, F1, IoU, Precision-Recall** using command `darknet detector map`... +- added drawing of chart of average-Loss and accuracy-mAP (`-map` flag) during training +- run `./darknet detector demo ... -json_port 8070 -mjpeg_port 8090` as JSON and MJPEG server to get results online over the network by using your soft or Web-browser +- added calculation of anchors for training +- added example of Detection and Tracking objects: https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp +- run-time tips and warnings if you use incorrect cfg-file or dataset +- added support for Windows +- many other fixes of code... And added manual - [How to train Yolo v4-v2 (to detect your custom objects)](#how-to-train-to-detect-your-custom-objects) @@ -275,77 +277,78 @@ Also, you might be interested in using a simplified repository where is implemen On Linux use `./darknet` instead of `darknet.exe`, like this:`./darknet detector test ./cfg/coco.data ./cfg/yolov4.cfg ./yolov4.weights` -On Linux find executable file `./darknet` in the root directory, while on Windows find it in the directory `\build\darknet\x64` - -* Yolo v4 COCO - **image**: `darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -thresh 0.25` -* **Output coordinates** of objects: `darknet.exe detector test cfg/coco.data yolov4.cfg yolov4.weights -ext_output dog.jpg` -* Yolo v4 COCO - **video**: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output test.mp4` -* Yolo v4 COCO - **WebCam 0**: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -c 0` -* Yolo v4 COCO for **net-videocam** - Smart WebCam: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights http://192.168.0.80:8080/video?dummy=param.mjpg` -* Yolo v4 - **save result videofile res.avi**: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -out_filename res.avi` -* Yolo v3 **Tiny** COCO - video: `darknet.exe detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights test.mp4` -* **JSON and MJPEG server** that allows multiple connections from your soft or Web-browser `ip-address:8070` and 8090: `./darknet detector demo ./cfg/coco.data ./cfg/yolov3.cfg ./yolov3.weights test50.mp4 -json_port 8070 -mjpeg_port 8090 -ext_output` -* Yolo v3 Tiny **on GPU #1**: `darknet.exe detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights -i 1 test.mp4` -* Alternative method Yolo v3 COCO - image: `darknet.exe detect cfg/yolov4.cfg yolov4.weights -i 0 -thresh 0.25` -* Train on **Amazon EC2**, to see mAP & Loss-chart using URL like: `http://ec2-35-160-228-91.us-west-2.compute.amazonaws.com:8090` in the Chrome/Firefox (**Darknet should be compiled with OpenCV**): +On Linux find executable file `./darknet` in the root directory, while on Windows find it in the directory `\build\darknet\x64` + +- Yolo v4 COCO - **image**: `darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -thresh 0.25` +- **Output coordinates** of objects: `darknet.exe detector test cfg/coco.data yolov4.cfg yolov4.weights -ext_output dog.jpg` +- Yolo v4 COCO - **video**: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output test.mp4` +- Yolo v4 COCO - **WebCam 0**: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -c 0` +- Yolo v4 COCO for **net-videocam** - Smart WebCam: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights http://192.168.0.80:8080/video?dummy=param.mjpg` +- Yolo v4 - **save result videofile res.avi**: `darknet.exe detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights test.mp4 -out_filename res.avi` +- Yolo v3 **Tiny** COCO - video: `darknet.exe detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights test.mp4` +- **JSON and MJPEG server** that allows multiple connections from your soft or Web-browser `ip-address:8070` and 8090: `./darknet detector demo ./cfg/coco.data ./cfg/yolov3.cfg ./yolov3.weights test50.mp4 -json_port 8070 -mjpeg_port 8090 -ext_output` +- Yolo v3 Tiny **on GPU #1**: `darknet.exe detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights -i 1 test.mp4` +- Alternative method Yolo v3 COCO - image: `darknet.exe detect cfg/yolov4.cfg yolov4.weights -i 0 -thresh 0.25` +- Train on **Amazon EC2**, to see mAP & Loss-chart using URL like: `http://ec2-35-160-228-91.us-west-2.compute.amazonaws.com:8090` in the Chrome/Firefox (**Darknet should be compiled with OpenCV**): `./darknet detector train cfg/coco.data yolov4.cfg yolov4.conv.137 -dont_show -mjpeg_port 8090 -map` -* 186 MB Yolo9000 - image: `darknet.exe detector test cfg/combine9k.data cfg/yolo9000.cfg yolo9000.weights` -* Remember to put data/9k.tree and data/coco9k.map under the same folder of your app if you use the cpp api to build an app -* To process a list of images `data/train.txt` and save results of detection to `result.json` file use: +- 186 MB Yolo9000 - image: `darknet.exe detector test cfg/combine9k.data cfg/yolo9000.cfg yolo9000.weights` +- Remember to put data/9k.tree and data/coco9k.map under the same folder of your app if you use the cpp api to build an app +- To process a list of images `data/train.txt` and save results of detection to `result.json` file use: `darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < data/train.txt` -* To process a list of images `data/train.txt` and save results of detection to `result.txt` use: +- To process a list of images `data/train.txt` and save results of detection to `result.txt` use: `darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show -ext_output < data/train.txt > result.txt` -* Pseudo-labelling - to process a list of images `data/new_train.txt` and save results of detection in Yolo training format for each image as label `.txt` (in this way you can increase the amount of training data) use: +- Pseudo-labelling - to process a list of images `data/new_train.txt` and save results of detection in Yolo training format for each image as label `.txt` (in this way you can increase the amount of training data) use: `darknet.exe detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -thresh 0.25 -dont_show -save_labels < data/new_train.txt` -* To calculate anchors: `darknet.exe detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416` -* 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` +- To calculate anchors: `darknet.exe detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416` +- 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 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 + - 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 2. Connect your Android phone to computer by WiFi (through a WiFi-router) or USB 3. Start Smart WebCam on your phone 4. Replace the address below, on shown in the phone application (Smart WebCam) and launch: -* Yolo v4 COCO-model: `darknet.exe detector demo data/coco.data yolov4.cfg yolov4.weights http://192.168.0.80:8080/video?dummy=param.mjpg -i 0` +- Yolo v4 COCO-model: `darknet.exe detector demo data/coco.data yolov4.cfg yolov4.weights http://192.168.0.80:8080/video?dummy=param.mjpg -i 0` ### How to compile on Linux/macOS (using `CMake`) The `CMakeLists.txt` will attempt to find installed optional dependencies like CUDA, cudnn, ZED and build against those. It will also create a shared object library file to use `darknet` for code development. -Install powershell if you do not already have it ([guide here](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell)). +To update CMake on Ubuntu, it's better to follow guide here: https://apt.kitware.com/ or https://cmake.org/download/ -To update CMake on Ubuntu, it's better to follow guide here: https://apt.kitware.com/ - -### Using `vcpkg` - -Open a shell and type these commands - -```PowerShell -PS Code/> git clone https://github.com/AlexeyAB/darknet -PS Code/> cd darknet -PS Code/darknet> ./build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN +```bash +git clone https://github.com/AlexeyAB/darknet +cd darknet +mkdir build_release +cd build_release +cmake .. +cmake --build . --target install --parallel 8 ``` -(add option `-EnableOPENCV_CUDA` if you want to build OpenCV with CUDA support - very slow to build!) -If you open the `build.ps1` script at the beginning you will find all available switches. +### Using also PowerShell -### Using libraries manually provided +Install: `Cmake`, `CUDA`, `cuDNN` [How to install dependencies](#requirements) -Open a shell and type these commands +Install powershell for your OS (Linux or MacOS) ([guide here](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell)). + +Open PowerShell type these commands ```PowerShell -PS Code/> git clone https://github.com/AlexeyAB/darknet -PS Code/> cd darknet -PS Code/darknet> ./build.ps1 -EnableOPENCV -EnableCUDA -EnableCUDNN +git clone https://github.com/AlexeyAB/darknet +cd darknet +./build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN ``` -(remove options like `-EnableCUDA` or `-EnableCUDNN` if you are not interested into). +- remove options like `-EnableCUDA` or `-EnableCUDNN` if you are not interested into +- remove option `-UseVCPKG` if you plan to manually provide OpenCV library to darknet or if you do not want to enable OpenCV integration +- add option `-EnableOPENCV_CUDA` if you want to build OpenCV with CUDA support - very slow to build! (requires `-UseVCPKG`) + If you open the `build.ps1` script at the beginning you will find all available switches. ### How to compile on Linux (using `make`) @@ -353,17 +356,17 @@ If you open the `build.ps1` script at the beginning you will find all available Just do `make` in the darknet directory. (You can try to compile and run it on Google Colab in cloud [link](https://colab.research.google.com/drive/12QusaaRj_lUwCGDvQNfICpa7kA7_a2dE) (press «Open in Playground» button at the top-left corner) and watch the video [link](https://www.youtube.com/watch?v=mKAEGSxwOAY) ) 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 -* `OPENCV=1` to build with OpenCV 4.x/3.x/2.4.x - allows to detect on video files and video streams from network cameras or web-cams -* `DEBUG=1` to build debug version of Yolo -* `OPENMP=1` to build with OpenMP support to accelerate Yolo by using multi-core CPU -* `LIBSO=1` to build a library `darknet.so` and binary runnable file `uselib` that uses this library. Or you can try to run so `LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./uselib test.mp4` How to use this SO-library from your own code - you can look at C++ example: https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp +- `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 +- `OPENCV=1` to build with OpenCV 4.x/3.x/2.4.x - allows to detect on video files and video streams from network cameras or web-cams +- `DEBUG=1` to build debug version of Yolo +- `OPENMP=1` to build with OpenMP support to accelerate Yolo by using multi-core CPU +- `LIBSO=1` to build a library `darknet.so` and binary runnable file `uselib` that uses this library. Or you can try to run so `LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./uselib test.mp4` How to use this SO-library from your own code - you can look at C++ example: https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp or use in such a way: `LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./uselib data/coco.names cfg/yolov4.cfg yolov4.weights test.mp4` -* `ZED_CAMERA=1` to build a library with ZED-3D-camera support (should be ZED SDK installed), then run +- `ZED_CAMERA=1` to build a library with ZED-3D-camera support (should be ZED SDK installed), then run `LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./uselib data/coco.names cfg/yolov4.cfg yolov4.weights zed_camera` -* You also need to specify for which graphics card the code is generated. This is done by setting `ARCH=`. If you use a never version than CUDA 11 you further need to edit line 20 from Makefile and remove `-gencode arch=compute_30,code=sm_30 \` as Kepler GPU support was dropped in CUDA 11. You can also drop the general `ARCH=` and just uncomment `ARCH=` for your graphics card. +- You also need to specify for which graphics card the code is generated. This is done by setting `ARCH=`. If you use a never version than CUDA 11 you further need to edit line 20 from Makefile and remove `-gencode arch=compute_30,code=sm_30 \` as Kepler GPU support was dropped in CUDA 11. You can also drop the general `ARCH=` and just uncomment `ARCH=` for your graphics card. 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/yolov4.cfg ./yolov4.weights` @@ -371,37 +374,37 @@ To run Darknet on Linux use examples from this article, just use `./darknet` ins Requires: -* MSVC: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community -* CMake GUI: `Windows win64-x64 Installer`https://cmake.org/download/ -* Download Darknet zip-archive with the latest commit and uncompress it: [master.zip](https://github.com/AlexeyAB/darknet/archive/master.zip) +- MSVC: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community +- CMake GUI: `Windows win64-x64 Installer`https://cmake.org/download/ +- Download Darknet zip-archive with the latest commit and uncompress it: [master.zip](https://github.com/AlexeyAB/darknet/archive/master.zip) In Windows: -* Start (button) -> All programs -> CMake -> CMake (gui) -> +- Start (button) -> All programs -> CMake -> CMake (gui) -> -* [look at image](https://habrastorage.org/webt/pz/s1/uu/pzs1uu4heb7vflfcjqn-lxy-aqu.jpeg) In CMake: Enter input path to the darknet Source, and output path to the Binaries -> Configure (button) -> Optional platform for generator: `x64` -> Finish -> Generate -> Open Project -> +- [look at image](https://habrastorage.org/webt/pz/s1/uu/pzs1uu4heb7vflfcjqn-lxy-aqu.jpeg) In CMake: Enter input path to the darknet Source, and output path to the Binaries -> Configure (button) -> Optional platform for generator: `x64` -> Finish -> Generate -> Open Project -> -* in MS Visual Studio: Select: x64 and Release -> Build -> Build solution +- in MS Visual Studio: Select: x64 and Release -> Build -> Build solution -* find the executable file `darknet.exe` in the output path to the binaries you specified +- find the executable file `darknet.exe` in the output path to the binaries you specified ![x64 and Release](https://habrastorage.org/webt/ay/ty/f-/aytyf-8bufe7q-16yoecommlwys.jpeg) - ### How to compile on Windows (using `vcpkg`) This is the recommended approach to build Darknet on Windows. -1. Install Visual Studio 2017 or 2019. In case you need to download it, please go here: [Visual Studio Community](http://visualstudio.com) +1. Install Visual Studio 2017 or 2019. In case you need to download it, please go here: [Visual Studio Community](http://visualstudio.com). Remember to install English language pack, this is mandatory for vcpkg! -2. Install CUDA (at least v10.0) enabling VS Integration during installation. +2. Install CUDA enabling VS Integration during installation. 3. Open Powershell (Start -> All programs -> Windows Powershell) and type these commands: ```PowerShell -PS Code/> git clone https://github.com/AlexeyAB/darknet -PS Code/> cd darknet -PS Code/darknet> .\build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN +Set-ExecutionPolicy unrestricted -Scope CurrentUser -Force +git clone https://github.com/AlexeyAB/darknet +cd darknet +.\build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN ``` (add option `-EnableOPENCV_CUDA` if you want to build OpenCV with CUDA support - very slow to build! - or remove options like `-EnableCUDA` or `-EnableCUDNN` if you are not interested in them). If you open the `build.ps1` script at the beginning you will find all available switches. @@ -423,31 +426,29 @@ https://groups.google.com/d/msg/darknet/NbJqonJBTSY/Te5PfIpuCAAJ Training Yolo v4 (and v3): 0. For training `cfg/yolov4-custom.cfg` download the pre-trained weights-file (162 MB): [yolov4.conv.137](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137) (Google drive mirror [yolov4.conv.137](https://drive.google.com/open?id=1JKF-bdIklxOOVy-2Cr5qdvjgGpmGfcbp) ) - 1. Create file `yolo-obj.cfg` with the same content as in `yolov4-custom.cfg` (or copy `yolov4-custom.cfg` to `yolo-obj.cfg)` and: -* change line batch to [`batch=64`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L3) -* change line subdivisions to [`subdivisions=16`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L4) -* change line max_batches to (`classes*2000`, but not less than number of training images and not less than `6000`), f.e. [`max_batches=6000`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L20) if you train for 3 classes -* change line steps to 80% and 90% of max_batches, f.e. [`steps=4800,5400`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L22) -* set network size `width=416 height=416` or any value multiple of 32: https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L8-L9 -* change line `classes=80` to your number of objects in each of 3 `[yolo]`-layers: - * https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L610 - * https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L696 - * https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L783 -* change [`filters=255`] to filters=(classes + 5)x3 in the 3 `[convolutional]` before each `[yolo]` layer, keep in mind that it only has to be the last `[convolutional]` before each of the `[yolo]` layers. - * https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L603 - * https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L689 - * https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L776 -* when using [`[Gaussian_yolo]`](https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L608) layers, change [`filters=57`] filters=(classes + 9)x3 in the 3 `[convolutional]` before each `[Gaussian_yolo]` layer - * https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L604 - * https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L696 - * https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L789 +- change line batch to [`batch=64`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L3) +- change line subdivisions to [`subdivisions=16`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L4) +- change line max_batches to (`classes*2000`, but not less than number of training images and not less than `6000`), f.e. [`max_batches=6000`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L20) if you train for 3 classes +- change line steps to 80% and 90% of max_batches, f.e. [`steps=4800,5400`](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L22) +- set network size `width=416 height=416` or any value multiple of 32: https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L8-L9 +- change line `classes=80` to your number of objects in each of 3 `[yolo]`-layers: + - https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L610 + - https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L696 + - https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L783 +- change [`filters=255`] to filters=(classes + 5)x3 in the 3 `[convolutional]` before each `[yolo]` layer, keep in mind that it only has to be the last `[convolutional]` before each of the `[yolo]` layers. + - https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L603 + - https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L689 + - https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L776 +- when using [`[Gaussian_yolo]`](https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L608) layers, change [`filters=57`] filters=(classes + 9)x3 in the 3 `[convolutional]` before each `[Gaussian_yolo]` layer + - https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L604 + - https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L696 + - https://github.com/AlexeyAB/darknet/blob/6e5bdf1282ad6b06ed0e962c3f5be67cf63d96dc/cfg/Gaussian_yolov3_BDD.cfg#L789 So if `classes=1` then should be `filters=18`. If `classes=2` then write `filters=21`. - **(Do not write in the cfg-file: filters=(classes + 5)x3)** - + (Generally `filters` depends on the `classes`, `coords` and number of `mask`s, i.e. filters=`(classes + coords + 1)*`, where `mask` is indices of anchors. If `mask` is absence, then filters=`(classes + coords + 1)*num`) So for example, for 2 objects, your file `yolo-obj.cfg` should differ from `yolov4-custom.cfg` in such lines in each of **3** [yolo]-layers: @@ -461,7 +462,6 @@ classes=2 ``` 2. Create file `obj.names` in the directory `build\darknet\x64\data\`, with objects names - each in new line - 3. Create file `obj.data` in the directory `build\darknet\x64\data\`, containing (where **classes = number of objects**): ```ini @@ -473,22 +473,22 @@ classes=2 ``` 4. Put image-files (.jpg) of your objects in the directory `build\darknet\x64\data\obj\` - 5. You should label each object on images from your dataset. Use this visual GUI-software for marking bounded boxes of objects and generating annotation files for Yolo v2 & v3: https://github.com/AlexeyAB/Yolo_mark -It will create `.txt`-file for each `.jpg`-image-file - in the same directory and with the same name, but with `.txt`-extension, and put to file: object number and object coordinates on this image, for each object in new line: +It will create `.txt`-file for each `.jpg`-image-file - in the same directory and with the same name, but with `.txt`-extension, and put to file: object number and object coordinates on this image, for each object in new line: ` ` - Where: - * `` - integer object number from `0` to `(classes-1)` - * ` ` - float values **relative** to width and height of image, it can be equal from `(0.0 to 1.0]` - * for example: ` = / ` or ` = / ` - * attention: ` ` - are center of rectangle (are not top-left corner) + Where: + +- `` - integer object number from `0` to `(classes-1)` +- ` ` - float values **relative** to width and height of image, it can be equal from `(0.0 to 1.0]` +- for example: ` = / ` or ` = / ` +- attention: ` ` - are center of rectangle (are not top-left corner) For example for `img1.jpg` you will be created `img1.txt` containing: - ``` + ```csv 1 0.716797 0.395833 0.216406 0.147222 0 0.687109 0.379167 0.255469 0.158333 1 0.420312 0.395833 0.140625 0.166667 @@ -496,60 +496,60 @@ It will create `.txt`-file for each `.jpg`-image-file - in the same directory an 6. Create file `train.txt` in directory `build\darknet\x64\data\`, with filenames of your images, each filename in new line, with path relative to `darknet.exe`, for example containing: - ``` + ```csv data/obj/img1.jpg data/obj/img2.jpg data/obj/img3.jpg ``` 7. Download pre-trained weights for the convolutional layers and put to the directory `build\darknet\x64` - * for `yolov4.cfg`, `yolov4-custom.cfg` (162 MB): [yolov4.conv.137](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137) (Google drive mirror [yolov4.conv.137](https://drive.google.com/open?id=1JKF-bdIklxOOVy-2Cr5qdvjgGpmGfcbp) ) - * for `yolov4-tiny.cfg`, `yolov4-tiny-3l.cfg`, `yolov4-tiny-custom.cfg` (19 MB): [yolov4-tiny.conv.29](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29) - * for `csresnext50-panet-spp.cfg` (133 MB): [csresnext50-panet-spp.conv.112](https://drive.google.com/file/d/16yMYCLQTY_oDlCIZPfn_sab6KD3zgzGq/view?usp=sharing) - * for `yolov3.cfg, yolov3-spp.cfg` (154 MB): [darknet53.conv.74](https://pjreddie.com/media/files/darknet53.conv.74) - * for `yolov3-tiny-prn.cfg , yolov3-tiny.cfg` (6 MB): [yolov3-tiny.conv.11](https://drive.google.com/file/d/18v36esoXCh-PsOKwyP2GWrpYDptDY8Zf/view?usp=sharing) - * for `enet-coco.cfg (EfficientNetB0-Yolov3)` (14 MB): [enetb0-coco.conv.132](https://drive.google.com/file/d/1uhh3D6RSn0ekgmsaTcl-ZW53WBaUDo6j/view?usp=sharing) - + - for `yolov4.cfg`, `yolov4-custom.cfg` (162 MB): [yolov4.conv.137](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137) (Google drive mirror [yolov4.conv.137](https://drive.google.com/open?id=1JKF-bdIklxOOVy-2Cr5qdvjgGpmGfcbp) ) + - for `yolov4-tiny.cfg`, `yolov4-tiny-3l.cfg`, `yolov4-tiny-custom.cfg` (19 MB): [yolov4-tiny.conv.29](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29) + - for `csresnext50-panet-spp.cfg` (133 MB): [csresnext50-panet-spp.conv.112](https://drive.google.com/file/d/16yMYCLQTY_oDlCIZPfn_sab6KD3zgzGq/view?usp=sharing) + - for `yolov3.cfg, yolov3-spp.cfg` (154 MB): [darknet53.conv.74](https://pjreddie.com/media/files/darknet53.conv.74) + - for `yolov3-tiny-prn.cfg , yolov3-tiny.cfg` (6 MB): [yolov3-tiny.conv.11](https://drive.google.com/file/d/18v36esoXCh-PsOKwyP2GWrpYDptDY8Zf/view?usp=sharing) + - for `enet-coco.cfg (EfficientNetB0-Yolov3)` (14 MB): [enetb0-coco.conv.132](https://drive.google.com/file/d/1uhh3D6RSn0ekgmsaTcl-ZW53WBaUDo6j/view?usp=sharing) 8. Start training by using the command line: `darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137` - + To train on Linux use command: `./darknet detector train data/obj.data yolo-obj.cfg yolov4.conv.137` (just use `./darknet` instead of `darknet.exe`) - - * (file `yolo-obj_last.weights` will be saved to the `build\darknet\x64\backup\` for each 100 iterations) - * (file `yolo-obj_xxxx.weights` will be saved to the `build\darknet\x64\backup\` for each 1000 iterations) - * (to disable Loss-Window use `darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -dont_show`, if you train on computer without monitor like a cloud Amazon EC2) - * (to see the mAP & Loss-chart during training on remote server without GUI, use command `darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -dont_show -mjpeg_port 8090 -map` then open URL `http://ip-address:8090` in Chrome/Firefox browser) + + - (file `yolo-obj_last.weights` will be saved to the `build\darknet\x64\backup\` for each 100 iterations) + - (file `yolo-obj_xxxx.weights` will be saved to the `build\darknet\x64\backup\` for each 1000 iterations) + - (to disable Loss-Window use `darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -dont_show`, if you train on computer without monitor like a cloud Amazon EC2) + - (to see the mAP & Loss-chart during training on remote server without GUI, use command `darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -dont_show -mjpeg_port 8090 -map` then open URL `http://ip-address:8090` in Chrome/Firefox browser) 8.1. For training with mAP (mean average precisions) calculation for each 4 Epochs (set `valid=valid.txt` or `train.txt` in `obj.data` file) and run: `darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -map` 9. After training is complete - get result `yolo-obj_final.weights` from path `build\darknet\x64\backup\` - * After each 100 iterations you can stop and later start training from this point. For example, after 2000 iterations you can stop training, and later just start training using: `darknet.exe detector train data/obj.data yolo-obj.cfg backup\yolo-obj_2000.weights` + - After each 100 iterations you can stop and later start training from this point. For example, after 2000 iterations you can stop training, and later just start training using: `darknet.exe detector train data/obj.data yolo-obj.cfg backup\yolo-obj_2000.weights` (in the original repository https://github.com/pjreddie/darknet the weights-file is saved only once every 10 000 iterations `if(iterations > 1000)`) - * Also you can get result earlier than all 45000 iterations. - + - Also you can get result earlier than all 45000 iterations. + **Note:** If during training you see `nan` values for `avg` (loss) field - then training goes wrong, but if `nan` is in some other lines - then training goes well. - + **Note:** If you changed width= or height= in your cfg-file, then new width and height must be divisible by 32. - + **Note:** After training use such command for detection: `darknet.exe detector test data/obj.data yolo-obj.cfg yolo-obj_8000.weights` - + **Note:** if error `Out of memory` occurs then in `.cfg`-file you should increase `subdivisions=16`, 32 or 64: [link](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L4) - -### How to train tiny-yolo (to detect your custom objects): + +### How to train tiny-yolo (to detect your custom objects) Do all the same steps as for the full yolo model as described above. With the exception of: -* Download file with the first 29-convolutional layers of yolov4-tiny: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29 + +- Download file with the first 29-convolutional layers of yolov4-tiny: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29 (Or get this file from yolov4-tiny.weights file by using command: `darknet.exe partial cfg/yolov4-tiny-custom.cfg yolov4-tiny.weights yolov4-tiny.conv.29 29` -* Make your custom model `yolov4-tiny-obj.cfg` based on `cfg/yolov4-tiny-custom.cfg` instead of `yolov4.cfg` -* Start training: `darknet.exe detector train data/obj.data yolov4-tiny-obj.cfg yolov4-tiny.conv.29` +- Make your custom model `yolov4-tiny-obj.cfg` based on `cfg/yolov4-tiny-custom.cfg` instead of `yolov4.cfg` +- Start training: `darknet.exe detector train data/obj.data yolov4-tiny-obj.cfg yolov4-tiny.conv.29` For training Yolo based on other models ([DenseNet201-Yolo](https://github.com/AlexeyAB/darknet/blob/master/build/darknet/x64/densenet201_yolo.cfg) or [ResNet50-Yolo](https://github.com/AlexeyAB/darknet/blob/master/build/darknet/x64/resnet50_yolo.cfg)), you can download and get pre-trained weights as showed in this file: https://github.com/AlexeyAB/darknet/blob/master/build/darknet/x64/partial.cmd If you made you custom model that isn't based on other models, then you can train it without pre-trained weights, then will be used random initial weights. - -## When should I stop training: + +## When should I stop training Usually sufficient 2000 iterations for each class(object), but not less than number of training images and not less than 6000 iterations in total. But for a more precise definition when you should stop training, use the following manual: @@ -561,18 +561,18 @@ Usually sufficient 2000 iterations for each class(object), but not less than num > **9002**: 0.211667, **0.60730 avg**, 0.001000 rate, 3.868000 seconds, 576128 images > Loaded: 0.000000 seconds - * **9002** - iteration number (number of batch) - * **0.60730 avg** - average loss (error) - **the lower, the better** +- **9002** - iteration number (number of batch) +- **0.60730 avg** - average loss (error) - **the lower, the better** When you see that average loss **0.xxxxxx avg** no longer decreases at many iterations then you should stop training. The final average loss can be from `0.05` (for a small model and easy dataset) to `3.0` (for a big model and a difficult dataset). - Or if you train with flag `-map` then you will see mAP indicator `Last accuracy mAP@0.5 = 18.50%` in the console - this indicator is better than Loss, so train while mAP increases. + Or if you train with flag `-map` then you will see mAP indicator `Last accuracy mAP@0.5 = 18.50%` in the console - this indicator is better than Loss, so train while mAP increases. 2. Once training is stopped, you should take some of last `.weights`-files from `darknet\build\darknet\x64\backup` and choose the best of them: For example, you stopped training after 9000 iterations, but the best result can give one of previous weights (7000, 8000, 9000). It can happen due to over-fitting. **Over-fitting** - is case when you can detect objects on images from training-dataset, but can't detect objects on any others images. You should get weights from **Early Stopping Point**: -![Over-fitting](https://hsto.org/files/5dc/7ae/7fa/5dc7ae7fad9d4e3eb3a484c58bfc1ff5.png) +![Over-fitting](https://hsto.org/files/5dc/7ae/7fa/5dc7ae7fad9d4e3eb3a484c58bfc1ff5.png) To get weights from Early Stopping Point: @@ -582,9 +582,9 @@ To get weights from Early Stopping Point: (If you use another GitHub repository, then use `darknet.exe detector recall`... instead of `darknet.exe detector map`...) -* `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` -* `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_8000.weights` -* `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_9000.weights` +- `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_7000.weights` +- `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_8000.weights` +- `darknet.exe detector map data/obj.data yolo-obj.cfg backup\yolo-obj_9000.weights` And compare last output lines for each weights (7000, 8000, 9000): @@ -592,9 +592,9 @@ Choose weights-file **with the highest mAP (mean average precision)** or IoU (in For example, **bigger mAP** gives weights `yolo-obj_8000.weights` - then **use this weights for detection**. -Or just train with `-map` flag: +Or just train with `-map` flag: -`darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -map` +`darknet.exe detector train data/obj.data yolo-obj.cfg yolov4.conv.137 -map` So you will see mAP-chart (red-line) in the Loss-chart Window. mAP will be calculated for each 4 Epochs using `valid=valid.txt` file that is specified in `obj.data` file (`1 Epoch = images_in_train_txt / batch` iterations) @@ -604,83 +604,82 @@ So you will see mAP-chart (red-line) in the Loss-chart Window. mAP will be calcu Example of custom object detection: `darknet.exe detector test data/obj.data yolo-obj.cfg yolo-obj_8000.weights` -* **IoU** (intersect over union) - average intersect over union of objects and detections for a certain threshold = 0.24 +- **IoU** (intersect over union) - average intersect over union of objects and detections for a certain threshold = 0.24 -* **mAP** (mean average precision) - mean value of `average precisions` for each class, where `average precision` is average value of 11 points on PR-curve for each possible threshold (each probability of detection) for the same class (Precision-Recall in terms of PascalVOC, where Precision=TP/(TP+FP) and Recall=TP/(TP+FN) ), page-11: http://homepages.inf.ed.ac.uk/ckiw/postscript/ijcv_voc09.pdf +- **mAP** (mean average precision) - mean value of `average precisions` for each class, where `average precision` is average value of 11 points on PR-curve for each possible threshold (each probability of detection) for the same class (Precision-Recall in terms of PascalVOC, where Precision=TP/(TP+FP) and Recall=TP/(TP+FN) ), page-11: http://homepages.inf.ed.ac.uk/ckiw/postscript/ijcv_voc09.pdf **mAP** is default metric of precision in the PascalVOC competition, **this is the same as AP50** metric in the MS COCO competition. In terms of Wiki, indicators Precision and Recall have a slightly different meaning than in the PascalVOC competition, but **IoU always has the same meaning**. ![precision_recall_iou](https://hsto.org/files/ca8/866/d76/ca8866d76fb840228940dbf442a7f06a.jpg) - -### Custom object detection: +### Custom object detection Example of custom object detection: `darknet.exe detector test data/obj.data yolo-obj.cfg yolo-obj_8000.weights` | ![Yolo_v2_training](https://hsto.org/files/d12/1e7/515/d121e7515f6a4eb694913f10de5f2b61.jpg) | ![Yolo_v2_training](https://hsto.org/files/727/c7e/5e9/727c7e5e99bf4d4aa34027bb6a5e4bab.jpg) | |---|---| -## How to improve object detection: +## How to improve object detection 1. Before training: -* set flag `random=1` in your `.cfg`-file - it will increase precision by training Yolo for different resolutions: [link](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L788) +- set flag `random=1` in your `.cfg`-file - it will increase precision by training Yolo for different resolutions: [link](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L788) -* increase network resolution in your `.cfg`-file (`height=608`, `width=608` or any value multiple of 32) - it will increase precision +- increase network resolution in your `.cfg`-file (`height=608`, `width=608` or any value multiple of 32) - it will increase precision -* check that each object that you want to detect is mandatory labeled in your dataset - no one object in your data set should not be without label. In the most training issues - there are wrong labels in your dataset (got labels by using some conversion script, marked with a third-party tool, ...). Always check your dataset by using: https://github.com/AlexeyAB/Yolo_mark +- check that each object that you want to detect is mandatory labeled in your dataset - no one object in your data set should not be without label. In the most training issues - there are wrong labels in your dataset (got labels by using some conversion script, marked with a third-party tool, ...). Always check your dataset by using: https://github.com/AlexeyAB/Yolo_mark -* my Loss is very high and mAP is very low, is training wrong? Run training with ` -show_imgs` flag at the end of training command, do you see correct bounded boxes of objects (in windows or in files `aug_...jpg`)? If no - your training dataset is wrong. +- my Loss is very high and mAP is very low, is training wrong? Run training with `-show_imgs` flag at the end of training command, do you see correct bounded boxes of objects (in windows or in files `aug_...jpg`)? If no - your training dataset is wrong. -* for each object which you want to detect - there must be at least 1 similar object in the Training dataset with about the same: shape, side of object, relative size, angle of rotation, tilt, illumination. So desirable that your training dataset include images with objects at different: scales, rotations, lightings, from different sides, on different backgrounds - you should preferably have 2000 different images for each class or more, and you should train `2000*classes` iterations or more +- for each object which you want to detect - there must be at least 1 similar object in the Training dataset with about the same: shape, side of object, relative size, angle of rotation, tilt, illumination. So desirable that your training dataset include images with objects at different: scales, rotations, lightings, from different sides, on different backgrounds - you should preferably have 2000 different images for each class or more, and you should train `2000*classes` iterations or more -* desirable that your training dataset include images with non-labeled objects that you do not want to detect - negative samples without bounded box (empty `.txt` files) - use as many images of negative samples as there are images with objects +- desirable that your training dataset include images with non-labeled objects that you do not want to detect - negative samples without bounded box (empty `.txt` files) - use as many images of negative samples as there are images with objects -* What is the best way to mark objects: label only the visible part of the object, or label the visible and overlapped part of the object, or label a little more than the entire object (with a little gap)? Mark as you like - how would you like it to be detected. +- What is the best way to mark objects: label only the visible part of the object, or label the visible and overlapped part of the object, or label a little more than the entire object (with a little gap)? Mark as you like - how would you like it to be detected. -* for training with a large number of objects in each image, add the parameter `max=200` or higher value in the last `[yolo]`-layer or `[region]`-layer in your cfg-file (the global maximum number of objects that can be detected by YoloV3 is `0,0615234375*(width*height)` where are width and height are parameters from `[net]` section in cfg-file) +- for training with a large number of objects in each image, add the parameter `max=200` or higher value in the last `[yolo]`-layer or `[region]`-layer in your cfg-file (the global maximum number of objects that can be detected by YoloV3 is `0,0615234375*(width*height)` where are width and height are parameters from `[net]` section in cfg-file) -* for training for small objects (smaller than 16x16 after the image is resized to 416x416) - set `layers = 23` instead of https://github.com/AlexeyAB/darknet/blob/6f718c257815a984253346bba8fb7aa756c55090/cfg/yolov4.cfg#L895 - * set `stride=4` instead of https://github.com/AlexeyAB/darknet/blob/6f718c257815a984253346bba8fb7aa756c55090/cfg/yolov4.cfg#L892 - * set `stride=4` instead of https://github.com/AlexeyAB/darknet/blob/6f718c257815a984253346bba8fb7aa756c55090/cfg/yolov4.cfg#L989 +- for training for small objects (smaller than 16x16 after the image is resized to 416x416) - set `layers = 23` instead of https://github.com/AlexeyAB/darknet/blob/6f718c257815a984253346bba8fb7aa756c55090/cfg/yolov4.cfg#L895 + - set `stride=4` instead of https://github.com/AlexeyAB/darknet/blob/6f718c257815a984253346bba8fb7aa756c55090/cfg/yolov4.cfg#L892 + - set `stride=4` instead of https://github.com/AlexeyAB/darknet/blob/6f718c257815a984253346bba8fb7aa756c55090/cfg/yolov4.cfg#L989 -* for training for both small and large objects use modified models: - * Full-model: 5 yolo layers: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3_5l.cfg - * Tiny-model: 3 yolo layers: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny_3l.cfg - * YOLOv4: 3 yolo layers: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-custom.cfg +- for training for both small and large objects use modified models: + - Full-model: 5 yolo layers: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3_5l.cfg + - Tiny-model: 3 yolo layers: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny_3l.cfg + - YOLOv4: 3 yolo layers: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-custom.cfg -* If you train the model to distinguish Left and Right objects as separate classes (left/right hand, left/right-turn on road signs, ...) then for disabling flip data augmentation - add `flip=0` here: https://github.com/AlexeyAB/darknet/blob/3d2d0a7c98dbc8923d9ff705b81ff4f7940ea6ff/cfg/yolov3.cfg#L17 +- If you train the model to distinguish Left and Right objects as separate classes (left/right hand, left/right-turn on road signs, ...) then for disabling flip data augmentation - add `flip=0` here: https://github.com/AlexeyAB/darknet/blob/3d2d0a7c98dbc8923d9ff705b81ff4f7940ea6ff/cfg/yolov3.cfg#L17 -* General rule - your training dataset should include such a set of relative sizes of objects that you want to detect: - * `train_network_width * train_obj_width / train_image_width ~= detection_network_width * detection_obj_width / detection_image_width` - * `train_network_height * train_obj_height / train_image_height ~= detection_network_height * detection_obj_height / detection_image_height` +- General rule - your training dataset should include such a set of relative sizes of objects that you want to detect: + - `train_network_width * train_obj_width / train_image_width ~= detection_network_width * detection_obj_width / detection_image_width` + - `train_network_height * train_obj_height / train_image_height ~= detection_network_height * detection_obj_height / detection_image_height` I.e. for each object from Test dataset there must be at least 1 object in the Training dataset with the same class_id and about the same relative size: - `object width in percent from Training dataset` ~= `object width in percent from Test dataset` + `object width in percent from Training dataset` ~= `object width in percent from Test dataset` That is, if only objects that occupied 80-90% of the image were present in the training set, then the trained network will not be able to detect objects that occupy 1-10% of the image. -* to speedup training (with decreasing detection accuracy) set param `stopbackward=1` for layer-136 in cfg-file +- to speedup training (with decreasing detection accuracy) set param `stopbackward=1` for layer-136 in cfg-file -* each: `model of object, side, illumination, scale, each 30 grad` of the turn and inclination angles - these are *different objects* from an internal perspective of the neural network. So the more *different objects* you want to detect, the more complex network model should be used. +- each: `model of object, side, illumination, scale, each 30 grad` of the turn and inclination angles - these are *different objects* from an internal perspective of the neural network. So the more *different objects* you want to detect, the more complex network model should be used. -* to make the detected bounded boxes more accurate, you can add 3 parameters `ignore_thresh = .9 iou_normalizer=0.5 iou_loss=giou` to each `[yolo]` layer and train, it will increase mAP@0.9, but decrease mAP@0.5. +- to make the detected bounded boxes more accurate, you can add 3 parameters `ignore_thresh = .9 iou_normalizer=0.5 iou_loss=giou` to each `[yolo]` layer and train, it will increase mAP@0.9, but decrease mAP@0.5. -* Only if you are an **expert** in neural detection networks - recalculate anchors for your dataset for `width` and `height` from cfg-file: +- Only if you are an **expert** in neural detection networks - recalculate anchors for your dataset for `width` and `height` from cfg-file: `darknet.exe detector calc_anchors data/obj.data -num_of_clusters 9 -width 416 -height 416` then set the same 9 `anchors` in each of 3 `[yolo]`-layers in your cfg-file. But you should change indexes of anchors `masks=` for each [yolo]-layer, so for YOLOv4 the 1st-[yolo]-layer has anchors smaller than 30x30, 2nd smaller than 60x60, 3rd remaining, and vice versa for YOLOv3. Also you should change the `filters=(classes + 5)*` before each [yolo]-layer. If many of the calculated anchors do not fit under the appropriate layers - then just try using all the default anchors. 2. After training - for detection: -* Increase network-resolution by set in your `.cfg`-file (`height=608` and `width=608`) or (`height=832` and `width=832`) or (any value multiple of 32) - this increases the precision and makes it possible to detect small objects: [link](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L8-L9) +- Increase network-resolution by set in your `.cfg`-file (`height=608` and `width=608`) or (`height=832` and `width=832`) or (any value multiple of 32) - this increases the precision and makes it possible to detect small objects: [link](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L8-L9) -* it is not necessary to train the network again, just use `.weights`-file already trained for 416x416 resolution +- it is not necessary to train the network again, just use `.weights`-file already trained for 416x416 resolution -* to get even greater accuracy you should train with higher resolution 608x608 or 832x832, note: if error `Out of memory` occurs then in `.cfg`-file you should increase `subdivisions=16`, 32 or 64: [link](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L4) +- to get even greater accuracy you should train with higher resolution 608x608 or 832x832, note: if error `Out of memory` occurs then in `.cfg`-file you should increase `subdivisions=16`, 32 or 64: [link](https://github.com/AlexeyAB/darknet/blob/0039fd26786ab5f71d5af725fc18b3f521e7acfd/cfg/yolov3.cfg#L4) -## How to mark bounded boxes of objects and create annotation files: +## How to mark bounded boxes of objects and create annotation files Here you can find repository with GUI-software for marking bounded boxes of objects and generating annotation files for Yolo v2 - v4: https://github.com/AlexeyAB/Yolo_mark @@ -700,40 +699,40 @@ Different tools for marking objects in images: ## How to use Yolo as DLL and SO libraries -* on Linux - * using `build.sh` or - * build `darknet` using `cmake` or - * set `LIBSO=1` in the `Makefile` and do `make` -* on Windows - * using `build.ps1` or - * build `darknet` using `cmake` or - * compile `build\darknet\yolo_cpp_dll.sln` solution or `build\darknet\yolo_cpp_dll_no_gpu.sln` solution +- on Linux + - using `build.sh` or + - build `darknet` using `cmake` or + - set `LIBSO=1` in the `Makefile` and do `make` +- on Windows + - using `build.ps1` or + - build `darknet` using `cmake` or + - compile `build\darknet\yolo_cpp_dll.sln` solution or `build\darknet\yolo_cpp_dll_no_gpu.sln` solution There are 2 APIs: -* C API: https://github.com/AlexeyAB/darknet/blob/master/include/darknet.h - * Python examples using the C API: - * https://github.com/AlexeyAB/darknet/blob/master/darknet.py - * https://github.com/AlexeyAB/darknet/blob/master/darknet_video.py +- C API: https://github.com/AlexeyAB/darknet/blob/master/include/darknet.h + - Python examples using the C API: + - https://github.com/AlexeyAB/darknet/blob/master/darknet.py + - https://github.com/AlexeyAB/darknet/blob/master/darknet_video.py -* C++ API: https://github.com/AlexeyAB/darknet/blob/master/include/yolo_v2_class.hpp - * C++ example that uses C++ API: https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp +- C++ API: https://github.com/AlexeyAB/darknet/blob/master/include/yolo_v2_class.hpp + - C++ example that uses C++ API: https://github.com/AlexeyAB/darknet/blob/master/src/yolo_console_dll.cpp ---- 1. To compile Yolo as C++ DLL-file `yolo_cpp_dll.dll` - open the solution `build\darknet\yolo_cpp_dll.sln`, set **x64** and **Release**, and do the: Build -> Build yolo_cpp_dll - * You should have installed **CUDA 10.0** - * To use cuDNN do: (right click on project) -> properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, and add at the beginning of line: `CUDNN;` + - You should have installed **CUDA 10.2** + - To use cuDNN do: (right click on project) -> properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, and add at the beginning of line: `CUDNN;` 2. To use Yolo as DLL-file in your C++ console application - open the solution `build\darknet\yolo_console_dll.sln`, set **x64** and **Release**, and do the: Build -> Build yolo_console_dll - * you can run your console application from Windows Explorer `build\darknet\x64\yolo_console_dll.exe` + - you can run your console application from Windows Explorer `build\darknet\x64\yolo_console_dll.exe` **use this command**: `yolo_console_dll.exe data/coco.names yolov4.cfg yolov4.weights test.mp4` - * after launching your console application and entering the image file name - you will see info for each object: + - after launching your console application and entering the image file name - you will see info for each object: ` ` - * to use simple OpenCV-GUI you should uncomment line `//#define OPENCV` in `yolo_console_dll.cpp`-file: [link](https://github.com/AlexeyAB/darknet/blob/a6cbaeecde40f91ddc3ea09aa26a03ab5bbf8ba8/src/yolo_console_dll.cpp#L5) - * you can see source code of simple example for detection on the video file: [link](https://github.com/AlexeyAB/darknet/blob/ab1c5f9e57b4175f29a6ef39e7e68987d3e98704/src/yolo_console_dll.cpp#L75) + - to use simple OpenCV-GUI you should uncomment line `//#define OPENCV` in `yolo_console_dll.cpp`-file: [link](https://github.com/AlexeyAB/darknet/blob/a6cbaeecde40f91ddc3ea09aa26a03ab5bbf8ba8/src/yolo_console_dll.cpp#L5) + - you can see source code of simple example for detection on the video file: [link](https://github.com/AlexeyAB/darknet/blob/ab1c5f9e57b4175f29a6ef39e7e68987d3e98704/src/yolo_console_dll.cpp#L75) `yolo_cpp_dll.dll`-API: [link](https://github.com/AlexeyAB/darknet/blob/master/src/yolo_v2_class.hpp#L42) diff --git a/build.ps1 b/build.ps1 index 07fb47aafa1..d230622e9a5 100755 --- a/build.ps1 +++ b/build.ps1 @@ -1,5 +1,6 @@ #!/usr/bin/env pwsh + param ( [switch]$DisableInteractive = $false, [switch]$EnableCUDA = $false, @@ -14,13 +15,26 @@ param ( [switch]$DoNotUseNinja = $false, [switch]$ForceCPP = $false, [switch]$ForceStaticLib = $false, + [switch]$ForceVCPKGCacheRemoval = $false, [switch]$ForceSetupVS = $false, - [switch]$ForceGCC8 = $false + [switch]$EnableCSharpWrapper = $false, + [Int32]$ForceGCCVersion = 0, + [Int32]$ForceOpenCVVersion = 0, + [Int32]$NumberOfBuildWorkers = 8, + [string]$AdditionalBuildSetup = "" # "-DCMAKE_CUDA_ARCHITECTURES=30" ) +$build_ps1_version = "0.9.5" + +$ErrorActionPreference = "SilentlyContinue" +Stop-Transcript | out-null +$ErrorActionPreference = "Continue" +Start-Transcript -Path $PSScriptRoot/build.log + Function MyThrow ($Message) { if ($DisableInteractive) { - Throw $Message + Write-Host $Message -ForegroundColor Red + throw } else { # Check if running in PowerShell ISE @@ -29,7 +43,7 @@ Function MyThrow ($Message) { # Show MessageBox UI $Shell = New-Object -ComObject "WScript.Shell" $Shell.Popup($Message, 0, "OK", 0) - return + throw } $Ignore = @@ -61,53 +75,79 @@ Function MyThrow ($Message) { 182, # Application 1 183 # Application 2 - Write-Host $Message + Write-Host $Message -ForegroundColor Red Write-Host -NoNewline "Press any key to continue..." - while ($null -eq $KeyInfo.VirtualKeyCode -or $Ignore -contains $KeyInfo.VirtualKeyCode) { + while (($null -eq $KeyInfo.VirtualKeyCode) -or ($Ignore -contains $KeyInfo.VirtualKeyCode)) { $KeyInfo = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown") } - exit + Write-Host "" + throw } } -if ($PSVersionTable.PSVersion.Major -eq 5) { - $IsWindowsPowerShell = $true +Function DownloadNinja() { + Write-Host "Unable to find Ninja, downloading a portable version on-the-fly" -ForegroundColor Yellow + Remove-Item -Force -Recurse -ErrorAction SilentlyContinue ninja + Remove-Item -Force -ErrorAction SilentlyContinue ninja.zip + if ($IsWindows -or $IsWindowsPowerShell) { + $url = "https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-win.zip" + } + elseif ($IsLinux) { + $url = "https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip" + } + elseif ($IsMacOS) { + $url = "https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-mac.zip" + } + else { + MyThrow("Unknown OS, unsupported") + } + Invoke-RestMethod -Uri $url -Method Get -ContentType application/zip -OutFile "ninja.zip" + Expand-Archive -Path ninja.zip + Remove-Item -Force -ErrorAction SilentlyContinue ninja.zip } -if ($PSVersionTable.PSVersion.Major -lt 5) { - MyThrow("Your PowerShell version is too old, please update it.") -} -if (-Not $DisableInteractive -and -Not $UseVCPKG) { +Write-Host "Darknet build script version ${build_ps1_version}" + +if ((-Not $DisableInteractive) -and (-Not $UseVCPKG)) { $Result = Read-Host "Enable vcpkg to install darknet dependencies (yes/no)" - if ($Result -eq 'Yes' -or $Result -eq 'Y' -or $Result -eq 'yes' -or $Result -eq 'y') { + if (($Result -eq 'Yes') -or ($Result -eq 'Y') -or ($Result -eq 'yes') -or ($Result -eq 'y')) { $UseVCPKG = $true } } -if (-Not $DisableInteractive -and -Not $EnableCUDA -and -Not $IsMacOS) { +if ((-Not $DisableInteractive) -and (-Not $EnableCUDA) -and (-Not $IsMacOS)) { $Result = Read-Host "Enable CUDA integration (yes/no)" - if ($Result -eq 'Yes' -or $Result -eq 'Y' -or $Result -eq 'yes' -or $Result -eq 'y') { + if (($Result -eq 'Yes') -or ($Result -eq 'Y') -or ($Result -eq 'yes') -or ($Result -eq 'y')) { $EnableCUDA = $true } } -if ($EnableCUDA -and -Not $DisableInteractive -and -Not $EnableCUDNN) { +if ($EnableCUDA -and (-Not $DisableInteractive) -and (-Not $EnableCUDNN)) { $Result = Read-Host "Enable CUDNN optional dependency (yes/no)" - if ($Result -eq 'Yes' -or $Result -eq 'Y' -or $Result -eq 'yes' -or $Result -eq 'y') { + if (($Result -eq 'Yes') -or ($Result -eq 'Y') -or ($Result -eq 'yes') -or ($Result -eq 'y')) { $EnableCUDNN = $true } } -if (-Not $DisableInteractive -and -Not $EnableOPENCV) { +if ((-Not $DisableInteractive) -and (-Not $EnableOPENCV)) { $Result = Read-Host "Enable OpenCV optional dependency (yes/no)" - if ($Result -eq 'Yes' -or $Result -eq 'Y' -or $Result -eq 'yes' -or $Result -eq 'y') { + if (($Result -eq 'Yes') -or ($Result -eq 'Y') -or ($Result -eq 'yes') -or ($Result -eq 'y')) { $EnableOPENCV = $true } } -$number_of_build_workers = 8 -#$additional_build_setup = " -DCMAKE_CUDA_ARCHITECTURES=30" +Write-Host -NoNewLine "PowerShell version:" +$PSVersionTable.PSVersion + +if ($PSVersionTable.PSVersion.Major -eq 5) { + $IsWindowsPowerShell = $true +} + +if ($PSVersionTable.PSVersion.Major -lt 5) { + MyThrow("Your PowerShell version is too old, please update it.") +} + if ($IsLinux -or $IsMacOS) { $bootstrap_ext = ".sh" @@ -115,24 +155,26 @@ if ($IsLinux -or $IsMacOS) { elseif ($IsWindows -or $IsWindowsPowerShell) { $bootstrap_ext = ".bat" } -Write-Host "Native shell script extension: ${bootstrap_ext}" +if ($UseVCPKG) { + Write-Host "vcpkg bootstrap script: bootstrap-vcpkg${bootstrap_ext}" +} -if (-Not $IsWindows -and -not $IsWindowsPowerShell -and -Not $ForceSetupVS) { +if ((-Not $IsWindows) -and (-Not $IsWindowsPowerShell) -and (-Not $ForceSetupVS)) { $DoNotSetupVS = $true } if ($ForceStaticLib) { Write-Host "Forced CMake to produce a static library" - $additional_build_setup = " -DBUILD_SHARED_LIBS=OFF " + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DBUILD_SHARED_LIBS=OFF " } -if ($IsLinux -and $ForceGCC8) { - Write-Host "Manually setting CC and CXX variables to gcc-8 and g++-8" - $env:CC = "gcc-8" - $env:CXX = "g++-8" +if (($IsLinux -or $IsMacOS) -and ($ForceGCCVersion -gt 0)) { + Write-Host "Manually setting CC and CXX variables to gcc version $ForceGCCVersion" + $env:CC = "gcc-$ForceGCCVersion" + $env:CXX = "g++-$ForceGCCVersion" } -if (($IsWindows -or $IsWindowsPowerShell) -and -Not $env:VCPKG_DEFAULT_TRIPLET) { +if (($IsWindows -or $IsWindowsPowerShell) -and (-Not $env:VCPKG_DEFAULT_TRIPLET)) { $env:VCPKG_DEFAULT_TRIPLET = "x64-windows" } @@ -165,18 +207,18 @@ else { Write-Host "OPENCV is disabled, please pass -EnableOPENCV to the script to enable" } -if ($EnableCUDA -and $EnableOPENCV -and -not $EnableOPENCV_CUDA) { +if ($EnableCUDA -and $EnableOPENCV -and (-Not $EnableOPENCV_CUDA)) { Write-Host "OPENCV with CUDA extension is not enabled, you can enable it passing -EnableOPENCV_CUDA" } -elseif ($EnableOPENCV -and $EnableOPENCV_CUDA -and -not $EnableCUDA) { +elseif ($EnableOPENCV -and $EnableOPENCV_CUDA -and (-Not $EnableCUDA)) { Write-Host "OPENCV with CUDA extension was requested, but CUDA is not enabled, you can enable it passing -EnableCUDA" $EnableOPENCV_CUDA = $false } -elseif ($EnableCUDA -and $EnableOPENCV_CUDA -and -not $EnableOPENCV) { +elseif ($EnableCUDA -and $EnableOPENCV_CUDA -and (-Not $EnableOPENCV)) { Write-Host "OPENCV with CUDA extension was requested, but OPENCV is not enabled, you can enable it passing -EnableOPENCV" $EnableOPENCV_CUDA = $false } -elseif ($EnableOPENCV_CUDA -and -not $EnableCUDA -and -not $EnableOPENCV) { +elseif ($EnableOPENCV_CUDA -and (-Not $EnableCUDA) -and (-Not $EnableOPENCV)) { Write-Host "OPENCV with CUDA extension was requested, but OPENCV and CUDA are not enabled, you can enable them passing -EnableOPENCV -EnableCUDA" $EnableOPENCV_CUDA = $false } @@ -201,6 +243,15 @@ else { Write-Host "VisualStudio integration is enabled, please pass -DoNotSetupVS to the script to disable" } +if ($EnableCSharpWrapper -and ($IsWindowsPowerShell -or $IsWindows)) { + Write-Host "Yolo C# wrapper integration is enabled. Will be built with Visual Studio generator. Disabling Ninja" + $DoNotUseNinja = $true +} +else { + $EnableCSharpWrapper = $false + Write-Host "Yolo C# wrapper integration is disabled, please pass -EnableCSharpWrapper to the script to enable. You must be on Windows!" +} + if ($DoNotUseNinja) { Write-Host "Ninja is disabled" } @@ -217,7 +268,7 @@ else { Push-Location $PSScriptRoot -$GIT_EXE = Get-Command git 2> $null | Select-Object -ExpandProperty Definition +$GIT_EXE = Get-Command "git" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition if (-Not $GIT_EXE) { MyThrow("Could not find git, please install it") } @@ -225,32 +276,64 @@ else { Write-Host "Using git from ${GIT_EXE}" } -if ((Test-Path "$PSScriptRoot/.git") -and -not $DoNotUpdateDARKNET) { - $proc = Start-Process -NoNewWindow -PassThru -FilePath $GIT_EXE -ArgumentList "pull" - $proc.WaitForExit() - $exitCode = $proc.ExitCode - if (-not $exitCode -eq 0) { - MyThrow("Updating darknet sources failed! Exited with $exitCode.") +if (Test-Path "$PSScriptRoot/.git") { + Write-Host "Darknet has been cloned with git and supports self-updating mechanism" + if ($DoNotUpdateDARKNET) { + Write-Host "Darknet will not self-update sources" -ForegroundColor Yellow + } + else { + Write-Host "Darknet will self-update sources, please pass -DoNotUpdateDARKNET to the script to disable" + $proc = Start-Process -NoNewWindow -PassThru -FilePath $GIT_EXE -ArgumentList "pull" + $handle = $proc.Handle + $proc.WaitForExit() + $exitCode = $proc.ExitCode + if (-Not ($exitCode -eq 0)) { + MyThrow("Updating darknet sources failed! Exited with error code $exitCode.") + } } } -$CMAKE_EXE = Get-Command cmake 2> $null | Select-Object -ExpandProperty Definition +$CMAKE_EXE = Get-Command "cmake" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition if (-Not $CMAKE_EXE) { MyThrow("Could not find CMake, please install it") } else { Write-Host "Using CMake from ${CMAKE_EXE}" + $proc = Start-Process -NoNewWindow -PassThru -FilePath ${CMAKE_EXE} -ArgumentList "--version" + $handle = $proc.Handle + $proc.WaitForExit() + $exitCode = $proc.ExitCode + if (-Not ($exitCode -eq 0)) { + MyThrow("CMake version check failed! Exited with error code $exitCode.") + } } if (-Not $DoNotUseNinja) { - $NINJA_EXE = Get-Command ninja 2> $null | Select-Object -ExpandProperty Definition + $NINJA_EXE = Get-Command "ninja" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition if (-Not $NINJA_EXE) { - $DoNotUseNinja = $true - Write-Host "Could not find Ninja, using msbuild or make backends as a fallback" -ForegroundColor Yellow + DownloadNinja + $env:PATH += ";${PSScriptRoot}/ninja" + $NINJA_EXE = Get-Command "ninja" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition + if (-Not $NINJA_EXE) { + $DoNotUseNinja = $true + Write-Host "Could not find Ninja, unable to download a portable ninja, using msbuild or make backends as a fallback" -ForegroundColor Yellow + } } - else { + if ($NINJA_EXE) { Write-Host "Using Ninja from ${NINJA_EXE}" - $generator = "Ninja" + Write-Host -NoNewLine "Ninja version " + $proc = Start-Process -NoNewWindow -PassThru -FilePath ${NINJA_EXE} -ArgumentList "--version" + $handle = $proc.Handle + $proc.WaitForExit() + $exitCode = $proc.ExitCode + if (-Not ($exitCode -eq 0)) { + $DoNotUseNinja = $true + Write-Host "Unable to run Ninja previously found, using msbuild or make backends as a fallback" -ForegroundColor Yellow + } + else { + $generator = "Ninja" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DCMAKE_BUILD_TYPE=Release" + } } } @@ -322,58 +405,65 @@ function getLatestVisualStudioWithDesktopWorkloadVersion() { return $installationVersion } +$vcpkg_root_set_by_this_script = $false if ((Test-Path env:VCPKG_ROOT) -and $UseVCPKG) { $vcpkg_path = "$env:VCPKG_ROOT" Write-Host "Found vcpkg in VCPKG_ROOT: $vcpkg_path" - $additional_build_setup = $additional_build_setup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" } elseif ((Test-Path "${env:WORKSPACE}/vcpkg") -and $UseVCPKG) { $vcpkg_path = "${env:WORKSPACE}/vcpkg" $env:VCPKG_ROOT = "${env:WORKSPACE}/vcpkg" + $vcpkg_root_set_by_this_script = $true Write-Host "Found vcpkg in WORKSPACE/vcpkg: $vcpkg_path" - $additional_build_setup = $additional_build_setup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" } elseif (-not($null -eq ${RUNVCPKG_VCPKG_ROOT_OUT})) { - if((Test-Path "${RUNVCPKG_VCPKG_ROOT_OUT}") -and $UseVCPKG) { + if ((Test-Path "${RUNVCPKG_VCPKG_ROOT_OUT}") -and $UseVCPKG) { $vcpkg_path = "${RUNVCPKG_VCPKG_ROOT_OUT}" $env:VCPKG_ROOT = "${RUNVCPKG_VCPKG_ROOT_OUT}" + $vcpkg_root_set_by_this_script = $true Write-Host "Found vcpkg in RUNVCPKG_VCPKG_ROOT_OUT: ${vcpkg_path}" - $additional_build_setup = $additional_build_setup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" } } elseif ($UseVCPKG) { if (-Not (Test-Path "$PWD/vcpkg")) { $proc = Start-Process -NoNewWindow -PassThru -FilePath $GIT_EXE -ArgumentList "clone https://github.com/microsoft/vcpkg" + $handle = $proc.Handle $proc.WaitForExit() $exitCode = $proc.ExitCode - if (-not $exitCode -eq 0) { - MyThrow("Cloning vcpkg sources failed! Exited with $exitCode.") + if (-not ($exitCode -eq 0)) { + MyThrow("Cloning vcpkg sources failed! Exited with error code $exitCode.") } } $vcpkg_path = "$PWD/vcpkg" $env:VCPKG_ROOT = "$PWD/vcpkg" + $vcpkg_root_set_by_this_script = $true Write-Host "Found vcpkg in $PWD/vcpkg: $PWD/vcpkg" - $additional_build_setup = $additional_build_setup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_VCPKG_INTEGRATION:BOOL=ON" } else { Write-Host "Skipping vcpkg integration`n" -ForegroundColor Yellow - $additional_build_setup = $additional_build_setup + " -DENABLE_VCPKG_INTEGRATION:BOOL=OFF" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_VCPKG_INTEGRATION:BOOL=OFF" } -if ($UseVCPKG -and (Test-Path "$vcpkg_path/.git") -and -not $DoNotUpdateVCPKG) { +if ($UseVCPKG -and (Test-Path "$vcpkg_path/.git") -and (-Not $DoNotUpdateVCPKG)) { Push-Location $vcpkg_path $proc = Start-Process -NoNewWindow -PassThru -FilePath $GIT_EXE -ArgumentList "pull" + $handle = $proc.Handle $proc.WaitForExit() $exitCode = $proc.ExitCode - if (-not $exitCode -eq 0) { - MyThrow("Updating vcpkg sources failed! Exited with $exitCode.") + if (-Not ($exitCode -eq 0)) { + MyThrow("Updating vcpkg sources failed! Exited with error code $exitCode.") } $proc = Start-Process -NoNewWindow -PassThru -FilePath $PWD/bootstrap-vcpkg${bootstrap_ext} -ArgumentList "-disableMetrics" + $handle = $proc.Handle $proc.WaitForExit() $exitCode = $proc.ExitCode - if (-not $exitCode -eq 0) { - MyThrow("Bootstrapping vcpkg failed! Exited with $exitCode.") + if (-Not ($exitCode -eq 0)) { + MyThrow("Bootstrapping vcpkg failed! Exited with error code $exitCode.") } Pop-Location } @@ -384,14 +474,46 @@ if ($UseVCPKG -and ($vcpkg_path.length -gt 40) -and ($IsWindows -or $IsWindowsPo Write-Host "You can use the subst command to ease the process if necessary" -ForegroundColor Yellow if (-Not $DisableInteractive) { $Result = Read-Host "Do you still want to continue? (yes/no)" - if ($Result -eq 'No' -or $Result -eq 'N' -or $Result -eq 'no' -or $Result -eq 'n') { + if (($Result -eq 'No') -or ($Result -eq 'N') -or ($Result -eq 'no') -or ($Result -eq 'n')) { MyThrow("Build aborted") } } } +if ($ForceVCPKGCacheRemoval -and (-Not $UseVCPKG)) { + Write-Host "VCPKG is not enabled, so local vcpkg binary cache will not be deleted even if requested" -ForegroundColor Yellow +} + +if (($ForceOpenCVVersion -eq 2) -and $UseVCPKG) { + Write-Host "You requested OpenCV version 2, so vcpkg will install that version" -ForegroundColor Yellow + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DVCPKG_USE_OPENCV4=OFF -DVCPKG_USE_OPENCV2=ON" +} + +if (($ForceOpenCVVersion -eq 3) -and $UseVCPKG) { + Write-Host "You requested OpenCV version 3, so vcpkg will install that version" -ForegroundColor Yellow + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DVCPKG_USE_OPENCV4=OFF -DVCPKG_USE_OPENCV3=ON" +} + +if ($UseVCPKG -and $ForceVCPKGCacheRemoval) { + if ($IsWindows -or $IsWindowsPowerShell) { + $vcpkgbinarycachepath = "$env:LOCALAPPDATA/vcpkg/archive" + } + elseif ($IsLinux) { + $vcpkgbinarycachepath = "$env:HOME/.cache/vcpkg/archive" + } + elseif ($IsMacOS) { + $vcpkgbinarycachepath = "$env:HOME/.cache/vcpkg/archive" + } + else { + MyThrow("Unknown OS, unsupported") + } + Write-Host "Removing local vcpkg binary cache from $vcpkgbinarycachepath" -ForegroundColor Yellow + Remove-Item -Force -Recurse -ErrorAction SilentlyContinue $vcpkgbinarycachepath +} + if (-Not $DoNotSetupVS) { - if ($null -eq (Get-Command "cl.exe" -ErrorAction SilentlyContinue)) { + $CL_EXE = Get-Command "cl" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition + if ((-Not $CL_EXE) -or ($CL_EXE -match "HostX86\\x86") -or ($CL_EXE -match "HostX64\\x86")) { $vsfound = getLatestVisualStudioWithDesktopWorkloadPath Write-Host "Found VS in ${vsfound}" Push-Location "${vsfound}\Common7\Tools" @@ -412,15 +534,15 @@ if (-Not $DoNotSetupVS) { $selectConfig = " --config Release " if ($tokens[0] -eq "14") { $generator = "Visual Studio 14 2015" - $additional_build_setup = $additional_build_setup + " -T `"host=x64`" -A `"x64`"" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -T `"host=x64`" -A `"x64`"" } elseif ($tokens[0] -eq "15") { $generator = "Visual Studio 15 2017" - $additional_build_setup = $additional_build_setup + " -T `"host=x64`" -A `"x64`"" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -T `"host=x64`" -A `"x64`"" } elseif ($tokens[0] -eq "16") { $generator = "Visual Studio 16 2019" - $additional_build_setup = $additional_build_setup + " -T `"host=x64`" -A `"x64`"" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -T `"host=x64`" -A `"x64`"" } else { MyThrow("Unknown Visual Studio version, unsupported configuration") @@ -432,11 +554,13 @@ if (-Not $DoNotSetupVS) { } if ($DoNotSetupVS -and $DoNotUseNinja) { $generator = "Unix Makefiles" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DCMAKE_BUILD_TYPE=Release" } Write-Host "Setting up environment to use CMake generator: $generator" if (-Not $IsMacOS -and $EnableCUDA) { - if ($null -eq (Get-Command "nvcc" -ErrorAction SilentlyContinue)) { + $NVCC_EXE = Get-Command "nvcc" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition + if (-Not $NVCC_EXE) { if (Test-Path env:CUDA_PATH) { $env:PATH += ";${env:CUDA_PATH}/bin" Write-Host "Found cuda in ${env:CUDA_PATH}" @@ -459,23 +583,27 @@ if (-Not $IsMacOS -and $EnableCUDA) { } if ($ForceCPP) { - $additional_build_setup = $additional_build_setup + " -DBUILD_AS_CPP:BOOL=ON" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DBUILD_AS_CPP:BOOL=ON" +} + +if (-Not $EnableCUDA) { + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_CUDA:BOOL=OFF" } -if (-Not($EnableCUDA)) { - $additional_build_setup = $additional_build_setup + " -DENABLE_CUDA:BOOL=OFF" +if (-Not $EnableCUDNN) { + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_CUDNN:BOOL=OFF" } -if (-Not($EnableCUDNN)) { - $additional_build_setup = $additional_build_setup + " -DENABLE_CUDNN:BOOL=OFF" +if (-Not $EnableOPENCV) { + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DENABLE_OPENCV:BOOL=OFF" } -if (-Not($EnableOPENCV)) { - $additional_build_setup = $additional_build_setup + " -DENABLE_OPENCV:BOOL=OFF" +if (-Not $EnableOPENCV_CUDA) { + $AdditionalBuildSetup = $AdditionalBuildSetup + " -DVCPKG_BUILD_OPENCV_WITH_CUDA:BOOL=OFF" } -if (-Not($EnableOPENCV_CUDA)) { - $additional_build_setup = $additional_build_setup + " -DVCPKG_BUILD_OPENCV_WITH_CUDA:BOOL=OFF" +if ($EnableCSharpWrapper) { + $additional_build_setup = $additional_build_setup + " -DENABLE_CSHARP_WRAPPER:BOOL=ON" } $build_folder = "./build_release" @@ -484,28 +612,41 @@ if (-Not $DoNotDeleteBuildFolder) { Remove-Item -Force -Recurse -ErrorAction SilentlyContinue $build_folder } -New-Item -Path $build_folder -ItemType directory -Force +New-Item -Path $build_folder -ItemType directory -Force | Out-Null Set-Location $build_folder -$cmake_args = "-G `"$generator`" ${additional_build_setup} -S .." +$cmake_args = "-G `"$generator`" ${AdditionalBuildSetup} -S .." +Write-Host "Configuring CMake project" -ForegroundColor Green Write-Host "CMake args: $cmake_args" $proc = Start-Process -NoNewWindow -PassThru -FilePath $CMAKE_EXE -ArgumentList $cmake_args +$handle = $proc.Handle $proc.WaitForExit() $exitCode = $proc.ExitCode -if (-not $exitCode -eq 0) { - MyThrow("Config failed! Exited with $exitCode.") +if (-Not ($exitCode -eq 0)) { + MyThrow("Config failed! Exited with error code $exitCode.") } -$proc = Start-Process -NoNewWindow -PassThru -FilePath $CMAKE_EXE -ArgumentList "--build . ${selectConfig} --parallel ${number_of_build_workers} --target install" +Write-Host "Building CMake project" -ForegroundColor Green +$proc = Start-Process -NoNewWindow -PassThru -FilePath $CMAKE_EXE -ArgumentList "--build . ${selectConfig} --parallel ${NumberOfBuildWorkers} --target install" +$handle = $proc.Handle $proc.WaitForExit() $exitCode = $proc.ExitCode -if (-not $exitCode -eq 0) { - MyThrow("Config failed! Exited with $exitCode.") +if (-Not ($exitCode -eq 0)) { + MyThrow("Config failed! Exited with error code $exitCode.") } -Remove-Item DarknetConfig.cmake -Remove-Item DarknetConfigVersion.cmake +Remove-Item -Force -ErrorAction SilentlyContinue DarknetConfig.cmake +Remove-Item -Force -ErrorAction SilentlyContinue DarknetConfigVersion.cmake $dllfiles = Get-ChildItem ./${dllfolder}/*.dll if ($dllfiles) { Copy-Item $dllfiles .. } Set-Location .. Copy-Item cmake/Modules/*.cmake share/darknet/ +Write-Host "Build complete!" -ForegroundColor Green Pop-Location + +if ($vcpkg_root_set_by_this_script) { + $env:VCPKG_ROOT = $null +} + +$ErrorActionPreference = "SilentlyContinue" +Stop-Transcript | out-null +$ErrorActionPreference = "Continue" diff --git a/scripts/deploy-cuda.sh b/scripts/deploy-cuda.sh new file mode 100755 index 00000000000..65f173aabaf --- /dev/null +++ b/scripts/deploy-cuda.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +if [[ "$OSTYPE" == "darwin"* ]]; then + echo "Unable to deploy CUDA on macOS, please wait for a future script update" +else + if [[ $(cut -f2 <<< $(lsb_release -r)) == "18.04" ]]; then + 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 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 rm -rf /usr/local/cuda + sudo ln -s /usr/local/cuda-10.2 /usr/local/cuda + elif [[ $(cut -f2 <<< $(lsb_release -r)) == "20.04" ]]; then + 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 + sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin + sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub + sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" + 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 libcudnn8-dev + sudo rm -rf /usr/local/cuda + sudo ln -s /usr/local/cuda-11.2 /usr/local/cuda + else + echo "Unable to deploy CUDA on this Linux version, please wait for a future script update" + fi +fi diff --git a/scripts/dice_label.sh b/scripts/dice_label.sh old mode 100644 new mode 100755 diff --git a/scripts/get_coco2017.sh b/scripts/get_coco2017.sh old mode 100644 new mode 100755 diff --git a/scripts/get_coco_dataset.sh b/scripts/get_coco_dataset.sh old mode 100644 new mode 100755 diff --git a/scripts/get_imagenet_train.sh b/scripts/get_imagenet_train.sh old mode 100644 new mode 100755 diff --git a/scripts/imagenet_label.sh b/scripts/imagenet_label.sh old mode 100644 new mode 100755 diff --git a/scripts/install_OpenCV4.sh b/scripts/install_OpenCV4.sh old mode 100644 new mode 100755 diff --git a/scripts/setup.ps1 b/scripts/setup.ps1 index c5c2ae22b21..ca54dba9754 100755 --- a/scripts/setup.ps1 +++ b/scripts/setup.ps1 @@ -1,10 +1,11 @@ #!/usr/bin/env pwsh -$install_cuda = $false +param ( + [switch]$InstallCUDA = $false +) if ($null -eq (Get-Command "choco.exe" -ErrorAction SilentlyContinue)) { # Download and install Chocolatey - Set-ExecutionPolicy unrestricted -Scope CurrentUser Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) Throw "Please close and re-open powershell and then re-run setup.ps1 script" } @@ -13,23 +14,26 @@ Start-Process -FilePath "choco" -Verb runAs -ArgumentList " install -y cmake nin Start-Process -FilePath "choco" -Verb runAs -ArgumentList " install -y visualstudio2019buildtools --package-parameters `"--add Microsoft.VisualStudio.Component.VC.CoreBuildTools --includeRecommended --includeOptional --passive --locale en-US --lang en-US`"" Push-Location $PSScriptRoot -if ($install_cuda) { - & ./deploy-cuda.ps1 - $features = "full" +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" + $CUDAisAvailable = $true } else { if (-not $null -eq $env:CUDA_PATH) { - $features = "full" + $CUDAisAvailable = $true } else{ - $features = "opencv-base" + $CUDAisAvailable = $false } } -git.exe clone https://github.com/microsoft/vcpkg ../vcpkg -Set-Location ..\vcpkg -.\bootstrap-vcpkg.bat -disableMetrics -.\vcpkg.exe install darknet[${features}]:x64-windows -Pop-Location - -Write-Host "Darknet installed in $pwd\x64-windows\tools\darknet" -ForegroundColor Yellow +if ($CUDAisAvailable) { + & $PSScriptRoot/../build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -DisableInteractive -DoNotUpdateDARKNET + #& $PSScriptRoot/../build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableOPENCV_CUDA -DisableInteractive -DoNotUpdateDARKNET +} +else { + & $PSScriptRoot/../build.ps1 -UseVCPKG -EnableOPENCV -DisableInteractive -DoNotUpdateDARKNET +} diff --git a/scripts/setup.sh b/scripts/setup.sh index c33379e92ef..51d641bcea4 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,85 +1,102 @@ #!/usr/bin/env bash -## enable or disable installed components +install_tools=false +bypass_driver_installation=false -install_cuda=true +POSITIONAL=() +while [[ $# -gt 0 ]] +do +key="$1" -########################### +case $key in + -InstallCUDA|--InstallCUDA) + install_tools=true + shift + ;; + -BypassDRIVER|--BypassDRIVER) + bypass_driver_installation=true + shift + ;; + *) # unknown option + POSITIONAL+=("$1") # save it in an array for later + shift # past argument + ;; +esac +done +set -- "${POSITIONAL[@]}" # restore positional parameters +script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +echo "This script is located in $script_dir" +cd $script_dir/.. temp_folder="./temp" mkdir -p $temp_folder cd $temp_folder -sudo apt-get install cmake git ninja-build build-essential g++ - -if [ "$install_cuda" = true ] ; then +if [ "$install_tools" = true ] ; then + $script_dir/deploy-cuda.sh if [[ "$OSTYPE" == "darwin"* ]]; then - echo "Unable to provide CUDA on macOS" + echo "Unable to provide tools on macOS, please wait for a future script update or do not put -InstallCUDA command line flag to continue" else - # Download and install CUDA if [[ $(cut -f2 <<< $(lsb_release -r)) == "18.04" ]]; then - 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 apt update + sudo apt-get update + sudo apt-get install git ninja-build build-essential g++ nasm yasm + sudo apt-get install apt-transport-https ca-certificates gnupg software-properties-common wget + wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null + sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main' + wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb + sudo dpkg -i packages-microsoft-prod.deb + sudo add-apt-repository universe + 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 rm -rf /usr/local/cuda - sudo ln -s /usr/local/cuda-10.2 /usr/local/cuda + 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 + fi export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH export CUDACXX=/usr/local/cuda/bin/nvcc export CUDA_PATH=/usr/local/cuda export CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda - features="full" + cuda_is_available=true elif [[ $(cut -f2 <<< $(lsb_release -r)) == "20.04" ]]; then - sudo apt update + sudo apt-get update + sudo apt-get install git ninja-build build-essential g++ nasm yasm + sudo apt-get install apt-transport-https ca-certificates gnupg software-properties-common wget + wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null + sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main' + wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb + sudo dpkg -i packages-microsoft-prod.deb + sudo add-apt-repository universe + sudo apt-get update sudo apt-get dist-upgrade -y - #sudo apt-get install -y --no-install-recommends nvidia-cuda-dev nvidia-cuda-toolkit - sudo wget -O /etc/apt/preferences.d/cuda-repository-pin-600 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/ /" - 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 libcudnn8-dev - sudo rm -rf /usr/local/cuda - sudo ln -s /usr/local/cuda-11.2 /usr/local/cuda + 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 + fi export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH export CUDACXX=/usr/local/cuda/bin/nvcc export CUDA_PATH=/usr/local/cuda export CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda - features="full" + cuda_is_available=true else - echo "Unable to auto-install CUDA on this Linux OS" - features="opencv-base" + echo "Unable to provide tools on macOS, please wait for a future script update or do not put -InstallCUDA command line flag to continue" fi fi -else - if [[ -v CUDA_PATH ]]; then - features="full" - else - features="opencv-base" - fi fi cd .. -rm -rf $temp_folder - -if [[ ! -v VCPKG_ROOT ]]; then - git clone https://github.com/microsoft/vcpkg - cd vcpkg - ./bootstrap-vcpkg.sh -disableMetrics - export VCPKG_ROOT=$(pwd) -fi - -$VCPKG_ROOT/vcpkg install darknet[${features}] +rm -rf "$temp_folder" -if [[ "$OSTYPE" == "darwin"* ]]; then - echo "Darknet installed in $VCPKG_ROOT/installed/x64-osx/tools/darknet" +if [[ -v CUDA_PATH ]]; then + ./build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -DisableInteractive -DoNotUpdateDARKNET + #./build.ps1 -UseVCPKG -EnableOPENCV -EnableCUDA -EnableCUDNN -EnableOPENCV_CUDA -DisableInteractive -DoNotUpdateDARKNET else - echo "Darknet installed in $VCPKG_ROOT/installed/x64-linux/tools/darknet" + ./build.ps1 -UseVCPKG -EnableOPENCV -DisableInteractive -DoNotUpdateDARKNET fi diff --git a/src/csharp/CMakeLists.txt b/src/csharp/CMakeLists.txt new file mode 100644 index 00000000000..971725b227d --- /dev/null +++ b/src/csharp/CMakeLists.txt @@ -0,0 +1,19 @@ + +project(YoloWrapper LANGUAGES CSharp) +include(CSharpUtilities) + +add_library(${PROJECT_NAME} + ${PROJECT_NAME}.cs +) + +target_link_libraries(${PROJECT_NAME} PRIVATE dark) + +set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DOTNET_REFERENCES + "System" + "System.Runtime.InteropServices" +) + +install(TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION "${INSTALL_BIN_DIR}" + COMPONENT dev +) diff --git a/src/csharp/YoloWrapper.cs b/src/csharp/YoloWrapper.cs new file mode 100644 index 00000000000..52c12adb80f --- /dev/null +++ b/src/csharp/YoloWrapper.cs @@ -0,0 +1,89 @@ +using System; +using System.Runtime.InteropServices; + +namespace Darknet +{ + public class YoloWrapper : IDisposable + { + private const string YoloLibraryName = "yolo_cpp_dll.dll"; + private const int MaxObjects = 1000; + + [DllImport(YoloLibraryName, EntryPoint = "init")] + private static extern int InitializeYolo(string configurationFilename, string weightsFilename, int gpu); + + [DllImport(YoloLibraryName, EntryPoint = "detect_image")] + private static extern int DetectImage(string filename, ref BboxContainer container); + + [DllImport(YoloLibraryName, EntryPoint = "detect_mat")] + private static extern int DetectImage(IntPtr pArray, int nSize, ref BboxContainer container); + + [DllImport(YoloLibraryName, EntryPoint = "dispose")] + private static extern int DisposeYolo(); + + [StructLayout(LayoutKind.Sequential)] + public struct bbox_t + { + public UInt32 x, y, w, h; // (x,y) - top-left corner, (w, h) - width & height of bounded box + public float prob; // confidence - probability that the object was found correctly + public UInt32 obj_id; // class of object - from range [0, classes-1] + public UInt32 track_id; // tracking id for video (0 - untracked, 1 - inf - tracked object) + public UInt32 frames_counter; + public float x_3d, y_3d, z_3d; // 3-D coordinates, if there is used 3D-stereo camera + }; + + [StructLayout(LayoutKind.Sequential)] + public struct BboxContainer + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxObjects)] + public bbox_t[] candidates; + } + + public YoloWrapper(string configurationFilename, string weightsFilename, int gpu) + { + InitializeYolo(configurationFilename, weightsFilename, gpu); + } + + public void Dispose() + { + DisposeYolo(); + } + + public bbox_t[] Detect(string filename) + { + var container = new BboxContainer(); + var count = DetectImage(filename, ref container); + + return container.candidates; + } + + public bbox_t[] Detect(byte[] imageData) + { + var container = new BboxContainer(); + + var size = Marshal.SizeOf(imageData[0]) * imageData.Length; + var pnt = Marshal.AllocHGlobal(size); + + try + { + // Copy the array to unmanaged memory. + Marshal.Copy(imageData, 0, pnt, imageData.Length); + var count = DetectImage(pnt, imageData.Length, ref container); + if (count == -1) + { + throw new NotSupportedException($"{YoloLibraryName} has no OpenCV support"); + } + } + catch (Exception exception) + { + return null; + } + finally + { + // Free the unmanaged memory. + Marshal.FreeHGlobal(pnt); + } + + return container.candidates; + } + } +} diff --git a/src/region_layer.c b/src/region_layer.c index 7aa1a196f80..b7aba32e1a9 100644 --- a/src/region_layer.c +++ b/src/region_layer.c @@ -20,6 +20,10 @@ region_layer make_region_layer(int batch, int w, int h, int n, int classes, int l.batch = batch; l.h = h; l.w = w; + l.c = n*(classes + coords + 1); + l.out_w = l.w; + l.out_h = l.h; + l.out_c = l.c; l.classes = classes; l.coords = coords; l.cost = (float*)xcalloc(1, sizeof(float));