ImageClassification

A ML.NET wrapper to ease image classification.


Keywords
ML.NET, image-classification
License
MIT
Install
Install-Package ImageClassification -Version 2.0.0.1

Documentation

ImageClassification

A NuGet package for image classification wrapper using ML.NET. https://www.nuget.org/packages/ImageClassification/

Usage

Prepare

  1. Prepare the foldered data as shown below. 100 images of each is enough.

Image

  1. That's all!

For those who want to prepare their own images using a webcam, this repo is kindly provided :) https://github.com/teonsen/WebcamImageClassification

Train and generate the model

using ImageClassification;
using ImageClassification.IO;

// Define data-set folder.
string dataDir = @"C:\Data\Fruits";
            
// Define hyper-paramters such as Epoch or BatchSize.
var hp = new HyperParameter {
    Epoch = 200,
    BatchSize = 10,
    LearningRate = 0.01f,
    eTrainerArchitecture = eTrainerArchitectures.ResnetV250,
    TestFraction = 0.3f,
    ResultsToShow = 10
};

// Train and generate the model.
var results = Trainer.GenerateModel(dataDir, hp);
// Save the results as HTML file.
results.SaveAsHTML();

Once you run the code above, pipeline.zip and model.zip will be created in the dataset folder.

Classify

To predict an image, pass the pipeline and model.zip output by Trainer.GenerateModel() above, as well as the image file, like following.

// Classify the single image.
string imageToClassify = @"C:\your\imageToClassify(apple_or_banana_or_orange).png";
var p = Classifier.GetSingleImagePrediction(results.Resultfiles.PipelineZip, results.Resultfiles.ModelZip, imageToClassify);
Console.WriteLine($@"Predicted image label is: ""{p.PredictedLabel}"". Score:{p.HighScore}");

Acknowledgements

  • Most part of the code is originaly written by @Hiromasa-Masuda
  • Thank you for all the effort to create ML.NET and SciSharp