Everybody what's going on this is Caleb with Deb slopes and in this video we're going to go ahead and
add in our core a male model that we created when to pull that over here.
And then what we're going to do is we're going to create some functions to engage with it pass in data
and then make a prediction and return that information we're going to actually process the requests
that we make.
So to begin let's take our M-L model file and we're going to just drop it into the animal classifier
App project.
And when you're done make sure that copy items if needed is ticked and click Finish.
And as you can see when we import it we get all that information that we created.
We see the input data and the output data.
We have a label for what the category of the image is.
And we also have a number of the probability of that category.
Now as you can see the model class here isn't automatically generated swift model class for our animal
classifier.
And as you can see X code does all of this work for us and basically converts it into a model.
It has all of the prediction functions.
Everything we need which is very cool.
So it does that for us right out of the box.
And so let's go ahead and let's create what is called a V.N. core M-L request.
And in order to actually do that we need to first import Corum L and we need to import vision.
Vision is the image processing framework from Apple and it's a part of core MLM we're going to need
it to actually make our classifications.
Now when I was thinking I realized we're not doing any custom stuff in view ad loads we can actually
get rid of that.
And with that in mind we can create our V.N. core M-L request right now.
So we're going to use what's called a lazy variable and a lazy variable is just what it sounds like
it's a variable that is only created or the memory for it is only allocated when it's actually needed.
So we can type lazy var and we'll just call this classification request and that's going to be of the
and core M-L request.
All righty.
Now the cool thing is with a lazy variable we can initialize it like so and then using curly brackets
we can set it up exactly how we want but it won't actually take up any memory or do anything computationally
until we actually use this variable somewhere else.
So what we are going to do is we're going to use a do try and catch block in order to perform these
tasks for core M-L you know classification and then use a catch block to catch our error.
So we're going to go ahead and type do.
And then down below we're going to add catch and that's where we're going to catch any errors that we
do have and let's just go ahead and use fatal error now to do that.
We're going to say fails to load vision animal model and for some more specific information we can actually
pass in the error here that is thrown.
So that is what we're going to do in order to talk about what actually happened.
And in order to actually throw an error we need to actually try something.
So to do that we're going to go ahead and essentially initialize our model and to do that we're going
to type let modeled equals.
And this is where the tri part comes in and it's going to basically say we're going to make a V and
core a male model instance.
OK.
And when we actually try to initialize this you can see that it throws and that's why we need to use
try and that's why we need to catch the error in case something goes wrong.
So the M-L model you might be wondering Well we're creating a model why are we passing in a model.
Well we need to pass in the model we generated and to do that.
Remember it auto generates an animal classifier swift class so we can type animal classifier and check
this out.
We can initialize it and then pull out the model property.
So we now have a swift instance of our Cornell model that we created which is so so cool.
So now assuming that worked properly what we're going to do is we're going to create what's called a
V.N. core a mail request.
So let's type let request and that's going to be equal to a v n core M-L request.
And if we try to initialize this you'll see there is an option with only a model and there's an option
with a completion handler and that's that's the one that we want.
Because that's how we're actually going to process all of the classification requests that we pass in.
So we need to pass in a model and that's the one we just made.
So go ahead and type model past that guy in there.
And then we're going to go ahead and push enter and we're going to essentially get a request and an
optional error.
OK.
And so we're going to go ahead and type request and error.
Those are the names of the parameters for the data that gets passed to the completion handler.
Now inside the actual closure here this is where we are going to process all of the classifications
and basically we're going to pass the request in and then we're going to essentially run through all
those classifications pull out the labels pull out the probability and handle all of the data that does
come back.
And that's the next function that we're going to create.
But for now assuming that the request is successful we need to actually tell it something very important
about our image.
Now if you pull in an image from your phone it is likely not a square but if you look our core a male
model accepts an input of 299 by 299 That's a square image.
So what we're going to do is we're going to go ahead and call request dot and we can choose image crop
and scale option.
And if we go ahead and look at our options here there are quite a few that will pop up.
But the one that's most important here is center Kropp.
So it's going to essentially crop the image in the middle into a square which is pretty cool.
So now once we have set everything up we have processed all of our results.
We can then return our request and that as soon as we use this it's going to run through all of this
and then return the request to this parameter for us to use.
So at this point we need to process our classification.
Okay.
And so to do that let's go ahead and just write a function down beneath our lazy variable and that's
going to be called process clarification and that's going to be called process classifications so phunk
process classifications for request of type Vienne request.
And we're also going to need to include an error here of type error.
However it is optional because you know there's not always going to be an error.
It is an optional parameter and hopefully we won't need to use it.
So we are going to go ahead and now create a parameter called classifications and it's actually going
to pull the results variable out of our request.
So we're going to go ahead and use guard let classifications equals request dot results.
OK.
That's the results that come back as the type V.N. observation.
Request dot results and we're going to cast these results in an array.
OK.
And the array the type that these need to be are V.N..
Cl. observation.
Now it's classification information produced by image analysis request which is perfect for what we're
doing.
But if for some reason we can't do this we need to fall back on our else block here and we're going
to go ahead and set the classification levels text parameter to say unable to classify image.
And then we're going to go ahead and use our little you know return line syntax here and we're going
to pass in a parameter into this string encapsulation syntax.
We're going to pass in the error already but the localized description of that error and in the instance
that there's not actually an error.
We'll just go ahead and pass in error.
Looks like.
So that looks pretty good.
OK.
So if we can't actually classify the image it'll say unable to classify image then it will return a
line and then it will spit out the error code.
Otherwise it'll just say error which that's you know a good amount of context information for our user.
Then of course we will return.
So that it stops doing what it's doing and cancels out of this function.
But let's say that we are successful and do in fact get some results back for our classifications what
we're going to check first to see is if it is empty if 0 classifications came back.
So if classifications classifications are it is dot is empty.
We're going to say hey self-caused classification level text We're gonna set it to say nothing recognized
that didn't recognize a single thing.
We'll tell it to tell us otherwise.
If it did recognize things and classifications came back we're going to go ahead and essentially pull
out the top two classifications and then we're going to map over them and return their percentage of
confidence as well as what the model thinks that the image is.
So let's go ahead and get the top two parameters by typing let top classifications
equals classifications dot prefix.
This is a really cool way to slice an array.
We're going to do prefix and then two.
And what that's going to do is it's going to cut off the top two items in an array and return them as
a new array.
So we have now an array of the top two classifications.
Now we need to go ahead and set up the descriptions.
OK.
And to do that we're going to basically map over the two items in here.
We're going to pull out their confidence and convert it to a percentage then we're going to pull out
the identifier which is the actual name of the animal.
So let description's equals top classifications dot map.
And we're going to map over each classification like so.
And what we're going to do is we're going to essentially return a property to this classification which
will then get returned back to descriptions.
But we're going to use a string and we're going to use some special string formatting here to convert
it into a percentage.
So for the actual format this string is going to look super weird but basically we're going to return
percentage sign dot to f.
And what this says the percentage indicator basically just indicates how a certain chunk of data should
be used.
Point 2 means after the decimal place we're going to allow two numbers after the decimal and it's going
to be a floating point type number.
OK.
Now we can get rid of this argument's parameter.
We don't actually need it but we're going to go ahead and pass in the pieces of data that are most important
to us.
So go ahead and pass in the classification remember we're mapping through so each classification has
you know its own variable.
And we're going to pull out a parameter called V confidence.
And the confidence it might not load here but it is the level of confidence in the observations accuracy
from zero to 100 percent.
But the way that it currently comes in is a decimal.
So for 75 percent it would be 0.75.
But instead I want it to show seventy five point to 5 percent like an actual how you would actually
see it written out.
So I'm going to basically take the confidence and multiply it by 100 to move the decimal over two spots.
Then I'm going to go ahead and add the percentage sign and a dash and another space.
And what this is going to do is it might say 60 percent dash and now is where we need to actually add
the identifier Maybe cat maybe Bird.
OK.
So now type classification dot identifier.
And as you can see the identifier is the classification label for the type of observation.
And so now we have an array of our descriptions in K and what we're going to do is we're going to essentially
take all of those descriptions we're going to join them with a line break and print them out on our
label.
So let's go ahead and go down below our descriptions parameter here and type self dot classification
label dot text and we're going to set that to be equal to classifications at the top with a colon and
our line break syntax.
Then we're going to go ahead and put an end quote there and we're going to add descriptions joined and
our separator here is important we need to separate each item in the descriptions with that line break
syntax just like so.
And at this point the classification label can successfully print out not only classifications at the
top but also all of the guesses that it has for what the item in the picture is.
OK so that's pretty cool.
That's a good way that we can do this so we're now ready to process our classifications in our model
here.
So let's actually use that we can pass in self datt process
classifications for will pass on our request and then error and we'll pass in our error parameter from
a peer.
So we're passing request and error because they're optional.
Shows up as blue the the auto complete was not working but at this point we should be able to build
this and get some requests back however I don't know about you but I want to try this.
So let's build and run pull open our simulator here and if all goes well we should be able to select
an image and then run our classification on it.
OK.
So let's go ahead and try this out.
We will select an image from our photo library and nothing happens.
And that is because we have not yet set it to update our UI but I want to see if this actually is working.
So we're going to head over to the next video where we're going to write our update classifications
function and actually get the information showing on the screen for the classifications that we're trying
to make.
So let's head over there now and let's do that.
I'll see there.