我不知道如何在Unity中使用ML.NET。
我做了什么:将我的项目转换为与框架4.x兼容。将api兼容性级别转换为框架4.x。制作资产/插件/ ml文件夹,并放入具有相应xml的Microsoft.ML api。将所有ml.dlls平台设置标记为仅兼容86_64(这是多余的)。
我现在可以:调用ML API,并创建MlContext,TextLoader并进行模型训练。训练模型后,我还可以评估训练后的模型,但是...
我不能:尝试从模型中获取预测时,出现错误:github comment on issue from 28.12.18(那里还有一个整个项目,您可以在其中看到代码)相同的代码可在Visual Studio解决方案中使用。
public float TestSinglePrediction(List<double> signal, MLContext mlContext, string modelPath)
{
ITransformer loadedModel;
using (var stream = new FileStream(modelPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
loadedModel = mlContext.Model.Load(stream);
}
var predictionFunction = loadedModel.MakePredictionFunction<AbstractSignal, PredictedRfd>(mlContext);
var abstractSignal = new AbstractSignal()
{
Sig1 = (float)signal[0],
Sig2 = (float)signal[1],
Sig3 = (float)signal[2],
Sig4 = (float)signal[3],
Sig5 = (float)signal[4],
Sig6 = (float)signal[5],
Sig7 = (float)signal[6],
Sig8 = (float)signal[7],
Sig9 = (float)signal[8],
Sig10 = (float)signal[9],
Sig11 = (float)signal[10],
Sig12 = (float)signal[11],
Sig13 = (float)signal[12],
Sig14 = (float)signal[13],
Sig15 = (float)signal[14],
Sig16 = (float)signal[15],
Sig17 = (float)signal[16],
Sig18 = (float)signal[17],
Sig19 = (float)signal[18],
Sig20 = (float)signal[19],
RfdX = 0
};
var prediction = predictionFunction.Predict(abstractSignal);
return prediction.RfdX;
}
这是返回错误行的方法:var predictionFunction = loadedModel.MakePredictionFunction<AbstractSignal, PredictedRfd>(mlContext);
[从Unity 2018.1开始,团结可以针对.net4.x。因此,您需要将.net版本设置为.NET 4.x等效版本或.net标准2.0(https://blogs.unity3d.com/2018/03/28/updated-scripting-runtime-in-unity-2018-1-what-does-the-future-hold/),并确保将dll添加到项目中,以作为Visual Studio中的参考。如果您不将其添加为参考,那么视觉sudio对此一无所知。
正如尼克在他的帖子中所说**,如果遵循这些步骤,它将与Unity一起使用。
但是,在我撰写本文时,ML.NET团队尚未使用Unity进行全面测试,因此它没有开箱即用也就不足为奇了。 This issue上的ML.NET Github repository已打开。我建议密切关注该问题,以了解Unity支持的状态。
**尼克:Starting with Unity 2018.1, unity can target .net 4.x. So you would need to set the .net version to .NET 4.x Equivalent, or .net standard 2.0 (https://blogs.unity3d.com/2018/03/28/updated-scripting-runtime-in-unity-2018-1-what-does-the-future-hold/) and make sure you add your dll to the project as a reference in visual studio. If you don't add it as a reference, then visual sudio doesn't know about it.
如下是从https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/iris-clustering修改的虹膜示例(由于某些ML API的更改而不再起作用)
创建一个名为MLuTest的脚本并将以下代码粘贴到其中:
公共类MLuTest:MonoBehaviour {
static readonly string _dataPath = Path.Combine(Environment.CurrentDirectory, "Assets", "Data", "iris.data");
static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, "Assets", "Data", "IrisClusteringModel.zip");
MLContext mlContext;
void Start()
{
Debug.Log("starting...");
mlContext = new MLContext(seed: 0);
IDataView dataView = mlContext.Data.ReadFromTextFile<IrisData>(_dataPath, hasHeader: false, separatorChar: ',');
string featuresColumnName = "Features";
var pipeline = mlContext.Transforms
.Concatenate(featuresColumnName, "SepalLength", "SepalWidth", "PetalLength", "PetalWidth")
.Append(mlContext.Clustering.Trainers.KMeans(featuresColumnName, clustersCount: 3));//read and format flowery data
var model = pipeline.Fit(dataView);//train
using (var fileStream = new FileStream(_modelPath, FileMode.Create, FileAccess.Write, FileShare.Write))//save trained model
{
mlContext.Model.Save(model, fileStream);
}
var predictor = mlContext.Model.CreatePredictionEngine<IrisData, ClusterPrediction>(model);//predict
IrisData Setosa = new IrisData
{
SepalLength = 5.1f,
SepalWidth = 3.5f,
PetalLength = 1.4f,
PetalWidth = 0.2f
};
Debug.Log(predictor.Predict(Setosa).PredictedClusterId);
Debug.Log("...done predicting, now do what u like with it");
}
}
public class IrisData
{
[LoadColumn(0)]
public float SepalLength;
[LoadColumn(1)]
public float SepalWidth;
[LoadColumn(2)]
public float PetalLength;
[LoadColumn(3)]
public float PetalWidth;
}
public class ClusterPrediction
{
[ColumnName("PredictedLabel")]
public uint PredictedClusterId;
[ColumnName("Score")]
public float[] Distances;
}
这应该立即可用...对我来说很好。当获取api文件时,您可能会陷入混乱,它们可能与我的版本不同或仅与某些.net框架兼容。因此,获取我的Plugins文件夹的内容(请注意,所有这些api不一定都是必需的,请自行挑选):https://github.com/dotnet/machinelearning/issues/1886以前(在以前的统一版本中)必须更改某些播放器设置,但我不必这样做。但是,这里是我的:我希望这会有所帮助,因为Unity更新19.2之后,我在此线程的先前文章中都没有遇到任何问题。