创建并训练图像分类模型。然后我根据指南将其添加到我的 ASP.NET Web API 中。在本地运行时一切正常。该模型对图像进行分类。但是发布到IIS后,出现错误:
“调用目标已引发异常”
如果从 model 中删除延迟初始化PredictionEngine,则只会记录未处理的异常:
“连接 ID“18014398514582257719”,请求 ID“40000039-0001-fa00-b63f-84710c7967bb”:应用程序引发了未处理的异常。”
public partial class Model
{
private readonly MLContext context = new();
private ITransformer? model;
private readonly string filePath;
public Model(string filePath)
{
this.filePath = filePath;
predictionEngine = new(CreatePredictionEngine, LazyThreadSafetyMode.ExecutionAndPublication);
}
public Model(PredictionEnginePool<WineImageData, WineImagePrediction> predictionEnginePool, IOptions<ModelConfiguration> options)
{
filePath = options.Value.FilePath;
predictionEngine = new(() => predictionEnginePool.GetPredictionEngine(options.Value.ModelName), LazyThreadSafetyMode.ExecutionAndPublication);
}
}
添加到 DI 容器:
public static class Extensions
{
public static IServiceCollection AddWineImageModel(this IServiceCollection services, string modelName, string filePath)
{
_ = services.AddPredictionEnginePool<WineImageData, WineImagePrediction>().FromFile(modelName, filePath, true);
_ = services.AddSingleton<ITrainingModel, Model.Model>();
_ = services.AddSingleton<IConsumptionModel, Model.Model>();
return services.Configure<ModelConfiguration>(config =>
{
config.ModelName = modelName;
config.FilePath = filePath;
});
}
}
变化#1
导致问题的异常System.DllNotFoundException是:
“无法加载 DLL“tensorflow”或其依赖项之一:找不到指定的模块。(0x8007007E)”
第一步是安装vs_redist.x64.exe。
接下来,在 IIS 服务管理器中,在所需的应用程序池中,您需要转到其他设置,并在“常规”选项卡中找到“允许的 32 位应用程序”字段。然后将该参数的值更改为
False。 Tensorflow 仅适用于 x64 进程。重新启动应用程序后,又出现了异常,表明可以加载库:
当前的异常可通过安装SciSharp.TensorFlow.Redist软件包2.3.1 或更低版本(≤ 2.3.1) 来修复。安装软件包并重新发布应用程序后,错误消失。