机器学习框架ML.NET学习笔记【6】TensorFlow图片分类

一、概述通过之前两篇文章的学习,我们应该已经了解了多元分类的工作原理,图片的分类其流程和之前完全一致,其中最核心的问题就是特征的提取,只要完成特征提取,分类算法就很好处理了,具体流程如下:之前介绍过,图片的特征是不能采用像素的灰度值的,这部分原理的台阶有点高,还好可以直接使用通过TensorFlow训练过的特征提取模型(美其名曰迁移学习)。模型文件为:tensorflow_inception_gr...

机器学习框架ML.NET学习笔记【6】TensorFlow图片分类

一、概述

通过之前两篇文章的学习,我们应该已经了解了多元分类的工作原理,图片的分类其流程和之前完全一致,其中最核心的问题就是特征的提取,只要完成特征提取,分类算法就很好处理了,具体流程如下:

之前介绍过,图片的特征是不能采用像素的灰度值的,这部分原理的台阶有点高,还好可以直接使用通过TensorFlow训练过的特征提取模型(美其名曰迁移学习)。

模型文件为:tensorflow_inception_graph.pb

二、样本介绍

我随便在网上找了一些图片,分成6类:男孩、女孩、猫、狗、男人、女人。tags文件标记了每个文件所代表的类型标签(Label)。

通过对这六类图片的学习,期望输入新的图片时,可以判断出是何种类型。

三、代码

全部代码:

namespace TensorFlow_ImageClassification{  class Program {  //Assets files download from:https://gitee.com/seabluescn/ML_Assets  static readonly string AssetsFolder = @"D:\StepByStep\Blogs\ML_Assets";  static readonly string TrainDataFolder = Path.Combine(AssetsFolder, "ImageClassification", "train");  static readonly string TrainTagsPath = Path.Combine(AssetsFolder, "ImageClassification", "train_tags.tsv");  static readonly string TestDataFolder = Path.Combine(AssetsFolder, "ImageClassification","test");  static readonly string inceptionPb = Path.Combine(AssetsFolder, "TensorFlow", "tensorflow_inception_graph.pb");  static readonly string imageClassifierZip = Path.Combine(Environment.CurrentDirectory, "MLModel", "imageClassifier.zip");  //配置用常量  private struct ImageNetSettings  {public const int imageHeight = 224;public const int imageWidth = 224;public const float mean = 117;public const float scale = 1;public const bool channelsLast = true;  }  static void Main(string[] args)  {TrainAndSaveModel();LoadAndPrediction();Console.WriteLine("Hit any key to finish the app");Console.ReadKey();  }  public static void TrainAndSaveModel()  {MLContext mlContext = new MLContext(seed: 1);// STEP 1: 准备数据var fulldata = mlContext.Data.LoadFromTextFile<ImageNetData>(path: TrainTagsPath, separatorChar: '\t', hasHeader: false);var trainTestData = mlContext.Data.TrainTestSplit(fulldata, testFraction: 0.1);var trainData = trainTestData.TrainSet;var testData = trainTestData.TestSet;// STEP 2:创建学习管道var pipeline = mlContext.Transforms.Conversion.MapValueToKey(outputColumnName: "LabelTokey", inputColumnName: "Label") .Append(mlContext.Transforms.LoadImages(outputColumnName: "input", imageFolder: TrainDataFolder, 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(inceptionPb).ScoreTensorFlowModel(outputColumnNames: new[] { "softmax2_pre_activation" }, inputColumnNames: new[] { "input" }, addBatchDimensionInput: true)) .Append(mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy(labelColumnName: "LabelTokey", featureColumnName: "softmax2_pre_activation")) .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabelValue", "PredictedLabel")) .AppendCacheCheckpoint(mlContext);// STEP 3:通过训练数据调整模型 ITransformer model = pipeline.Fit(trainData);// STEP 4:评估模型Console.WriteLine("===== Evaluate model =======");var evaData = model.Transform(testData);var metrics = mlContext.MulticlassClassification.Evaluate(evaData, labelColumnName: "LabelTokey", predictedLabelColumnName: "PredictedLabel");PrintMultiClassClassificationMetrics(metrics);//STEP 5:保存模型Console.WriteLine("====== Save model to local file =========");mlContext.Model.Save(model, trainData.Schema, imageClassifierZip);  }  static void LoadAndPrediction()  {MLContext mlContext = new MLContext(seed: 1);// Load the model  
源文地址:https://www.guoxiongfei.cn/cntech/18405.html