diff --git a/Demos/Converter/App.config b/Demos/Converter/App.config deleted file mode 100644 index 8d23437..0000000 --- a/Demos/Converter/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/Demos/Converter/CommonLib/ConfigLoader.cs b/Demos/Converter/CommonLib/ConfigLoader.cs index 3760738..7275017 100644 --- a/Demos/Converter/CommonLib/ConfigLoader.cs +++ b/Demos/Converter/CommonLib/ConfigLoader.cs @@ -1,6 +1,4 @@ using Newtonsoft.Json; -using System.Collections.Generic; -using System.IO; namespace Converter.CommonLib { diff --git a/Demos/Converter/CommonLib/Sentencepiece.cs b/Demos/Converter/CommonLib/Sentencepiece.cs index 679e1a2..134e9cb 100644 --- a/Demos/Converter/CommonLib/Sentencepiece.cs +++ b/Demos/Converter/CommonLib/Sentencepiece.cs @@ -1,5 +1,4 @@ using ProtoBuf; -using System.Collections.Generic; namespace Converter.CommonLib { diff --git a/Demos/Converter/CommonTensorConverterDemo.cs b/Demos/Converter/CommonTensorConverterDemo.cs index 8bf6cce..ca358b8 100644 --- a/Demos/Converter/CommonTensorConverterDemo.cs +++ b/Demos/Converter/CommonTensorConverterDemo.cs @@ -1,8 +1,4 @@ using GGMLSharp; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; namespace Converter { diff --git a/Demos/Converter/Converter.csproj b/Demos/Converter/Converter.csproj index 15b22cc..3e505a5 100644 --- a/Demos/Converter/Converter.csproj +++ b/Demos/Converter/Converter.csproj @@ -1,91 +1,31 @@ - - - + + - Debug - AnyCPU - {422A11E6-EFB2-4486-85A8-3B2CC9692FA8} Exe - Converter - Converter - v4.6.2 - 512 - true - true - - - + net8.0 + enable + enable x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + - - - - - - - - + + + + - - - - - - - - - - - + + + - - + PreserveNewest - + PreserveNewest - - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - - - {e48f9d97-86ef-42cd-be41-46365026bee7} - ModelLoader - - - - - 13.0.3 - - - 3.2.30 - - - 4.3.0 - - - - - \ No newline at end of file + + diff --git a/Demos/Converter/Pickle/Llama3.cs b/Demos/Converter/Pickle/Llama3.cs index 2ca0ea4..e585641 100644 --- a/Demos/Converter/Pickle/Llama3.cs +++ b/Demos/Converter/Pickle/Llama3.cs @@ -1,10 +1,6 @@ using Converter.Abstractions; using Converter.CommonLib; using GGMLSharp; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using System.Runtime.InteropServices; using System.Text.RegularExpressions; diff --git a/Demos/Converter/Program.cs b/Demos/Converter/Program.cs index 0c9101e..1c1f719 100644 --- a/Demos/Converter/Program.cs +++ b/Demos/Converter/Program.cs @@ -9,12 +9,12 @@ static void Main(string[] args) // Tested Qwen model link is from https://huggingface.co/Qwen/Qwen1.5-1.8B-Chat // Tested LLama3 model link is from https://huggingface.co/bineric/NorskGPT-Llama3-8b/tree/main - //new Safetensors.Qwen().Convert(@".\models\qwen", "qwen.gguf"); - //new Safetensors.Llama().Convert(@".\models\open_llama", "open_llama.gguf"); - //new Pickle.Llama3().Convert(@".\models\NorskGPT-Llama3-8b", "NorskGPT-Llama3-8b.gguf"); + //new Safetensors.Qwen().Convert(@"./models/qwen", "qwen.gguf"); + //new Safetensors.Llama().Convert(@"./models/open_llama", "open_llama.gguf"); + //new Pickle.Llama3().Convert("./models/NorskGPT-Llama3-8b", "NorskGPT-Llama3-8b.gguf"); - new CommonTensorConverterDemo().ConvertToGguf(@".\Assets\model.pt", "model.gguf", CommonTensorConverterDemo.ModelType.Pickle, true); - //new CommonTensorConverterDemo().ConvertToGguf(@".\Assets\taesd.safetensors", "taesd.gguf", CommonTensorConverterDemo.ModelType.Safetensors, false); + new CommonTensorConverterDemo().ConvertToGguf("./Assets/model.pt", "model.gguf", CommonTensorConverterDemo.ModelType.Pickle, true); + //new CommonTensorConverterDemo().ConvertToGguf("./Assets/taesd.safetensors", "taesd.gguf", CommonTensorConverterDemo.ModelType.Safetensors, false); } } diff --git a/Demos/Converter/Properties/AssemblyInfo.cs b/Demos/Converter/Properties/AssemblyInfo.cs deleted file mode 100644 index f205da6..0000000 --- a/Demos/Converter/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("Converter")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Converter")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("422a11e6-efb2-4486-85a8-3b2cc9692fa8")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/Converter/Safetensors/Llama.cs b/Demos/Converter/Safetensors/Llama.cs index edc960c..a40dd57 100644 --- a/Demos/Converter/Safetensors/Llama.cs +++ b/Demos/Converter/Safetensors/Llama.cs @@ -2,10 +2,6 @@ using Converter.CommonLib; using GGMLSharp; using ProtoBuf; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using static GGMLSharp.Structs; diff --git a/Demos/Converter/Safetensors/Qwen.cs b/Demos/Converter/Safetensors/Qwen.cs index 1ee6f0e..fda49e9 100644 --- a/Demos/Converter/Safetensors/Qwen.cs +++ b/Demos/Converter/Safetensors/Qwen.cs @@ -1,10 +1,6 @@ using Converter.Abstractions; using Converter.CommonLib; using GGMLSharp; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using System.Runtime.InteropServices; using System.Text.RegularExpressions; diff --git a/Demos/MNIST_CNN/App.config b/Demos/MNIST_CNN/App.config deleted file mode 100644 index 8d23437..0000000 --- a/Demos/MNIST_CNN/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/Demos/MNIST_CNN/Program.cs b/Demos/MNIST_CNN/Program.cs index ee5ca79..0a45fbe 100644 --- a/Demos/MNIST_CNN/Program.cs +++ b/Demos/MNIST_CNN/Program.cs @@ -1,8 +1,4 @@ using GGMLSharp; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using static GGMLSharp.Structs; namespace MNIST_CNN @@ -11,8 +7,8 @@ internal class Program { static void Main(string[] args) { - Model model = LoadModel(@".\Assets\mnist-cnn-model.gguf"); - byte[] bytes = File.ReadAllBytes(@".\Assets\image.raw"); + Model model = LoadModel("./Assets/mnist-cnn-model.gguf"); + byte[] bytes = File.ReadAllBytes("./Assets/image.raw"); Console.WriteLine("The image is:"); for (int i = 0; i < 28; i++) { diff --git a/Demos/MNIST_CNN/Properties/AssemblyInfo.cs b/Demos/MNIST_CNN/Properties/AssemblyInfo.cs deleted file mode 100644 index 19b3c53..0000000 --- a/Demos/MNIST_CNN/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("MNIST_CNN")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("MNIST_CNN")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("9d9440d2-eead-42d6-ba7b-d9e1658575ed")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/MNIST_CNN/mnist_cnn.csproj b/Demos/MNIST_CNN/mnist_cnn.csproj index 3b712b1..c66ae05 100644 --- a/Demos/MNIST_CNN/mnist_cnn.csproj +++ b/Demos/MNIST_CNN/mnist_cnn.csproj @@ -1,69 +1,24 @@ - - - + + - Debug - AnyCPU - {9D9440D2-EEAD-42D6-BA7B-D9E1658575ED} Exe - MNIST_CNN - MNIST_CNN - v4.6.2 - 512 - true - true - - - + net8.0 + enable + enable x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + - - - - - - - - + + - - - - - - + PreserveNewest - - - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - - - - + PreserveNewest - - + - - \ No newline at end of file + + diff --git a/Demos/MNIST_CPU/App.config b/Demos/MNIST_CPU/App.config deleted file mode 100644 index 8d23437..0000000 --- a/Demos/MNIST_CPU/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/Demos/MNIST_CPU/Program.cs b/Demos/MNIST_CPU/Program.cs index e4b0ee5..1440915 100644 --- a/Demos/MNIST_CPU/Program.cs +++ b/Demos/MNIST_CPU/Program.cs @@ -1,9 +1,5 @@ using GGMLSharp; using ModelLoader; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using static GGMLSharp.Structs; namespace MNIST_CPU @@ -12,8 +8,8 @@ internal class Program { static void Main(string[] args) { - Model model = LoadModel(@".\Assets\mnist_model.state_dict"); - byte[] bytes = File.ReadAllBytes(@".\Assets\image.raw"); + Model model = LoadModel("./Assets/mnist_model.state_dict"); + byte[] bytes = File.ReadAllBytes("./Assets/image.raw"); Console.WriteLine("The image is:"); for (int i = 0; i < 28; i++) { diff --git a/Demos/MNIST_CPU/Properties/AssemblyInfo.cs b/Demos/MNIST_CPU/Properties/AssemblyInfo.cs deleted file mode 100644 index 2b3dac4..0000000 --- a/Demos/MNIST_CPU/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("MNIST_CPU")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("MNIST_CPU")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("725e0684-c0cd-4ed8-826a-45b4a3c0b564")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/MNIST_CPU/mnist_cpu.csproj b/Demos/MNIST_CPU/mnist_cpu.csproj index f53c0a0..cc081de 100644 --- a/Demos/MNIST_CPU/mnist_cpu.csproj +++ b/Demos/MNIST_CPU/mnist_cpu.csproj @@ -1,73 +1,25 @@ - - - + + - Debug - AnyCPU - {725E0684-C0CD-4ED8-826A-45B4A3C0B564} Exe - MNIST_CPU - MNIST_CPU - v4.6.2 - 512 - true - true - - - + net8.0 + enable + enable x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + - - - - - - - - + + + - - - - - - + PreserveNewest - - - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - - - {e48f9d97-86ef-42cd-be41-46365026bee7} - ModelLoader - - - - + PreserveNewest - - + - - \ No newline at end of file + + diff --git a/Demos/MNIST_Train/App.config b/Demos/MNIST_Train/App.config deleted file mode 100644 index b50c74f..0000000 --- a/Demos/MNIST_Train/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Demos/MNIST_Train/Program.cs b/Demos/MNIST_Train/Program.cs index 78a1a30..31494d8 100644 --- a/Demos/MNIST_Train/Program.cs +++ b/Demos/MNIST_Train/Program.cs @@ -1,8 +1,4 @@ using GGMLSharp; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using static GGMLSharp.Structs; namespace MNIST_Train @@ -17,7 +13,7 @@ static void Main(string[] args) // Step count int sp = 300; - mnist_data[] datas = LoadData(@".\Assets\t10k-images.idx3-ubyte", @".\Assets\t10k-labels-idx1-ubyte"); + mnist_data[] datas = LoadData("./Assets/t10k-images.idx3-ubyte", "./Assets/t10k-labels-idx1-ubyte"); SafeGGmlContext context = new SafeGGmlContext(); @@ -154,7 +150,7 @@ private static void TestModel() SafeGGmlGraph gf = context.CustomNewGraph(); gf.BuildForwardExpend(probs); - mnist_data[] datas = LoadData(@".\Assets\t10k-images.idx3-ubyte", @".\Assets\t10k-labels-idx1-ubyte"); + mnist_data[] datas = LoadData("./Assets/t10k-images.idx3-ubyte", "./Assets/t10k-labels-idx1-ubyte"); mnist_data data = datas[5008]; input.SetData(data.data); diff --git a/Demos/MNIST_Train/Properties/AssemblyInfo.cs b/Demos/MNIST_Train/Properties/AssemblyInfo.cs deleted file mode 100644 index 75e533f..0000000 --- a/Demos/MNIST_Train/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("MNIST_Train")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("MNIST_Train")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("a283b841-d3c7-4a82-b675-35f14b1d263a")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/MNIST_Train/mnist_train.csproj b/Demos/MNIST_Train/mnist_train.csproj index 500b9e1..1d16379 100644 --- a/Demos/MNIST_Train/mnist_train.csproj +++ b/Demos/MNIST_Train/mnist_train.csproj @@ -1,66 +1,28 @@ - - - + + - Debug - AnyCPU - {A283B841-D3C7-4A82-B675-35F14B1D263A} Exe - MNIST_Train - MNIST_Train - v4.6.2 - 512 - true - true - - + net8.0 + enable + enable x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + - - - - - - - - - - - - + + - - + PreserveNewest - + PreserveNewest + - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - + - - \ No newline at end of file + + diff --git a/Demos/Magika/App.config b/Demos/Magika/App.config deleted file mode 100644 index 8d23437..0000000 --- a/Demos/Magika/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/Demos/Magika/Program.cs b/Demos/Magika/Program.cs index f26b0d6..66856c1 100644 --- a/Demos/Magika/Program.cs +++ b/Demos/Magika/Program.cs @@ -1,8 +1,4 @@ using GGMLSharp; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; namespace Magika { @@ -95,7 +91,7 @@ private static SafeGGmlTensor CheckedGetTensor(SafeGGmlContext ctx, string name) private static MagikaModel LoadModel(string fname) { MagikaModel model = new MagikaModel(); - SafeGGufContext ggufContext = SafeGGufContext.InitFromFile(@"./Assets/magika.gguf", model.context, true); + SafeGGufContext ggufContext = SafeGGufContext.InitFromFile("./Assets/magika.gguf", model.context, true); model.backend = SafeGGmlBackend.CpuInit(); // init device 0 @@ -299,10 +295,10 @@ struct result static void Main(string[] args) { - MagikaModel model = LoadModel(@".\Assets\magika.gguf"); + MagikaModel model = LoadModel("./Assets/magika.gguf"); Console.WriteLine("Loaded model"); - float[] result = Eval(model, @".\Assets\test"); + float[] result = Eval(model, "./Assets/test"); List results = new List(); for (int i = 0; i < result.Length; i++) { diff --git a/Demos/Magika/Properties/AssemblyInfo.cs b/Demos/Magika/Properties/AssemblyInfo.cs deleted file mode 100644 index ea74d10..0000000 --- a/Demos/Magika/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("Magika")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Magika")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("0880cc0e-50e0-4c51-a07d-5d8336b643a1")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/Magika/magika.csproj b/Demos/Magika/magika.csproj index 1b0b5db..9700f4d 100644 --- a/Demos/Magika/magika.csproj +++ b/Demos/Magika/magika.csproj @@ -1,66 +1,24 @@ - - - + + - Debug - AnyCPU - {0880CC0E-50E0-4C51-A07D-5D8336B643A1} Exe - Magika - Magika - v4.6.2 - 512 - true - true - - - + net8.0 + enable + enable x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + - - - - - - - - - - - - + + - - + PreserveNewest - + PreserveNewest - - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - - - - \ No newline at end of file + + diff --git a/Demos/ModelLoader/IModelLoader.cs b/Demos/ModelLoader/IModelLoader.cs index adccf26..0d7154a 100644 --- a/Demos/ModelLoader/IModelLoader.cs +++ b/Demos/ModelLoader/IModelLoader.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace ModelLoader +namespace ModelLoader { public interface IModelLoader { diff --git a/Demos/ModelLoader/ModelLoader.csproj b/Demos/ModelLoader/ModelLoader.csproj index 3425f8b..15e0195 100644 --- a/Demos/ModelLoader/ModelLoader.csproj +++ b/Demos/ModelLoader/ModelLoader.csproj @@ -1,69 +1,18 @@ - - - + + - Debug - AnyCPU - {E48F9D97-86EF-42CD-BE41-46365026BEE7} - Library - Properties - ModelLoader - ModelLoader - v4.6.2 - 512 - true - + net8.0 + + enable + enable - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - + - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - + + - - 13.0.3 - + - + \ No newline at end of file diff --git a/Demos/ModelLoader/PickleLoader.cs b/Demos/ModelLoader/PickleLoader.cs index d01f04a..25a887e 100644 --- a/Demos/ModelLoader/PickleLoader.cs +++ b/Demos/ModelLoader/PickleLoader.cs @@ -1,10 +1,6 @@ using GGMLSharp; -using System; -using System.Collections.Generic; using System.Collections.ObjectModel; -using System.IO; using System.IO.Compression; -using System.Linq; namespace ModelLoader { diff --git a/Demos/ModelLoader/Properties/AssemblyInfo.cs b/Demos/ModelLoader/Properties/AssemblyInfo.cs deleted file mode 100644 index 7ed8bf4..0000000 --- a/Demos/ModelLoader/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("ModelLoader")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("ModelLoader")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("e48f9d97-86ef-42cd-be41-46365026bee7")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/ModelLoader/SafetensorsLoader.cs b/Demos/ModelLoader/SafetensorsLoader.cs index 4ae9377..25bd063 100644 --- a/Demos/ModelLoader/SafetensorsLoader.cs +++ b/Demos/ModelLoader/SafetensorsLoader.cs @@ -1,8 +1,4 @@ using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using System.Text; using static GGMLSharp.Structs; diff --git a/Demos/SAM/App.config b/Demos/SAM/App.config deleted file mode 100644 index 8d23437..0000000 --- a/Demos/SAM/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/Demos/SAM/Program.cs b/Demos/SAM/Program.cs index aa1589a..5a5867e 100644 --- a/Demos/SAM/Program.cs +++ b/Demos/SAM/Program.cs @@ -1,11 +1,6 @@ using GGMLSharp; using ModelLoader; -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; -using System.IO; -using System.Linq; +using SkiaSharp; using System.Runtime.InteropServices; using static GGMLSharp.Structs; @@ -24,7 +19,7 @@ static void Main(string[] args) { IouThreshold = 0.9f, StabilityScoreThreshold = 0.9f, - Point = new PointF(274.0f, 244.0f), + Point = new SKPoint(274.0f, 244.0f), ModelPath = @"./Assets/sam_vit_b_01ec64.pth", ImageInputPath = @"./Assets/example.jpg", Threads = 16, @@ -361,7 +356,7 @@ class SamParams public float StabilityScoreOffset = 1.0f; public float Eps = 1e-6f; public float EpsDecoderTransformer = 1e-5f; - public PointF Point = new PointF(414.375f, 162.796875f); + public SKPoint Point = new SKPoint(414.375f, 162.796875f); }; static void DisconnectNodeFromGraph(SafeGGmlTensor t) @@ -376,7 +371,7 @@ static void DisconnectNodeFromGraph(SafeGGmlTensor t) static void GraphComputeHelper(SafeGGmlGraph graph, int threads) { ulong mem_size = Common.TensorOverheadLength * Structs.GGML_DEFAULT_GRAPH_SIZE * (ulong)graph.NodeCount + Common.GraphOverheadLength; - SafeGGmlContext context = new SafeGGmlContext(IntPtr.Zero, mem_size, false); + using SafeGGmlContext context = new SafeGGmlContext(IntPtr.Zero, mem_size, false); graph.ComputeWithGGmlContext(context, threads); } @@ -447,15 +442,55 @@ static void SamCos(SafeGGmlTensor dst, SafeGGmlTensor src, int ith, int nth, Int static SamImageU8 LoadImageFromFile(string fname) { - Bitmap bitmap = new Bitmap(fname); + using var bitmap = SKBitmap.Decode(fname); + if (bitmap == null) + { + throw new FileNotFoundException("Failed to load image", fname); + } + + // Ensure the bitmap is in a known 8-bit format, like Rgba8888 or Bgra8888 + if (bitmap.ColorType != SKColorType.Rgba8888 && bitmap.ColorType != SKColorType.Bgra8888) + { + // Convert to a usable format if necessary + var temp = new SKBitmap(bitmap.Width, bitmap.Height, SKColorType.Rgba8888, SKAlphaType.Opaque); + using var canvas = new SKCanvas(temp); + canvas.DrawBitmap(bitmap, 0, 0); + bitmap.Dispose(); + } + var currentBitmap = bitmap.ColorType == SKColorType.Rgba8888 || bitmap.ColorType == SKColorType.Bgra8888 ? bitmap : null; + if (currentBitmap == null) + { + // This block is a fallback in case the original bitmap was not in the expected format + using var temp = new SKBitmap(bitmap.Width, bitmap.Height, SKColorType.Rgba8888, SKAlphaType.Opaque); + using (var canvas = new SKCanvas(temp)) + { + canvas.DrawBitmap(bitmap, 0, 0); + } + currentBitmap = temp; + } + + SamImageU8 img = new SamImageU8(); - img.Width = bitmap.Width; - img.Height = bitmap.Height; + img.Width = currentBitmap.Width; + img.Height = currentBitmap.Height; img.Data = new byte[img.Width * img.Height * 3]; - BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); - Marshal.Copy(bitmapData.Scan0, img.Data, 0, img.Data.Length); - bitmap.UnlockBits(bitmapData); + var pixels = currentBitmap.Pixels; + int pixelIndex = 0; + for (int y = 0; y < img.Height; y++) + { + for (int x = 0; x < img.Width; x++) + { + var color = pixels[y * img.Width + x]; + int dataIndex = (y * img.Width + x) * 3; + img.Data[dataIndex + 0] = color.Red; // R + img.Data[dataIndex + 1] = color.Green; // G + img.Data[dataIndex + 2] = color.Blue; // B + } + } + + if (currentBitmap != bitmap) currentBitmap.Dispose(); + return img; } @@ -1163,7 +1198,7 @@ static bool SamDecodeMask(SamModel model, PromptEncoderResult prompt, SafeGGmlTe return true; } - static SafeGGmlGraph SamBuildFastGraph(SamModel model, SamState state, int nx, int ny, PointF point) + static SafeGGmlGraph SamBuildFastGraph(SamModel model, SamState state, int nx, int ny, SKPoint point) { ulong size = Common.TensorOverheadLength * GGML_DEFAULT_GRAPH_SIZE + Common.GraphOverheadLength; SafeGGmlContext ctx0 = new SafeGGmlContext(IntPtr.Zero, 1024 * 1024 * 1024, true); @@ -1619,14 +1654,17 @@ static bool WriteMasks(SamHparams hparams, int nx, int ny, SamState state, strin string filename = fname + i + ".png"; - Bitmap bitmap = new Bitmap(res.Width, res.Height, PixelFormat.Format8bppIndexed); - BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, res.Width, res.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); - Marshal.Copy(res.Data, 0, bitmapData.Scan0, res.Data.Length); - bitmap.UnlockBits(bitmapData); - bitmap.Save(filename, ImageFormat.Png); - } + var info = new SKImageInfo(res.Width, res.Height, SKColorType.Gray8, SKAlphaType.Opaque); + using var bitmap = new SKBitmap(info); + var pixelPtr = bitmap.GetPixels(); + Marshal.Copy(res.Data, 0, pixelPtr, res.Data.Length); + using (var stream = File.OpenWrite(filename)) + { + bitmap.Encode(stream, SKEncodedImageFormat.Png, 100); + } + } return true; } diff --git a/Demos/SAM/Properties/AssemblyInfo.cs b/Demos/SAM/Properties/AssemblyInfo.cs deleted file mode 100644 index 428e121..0000000 --- a/Demos/SAM/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("SAM")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("SAM")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("0c06afc4-b825-4898-89d0-605fbdcca9a4")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/SAM/SAM.csproj b/Demos/SAM/SAM.csproj index 08db77a..fe1fcad 100644 --- a/Demos/SAM/SAM.csproj +++ b/Demos/SAM/SAM.csproj @@ -1,71 +1,30 @@ - - - + + - Debug - AnyCPU - {0C06AFC4-B825-4898-89D0-605FBDCCA9A4} Exe - SAM - SAM - v4.6.2 - 512 - true - true - - - + net8.0 x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + + enable + enable + - - - - - - - - - - - - - - - - + + + + + - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - - - {e48f9d97-86ef-42cd-be41-46365026bee7} - ModelLoader - + + + - + PreserveNewest - + - + \ No newline at end of file diff --git a/Demos/SimpleBackend/App.config b/Demos/SimpleBackend/App.config deleted file mode 100644 index b50c74f..0000000 --- a/Demos/SimpleBackend/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Demos/SimpleBackend/Properties/AssemblyInfo.cs b/Demos/SimpleBackend/Properties/AssemblyInfo.cs deleted file mode 100644 index e243627..0000000 --- a/Demos/SimpleBackend/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("SimpleBackend")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("SimpleBackend")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("a9a8673f-febd-4025-ba45-70d56b99cc8c")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/SimpleBackend/SimpleBackend.csproj b/Demos/SimpleBackend/SimpleBackend.csproj index 0fb11a9..00f2233 100644 --- a/Demos/SimpleBackend/SimpleBackend.csproj +++ b/Demos/SimpleBackend/SimpleBackend.csproj @@ -1,59 +1,16 @@ - - - + + - Debug - AnyCPU - {A9A8673F-FEBD-4025-BA45-70D56B99CC8C} Exe - SimpleBackend - SimpleBackend - v4.6.2 - 512 - true - true - - + net8.0 x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + + enable + enable + - - - - - - - - - - - - - - - - - - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - + - + \ No newline at end of file diff --git a/Demos/TestOpt/App.config b/Demos/TestOpt/App.config deleted file mode 100644 index b50c74f..0000000 --- a/Demos/TestOpt/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Demos/TestOpt/Program.cs b/Demos/TestOpt/Program.cs index 9e2cf5a..65bdf0c 100644 --- a/Demos/TestOpt/Program.cs +++ b/Demos/TestOpt/Program.cs @@ -1,5 +1,4 @@ using GGMLSharp; -using System; using static GGMLSharp.Structs; namespace TestOpt diff --git a/Demos/TestOpt/Properties/AssemblyInfo.cs b/Demos/TestOpt/Properties/AssemblyInfo.cs deleted file mode 100644 index 03e5e3e..0000000 --- a/Demos/TestOpt/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("TestOpt")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TestOpt")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("d6ced790-5462-480b-aca6-f65a22c5cf56")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/TestOpt/TestOpt.csproj b/Demos/TestOpt/TestOpt.csproj index dc1c953..00f2233 100644 --- a/Demos/TestOpt/TestOpt.csproj +++ b/Demos/TestOpt/TestOpt.csproj @@ -1,59 +1,16 @@ - - - + + - Debug - AnyCPU - {D6CED790-5462-480B-ACA6-F65A22C5CF56} Exe - TestOpt - TestOpt - v4.6.2 - 512 - true - true - - + net8.0 x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + + enable + enable + - - - - - - - - - - - - - - - - - - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - + - + \ No newline at end of file diff --git a/Demos/Yolov3Tiny/App.config b/Demos/Yolov3Tiny/App.config deleted file mode 100644 index b50c74f..0000000 --- a/Demos/Yolov3Tiny/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Demos/Yolov3Tiny/Program.cs b/Demos/Yolov3Tiny/Program.cs index c2cb54c..858f575 100644 --- a/Demos/Yolov3Tiny/Program.cs +++ b/Demos/Yolov3Tiny/Program.cs @@ -1,11 +1,5 @@ using GGMLSharp; -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices; +using SkiaSharp; namespace Yolov3Tiny { @@ -13,10 +7,10 @@ internal class Program { static void Main(string[] args) { - string modelPath = @".\Assets\yolov3-tiny.gguf"; - string inputImgPath = @".\Assets\test.jpg"; - string outputImgPath = @".\output.jpg"; - string labelPath = @".\Assets\coco.names"; + string modelPath = "./Assets/yolov3-tiny.gguf"; + string inputImgPath = "./Assets/test.jpg"; + string outputImgPath = "./output.jpg"; + string labelPath = "./Assets/coco.names"; int modelWidth = 416; int modelHeight = 416; @@ -30,24 +24,24 @@ static void Main(string[] args) YoloModel model = LoadModel(modelPath); - Bitmap inputImg = new Bitmap(inputImgPath); - Bitmap resizedImg = ResizeImage(inputImg, modelWidth, modelHeight, out float ratio); + using SKBitmap inputImg = SKBitmap.Decode(inputImgPath); + using SKBitmap resizedImg = ResizeImage(inputImg, modelWidth, modelHeight, out float ratio); - BitmapData bitmapData = resizedImg.LockBits(new Rectangle(0, 0, resizedImg.Width, resizedImg.Height), ImageLockMode.ReadOnly, resizedImg.PixelFormat); - byte[] data = new byte[3 * modelWidth * modelHeight]; - Marshal.Copy(bitmapData.Scan0, data, 0, data.Length); - SafeGGmlTensor input = ctx0.NewTensor4d(Structs.GGmlType.GGML_TYPE_F32, modelWidth, modelHeight, 3, 1); + // --- SkiaSharp Pixel Access --- + byte[] data = resizedImg.Bytes; + using SafeGGmlTensor input = ctx0.NewTensor4d(Structs.GGmlType.GGML_TYPE_F32, modelWidth, modelHeight, 3, 1); input.Name = "input"; - for (int w = 0; w < modelWidth; w++) + + for (int y = 0; y < modelHeight; y++) { - for (int h = 0; h < modelHeight; h++) + for (int x = 0; x < modelWidth; x++) { - input.SetData(data[bitmapData.Stride * h + w * 3 + 2] / 255.0f, w, h, 0, 0); - input.SetData(data[bitmapData.Stride * h + w * 3 + 1] / 255.0f, w, h, 1, 0); - input.SetData(data[bitmapData.Stride * h + w * 3 + 0] / 255.0f, w, h, 2, 0); + var pixelIndex = (y * resizedImg.Width + x) * resizedImg.BytesPerPixel; + input.SetData(data[pixelIndex + 2] / 255.0f, x, y, 0, 0); // R + input.SetData(data[pixelIndex + 1] / 255.0f, x, y, 1, 0); // G + input.SetData(data[pixelIndex + 0] / 255.0f, x, y, 2, 0); // B } } - resizedImg.UnlockBits(bitmapData); SafeGGmlTensor result = ApplyConv2d(ctx0, input, model.Conv2dLayers[0]); PrintShape(0, result); @@ -120,33 +114,40 @@ static void Main(string[] args) Console.WriteLine(); - using (Graphics g = Graphics.FromImage(inputImg)) + using (SKCanvas canvas = new SKCanvas(inputImg)) { int d = Math.Abs(inputImg.Width - inputImg.Height) / 2; bool wIsLonger = inputImg.Width.CompareTo(inputImg.Height) > 0; foreach (Detection detection in detections) { - int w = (int)(detection.BBox.W * resizedImg.Width / ratio); int h = (int)(detection.BBox.H * resizedImg.Height / ratio); int x = (int)(detection.BBox.X * resizedImg.Width / ratio - w / 2) - (wIsLonger ? 0 : d); int y = (int)(detection.BBox.Y * resizedImg.Height / ratio - h / 2) - (wIsLonger ? d : 0); - Rectangle rect = new Rectangle(x, y, w, h); - g.DrawRectangle(Pens.Red, rect); + var rect = new SKRect(x, y, x + w, y + h); + + using (var paint = new SKPaint { Color = SKColors.Red, Style = SKPaintStyle.Stroke, StrokeWidth = 2 }) + { + canvas.DrawRect(rect, paint); + } int index = detection.Prob.ToList().IndexOf(detection.Prob.Max()); string str = labels[index] + " " + (detection.Objectness * 100).ToString("f2") + "%"; - SolidBrush redBrush = new SolidBrush(Color.Red); - Font font = new Font("Arial", 16); - g.DrawString(str, font, redBrush, x, y); - g.Save(); + using (var textPaint = new SKPaint { Color = SKColors.Red, TextSize = 16 }) + { + canvas.DrawText(str, x, y - 5, textPaint); + } + Console.WriteLine(string.Format("Detect: " + str)); } } Console.WriteLine(); - inputImg.Save(outputImgPath); + using (var stream = File.OpenWrite(outputImgPath)) + { + inputImg.Encode(stream, SKEncodedImageFormat.Jpeg, 100); + } Console.WriteLine("Done."); Console.ReadLine(); } @@ -195,27 +196,25 @@ static void PrintShape(int layer, SafeGGmlTensor t) Console.WriteLine(string.Format("Layer {0} output shape: {1} x {2} x {3} x {4}", layer, (int)t.Shape[0], (int)t.Shape[1], (int)t.Shape[2], (int)t.Shape[3])); } - public static Bitmap ResizeImage(Bitmap image, int targetWidth, int targetHeight, out float ratio) + public static SKBitmap ResizeImage(SKBitmap image, int targetWidth, int targetHeight, out float ratio) { - PixelFormat format = image.PixelFormat; - Bitmap output = new Bitmap(targetWidth, targetHeight, format); - int w = image.Width; - int h = image.Height; - float xRatio = targetWidth / (float)w; - float yRatio = targetHeight / (float)h; + var outputInfo = new SKImageInfo(targetWidth, targetHeight); + var output = new SKBitmap(outputInfo); + + float xRatio = targetWidth / (float)image.Width; + float yRatio = targetHeight / (float)image.Height; ratio = Math.Min(xRatio, yRatio); - int width = (int)(w * ratio); - int height = (int)(h * ratio); + + int width = (int)(image.Width * ratio); + int height = (int)(image.Height * ratio); int x = targetWidth / 2 - width / 2; int y = targetHeight / 2 - height / 2; - Rectangle roi = new Rectangle(x, y, width, height); - using (Graphics graphics = Graphics.FromImage(output)) + var roi = new SKRect(x, y, x + width, y + height); + + using (var canvas = new SKCanvas(output)) { - graphics.Clear(Color.Black); - graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None; - graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; - graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; - graphics.DrawImage(image, roi); + canvas.Clear(SKColors.Black); + canvas.DrawBitmap(image, roi); } return output; } @@ -417,23 +416,19 @@ private static float BoxIou(Detection detA, Detection detB) return 0; } - Rectangle a = new Rectangle((int)(detA.BBox.X * 100), (int)(detA.BBox.Y * 100), (int)(detA.BBox.W * 100), (int)(detA.BBox.H * 100)); - Rectangle b = new Rectangle((int)(detB.BBox.X * 100), (int)(detB.BBox.Y * 100), (int)(detB.BBox.W * 100), (int)(detB.BBox.H * 100)); + // Use SKRect for intersection calculation + var a = new SKRect((int)(detA.BBox.X * 100), (int)(detA.BBox.Y * 100), (int)((detA.BBox.X + detA.BBox.W) * 100), (int)((detA.BBox.Y + detA.BBox.H) * 100)); + var b = new SKRect((int)(detB.BBox.X * 100), (int)(detB.BBox.Y * 100), (int)((detB.BBox.X + detB.BBox.W) * 100), (int)((detB.BBox.Y + detB.BBox.H) * 100)); float areaA = a.Width * a.Height; float areaB = b.Width * b.Height; float area = Math.Min(areaA, areaB); + if (area == 0) return 0; - a.Intersect(b); - float ins = a.Width * a.Height / area; - if (float.IsNaN(ins)) - { - return 0; - } - else - { - return ins; - } + var intersection = SKRect.Intersect(a, b); + float ins = intersection.Width * intersection.Height / area; + + return float.IsNaN(ins) ? 0 : ins; } } diff --git a/Demos/Yolov3Tiny/Properties/AssemblyInfo.cs b/Demos/Yolov3Tiny/Properties/AssemblyInfo.cs deleted file mode 100644 index c304a6e..0000000 --- a/Demos/Yolov3Tiny/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("Yolov3Tiny")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Yolov3Tiny")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("2f1ad71d-f846-4cb4-827b-507f8224318a")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Demos/Yolov3Tiny/Yolov3Tiny.csproj b/Demos/Yolov3Tiny/Yolov3Tiny.csproj index 339e7a1..b234570 100644 --- a/Demos/Yolov3Tiny/Yolov3Tiny.csproj +++ b/Demos/Yolov3Tiny/Yolov3Tiny.csproj @@ -1,72 +1,37 @@ - - - + + - Debug - AnyCPU - {2F1AD71D-F846-4CB4-827B-507F8224318A} Exe - Yolov3Tiny - Yolov3Tiny - v4.6.2 - 512 - true - true - - + net8.0 x64 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + + enable + enable + - - - - - - - - - - + + - - - - - - + PreserveNewest - - PreserveNewest - - - - - {001fd1f1-ec17-45e9-b750-a2a2029e6c60} - GGMLSharp - + - - PreserveNewest - + + 3.119.0 + + + 3.119.0 + + + 3.119.0 + + + 3.119.0 + - + \ No newline at end of file diff --git a/GGMLSharp/GGMLSharp.csproj b/GGMLSharp/GGMLSharp.csproj index f87aac8..8b803bb 100644 --- a/GGMLSharp/GGMLSharp.csproj +++ b/GGMLSharp/GGMLSharp.csproj @@ -1,75 +1,32 @@ - - - + + - Debug - AnyCPU - {001FD1F1-EC17-45E9-B750-A2A2029E6C60} + net8.0 Library - Properties - GGMLSharp - GGMLSharp - v4.6.2 - 512 - true - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - x64 + true + x64 + + enable + enable + + GGMLSharp + 0.1.0 + SciSharp STACK + High-performance, cross-platform C#/.NET wrapper for the ggml library + GGML, MachineLearning, SciSharp, LLM, GPT, LLaMA + Copyright © 2023-2025 SciSharp STACK + https://github.com/SciSharp/GGMLSharp + https://github.com/SciSharp/GGMLSharp - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + PreserveNewest + + PreserveNewest + - + \ No newline at end of file diff --git a/GGMLSharp/Properties/AssemblyInfo.cs b/GGMLSharp/Properties/AssemblyInfo.cs deleted file mode 100644 index 66d720c..0000000 --- a/GGMLSharp/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// 有关程序集的一般信息由以下 -// 控制。更改这些特性值可修改 -// 与程序集关联的信息。 -[assembly: AssemblyTitle("GGMLSharp")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("GGMLSharp")] -[assembly: AssemblyCopyright("Copyright © 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// 将 ComVisible 设置为 false 会使此程序集中的类型 -//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 -//请将此类型的 ComVisible 特性设置为 true。 -[assembly: ComVisible(false)] - -// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID -[assembly: Guid("001fd1f1-ec17-45e9-b750-a2a2029e6c60")] - -// 程序集的版本信息由下列四个值组成: -// -// 主版本 -// 次版本 -// 生成号 -// 修订号 -// -//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 -//通过使用 "*",如下所示: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/GGMLSharp/libggml.so b/GGMLSharp/libggml.so new file mode 100644 index 0000000..d63485f Binary files /dev/null and b/GGMLSharp/libggml.so differ diff --git a/README.md b/README.md index 1218688..0eed3c5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # GGMLSharp Introduction -GGMLSharp is a API for C# to use [ggml](https://github.com/ggerganov/ggml).
+GGMLSharp is an API for C# to use [ggml](https://github.com/ggerganov/ggml).
ggml is a wonderful C-language machine-learning tool, and now you can use it with C#.
GGMLSharp contains all ggml shared libs and some demos. @@ -8,7 +8,6 @@ GGMLSharp contains all ggml shared libs and some demos. - Written in C# only - Only depends on ggml -- DotNet 462 Support! - All Demos can use safe code only! ## Demos