1 00:00:00,300 --> 00:00:07,860 In this lesson we're gonna be learning how to make predictions on individual images using Charisse after 2 00:00:07,860 --> 00:00:10,720 our model has been trained. 3 00:00:10,800 --> 00:00:15,920 I'll add a markdown cell here to create a subsection for us. 4 00:00:16,150 --> 00:00:22,830 And now what I'll do is I'll quickly pull up the documentation from Caris to see how it is that we can 5 00:00:22,830 --> 00:00:24,500 make a prediction. 6 00:00:24,600 --> 00:00:31,230 So scrolling down on this page here the model functional API we had our compile method we had our fit 7 00:00:31,230 --> 00:00:38,610 method and then down here we have evaluate which will we'll use later on and here predict this seems 8 00:00:38,610 --> 00:00:39,620 what we're looking for right. 9 00:00:40,710 --> 00:00:45,260 This method will generate output predictions for the input samples. 10 00:00:45,570 --> 00:00:52,990 And if you give it a lot of input samples then it will do the computation in batches as an output. 11 00:00:53,010 --> 00:00:56,970 This method will give us a name pi array of predictions. 12 00:00:57,630 --> 00:00:59,190 So let's try this out. 13 00:00:59,190 --> 00:01:02,310 Let's try and predict a single image. 14 00:01:02,310 --> 00:01:09,450 I want to use my validation data set him X Val and I'll quickly remind ourselves of the shape of the 15 00:01:09,450 --> 00:01:11,040 validation data set. 16 00:01:11,220 --> 00:01:18,810 It had 10000 images and each image had three thousand seventy two values in it that corresponded to 17 00:01:18,810 --> 00:01:20,560 the individual pixels. 18 00:01:20,610 --> 00:01:27,630 So that means if I wanted to pull out the very first image in the validation data set then I could do 19 00:01:27,630 --> 00:01:31,920 so with square brackets and then zero. 20 00:01:31,920 --> 00:01:36,140 This here will give us the very first image all right. 21 00:01:36,150 --> 00:01:41,150 So now that we've got a single image let's take a look at what the shape of this image is. 22 00:01:41,310 --> 00:01:44,310 And here you can see that this is actually flattened right. 23 00:01:44,310 --> 00:01:46,560 It's a one dimensional array. 24 00:01:46,770 --> 00:01:54,960 If we want to make this a two dimensional right then we could use num PI's capabilities and use N.P. 25 00:01:55,050 --> 00:01:57,810 dot expand after school. 26 00:01:57,910 --> 00:02:00,050 DRM is four dimensions. 27 00:02:00,420 --> 00:02:08,910 So here we could supply our X Vale square brackets zero and then we can say it should add another dimension 28 00:02:08,910 --> 00:02:16,470 here with access equals zero shift tab on my keyboard and bring up the quick documentation then I can 29 00:02:16,470 --> 00:02:25,130 see that axis is an integer and it's the position in the expanded axis where the new axis is placed. 30 00:02:25,420 --> 00:02:32,850 I want to store this whole thing in a variable called test and I'm very quickly going to take a look 31 00:02:32,850 --> 00:02:36,300 at what the shape of test now is. 32 00:02:36,300 --> 00:02:42,120 And here you can see that all we've done is add an extra dimension here so that we can now feed this 33 00:02:42,120 --> 00:02:43,920 into our predict method. 34 00:02:43,920 --> 00:02:44,640 Why is that. 35 00:02:45,030 --> 00:02:49,610 Well it's because it actually usually expects more than one prediction. 36 00:02:49,610 --> 00:02:56,100 It will want to make predictions on a number of samples here's how we would use the predict method. 37 00:02:56,100 --> 00:03:00,050 I want to pick model number to model on a score to predict. 38 00:03:00,520 --> 00:03:04,100 And if I wanted to make a predict on my test image. 39 00:03:04,320 --> 00:03:06,270 So that's our argument. 40 00:03:06,270 --> 00:03:08,610 Let's take a look at what the output is here. 41 00:03:09,060 --> 00:03:12,480 What we get here are actually our probabilities. 42 00:03:12,480 --> 00:03:14,310 All of these should sum to 1. 43 00:03:15,660 --> 00:03:20,990 So if I put that sum afterwards then I can prove this to you. 44 00:03:21,000 --> 00:03:25,660 So these here are the probabilities for each of the different categories. 45 00:03:25,740 --> 00:03:32,190 Each of the different classes for this image the reason we've got these nice probabilities is because 46 00:03:32,250 --> 00:03:38,360 we've used soft Max in our output layer for our activation function right now. 47 00:03:38,550 --> 00:03:44,880 I think this is some really ugly formatting and I think we could do a little better than this and the 48 00:03:44,880 --> 00:03:50,970 way we're going to change the way these numbers are formatted in our Jupiter notebook is to change the 49 00:03:51,270 --> 00:03:53,970 print options for num pi. 50 00:03:54,030 --> 00:04:01,860 So with MP dot set on a scope print options we can actually say how precise how many decimal points 51 00:04:02,010 --> 00:04:07,580 we want to put a notebook to print out so we can do that by setting the precision. 52 00:04:07,710 --> 00:04:15,450 And if I said that 2 3 and hit shift enter on this and hit shift enter again on my prediction then you 53 00:04:15,450 --> 00:04:20,780 can see that I'm only printing the probability up to three decimal points now. 54 00:04:20,790 --> 00:04:24,360 What if we wanted to predict more than one image. 55 00:04:24,480 --> 00:04:31,500 Well in this case we use our model model number two and then as an argument to the predict method we 56 00:04:31,500 --> 00:04:35,580 can actually supply the entire evaluation dataset. 57 00:04:35,580 --> 00:04:41,640 In this case what we'd get is an enormous array with all the predictions. 58 00:04:41,670 --> 00:04:43,050 Let me show you what I mean. 59 00:04:43,170 --> 00:04:47,970 If I had shift enter on the cell then we get something like this. 60 00:04:48,190 --> 00:04:53,980 These probabilities are in scientific notation which is why you see this e afterwards. 61 00:04:53,980 --> 00:05:01,540 But if I take a look at the shape of this array then what you'll see is I get 10 predictions or 10 probabilities 62 00:05:01,840 --> 00:05:09,040 for each and every single of the 10000 images in our valuation dataset. 63 00:05:09,040 --> 00:05:12,720 Now what if we're just interested in the predicted class. 64 00:05:12,730 --> 00:05:16,120 What if we're just interested in the largest number of these 10. 65 00:05:17,260 --> 00:05:21,050 Well of course there's a handy method from Caris once again. 66 00:05:21,220 --> 00:05:30,830 So model underscored to dot predict underscore classes and then test will give us the predicted class. 67 00:05:30,850 --> 00:05:34,800 So in this case it will predict class number six. 68 00:05:34,840 --> 00:05:38,090 Now of course the question is did it get it right. 69 00:05:38,350 --> 00:05:42,900 The place we would look for that of course is why on the score Val. 70 00:05:43,000 --> 00:05:50,230 So the labels for evaluation valuation dataset y underscore Val square bracket 0 will give us the actual 71 00:05:50,230 --> 00:05:54,170 class the actual category that this belongs to. 72 00:05:54,250 --> 00:06:02,160 In this case our model predicted the correct class for the very first image in the validation data set. 73 00:06:02,250 --> 00:06:10,210 Now as a challenge can you write a for loop where you print out the actual value and the predicted value 74 00:06:10,510 --> 00:06:14,760 for the first 10 images in the validation dataset. 75 00:06:14,880 --> 00:06:19,620 You will get bonus points if you format this nicely and use an F string. 76 00:06:19,630 --> 00:06:26,940 I'll give you a few seconds to pause the video and give this a go. 77 00:06:26,990 --> 00:06:27,800 Ready. 78 00:06:27,800 --> 00:06:29,360 Here's the solution. 79 00:06:29,480 --> 00:06:36,040 It's gonna be four and then let's say number in range 10. 80 00:06:36,350 --> 00:06:40,700 And here what I'll do is I'll say test on a score. 81 00:06:40,700 --> 00:06:44,600 Image is equal to number pi. 82 00:06:44,780 --> 00:06:53,350 Expand dimensions parentheses x on a score Val square brackets No. 83 00:06:53,630 --> 00:06:59,960 So as I'm iterating through my loop I'm going to pull out the corresponding image from my validation 84 00:06:59,960 --> 00:07:01,410 data set. 85 00:07:01,430 --> 00:07:05,990 Put a comma C axis is equal to zero. 86 00:07:06,470 --> 00:07:18,820 Then I'll say predicted underscore Val is equal to model on a score to don't predict classes. 87 00:07:19,040 --> 00:07:20,870 Test image. 88 00:07:21,650 --> 00:07:28,490 And since the output from predict classes is an array I'm going to pick out the first element here with 89 00:07:28,580 --> 00:07:38,540 square brackets zero and now I'll write my print statement using an F string or I'll see actual value. 90 00:07:38,540 --> 00:07:45,650 Curly braces y on the score bough square brackets No. 91 00:07:46,280 --> 00:07:57,930 And since this is also an array I'll add another pair of square brackets here put a 0 c versus predicted. 92 00:07:58,040 --> 00:08:03,510 Curly braces predicted underscore Val. 93 00:08:03,590 --> 00:08:04,890 There we go. 94 00:08:04,910 --> 00:08:07,130 Let's see if I got this right. 95 00:08:07,130 --> 00:08:14,060 Here we are and what we should see is that our model got approximately 50 percent of the predictions 96 00:08:14,090 --> 00:08:15,680 correct. 97 00:08:15,680 --> 00:08:20,060 We've already seen that the first image in the valuation data set was predicted correctly. 98 00:08:20,060 --> 00:08:21,470 But the second one I got wrong. 99 00:08:21,470 --> 00:08:22,990 The third one to get right. 100 00:08:23,120 --> 00:08:24,530 Fourth one I got wrong. 101 00:08:24,530 --> 00:08:25,580 Fifth one. 102 00:08:25,580 --> 00:08:27,980 Wrong again and so on. 103 00:08:27,980 --> 00:08:34,730 So that's how you would use the predict method and the predict class's method from Caris. 104 00:08:34,940 --> 00:08:43,880 You can use these methods on individual items or you can use these methods on entire arrays in the next 105 00:08:43,880 --> 00:08:44,430 lesson. 106 00:08:44,450 --> 00:08:48,710 We're going to be evaluating our favorite model and a bit more detail. 107 00:08:48,830 --> 00:08:56,390 We're gonna be judging it based on its accuracy its precision its recall and its F school for all of 108 00:08:56,390 --> 00:08:57,180 that and more. 109 00:08:57,230 --> 00:08:58,880 I'll see you in the next lesson. 110 00:08:58,880 --> 00:08:59,450 Take care.