Page 42 - MSDN Magazine, November 2017
P. 42

The AnomalyDetectionClient is utilized in the AddTest- Value method of the AnomalyDetector class (Figure 12). Like AddTrainingValue, AddTestValue is also invoked in the Image- Processor_ProcessingDone event handler (see MainPage.xaml.cs in the companion code). However, AddTestValue proceeds in a slightly different manner than the AddTrainingValue method. In AddTestValue I add brightness data points to an instance of the BrightnessDataset class, which internally uses the generic List class to implement a rolling window. This window, like the one in James McCaffrey’s October article, is used to store test values. By default, the size of the rolling window is set to 30 elements, but you can control this value using a constructor of the BrightnessData- set. As shown in Figure 12, I don’t send data for analysis until the window is full, then I check whether the collection of anomalous values returned by the Web service contains any elements. If so,
Figure 11 Sending Requests to the Azure Machine Learning Studio Web Service
I invoke the AnomalyDetected event, which is also used to pass abnormalities to listeners.
To display anomalous values in the UI, I handle the Anoma- lyDetected event in the MainPage class as follows:
private async void AnomalyDetector_AnomalyDetected( object sender, AnomalyDetectedEventArgs e)
{
}
Specifically, I iterate over the collection of obtained values to check whether they were already added to the local datastore (the AnomalousValues property of the view model). If not, I add them to the observable collection. As a result, only new abnormal val- ues will appear in the list shown previously in Figure 1. I do this additional check because of the rolling window, in which only one element is changed between successive calls to the Web service.
To test my solution, you’ll need to run the RemoteCamera app, start the camera preview and enable anomaly detection using the checkbox on the Anomaly Detection tab. Once this is done you can generate abnormal values by covering your camera. These val- ues should be quickly recognized by the remote ML detector as anomalous and displayed in the listbox (as in Figure 1).
By default, the size of the rolling window is set to 30 elements, but you can control this value using a constructor of the BrightnessDataset.
Wrapping up
I demonstrated here how to design two different anomaly detection experiments in the Azure Machine Learning Studio. Both experi- ments were also deployed as Web services and combined with the client app, RemoteCamera, which sends locally acquired time-series data for machine learning analysis to identify abnormalities. Here, I used a Web service in the UWP app. However, you can use the same code to access a Web service from an ASP.NET Web app, where you handle the ML logic at the back end rather than on the endpoint— which, in the case of IoT can be just a simple sensor. n
DawiD Borycki is a software engineer and biomedical researcher, author and con- ference speaker. He enjoys learning new technologies for software experimenting and prototyping.
Thanks to the following Microsoft technical expert for reviewing this article: Dr. James McCaffrey
await ThreadHelper.InvokeOnMainThread(() => {
foreach (var anomalousValue in e.AnomalousValues) {
if (!remoteCameraViewModel.AnomalousValues.Contains(anomalousValue)) {
remoteCameraViewModel.AnomalousValues.Add(anomalousValue); });
} }
public async Task<IList<BrightnessDataPoint>> DetectAnomalyAsync(IList<BrightnessDataPoint> brightnessData)
{
var request = new AnomalyDetectionRequest(brightnessData);
var response = await httpClient.PostAsJsonAsync(string.Empty, request); IList<BrightnessDataPoint> result;
if (response.IsSuccessStatusCode) {
var anomalyDetectionResponse = await response.Content.ReadAsAsync<AnomalyDetectionResponse>();
result = ConversionHelper. AnomalyDetectionResponseToBrightnessData(anomalyDetectionResponse);
} else {
throw new Exception(response.ReasonPhrase); }
return result; }
Figure 12 Detecting Anomalies
public event EventHandler<AnomalyDetectedEventArgs> AnomalyDetected; private BrightnessDataset dataSet = new BrightnessDataset();
public async Task AddTestValue(byte brightness) {
dataSet.Add(new BrightnessDataPoint(brightness));
if (dataSet.IsFull) {
try {
var anomalousValues = await anomalyDetectionClient. DetectAnomalyAsync(dataSet.Data);
if (anomalousValues.Count > 0) {
AnomalyDetected?.Invoke(this,
new AnomalyDetectedEventArgs(anomalousValues));
} }
catch (Exception ex) {
Debug.WriteLine(ex); }
} }
38 msdn magazine
Machine Learning






















































   40   41   42   43   44