dotnet / dotnet/machinelearning
ML.Net Schema mismatch for feature column 'ImagePath': expected VarVector<Byte>, got String
- Dominant language
- C#
- Stars
- 9.4k
- Forks
- 2k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 11
Description
### System information
- **OS version/distro**:
- **.NET Version (eg., dotnet --info)**:
### Issue
- **What did you do?**
I'm learning ML.Net, and this maybe a simple question so forgive me in advance if it is but hardly any tutorials I've watched on youtube take the time to explain the code to the viewer. All I'm trying to do is follow a tutorial found [here](https://www.youtube.com/watch?v=bXTN-rnwDso&t=421s&ab_channel=JonWood), I got stuck at 6:58 in the video when I'm supposed to call the fit function.
- **What happened?**
I get the error "'Schema mismatch for feature column 'ImagePath': expected VarVector, got String (Parameter 'inputSchema')'" Which is weird because of course the video uploader didn't get this.
- **What did you expect?**
I just expected to get the model when the fit function was called and continue the code.
### Source code / logs
1) **Main Code (Program.cs)**
` string baseDir = Directory.GetCurrentDirectory();
string realDir = Path.GetFullPath(Path.Combine(baseDir, @"..\..\..\"));
string imgFolderDir = realDir + "images\\";
// Get ref to images folder.
//var imgFolder = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "images");
// Go to images folder, get all files (*) from both folders.
var file = Directory.GetFiles(imgFolderDir, "*", SearchOption.AllDirectories);
/* For each file, create an imagedata object. */
var images = file.Select(f => new ImageData
{
ImagePath = f,
Label = Directory.GetParent(f).Name
});
// Begin algorithm for ML.
MLContext ml = new MLContext();
/* Load the images.
Shuffle them (for more randomness?)
*/
var imageData = ml.Data.LoadFromEnumerable(images);
var imgDataShuffle = ml.Data.ShuffleRows(imageData);
// Split because we'd like to get a portion for training and another for testing.
var testTrainData = ml.Data.TrainTestSplit(imgDataShuffle, testFraction: 0.2);
var validateData = ml.Transforms.Conversion.MapValueToKey("LabelKey", "Label",
keyOrdinality: Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality.ByValue)
.Fit(testTrainData.TestSet)
.Transform(testTrainData.TestSet);
var pipeline = ml.MulticlassClassification.Trainers.ImageClassification(featureColumnName: "ImagePath")
.Append(ml.Transforms.Conversion.MapKeyToValue(outputColumnName: "PredictedLabel",
inputColumnName: "PredictedLabel"));
var model = pipeline.Fit(testTrainData.TrainSet);
var predictions = model.Transform(testTrainData.TestSet);
var metrics = ml.MulticlassClassification.Evaluate(predictions, labelColumnName: "LabelKey",
predictedLabelColumnName: "PredictedLabel");
Console.WriteLine("Log loss: {0}.", metrics.LogLoss);
var predictionEngine = ml.Model.CreatePredictionEngine(model);
var testImagesFolder = Path.Combine(Environment.CurrentDirectory, "..", "..", "..", "test");
var testFiles = Directory.GetFiles(testImagesFolder, "*", SearchOption.AllDirectories);
var testImages = testFiles.Select(file => new ImageData
{
ImagePath = file
});
VBuffer> keys = default;
predictionEngine.OutputSchema["LabelKey"].GetKeyValues(ref keys);
var originalLabels = keys.DenseValues().ToArray();
foreach (var image in testImages)
{
// Get the prediction using the prediction engine itself of course.
var prediction = predictionEngine.Predict(image);
// And get the label.
var labelIndex = prediction.PredictedLabel;
// Write it out.
Console.WriteLine("-Image Path:- {0}. -Score:- {1}. -Predicted Label- {2}.",
Path.GetFileName(image.ImagePath),
prediction.Score.Max(),
originalLabels[labelIndex]);
}
Console.ReadLine();
2) **ImageData (Input)**
` public class ImageData
{
[LoadColumn(0)]
public string ImagePath;
[LoadColumn(1)]
public string Label;
}`
3) **ImagePrediction (Output)**
` public class ImagePrediction
{
// Scores we get from the model. How sure it is on a guess.
[ColumnName("Score")]
public float[] Score;
// The actual label. DNN doesn't give us a string for it.
[ColumnName("PredictedLabel")]
public uint PredictedLabel;
}`
Also, here's a [link](https://github.com/jwood803/MLNetExamples/blob/master/MLNetExamples/DeepNeuralNetwork/Program.cs) to the guy in the videos github so you can compare and contrast my code to his. What exactly am I doing wrong?
Contributor guide
Assessment
This issue has not been assessed yet.