The course is part of this learning path
This course focuses on machine learning. We're going to discuss what machine learning is and how we can leverage it to create intelligent and engaging apps. You'll also follow along as we create an image recognition app.
Hi. Within this lecture, we're going to create our request to get a hold on our machine learning model. Remember, we have two steps. First, we have to create the request. Second, we have to create the handler to handle this request. So, we're going to do the first step in this lecture. And remember, we can get this from the documentation, but I'm going to simplify the code so that you can understand it better.
So, we're going to work with something called VNCoreML model. So, as you can see, this is a Core ML model used with vision request. So, this is Vision request Core ML Model. And if you open parentheses, it will ask you for a model and it throws some errors. So, for model, we already have our model integrated in our project. Right? So, if you say something like, if let model try, we're going to try because it throws an error. And in here, let me open and close parentheses,
and rather than ML model, so this won't work, just delete this. Rather than ML model, we're going to give our MobileNetV2 model. So, we already have our model in here, so you can just write MobileNetV2. If it doesn't auto complete, you can just write whatever you see in your model name and you have to convert this to a model object like you can just say MobileNetV2 open parentheses and dot model. So, this is how you actually define your model as a variable inside your project.
So, if you make this optional, I believe the error will go away. Yes. Now, this is good. Now, this means that we get to create our model inside of a variable. Now, I can actually use this model in my coat. The next step would be for me to create the request variable itself. So, if you say let request, and this is going to be a VNCore ML request. So, this one. So, that's what we have been looking for, the first step creating the request and this will give us the completion handler in return of a model.
So, we already have our model so that we can use in this request function, and this will give us a completion handler. Remember, this is a closure and in this closure we get two outputs like VN request and the error. And the actual request that we have been looking for is this one. So, we're going to use this VN request if there is no error. So, I'm going to call this VN request and I'm going to call this error.
Now, I'm going to use this VN request as I set and I have to understand what it does. So, if you come over your documentation, you can try to see what you can do with this VN request. But again, this is complicated in this documentation, so let me see how this works. If you say VN request in here, you can see the type, this is a VN request indeed. And if you say dot results, so we are looking for results, it will give you any array. So, not pictionary. It just gives you an array.
So, this is actually an array of the observations, array of the assumptions that you're going to get out of this model. So, this is given as an any array and we're going to have to convert this any array into something that is compatible with the Core ML model so that we can get the observations, we can get the confidence level, for example. And by confidence level, I mean the probability like 60%. This is monkey, 90%. This is another animal. I don't know.
So, let me show you how this works. You're going to have to create these results in a variable and you can just say VNRequest.results and cast this optionally as a VN classification observation. So, this is produced by the image analysis. So, we're getting the result of the image analysis from the model at this point. So, that's it. That's what we have been looking for.
Now, we're going to process this results so that we can show them to the user. So, there will be more than one observations in this array. And I'm going to try to get the first result, the top result. And in order to do that, I'm going to say something like, let top results and this is going to be derived from results.first. And the reason that I'm doing this actually is to get the most probable observation. And in fact, before we do that, let me check to see if results.count is actually greater than zero.
So, if I have any observation, then I want to get the first one because this is going to be the most probable one. For example, if I show a monkey picture, I can get the monkey as the first result, but elephant as a second result. And I want to get the most probable one to show them to the user. Of course, you can try to find the other results by adding a for loop in here, but it won't make sense.
So, I believe the best course of action here is to get the first result, the most probable result out of that observations and show it to the user. And this is going to be the result that we want to process and then add to our result label, in fact. So, this is the end of our request, but not completely. After we complete our request of course, we're going to go into the handler. And in this request, I'm going to use dispatchq.mean.sync one more time as we did before because we're going to do this asynchronously and we're going to change the main user interface in here like we're going to change the labels. And we're doing this all in background so that we want be blocking the user interface.
So, we have done this before. Right? You can come over here and say something like self.resultlabel.text is topresult.identifier. So, this will worked asynchronously and it won't block the user interface and it will show the user the top result identifier meaning the top result text. But I don't want to show only the identifier, but I'm going to get the confidence level or confidence index as well.
So confidence level, again, the probability. So, this is something like 60%, 80%. And if you say the topresult.confidence, this gives you the confidence level, but unfortunately, we get this in a number between 0 and 1. So, we have to just convert this to be an integer and, or not an integer actually, but we can actually multiply this by 100 in order to have a meaningful value something like 21% rather than 0.021. So, if you do it like this, then you're going to get your confidence level. And then I believe we have to provide some default value because this is optional. And yes, let me do that, let me just put two questions mark. And if this is nil or if this is empty, I'm going to show zero confidence level to the user. And there will be more than one decimal, so I believe we have to round this up, but you won't understand why I am doing this right now.
So, let me just use this confidence level, so that you will understand it better later on when we do that. So, rather than just showing the identifier, I'm going to say something like this is 90% monkey. So, let's see, confidence level in here, percent, and it is something like monkey. So, 90%, it is monkey. So, all you have to say confidence level percent, it is topresult.identifier.
And let's see, yes, I misspelled confidence level so this is not conf but rather this is confidence level. So, here you go. That's actually going to get this results and going to display the result to the user. And in order to make this work, as you can see, we get this error in here saying that you created this request but you have never used it. And it's correct because I don't have my handler yet. So, the next step would be to go to the second step and create the handler to fire up this request. That's what we're going to do within the next lecture.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.