So the next step is to use CreateML to train our machine learning model. And in order to do that, 1
we, of course, have to open up our brand-new Xcode 10. And as you can see, at the moment, I'm working with 2
Version 10 beta 3, and this is because I'm recording this ahead of time so that you'll be able to get 3
started using these brand-new tools and brand-new APIs and frameworks as soon as possible. 4
Now, if you're following along with the tutorials before September which is when the grandmaster or the 5
GM version of Xcode 10 gets released, 6
so Apple normally releases the official version sometime around September, late September. And you'll 7
know that it's the official version because you'll be able to download it from the App Store. 8
Now, if you're trying to follow along with the tutorials before you're able to download Xcode 10 off 9
the App Store, then you would have noticed a note in the last lesson where I described how you can get 10
hold of the beta version of Xcode 10 so that you can follow along with me even before the official version 11
gets released 12
out to the public. So as a rule of thumb, if you're doing this tutorial in October, then you should be 13
going to the App Store and downloading the latest version of Xcode 10. 14
If you're doing this before October, then you need to follow the instructions that I've given in order 15
to download the beta version of Xcode 10. 16
So once you have done though, then you should be able to open it up, and you can run Xcode beta alongside 17
with the normal Xcode. 18
So that's not a problem. 19
You don't have to give up one for the other. 20
So, now we're on our familiar welcome screen and we're going to create a new playground. Now whereas usually 21
we've been creating playgrounds for iOS. 22
Now in this case, in order to be able to use CreateML to build our machine learning models, 23
we have to do that using the power of our laptop. 24
So we need to click on the macOS tab to create a brand-new Blank playground for macOS. 25
And so we're going to go ahead and click Next and you can save it anywhere you wish and give it a name. 26
So I'm gonna call my playground ImageRecognitionModelMaker. 27
So once you've created this brand-new macOS playground, the first thing that we're going to do is to 28
make sure that we've enabled manual running of our playground. 29
So you might have seen this in previous lessons. But down here, there is a little play button. And when 30
you press on it, it will execute all the code inside your playground. 31
Now, if you click on it and hold, then you will get to a menu with two options. 32
Now, you can either automatically run your playground so it runs all the code every few seconds or whenever 33
there's changes, or you can select manually run. 34
And this will allow you to specify the parts of your code that you want to run and when you want to 35
run it. 36
Now, when we're creating our machine learning model, we're going to train our data and save it to a location. 37
We don't want that to happen every single time we write a new line of code or every few seconds when 38
it decides to automatically run our code. 39
So we're going to make sure that the Manually \Run part is selected. 40
And when you do that, you'll see this little play button show up inside the left margin. 41
And that means that we can click on it to run, for example, only line 1. 42
If I click on it here or if I click on it here, it'll run lines 1 through to four, and that gives us much 43
more fine-grained control over what happens in our playground. 44
So, now that we've done that, we're just gonna go ahead and delete all of the existing code that's inside our 45
playground, and I'm going to import not just CreateML, but I'm going to import CreateMLUI. 46
And this is a framework that allow us to create a user interface inside playgrounds so that we can vastly 47
simplify the process of creating a image recognition machine learning model down to just writing two 48
lines of code. 49
And let me show you what those two lines are. 50
So the first thing we need to do is to create a new image classifier builder object. 51
So I'm going to use a constant and I'm gonna call it builder and it's going to be set to equal MLImageClassifierBuilder. 52
. 53
And I'm going to initialize it as a new builder object. 54
Now, once I've got that object, then I can simply say builder dot, and I'm gonna call the method called 55
showInLiveView. And that's it. 56
That's all the code that we need to build a image classification user interface that allow us to build 57
our image recognition model. 58
So the next step, once we've written all of that code, is to switch on the assistant editor or the split 59
screen. 60
And this, on the right side, is our live view, as you can see here. 61
And when we click on this button, 62
so the run button, somewhere below the line where we've got our builder.showInLiveView, 63
so we want to run all three lines of code, basically. 64
And if I click it here, then it'll run all the code from line 1 through to 7, so that it can import 65
CreateMLUI. 66
It will create a new instance of MLImageClassifierBuilder and it will run the method to show the 67
builder in the live view. 68
So let's go ahead and give that a spin. 69
Now, depending on the age of your laptop and the amount of rRAM that you have in your laptop, then this 70
can take anywhere between just under a minute to maybe five minutes. 71
But be patient and just let it do its thing. 72
Once it's ready, you'll see that it says, "Ready to continue 73
image recognition model maker." 74
And inside the live view on the right side here, you can see we have this dotted line to allow us to 75
start dropping images to begin training. 76
So this is a good cue for us to go ahead and find our Training Data folder and easiest ways just to 77
have it here on your desktop. 78
And we're going to drag the entire folder of Training Data which, remember, currently, has three subfolders 79
called Cat, Dog, and Rabbit, 80
each containing a number of images that represent those labels. 81
So we're going to take this entire folder called Training Data and we're just going to drop it into 82
this dotted line area. 83
And now you can see something pretty crazy and pretty special goes on in the background, as the machine learning 84
image classifier builder goes through looking at all of these images trying to identify the features 85
that are unique to each folder. 86
What is unique about a rabbit or what is unique about the image of a cat and try to see how it can classify 87
and learn what each of these animals look like. 88
Now, once that is done, you will get a percentage up here and you'll see a success optimal solution found, 89
that means that everything was successful, and we can go about going to the next part. And you can see 90
down here, it says, "Drop images in here to begin testing." 91
So, now this is our cue to grab our Testing Data set which, remember, again, contains folders that have 92
our cat, dog, and rabbit each with around four or five images, around 20 percent of our Training Data 93
set, and we're just going to drag and drop it into this area here. And now our model is going to go through 94
those testing images, 95
use what it's learned from our Training Data to try and classify those images. And you can see if you 96
scroll down over here in the Live View, you can see that the prediction was there should be a cat 97
and it checks the label or the answer, and it says, "I got it right," "I got it right," I got it right." 98
And you can look through it's testing of itself. 99
This is kind of like when you have flashcards and you test yourself on whether if you've got the right 100
answer. 101
That's basically what it's doing here. And in our case, we've got a 100 percent evaluation. 102
So this number is important. 103
Whenever you train your image classifier or any sort of machine learning model, you want to see what 104
was the success rate of when we tested our model against images that it's never seen before. 105
Now, of course, if you show it the same image that you had in the Training Data set, it already knows the 106
answer, 107
so that won't be a test. But if you show it images that it's never seen before, but of the same object 108
and it classifies them correctly, then that gives you a higher evaluation score. 109
Now, if you find that based off the data that you're trying to train your machine learning model on, that 110
your evaluation score or your testing score is quite low, 111
so, say, below 80 percent, or if it varies a lot, 112
so sometimes it's 20 percent, 113
other times, it's 80 percent, then that means that you don't have enough data. 114
So for each of your categories or each of your labels, you need to provide more data until you can reach 115
a higher accuracy. 116
So usually that involves taking the object, say, a cat, and finding images of different types of cats, or 117
cats taken from different angles or with different rotations, and basically providing it with more data, 118
so that it can learn the intricate differences so that you can learn what are the intricate features 119
that makes a cat a cat. 120
So, for example, if you were to train your machine learning model, not just on cards, but on different species 121
of cats, so they can recognize the species, then you'll need to provide a lot more data in order to get 122
it to a satisfactory evaluation score. 123
So something between 80 and 100. 124
So within that short period of time of writing very little code and dragging and dropping some images, 125
we've trained an image classifier that now can recognize a cat, a dog, and a rabbit. 126
So all we have to do now is we can rename our ImageClassifier by double-clicking up here. 127
So I'm going to give it a name that makes sense. 128
So I'm going to call it my PetImageClassifier and you can edit the author or the description, or the 129
license, the version, and also specify where you want to save it. 130
So I'm just gonna save it inside my desktop. 131
And I'm going to click Save. 132
So, now if we head over to our desktop, then you can see we have our brand-new .mlmodel file that 133
is our pet image classifier. And that's all that we have to do in order to do something that used to 134
be so complex that we need to rely on IBM to create it for us, 135
and we've just done it using our own laptop and only two lines of code. 136
So that is so incredibly cool. 137
And in the next lesson, we're going to be using this pet image classifier to build our own image recognition 138
app using our own data and our own model. 139
So for all of that and more, I'll see you on the next lesson.