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