Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
107 lines (65 sloc) 6.88 KB
title titleSuffix description services author manager ms.service ms.subservice ms.topic ms.date ms.author
Quickstart: Create an image classification project with the Custom Vision SDK for C#
Azure Cognitive Services
Create a project, add tags, upload images, train your project, and make a prediction using the .NET SDK with C#.
cognitive-services
anrothMSFT
nitinme
cognitive-services
custom-vision
quickstart
12/05/2019
anroth

Quickstart: Create an image classification project with the Custom Vision .NET SDK

This article provides information and sample code to help you get started using the Custom Vision SDK with C# to build an image classification model. After it's created, you can add tags, upload images, train the project, obtain the project's default prediction endpoint URL, and use the endpoint to programmatically test an image. Use this example as a template for building your own .NET application. If you want to go through the process of building and using a classification model without code, see the browser-based guidance instead.

Prerequisites

Get the Custom Vision SDK and sample code

To write a .NET app that uses Custom Vision, you'll need the Custom Vision NuGet packages. These packages are included in the sample project you'll download, but you can access them individually here.

Clone or download the Cognitive Services .NET Samples project. Navigate to the CustomVision/ImageClassification folder and open ImageClassification.csproj in Visual Studio.

This Visual Studio project creates a new Custom Vision project named My New Project, which can be accessed through the Custom Vision website. It then uploads images to train and test a classifier. In this project, the classifier is intended to determine whether a tree is a Hemlock or a Japanese Cherry.

[!INCLUDE get-keys]

Understand the code

Open the Program.cs file and inspect the code. Create environment variables for your training and prediction keys named CUSTOM_VISION_TRAINING_KEY and CUSTOM_VISION_PREDICTION_KEY, respectively. The script will look for these variables.

[!code-csharp]

Also, get your Endpoint URL from the Settings page of the Custom Vision website. Save it to an environment variable called CUSTOM_VISION_ENDPOINT. The script saves a reference to it at the root of your class.

[!code-csharp]

The following lines of code execute the primary functionality of the project.

Create a new Custom Vision service project

The created project will show up on the Custom Vision website that you visited earlier. See the CreateProject method to specify other options when you create your project (explained in the Build a classifier web portal guide).

[!code-csharp]

Create tags in the project

[!code-csharp]

Upload and tag images

The images for this project are included. They are referenced in the LoadImagesFromDisk method in Program.cs. You can upload up to 64 images in a single batch.

[!code-csharp]

Train the classifier and publish

This code creates the first iteration in the project and then publishes that iteration to the prediction endpoint. You can use the name of the iteration to send prediction requests. An iteration is not available in the prediction endpoint until it's published.

[!code-csharp]

Set the prediction endpoint

The prediction endpoint is the reference that you can use to submit an image to the current model and get a classification prediction.

[!code-csharp]

Submit an image to the default prediction endpoint

In this script, the test image is loaded in the LoadImagesFromDisk method, and the model's prediction output is to be displayed in the console. The value of the publishedModelName variable should correspond to the "Published as" value found on the Custom Vision portal's Performance tab.

[!code-csharp]

Run the application

As the application runs, it should open a console window and write the following output:

Creating new project:
        Uploading images
        Training
Done!

Making a prediction:
        Hemlock: 95.0%
        Japanese Cherry: 0.0%

You can then verify that the test image (found in Images/Test/) is tagged appropriately. Press any key to exit the application. You can also go back to the Custom Vision website and see the current state of your newly created project.

[!INCLUDE clean-ic-project]

Next steps

Now you've seen how to do every step of the image classification process in code. This sample executes a single training iteration, but often you will need to train and test your model multiple times in order to make it more accurate.

[!div class="nextstepaction"] Test and retrain a model

You can’t perform that action at this time.