Skip to content

Commit

Permalink
Supported batch size & NET7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
maalik0786 committed Oct 4, 2022
1 parent 701ab3e commit bf47bc8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 37 deletions.
9 changes: 6 additions & 3 deletions FastYolo.TestApp/FastYolo.TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<StartupObject>FastYolo.TestApp.Program</StartupObject>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand All @@ -28,6 +27,10 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FastYolo.Tests\FastYolo.Tests.csproj" />
</ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions FastYolo.Tests/FastYolo.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
Expand Down Expand Up @@ -63,7 +62,7 @@
<PackageReference Include="ManagedCudaNet" Version="11.6.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 19 additions & 4 deletions FastYolo.Tests/YoloWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using FastYolo.Model;
using ManagedCuda;
using NUnit.Framework;
using static FastYolo.ImageConverter;
using static FastYolo.Tests.YoloConfigurationTests;
Expand Down Expand Up @@ -61,12 +62,27 @@ public unsafe void PassIntPtrFromFloatArray(bool track)
fixed (float* floatArrayPointer = &floatYoloFormatArray[0])
{
yoloItems = track
? yolo.Track(new IntPtr(floatArrayPointer), colorImage.Width, colorImage.Height, channels)
: yolo.Detect(new IntPtr(floatArrayPointer), colorImage.Width, colorImage.Height, channels);
? yolo.Track(new IntPtr(floatArrayPointer), colorImage.Width, colorImage.Height,
channels)
: yolo.Detect(new IntPtr(floatArrayPointer), colorImage.Width, colorImage.Height,
channels);
}
WriteDetectedObjectsOnConsole(yoloItems);
}

[Test]
//[Category("Slow")]
public unsafe void BenchmarkIntPtrMethod()
{
fixed (float* floatArrayPointer = &floatYoloFormatArray[0])
{
var arrayPointer = new IntPtr(floatArrayPointer);
for (var index = 0; index < 100; index++)
yolo.Detect(arrayPointer, colorImage.Width, colorImage.Height,
channels);
}
}

[TestCase(true)]
[TestCase(false)]
public unsafe void PassIntPtrFromColorImageConversion(bool track)
Expand All @@ -87,8 +103,7 @@ public void GpuCudaDevicePointer()
{
var bitmap = Image.FromFile(ImageFilename.Replace(".jpg", " out.jpg"));
var image = BitmapToColorImage((Bitmap) bitmap, channels);
ManagedCuda.CudaDeviceVariable<float>
gpuPointer = new float[image.Width * image.Height * 3];
CudaDeviceVariable<float> gpuPointer = new float[image.Width * image.Height * 3];
gpuPointer.CopyToDevice(floatYoloFormatArray);
var yoloItems = yolo.DetectCuda(gpuPointer.DevicePointer.Pointer, image.Width,
image.Height);
Expand Down
9 changes: 4 additions & 5 deletions FastYolo/FastYolo.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
<PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Configurations>Debug;Release</Configurations>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Version>11.7.2</Version>
<IsPackable>true</IsPackable>
<Version>11.7.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

Expand Down
10 changes: 5 additions & 5 deletions FastYolo/FastYolo.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>FastYolo</id>
<version>11.7.2</version>
<version>11.7.3</version>
<authors>DeltaEngine</authors>
<owners>DeltaEngine</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">Apache-2.0</license>
<projectUrl>https://github.com/maalik0786/FastYolo</projectUrl>
<iconUrl>https://res-5.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_120,w_120,f_auto,b_white,q_auto:eco/v1426438540/hacptxu3ali6vplrrhfy.png</iconUrl>
<description>
11.7.2: Shape Detection enabled, Fixed compiling error, CUDA and CUDNN are updated on X64 Linux.
11.7.3: Fixed nuget error which reference Windows on ARM64 Linux.

Fast .NET Yolo library, requires Cuda 11.7+ to be installed to work, it will use the GPU for processing (which is many times faster than using the CPU version disabled in this release). Any GPU from Maxwell upwards is support (Cuda api 5.2+, Maxwell GPUs like GTX 750 and above). Since 1.0.3 it also supports OpenCV for more advanced features like tracking, shape detection, etc.

Includes the .dll files for Yolo Darknet Wrapper, Real-Time Object Detection (AlexeyAB/darknet with additions and fixes here: https://github.com/maalik0786/darknet), including cudnn64_8.dll, opencv_world460.dll and pthreadVC2.dll as needed by the yolo_cpp_dll implementation. Tested on Windows x64, Linux AMD64 and ARM64 (Jetson Nano/Xavier).
Includes the .dll files for Yolo Darknet Wrapper, Real-Time Object Detection (AlexeyAB/darknet with additions and fixes here: https://github.com/maalik0786/darknet), including cudnn64_8.dll, opencv_world460.dll and pthreadVC2.dll as needed by the yolo_cpp_dll implementation. Tested on Windows x64, Linux AMD64 and ARM64 (Jetson Orin/Xavier).

v1.0.4 adds resize detection and will output a warning in the console and allow to check via the CheckIfImageWasResized method if any resize happened. Resizing should not be done dynamically in the darknet library, extra memory would be required and it is obviously much slower than if we already have 416x416 data (or whatever the configuration defines).
</description>
<repository url="https://github.com/maalik0786/FastYolo" />
<dependencies>
<group targetFramework=".NET6.0">
<group targetFramework=".NET7.0">
<dependency id="System.Drawing.Common" version="5.0.3" exclude="Build,Analyzers" />
</group>
</dependencies>
Expand All @@ -39,6 +39,6 @@ v1.0.4 adds resize detection and will output a warning in the console and allow
<file src="libdarknet_arm.so" target="build" />
<file src="libpthread_arm.so" target="build" />
<file src="FastYolo.targets" target="build" />
<file src="bin\Release\net6.0\FastYolo.dll" target="lib/net6.0" />
<file src="bin\Release\net7.0\FastYolo.dll" target="lib/net7.0" />
</files>
</package>
24 changes: 12 additions & 12 deletions FastYolo/FastYolo.targets
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="libdarknet_amd.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">
<ItemGroup Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">
<None Include="libdarknet_amd.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libdarknet_amd.so</TargetPath>
</None>
<None Include="libpthread_amd.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">
<None Include="libpthread_amd.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libpthread_amd.so</TargetPath>
</None>
<None Include="libopencv_world.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='X64'">
<None Include="libopencv_world.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libopencv_world.so</TargetPath>
</None>
</ItemGroup>

<ItemGroup>
<None Include="libdarknet_arm.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='Arm64'">
<ItemGroup Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='Arm64'">
<None Include="libdarknet_arm.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libdarknet_arm.so</TargetPath>
</None>
<None Include="libpthread_arm.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='Arm64'">
<None Include="libpthread_arm.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libpthread_arm.so</TargetPath>
</None>
<None Include="libopencv_world.so" Condition=" '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)'=='Arm64'">
<None Include="libopencv_world.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>libopencv_world.so</TargetPath>
</None>
</ItemGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\pthreadVC2.dll" Condition=" '$(OS)' == 'Windows_NT' ">
<ItemGroup Condition=" '$(OS)' == 'Windows_NT' ">
<None Include="$(MSBuildThisFileDirectory)\pthreadVC2.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>pthreadVC2.dll</TargetPath>
</None >
<None Include="$(MSBuildThisFileDirectory)\opencv_world460.dll" Condition=" '$(OS)' == 'Windows_NT' ">
<None Include="$(MSBuildThisFileDirectory)\opencv_world460.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>opencv_world460.dll</TargetPath>
</None >
<None Include="$(MSBuildThisFileDirectory)\yolo_cpp_dll.dll" Condition=" '$(OS)' == 'Windows_NT' ">
<None Include="$(MSBuildThisFileDirectory)\yolo_cpp_dll.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>yolo_cpp_dll.dll</TargetPath>
</None>
Expand Down
10 changes: 5 additions & 5 deletions FastYolo/YoloWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static extern int TrackObjectsGpu(IntPtr pArray, int width, int height,
private static extern int GetDeviceName(int gpu, StringBuilder deviceName);

// ReSharper disable once MethodTooLong
private void Initialize(string configurationFilename, string weightsFilename, int gpu = 0)
private void Initialize(string configurationFilename, string weightsFilename, int gpu = 0, int batchSize=1)
{
if (IntPtr.Size != 8)
throw new NotSupportedException("Only 64-bit processes are supported");
Expand Down Expand Up @@ -154,7 +154,7 @@ private void Initialize(string configurationFilename, string weightsFilename, in
var deviceName = new StringBuilder();
GetDeviceName(gpu, deviceName);
GraphicDeviceName = deviceName.ToString();
InitializeYoloGpu(configurationFilename, weightsFilename, gpu);
InitializeYoloGpu(configurationFilename, weightsFilename, gpu, batchSize);
}

public IEnumerable<YoloItem> Detect(string filepath)
Expand Down Expand Up @@ -183,9 +183,9 @@ public IEnumerable<YoloItem> Detect(byte[] imageData)
if (count == -1)
throw new NotSupportedException($"{YoloGpuDllFilename} has no OpenCV support");
}
catch (Exception)
catch
{
return null!;
return Array.Empty<YoloItem>();
}
finally
{
Expand Down Expand Up @@ -221,7 +221,7 @@ public IEnumerable<YoloItem> DetectCuda(IntPtr sizeTPointer, int width, int heig
{
DetectObjectsCuda(sizeTPointer, width, height, channels, ref container);
}
catch (Exception)
catch
{
var networkWidth = GetDetectorNetworkWidth();
var networkHeight = GetDetectorNetworkHeight();
Expand Down

0 comments on commit bf47bc8

Please sign in to comment.