Update NN tools

pull/1/head
Ogoun 3 years ago
parent a7668a79e2
commit 9e84521e21

@ -210,6 +210,8 @@ For I=Maxes[0] to Maxes[1]
*/ */
public int CuttOff() public int CuttOff()
{
if (Values.Length > 1)
{ {
var grad = new int[Values.Length]; var grad = new int[Values.Length];
grad[0] = 0; grad[0] = 0;
@ -271,6 +273,7 @@ For I=Maxes[0] to Maxes[1]
} }
return cutoff; return cutoff;
} }
}
return -1; return -1;
} }

@ -5,7 +5,7 @@ namespace ZeroLevel.HNSW
public sealed class NSWOptions<TItem> public sealed class NSWOptions<TItem>
{ {
/// <summary> /// <summary>
/// Mox node connections on Layer /// Max node connections on Layer
/// </summary> /// </summary>
public readonly int M; public readonly int M;
/// <summary> /// <summary>

@ -81,10 +81,10 @@ namespace ZeroLevel.HNSW
} }
public void Deserialize(IBinaryReader reader) public void Deserialize(IBinaryReader reader)
{ {
if (reader.ReadBoolean() != false) /*if (reader.ReadBoolean() != false)
{ {
throw new InvalidOperationException("Incompatible format"); throw new InvalidOperationException("Incompatible format");
} }*/
_set.Clear(); _set.Clear();
_set = null; _set = null;
var count = reader.ReadInt32(); var count = reader.ReadInt32();

@ -2,29 +2,29 @@
namespace ZeroLevel.HNSW namespace ZeroLevel.HNSW
{ {
internal class VisitedBitSet public class VisitedBitSet
{ {
// bit map // bit map
private int[] Buffer; private int[] Buffer;
internal VisitedBitSet(int nodesCount, int M) public VisitedBitSet(int nodesCount, int M)
{ {
Buffer = new int[(nodesCount >> 5) + M + 1]; Buffer = new int[(nodesCount >> 5) + M + 1];
} }
internal bool Contains(int nodeId) public bool Contains(int nodeId)
{ {
int carrier = Buffer[nodeId >> 5]; int carrier = Buffer[nodeId >> 5];
return ((1 << (nodeId & 31)) & carrier) != 0; return ((1 << (nodeId & 31)) & carrier) != 0;
} }
internal void Add(int nodeId) public void Add(int nodeId)
{ {
int mask = 1 << (nodeId & 31); int mask = 1 << (nodeId & 31);
Buffer[nodeId >> 5] |= mask; Buffer[nodeId >> 5] |= mask;
} }
internal void Clear() public void Clear()
{ {
Array.Clear(Buffer, 0, Buffer.Length); Array.Clear(Buffer, 0, Buffer.Length);
} }

@ -0,0 +1,17 @@
namespace ZeroLevel.NN.Architectures
{
internal class Yolo5
: SSDNN
{
public Yolo5(string modelPath, int width, int height)
: base(modelPath)
{
this.InputH = height;
this.InputW = width;
}
public int InputW { private set; get; }
public int InputH { private set; get; }
}
}

@ -0,0 +1,133 @@
namespace ZeroLevel.NN.Models
{
public class Cluster<T>
{
private int _key;
private readonly List<T> _points = new List<T>();
public T this[int index] => _points[index];
public IReadOnlyList<T> Points => _points;
public int Key { get { return _key; } set { _key = value; } }
public Cluster()
{
}
public Cluster(T point)
{
_points.Add(point);
}
public Cluster(IEnumerable<T> points)
{
_points.AddRange(points);
}
public void Add(T point)
{
_points.Add(point);
}
public void Remove(T point)
{
_points.Remove(point);
}
/*
public bool IsNeighbor(T feature,
Func<T, float[]> embeddingFunction,
Func<float[], float[], double> similarityFunction,
float threshold,
float clusterThreshold)
{
if (_points.Count == 0) return true;
if (_points.Count == 1)
{
var similarity = similarityFunction(embeddingFunction(feature), embeddingFunction(_points[0]));
return similarity >= threshold;
}
var clusterNearestElementsCount = 0;
foreach (var f in _points)
{
var similarity = similarityFunction(embeddingFunction(feature), embeddingFunction(f));
if (similarity >= threshold)
{
clusterNearestElementsCount++;
}
}
var clusterToFaceScore = (float)clusterNearestElementsCount / (float)_points.Count;
return clusterToFaceScore > clusterThreshold;
}
*/
public bool IsNearest(T feature,
Func<T, T, double> distanceFunction,
double maxDistance)
{
if (_points.Count == 0) return true;
if (_points.Count == 1)
{
var distance = distanceFunction(feature, _points[0]);
return distance <= maxDistance;
}
foreach (var f in _points)
{
var distance = distanceFunction(feature, f);
if (distance > maxDistance)
{
return false;
}
}
return true;
}
public double MinimalDistance(T feature,
Func<T, T, double> distanceFunction)
{
if (_points.Count == 0) return int.MaxValue;
var min = distanceFunction(feature, _points[0]);
if (_points.Count == 1)
{
return min;
}
for (int i = 0; i<_points.Count; i++)
{
var distance = distanceFunction(feature, _points[i]);
if (distance < min)
{
min = distance;
}
}
return min;
}
/*
public bool IsNeighborCluster(Cluster<T> cluster,
Func<T, float[]> embeddingFunction,
Func<float[], float[], double> similarityFunction,
float threshold,
float clusterThreshold)
{
if (_points.Count == 0) return true;
if (_points.Count == 1 && cluster.IsNeighbor(_points[0], embeddingFunction, similarityFunction, threshold, clusterThreshold))
{
return true;
}
var clusterNearestElementsCount = 0;
foreach (var f in _points)
{
if (cluster.IsNeighbor(f, embeddingFunction, similarityFunction, threshold, clusterThreshold))
{
clusterNearestElementsCount++;
}
}
var localCount = _points.Count;
var remoteCount = cluster._points.Count;
var localIntersection = (float)clusterNearestElementsCount / (float)localCount;
var remoteIntersection = (float)clusterNearestElementsCount / (float)remoteCount;
var score = Math.Max(localIntersection, remoteIntersection);
return score > clusterThreshold;
}
*/
public void Merge(Cluster<T> other)
{
this._points.AddRange(other.Points);
}
}
}

@ -11,9 +11,26 @@ namespace ZeroLevel.NN
{ {
private readonly InferenceSession _session; private readonly InferenceSession _session;
public SSDNN(string path) public SSDNN(string modelPath, bool gpu = false)
{ {
_session = new InferenceSession(path); if (gpu)
{
try
{
var so = SessionOptions.MakeSessionOptionWithCudaProvider(0);
so.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL;
_session = new InferenceSession(modelPath, so);
}
catch (Exception ex)
{
Log.Error(ex, "Fault create InferenceSession with CUDA");
_session = new InferenceSession(modelPath);
}
}
else
{
_session = new InferenceSession(modelPath);
}
} }
protected void Extract(IDictionary<string, Tensor<float>> input, Action<IDictionary<string, Tensor<float>>> inputHandler) protected void Extract(IDictionary<string, Tensor<float>> input, Action<IDictionary<string, Tensor<float>>> inputHandler)

@ -3,14 +3,36 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>disable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<DebugType>embedded</DebugType>
<ErrorReport>none</ErrorReport>
<Version>1.0.0.0</Version>
<Company>Ogoun</Company>
<Authors>Ogoun</Authors>
<Copyright>Copyright Ogoun 2022</Copyright>
<PackageIcon>zero.png</PackageIcon>
<PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl>
<RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>False</Optimize>
</PropertyGroup>
<ItemGroup>
<None Include="..\zero.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Aurigma.GraphicsMill.Core.x64" Version="10.5.308" /> <PackageReference Include="Aurigma.GraphicsMill.Core.x64" Version="10.5.308" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.10.0" /> <PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.10.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.Managed" Version="1.10.0" /> <PackageReference Include="Microsoft.ML.OnnxRuntime.Managed" Version="1.10.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.0.0" /> <PackageReference Include="SixLabors.ImageSharp" Version="2.1.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -6,21 +6,22 @@
</Description> </Description>
<Authors>ogoun</Authors> <Authors>ogoun</Authors>
<Company>ogoun</Company> <Company>ogoun</Company>
<AssemblyVersion>3.3.6.1</AssemblyVersion> <AssemblyVersion>3.3.6.2</AssemblyVersion>
<PackageReleaseNotes>New ConcurrentHashSet implementation</PackageReleaseNotes> <PackageReleaseNotes>Update distance metrics</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl> <PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl>
<Copyright>Copyright Ogoun 2022</Copyright> <Copyright>Copyright Ogoun 2022</Copyright>
<PackageLicenseUrl></PackageLicenseUrl> <PackageLicenseUrl></PackageLicenseUrl>
<PackageIconUrl></PackageIconUrl> <PackageIconUrl></PackageIconUrl>
<RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl> <RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl>
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<Version>3.3.6.1</Version> <Version>3.3.6.2</Version>
<FileVersion>3.3.6.1</FileVersion> <FileVersion>3.3.6.2</FileVersion>
<Platforms>AnyCPU;x64;x86</Platforms> <Platforms>AnyCPU;x64;x86</Platforms>
<PackageIcon>zero.png</PackageIcon> <PackageIcon>zero.png</PackageIcon>
<DebugType>full</DebugType> <DebugType>none</DebugType>
<ErrorReport>none</ErrorReport> <ErrorReport>none</ErrorReport>
<ApplicationIcon>zero.ico</ApplicationIcon> <ApplicationIcon>zero.ico</ApplicationIcon>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

Loading…
Cancel
Save

Powered by TurnKey Linux.