创建并训练图像分类模型。然后我根据指南将其添加到我的 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)”



