From the course: Machine Learning with ML.NET

Deep learning with ML.NET: Image classification - .NET Tutorial

From the course: Machine Learning with ML.NET

Deep learning with ML.NET: Image classification

- In this video, we will take a look at the deep learning support in ML.NET by building an image classification model. Deep learning with ML.NET has enabled developers to use machine learning across a wide variety of domains, all the way from sound, vision, text, NLP, which is natural language processing, and a lot more. So now you can use deep learning to classify or understand sounds. You can classify music, you can predict music or music notes. You can understand vision, so you can classify images or you can detect objects. That is going to make online cataloging or inventory management much more easier. It can also understand text, so you can do semantic analysis, or you can do entity recognition, or you can do sentiment analysis, or you can do price prediction, or you can classify documents. And you can also teach machines to learn, as well. So deep learning has opened up a large set of scenarios that were not possible before. Image classification is a very popular example of deep learning. Simply put, image classification allows you to classify images into one or more category. In this example, what we are doing is, we are classifying images into whether the image is a flower, and we're categorizing the type of flower, whether it's a daisy, a rose, or a tulip. And the way to build a model with deep learning is around, you have your input data, which is your training data, which has lots of images, which we have labeled in folders. And we feed all these images into a training process, which is going to train a custom model for you that you can use to predict what category of flower does a particular image belong to. So let's see an example of how can we build a custom model for image classification. This is a console app where I'll build this machine learning model, or this deep learning model. What I have over here is a data set where I have went ahead and captured a bunch of flower images, and I've categorized them into each flower type. So here, it's a folder of daisy, which has all pictures of daisy flower. Here's a picture of dandelion, which has all pictures of dandelion. Here's a picture of a folder of rose, which has all the roses pictures. And what we're going to do in this example is, we're going to train the model on this dataset, so it's going to understand all characteristics of what makes this flower a daisy flower or not, and then we're going to use the training model on entirely new data to predict what's going to be the flower type. As we have learned in the ML.NET journey, the first step is to create an MLContext, so you can create a context. The second step is around loading the images. So here, I'm loading all the images that I just showed you from this flower photos folder. The next step is around choosing your algorithm and sort of specifying your label column, or the column to predict. And in this case, what I'm doing is, the label key is basically the image path, or the folder names. And then the model I'm going to choose is a Resnet model. So in this case, I'm doing transfer learning, where I'm using one of these prebuilt models from the ecosystem to build a model for my own scenario. And once I have defined this pipeline, I can go ahead and call pipeline.Fit, which is going to train this model, and then I can evaluate the model by printing out the metrics of what was the accuracy of the model like. The last step is around trying the model, which is creating a prediction engine, and then calling the predict function on one of the images that I have in my image for prediction folder. So let's go ahead and run this app, and then see how the model was trained and how does it perform on my test dataset, as well. So let's go ahead and start this application. And so, what's happening behind the scenes in the training processes is, this program is taking all the images and it's extracting all the features from all the images, and then it's going to run a classification algorithm at the end of the process to classify all these images into one of the many categories. And so what has happened so far is, now the training process is about to finish, and we get some output metrics over here where the accuracy of the model was around 83%. It's a helpful guide that the closer to one it is, the better it is. And what you're seeing over here is, the model was run on certain, some images. In this case, the model was also run on this rare flower picture, which looks like a rose, over here. And then the output of the model or the prediction was a label of rose with a confidence level or accuracy level or 62% that it's a rose. So what we defined over here was a program to use deep learning to train an image classification model. This concludes our series on ML.NET. I hope you'll learn a lot about machine learning and are excited to use machine learning in your apps. Be sure to check out our other series on csharp.net, asp.net, and more.

Contents