Buenas!
Nueva version de Machine Learning .Net y esta vez la novedad principal es la capacidad de utilizar modelos de TensorFlow en ML.Net.
Durante el proceso de creación de una Pipeline, ahora podemos utilizar modelos frezados de TF y utilizar los mismos para entrenar un modelo y realizar predicciones. En una aplicación de Consola, al momento de agregar los paquetes de ML.Net podemos ver una serie nueva de paquetes para trabajar con TensorFlow
El siguiente código es la mejor forma de entender como funciona una Pipeline con un modelo de TF. En la definición de la misma se utilizan un par de trainers para cargar y modificar imágenes; y luego en la línea 40 se carga el modelo para su utilización.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Microsoft.ML; | |
using Microsoft.ML.Runtime.Api; | |
using Microsoft.ML.Trainers; | |
using Microsoft.ML.Transforms; | |
namespace MLNetConsole10 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const int imageHeight = 32; | |
const int imageWidth = 32; | |
const string modelLocation = "cifar_model/frozen_model.pb"; | |
const string dataFile = "images/images.tsv"; | |
var imageFolder = Path.GetDirectoryName(dataFile); | |
var pipeline = new LearningPipeline(); | |
pipeline.Add(new Microsoft.ML.Data.TextLoader(dataFile).CreateFrom<CifarData>()); | |
pipeline.Add(new ImageLoader(("ImagePath", "ImageReal")) | |
{ | |
ImageFolder = imageFolder | |
}); | |
pipeline.Add(new ImageResizer(("ImageReal", "ImageCropped")) | |
{ | |
ImageHeight = imageHeight, | |
ImageWidth = imageWidth, | |
Resizing = ImageResizerTransformResizingKind.IsoCrop | |
}); | |
pipeline.Add(new ImagePixelExtractor(("ImageCropped", "Input")) | |
{ | |
UseAlpha = false, | |
InterleaveArgb = true | |
}); | |
pipeline.Add(new TensorFlowScorer() | |
{ | |
ModelFile = modelLocation, | |
InputColumns = new[] { "Input" }, | |
OutputColumn = "Output" | |
}); | |
pipeline.Add(new ColumnConcatenator("Features", "Output")); | |
pipeline.Add(new TextToKeyConverter("Label")); | |
pipeline.Add(new StochasticDualCoordinateAscentClassifier()); | |
var model = pipeline.Train<CifarData, CifarPrediction>(); | |
model.TryGetScoreLabelNames(out var scoreLabels); | |
Console.WriteLine($"ScoreLabels.Length {scoreLabels.Length}"); | |
Console.WriteLine($"banana {scoreLabels[0]}"); | |
Console.WriteLine($"hotdog {scoreLabels[1]}"); | |
Console.WriteLine($"tomato {scoreLabels[2]}"); | |
var prediction = model.Predict(new CifarData() | |
{ | |
ImagePath = "images/banana.jpg" | |
}); | |
Console.WriteLine($"{prediction.PredictedLabels[0]}"); | |
Console.WriteLine($"{prediction.PredictedLabels[1]}"); | |
Console.WriteLine($"{prediction.PredictedLabels[2]}"); | |
} | |
} | |
public class CifarData | |
{ | |
[Column("0")] | |
public string ImagePath; | |
[Column("1")] | |
public string Label; | |
} | |
public class CifarPrediction | |
{ | |
[ColumnName("Score")] | |
public float[] PredictedLabels; | |
} | |
} |
En este momento el archivo [cifar_model/frozen_model.pb] no es parte del repositorio de ML.Net. Si quieres probar el código, puedes descargar una version de prueba desde https://github.com/deeplearning4j/dl4j-test-resources/tree/master/src/main/resources/tf_graphs/examples/yolov2_608x608
Happy Coding!
Saludos @ Toronto
El Bruno
References
My Posts
- New version 0.4, news Improvements in Text analysis using Word Embedding
- Error ‘Entry point ‘Trainers.LightGbmClassifier’ not found’ and how to fix it
- Machine Learning Glossary of terms
- Export Machine Learning.Net models to ONNX format
- Loading Data In our Learning Pipeline With List (Lists for ever!)
- What’s new in version 0.2.0
- What’s a Machine Learning model? A 7 minute video as the best possible explanation
- Write and Load models using Machine Learning .Net
- Understanding the step by step of Hello World
- Hello World in ML.Net, Machine Learning for .Net !