【问题标题】:Running a CNTK model under a VS unit test在 VS 单元测试下运行 CNTK 模型
【发布时间】:2018-02-23 22:24:14
【问题描述】:

我已经使用 C# 训练了一个 CNTK 模型,现在我想针对一个包装类运行测试用例,以便我们可以检测到有人用性能不佳的模型替换模型时出现的问题,另外还有重复将神经网络与我们的旧模型进行比较。但是,当我尝试运行测试时,我得到:

Result StackTrace:  
at CNTK.CNTKLibPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_CNTKLib(ExceptionDelegate applicationDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate divideByZeroDelegate, ExceptionDelegate indexOutOfRangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate invalidOperationDelegate, ExceptionDelegate ioDelegate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate outOfMemoryDelegate, ExceptionDelegate overflowDelegate, ExceptionDelegate systemExceptionDelegate)
   at CNTK.CNTKLibPINVOKE.SWIGExceptionHelper..cctor()
 --- End of inner exception stack trace ---
...
System.TypeInitializationException: The type initializer for 'CNTK.CNTKLibPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'Cntk.Core.CSBinding-2.3.1.dll': A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

我从独立的控制台应用程序中遇到了同样的错误,并通过确保控制台应用程序和类库都构建为 x64 来修复它。我对测试项目进行了相同的更改,另外将测试架构设置为 x64(测试 > 测试设置 > 默认处理器架构 > x64),但没有骰子。

我正在使用 CNTK (2.3.1) 的 GPU 版本,但对于单元测试上下文,它应该使用 CPU。我已验证 Cntk.Core.CSBinding-2.3.1.dll 存在于 bin 目录中。

有什么想法吗?有人试过吗?

【问题讨论】:

  • 在进行涉及需要在 app.config 中输入才能正确初始化的类的单元测试时,我遇到了一个非常相似的错误。我不知道你是否有这个问题,但这个错误几乎总是归结为你的主项目和测试项目之间的一些差异。

标签: c# visual-studio unit-testing cntk


【解决方案1】:

如果您使用的是 NUnit,我已经看到了很多。 CNTK 包附带了一堆本机 DLL,测试运行程序找不到,因为工作目录设置为某个临时目录,而不是您的输出文件夹。我的解决方案是每次测试开始时将所有必要的 DLL 复制到工作目录。我在有问题的 [TestFixture] 的 [SetUp] 方法中执行此操作。

如果您使用的是不同的测试框架,可能会有类似的问题。

【讨论】:

  • 谢谢!不使用 NUnit,只是默认的微软测试基础设施。我猜 pInvoke 只知道工作目录而不知道 bin 目录。将当前目录设置为二进制目录是可行的,尽管它不是很好。我会按照您的建议进行复制,或者确定它是否使用 PATH 环境变量。
  • 您可以在 TestInitialize 期间将二进制文件夹添加到 PATH:Environment.SetEnvironmentVariable("PATH", $"{Environment.GetEnvironmentVariable("PATH")}{Path.PathSeparator}{binFolderPath}");
【解决方案2】:

我在使用 NUnit 时遇到了同样的问题。将以下代码添加到测试设置中,解决了我的问题。

[SetUp]
public void Setup()
{
    Environment.CurrentDirectory = TestContext.CurrentContext.TestDirectory;
}

【讨论】:

  • 我没有 TestContext.CurrentContext,但修改 Environment.CurrentDirectory 指向 bin 文件夹对我来说确实有效。
猜你喜欢
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 2018-10-24
  • 2011-07-09
  • 1970-01-01
  • 2015-12-29
相关资源
最近更新 更多