【问题标题】:Execution error when calling Matlab compiled function from c#从C#调用Matlab编译函数时执行错误
【发布时间】:2016-05-21 16:18:55
【问题描述】:

我正在使用 Matlab 2015a,并且已经开发、训练和测试了一个分类集成(增强树)并保存了经过最​​佳训练的模型(.mat 文件)。

由于我希望在 .Net C# 应用程序中使用此模型,我创建了一个 .m 文件来加载包含经过训练的分类器的 .mat 文件,并使用它来根据传入的特征预测结果(参见代码.m 文件)。

function [ypredict score] = fnTrainedClassifer( input_args )
  load ('trainedClassifier.mat');
  [ypredict score] = predict(trainedClassifier,input_args);
end

然后我使用 Matlab 编译器创建了一个 .Net 程序集,并确保在运行库所需的文件部分中包含 .mat 文件。到目前为止一切顺利......然后我创建了一个快速的 C# 控制台应用程序来测试库(请参阅下面的应用程序代码)并运行它。

using System;
using MathWorks.MATLAB.NET.Arrays;
using TrainedClassifierComp;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            MLTestClass theModel = null;   /* Stores deployment class instance */
            MWStructArray inputs = null;   /* Sample input data */
            MWArray[] result = null;       /* Stores the result */
            MWNumericArray prediction = null;  /* Ouptut data extracted from result */
            MWNumericArray score = null;  /* Ouptut data extracted from result */

            /* Create the new deployment object */
            theModel = new MLTestClass();

            /* Create an MWStructArray */
            String[] myFieldNames = { "1", "2", "3", "4", "5", "6", "7", "8" };
            inputs = new MWStructArray(1, 8, myFieldNames);

            /* Populate struct with some sample inputs  */
            inputs["1", 1] = 1;
            inputs["2", 2] = 2;
            inputs["3", 3] = 3;
            inputs["4", 4] = 4;
            inputs["5", 5] = 5;
            inputs["6", 6] = 6;
            inputs["7", 7] = 7;
            inputs["8", 8] = 8;

            /* Show some of the sample data */
            Console.WriteLine("Inputs: ");
            Console.WriteLine(inputs.ToString());

            /* Pass it to a MATLAB function */
            result = theModel.fnTrainedClassifier(1, inputs);
            prediction = (MWNumericArray) result[0];
            score = (MWNumericArray)result[1];

            /* Show the results */
            Console.WriteLine("Prediction: ");
            Console.WriteLine(prediction.ToString());
            Console.WriteLine("Score: ");
            Console.WriteLine(prediction.ToString());
        }
    }
}

不幸的是,我遇到了以下执行时间错误,在 Google 上搜索没有任何有用的结果:

警告:最初保存为 classreg.learning.classif.ClassificationEnsemble 的变量“trainedClassifier”无法实例化为对象,将作为 uint32 读入。 在 fnTrainedClassifier 中(第 4 行) 使用预测时出错(第 84 行) uint32 类的系统不能与“predict”命令一起使用。首先将系统转换为已识别的模型,例如使用“idss”命令。 fnTrainedClassifier 中的错误(第 5 行)

任何有关如何解决此问题的指导以及您如何能够实现类似目标的信息将不胜感激。

附加信息:我尝试了其他几个 AI 系统,例如神经网络和我有同样的问题。在我看来,我没有正确地解决这个问题 - 是否有另一种方法可以在 c# 等外部编译语言中使用 Matlab 函数?

【问题讨论】:

  • 嗨 Harnish,我遇到了同样的问题(在 Java 中),你能找到解决这个问题的方法吗?或者,你放弃了吗?如果您分享您的经验,这将非常有帮助。谢谢

标签: c# .net matlab matlab-compiler


【解决方案1】:

这个问题已经问了一段时间了,但我遇到了同样的问题。我希望以下解决方案可以帮助一些人节省一些时间。 因此,当从 .mat 文件加载类对象时,Matlab 编译器似乎会读取类类型,但不会加载类本身。因此,我们需要以某种方式添加实例化对象的类定义。

为了强制编译器加载所需的类,我创建了该类的一个空对象,然后从 .mat 文件加载对象并将其分配给空对象。 您的垫子代码应如下所示;

function [ypredict score] = fnTrainedClassifer( input_args )
     trainedClassifier = classreg.learning.classif.ClassificationEnsemble.empty; % only this line is added.
     load ('trainedClassifier.mat');
     [ypredict score] = predict(trainedClassifier,input_args);
end

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    相关资源
    最近更新 更多