Skip to content
This repository was archived by the owner on Feb 13, 2026. It is now read-only.

(User) Training an image classification model

Bùi Nguyễn Tấn Sang edited this page Jul 19, 2024 · 2 revisions

An image classification model allows you to classify input images into labels. There are many applications with this type of model, such as garbage classification, animal classification, object classification, etc.
So you can try training such a model to apply in your application with minimal coding skills.

Creating a new project

First, let's create an Image Classification project in Trainer Studio:

image

Here do the same steps as instructed here leaving a new project with YourProjectName as your project name (Feel free to change it to your liking), next select CSharp for the language (currently image classification is only CSharp support) and select Image Classification for the project type.

image

After creating the project, you will get the above window, now go to the Prepare your data tab

Prepare your training data

Dataset file

Could you prepare a zip file containing many folders, each folder corresponding to a label? Each folder/label contains many pictures related to it (e.g. dog pictures and cat pictures into 2 folders dog and cat). Overall your zip file will look like this:

YourZipFile.zip
├── Label1
│   ├── 1.png
│   ├── a.jpg
│   ├── x.webp
│   └── ...
├── Label2
│   ├── 1.png
│   ├── 2.png
│   ├── 3.png
│   └── ...
└── ...

For example:

MyDataSet.zip
├── Cat
│   ├── cat1.png
│   ├── cat2.png
│   └── cat3.jpg
└── Dog
    ├── dog1.png
    ├── dog2.jpg
    └── dog3.webp

Import your training data file

Once you have prepared your training data, you can use it, go to the Prepare your data tab in the project window, and the system will automatically prepare it for you, depending on data size, you may have to wait a few minutes:

image

image

Note, that if your data is in the required structure, you can go to the next step, but it will notify you if the data does not match:

image

Train your model

After completing the data creation, Ready for train your model will be notified and the Train button will become active on the Train the model tab, now make sure everything is okay before clicking it. When the Train button is pressed, the application will temporarily become unresponsive until the job is completed:

image

image

Test your trained model

Go to the Test trained model tab to test your model if you want, or you can skip this step:

image

Press Choose Image to select the image you want the model to classify, as soon as you load the image it will start classifying, and the application will stop working within a few seconds to a few minutes depending on the model size of your image, then it will display the classification label and work again:

image

Get the outputs/results

Once you have completed training, you can click to go to the Use it in your app tab, where the Open output folder button will open the outputs folder, which includes the necessary model files. Below is the C# code that allows you to use it in your application:

image

private MLContext mlContext;
private PredictionEngine<ImageData, ImagePrediction> predictionEngine;
mlContext = new MLContext();
DataViewSchema predictionPipelineSchema;
ITransformer predictionPipeline = mlContext.Model.Load($"model.zip", out predictionPipelineSchema);
predictionEngine = mlContext.Model.CreatePredictionEngine<ImageData, ImagePrediction>(predictionPipeline);
var imageData = new ImageData()
{
    ImagePath = "your_image_file_path.png"
};

var prediction = predictionEngine.Predict(imageData);
public class ImageData
{
    [LoadColumn(0)]
    public string ImagePath;

    [LoadColumn(1)]
    public string Label;
}

public class ImagePrediction : ImageData
{
    public float[] Score;

    public string PredictedLabelValue;
}

To use in your c# applications you need to install some required packages via Nuget:

  • Microsoft.ML
  • Microsoft.ML.ImageAnalytics

So you've finished training an image classification model without coding or artificial intelligence knowledge. However, you will have to have a little coding knowledge to use it in your application.