dotnet / machinelearning-samples

Samples for ML.NET, an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
4.5k stars 2.69k forks source link

Consuming a trained model and choosing the input image #793

Closed Phoenix-313 closed 4 years ago

Phoenix-313 commented 4 years ago

Hi,

In the tutorial "Tutorial: Automated visual inspection using transfer learning with the ML.NET Image Classification API", how can the trained model be saved and consumed for later? and how to choose the input image that the trained model will classify?

Many thanks!

luisquintanilla commented 4 years ago

Hi @Phoenix-313 ,

When you train using the image classification API, 2 models are created. In the workspace directory that's provided in the sample, the TensorFlow .pb version of the model is saved. However, I'm guessing you want to use the ML.NET version of the model. That model in this case is the trainedModel ITransformer object. This model you can use just like you would any other model in ML.NET.

See this example for how to save the model.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.ml.visioncatalog.imageclassification?view=ml-dotnet#Microsoft_ML_VisionCatalog_ImageClassification_Microsoft_ML_MulticlassClassificationCatalog_MulticlassClassificationTrainers_Microsoft_ML_Vision_ImageClassificationTrainer_Options_

// Save the trained model.
mlContext.Model.Save(trainedModel, shuffledFullImagesDataset.Schema,"model.zip");

// Load the trained and saved model for prediction.
ITransformer loadedModel;
DataViewSchema schema;
using (var file = File.OpenRead("model.zip"))
    loadedModel = mlContext.Model.Load(file, out schema);

In terms of providing a new image, you would create an IDataView that contains the path of the image you want to predict and using the model you'd create a PredictionEngine for a single prediction or use the Transform operation for multiple predictions.

Here's some more general guidance:

Save/Load Model: https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/save-load-machine-learning-models-ml-net

Use the model to make predictions: https://docs.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/machine-learning-model-predictions-ml-net

Hope this helps.