Files
FishmanTheMurloc 0c02808626 使用TorchSharp重写RodNet,以利后续优化 (#1613)
* 使用TorchSharp重写RodNet,以利后续优化

* 增加一个外部torch加载配置来代替直接的依赖,如配置不生效则使用原先手搓的算法

* BgiOnnxFactory取消单例,改为在App服务类中注册为单例,由此修复了一堆单元测试

* BgiOnnxFactory中几个静态方法改为成员方法以和App解耦;因不再有多个mat源供消耗,FishBite中文字块算法不再改动传入的mat,使得后续串联的算法不受其影响

* 将BehavioursTests中临时的配置读取方式改为读取主项目编译环境中的json文件;新建单元测试的README

* 将RodNet算法更新到 010006a44c 的版本;RodNet中关于torch库推理和直接数学计算的校验移至单元测试

* 更新RodNet算法至最新:add5672731

* 注释调试用的代码
2025-06-01 15:16:54 +08:00

56 lines
2.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using BetterGenshinImpact.GameTask.AutoFishing;
using BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BetterGenshinImpact.GameTask.AutoFishing.RodNet;
using static TorchSharp.torch;
namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
{
[Collection("Init Collection")]
public class RodNetTests
{
public RodNetTests(TorchFixture torch)
{
if (!torch.UseTorch)
throw new NotSupportedException("torch加载失败请检查BetterGenshinImpact项目编译环境的配置");
}
[Theory]
[InlineData(517.6326F, 548.49023F, 255.25723F, 263.55743F, 256.57538F, 351.56964F, 274.65656F, 333.1523F, 5)]
/// <summary>
/// 测试计算给到后处理之前的浮点数输出Torch推理的结果和直接用数学计算的结果两者的数值应该在转换到单精度时相同
/// </summary>
public void ComputeScoresTest_ShouldBeTheSame(double rod_x1, double rod_x2, double rod_y1, double rod_y2, double fish_x1, double fish_x2, double fish_y1, double fish_y2, int fish_label)
{
//
RodInput rodInput = new RodInput
{
rod_x1 = rod_x1,
rod_x2 = rod_x2,
rod_y1 = rod_y1,
rod_y2 = rod_y2,
fish_x1 = fish_x1,
fish_x2 = fish_x2,
fish_y1 = fish_y1,
fish_y2 = fish_y2,
fish_label = fish_label
};
RodNet sut = new RodNet();
//
NetInput netInput = GeometryProcessing(rodInput) ?? throw new NullReferenceException();
Tensor outputTensor = sut.ComputeScores_Torch(netInput);
double[] pred = ComputeScores(netInput);
//
Assert.Equal((float)pred[0], (float)outputTensor.data<double>()[0]); // 对比时降低精度,差不多就行
Assert.Equal((float)pred[1], (float)outputTensor.data<double>()[1]);
Assert.Equal((float)pred[2], (float)outputTensor.data<double>()[2]);
}
}
}