ML.NET(二) 使用机器学习预测表情分析

这个例子使用模型进行表情分析:

准备数据: happy,sad 等;

using Common;
using ConsoleApp2;
using Microsoft.ML;
using Microsoft.ML.Data;
using System.Diagnostics;
using static Microsoft.ML.Transforms.ValueToKeyMappingEstimator;


/*
 * 训练一个Happy 和Sad 等表情的模型并使用预测  图像分类器(Image Classification) 
 *  ***********************预测不是很准,数据集找对应人种数据可以尝试亚洲,欧美分开
 */

var projectDirectory = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "./"));
var workspaceRelativePath = Path.Combine(projectDirectory, "workspace");
var assetsRelativePath = Path.Combine(projectDirectory, "assets");


string outputMlNetModelFilePath = "model.zip";//Path.Combine("", "outputs", "imageClassifier.zip");
string imagesFolderPathForPredictions = Path.Combine("", "inputs", "test-images");
// 设置ML.NET环境
var mlContext = new MLContext();

// 加载数据


IEnumerable<ImageData> images = LoadImagesFromDirectory(folder: assetsRelativePath, useFolderNameAsLabel: true);
IDataView fullImagesDataset = mlContext.Data.LoadFromEnumerable(images);
IDataView shuffledFullImageFilePathsDataset = mlContext.Data.ShuffleRows(fullImagesDataset);

// 3. Load Images with in-memory type within the IDataView and Transform Labels to Keys (Categorical)
IDataView shuffledFullImagesDataset = mlContext.Transforms.Conversion.
        MapValueToKey(outputColumnName: "LabelAsKey", inputColumnName: "Label", keyOrdinality: KeyOrdinality.ByValue)
    .Append(mlContext.Transforms.LoadRawImageBytes(
                                    outputColumnName: "Image",
                                    imageFolder: assetsRelativePath,
                                    inputColumnName: "ImagePath"))
    .Fit(shuffledFullImageFilePathsDataset)
    .Transform(shuffledFullImageFilePathsDataset);


// 4. Split the data 80:20 into train and test sets, train and evaluate.
var trainTestData = mlContext.Data.TrainTestSplit(shuffledFullImagesDataset, testFraction: 0.2);
IDataView trainDataView = trainTestData.TrainSet;
IDataView testDataView = trainTestData.TestSet;

// 5. Define the model's training pipeline using DNN default values
//
var pipeline = mlContext.MulticlassClassification.Trainers
        .ImageClassification(featureColumnName: "Image",
                             labelColumnName: "LabelAsKey",
                             validationSet: testDataView)
    .Append(mlContext.Transforms.Conversion.MapKeyToValue(outputColumnName: "PredictedLabel",
                                                          inputColumnName: "PredictedLabel"));
// Measuring training time
var watch = Stopwatch.StartNew();
Console.WriteLine($"--------------------开始训练-------------------------------");

//6. Train
ITransformer trainedModel = pipeline.Fit(trainDataView);


watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;

Console.WriteLine($"--------------------训练用时: {elapsedMs / 1000} seconds --------------------");

// 7. Get the quality metrics (accuracy, etc.)
EvaluateModel(mlContext, testDataView, trainedModel);

 8. Save the model to assets/outputs (You get ML.NET .zip model file and TensorFlow .pb model file)
mlContext.Model.Save(trainedModel, trainDataView.Schema, outputMlNetModelFilePath); //outputMlNetModelFilePath
Console.WriteLine($"Model saved to: {outputMlNetModelFilePath}");

 9. Try a single prediction simulating an end-user app
TrySinglePrediction(imagesFolderPathForPredictions, mlContext, trainedModel);


static IEnumerable<ImageData> LoadImagesFromDirectory(
   string folder,
   bool useFolderNameAsLabel = true)
   => FileUtils.LoadImagesFromDirectory(folder, useFolderNameAsLabel)
       .Select(x => new ImageData(x.imagePath, x.label));

static void EvaluateModel(MLContext mlContext, IDataView testDataset, ITransformer trainedModel)
{
    Console.WriteLine("Making predictions in bulk for evaluating model's quality...");

    // Measuring time
    var watch = Stopwatch.StartNew();

    var predictionsDataView = trainedModel.Transform(testDataset);

    var metrics = mlContext.MulticlassClassification.Evaluate(predictionsDataView, labelColumnName: "LabelAsKey", predictedLabelColumnName: "PredictedLabel");
    ConsoleHelper.PrintMultiClassClassificationMetrics("TensorFlow DNN Transfer Learning", metrics);

    watch.Stop();
    var elapsed2Ms = watch.ElapsedMilliseconds;

    Console.WriteLine($"Predicting and Evaluation took: {elapsed2Ms / 1000} seconds");
}
static void TrySinglePrediction(string imagesFolderPathForPredictions, MLContext mlContext, ITransformer trainedModel)
{
    // Create prediction function to try one prediction
    var predictionEngine = mlContext.Model
        .CreatePredictionEngine<InMemoryImageData, ImagePrediction>(trainedModel);

    var testImages = FileUtils.LoadInMemoryImagesFromDirectory(
        imagesFolderPathForPredictions, false);

    var imageToPredict = testImages.Last
        ();

    var prediction = predictionEngine.Predict(imageToPredict);

    Console.WriteLine(
        $"Image Filename : [{imageToPredict.ImageFileName}], " +
        $"Scores : [{string.Join(",", prediction.Score)}], " +
        $"Predicted Label : {prediction.PredictedLabel}");
}
// 定义数据结构
class ImageData
{
    public ImageData(string imagePath, string label)
    {
        ImagePath = imagePath;
        Label = label;
    }

    public readonly string ImagePath;

    public readonly string Label;
}

class ModelInput
{
    public byte[] Image { get; set; }

    public UInt32 LabelAsKey { get; set; }

    public string ImagePath { get; set; }

    public string Label { get; set; }
}
class ModelOutput
{
    public string ImagePath { get; set; }

    public string Label { get; set; }

    public string PredictedLabel { get; set; }
}
public class ImagePrediction
{
    [ColumnName("Score")]
    public float[] Score;

    [ColumnName("PredictedLabel")]
    public string PredictedLabel;
}
相关推荐
VinciYan1 小时前
基于Jenkins+Docker的自动化部署实践——整合Git与Python脚本实现远程部署
python·ubuntu·docker·自动化·jenkins·.net·运维开发
码农君莫笑1 小时前
使用blazor开发信息管理系统的应用场景
数据库·信息可视化·c#·.net·visual studio
de之梦-御风4 小时前
【进阶编程】MVVM框架的每层对应那些业务
.net·个人开发·mvvm
Zhen (Evan) Wang4 小时前
利用.NET Upgrade Assitant对项目进行升级
.net
界面开发小八哥5 小时前
DevExpress WPF中文教程:Grid - 如何移动和调整列大小?(二)
ui·.net·wpf·界面控件·devexpress·ui开发
大G哥5 小时前
深入理解.NET内存回收机制
jvm·.net
小码编匠8 小时前
WPF 星空效果:创建逼真的宇宙背景
后端·c#·.net
一只小灿灿1 天前
VB.NET在 Excel 二次开发中的全面应用
.net·excel
码农君莫笑1 天前
信管通低代码信息管理系统应用平台
linux·数据库·windows·低代码·c#·.net·visual studio