Update NN tools

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

@ -211,65 +211,68 @@ For I=Maxes[0] to Maxes[1]
public int CuttOff()
{
var grad = new int[Values.Length];
grad[0] = 0;
grad[1] = 0;
for (int k = 2; k < Values.Length; k++)
if (Values.Length > 1)
{
grad[k - 1] = Values[k] - Values[k - 1];
}
var modes = 0;
var window = 0;
var sign = 1;
var sum = 0;
var max = 0;
var maxInd = 0;
var maxes = new List<int>();
do
{
maxes.Clear();
window++;
modes = 0;
sum = 0;
for (int i = 0; i < grad.Length; i += window)
var grad = new int[Values.Length];
grad[0] = 0;
grad[1] = 0;
for (int k = 2; k < Values.Length; k++)
{
grad[k - 1] = Values[k] - Values[k - 1];
}
var modes = 0;
var window = 0;
var sign = 1;
var sum = 0;
var max = 0;
var maxInd = 0;
var maxes = new List<int>();
do
{
sum = grad[i];
max = Values[i];
maxInd = i;
for (var w = 1; w < window && (i + w) < grad.Length; w++)
maxes.Clear();
window++;
modes = 0;
sum = 0;
for (int i = 0; i < grad.Length; i += window)
{
sum += grad[i + w];
if (Values[i + w] > max)
sum = grad[i];
max = Values[i];
maxInd = i;
for (var w = 1; w < window && (i + w) < grad.Length; w++)
{
max = Values[i + w];
maxInd = i + w;
sum += grad[i + w];
if (Values[i + w] > max)
{
max = Values[i + w];
maxInd = i + w;
}
}
if (sum > 0 && sign < 0)
{
sign = 1;
}
else if (sum < 0 && sign > 0)
{
modes++;
maxes.Add(maxInd);
sign = -1;
}
}
if (sum > 0 && sign < 0)
{
sign = 1;
}
else if (sum < 0 && sign > 0)
{
modes++;
maxes.Add(maxInd);
sign = -1;
}
}
} while (modes > 2);
if (modes == 2)
{
var cutoff = maxes[0];
var min = Values[cutoff];
for (int i = maxes[0] + 1; i < maxes[1]; i++)
} while (modes > 2);
if (modes == 2)
{
if (Values[i] < min)
var cutoff = maxes[0];
var min = Values[cutoff];
for (int i = maxes[0] + 1; i < maxes[1]; i++)
{
cutoff = i;
min = Values[i];
if (Values[i] < min)
{
cutoff = i;
min = Values[i];
}
}
return cutoff;
}
return cutoff;
}
return -1;
}

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

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

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

@ -3,14 +3,36 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<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 Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>False</Optimize>
</PropertyGroup>
<ItemGroup>
<None Include="..\zero.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Aurigma.GraphicsMill.Core.x64" Version="10.5.308" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" 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>

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

Loading…
Cancel
Save

Powered by TurnKey Linux.