Microsoft AI, microsoftai
Overview
In the previous article Microsoft AI-Custom Vision, we introduced the Custom image service in Azure cognitive service: Custom Vision. We also introduced how to use this online service, visually Complete Project Creation, dataset upload and tagging, model training, model evaluation and testing. We also mentioned that in addition to visualized online operations, Custom Vision also provides sdks to complete the entire machine learning process. The two languages are available: C # And Python, today, we will perform an actual development operation for the C # version.
Development Process
Preparations
C # Custom Vision SDK is open-source on GitHub: Microsoft/Cognitive-CustomVision-Windows. This SDK is mainly divided into two parts: Prediction and Training. If you do not want to download SourceCode to compile it yourself, you can also directly install the Nuget Package in VS through package Management:
- Microsoft. Cognitive. CustomVision. PredictionInstall-Package Microsoft. Cognitive. CustomVision. Prediction-Version 1.2.0
- Microsoft. Cognitive. CustomVision. TrainingInstall-Package Microsoft. Cognitive. CustomVision. Training-Version 1.2.0
Actual development
Next, we will create a WPF project to operate the entire use process of the Custom Vision SDK:
1. Install the Prediction and Training packages through Nuget Package Management, as shown in the preceding preparations. Let's take a look at the namespace composition in the Package:
In addition, you need to install the Microsoft. Rest. ClientRuntime Nuget, because the Custom Vision SDK depends on it. Address:Install-Package Microsoft. Rest. ClientRuntime-Version 2.3.11
2. After the Nuget package is installed, introduce the following Namespace in the Code:
using Microsoft.Cognitive.CustomVision.Training;using Microsoft.Cognitive.CustomVision.Prediction;using Microsoft.Cognitive.CustomVision.Training.Models;
3. Create a Custom Vision Project:
The ApiKey must be replaced by the developer in CustomVision. the Training Key obtained by the ai website. In addition, when the CreateProject is created, the name is required, and the description and domain are optional. The domain type is GUID. I have reviewed the SDK Doc and source code, no description of the GUID value of the domain was found. ai found the GUID corresponding to the Domains field through the webpage debugging method and shared it with you here. Let's take a look at the code implementation and implementation results:
TrainingApi trainingApi = new TrainingApi() { ApiKey = "replace with your api key" };Project demoProject = trainingApi.CreateProject( "CsharpDemoProject01", "It's description of our demo project.", new System.Guid("0732100f-1a38-4e49-a514-c9b44c697ab5"));
Project Type |
GUID |
General |
Ee85a74c-405e-4adc-bb47-ffa8ca0c9f31 |
Food |
C151d5b5-dd07-472a-acc8-15d29dea8518 |
Landmarks |
Ca455789-012d-4b50-9fec-5bb63841c793 |
Retail |
B30a91ae-e3c1-4f73-a81e-c270bff27c39 |
Adult |
45badf75-3591-4f26-a705-45678d3e9f5f |
General (compact) |
0732100f-1a38-4e49-a514-c9b44c697ab5 |
Landmarks (compact) |
B5cfd229-2ac7-4b2b-8d0a-2b0661344894 |
Retail (compact) |
6b4faeda-8396-481b-9f8b-177b9fa3097f |
4. Add tags to the training dataset of the project:
In the example, we added two labels airplane and alarmclock.
// create two tags in our demo projectvar airplaneTag = trainingApi.CreateTag(demoProject.Id, "airplane");var alarmclockTag = trainingApi.CreateTag(demoProject.Id, "alarmclock");
5. Upload the image dataset to the project:
We saved two categories in the project Assets folder, each of which has five images. The sample code is as follows:
string[] images = new string[] { "001.jpg", "002.jpg", "003.jpg", "004.jpg", "005.jpg"};foreach (var image in images){ var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(string.Format("ms-appx:///Assets/airplane/{0}", image), UriKind.RelativeOrAbsolute)); using (var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { trainingApi.CreateImagesFromData(demoProject.Id, stream.AsStream(), new List<string>() { airplaneTag.Id.ToString() }); }}foreach (var image in images){ var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(string.Format("ms-appx:///Assets/alarmclock/{0}", image), UriKind.RelativeOrAbsolute)); using (var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { trainingApi.CreateImagesFromData(demoProject.Id, stream.AsStream(), new List<string>() { alarmclockTag.Id.ToString() }); }}
Verify the tag and dataset we added.
6. After preparing the dataset, start training the model.
var iteration = trainingApi.TrainProject(demoProject.Id);while (iteration.Status == "Training"){ iteration = trainingApi.GetIteration(demoProject.Id, iteration.Id);}iteration.IsDefault = true;trainingApi.UpdateIteration(demoProject.Id, iteration.Id, iteration);
After training, let's take a look at the training results.
7. After model training is completed, start model verification.
ApiKey is replaced with the Prediction Key corresponding to Custom Vision. We use an airplane image as the test input to see the code and result:
PredictionEndpoint endpoint = new PredictionEndpoint() { ApiKey = "replace with your prediction key" };var testFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(string.Format("ms-appx:///Assets/airplane.jpg", ""), UriKind.RelativeOrAbsolute));using (var testStream = await testFile.OpenAsync(Windows.Storage.FileAccessMode.Read)){ var result = endpoint.PredictImage(demoProject.Id, testStream.AsStream());}
We can see that the probability of airplane is 0.98, and alarmclock is 0.01. This result meets our expectations for the model.
Summary
Now the implementation of Custom Vision C # is completed. Because it is only a simple Demo demonstration, only 10 images are taken for the training dataset, I just briefly explained the implementation process of the Code. If you are interested, you can combine this basic process to make the process of uploading tags and images easier to interact with, such as selecting folders for batch upload and management; the model training process can be further refined, and the training results can be monitored through code return results. Model Tests can also intuitively reflect the test results, or process test data in batches, richer presentation and Management of test results and evaluation of data models.
We welcome you to discuss technical issues related to Custom Vision and Azure cognitive services with me. Thank you!