<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Bahrudin Hrnjica - DEVELOPERS.DE]]></title><description><![CDATA[Software Development Blog with focus on .NET, Windows, Microsoft Azure powered by daenet]]></description><link>https://developers.de/</link><image><url>https://developers.de/favicon.png</url><title>Bahrudin Hrnjica - DEVELOPERS.DE</title><link>https://developers.de/</link></image><generator>Ghost 1.21</generator><lastBuildDate>Sun, 19 Apr 2026 22:59:13 GMT</lastBuildDate><atom:link href="https://developers.de/author/bhrnjica/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Daany – .NET DAta ANalYtics library]]></title><description><![CDATA[<div class="kg-card-markdown"><p><img src="https://bhrnjica.files.wordpress.com/2019/12/daany_logo_small.png" alt="Daany - .NET DAta ANalYtics library "></p>
<h1 id="introduction">Introduction</h1>
<p><code>Daany</code> is .NET data analytics library written in C# and it supposed to be a tool<br>
for data preparation, feature engineering and other kinds of data<br>
transformations prior to creating ml-ready data set. It is .NET Core based<br>
library with ability to run on Windows Linux based distribution and</p></div>]]></description><link>https://developers.de/2020/01/08/daany-net-data-analytics-library/</link><guid isPermaLink="false">5df290b89e2ae3177074183c</guid><category><![CDATA[.NET]]></category><category><![CDATA[C#]]></category><category><![CDATA[Jupyter]]></category><category><![CDATA[.NET Core]]></category><category><![CDATA[Daany]]></category><category><![CDATA[ml.net]]></category><category><![CDATA[AI]]></category><category><![CDATA[analytics]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Wed, 08 Jan 2020 19:17:00 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p><img src="https://bhrnjica.files.wordpress.com/2019/12/daany_logo_small.png" alt="Daany - .NET DAta ANalYtics library "></p>
<h1 id="introduction">Introduction</h1>
<p><code>Daany</code> is .NET data analytics library written in C# and it supposed to be a tool<br>
for data preparation, feature engineering and other kinds of data<br>
transformations prior to creating ml-ready data set. It is .NET Core based<br>
library with ability to run on Windows Linux based distribution and Mac. It is<br>
based on .NET Standard 2.1.</p>
<p>Besides data analysis, the library implements a set of statistics or data<br>
science features e.g. time series decompositions, optimization performance<br>
parameters and similar.</p>
<p>Currently <code>Daany</code> project consists of four main<br>
components:</p>
<ul>
<li><code>Daany.DataFrame</code>,</li>
<li><code>Daany.Stats</code>,</li>
<li><code>Daany.MathStuff</code> and</li>
<li><code>Daany.DataFrame.Ext</code></li>
</ul>
<p>The main <code>Daany</code> component is <code>Daany.DataFrame</code> - a data frame implementation for<br>
data analysis. It is much like <code>Pandas</code> but the component is not going to follow<br>
pandas implementation. It is suitable for doing data exploration and preparation<br>
with <code>C# Jupyter Notebook</code>. In order to create or load data into data frame it<br>
doesn’t require any predefined class type. In order to defined relevant value type of each column all data are parsed internally during data frame creation. The <code>Daany.DataFrame</code> implements set of powerful features for data manipulation, handling missing values, calculated columns, merging two or more data frames into one, and similar. It is handy for extracting its rows or columns as series of elements and put into the chart to visualizing the data.</p>
<p><code>Daany.Stat</code> is a collection of statistics features e.g. time series<br>
decompositions, optimization, performance parameters and similar.</p>
<p><code>Daany.Math</code> is a component within data frame with implementation of od matrix and related<br>
linear algebra capabilities. It also contains some implementation of other great open source projects. The component is not going to be separate NuGet package.</p>
<p><code>Daany.DataFrame.Ext</code> contains extensions for <code>Daany.DataFrame</code> component, but they are related to other projects mostly to ML.NET. The <code>Daany.DataFrame</code>  should not be dependent on <code>ML.NET</code>  and other libraries. So, any future data frame feature which depends on something other than <code>Daany.Math</code>, should be placed in <code>Daany.Ext</code>.</p>
<p>The project is developed as a need to have a set of data transformation features<br>
in one library while I am working with machine learning. So, I thought it might<br>
help to others. Currently, the library has pretty much data transformation<br>
features and might be your number one data analytics library on .NET<br>
platform. Collaboration to the project is also welcome.</p>
<h1 id="howtostartwithdaany">How to start with Daany</h1>
<p><code>Daany</code> is 100% .NET Core component and can be run on any platform .NET Core supports, from the Windows x86/x64 to Mac or Linux based OS. It can be used by Visual Studio or Visual Studio Code. It consisted of 3 NuGet packages, so<br>
the easiest way to start with it is to install the packages in your .NET<br>
application. Within Visual Studio create or open your .NET application and open<br>
NuGet packages window. Type <code>Daany</code> in the browse edit box and hit enter. You can<br>
find four packages starting with Daany. You have few options to install the<br>
packages.</p>
<ol>
<li>
<p>Install <code>Daany.DataFrame</code>  – only. <em>Use this option if you want only data<br>
analysis by using data frame. Once you click Install button, Daany.DataFrame<br>
and Daany.Math will be installed into your project app.</em></p>
</li>
<li>
<p>Install <code>Daany.Stat</code> package. This package already contains <code>DataFrame</code>, as well as time series decomposition and related statistics features.</p>
</li>
</ol>
<p><img src="https://bhrnjica.files.wordpress.com/2019/12/daany_nuget.png" alt=""></p>
<p>Once you install the packages, you can start developing your app using Daany<br>
packages.</p>
<h1 id="usingdaanyasassemblyreference">Using <code>Daany</code> as assembly reference</h1>
<p>Since <code>Daany</code> has no dependency to other libraries you can copy three dlls and add them as reference to your project.</p>
<p><img src="https://bhrnjica.files.wordpress.com/2019/12/daany_file_exp.png" alt="file explorer"></p>
<p>In order to do so clone the project from <a href="http://github.com/bhrnjica/daany">http://github.com/bhrnjica/daany</a>,build it and copy <code>Daany.DataFrame.dll</code>, <code>Daany.Math.dll</code> and <code>Daany.Stat.dll</code> to your project as assembly references. Whole project is just 270 KB.</p>
<h1 id="usingdaanywithnetjupyternotebook">Using <code>Daany</code> with .NET Jupyter Notebook</h1>
<p>Daany library is ideal with .NET Jupyter Notebook, and some of the great notebooks are implemented already, and can be viewed at <a href="http://github.com/bhrnjica/notebooks">http://github.com/bhrnjica/notebooks</a>. The GitHub project contains the code necessary to run the notebooks in Binder, a Jupyter Virtual Environment, and try Daany without any local installation. So the first recommendation is to try Daany with already implemented notebooks using Binder.com.</p>
<h1 id="namespacesindaany">Namespaces in Daany</h1>
<p><code>Daany</code> project contains several namespaces for separating different<br>
implementation. The following list contains relevant namespaces:</p>
<ul>
<li><code>using Daany</code> – data frame and related code implementation,</li>
<li><code>using Daany.Ext</code> – data frame extensions, used with dependency on third party<br>
library,</li>
<li><code>using Daany.MathStuff</code> – math related stuff implemented in Daany,</li>
<li><code>using Daany.Optimizers</code> – set of optimizers like SGD,</li>
<li><code>using Daany.Stat</code> – set of statistics implementations in the project.</li>
</ul>
<p>That's all for this post. Next blog posts will show more exciting implementation using Daany.</p>
</div>]]></content:encoded></item><item><title><![CDATA[Create CIFAR-10 Deep Learning Model with ANNdotNET GUI Tool]]></title><description><![CDATA[<div class="kg-card-markdown"><p>With ANNdotNET 1.2 the user is able to create and train deep learning models for image classification. Image classification module provides minimum of GUI actions in order to fully prepare data set. In this post, we are going to create and train deep learning model for CIFAR-10 data set,</p></div>]]></description><link>https://developers.de/2019/12/12/create-cifar-10-deep-learning-model-with-anndotnet-gui-tool/</link><guid isPermaLink="false">5c29fc7eb6eb02229c4cac47</guid><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Thu, 12 Dec 2019 18:58:32 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p>With ANNdotNET 1.2 the user is able to create and train deep learning models for image classification. Image classification module provides minimum of GUI actions in order to fully prepare data set. In this post, we are going to create and train deep learning model for CIFAR-10 data set, and see how it easy to do that with ANNdotNET v1.2.</p>
<p>In order to prepare data we have to download CIFAR-10 data set from official <a href="http://www.cs.utoronto.ca/%7Ekriz/cifar.html">web site </a>. The CIFAR-10 data set is provided in 6 binary batch files that should be extracted and persisted on your local machine. Number 10 in the name means that data set is created for 10 labels.The following image shows 10 labels of CIFAR-10 data set each label with few sample images.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-10_11-56-56.png" alt="" class="wp-image-7666"><figcaption>CIFAR-10 data set (<a href="http://www.cs.utoronto.ca/%7Ekriz/learning-features-2009-TR.pdf">Learning Multiple Layers of Features from Tiny Images</a>, Alex Krizhevsky, 2009.)</figcaption></figure>
<p>The data set contains 60 000 (50 000 for training and validation, and 10 000 for test) tinny colored images dimensions of 32x32. There is also bigger version of the data set CIFAR-100 with 100 labels. Our task is to create deep learning model capable of recognizing only one of 10 predefined labels from each image.</p><p></p>
<h2 id="datapreparation">Data preparation</h2>
<p>In order to prepare images, we need to do the following:</p>
<ul>
    <li>download compressed file (<a href="http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz">http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz</a>)</li>
    <li>decompress the file and&nbsp; store each image in 10 different folders that are represent image label.</li>
    <li>each folder contains images for one label. </li>
</ul>
<p>The following image shows extracted data set persisted in 10 label folders. The bird folder is opened and shows all images labeled for bird. The <strong>test</strong> folder contains all images created for testing the model once the model is trained.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-10_12-08-02.png" alt="" class="wp-image-7667"></figure>
<p>In order to properly save all images, we need to create simple C# Console application which should extract and save all 60 000 images. Complete C# program can be downloaded from <a href="https://1drv.ms/u/s!AgPZDj-_uxGLhZYUJrd6qX2BE5LA0w">here</a>.</p>
<p>In order to successfully extract the images, we have to see how those images are stored in binary files. From the official site we can see that there are 5 for training and 1 for test binary files: <em>data_batch_1.bin</em>, <em>data_batch_2.bin</em>, …, <em>data_batch_5.bin</em>, as well as <em>test_batch.bin</em>. Each of these files is formatted as follows so that the first byte of the array is label index, and the next 3072 represent the image. Each batch contains 10000 images.</p>
<p>Important to know is that images are stored in CHW format which means that 1d image array is created so that the first 1024 bytes are the red channel values, the next 1024 the green, and the final 1024 the blue. The values are stored in row-major order, so the first 32 bytes are the red channel values of the first row of the image. So all those information have been carried out when implementing the Extractor application. The most important methods are reshaping the 1D byte array into [3, height, width] image tensor, and creating the image from the byte tensor. The following implementation shows how 1D byte array transformed into 3channel bitmap tensor.</p>
<pre><code class="language-csharp">static int[][][] reshape(int channel, int height, int width,  byte[] img)
{
    var data = new int[channel][][];
    int counter = 0;
    for(int c = 0; c &lt; channel; c++)
    {
        data[c] = new int[height][];
        for (int y = 0; y &lt; height; y++)
        {
            data[c][y] = new int[width];
            for (int x = 0; x &lt; width; x++)
            {
                data[c][y][x] = img[counter];
                counter++;
            }
        }
    }
    return data;
}
</code></pre>
<p>Once the 1D byte array is transformed into tensor, the image can be created and persisted on disk. The following method iterates through all 10000 images in one batch extract them and persist on disk.</p>
<pre><code class="language-csharp">public static void extractandSave(byte[] batch, string destImgFolder, ref int imgCounter)
{
    var nStep = 3073;//1 for label and 3072 for image
    //
    for (int i = 0; i &lt; batch.Length; i += nStep)
    {
        var l = (int)batch[i];
        var img = new ArraySegment&lt;byte&gt;(batch, i + 1, nStep - 1).ToArray();
// data in CIFAR-10 dataset is in CHW format, which means CHW: RR...R, GG..G, BB..B;

        // while HWC: RGB, RGB, ... RGB
        var reshaped = reshape(3, 32, 32, img);
        var image = ArrayToImg(reshaped);
        //check if folder exist
        var currentFolder = destImgFolder + classNames[l];

        if (!Directory.Exists(currentFolder))
            Directory.CreateDirectory(currentFolder);

        //save image to specified folder
        image.Save(currentFolder + &quot;\\&quot; + imgCounter.ToString() + &quot;.png&quot;);

        imgCounter++;
   }
}
</code></pre>
<p>Now that we have 60000 tiny images on disk arranged by label we can start creating deep learning model.</p>
<h2 id="createnewimageclassificationannprojectfileinanndotnet">Create new Image Classification annproject file in ANNdotNET</h2>
<p>Open the latest ANNdotNET v1.1 and select <em>New-&gt; Image Classification</em> project. Enter CIFAR project name and press save button. The following image shows CIFAR new ann-project:</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_15-19-06-2.png" alt="" class="wp-image-7674"></figure>
<p>Once we have new project, we can start defining image labels by pressing Add button. For each 10 labels we need to add new label item in the list. In each item the following fields should be defined:</p>
<ul>
    <li>Image label</li><li>Path to images with the label.</li>
    <li>Query - in case we need to get all images within the specified path with certain part of the name. In case all images withing the specified path are images that indicate one label, query should be empty string.</li>
</ul>
<p>Beside Label item, image transformation should be defined in order to define the size of the images, as well as how many images create validation/test data set.</p>
<p>Assuming the CIFAR-10 data set is extracted at <strong>c:/data</strong> folder, the following image shows how label items should be defined:</p>
<figure class="wp-block-image is-resized"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_15-25-55-3.png?w=584" alt="" class="wp-image-7678" width="598" height="682"></figure>
<p>In case label item should be removed from the list, this is done by selecting the item, and then pressing Remove button. Beside image properties, we should defined how many images belong to validation data set. As can be seen 20% of all extracted images will be created validation data set. Notice that images from the test folder are not part of those two data set. they will be used for testing phase once the model is trained. Now that we done with data preparation we can move to the next step: creating mlconifg file.</p>
<h2 id="createmlconfiginanndotnet">Create mlconfig in ANNdotNET</h2>
<p>By selecting New MLConfig command the new mlconfig file is created within the project explorer. Moreover by pressing F2 key on selected mlconfig tree item, we can easily change the name into &quot;CIRAF-10-ConvNet&quot;. The reason why we gave such name is because we are going to use <em>convolution neural network</em> in order to build model.</p>
<p>In order to define mlconfig file we need to define the following:</p>
<ul>
    <li>Network configuration using Visual Network Designer</li>
    <li>Define Learning parameters</li>
    <li>Define training parameters</li>
</ul>
<h2 id="createnetworkconfiguration">Create Network configuration</h2>
<p>By using Visual Network Designer we can quickly create network model. For this CIFAR-10 dataset we are going to create 9 layers model with two Convolutional, 2 Pooling, 2 DropOut and 2 Dense layer, all followed by Scale layer:</p>
<pre><code>Scale (1/255)-&gt;Conv2D(32,[3,3])-&gt;Pooling2d([2,2],2)-&gt;DropOut(0.2)-&gt;Conv2D(64,[3,3])-&gt;Pooling2d([2,2],2)-&gt;DropOut(0.2)-&gt;Dense(512, ReLU)-&gt;Dense(10,Softmax)
</code></pre>
<p>This network can be created so that selecting appropriate layer from the VND combo box and clicking on the Add button. The first layer is Scale layer, since we need to normalize input values to be in interval (0,1). Then we created two sequence of Convolution, Pooling and Drop out layers. Once we done with that, we can add one Dense layer with 512 neurons and ReLU activation function. The last layer is output layer that must follow the output dimension, and Softmax activation function.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_15-25-56.png" alt="" class="wp-image-7686"></figure>
<p>Once network model is defined, we can move to the next step: Setting learning and training parameters.</p>
<p>Learning parameters can be defined through the Learning parameters interface: For this model we can select:</p>
<ul>
    <li>AdamLearner with 0.005 rate and 0.9 momentum value. Loss function is Classification Error, and the evaluation function is Classification Accuracy</li>
</ul>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-10_19-00-25.png" alt="" class="wp-image-7680"></figure>
<p>In order to define the training parameters we switch to Training tabe page and setup:</p>
<ul>
    <li>Number of epoch</li><li>Minibatch size</li>
    <li>Progress frequency</li><li>Randomize minibatch during training<br></li>
</ul>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_16-59-49.png" alt="" class="wp-image-7681"></figure>
<p>Now we have enough information to start training model. The training process is started by selecting <strong>Run </strong>command from the application ribbon. In order to get good model we need to train the model at least few thousands epoch. The following image shows trained model with training history charts.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-11_7-16-15.png" alt="" class="wp-image-7691"></figure>
<p>The model is trained with exactly of 1144 epoch, with network parameters mentioned above. As can be seen from the upper chart, minibatch loss function was CrossEntropyWithSoftmax, while the evaluation was classification accuracy.  The bottom chart shows performance of the training and validation data sets for each 1144 epoch. We can also recognize that validation data set has roughly the same accuracy as training data set which indicates the model is trained well.  More details about model performance can be seen on the next image:</p>
<figure class="wp-block-image is-resized"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_17-03-27.png" alt="" class="wp-image-7692" width="612" height="344"></figure>
<p>Upper charts of the image above show actual and predicted values for training (left) and validation (right). Most of the point values are blue and overlap the orange which indicates that most of value are correctly predicted. The charts can be zoomed and view details of each value.The bottom part of the evaluation show performance parameters of the model for corresponded data set. As can be seen the trained model has 0.989 accuracy for training data set and 0.986 accuracy for validation data set, which indicate high accuracy of the model. Moreover the next two images shows confusion matrix for the both data set, which in details shows how model predict all 10 labels.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_17-04-23.png" alt="" class="wp-image-7693"></figure>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_17-04-49.png" alt="" class="wp-image-7694"></figure>
<p>The last part of the post is testing model for test data set. For that purpose we selected 10 random images from each label of the test set, and evaluate the model. The following images shows the model correctly predicted all 10 images.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_17-14-03.png" alt="" class="wp-image-7695"></figure>
<h2 id="conclusion">Conclusion</h2>
<p>ANNdotNET v1.2 image classification module offers complete data preparation and model development for image classification. The user can prepare data for training, create network model with Neural Network Designer, and perform set of statistical tools against trained model in order to validate and evaluate model. The important note is that the data set of images must be stored on specific location in order to use this trained model shown in the blog post. The trained model, as well as mlcofig files, can be load directly into ANNdotNET project explorer by doublick on CIFAR-10.zip feed example.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2019/01/2019-01-01_19-05-56.png" alt="" class="wp-image-7695"></figure>
</div>]]></content:encoded></item><item><title><![CDATA[How to start with C# Jupyter Notebook]]></title><description><![CDATA[<div class="kg-card-markdown"><p>Yesterday at Ignite conference .NET team has announced the Jupyter Notebook for .NET languages C# and F#. This is a huge step ahead for all data scientists who want to do data science and machine learning on the .NET platform. With C# Jupyter Notebook you can perform data exploration and</p></div>]]></description><link>https://developers.de/2019/11/08/how-to-start-with-c-jupyter-notebook/</link><guid isPermaLink="false">5dc520c6bf909803b4221350</guid><category><![CDATA[ml.net]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[.NET]]></category><category><![CDATA[C#]]></category><category><![CDATA[Jupyter]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Fri, 08 Nov 2019 08:02:26 GMT</pubDate><media:content url="https://developersde.blob.core.windows.net/usercontent/2019/11/882_2019-11-07_19-43-42.png" medium="image"/><content:encoded><![CDATA[<div class="kg-card-markdown"><img src="https://developersde.blob.core.windows.net/usercontent/2019/11/882_2019-11-07_19-43-42.png" alt="How to start with C# Jupyter Notebook"><p>Yesterday at Ignite conference .NET team has announced the Jupyter Notebook for .NET languages C# and F#. This is a huge step ahead for all data scientists who want to do data science and machine learning on the .NET platform. With C# Jupyter Notebook you can perform data exploration and transformation, training, evaluation and testing your Ml models. All operations are performed by code block and you can quickly see the result without running and debugging application every time you want to change something.  In order to see how it looks like,  in this blog post we are going to explore some of the basic functionalities in C# Jupyter Notebook.</p>
<h2 id="howtoinstallnetjupyternotebook">How to Install .NET Jupyter Notebook</h2>
<p>In order to install Jupyter Notebook you can see the <a href="https://devblogs.microsoft.com/dotnet/net-core-with-juypter-notebooks-is-here-preview-1/">official blog post</a>, anyhow here I am going to present this process because it is very short and easy. Before install .NET Jupyter components, you have to install the latest version of <a href="https://devblogs.microsoft.com/dotnet/net-core-with-juypter-notebooks-is-here-preview-1/">.NET SDK</a> and <a href="https://www.anaconda.com/distribution/#download-section">Anaconda</a>. Once you have Anaconda installed on your machine, open Anaconda Prompt from Windows Start Menu.</p>
<p><img src="https://bhrnjica.files.wordpress.com/2019/11/2019-11-07_19-25-43.png" alt="How to start with C# Jupyter Notebook"></p>
<p>To run <em>Anaconda Prompt</em> you have two options:</p>
<ul>
<li>to open power shell or</li>
<li>to open classic command prompt.</li>
</ul>
<p>Select <strong>Anaconda Powershell Prompt</strong>, and the powershell window will pop up. Once the powershell prompt is opened we can start with the installation of Jupyter Notebook components. The first step is  to install the <code>dotnet try global tool</code>.</p>
<p>Type this to cmd:</p>
<pre><code>dotnet tool install -g dotnet-try
</code></pre>
<p>After some time you should get the following message:</p>
<p><img src="https://bhrnjica.files.wordpress.com/2019/11/2019-11-07_19-31-06.png" alt="How to start with C# Jupyter Notebook"></p>
<p>Then we need to install <strong>.NET Kernel</strong> by typing the following command:</p>
<pre><code>dotnet try jupyter install 
</code></pre>
<p>Then the following message should appear:</p>
<p><img src="https://bhrnjica.files.wordpress.com/2019/11/2019-11-07_19-34-17.png" alt="How to start with C# Jupyter Notebook"></p>
<p>In case you have any problems with the installation please refer to official blog post or post an Issue at <a href="https://github.com/dotnet/try/issues">https://github.com/dotnet/try/issues</a>.</p>
<p>Also note that this version of Jupyter Notebook is in preview, so not all actions will work as you expected.<br>
Now that you have installed C# Jupyter, you can open Jupyter notebook from the Anaconda navigator, or just type Jupyter Notebook in to Anaconda Prompt. Once we did that, your default bowers pops up and shows the starting directory in the Jupyter Notebook. If you click New button, you can see option to create C# and F# notebooks. Press C#, and the new C# notebook will appeared in the browser.<br>
<img src="https://bhrnjica.files.wordpress.com/2019/11/2019-11-07_19-39-58.png" alt="How to start with C# Jupyter Notebook"></p>
<p>Try some basic stuff in notebook.</p>
<p><img src="https://bhrnjica.files.wordpress.com/2019/11/2019-11-07_19-43-42.png" alt="How to start with C# Jupyter Notebook"></p>
<p>In the next blog post we are going to explore more and see some of the coolest features in C# Jupyter Notebook.</p>
</div>]]></content:encoded></item><item><title><![CDATA[How to prepare machine with GPU for Deep Learning with CNTK, TensorFlow and Keras]]></title><description><![CDATA[<div class="kg-card-markdown"><p>In this blog post, step by step instruction is going to be described in order to prepare clean Windows based machine (virtual) with GPU for deep learning with CNTK, Tensorflow and Keras. Installation of OS is not covered in the post, and this is task is assumed it is already</p></div>]]></description><link>https://developers.de/2018/12/21/how-to-prepare-machine-with-gpu-for-deep-learning-with-cntk-tensorflow-and-keras/</link><guid isPermaLink="false">5c1d2b7abf175607fcb1cf29</guid><category><![CDATA[CNTK]]></category><category><![CDATA[tensorflow]]></category><category><![CDATA[keras]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[AI]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Fri, 21 Dec 2018 18:20:29 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p>In this blog post, step by step instruction is going to be described in order to prepare clean Windows based machine (virtual) with GPU for deep learning with CNTK, Tensorflow and Keras. Installation of OS is not covered in the post, and this is task is assumed it is already completed.</p>
<h2 id="preparingthemachine">Preparing the machine</h2>
<p>Once you have up and running clean Windows machine, there are several things you should concider:<br>
1. <strong>Physical machine with NVIDIA compatible graphics card.</strong><br>This requirement will provide deep learning frameworks to train models on GPU, which speedups the training process rapidly. </p><p></p>
<p> 2. <strong>Virtual Machine with GPU.</strong><br>In case you plan to prepare virtual machine, or Azure virtual machine, be aware that (for my knowledge) <strong>only Windows Server 2016</strong> based virtual machine recognize GPU card. So if you install  Windows 10 or lower version on virtual machine, you will not be able to use GPU for training deep learning models.  </p>
<p>3. <strong>Azure N-Series VM</strong><br>In case you plan to select one of Azure virtual machine, only N-series support GPU.</p>
<h2>Installation for NVIDIA driver and related stuff </h2>
<p>In this blog post only NVIDIA related driver will be described, and no other installation driver will be considered. In case of other driver installation, please refer to related vendor site.<br><br>For this blog post, drivers and related stuff for NVIDIA Tesla K80 graphics card will be explained. For other NVIDIA cards the installation process is almost the same.  </p>
<p>1. First you have to know what NVIDIA graphics card is installed your machine. </p>
<p>2. Then go to NVIDIA official <a href="http://nvidia.com/Download/Index.aspx">site</a>, and select appropriate information before driver download. In my case the following information are selected:</p>
<p> 3. Press search and download the driver.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-20_10-06-18.png" alt="" class="wp-image-7702"></figure>
<p>Once you download the driver, install it on your machine.</p>
<p>Once you have driver installed, you have to download and install two more NVIDIA software components:<br><br>1. CUDA Toolkit 9.0<br>2. cuDNN 7.4<br><br>Those two software components are used by deep learning frameworks (CNTK and TensorFlow) for GPU based computation.<br>The CUDA 9.0 is compatible with the latest versions of CNTK 2.6 and Tensorflow 1.12, so it makes easier to used one CUDA version for both frameworks, which was not the case in the past.</p>
<h2>Installation of CUDA 9.0</h2>
<p> In order to install CUDA Toolkit,  go to <a href="https://developer.nvidia.com/cuda-downloads">CUDA download page</a> and select appropriate information of your machine. The following information I have selected in order to download it:   </p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-20_10-09-30.png" alt="" class="wp-image-7703"></figure>
<p>Once you select the right information, press download button. Once the CUDA 9.0 is downloaded on you machine install it by performing Express installation option.</p>
<h2>Installation of cuDNN 7.4</h2>
<p>Download the cuDNN from the <a href="https://developer.nvidia.com/cudnn">official site</a>, and then press <strong>Download cuDNN</strong> button.<br><br>Once you press it, the following page should appear. Notice also notice that login page might appear before download page. </p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-20_10-06-45.png" alt="" class="wp-image-7704"></figure>
<p>Once the cuDNN is downloaded unzip it. Only three files are exist in the installation, and those should be copied on the right place. In order to successfully install cuDNN, perform the following files copy:<br><br> 1. <em>cudnn64_7.dll</em> to <em>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin</em></p>
<p><em>2. cudnn.lib</em> to <em>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64</em></p>
<p>3. <em>cudnn.h</em> to <em>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include</em></p>
<p>Once you've done that, the NVIDIA related stuff installation has been completed, and you can switch to installation Python related stuff.</p>
<h2>Installation Python development environment</h2>
<p>CNTK and TensorFlow support various python environments, but you should always see official site for the compatibility. In order to use CNTK and Tensor flow on the same python environment, it is recommended to use <strong>Anaconda3</strong> version <strong>4.1.1</strong> environment.</p>
<p>First download the Anaconda3 v4.1.1 from the official site:<br><br>- Go to <a href="http://repo.continuum.io/archive/">http://repo.continuum.io/archive/</a> and download Anacoda3 4.1.1 for 64 bit.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-20_10-43-51.png" alt="" class="wp-image-7701"></figure>
<p>Once the Anaconda is downloaded install it, on standard way using installer.</p>
<h2>Prepare python environment for the installation</h2>
<p>Once Anaconda3 4.1.1 has been installed several commands needs to be performed in order to install all necessary software. Before start, we need to upgrade <strong>pip</strong> since Anaconda3 4.1.1 is little bit old. So run the Anaconda Command Prompt from the <strong>Start-&gt;Anacoda-&gt;Anaconda Prompt</strong></p>
<p>Once the Anaconda Prompt is running, type the following command:<br>
<code>python -m pip install --upgrade pip</code></p>
<p>Now we are ready to install CNTK, Tensorflow and Keras. But before that we should create separate python environment with python 3.5. Once we have the environment we can install those frameworks to it. The new environment must be relies on python 3.5. So type the following command into Anaconda Prompt:<br>
<code>conda create --name mlenv1218 python=3.5</code><br>
We have created environment named &quot;mlenv1218&quot;.</p>
<p>Now, don't forget to activate the environment before installing software. Type the following commands in order to activate environment.<br>
<code>activate mlenv1218</code><br>
Once we've done that, the Anaconda Prompt should looks like (active environment is shown on the left site):</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-20_11-01-00.png" alt="" class="wp-image-7700"></figure>
<h2>Installation of CNTK, Tensorflow and  Keras</h2>
<p>It is very important to properly install NVIDIA related stuff, before installation of deep learning libraries, because most of the installation problems are related to it. Once we have NVIDIA and Python environment installed properly, the installation process for deep learning frameworks is very easy. <br>In Anaconda Prompt, with activate "mlenv2118" environment, type the following command in order to install CNTK:
    <code>pip install cntk-gpu</code>
</p><p>The type the following python code to test CNTK installation:<br>
<code>python -c &quot;import cntk; print(cntk.<strong>version</strong>)&quot;</code></p>
<p>Once you've done that, type the following command in order to install Tensorflow:<br>
<code>pip install tensorflow-gpu</code></p>
<p>Type the following command in order to test installation:<br>
<code>python -c &quot;import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))&quot;</code></p>
<p>At the end type the following command to install Keras:</p>
<p><code>pip install keras</code></p><p></p>
<p>In addition it is useful to install the following packages:</p>
<ul><li>matplotlib</li><li>opencv</li><li></li></ul>
<p>Those packages are installed using:</p>
<pre class="wp-block-preformatted">pip install matplotlib, and<br>pip install python-opencv<br></pre>
<p>That is all to install in order to run CNTK, TensorFlow and Keras. </p>
<h2>Install Visual Studio Code to write python code</h2>
<p>In order to write python code for deep learning you have two options among many other:</p>
<ul><li>Install Visual Studio 2017</li><li>Install Visual Studio Code</li></ul>
<p>Visual Studio Code can be downloaded from official <a href="https://code.visualstudio.com/">site</a>. Download it and install. Once you install the VS Code, run it. Press Extension button on the lest side and type python in search box. Select on Python extension and press Install.</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-20_15-47-11-1.png" alt="" class="wp-image-7709"></figure>
<ul><li>Restart VS Code, and </li><li>Select <em>File-&gt;New File</em></li><li>Save file as <em>python_test.py</em></li><li>Change current python environment to "mlenv1218" (by double click)</li><li>Run python code, by right click on python code and select "<em>Run Python File in Terminal</em>"</li></ul>
<p><code>import cntk import tensorflow as tf import keras print(&quot;CNTK:&quot;,cntk.__version__) print(&quot;TensorFlow:&quot;,tf.__version__) print(&quot;Keras:&quot;,keras.__version__)</code></p>
<p>the following output produced the above code:</p>
<figure class="wp-block-image"><img src="https://bhrnjica.files.wordpress.com/2018/12/2018-12-20_15-52-08.png?w=859" alt="" class="wp-image-7708"></figure>
</div>]]></content:encoded></item><item><title><![CDATA[How to visualize CNTK network in C#]]></title><description><![CDATA[<div class="kg-card-markdown"><p>When building deep learning models, it is often required to check the model for consistency and proper parameters definition. In ANNdotNET, ml network models are designed using Visual Network Designer (VND), so it is easy to see the network configuration. Beside VND, in ANNdotNET there are several visualization features on</p></div>]]></description><link>https://developers.de/2018/11/23/how-to-visualize-cntk-network-in-c/</link><guid isPermaLink="false">5bf7ca3902f971202856c47e</guid><category><![CDATA[.NET]]></category><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[C#]]></category><category><![CDATA[AI]]></category><category><![CDATA[.NET Core]]></category><category><![CDATA[ANNdotNET]]></category><category><![CDATA[mvpbuzz]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Fri, 23 Nov 2018 09:50:44 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p>When building deep learning models, it is often required to check the model for consistency and proper parameters definition. In ANNdotNET, ml network models are designed using Visual Network Designer (VND), so it is easy to see the network configuration. Beside VND, in ANNdotNET there are several visualization features on different level: network preparation, model training phase, post training evaluation, performance analysis, and export results. In this blog post we will learn how to use those features when working with deep learning models.</p>
<h2 id="visualizationduringnetworkpreparationandmodeltraining">Visualization during network preparation and model training</h2>
<p>When preparing network and training parameters, we need information about data sets, input format and output type. This information is relevant for selecting what type of network model to configure, what types of layers we will use, and what learner to select. For example the flowing image shows  network configuration containing of 2 embedding layers, 3 dense layers and 2 dropout layers. This network configuration is used to train CNTK model for <a href="https://archive.ics.uci.edu/ml/datasets/mushroom">mushroom data set</a>. As can be seen network layers are arranged as listbox items, and the user has possibility to see, on the highest level, how neural networks looks like, which layers are included in the network, and how many dimensions each layer is defined. This is very helpful, since it provides the way of building network very quickly and accurately, and it requires much less times in comparisons to use traditional way of coding the network in python, or other programming language.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/11/2018-11-22_19-42-03.png" alt="">
<p>ANNdotNET Network Settings page provides pretty much information about the network, input and output layers, what data set are defined, as well as whole network configuration arranged in layers. Beside network related information, the Network Settings tab page also provides the learning parameters for the network training. More about Visual Network Designer the ready can find on one of the previous blog post.</p>
<p>Since ANNdotNET implements <a href="https://bhrnjica.net/2018/10/14/anndotnet-v1-0-has-been-released/">MLEngine</a> which is based on CNTK, so all CNTK related visualization features could be used. The CNTK library provides rich set of visualizations. For example you can use <a href="https://docs.microsoft.com/en-us/cognitive-toolkit/Using-TensorBoard-for-Visualization">Tensorboard in CNTK</a>  for visualization not just computational graph, but also training history, model evaluation etc. Beside <a href="https://www.tensorflow.org/guide/summaries_and_tensorboard">Tensorboard</a>, CNTK provides<a href="https://docs.microsoft.com/en-us/cognitive-toolkit/using-graphviz-for-visualization"> logger module</a> which uses <a href="https://graphviz.gitlab.io/">Graphviz </a>tool for visualizing network graph.The bad news of this is that all above features cannot be run on C#, since those implementation are available only in python.</p>
<p>This is one of the main reason why ANNdotNET provides rich set of visualizations for .NET platform. This includes: training history, model evaluation for training and validation data set, as well as model performance analysis. The following image show some of the visualization features: the training history (loss and evaluation) of minibatches during training of mushroom model:</p>
<img src="https://bhrnjica.files.wordpress.com/2018/11/2018-11-22_19-59-20.png" alt="" data-id="7628" data-link="https://bhrnjica.net/2018-11-22_19-59-20/" class="wp-image-7628">
<p>Moreover, the following image shows evaluation of training and validation set for each iteration during training:</p>
<img src="https://bhrnjica.files.wordpress.com/2018/11/2018-11-22_19-59-49.png" alt="" class="wp-image-7629">
<p>The graphs are generated during training phase, so the user can see what is happening with the model.  This is of tremendous help, when deciding when to stop the training process, or are training parameters produce good model at all, or this can be helpful in case when can stop and change parameters values. In case we need to stop the training process immediately, ANNdotNET provides Stop command which stops training process at any time.</p>
<h2 id="modelperformancevisualization">Model performance visualization</h2>
<p>Once the model is trained, ANNdotNET provides performance analysis tool for all three types of ML problems: regression, binary and multi class classification.</p><p></p>
<p>Since the mushrooms project is binary ML problem the following image shows the performance of the trained model:
<img src="https://bhrnjica.files.wordpress.com/2018/11/2018-11-22_20-16-04.png" alt="" class="wp-image-7630">
</p><h2 id="usinggraphviztovisualizecntknetworkgraphinc">Using Graphviz to visualize CNTK network graph in C#</h2>
<p>We have seen that ANNdotNET provides all types of visualizations CNTK models, and those features are provided by mouse click through the GUI interfaces.  One more feature are coming to ANNdotNET v1.1 which uses Grpahviz to visualize CNTK network graph. The feature is implemented based on original CNTK python implementation with some modification and style.</p>
<p>In order to use Graphviz to visualize network computation graph the following requirements must be met:</p>
<ul><li><a href="https://graphviz.gitlab.io/_pages/Download/Download_windows.html">Install Graphviz</a> on you machine.</li><li>Register Graphviz path as system variable. (See image below)<br></li></ul>
<img src="https://bhrnjica.files.wordpress.com/2018/11/2018-11-22_20-16-05.png" alt="" class="wp-image-7631">
<p>Now that you have install Graphviz tool, you can generate nice image of your network model directly in ANNdotNET just by click on <strong>Graph </strong>button above the Visual Network Designer (see image 1).</p>
<p>Here is some of nice graphs which can be generate from ANNdotNET preclaculated models.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/11/moshoorm_model.png" alt="" class="wp-image-7632"> 
Graphviz generated graph of mushrooms model implemented in ANNdotNET
<img src="https://bhrnjica.files.wordpress.com/2018/11/snaghtml5827ad3.png" alt="" class="wp-image-7633">
<img src="https://bhrnjica.files.wordpress.com/2018/11/e5e8cab5-b49e-47c3-8438-41d142f0bdb3.png" alt="" class="wp-image-7634">
<p>In case you like this nice visualization features go to <a href="http://github.com/bhrnjica/anndotnet">http://github.com/bhrnjica/anndotnet</a>, download the latest version from <a href="https://github.com/bhrnjica/anndotnet/releases">release section</a> or just download the <a href="https://github.com/bhrnjica/anndotnet">source code</a> and try it with Visual Studio, but don't forget to give a star.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/11/2018-11-22_20-50-41.png" alt="Star ANNdotNET project if you found it useful.">
<p>In the next blog post I will show you how visualization of CNTK<br>
computational graph is implemented, so you will be able to use it in<br>
your custom solutions.</p>
</div>]]></content:encoded></item><item><title><![CDATA[Sentiment Analysis using ANNdotNET]]></title><description><![CDATA[<div class="kg-card-markdown"><p>The October 2018 issue of MSDN magazine brings the article &quot;Sentiment Analysis Using CNTK&quot; written by <a href="https://jamesmccaffrey.wordpress.com/2018/10/03/sentiment-analysis-using-cntk">James McCaffrey</a>. I was wondering if I can implement this solution in ANNdotNET as Dr. McCaffrey written in the magazine. Indeed I have implemented complete solution in less than 5 minutes.</p>
<p>In</p></div>]]></description><link>https://developers.de/2018/10/18/sentiment-analysis-using-anndotnet/</link><guid isPermaLink="false">5bc8230c181f1d3f60d27dc2</guid><category><![CDATA[.NET]]></category><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[C#]]></category><category><![CDATA[AI]]></category><category><![CDATA[.NET Core]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Thu, 18 Oct 2018 06:22:39 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p>The October 2018 issue of MSDN magazine brings the article &quot;Sentiment Analysis Using CNTK&quot; written by <a href="https://jamesmccaffrey.wordpress.com/2018/10/03/sentiment-analysis-using-cntk">James McCaffrey</a>. I was wondering if I can implement this solution in ANNdotNET as Dr. McCaffrey written in the magazine. Indeed I have implemented complete solution in less than 5 minutes.</p>
<p>In this blog post I am going to walk you through this very good and well written MSDN article example. I am not going to repeat the text written in the MSDN article, so it is recommendation to read the article first, and back here and implement the example in ANNdotNET. Since the ANNdotNET is GUI tool, it is interesting to see all great visualizations during the model training and evaluation. Also the ANNdotNET provides complete binary model evaluation by providing the confusion matrix, ROC Curve, and other binary performance parameters, this example makes more interesting and valuable to read.</p>
<p>Whole example is implemented in five steps.</p>
<h1 id="step1preparefilesandfolderstructure">Step 1: Prepare files and folder structure</h1>
<p>First we need to create several folders and files in order to create empty annproject. This manual creation of folders are necessary because ANNdotNET v1.0 has not option to create Empty project. This will be added in the next version.</p>
<p>So first, create the following set of hierarchically ordered folders:</p>
<pre><code>SentimentAnalysis
    MoveReview
        data
</code></pre>
<p>The following figure shows this set of folder.</p>
<img class="alignnone size-full wp-image-7570" src="https://bhrnjica.files.wordpress.com/2018/10/2018-10-15_21-08-04.png" alt="2018-10-15_21-08-04" width="1099" height="314">
# Step 2: Download data sets used in the example.
<p>Only thing we need from the MSDN article is train and test data sets. The data can be downloaded from the MSDN sample: Code_McCaffreyTestRun1018.zip. Once the zip file is downloaded unzip the sample, and copy files: imdb_sparse_train_50w.txt and indb_sparse_test_50w.txt to data folder as image above shows.</p>
<h1 id="step3createmoviewreviewannandlstmnetmlconfigfiles">Step 3: Create MoviewReview.ann and LSTM-Net.mlconfig files</h1>
<p>Open Notepad and create file with the following content:</p>
<pre><code class="language-html">project:|Name:MovieReview |Type:NoRawData |MLConfigs:LSTM-Net
data:|RawData:MovieReview_rawdata.txt
parser:|RowSeparator:rn |ColumnSeparator: ; |Header:0 |SkipLines:0
</code></pre>
<p>Save file in SentimenAnalysis folder as MovieReview.ann. The following picture shows saved annproject file on disk.</p>
<img class="alignnone size-full wp-image-7572" src="https://bhrnjica.files.wordpress.com/2018/10/2018-10-15_21-29-24.png" alt="2018-10-15_21-29-24" width="988" height="463">
<p>Now open Notepad again, create a new empty file. The empty file is supposed to be  mlconfig file with the content shown below. Don't worry about the content of the file, since all those details will be visible once we open it with ANNdotNET. If you want to know more about structure of the mlconfig file, please refer to this wiki page of the ANNdotNET project.</p>
<pre><code class="language-html">configid:msdn-oct-2018-issue-sentiment-analysis-article
metadata:|Column02:y;Category;Label;Random;0;1
features:|x 129892 1
labels:|y 2 0
network:|Layer:Embedding 50 0 0 None 0 0 |Layer:LSTM 25 25 0 TanH 1 1 |Layer:Dense 2 0 0 Softmax 0 0
learning:|Type:AdamLearner |LRate:0.01 |Momentum:0.85 |Loss:CrossEntropyWithSoftmax |Eval:ClassificationAccuracy |L1:0 |L2:0
training:|Type:Default |BatchSize:250 |Epochs:400 |Normalization:0 |RandomizeBatch:0 |SaveWhileTraining:0 |FullTrainingSetEval:1 |ProgressFrequency:1 |ContinueTraining:0 |TrainedModel:
paths:|Training:data\imdb_sparse_train_50w.txt |Validation:data\imdb_sparse_test_50w.txt |Test:data\imdb_sparse_test_50w.txt |TempModels:temp_models |Models:models |Result:LSTM-Net_result.csv |Logs:log
</code></pre>
<p>The file should be saved in the MovieReview folder with LSTM-Net.mlconfig file name. The next image shows where mlconfig file is stored.</p>
<img class="alignnone size-full wp-image-7573" src="https://bhrnjica.files.wordpress.com/2018/10/2018-10-15_21-41-16.png" alt="2018-10-15_21-41-16" width="1107" height="374">
# Step 4. Open annproject file with ANNdotNET GUI tool
<p>Now we have setup everything in order to open and train sentiment analysis example with ANNdotNET. Since ANNdotNET implements MLEngine which is based on CNTK, data sets are compatible and can be read by the trainer. In order to get better result we have changed learning parameter a little bit. Instead of SGD we used AdamLearner.</p>
<p>In case you don't have ANNdotNET tool installed on your machine, just go to release section and download the latest version. Or clone the GitHub repository and run it within the Visual Studio. All information about how to run ANNdotNET as standalone application or as the Visual Studio solution can be found at GitHub page <a href="https://github.com/bhrnjica/anndotnet">https://github.com/bhrnjica/anndotnet</a>.</p>
<p>After simple unzipping binaries of the ANNdotNET on your machine, run it by simply selecting anndotnet.wnd.exe file. Once the ANNdotNET is running, click the Open application command and select the MoveReview.ann file. In a second the application loads the project with corresponded mlconfig file. From the project explorer, click on LSTM-NET three item, and similar content as image below should be appeared.</p>
<img class="alignnone size-full wp-image-7577" src="https://bhrnjica.files.wordpress.com/2018/10/2018-10-15_21-54-342.png" alt="2018-10-15_21-54-34" width="1305" height="819">
<p>Everything we have written into mlconfig file are now shown in the Network settings tab page.</p>
<pre><code>Input layer with 129892 dimensions
Output layer with 2 dimension (binary problem)
Learning parameters:
    AdamLearner, with 0.01 lr and 0.85 momentum,
    Loss Function is CrossEntropywithSoftmax
    Evaluation function is ClassificationAccuracy
NNetwork Designer shows typical LSTM recurrent network
</code></pre>
<h1 id="step5trainingandevaluationoftheexample">Step 5. Training and Evaluation of the Example</h1>
<p>Now that we reviewed the network settings, we can switch to the train tab page, and review the training parameters. Since we already setup training parameters in the mlconfig file, we don't need to change anything.</p>
<p>Start training process by click on the Run application command. After some time we should see the following result:</p>
<img class="alignnone size-full wp-image-7578" src="https://bhrnjica.files.wordpress.com/2018/10/2018-10-16_16-44-28.png" alt="2018-10-16_16-44-28" width="1305" height="819">
<p>If we switch to Evaluation page we can perform some statistics analysis in order to evaluate if the model is good or not. Once the evaluation tab page is shown, click on Refresh button to evaluate the model against training and validation data stets.</p>
<img class="alignnone size-full wp-image-7579" src="https://bhrnjica.files.wordpress.com/2018/10/2018-10-16_16-44-39.png" alt="2018-10-16_16-44-39" width="1305" height="819">
<p>The left statistics are for the training dataset, and the left side is for the validation data set. As can be seen, the model perfectly predicted all data from the training data set, and about 70% of accuracy described the validation data set. Off cource, the model is not good as we expected for the production, but for this demonstration is good enough. There are also two buttons to show ROC curve, and other binary performance parameters, for both data sets, which the reader my taste.</p>
<p>That's all needed in order to have complete Sentiment Analysis exemple setup and running. In case you want complete ANNdotNET project, it can be downloaded from <a href="https://1drv.ms/u/s!AgPZDj-_uxGLhY07XnfXsaj9CXEURA">here</a>.</p>
</div>]]></content:encoded></item><item><title><![CDATA[Visual Neural Network Designer in ANNdotNET]]></title><description><![CDATA[ANNdotNET – is an open source project for deep learning on .NET platform (.NET
Framework and .NET Core). The project is hosted at
<http://githubcom/bhrnjica/anndotnet>. More information can be found at Wiki
page of the GitHub portal, or t the <http://bhrnjica.net/anndotnet>.]]></description><link>https://developers.de/2018/09/30/visual-neural-network-designer-in-anndotnet/</link><guid isPermaLink="false">5bb07986250e26119c1bb0a3</guid><category><![CDATA[.NET]]></category><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[C#]]></category><category><![CDATA[AI]]></category><category><![CDATA[.NET Core]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Sun, 30 Sep 2018 08:17:32 GMT</pubDate><media:content url="https://developersde.blob.core.windows.net/usercontent/2018/9/30815_ANNLogo_350x134pix.png" medium="image"/><content:encoded><![CDATA[<div class="kg-card-markdown"><img src="https://bhrnjica.files.wordpress.com/2018/09/e497cb4604c01c7b0013646bfa2d0518.png" alt="Visual Neural Network Designer in ANNdotNET">
<h1 id="briefintroductiontoanndotnet">Brief Introduction to ANNdotNET</h1>
<img src="https://developersde.blob.core.windows.net/usercontent/2018/9/30815_ANNLogo_350x134pix.png" alt="Visual Neural Network Designer in ANNdotNET"><p>ANNdotNET – is an open source project for deep learning on .NET platform (.NET<br>
Framework and .NET Core). The project is hosted at<br>
<a href="http://githubcom/bhrnjica/anndotnet">http://githubcom/bhrnjica/anndotnet</a>. More information can be found at Wiki<br>
page of the GitHub portal, or t the <a href="http://bhrnjica.net/anndotnet">http://bhrnjica.net/anndotnet</a>.</p>
<p>The project comes in two versions: GUI and CMD tool. The main purpose of the<br>
project is focus on building deep learning models without to be distracted with<br>
debugging the source code and installing/updating missing packages and<br>
environments. The user should no worry which version of ML Engine the<br>
application is using. In other words, the ANNdotNET is ideal in several<br>
scenarios:</p>
<ol>
<li>
<p>more focus on network development and training process using classic desktop<br>
approach, instead of focusing on coding,</p>
</li>
<li>
<p>less time spending on debugging source code, more focusing on different<br>
configuration and parameter variants,</p>
</li>
<li>
<p>ideal for engineers/users which are not familiar with supported programming<br>
languages,</p>
</li>
<li>
<p>in case the problem requires coding more advanced custom models, or training<br>
process, ANNdotNET CMD provides high level of API for such implementation,</p>
</li>
<li>
<p>all ml configurations developed with GUI tool, can be handled with CMD tool<br>
and vice versa.</p>
</li>
</ol>
<p>With ANNdotNET GUI Tool the user can prepare data for training, by performing<br>
several actions: data cleaning, feature selection, category encoding, missing<br>
values handling, and create training and validation dataset prior to start<br>
building deep neural network. Once the data is prepared, the user can create<br>
Machine Learning Configuration (mlconfig) file in order to start building and<br>
training deep neural network. All previous actions user can handle using GUI<br>
tool implemented in the application.</p>
<p>For persisting all data preparation and transformation actions, the application<br>
uses annproject file type which consists information about raw dataset, metadata<br>
information and information about machine learning configuration files.</p>
<p>The machine learning configurations are stored in separated files with mlconfig<br>
file extension. For more information about files in ANNdotNET the reader may<br>
open this<br>
<a href="https://github.com/bhrnjica/anndotnet/wiki/File-Structure-in-ANNdotNET">link</a>.<br>
The following image shows how ANNdotNET handles annproject and corresponded<br>
machine learning configurations within the annproject:</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/14ab6f259a54b6e22d50b2fcba593dc4.png" alt="Visual Neural Network Designer in ANNdotNET">
<p>As can be seen the annproject can be consisted of arbitrary number of mlconfigs,<br>
which is typical scenario when working on ML Project. User can switch between<br>
mlconfigs any time except when the application is in training or evaluation<br>
mode.</p>
<h1 id="anndotnetmlengine">ANNdotNET ML Engine</h1>
<p>ANNdotNET introduces the ANNdotNET Machine Learning Engine (MLEngine) which is<br>
responsible for training and evaluation models defined in the mlconfig files.<br>
The ML Engine relies on Microsoft Cognitive Toolkit, CNTK open source library<br>
which is proved to be one of the best open source library for deep learning.<br>
Through all application ML Engine exposed all great features of the CNTK e.g.<br>
GPU support for training and evaluation, different kind of learners, but also<br>
extends CNTK features with more Evaluation functions (RMSE, MSE, Classification<br>
Accuracy, Coefficient of Determination, etc.), Extended Mini-batch Sources,<br>
Trainer and Evaluator.</p>
<p>ML Engine also contains the implementation of neural network layers which<br>
supposed to be high level CNTK API very similar as layer in Keras and other<br>
python based deep learning APIs. With this implementation the ANNdotNET<br>
implements the Visual Neural Network Designer called ANNdotNET NNDesigner which<br>
allows the user to design neural network configuration of any size with any type<br>
of the layers. In the first release the following layesr are implemented:</p>
<ul>
<li>
<p>Normalization Layer – takes the numerical features and normalizes its values<br>
before getting to the network. More information can be found<br>
<a href="https://bhrnjica.net/2018/07/13/input-normalization-as-separate-layer-in-cntk-with-c/">here</a>.</p>
</li>
<li>
<p>Dense – classic neural network layer with activation function</p>
</li>
<li>
<p>LSTM – LSTM layer with option for peephole and self-stabilization.</p>
</li>
<li>
<p>Embedding – Embedding layer,</p>
</li>
<li>
<p>Drop – drop layer.</p>
</li>
</ul>
<p>Designing the neural network can be simplify by using pre defined layer. So on<br>
this way we can implement almost any network we usually can implement through<br>
the source code.</p>
<h1 id="howtouseanndotnetnndesigner">How to use ANNdotNET NNDesigner</h1>
<p>Once the MLConfig is created user can open it and start building neural network.<br>
NNDesigner is placed in the Network Setting tab page. The following image shows<br>
the Network Setting tab page.</p>
<p>NNetwork Designer contains combo box with supported NN layers, and two action<br>
buttona for adding and removing layers in/from the network. Adding and removing<br>
layers is simple as adding and removing items in/from the list box. In order to<br>
add the layer, select the layer from the combo box, and press Add button. In<br>
order to remove the layer form the network, click the layer in the listbox and<br>
press Remove button, then confirm deletion. In order to successfully create the<br>
network, the layer must be created with the same output dimension as the Output<br>
layer shown on the left side of the window, otherwise the warning messages will<br>
appear about this information once the training is stared.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/bde853146354c83dc40e4a6ff08c0605.png" alt="Visual Neural Network Designer in ANNdotNET">
<p>Depending on the type, once the layer is added to the network it must be<br>
configured. The main parameter for each layer is output dimension and activation<br>
function, except the drop and normalization layer. The following text explains<br>
parameters for all supported layers:</p>
<blockquote>
<p><em>Normalization layer</em> – does not require any parameter. The following image<br>
shows the normalization item in the NNDesigner. You can insert only one<br>
normalization layer, and it is positioned at the first place.</p>
</blockquote>
<img src="https://bhrnjica.files.wordpress.com/2018/09/58def1c00db1b050b852f2edbc95cacd.png" alt="Visual Neural Network Designer in ANNdotNET">
<blockquote>
<p><em>Drop layer</em> - requires percentage drop value which is integer. The<br>
following image shows how drop layer looks in the NNDesigner. The drop out<br>
value is in percentage type. There is no any constrains for this layer.</p>
</blockquote>
<img src="https://bhrnjica.files.wordpress.com/2018/09/e33c9fbb01a5887fc95b00b4bf1668be.png" alt="Visual Neural Network Designer in ANNdotNET">
<blockquote>
<p><em>Embedding layer</em> – requires only output dimension to be configured. There<br>
is no any constrains for the layer. The following image shows hot it looks<br>
in the NNDesigner:</p>
</blockquote>
<img src="https://bhrnjica.files.wordpress.com/2018/09/5a0f04d7d17e53735fb0d952eb579d82.png" alt="Visual Neural Network Designer in ANNdotNET">
<blockquote>
<p><em>Dense layer</em> – requires output dimension and activation function to be<br>
configured. There is no any constrains for the layer.</p>
</blockquote>
<img src="https://bhrnjica.files.wordpress.com/2018/09/6bd4466a7928fbed0c5dda668977c8bf.png" alt="Visual Neural Network Designer in ANNdotNET">
<blockquote>
<p><em>LSTM layer</em> – requires: output and cell dimension, activation function, and<br>
two Boolean parameters to enable peephole and self-stabilization variant in<br>
the layer. The following image shows how LSTM item looks in the NNDesigner.</p>
</blockquote>
<img src="https://bhrnjica.files.wordpress.com/2018/09/5840690d3a732f0c600db46597064d26.png" alt="Visual Neural Network Designer in ANNdotNET">
<p>The LSTM layer has some constrains which is already implemented in the code. In<br>
case thwo LSTM layer is added in the network, the network becomes the Stacked<br>
LSTM which should be treated differently. Also LSTM layer is always inserted as<br>
stack, and they cannot be inserted on different places in the list. The<br>
implementation of the Stacked LSTM layer will be shows later.</p>
<h1 id="diferentnetworkconfigurations">Diferent network configurations</h1>
<p>In this section, various network configuration will be listed, in order to show<br>
how easy is to use NNDesigner to create very complex neural network<br>
configurations. Network examples are implemented in pre-calculated examples<br>
which come with default ANNdotNET installation package.</p>
<h1 id="feedforwardnetwork">Feed Forward network</h1>
<p>This example shows how to implement Feed Forward network, with one hidden and<br>
one output layer which is the last layer in the NNDesinger. The example is part<br>
of the ANNdotNET installation package.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/f686019791219d33ee9862023e391977.png" alt="Visual Neural Network Designer in ANNdotNET">
<h1 id="feedforwardwithnormalizationlayer">Feed Forward with Normalization layer</h1>
<p>Tis example shows feed forward network with normalization layer as the first<br>
layer. The example of this configuration can be found in the installation<br>
package of the ANNdotNET.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/6f46879f36026057129de6b82e5b114c.png" alt="Visual Neural Network Designer in ANNdotNET">
<h1 id="feedforwardnetworkwithembeddinglayers">Feed Forward Network with Embedding layers</h1>
<p>In this example embedding layers are used in order to reduce the dimensions of<br>
the input layer. The example is part of the ANNdotNET installation package.<br>
Network is configured with 3 embedding layers, one hidden and output layer.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/9cebf834abfabc6b2b1dc32b5ba3e1d6.png" alt="Visual Neural Network Designer in ANNdotNET">
<h1 id="deepneuralnetwork">Deep Neural Network</h1>
<p>This example shows Deep neural network with three kind of layers: Embeddings,<br>
Drop and Dense layers. The project is part of the ANNdotNET installation<br>
package.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/4ab15c6907fbf7ee5ab7da1924d090ad.png" alt="Visual Neural Network Designer in ANNdotNET">
<h1 id="lstmdeepneuralnetwork">LSTM Deep Neural Network</h1>
<p>This example shows how to configure LSTM based network. The example is part of<br>
the ANNdotNET installation package. The network consist of Normalization,<br>
Embedding, Drop, Dense and LSTM layers. The project is part of the ANNdotNET<br>
installation package.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/009fab18b48fb06aba77f5686353deed.png" alt="Visual Neural Network Designer in ANNdotNET">
<h1 id="stackedlstmneuralnetwork">Stacked LSTM Neural Network</h1>
<p>This is example of Stacked LSTM network, consist of multiple LSTM layers<br>
connected into stack. The example is part of the installation package.</p>
<img src="https://bhrnjica.files.wordpress.com/2018/09/6bdb0e2f07062cb8021a266cddbb7f6b.png" alt="Visual Neural Network Designer in ANNdotNET">
<p>The complete list of examples can be seen at the ANNdotNET Start Page. In order<br>
to open the example, the user just need to click the link.</p>
</div>]]></content:encoded></item><item><title><![CDATA[ANNdotNET the first GUI based CNTK tool]]></title><description><![CDATA[<div class="kg-card-markdown"><h1 id="anndotnet">ANNdotNET</h1>
<p><img src="https://github.com/bhrnjica/anndotnet/blob/master/ANNdotNET/ANNdotNET.Wnd.App/Images/annLogo_start2.png" alt="ANNdotNET Logo"></p>
<p>ANNdotNET is windows desktop application written in C# for creating and training ANN models. The application relies on Microsoft Cognitive Toolkit, CNTK, and it is supposed to be GUI tool for CNTK library with extensions in data preprocessing, model evaluation and exporting capabilities. It is hosted at <a href="http://github.com/bhrnjica/anndotnet">http://github.</a></p></div>]]></description><link>https://developers.de/2018/03/23/anndotnet-the-first-gui-based-cntk-tool/</link><guid isPermaLink="false">5ab4af0f0217470d44580018</guid><category><![CDATA[.NET]]></category><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[C#]]></category><category><![CDATA[AI]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Fri, 23 Mar 2018 07:43:19 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><h1 id="anndotnet">ANNdotNET</h1>
<p><img src="https://github.com/bhrnjica/anndotnet/blob/master/ANNdotNET/ANNdotNET.Wnd.App/Images/annLogo_start2.png" alt="ANNdotNET Logo"></p>
<p>ANNdotNET is windows desktop application written in C# for creating and training ANN models. The application relies on Microsoft Cognitive Toolkit, CNTK, and it is supposed to be GUI tool for CNTK library with extensions in data preprocessing, model evaluation and exporting capabilities. It is hosted at <a href="http://github.com/bhrnjica/anndotnet">http://github.com/bhrnjica/anndotnet</a></p>
<p>Currently supported Network Types of:</p>
<ul>
<li>Simple Feed Forward NN</li>
<li>Deep Feed Forward NN</li>
<li>Recurrent NN with LSTM</li>
</ul>
<p>The process of creating, training, evaluating and exporting models is provided from the GUI Application and does not require knowledge for supported programming languages. The ANNdotNET is ideal for engineers which are not familiar with programming languages.</p>
<h1 id="softwarerequirements">Software Requirements</h1>
<p>ANNdotNET is x64 Windows desktop application which is running on .NET Framework 4.7.1. In order to run the application, the following requirements must be met:</p>
<ul>
<li>Windows 7, 8 or 10 with x64 architecture</li>
<li>.NET Framework 4.7.1</li>
</ul>
<h1 id="howtorunapplication">How to run application</h1>
<p>In order to run the application there are two possibilities:</p>
<ol>
<li>Clone the GitHub repository and open it in Visual Studio 2017. Change build architecture into x64, build and run the application.</li>
<li>Download released version unzip and run ANNdotNET.exe.</li>
</ol>
<p>The following three short videos quickly show how to create, train and evaluate reression, binary and multiclass classification models.</p>
<p><a href="https://raw.githubusercontent.com/bhrnjica/anndotnet/master/Tutorials/anndotnetv1.mp4"> Regression model </a><br>
<a href="https://raw.githubusercontent.com/bhrnjica/anndotnet/master/Tutorials/anndotnetv2.mp4"> Binary classification model </a><br>
<a href="https://raw.githubusercontent.com/bhrnjica/anndotnet/master/Tutorials/anndotnetv3.mp4"> Multiclass classification model </a></p>
<p>More info at <a href="https://bhrnjica.net/anndotnet/">https://bhrnjica.net/anndotnet/</a></p>
</div>]]></content:encoded></item><item><title><![CDATA[Data Preparation Tool for Machine Learning]]></title><description><![CDATA[<div class="kg-card-markdown"><p>Regardless of machine learning library you use, the data preparation is the first and one of the most important step in developing predictive models. It is very often case that the data supposed to be used for the training is dirty with lot of unnecessary columns, full of missing values,</p></div>]]></description><link>https://developers.de/2018/03/05/data-preparation-tool-for-machine-learning/</link><guid isPermaLink="false">5a991d5e4431e912c0f1d7e1</guid><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[.NET]]></category><category><![CDATA[.NET Core]]></category><category><![CDATA[AI]]></category><category><![CDATA[C#]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Mon, 05 Mar 2018 09:57:00 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p>Regardless of machine learning library you use, the data preparation is the first and one of the most important step in developing predictive models. It is very often case that the data supposed to be used for the training is dirty with lot of unnecessary columns, full of missing values, un-formatted numbers etc. Before training the data must be cleaned and properly defined in order to get good model. This is known as data preparation. The data preparation consist of cleaning the data, defining features and labels, deriving the new features from the existing data, handling missing values, scaling the data etc.  It can be concluded that the total time we spend in ML modelling,the most of it is related to data preparation.</p>
<p>In this blog post I am going to present the simple tool which can significantly reduce the preparation time for ML. The tool simply loads the data in to GUI, and then the user can define all necessary information. Once the data is prepared user can store the data it to files which can be then directly imported into ML algorithm such as CNTK.<br>
The following image shows the ML Data Preparation Tool main window.</p>
<img class="alignnone wp-image-7279 size-large" src="https://bhrnjica.files.wordpress.com/2018/03/2018-03-01_9-56-25.png?w=604" alt="" width="604" height="231">
<p>From the image above, the data preparation can be achieved in several steps.<br>
From the image above, the data preparation can be achieved in several steps.</p>
<ul>
<li>Load dirty data into ML Prep Tool, by pressing Import Data button</li>
<li>Transform the data by providing the flowing table:</li>
</ul>
<table>
<thead>
<tr>
<th>Column option</th>
<th>Suboptions</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>xi, y</td>
<td>In case the header is not provided in the imported data, automatic column names is generated.</td>
</tr>
<tr>
<td>Type</td>
<td>Numeric</td>
<td>Indicates the column is cominuous numeric value.</td>
</tr>
<tr>
<td></td>
<td>Binary</td>
<td>Idicated the column data is binary with ony two posible values e.g. (male, femail)</td>
</tr>
<tr>
<td></td>
<td>Category</td>
<td>Indicates the column data is categorical with more than two values. e.g. (R,G,B)</td>
</tr>
<tr>
<td></td>
<td>String</td>
<td>The column will be ignore during export.</td>
</tr>
<tr>
<td>Encoding</td>
<td></td>
<td>In case of Binary and Category column type, the encoding must be defined.</td>
</tr>
<tr>
<td></td>
<td>(0,1)</td>
<td>First binary values will be 0, and second binary values will be 1.</td>
</tr>
<tr>
<td></td>
<td>(-1,1)</td>
<td>First binary values will be -1, and second binary values will be 1.</td>
</tr>
<tr>
<td></td>
<td>N</td>
<td>Category Level where each class treats as numeric value. In case of 3 categories(R,G, B), encoding will be (0,1,2)</td>
</tr>
<tr>
<td></td>
<td>1:N</td>
<td>Category representation with One-Hot vector with N columns. In case of 3 categories(R,G, B), encoding will be R =  (1,0,0),G =  (0,1,0), B =  (0,0,1)</td>
</tr>
<tr>
<td></td>
<td>1:N-1(0)</td>
<td>Category representation with dummy coding with N-1 columns. In case of 3 categories(R, G, B), encoding will be R =  (1,0),G =  (0,1), B =  (0,0)</td>
</tr>
<tr>
<td></td>
<td>1:N-1(-1)</td>
<td>Category representation with dummy coding with N-1 columns. In case of 3 categories(R, G, B), encoding will be R =  (1,0),G =  (0,1), B =  (-1,-1)</td>
</tr>
<tr>
<td>Variable</td>
<td>Input</td>
<td>The column will be treated as feature during export.</td>
</tr>
<tr>
<td></td>
<td>Output</td>
<td>The column will be treated as label during export</td>
</tr>
<tr>
<td></td>
<td>Ignore</td>
<td>The column will be ignore during export.</td>
</tr>
<tr>
<td>Scaling</td>
<td>None</td>
<td>No scaling will be performed during export.</td>
</tr>
<tr>
<td></td>
<td>MinMax</td>
<td>MinMax normalisation will be performed during export.</td>
</tr>
<tr>
<td></td>
<td>Gauss</td>
<td>Gauss standardization will be performed during export.</td>
</tr>
<tr>
<td>Missing Value</td>
<td></td>
<td>defines the replacement for the missing value withing the column. There are several options related to numeric and two options (Random and Mode ) for categorical type.</td>
</tr>
<tr>
<td></td>
<td>Ignore</td>
<td>In case the missing value whole row will be ommited during export.</td>
</tr>
<tr>
<td></td>
<td>Average</td>
<td>Missing value will be replaces with column average value.</td>
</tr>
<tr>
<td></td>
<td>Max</td>
<td>Missing value will be replaces with column max value.</td>
</tr>
<tr>
<td></td>
<td>Min</td>
<td>Missing value will be replaces with column min value.</td>
</tr>
<tr>
<td></td>
<td>Mode</td>
<td>Missing value will be replaces with column mode value.</td>
</tr>
<tr>
<td></td>
<td>Random</td>
<td>Usialy good for binary and Categorical columns. Missing value will be replaces with random value.</td>
</tr>
</tbody>
</table>
<ul>
<li>Defines the testing data set size by providing information of row numbers or percent.
<ul>
<li>Defines export options,</li>
<li>Press Export Button.</li>
</ul>
</li>
</ul>
<p>As can be seen this is straightforward workflow of data preparation.</p>
<p>Besides the general export options which can be achieved by selecting different delimiter options, you can export data set in to CNTK format, which is very handy if you play with CNTK.</p>
<p>After data transformations, the user need to check CNTK format in the export options group box and press Export button in order to get CNTK training and testing files, which can be directly used in the code without any modifications.</p>
<p>Some of examples will be provided in the next blog post.</p>
<p>The project is hosted at GitHub, where the source code can be freely downloaded and used at this location:<a href="https://github.com/bhrnjica/MLDataPreparationTool">https://github.com/bhrnjica/MLDataPreparationTool</a>.</p>
<p>In case you want only binaries, the release of version v1.0 is published here: <a href="https://github.com/bhrnjica/MLDataPreparationTool/releases/tag/v1.0">https://github.com/bhrnjica/MLDataPreparationTool/releases/tag/v1.0</a>.</p>
</div>]]></content:encoded></item><item><title><![CDATA[Using CNTK and Python to learn from Iris data]]></title><description><![CDATA[<div class="kg-card-markdown"><p>In this blog post we are going to implement training and evaluation ANN model based on Iris data set using CNTK and Python. The Iris data set has categorical output value which contains three classes : Sentosa, Virglica and Versicolor. The features consist of the 4 real value inputs. The Iris</p></div>]]></description><link>https://developers.de/2018/02/28/using-cntk-and-python-to-learn-from-iris-data/</link><guid isPermaLink="false">5a9727434431e912c0f1d7d4</guid><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Wed, 28 Feb 2018 22:15:03 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p>In this blog post we are going to implement training and evaluation ANN model based on Iris data set using CNTK and Python. The Iris data set has categorical output value which contains three classes : Sentosa, Virglica and Versicolor. The features consist of the 4 real value inputs. The Iris data set can be easily found on  the internet. One of the places is on <a href="http://kaggle.com">http://kaggle.com</a></p>
<p>Usually, the Iris data is given in the flowing format:</p>
<img class="alignnone size-full wp-image-7082" src="https://bhrnjica.files.wordpress.com/2017/11/iris_data_format.png" alt="" width="506" height="434">
<p>Since we are going to use CNTK we should prepare the data in cntk file format, which is far from the format we can see on the previous image. This format has different structure and looks like on the flowing image:</p>
<img class="alignnone size-full wp-image-7083" src="https://bhrnjica.files.wordpress.com/2017/11/iris_data_format_cntk.png" alt="" width="506" height="442">
<p>The difference is obvious. To transform the previous file format in to the cntk format it tooks me several minutes and now we can continue with the implementation.</p>
<p>First, lets implement simple python function to read the cntk format. For the implementation we are going to use CNTK MinibatchSource, which is specially developed to handle file data. The flowing python code reads the file and return the MinibatchSource.</p>
<pre><code class="language-python">import cntk

# The data in the file must satisfied the following format:
# |labels 0 0 1 |features 2.1 7.0 2.2 - the format consist of 4 features and one 3 component hot vector
# represents the iris flowers
def create_reader(path, is_training, input_dim, num_label_classes):

# create the streams separately for the label and for the features
labelStream = cntk.io.StreamDef(field='label', shape=num_label_classes, is_sparse=False)
featureStream = cntk.io.StreamDef(field='features', shape=input_dim, is_sparse=False)

# create deserializer by providing the file path, and related streams
deserailizer = cntk.io.CTFDeserializer(path, cntk.io.StreamDefs(labels = labelStream, features = featureStream))

# create mini batch source as function return
mb = cntk.io.MinibatchSource(deserailizer, randomize = is_training, max_sweeps = cntk.io.INFINITELY_REPEAT if is_training else 1)
return mb
</code></pre>
<p>The code above take several arguments:</p>
<p>-path - the file path where the data is stored,</p>
<p>-is_training - Boolean variable which indicates if the data is for training or testing. In case of training the data will be randomized.</p>
<ul>
<li>input_dim, num_label_classes are the numbers of the input features and the output hot vector size. Those two arguments are important in order to properly parse the file.</li>
</ul>
<p>The first method creates the two streams , which are passed as argument in order to create deserializer, and then for minibatchsource creation. The function returns minibatchsource object which the trainer uses for data handling.</p>
<p>Once that we implemented the data reader, we need the python function for model creation. For the Iris data set we are going to create 4-50-3 feed forward neural network, which consist of one input layer with 4 neurons, one hidden layer with 50 neurons and the output layer with 4 neurons. The hidden layer will contain tanh- activation function.</p>
<p>The function which creates the NN model will looks like on the flowing code snippet:</p>
<pre><code>#model creation
# FFNN with one input, one hidden and one output layer 
def create_model(features, hid_dim, out_dim):
    #perform some initialization 
    with cntk.layers.default_options(init = cntk.glorot_uniform()):
        #hidden layer with hid_def number of neurons and tanh activation function
        h1=cntk.layers.Dense(hid_dim, activation= cntk.ops.tanh, name='hidLayer')(features)
        #output layer with out_dim neurons
        o = cntk.layers.Dense(out_dim, activation = None)(h1)
        return o
</code></pre>
<p>As can be seen Dense function creates the layer where the user has to specify the dimension of the layer, activation function and the input variable. When the hidden layer is created, input variable is set to the input data. The output layer is created for the hidden layer as input.</p>
<p>The one more helper function would be showing the progress of the learner. The flowing function takes the three arguments and prints the current status of the trainer.</p>
<pre><code># Function that prints the training progress
def print_training_progress(trainer, mb, frequency):
    training_loss = &quot;NA&quot;
    eval_error = &quot;NA&quot;

    if mb%frequency == 0:
        training_loss = trainer.previous_minibatch_loss_average
        eval_error = trainer.previous_minibatch_evaluation_average
        print (&quot;Minibatch: {0}, Loss: {1:.4f}, Error: {2:.2f}%&quot;.format(mb, training_loss, eval_error*100))   
    return mb, training_loss, eval_error
</code></pre>
<p>Once we implemented all three functions we can start with CNTK learning on the Iris data.</p>
<p>At the beginning,  we have to specify some helper variable which we will use later.</p>
<pre><code>#setting up the NN type
input_dim=4
hidden_dim = 50
num_output_classes=3
input = cntk.input_variable(input_dim)
label = cntk.input_variable(num_output_classes)
</code></pre>
<p>Create the reader for data batching.</p>
<pre><code># Create the reader to training data set
reader_train= create_reader(&quot;C:/sc/Offline/trainData_cntk.txt&quot;,True,input_dim, num_output_classes)
</code></pre>
<p>Then create the NN model, with Loss and Error functions:</p>
<pre><code>#Create model and Loss and Error function
z= create_model(input, hidden_dim,num_output_classes);
loss = cntk.cross_entropy_with_softmax(z, label)
label_error = cntk.classification_error(z, label)
</code></pre>
<p>Then we defined how look like the trainer. The trainer will be with Stochastic Gradient Decadent learner, with learning rate of 0.2</p>
<pre><code># Instantiate the trainer object to drive the model training
learning_rate = 0.2
lr_schedule = cntk.learning_parameter_schedule(learning_rate)
learner = cntk.sgd(z.parameters, lr_schedule)
trainer = cntk.Trainer(z, (loss, label_error), [learner])
</code></pre>
<p>Now we need to defined parameters for learning, and showing results.</p>
<pre><code># Initialize the parameters for the trainer
minibatch_size = 120 #mini batch size will be full data set
num_iterations = 20 #number of iterations 

# Map the data streams to the input and labels.
input_map = {
label  : reader_train.streams.labels,
input  : reader_train.streams.features
} 
# Run the trainer on and perform model training
training_progress_output_freq = 1

plotdata = {&quot;batchsize&quot;:[], &quot;loss&quot;:[], &quot;error&quot;:[]}
</code></pre>
<p>As can be seen the batchsize is set to dataset size which is typical for small data sets.  Since we defined minibach to dataset size, the iteration should be very small value since Iris data is very simple and the learner will find good result very fast.</p>
<p>Running the trainer looks very simple. For each iteration, the reader load the batch size amount of the data, and pass to the trainer. The trainer performs the learning process using SGD learner, and returns the Loss and the error value for the current iteration. Then we call print function to show the progress of the trainer.</p>
<pre><code>for i in range(0, int(num_iterations)):
        # Read a mini batch from the training data file
        data=reader_train.next_minibatch(minibatch_size, input_map=input_map) 
        trainer.train_minibatch(data)
        batchsize, loss, error = print_training_progress(trainer, i, training_progress_output_freq)
        if not (loss == &quot;NA&quot; or error ==&quot;NA&quot;):
            plotdata[&quot;batchsize&quot;].append(batchsize)
            plotdata[&quot;loss&quot;].append(loss)
            plotdata[&quot;error&quot;].append(error)
</code></pre>
<p>Once the learning process completes, we can perform some result presentation.</p>
<pre><code># Plot the training loss and the training error
import matplotlib.pyplot as plt

plt.figure(1)
plt.subplot(211)
plt.plot(plotdata[&quot;batchsize&quot;], plotdata[&quot;loss&quot;], 'b--')
plt.xlabel('Minibatch number')
plt.ylabel('Loss')
plt.title('Minibatch run vs. Training loss')

plt.show()

plt.subplot(212)
plt.plot(plotdata[&quot;batchsize&quot;], plotdata[&quot;error&quot;], 'r--')
plt.xlabel('Minibatch number')
plt.ylabel('Label Prediction Error')
plt.title('Minibatch run vs. Label Prediction Error')
plt.show()
</code></pre>
<p>We plot the Loss and Error function converted in to total accuracy of the classifier. The folowing pictures shows those graphs.</p>
<img class="alignnone size-full wp-image-7099" src="https://bhrnjica.files.wordpress.com/2017/11/iris_cntk_grapf01.png" alt="" width="396" height="159">
<img class="alignnone size-large wp-image-7100" src="https://bhrnjica.files.wordpress.com/2017/11/iris_cntk_grapf02.png?w=396" alt="" width="396" height="159">
<p>The last part of the ML procedure is the testing or validating the model. FOr the Iris data set we prepare 20 samples which will be used for the testing. The code i similar to the previous, except we call create_reader with different file name. Then we try to evaluate the model and grab the Loss and error values, and print out.</p>
<pre><code># Read the training data
reader_test = create_reader(&quot;C:/sc/Offline/testData_cntk.txt&quot;,False, input_dim, num_output_classes)

test_input_map = {
    label  : reader_test.streams.labels,
    input  : reader_test.streams.features,
}

# Test data for trained model
test_minibatch_size = 20
num_samples = 20
num_minibatches_to_test = num_samples // test_minibatch_size
test_result = 0.0

for i in range(num_minibatches_to_test):
    
    data = reader_test.next_minibatch(test_minibatch_size,input_map = test_input_map)
    eval_error = trainer.test_minibatch(data)
    test_result = test_result + eval_error

# Average of evaluation errors of all test minibatches
print(&quot;Average test error: {0:.2f}%&quot;.format(test_result*100 / num_minibatches_to_test))
</code></pre>
<p>Full sample with python code and data set can be found <a href="https://1drv.ms/f/s!AgPZDj-_uxGLhO46bF3FiQPD1t-dkQ">here</a>.</p>
</div>]]></content:encoded></item><item><title><![CDATA[Using CNTK and C# to train Mario to drive Kart]]></title><description><![CDATA[<div class="kg-card-markdown"><h2>Introduction</h2>
In this blog post I am going to explain one of possible way how to implement Deep Learning ML to play video game. For this purpose I used the following:
<ol>
 	<li> N64 Nintendo emulator which can be found <a href="https://www.pj64-emu.com/">here,</a></li>
 	<li>Mario Kart 64 ROM, which can be found on internet as</li></ol></div>]]></description><link>https://developers.de/2018/02/20/using-cntk-and-c-to-train-mario-to-drive-kart/</link><guid isPermaLink="false">5a8c7eabc2b22913a88f888f</guid><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[.NET]]></category><category><![CDATA[.NET Core]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Tue, 20 Feb 2018 20:08:03 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><h2>Introduction</h2>
In this blog post I am going to explain one of possible way how to implement Deep Learning ML to play video game. For this purpose I used the following:
<ol>
 	<li> N64 Nintendo emulator which can be found <a href="https://www.pj64-emu.com/">here,</a></li>
 	<li>Mario Kart 64 ROM, which can be found on internet as well,</li>
 	<li>CNTK - Microsoft Cognitive Toolkit</li>
 	<li>.NET Framework and C#</li>
</ol>
The idea behind this machine learning project is to capture images together with action, while you play Mario Kart game. Then captured images are transformed into features of training data set, and action keys into label hot vectors respectively.  Since we need to capture images, the emulator should be positioned at fixed location and size during playing the game, as well as during testing algorithm to play game. The flowing image shows N64 emulator graphics configuration settings.
<img class="alignnone size-full wp-image-7269" src="https://bhrnjica.files.wordpress.com/2018/02/2018-02-15_16-34-03.png" alt="2018-02-15_16-34-03" width="802" height="670">
<p>Also the N64 emulator is positioned to Top-Left corned of screen, so it is easier to capture the images.</p>
<h2>Data collection for training data set</h2>
During image captures game is played as you would play normally. Also no special agent, not platform is required.
<p>In .NET and C# it is implemented image capture from the specific position of screen, as well as it is recorded which keys are pressed during game play. In order to record keys press, the code found <a href="https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/">here </a>is modified and used.</p>
<p>The flowing image shows the position of N64 emulator with playing Mario Kart game (1), the windows which is capture and transform the image (2), and the application which collect images, and key press action and generated training data set into file(3).</p>
<img class="alignnone size-full wp-image-7270" src="https://bhrnjica.files.wordpress.com/2018/02/2018-02-15_16-31-42.png" alt="2018-02-15_16-31-42" width="1920" height="1157">
<p>The data is generated on the following way:</p>
<ul>
 	<li>each image is captured, resized to 100x74 pixels and gray scaled prior to be transformed and persisted to data set training file.</li>
 	<li>before image is persisted the hotkey of action key press is recorded and connected to image.</li>
</ul>
So the training data is persisted into CNTK format which consist of:
<ol>
 	<li><span style="color: #0000ff;">|label</span> - which represent 5 component hot vector, indicate: Forward, Break, Forward-Left, Forward-Right and None (1 0 0 0 0)</li>
 	<li><span style="color: #0000ff;">|features</span> consist of 100x74 numbers which represent pixels of the images.</li>
</ol>
The following data sample shows how training data set are persisted in the txt file:
<pre>|label 1 0 0 0 0 |features 202 202 202 202 202 202 204 189 234 209 199...
|label 0 1 0 0 0 |features 201 201 201 201 201 201 201 201 203 18...
|label 0 0 1 0 0 |features 199 199 199 199 199 199 199 199 199 19...
|label 0 0 0 1 1 |features 199 199 199 199 199 199 199 199 199 19...
</pre>
Since my training data is more than 300 000 MB of size, I provided just few BM sized file, but you can generate file as big as you wish with just playing the game, and running the flowing code from Program.cs file:
<pre><code class="language-csharp"> await GenerateData.Start();
</code></pre>
<h2>Training Model to play the game</h2>
Once we generate the data, we can move to the next step: training RCNN model to play the game. For training model the CNTK is used. Also since we play a game and previous sequence will determined the next sequence in the game, LSTM RNN is used. More information about CNTK and LSTM can be found in previous posts. In my case I have collected nearly 15000 images during several round of playing the same level and route. Also for more accurate model much more images should be collected, nearly 100 000. The model is trained in one hour, with 500000 iterations. The source code about whole project can be found on <a href="https://github.com/bhrnjica/LSTMBotGame">GitHub </a>page. (<a href="http://github.com/bhrnjica/LSTMBotGame">http://github.com/bhrnjica/LSTMBotGame </a>)
<p>By running the following code, the training process is started with provided training data:</p>
<pre><code class="language-csharp"> CNTKDeepNN.Train(DeviceDescriptor.GPUDevice(0));
</code></pre>
<h2>Playing the game with CNTK model</h2>
Once we trained the model, we move to the next step: playing a game. The emulator should be positioned on the same position and with the same size in order to play the game.ONce the model is trained and created in th training folder, the playing game can be achive by running:
<pre><code class="language-csharp">var dev = DeviceDescriptor.CPUDevice;
MarioKartPlay.LoadModel(&quot;../../../../training/mario_kart_modelv1&quot;, dev);
MarioKartPlay.PlayGame(dev);
</code></pre>
<p>How it looks like on my case, you can see on this youtube video:</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/oTzc2ixnNQE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></div>]]></content:encoded></item><item><title><![CDATA[CNTK Object Detection on Custom Dataset with Python]]></title><description><![CDATA[Recently, I was playing with CNTK object detection API, and produced very interesting model which can recognize the Nokia3310 mobile phone. As you probably already know Nokia3310 is legendary mobile phone which was popular 15 years ago, and recently re-branded by Nokia.]]></description><link>https://developers.de/2018/02/16/cntk-object-detection-on-custom-dataset-with-python/</link><guid isPermaLink="false">5a86e67dc2b22913a88f887e</guid><category><![CDATA[CNTK]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Fri, 16 Feb 2018 14:16:32 GMT</pubDate><media:content url="https://developersde.blob.core.windows.net/usercontent/2018/2/161414_cntk_obj_det.png" medium="image"/><content:encoded><![CDATA[<div class="kg-card-markdown"><img src="https://developersde.blob.core.windows.net/usercontent/2018/2/161414_cntk_obj_det.png" alt="CNTK Object Detection on Custom Dataset with Python"><p><img src="https://bhrnjica.files.wordpress.com/2018/02/figure_1.png" alt="CNTK Object Detection on Custom Dataset with Python"></p>
<p>Recently, I was playing with CNTK object detection API, and produced very interesting model which can recognize the Nokia3310 mobile phone. As you probably already know Nokia3310 is legendary mobile phone which was popular 15 years ago, and recently re-branded by Nokia.</p>
<p>In this blog post I will provide you with step by step introductions how to:</p>
<ul>
<li>prepare images for training</li>
<li>generate training data for selected images by using VOOT tool,</li>
<li>prepare Python code for object detection using FasterRCNN alogirithm implemented with CNTK,</li>
<li>testing custom image in order to detect Nokia3310 on image.</li>
</ul>
<h2 id="preparingimageformodeltraining">Preparing Image for model training</h2>
<p>Finding appropriate images for our model is very easy. Just go to google.com and type &quot;Nokia3310&quot; and bum, there are plenty of images.</p>
<img class="alignnone size-medium wp-image-7255" src="https://bhrnjica.files.wordpress.com/2018/02/google_search_nokia3310.png?w=300" alt="CNTK Object Detection on Custom Dataset with Python" width="300" height="267">
<p>Find at least 20 images, and put into the Nokia3310 image folder. Once we collect enough image for the model, we can move to the next step.</p>
<h2 id="generatingdatafromtheimagedatasetusingvotttool">Generating data from the image data set using VOTT tool</h2>
<p>In order to train image detection model by using FasterRCNN algoritm, we have to provide three kinds of data separated in three different files:</p>
<ol>
 	<li><em>class_map file</em> - which contains list of available objects which the model should recognize on the image,</li>
 	<li><em>train_image file</em> - which contains the list of image file paths</li>
 	<li><em>train roi file</em> - which contains "region of interest" data. The data is consisting of list of 4 numbers which represent the top, left, right and bottom coordinate producing rectangle of the object.</li>
</ol>
Seems pretty much job for simple object detection, but hopefully there is a tool which can generate all data for us. It is called  VoTT: Visual Object Tagging Tool, and it can be found at : https://github.com/Microsoft/VoTT.
## Generating Image data with VOTT
Here we will explain in detail how to generate image data by using VOTT tool.
<ol>
<li>
<p>Open VOTT tool, from File menu and select folder we previously collected with images.</p>
</li>
<li>
<p>Enter &quot;<em>nokia3310</em>&quot;  in Labels edit box and click <em>Continue</em> button. In case we have more than one</p>
</li>
</ol>
<img class="alignnone size-medium wp-image-7256" src="https://bhrnjica.files.wordpress.com/2018/02/vott-tool-preparation.png?w=300" alt="CNTK Object Detection on Custom Dataset with Python" width="300" height="211">
<ol start="3">
<li>
<p>Then for each image, make a rectangle on each object which represents the Nokia3310.</p>
</li>
<li>
<p>Once you finish with tagging for one image, press Next, and do the same for all selected images.</p>
</li>
</ol>
<img class="alignnone size-large wp-image-7257" src="https://bhrnjica.files.wordpress.com/2018/02/tagginginvott.png?w=604" alt="CNTK Object Detection on Custom Dataset with Python" width="604" height="428">
<ol start="5">
<li>Once the process of tagging is finished, then the export action can be performed.</li>
</ol>
<img class="alignnone size-large wp-image-7258" src="https://bhrnjica.files.wordpress.com/2018/02/tagginginvott01.png?w=604" alt="CNTK Object Detection on Custom Dataset with Python" width="604" height="423">
<ol start="6">
<li>With Export option data is generated for each rectangle we made, and the two files are generated for each image in data set. Also once the tagging process is completed VOTT tool generated three folders:</li>
</ol>
<p>a) negative - contains images which have no any tagged rectangle (no nokia3310 on images),</p>
<p>b) positive - contains approximate 70% of all images which we tagged Nokia3310 object, and this folder will be used for training the model,</p>
<p>c) testImages - contains approximate 30% of all images which we tagged Nokia3310 object, and this folder will be used for evaluation and testing the model.</p>
<p>The VOOT classified all images in three folders. In case there are images with no tagging, images will be moved to negatives, all other images is separated into positive and testImages folder.</p>
<p>From each image two files are generated:</p>
<p>-<em>[imagename].bboxes.labels.tsv</em> - which consist of all labels tagged in image file.</p>
<p>-<em>[imagename].bboxes.tsv</em> - rectangle coordinates of all tags in the image.</p>
<h2 id="processingvottgenerateddataintocntktrainingandtestingdatasetfiles">Processing VOTT generated data into CNTK training and testing dataset files</h2>
<p>Once we have VOTT generated data, we need to transform them into cntk format. First we will generate: <em>class_map file.txt</em>.</p>
<ol start="7">
<li>Create new &quot;<em>class_map file.txt</em>&quot;  file, and put the following text into it:</li>
</ol>
<pre><code>__background__	0
Nokia3310	1
</code></pre>
<p>As can be seen there is only one class which we want to detect, and ti is Nokia3310, (the <strong>backgroud</strong> is reserved tag which is added by default and cannot be removed). Now we need to generate the second file:<br>
8. Create new &quot;<em>train_image_file.txt</em>&quot; file, and put text similar with this one:</p>
<pre><code>0 positive/img01.jpg 0
1 positive/img05.jpg 0
2 positive/img10.jpg 0
...
</code></pre>
<p>The content of the file is list of all images placed in positive folder, with ID on the left side and zero on the right side, separated by tabulator. Image path should be relative.<br>
9. Create new <em>&quot;train_roi_file.txt&quot;</em>, and put data similar with this one:</p>
<pre><code>0 |roiAndLabel 0	10	418	340 1 
1 |roiAndLabel 631	75	731	298 1
2 |roiAndLabel 47	12	222	364 1
3 |roiAndLabel 137	67	186	184 1 188	69	234	180 1
...
</code></pre>
<p>As can be seen blue text is rectangle coordinate, which folow the red number indicates classValue. Since we have only one class 1 is always after 4 blue numbers. Also in case image contains more than one rectangle which is the case of line 3, after every four blue numbers it goes class value.</p>
<p>This is procedure how can we make three files for training, needed to run CNTK object detection. Also for testing data we need image and roi files. WHole dataset and coresponded files can be found on GitHub page.</p>
<h2 id="implementationofobjectdetection">Implementation of Object Detection</h2>
<p>CNTK comes with example how to implement object detection which can be found at: <a href="https://github.com/Microsoft/CNTK/tree/master/Examples/Image/Detection">https://github.com/Microsoft/CNTK/tree/master/Examples/Image/Detection</a></p>
<p>So I took the source code from <a href="https://github.com/Microsoft/CNTK/tree/master/Examples/Image/Detection">there</a>, and modify it for my case, and published at git hub which can be found <a href="https://github.com/bhrnjica/ObjectDetection">here</a>.</p>
<ol start="10">
<li>
<p>Before downloading source code, be sure the CNTK 2.3 is installed on your machine with Anaconda 4.1.1, in the environment with Python 3.5 version.</p>
</li>
<li>
<p>Clone the Github repository <a href="https://github.com/bhrnjica/ObjectDetection">https://github.com/bhrnjica/ObjectDetection</a> and open it in Visual Studio or Visual Studio Code.</p>
</li>
<li>
<p>First thing you should do is to download pre-trained &quot;Alex net&quot; model. You can easily download it, by running the <em>download_model.py</em> python code placed in <em>PretrainedModels</em> folder.</p>
</li>
<li>
<p>Process of training is started when you run <em>Nokia3310_detection.py</em> python file. Beside pre-trained model, no other resources are required in order to run the project. The folowing picture shows main parts of the solution.</p>
</li>
</ol>
<img class="alignnone size-full wp-image-7263" src="https://bhrnjica.files.wordpress.com/2018/02/structure-of-the-example.png" alt="CNTK Object Detection on Custom Dataset with Python" width="520" height="664">
<p>Once the training process is finished, once image is evaluated and shown in order to evaluate how model is good in detecting the phone. Such image is shows at the beginning of the blog post.</p>
<img class="alignnone size-large wp-image-7265" src="https://bhrnjica.files.wordpress.com/2018/02/cntk_obj_det_evaluation.png?w=604" alt="CNTK Object Detection on Custom Dataset with Python" width="604" height="511">
<p>All source code with image dataset you can download from GitHub at <a href="https://github.com/bhrnjica/ObjectDetection">https://github.com/bhrnjica/ObjectDetection</a></p>
</div>]]></content:encoded></item><item><title><![CDATA[Use CNTK and LSTM in Time Series prediction with .NET and C#]]></title><description><![CDATA[This post shows how to model Time Series data using CNTK library, and LSTM RNN in C# programming language.
Originally the tutorial is written in Python so this would give .NET community more examples how to use CNTK in .NET.]]></description><link>https://developers.de/2018/01/20/use-cntk-and-lstm-in-time-series-prediction-using-c/</link><guid isPermaLink="false">5a6363fd00f70e2a908d9a09</guid><category><![CDATA[.NET]]></category><category><![CDATA[MachineLearning]]></category><category><![CDATA[CNTK]]></category><category><![CDATA[TimeSeries]]></category><dc:creator><![CDATA[Bahrudin Hrnjica]]></dc:creator><pubDate>Sat, 20 Jan 2018 17:00:09 GMT</pubDate><media:content url="https://developersde.blob.core.windows.net/usercontent/2018/1/201623_sinewave.jpg" medium="image"/><content:encoded><![CDATA[<div class="kg-card-markdown"><img src="https://developersde.blob.core.windows.net/usercontent/2018/1/201623_sinewave.jpg" alt="Use CNTK and LSTM in Time Series prediction with .NET and C#"><p>This post shows how to implement CNTK 106 Tutorial in C#. This tutorial lecture is written in Python and there is no related example in C#. For this reason I decided to translate this very good tutorial into C#. The tutorial can be found at: <a href="http://http://cntk.ai/pythondocs/CNTK_106A_LSTM_Timeseries_with_Simulated_Data.html">CNTK 106: Part A – Time series prediction with LSTM (Basics)</a> and uses <em>sin wave</em> function in order to predict time series data. For this problem the Long Short Term Memory (LSTM) Recurrent Neural Network is used.</p>
<h2 id="goal">Goal</h2>
<p>The goal of this tutorial is prediction the simulated data of a continuous function ( <a href="https://en.wikipedia.org/wiki/Sine">sin wave</a>). From <em>N</em> previous values of the <em>y=sin(t)</em> function where <em>y</em> is the observed amplitude signal at time <em>t</em>, prediction of  <em>M</em> values of <em>y</em> is going to predict for the corresponding future time points.<br>
<img src="https://bhrnjica.files.wordpress.com/2017/12/sinewave.jpg" alt="Use CNTK and LSTM in Time Series prediction with .NET and C#"></p>
<p>The excitement of this tutorial is using the LSTM recurrent neural network which is nicely suited for this kind of problems. As you probably know LSTM is special recurrent neural network which has ability to learn from its experience during the training. More information about this fantastic version of recurrent neural network can be found <a href="http://colah.github.io/posts/2015-08-Understanding-LSTMs">here</a>.</p>
<p>The blog post is divided into several sub-sections:</p>
<ul>
<li>Simulated data part,</li>
<li>LSTM Network, and</li>
<li>Model training and evaluation.</li>
</ul>
<p>Since the simulated data set is huge, the original tutorial has two running mode which is described by the variable <em>isFast</em>. In case of fast mode, the variable is set to <em>True</em>, and this mode will be used in this tutorial. Later, the reader may change the value to <em>False</em> in order to see much better training model, but the training time will be much longer. The Demo for this this blog post exposes variables of the batch size and iteration number to the user, so the user may defined those numbers as he/she want.</p>
<h2 id="datageneration">Data generation</h2>
<p>In order to generate simulated sin wave data, we are going to implement several helper methods. Let $latex N$ and $latex M$  be a ordered set of past values and future (desired predicted values) of the sine wave, respectively. The two methods are implemented:</p>
<ul>
<li>generateWaveDataset() and</li>
<li>splitData()</li>
</ul>
<p>The generateWaveDataset takes the periodic function,set of independent values (which is corresponded the time for this case) and generate the wave function, by providing the time steps and time shift. The method is related to the generate_data() python method from the original tutorial.</p>
<pre><code>static Dictionary&lt;string, (float[][] train, float[][] valid, float[][] test)&gt; generateWaveDataset(Func&lt;double, double&gt; fun, float[] x0, int timeSteps, int timeShift)
{
    ////fill data
    float[] xsin = new float[x0.Length];//all data
    for (int l = 0; l &lt; x0.Length; l++)
        xsin[l] = (float)fun(x0[l]);


    //split data on training and testing part
    var a = new float[xsin.Length - timeShift];
    var b = new float[xsin.Length - timeShift];

    for (int l = 0; l &lt; xsin.Length; l++)
    {
        //
        if (l &lt; xsin.Length - timeShift) a[l] = xsin[l]; // if (l &gt;= timeShift)
            b[l - timeShift] = xsin[l];
    }

    //make arrays of data
    var a1 = new List&lt;float[]&gt;();
    var b1 = new List&lt;float[]&gt;();
    for (int i = 0; i &lt; a.Length - timeSteps + 1; i++)
    {
        //features
        var row = new float[timeSteps];
        for (int j = 0; j &lt; timeSteps; j++)
            row[j] = a[i + j];
        //create features row
        a1.Add(row);
        //label row
        b1.Add(new float[] { b[i + timeSteps - 1] });
    }

    //split data into train, validation and test data set
    var xxx = splitData(a1.ToArray(), 0.1f, 0.1f);
    var yyy = splitData(b1.ToArray(), 0.1f, 0.1f);

    var retVal = new Dictionary&lt;string, (float[][] train, float[][] valid, float[][] test)&gt;();
    retVal.Add(&quot;features&quot;, xxx);
    retVal.Add(&quot;label&quot;, yyy);
    return retVal;
}
</code></pre>
<p>Once the data is generated, three datasets should be created:</p>
<ul>
<li>train,</li>
<li>validate and</li>
<li>test dataset.</li>
</ul>
<p>They are generated by splitting the dataset generated by the above method. The following splitData method splits the original <em>sin wave</em> dataset into three datasets.</p>
<pre><code>static (float[][] train, float[][] valid, float[][] test) splitData(float[][] data, float valSize = 0.1f, float testSize = 0.1f)
{
    //calculate
    var posTest = (int)(data.Length * (1 - testSize));
    var posVal = (int)(posTest * (1 - valSize));

    return (data.Skip(0).Take(posVal).ToArray(), data.Skip(posVal).Take(posTest - posVal).ToArray(), data.Skip(posTest).ToArray());
}
</code></pre>
<p>In order to visualize the data, the Windows Forms project is created. Moreover, the ZedGraph .NET class library is used in order to visualize the data. The following picture shows the generated data.</p>
<p><img src="https://bhrnjica.files.wordpress.com/2017/12/sinwavefun_vizualization.png?w=604" alt="Use CNTK and LSTM in Time Series prediction with .NET and C#"></p>
<h2 id="networkmodeling">Network modeling</h2>
<p>As mentioned on the beginning of the blog post, we are going to create LSTM recurrent neural network, with 1 LSTM cell for each input. We have N inputs and each input is a value in our continuous function. The N outputs from the LSTM are the input into a dense layer that produces a single output. Between LSTM and dense layer we insert a dropout layer that randomly drops 20% of the values coming from the LSTM to prevent overfitting the model to the training dataset. We want use use the dropout layer during training but when using the model to make predictions we don’t want to drop values.<br>
The description above can be illustrated on the following picture:</p>
<p><img src="https://bhrnjica.files.wordpress.com/2017/12/cntk106a_model_s3.png" alt="Use CNTK and LSTM in Time Series prediction with .NET and C#"></p>
<p>The implementation of the LSTM can be sumarize in one method, but the real implementation can be viewed in the demo sample which is attached with this blog post.<br>
The following methods implements LSTM network depicted on the image above. The arguments for the method are already defined.</p>
<pre><code>public static Function CreateModel(Variable input, int outDim, int LSTMDim, int cellDim, DeviceDescriptor device, string outputName)
{

    Func&lt;Variable, Function&gt; pastValueRecurrenceHook = (x) =&gt; CNTKLib.PastValue(x);

    //creating LSTM cell for each input variable
    Function LSTMFunction = LSTMPComponentWithSelfStabilization&lt;float&gt;(
        input,
        new int[] { LSTMDim },
        new int[] { cellDim },
        pastValueRecurrenceHook,
        pastValueRecurrenceHook,
        device).Item1;

    //after the LSTM sequence is created return the last cell in order to continue generating the network
    Function lastCell = CNTKLib.SequenceLast(LSTMFunction);

    //implement drop out for 10%
    var dropOut = CNTKLib.Dropout(lastCell,0.2, 1);

    //create last dense layer before output
    var outputLayer =  FullyConnectedLinearLayer(dropOut, outDim, device, outputName);

    return outputLayer;
}
</code></pre>
<h2 id="trainingthenetwork">Training the network</h2>
<p>In order to train the model, the nextBatch() method is implemented that produces batches to feed the training function. Note that because CNTK supports variable sequence length, we must feed the batches as list of sequences. This is a convenience function to generate small batches of data often referred to as minibatch.</p>
<pre><code>private static IEnumerable&lt;(float[] X, float[] Y)&gt; nextBatch(float[][] X, float[][] Y, int mMSize)
{

    float[] asBatch(float[][] data, int start, int count)
    {
        var lst = new List&lt;float&gt;();
        for (int i = start; i &lt; start + count; i++) { if (i &gt;= data.Length)
                break;

            lst.AddRange(data[i]);
        }
        return lst.ToArray();
    }

    for (int i = 0; i &lt;= X.Length - 1; i += mMSize) { var size = X.Length - i; if (size &gt; 0 &amp;&amp; size &gt; mMSize)
            size = mMSize;

        var x = asBatch(X, i, size);
        var y = asBatch(Y, i, size);

        yield return (x, y);
    }
}
</code></pre>
<p>Note: Since the this tutorial is implemented as WinForms C# project which can visualize training and testing datasets, as well as it  can show the best found model during the training process, there are lot of other implemented methods which are not mentioned here, but can be found in the demo source code attached in this blog post.</p>
<h2 id="keyinsight">Key Insight</h2>
<p>When working with LSTM the user should pay attention on the following:</p>
<ul>
<li>Since LSTM must work with axes with unknown dimensions, the variables should be defined on different way as we could saw in the previous blog posts. So the input and the output variable are initialized with the following code listing:</li>
</ul>
<pre><code>// build the model
var feature = Variable.InputVariable(new int[] { inDim }, DataType.Float, featuresName, null, false /*isSparse*/);
var label = Variable.InputVariable(new int[] { ouDim }, DataType.Float, labelsName, new List&lt;CNTK.Axis&gt;() { CNTK.Axis.DefaultBatchAxis() }, false);
</code></pre>
<ul>
<li>As specified in the original tutorial: &quot;<em>Specifying the dynamic axes enables the recurrence engine handle the time sequence data in the expected order. Please take time to understand how to work with both static and dynamic axes in CNTK as described <a href="https://www.cntk.ai/pythondocs/sequence.html">here</a>&quot;</em>, the dynamic axes is key point in LSTM.<br>
Now the implementation is continue with the defining learning rate, momentum, the learner and the trainer.</li>
</ul>
<pre><code>var lstmModel = LSTMHelper.CreateModel(feature, ouDim, hiDim, cellDim, device, &quot;timeSeriesOutput&quot;);

* Function trainingLoss = CNTKLib.SquaredError(lstmModel, label, &quot;squarederrorLoss&quot;);
Function prediction = CNTKLib.SquaredError(lstmModel, label, &quot;squarederrorEval&quot;);


// prepare for training
TrainingParameterScheduleDouble learningRatePerSample = new TrainingParameterScheduleDouble(0.0005, 1);
TrainingParameterScheduleDouble momentumTimeConstant = CNTKLib.MomentumAsTimeConstantSchedule(256);

IList&lt;Learner&gt; parameterLearners = new List&lt;Learner&gt;() {
    Learner.MomentumSGDLearner(lstmModel.Parameters(), learningRatePerSample, momentumTimeConstant, /*unitGainMomentum = */true)  };

//create trainer
var trainer = Trainer.CreateTrainer(lstmModel, trainingLoss, prediction, parameterLearners);
</code></pre>
<ul>
<li>Now the code is ready, and the 10 epochs should return acceptable result:</li>
</ul>
<pre><code>// train the model
for (int i = 1; i &lt;= iteration; i++)
{
    //get the next minibatch amount of data
    foreach (var miniBatchData in nextBatch(featureSet.train, labelSet.train, batchSize))
    {
        var xValues = Value.CreateBatch&lt;float&gt;(new NDShape(1, inDim), miniBatchData.X, device);
        var yValues = Value.CreateBatch&lt;float&gt;(new NDShape(1, ouDim), miniBatchData.Y, device);

        //Combine variables and data in to Dictionary for the training
        var batchData = new Dictionary&lt;Variable, Value&gt;();
        batchData.Add(feature, xValues);
        batchData.Add(label, yValues);

        //train minibarch data
        trainer.TrainMinibatch(batchData, device);
    }

    if (this.InvokeRequired)
    {
        // Execute the same method, but this time on the GUI thread
        this.Invoke(
            new Action(() =&gt;
            {
                //output training process
                progressReport(trainer, lstmModel.Clone(), i, device);
            }
            ));
    }
    else
    {
        //output training process
        progressReport(trainer, lstmModel.Clone(), i, device);

    }             
}
</code></pre>
<h2 id="modelevaluation">Model Evaluation</h2>
<p>Model evaluation is implemented during the training process. In this way we can see the learning process and how the model is getting better and better.</p>
<p>Fore each minibatch the progress method is called which updates the charts for the training and testing data set.</p>
<pre><code>void progressReport(Trainer trainer, Function model, int iteration, DeviceDescriptor device)
{
    textBox3.Text = iteration.ToString();
    textBox4.Text = trainer.PreviousMinibatchLossAverage().ToString();
    progressBar1.Value = iteration;

    reportOnGraphs(trainer, model, iteration, device);
}

private void reportOnGraphs(Trainer trainer, Function model, int i, DeviceDescriptor device)
{
    currentModelEvaluation(trainer, model, i, device);
    currentModelTest(trainer, model, i, device);
}
</code></pre>
<p>The following picture shows the training process, where the model evaluation is shown simultaneously, for the training and testing data set.<br>
Also the simulation of the Loss value during the training is simulated as well.<br>
<img src="https://bhrnjica.files.wordpress.com/2017/12/2017-12-07_21-15-32.png?w=604" alt="Use CNTK and LSTM in Time Series prediction with .NET and C#"></p>
<p>As can be see the blog post extends the original Tutorial with some handy tricks during the training process. Also this demo is good strarting point for development bether tool for LSTM Time Series training. The full source code of this blog post, which shows much more implementation than presented in the blog post can be found <a href="https://1drv.ms/u/s!AgPZDj-_uxGLhPEhEoRZA722K0cN5g">here</a></p>
</div>]]></content:encoded></item></channel></rss>