AI与.NET技术实现图像分类模型的部署与调用的详细步骤
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">引言</a></li><li><a href="#_label1">准备工作</a></li><li><a href="#_label2">安装步骤</a></li><li><a href="#_label3">图像分类模型的选择</a></li><li><a href="#_label4">模型训练与导出</a></li><li><a href="#_label5">在 .NET 中调用模型</a></li><ul class="second_class_ul"><li><a href="#_lab2_5_0">1. 创建 .NET 项目</a></li><li><a href="#_lab2_5_1">2. 定义输入和输出数据结构</a></li><li><a href="#_lab2_5_2">3. 定义 estimator 管道</a></li></ul><li><a href="#_label6">4. 提取预测结果</a></li><ul class="second_class_ul"></ul><li><a href="#_label7">调用模型进行图像分类</a></li><ul class="second_class_ul"><li><a href="#_lab2_7_3">1. 准备素材与分类文件</a></li><li><a href="#_lab2_7_4">2. 加载模型</a></li><li><a href="#_lab2_7_5">3. 解析输出结果</a></li></ul><li><a href="#_label8">其他实现方式</a></li><ul class="second_class_ul"></ul><li><a href="#_label9">实际应用场景</a></li><ul class="second_class_ul"></ul><li><a href="#_label10">挑战与解决方案</a></li><ul class="second_class_ul"></ul><li><a href="#_label11">结论</a></li><ul class="second_class_ul"></ul><li><a href="#_label12">参考资料</a></li><ul class="second_class_ul"></ul></ul></div><p class="maodian"><a name="_label0"></a></p><h2>引言</h2><p>人工智能(AI)技术的迅猛发展推动了各行各业的数字化转型。图像分类,作为计算机视觉领域的核心技术之一,能够让机器自动识别图像中的物体、场景或特征,已广泛应用于医疗诊断、安防监控、自动驾驶和电子商务等领域。</p>
<p>与此同时,.NET 平台凭借其高效性、跨平台能力和强大的 C# 编程语言支持,成为开发者构建企业级应用的首选技术栈。将 AI 图像分类模型与 .NET 技术结合,不仅能充分发挥两者的优势,还能为开发者提供一种高效、直观的实现方式。</p>
<p>本文将详细介绍如何在 .NET 环境下使用 C# 部署和调用 AI 图像分类模型。我们将从环境搭建、模型选择,到模型调用,再到实际应用场景,逐步展开讲解,并提供丰富的代码示例和实践指导,帮助开发者快速上手并应用到实际项目中。</p>
<p class="maodian"><a name="_label1"></a></p><h2>准备工作</h2>
<p>在开始实现图像分类之前,我们需要准备必要的开发环境和工具。以下是所需的软件和库:</p>
<ul><li>Visual Studio:Visual Studio 2022。</li><li>.NET SDK:安装 .NET 6.0 或更高版本,确保支持最新的功能和性能优化。</li><li>ML.NET:微软提供的开源机器学习框架,专为 .NET 开发者设计,支持模型训练和推理。</li><li>模型文件:我们将使用预训练的图像分类模型 tensorflow_inception_graph.pb。</li></ul>
<p class="maodian"><a name="_label2"></a></p><h2>安装步骤</h2>
<p><strong>创建项目并添加依赖</strong>:在命令行中运行以下命令,创建一个控制台应用程序并安装必要的 NuGet 包:</p>
<div class="jb51code"><pre class="brush:csharp;">dotnet new console -n ImageClassificationDemo
cd ImageClassificationDemo
dotnet add package Microsoft.ML
dotnet add package Microsoft.ML.ImageAnalytics
dotnet add package Microsoft.ML.TensorFlow
dotnet add package SciSharp.TensorFlow.Redist</pre></div>
<p>完成以上步骤后,你的环境就准备好了。接下来,我们将选择一个合适的图像分类模型。</p>
<p class="maodian"><a name="_label3"></a></p><h2>图像分类模型的选择</h2>
<p>图像分类模型是基于监督学习的神经网络,其目标是将输入图像分配到预定义的类别中。在选择模型时,我们需要考虑模型的性能、计算复杂度和适用场景。以下是几种常见的图像分类模型:</p>
<ul><li>卷积神经网络(CNN):如 LeNet、AlexNet 和 VGGNet,适合基本的图像分类任务,但层数较深时可能面临梯度消失问题。</li><li>残差网络(ResNet):通过引入残差连接(skip connections),解决了深层网络的训练难题,适用于高精度分类任务。</li><li>EfficientNet:通过平衡网络深度、宽度和分辨率,提供高效的性能,适合资源受限的场景。</li></ul>
<p class="maodian"><a name="_label4"></a></p><h2>模型训练与导出</h2>
<p>考虑到时间和资源成本,我们将直接使用预训练的 <strong>tensorflow_inception_graph.pb</strong> 模型。如果你有自定义需求,可以使用以下步骤训练并导出模型:</p>
<ul><li><strong>数据准备</strong>:收集并标注图像数据集,分为训练集和验证集。</li><li><strong>训练模型</strong>:使用 TensorFlow 或 PyTorch 等框架训练模型。</li><li><strong>导出模型</strong>:利用框架提供的导出工具导出模型。</li></ul>
<p>在本文中,我们选择 <strong>tensorflow_inception_graph.pb</strong> 作为示例模型,这是一种由Google开发的高性能卷积神经网络(CNN)架构。</p>
<blockquote><p>该模块通过并行使用不同大小的卷积核(如1x1、3x3、5x5)和池化层,提取图像的多尺度特征。这种设计提高了模型在图像分类任务中的表现,同时保持了计算效率。支持 1000 个类别的分类,且可以轻松集成到 .NET 中。</p></blockquote>
<p>大家可以直接点击<a href="https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip" rel="external nofollow"target="_blank"> tensorflow_inception_graph.pb </a>下载(文章最后也有下载方式)预训练的模型文件和分类文件,并将其放入项目目录中。</p>
<p>也可以到<a href="https://github.com/martinwicke/tensorflow-tutorial/blob/master/tensorflow_inception_graph.pb" rel="external nofollow"target="_blank">github</a>上下载(文章最后也有下载方式),里面的内容相对来说也更丰富些。</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025052309394921.png" /></p>
<p class="maodian"><a name="_label5"></a></p><h2>在 .NET 中调用模型</h2>
<p>现在,我们进入核心部分:在 .NET 中调用 tensorflow_inception_graph.pb。以下是逐步实现的过程。</p>
<p class="maodian"><a name="_lab2_5_0"></a></p><h3>1. 创建 .NET 项目</h3>
<p>使用命令行创建一个控制台应用,项目基本结构如下:</p>
<div class="jb51code"><pre class="brush:csharp;">ImageClassificationDemo/
├── ImageClassificationDemo.csproj
├── Program.cs
├── assets/inputs/inception/tensorflow_inception_graph.pb
├── assets/inputs/inception/imagenet_comp_graph_label_strings.txt</pre></div>
<p class="maodian"><a name="_lab2_5_1"></a></p><h3>2. 定义输入和输出数据结构</h3>
<p>如果在运行的时候报错说找不到模型或者label文件,可以进行如下操作:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025052309394922.png" /></p>
<p>输入类中定义数据的结构如下,后续会使用 <strong>TextLoader</strong> 加载数据时引用该类型。此处的类名为 <strong>ImageNetData</strong>:</p>
<div class="jb51code"><pre class="brush:csharp;"> public class ImageNetData
{
public string ImagePath;
public string Label;
public static IEnumerable<ImageNetData> ReadFromCsv(string file, string folder)
{
return File.ReadAllLines(file)
.Select(x => x.Split('\t'))
.Select(x => new ImageNetData { ImagePath = Path.Combine(folder, x), Label = x } );
}
}
public class ImageNetDataProbability : ImageNetData
{
public string PredictedLabel;
public float Probability { get; set; }
}</pre></div>
<blockquote><p>需要强调的是,<strong>ImageNetData</strong> 类中的标签在使用 TensorFlow 模型进行评分时并没有真正使用。而是在测试预测时使用它,这样就可以将每个样本数据的实际标签与 TensorFlow 模型提供的预测标签进行比较。</p></blockquote>
<p>输出类的结构如下:</p>
<div class="jb51code"><pre class="brush:csharp;">public class ImageNetPrediction
{
public float[] PredictedLabels;
}</pre></div>
<p>Inception 模型还需要几个传入的默认参数:</p>
<div class="jb51code"><pre class="brush:csharp;">public struct ImageNetSettings
{
public const int imageHeight = 224;
public const int imageWidth = 224;
public const float mean = 117;
public const bool channelsLast = true;
} </pre></div>
<p class="maodian"><a name="_lab2_5_2"></a></p><h3>3. 定义 estimator 管道</h3>
<blockquote><p>在处理深度神经网络时,必须使图像适应网络期望的格式。这就是图像被调整大小然后转换的原因(主要是像素值在所有R,G,B通道上被归一化)。</p></blockquote>
<div class="jb51code"><pre class="brush:csharp;">var pipeline = mlContext.Transforms.LoadImages(outputColumnName: "input", imageFolder: imagesFolder, inputColumnName: nameof(ImageNetData.ImagePath))
.Append(mlContext.Transforms.ResizeImages(outputColumnName: "input", imageWidth: ImageNetSettings.imageWidth, imageHeight: ImageNetSettings.imageHeight, inputColumnName: "input"))
.Append(mlContext.Transforms.ExtractPixels(outputColumnName: "input", interleavePixelColors: ImageNetSettings.channelsLast, offsetImage: ImageNetSettings.mean))
.Append(mlContext.Model.LoadTensorFlowModel(modelLocation)
.ScoreTensorFlowModel(outputColumnNames: new[] { "softmax2" }, inputColumnNames: new[] { "input" },
addBatchDimensionInput:true));</pre></div>
<p>运行代码后,模型将被成功加载到内存中,接下来我们可以调用它进行图像分类。</p>
<blockquote><p>通常情况下,这里经常报的错就是输入/输出节点的名称不正确,你可以通过 Netron (https://netron.app/)工具查看输入/输出节点的名称。</p>
<p>因为这两个节点的名称后面会在 estimator 的定义中使用:在 inception 网络的情况下,输入张量命名为 ‘input’,输出命名为 ‘softmax2’。</p></blockquote>
<p>下图是通过 <strong>Netron</strong> 读取的 <strong>tensorflow_inception_graph.pb</strong> 模型分析图:</p>
<p style="text-align:center"><img alt="输入张量名" src="https://img.jbzj.com/file_images/article/202505/2025052309395023.png" /></p>
<p style="text-align:center"><img alt="输出张量名" src="https://img.jbzj.com/file_images/article/202505/2025052309395024.png" /></p>
<p class="maodian"><a name="_label6"></a></p><h2>4. 提取预测结果</h2>
<p>填充 <strong>estimator</strong> 管道</p>
<div class="jb51code"><pre class="brush:csharp;">ITransformer model = pipeline.Fit(data);
var predictionEngine = mlContext.Model.CreatePredictionEngine<ImageNetData, ImageNetPrediction>(model);</pre></div>
<p>当获得预测结果后,我们会在属性中得到一个浮点数数组。数组中的每个位置都会分配到一个标签。</p>
<p>例如,如果模型有5个不同的标签,则数组将为length = 5。数组中的每个位置都表示标签在该位置的概率;所有数组值(概率)的和等于1。</p>
<p>然后,您需要选择最大的值(概率),并检查配给了该位置的那个以填充 estimator 管道标签。</p>
<p class="maodian"><a name="_label7"></a></p><h2>调用模型进行图像分类</h2>
<p>接下来我们需要编写代码来加载图像、进行预测并解析结果。</p>
<p class="maodian"><a name="_lab2_7_3"></a></p><h3>1. 准备素材与分类文件</h3>
<p>定义图像文件夹目录和图像分类目录。以下代码加载并预处理图像:</p>
<div class="jb51code"><pre class="brush:csharp;">string assetsRelativePath = @"../../../assets";
string assetsPath = GetAbsolutePath(assetsRelativePath);
string tagsTsv = Path.Combine(assetsPath, "inputs", "images", "tags.tsv");
string imagesFolder = Path.Combine(assetsPath, "inputs", "images");
string inceptionPb = Path.Combine(assetsPath, "inputs", "inception", "tensorflow_inception_graph.pb");
string labelsTxt = Path.Combine(assetsPath, "inputs", "inception", "imagenet_comp_graph_label_strings.txt");</pre></div>
<p class="maodian"><a name="_lab2_7_4"></a></p><h3>2. 加载模型</h3>
<div class="jb51code"><pre class="brush:csharp;">private PredictionEngine<ImageNetData, ImageNetPrediction> LoadModel(string dataLocation, string imagesFolder, string modelLocation)
{
ConsoleWriteHeader("Read model");
Console.WriteLine($"Model location: {modelLocation}");
Console.WriteLine($"Images folder: {imagesFolder}");
Console.WriteLine($"Training file: {dataLocation}");
Console.WriteLine($"Default parameters: image size=({ImageNetSettings.imageWidth},{ImageNetSettings.imageHeight}), image mean: {ImageNetSettings.mean}");
var data = mlContext.Data.LoadFromTextFile<ImageNetData>(dataLocation, hasHeader: true);
var pipeline = mlContext.Transforms.LoadImages(outputColumnName: "input", imageFolder: imagesFolder, inputColumnName: nameof(ImageNetData.ImagePath))
.Append(mlContext.Transforms.ResizeImages(outputColumnName: "input", imageWidth: ImageNetSettings.imageWidth, imageHeight: ImageNetSettings.imageHeight, inputColumnName: "input"))
.Append(mlContext.Transforms.ExtractPixels(outputColumnName: "input", interleavePixelColors: ImageNetSettings.channelsLast, offsetImage: ImageNetSettings.mean))
.Append(mlContext.Model.LoadTensorFlowModel(modelLocation).
ScoreTensorFlowModel(outputColumnNames: new[] { "softmax2" },
inputColumnNames: new[] { "input" }, addBatchDimensionInput:true));
ITransformer model = pipeline.Fit(data);
var predictionEngine = mlContext.Model.CreatePredictionEngine<ImageNetData, ImageNetPrediction>(model);
return predictionEngine;
}</pre></div>
<p class="maodian"><a name="_lab2_7_5"></a></p><h3>3. 解析输出结果</h3>
<div class="jb51code"><pre class="brush:csharp;">protected IEnumerable<ImageNetData> PredictDataUsingModel(string testLocation,
string imagesFolder,
string labelsLocation,
PredictionEngine<ImageNetData, ImageNetPrediction> model)
{
ConsoleWriteHeader("Classify images");
Console.WriteLine($"Images folder: {imagesFolder}");
Console.WriteLine($"Training file: {testLocation}");
Console.WriteLine($"Labels file: {labelsLocation}");
var labels = ReadLabels(labelsLocation);
var testData = ImageNetData.ReadFromCsv(testLocation, imagesFolder);
foreach (var sample in testData)
{
var probs = model.Predict(sample).PredictedLabels;
var imageData = new ImageNetDataProbability()
{
ImagePath = sample.ImagePath,
Label = sample.Label
};
(imageData.PredictedLabel, imageData.Probability) = GetBestLabel(labels, probs);
imageData.ConsoleWrite();
yield return imageData;
}
}</pre></div>
<p>在 <code>Main</code> 方法中调用,完整代码如下:</p>
<div class="jb51code"><pre class="brush:csharp;">static void Main(string[] args)
{
string assetsRelativePath = @"../../../assets";
string assetsPath = GetAbsolutePath(assetsRelativePath);
string tagsTsv = Path.Combine(assetsPath, "inputs", "images", "tags.tsv");
string imagesFolder = Path.Combine(assetsPath, "inputs", "images");
string inceptionPb = Path.Combine(assetsPath, "inputs", "inception", "tensorflow_inception_graph.pb");
string labelsTxt = Path.Combine(assetsPath, "inputs", "inception", "imagenet_comp_graph_label_strings.txt");
try
{
TFModelScorer modelScorer = new TFModelScorer(tagsTsv, imagesFolder, inceptionPb, labelsTxt);
modelScorer.Score();
}
catch (Exception ex)
{
ConsoleHelpers.ConsoleWriteException(ex.ToString());
}
ConsoleHelpers.ConsolePressAnyKey();
}</pre></div>
<p>运行程序后,你将看到类似以下的输出:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202505/2025052309395025.png" /></p>
<p class="maodian"><a name="_label8"></a></p><h2>其他实现方式</h2>
<p>在实际应用中,我们也可以使用ONNX模型,此处不做额外叙述。由于模型的性能和效率至关重要,只是提供一些优化建议:</p>
<ul><li><strong>模型量化</strong>:使用 ONNX Runtime 的量化工具,将模型从浮点数(FP32)转换为整数(INT8),减少模型大小和推理时间。</li><li><strong>硬件加速</strong>:结合 ONNX Runtime 的 GPU 支持,利用 CUDA 或 DirectML 加速推理。</li><li><strong>批处理</strong>:如果需要处理多张图像,可以将输入组织为批次(batch),提高吞吐量。例如:</li></ul>
<div class="jb51code"><pre class="brush:csharp;">var inputs = new List<ImageInput> { input1, input2, input3 };
var batchPrediction = mlContext.Data.LoadFromEnumerable(inputs);
var predictions = model.Transform(batchPrediction);</pre></div>
<ul><li><strong>缓存机制</strong>:对于频繁使用的模型,保持预测引擎的单例实例,避免重复加载。</li></ul>
<p>通过这些优化,模型可以在 .NET 环境中实现更高的性能,满足实时应用的需求。</p>
<p class="maodian"><a name="_label9"></a></p><h2>实际应用场景</h2>
<p>图像分类模型在 .NET 应用中有广泛的用途,以下是几个典型场景:</p>
<p><strong>医疗影像分析</strong><br />在医疗系统中,部署图像分类模型可以辅助医生识别 X 光片或 MRI 图像中的异常。例如,检测肺部结节或肿瘤。</p>
<p><strong>智能安防</strong><br />在监控系统中,模型可以实时识别可疑物体或行为,如检测闯入者或遗留物品。</p>
<p><strong>电子商务</strong><br />在商品管理系统中,自动分类上传的商品图像,提升搜索和推荐的准确性。</p>
<p class="maodian"><a name="_label10"></a></p><h2>挑战与解决方案</h2>
<ul><li><strong>数据隐私</strong>:通过加密传输和本地推理保护用户数据。</li><li><strong>模型更新</strong>:定期从云端下载新模型,并使用版本控制管理。</li><li><strong>计算资源</strong>:在资源受限的设备上,使用轻量化模型(如 MobileNet)。</li></ul>
<p class="maodian"><a name="_label11"></a></p><h2>结论</h2>
<p>本文详细介绍了如何在 .NET 环境下使用 C# 部署和调用 AI 图像分类模型。从环境搭建到模型选择、部署与调用,再到性能优化和应用场景,我们提供了一套完整的实践指南。通过 ML.NET 和预测模式的支持,开发者可以轻松地将强大的 AI 能力集成到 .NET 应用中。</p>
<p>随着 AI 技术的不断进步和 .NET 平台的持续发展,二者的结合将为开发者带来更多可能性。无论是构建智能桌面应用、Web 服务还是跨平台解决方案,图像分类模型都能为项目增添创新价值。希望本文能为你的 AI 之旅提供启发和帮助!</p>
<p class="maodian"><a name="_label12"></a></p><h2>参考资料</h2>
<ul><li>素材下载地址: https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip</li><li>Netron工具地址: https://netron.app/</li><li>224x224图像素材: https://www.kaggle.com/datasets/abhinavnayak/catsvdogs-transformed/data</li><li>tensorflow教程及模型文件和label文件: https://github.com/martinwicke/tensorflow-tutorial</li><li>Image Classification - Scoring sample: https://github.com/dotnet/machinelearning-samples/blob/main/samples/csharp/getting-started/DeepLearning_ImageClassification_TensorFlow/README.md</li><li>ML.NET 官方文档: https://dotnet.microsoft.com/apps/machinelearning-ai/ml-dotnet</li><li>ONNX Model Zoo: https://github.com/onnx/models</li></ul>
頁:
[1]