I'm new with the ML.NET and have a problem which i cannot resolve for few days. I can train my model, but when i try to CreateEnginePrediciton, i have an error: "Feature column 'Feature' not found" Here is my code:
var context = new MLContext(seed: 0);
// Create a DataView containing the image paths and labels
var input = LoadLabeledImagesFromPath(_imagePath);
var data = context.Data.LoadFromEnumerable(input);
data = context.Data.ShuffleRows(data);
// Load the images and convert the labels to keys to serve as categorical values
var images = context.Transforms.Conversion.MapValueToKey(inputColumnName: nameof(Input.Label), outputColumnName: _keyColumnName)
.Append(context.Transforms.LoadRawImageBytes(inputColumnName: nameof(Input.ImagePath), outputColumnName: nameof(Input.Image), imageFolder: _imagePath));
var dataPrepModel = images.Fit(data);
var dataPrepDataView = dataPrepModel.Transform(data);
// Split the dataset for training and testing
var trainTestData = context.Data.TrainTestSplit(dataPrepDataView, testFraction: 0.2, seed: 1);
var trainData = trainTestData.TrainSet;
var testData = trainTestData.TestSet;
// Create an image-classification pipeline and train the model
var options = new ImageClassificationTrainer.Options()
{
FeatureColumnName = nameof(Input.Image),
LabelColumnName = _keyColumnName,
ValidationSet = testData,
Arch = ImageClassificationTrainer.Architecture.ResnetV2101, // Pretrained DNN
MetricsCallback = (metrics) => Console.WriteLine(metrics),
TestOnTrainSet = false
};
var pipeline = context.MulticlassClassification.Trainers.ImageClassification(options)
.Append(context.Transforms.Conversion.MapKeyToValue(_predictedLabelColumnName));
Console.WriteLine("Training the model...");
var model = pipeline.Fit(trainData);
// Evaluate the model and show the results
var predictions = model.Transform(trainData);
var metrics = context.MulticlassClassification.Evaluate(predictions, labelColumnName: _keyColumnName, predictedLabelColumnName: _predictedLabelColumnName);
Console.WriteLine();
Console.WriteLine($"Macro accuracy = {metrics.MacroAccuracy:P2}");
Console.WriteLine($"Micro accuracy = {metrics.MicroAccuracy:P2}");
Console.WriteLine(metrics.ConfusionMatrix.GetFormattedConfusionTable());
Console.WriteLine();
// Save the model
Console.WriteLine();
Console.WriteLine("Saving the model...");
context.Model.Save(model, trainData.Schema, _savePath);
}
private static List<Input> LoadLabeledImagesFromPath(string path)
{
var images = new List<Input>();
var directories = Directory.EnumerateDirectories(path);
foreach (var directory in directories)
{
var files = Directory.EnumerateFiles(directory);
images.AddRange(files.Select(x => new Input
{
ImagePath = Path.GetFullPath(x),
Label = Path.GetFileName(directory)
}));
}
return images;
And my classess:
public class Input
{
public byte[] Image;
public string ImagePath;
public string Label;
}
public class Output
{
public float[] Score;
public string PredictedLabel;
}
And code where i try to create prediciton engine:
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
{
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var modelInputSchema);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
return predEngine;
}
Could you help me? Code is mostly from ML.NET tutorials and i can train it but cannot use it :-(