首页 > 解决方案 > 如何修复 Keras.NET BadImageFormatException 异常?

问题描述

我使用 python 创建了一个带有 keras 的机器学习模型。我将此模型保存在一个 .h5 文件中,并希望在我的 ASP.NET Core API 中使用该文件。要加载我使用 Keras.NET 的模型。

我遇到的问题是,当我尝试调试应用程序时,出现以下错误:

  Message: 
    System.BadImageFormatException : An attempt was made to load a program with an incorrect format. (0x8007000B)
  Stack Trace: 
    Runtime.Py_IsInitialized()
    Runtime.Initialize(Boolean initSigs)
    PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv, Boolean initSigs)
    PythonEngine.Initialize(Boolean setSysArgv, Boolean initSigs)
    PythonEngine.Initialize()
    Keras.InstallAndImport(String module)
    <.cctor>b__27_0()
    Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
    Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
    Lazy`1.CreateValue()
    Lazy`1.get_Value()
    Keras.get_Instance()
    Sequential.ctor()

我读过其他人遇到这个问题。他们说我应该将我的平台目标更改为 x64。

这样做会导致以下错误:

  Message: 
    System.BadImageFormatException : Could not load file or assembly 'Project.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
  Stack Trace: 
    ModelsTest.Test_models()

现在我的其余测试都以BadImageFromatException. 这让我认为 python 安装为 64 位版本,.NET Core 安装为 32 位版本。为了确保不是这种情况,我重新安装了 .NET Core 的 64 位版本,但我一直收到同样的错误。

我希望有一个人可以帮助我。

标签: pythonasp.net-corekeras.net-core

解决方案


推荐阅读