RAIDI 2025.3.11

dotnet add package RAIDI --version 2025.3.11
                    
NuGet\Install-Package RAIDI -Version 2025.3.11
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RAIDI" Version="2025.3.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RAIDI" Version="2025.3.11" />
                    
Directory.Packages.props
<PackageReference Include="RAIDI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RAIDI --version 2025.3.11
                    
#r "nuget: RAIDI, 2025.3.11"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RAIDI@2025.3.11
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RAIDI&version=2025.3.11
                    
Install as a Cake Addin
#tool nuget:?package=RAIDI&version=2025.3.11
                    
Install as a Cake Tool

推理:

创建检测器

using RAIDI;
using SixLabors.ImageSharp;

// 使用CPU
using var predictor = new Detector(model);
// 使用GPU
using var predictor = new Detector(model, SessionOptions.MakeSessionOptionWithCudaProvider(0));

设置检测器的参数

// 设置交并比(分类、检测、分割)
predictor.Parameters.IoU = .45f
// 设置置信度(分类、检测、分割)
predictor.Parameters.Confidence = .5f
// 使用原始宽高比处理(分类、检测、分割)
predictor.Parameters.ProcessWithOriginalAspectRatio = true
// 分割结果的掩膜置信度
predictor.Parameters.SegmentPixelsWithDefaultConfidence = .5f;

支持的图像格式

(Image image) using SixLabors.ImageSharp.Image (string path) path to image (byte[] data) byte (Stream stream) stream

使用以下代码执行快速推理获取结果(不区分深度学习方法,但编译器可能无法在编译时检查某些属性是否存在)

var result = predictor.Inference("path/to/image");

Console.WriteLine(result);

使用以下代码推理.rairun 分类 模型

IClassificationResult result = predictor.Classify("path/to/image");
// 异步
var result = await predictor.ClassifyAsync("path/to/image");

Console.WriteLine(result);

使用以下代码推理.rairun 目标检测 模型

IDetectionResult result = predictor.Detect("path/to/image");
// 异步
var result = await predictor.DetectAsync("path/to/image");

Console.WriteLine(result);

使用以下代码推理.rairun 实例分割 模型

ISegmentationResult result = predictor.Segment("path/to/image");
// 异步
var result = await predictor.SegmentAsync("path/to/image");

Console.WriteLine(result);

使用以下代码推理.rairun 异常检测 模型

IAnomalyResult result = predictor.Anomaly("path/to/image");
// 异步
var result = await predictor.AnomalyAsync("path/to/image");

Console.WriteLine(result);

绘图

分类、检测、分割

using RAIDI;
using RAIDI.Plotting;
using SixLabors.ImageSharp;

var imagePath = "path/to/image";

using var predictor = new Detector("path/to/model");

var result = await predictor.SegmentAsync(imagePath);

using var image = Image.Load(imagePath);
using var ploted = await result.PlotImageAsync(image);

ploted.Save("./Image.jpg")

异常检测

using RAIDI;

var imagePath = "path/to/image";

using var predictor = new Detector("path/to/model");

// result内包含热力图、合并图、原图图像、平均异常分数、最大异常分数、异常分数的95百分位值、超过预定阈值(128)的像素比例、每个像素的原始异常分数(分数已经归一化到0-255范围)
var result = await predictor.AnomalyAsync(imagePath);

// 使用 ToString 将保存图像及结果
Console.WriteLine(result)

其中RAIDI.Plotting可以设置绘图的字体位置、线宽、颜色等信息。

以下是一个完整的示例
using RAIDI;
using RAIDI.Plotting;
using System.Diagnostics;
using SixLabors.ImageSharp;
using Microsoft.ML.OnnxRuntime;

var output = "./path/to";

if (Directory.Exists(output) == false)
	Directory.CreateDirectory(output);


if (OperatingSystem.IsWindows())
{
	Process.Start(new ProcessStartInfo
	{
		FileName = Path.GetFullPath(output),
		UseShellExecute = true,
	});
}


await Classify(new string[] { "./path/to/Part_A.jpg", "./path/to/Part_B.jpg" }, "./path/to/Sample_Part_best.rairun");

await Detect("./path/to/Abnormal.jpg", "./path/to/Sample_Abnormal_best.rairun");

await Segment("./path/to/Workpiece.jpg", "./assets/to/Sample_Workpiece_best.rairun");

await Anomaly("./path/to/Car.jpg", "./assets/to/Sample_Car_best.rairun");


async Task Classify(string[] images, string model)
{
	Console.WriteLine("\n================ 图像分类 ================\n");

	Console.WriteLine("加载模型...");

	// 使用GPU
	using var predictor = new Detector(model, SessionOptions.MakeSessionOptionWithCudaProvider(0));

	foreach (var image in images)
	{
		Console.WriteLine("执行计算... ({0})", image);
		var result = await predictor.ClassifyAsync(image);

		Console.WriteLine($"结果: {result}");
		Console.WriteLine($"速度: {result.Speed}");

		Console.WriteLine("绘图及保存...");
		using var origin = Image.Load(image);

		using var ploted = await result.PlotImageAsync(origin);

		var pathToSave = Path.Combine(output, Path.GetFileName(image));

		ploted.Save(pathToSave);
	}
}


async Task Detect(string image, string model)
{
	Console.WriteLine("\n================ 目标检测 ================\n");

	Console.WriteLine("加载模型...");
	using var predictor = new Detector(model);

	Console.WriteLine("执行计算...");
	var result = await predictor.DetectAsync(image);

	Console.WriteLine($"结果: {result}");
	Console.WriteLine($"速度: {result.Speed}");

	Console.WriteLine("绘图及保存...");
	using var origin = Image.Load(image);

	using var ploted = await result.PlotImageAsync(origin);

	var pathToSave = Path.Combine(output, Path.GetFileName(image));

	ploted.Save(pathToSave);
}


async Task Segment(string image, string model)
{
	Console.WriteLine("\n================ 实例分割 ================\n");

	Console.WriteLine("加载模型...");
	using var predictor = new Detector(model);

	Console.WriteLine("执行计算...");
	var result = await predictor.SegmentAsync(image);

	Console.WriteLine($"结果: {result}");
	Console.WriteLine($"速度: {result.Speed}");

	Console.WriteLine("绘图及保存...");
	using var origin = Image.Load(image);

	//掩膜置信度设置为0.5,掩膜轮廓线宽设置为0.1
	using var ploted = await result.PlotImageAsync(origin, new SegmentationPlottingOptions { MaskConfidence = 5f, ContoursThickness = 1f });

	var filename = $"{Path.GetFileNameWithoutExtension(image)}_seg";
	var extension = Path.GetExtension(image);

	var pathToSave = Path.Combine(output, filename + extension);

	ploted.Save(pathToSave);
}


async Task Anomaly(string image, string model)
{
	Console.WriteLine("\n================ 异常检测 ================\n");

	Console.WriteLine("加载模型...");
	using var predictor = new Detector(model);

	Console.WriteLine("执行计算...");
	var result = await predictor.AnomalyAsync(image);

	// 保存图像及结果
	Console.WriteLine($"结果: {result}");
	Console.WriteLine($"速度: {result.Speed}");
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

优化分割推理速度