1 00:00:00,510 --> 00:00:03,120 Now we've seen what an image looks like intensive form. 2 00:00:03,360 --> 00:00:07,990 Let's build this little function that's gonna do these steps here. 3 00:00:08,040 --> 00:00:13,940 So what we might do is just copy them so we can see them down below. 4 00:00:14,020 --> 00:00:18,480 Am I gonna just write a little sell here. 5 00:00:18,480 --> 00:00:34,640 Now we've seen what an image looks like as a tensor let's make a function to pre process them already. 6 00:00:34,740 --> 00:00:42,610 And so what we might do is define a variable here image size to 24 and you might be wondering like I 7 00:00:42,610 --> 00:00:48,790 always do when I see things like this that aren't explained what is 224 whether Look get that will we're 8 00:00:48,790 --> 00:00:54,440 going to see that in a later video when we pick out a machine learning model that we can use. 9 00:00:54,550 --> 00:01:00,130 And so a lot of machine learning is lining up the size of your inputs or the shape of your inputs in 10 00:01:00,130 --> 00:01:03,880 our case images with the model that you're using. 11 00:01:03,880 --> 00:01:09,730 Again we'll cover this later on but essentially we're making sure that our images that we feed into 12 00:01:09,730 --> 00:01:17,620 a machine learning model are of this shape so to 24 high to 24 wide because that is what the machine 13 00:01:17,620 --> 00:01:22,080 learning model that we're going to use which trained on images in that same shape. 14 00:01:22,090 --> 00:01:28,810 Remember how it's important to feed data in the same format and shape to your machine learning model 15 00:01:29,170 --> 00:01:31,830 as it was trained on when you're trying to make predictions. 16 00:01:31,900 --> 00:01:33,600 But we'll see that in practice later on. 17 00:01:34,330 --> 00:01:35,620 But let's write this function. 18 00:01:35,620 --> 00:01:41,850 So create a function for pre processing images. 19 00:01:42,050 --> 00:01:46,950 So I call this nice and simple process image. 20 00:01:46,960 --> 00:01:51,730 It's going to take an image path as input and 21 00:01:54,430 --> 00:02:06,340 little doctoring could be takes an image file path and turns the image into a tensor something simple 22 00:02:06,340 --> 00:02:12,070 like that we could make it a little bit more intuitive later on but to begin with that's enough to explain 23 00:02:12,190 --> 00:02:13,680 it for ourselves. 24 00:02:13,720 --> 00:02:21,120 So we want to read in an image file and to do that we'll save it to a variable called image. 25 00:02:21,490 --> 00:02:27,790 And if we read through loading images that you can do this for other types of data to write there's 26 00:02:27,790 --> 00:02:29,000 text data there. 27 00:02:29,050 --> 00:02:35,740 This pan this data frame num pi CSA but reading through there we see that we can go t after Io for input 28 00:02:35,740 --> 00:02:38,130 output read file. 29 00:02:38,260 --> 00:02:39,850 There we go. 30 00:02:39,970 --> 00:02:40,750 What's this say. 31 00:02:40,870 --> 00:02:43,900 Reads and outputs the entire contents of the input file name. 32 00:02:43,990 --> 00:02:45,170 Well that makes sense. 33 00:02:45,310 --> 00:02:48,040 Returns a tensor of type String. 34 00:02:48,040 --> 00:02:49,270 Beautiful. 35 00:02:49,420 --> 00:02:52,600 Let's do image path but don't take my word for it. 36 00:02:52,810 --> 00:02:56,800 What we'll do is and I want you to adopt this practice too. 37 00:02:57,280 --> 00:03:01,430 If you ever see a function that you're not entirely sure of what it does. 38 00:03:01,570 --> 00:03:04,880 Rewrite it line by line so let's check this out. 39 00:03:05,170 --> 00:03:15,220 We'll go tensor equals t after Io read read file and then we need an image path. 40 00:03:15,220 --> 00:03:17,070 So what do file names. 41 00:03:17,170 --> 00:03:23,100 I'm 26 so we'll get file names 26 and what's that look like 42 00:03:27,070 --> 00:03:27,550 all. 43 00:03:28,280 --> 00:03:35,880 So it's shape of a string and then a whole bunch of characters I don't understand but that's an alright 44 00:03:35,930 --> 00:03:38,800 thing because the computer understands them better than I do. 45 00:03:38,810 --> 00:03:47,570 So what it's done is it just taken the file name and turned the contents into this long tensor. 46 00:03:47,960 --> 00:03:48,880 Wonderful. 47 00:03:48,920 --> 00:03:49,900 So the next step. 48 00:03:50,080 --> 00:03:52,940 Use tens flow to read the file save it to image. 49 00:03:52,940 --> 00:03:54,100 Yep. 50 00:03:54,190 --> 00:03:56,820 To now image a j peg into tenses. 51 00:03:56,840 --> 00:03:57,320 Okay. 52 00:03:57,470 --> 00:03:59,120 So let's do that. 53 00:03:59,120 --> 00:04:13,360 Turn the j peg image into numerical tensor with three color channels Red Rain blue 54 00:04:16,450 --> 00:04:23,260 and again checking out the loading images documentation so we're reusing this variable here. 55 00:04:23,290 --> 00:04:32,220 Now we're going to reset it to B T after image dot decode see how we've got some options here we go 56 00:04:32,270 --> 00:04:39,100 to PJ which is the type of image file we're gonna use J peg because our images are in j peg image and 57 00:04:39,100 --> 00:04:46,220 then we pass it the variable channels the outer channels indicates the desired number of coloured channels 58 00:04:46,220 --> 00:04:54,090 for the decoded image in our case we want three for red green on blue beautiful. 59 00:04:54,320 --> 00:05:02,850 Now again if we wanted to check out exactly what this is doing let's take tensor and go after the image 60 00:05:03,310 --> 00:05:10,410 that decode j peg we'll take tensor and then we'll go channels equals three 61 00:05:13,800 --> 00:05:23,140 Oh look at that you've got exactly the same sort of data as that we have up here wonderful right now 62 00:05:23,140 --> 00:05:27,640 to save in the interest of time we're going to write a couple more lines in this function but we won't 63 00:05:27,700 --> 00:05:31,600 go through these similar steps that's what I want I want you to have some practice doing. 64 00:05:31,600 --> 00:05:37,480 If you do see a function and you're curious to what it does try writing it out line by line and see 65 00:05:37,480 --> 00:05:39,150 what happens. 66 00:05:39,190 --> 00:05:41,680 So you're just gonna have to trust me with what these lines do. 67 00:05:42,100 --> 00:05:50,300 So convert the color channel values so this is called normalization I'll write the code first and then 68 00:05:50,300 --> 00:05:58,580 tell you about it from 0 to 255 to 0 2 1 values. 69 00:05:58,580 --> 00:06:10,140 So we want to go image equals t after image convert image D type image TAF float 32. 70 00:06:10,160 --> 00:06:13,340 Now what's the doctoring say here. 71 00:06:13,550 --> 00:06:17,930 Convert image to day type scaling its values if needed. 72 00:06:17,930 --> 00:06:24,050 Images that are represented using floating point values are expected to have values in the range 0 1. 73 00:06:24,050 --> 00:06:25,210 Wonderful. 74 00:06:25,220 --> 00:06:27,980 Now you might be thinking how how do you know this. 75 00:06:27,980 --> 00:06:36,430 Daniel how do you know that you have to convert it from 0 to 255 to between 0 1 Well actually I lied. 76 00:06:36,670 --> 00:06:39,580 I said we weren't going to look at that but this is a good one to investigate 77 00:06:42,100 --> 00:06:51,970 tensor just so you can see we've got values there between 0 and 255 and now I'll run this Oh no I don't 78 00:06:51,970 --> 00:07:01,900 actually want that messing myself up here rerun this rerun this so just when I demonstrate what this 79 00:07:01,900 --> 00:07:05,580 is doing so image convert. 80 00:07:06,000 --> 00:07:12,660 So basically all it's doing is it converts our values. 81 00:07:12,660 --> 00:07:15,640 It scales them instead of being between 0 and 255. 82 00:07:15,690 --> 00:07:20,490 They now are between 0 and 1 with the same scale float 32 83 00:07:26,340 --> 00:07:27,650 TFT float 32 84 00:07:37,650 --> 00:07:38,430 wonderful. 85 00:07:38,430 --> 00:07:39,890 So we've got the same shape. 86 00:07:39,960 --> 00:07:46,920 But now instead of being between zero activity five between zero one and now this type of process makes 87 00:07:46,920 --> 00:07:49,350 computing on these more efficient. 88 00:07:49,500 --> 00:07:52,190 That's the most you need to know it's called normalization. 89 00:07:52,200 --> 00:07:56,980 And again this is just straight from the loading images documentation. 90 00:07:56,990 --> 00:07:58,750 Now what do we have to do. 91 00:07:58,760 --> 00:08:09,060 Actually I don't think we had a step here for that we might have to put in for normalize our image convert 92 00:08:10,480 --> 00:08:17,550 color channel values from 0 to 255 to 0 to 1 93 00:08:21,320 --> 00:08:23,510 5 6. 94 00:08:23,600 --> 00:08:24,370 OK. 95 00:08:24,470 --> 00:08:29,270 Now finally we can resize the image now by now. 96 00:08:30,170 --> 00:08:36,290 I believe you trust me and what this line is going to do so we not not going to type it out. 97 00:08:36,290 --> 00:08:40,640 I'm going to leave that to you two twenty four to twenty four. 98 00:08:40,640 --> 00:08:44,090 So basically a square exactly a square actually 99 00:08:47,020 --> 00:08:52,630 image dot resize resize as images to size using the specified method. 100 00:08:52,630 --> 00:08:53,110 OK. 101 00:08:53,140 --> 00:09:01,770 And now we want to resize image and the size is going to be height width. 102 00:09:01,780 --> 00:09:04,190 Now you see why we define this here. 103 00:09:04,330 --> 00:09:11,890 Maybe that if we wrote this function a bit better we could input image size as image size 104 00:09:15,010 --> 00:09:16,030 that's probably a little bit better. 105 00:09:17,620 --> 00:09:18,190 Wonderful. 106 00:09:18,190 --> 00:09:21,880 And now you'll get this kind of practice as you start to write your own functions. 107 00:09:22,150 --> 00:09:25,290 The first time you write a function are probably pretty bad pretty terrible. 108 00:09:25,360 --> 00:09:29,470 But then as you start to work out what your workflow is you'll start to understand how to write a better 109 00:09:29,470 --> 00:09:30,830 function. 110 00:09:30,830 --> 00:09:34,450 Now we can return the image. 111 00:09:34,450 --> 00:09:36,260 So what has this done. 112 00:09:36,470 --> 00:09:38,560 Has it done exactly what we just said. 113 00:09:38,750 --> 00:09:41,870 It takes an image path and an image size. 114 00:09:41,870 --> 00:09:44,470 We can define that here. 115 00:09:44,570 --> 00:09:49,820 It reads in the image file from the path named beautiful it turns the j peg into numerical tensor. 116 00:09:49,820 --> 00:09:54,950 So we're just converting it to a tensor with three color channels red green and blue because that's 117 00:09:54,950 --> 00:09:57,880 the convention for most deplaning image models. 118 00:09:57,890 --> 00:09:58,760 They want red. 119 00:09:58,910 --> 00:10:01,090 I said read that but that's a bad thing. 120 00:10:01,100 --> 00:10:08,840 Reading is a great thing we then converted the color channels from 0 to 255 to 0 to 1 values and we 121 00:10:08,840 --> 00:10:11,360 finally resize the image to our desired value. 122 00:10:11,360 --> 00:10:13,990 Now we've written this in a function. 123 00:10:14,180 --> 00:10:17,040 So the beautiful thing is that we can use this later on. 124 00:10:17,060 --> 00:10:18,170 This is what I used to do right. 125 00:10:18,170 --> 00:10:22,690 I used to run through these lines of code every time I wanted to process an image. 126 00:10:22,820 --> 00:10:24,410 Terrible workflow. 127 00:10:24,410 --> 00:10:30,160 So we can actually remove these remember if you want to check out what the function is actually doing. 128 00:10:30,160 --> 00:10:36,560 Break it down line by line takes an image path as input and then see what the rest of it does. 129 00:10:36,580 --> 00:10:37,930 Now I think that's enough for this video. 130 00:10:37,930 --> 00:10:44,620 We've got our function created ready to pre process some images what we might do now is again if we 131 00:10:44,620 --> 00:10:47,290 were to read through the loading data intensive low. 132 00:10:47,770 --> 00:10:49,370 I think it's this one yes. 133 00:10:49,570 --> 00:10:54,520 Back through here there's a little section in here that tends to flow likes to see things in the form 134 00:10:54,520 --> 00:10:55,730 of batches. 135 00:10:55,900 --> 00:10:59,310 So we'll figure what batches are in the next video.