‫In this video, we're going to create our first CNN model.
‫For dog and cat dataset that we discussed in our last lecture.
‫We have our data in the form of JPEG images.
‫You can download this images from the links that we have provided in the resources section of this video.
‫Once you download the zip file and expect that you will get three folders,test train & validation.
‫Each of these folders contain two sub folders, cats and dogs.
‫And in this folders, you can find the images of cats and dogs that we are going to use for our model
‫.
‫One thing you can straightaway notice is that the dimensions of this images are not standardized.
‫So you can see this is a portrait image.
‫And this is a landscape image.
‫So we have to resize over images as well.
‫Now we have around 4000 different images.
‫We will use 2000 images for training.
‫We will take out 1000 images for our testing purpose, and we will take another 1000 images
‫for our validation data set.
‫Now, before starting, we will save the location of this folders in three variables that are train
‫directory validation directory and test directory.
‫So just.
‫Copy.
‫The address of this folders
‫And save it and this three different variables.
‫Now, if you are coping, you will get backward slash instead of forward slashes.
‫So just remember to put R before pasting this folder addresses.
‫So we have saved addresses of all these folders into three different variables, train directory
‫validation directory and test directory.
‫Now we have to convert this picture images in the form of values.
‫The first step to do this is to read the picture fine, then decode the JPEG, conted to rgb grids
‫of pixel.
‫So we want three channels red, blue and green.
‫Then we need to convert this RGB floating point values and what value between zero and one.
‫So to do all these steps we will use keras image data generator module.
‫This module have functions to perform, all these steps using the single function.
‫so
‫Let's just import image data generator.
‫module
‫Now, there are two parts of this.
‫image data generator module.
‫First is to get the image and then the second part is to rescale that image.
‫You can feel the images from directly from numpy array or from any other source here.
‫In our case, we want our images to directly feed it from a directory or a folder.
‫So we will be using flow from directory method of image data generator
‫So I have created this train data gen Object from image data generator.
‫And here.
‫As an argument, we are providing the rescaling value to be weighed RGB values by 255.
‫Our RGB values lies between zero to 255 and to convert them and to zero to one interval, we have to
‫divide that by 255.
‫So first, we are creating an object with data generator function with rescaling value of one divided by 255.
‫Then we are using flow from directory method
‫Since our data is in the form of folders or directories.
‫Then we are creating two different generators, one for our train data and another one for our valuation
‫data.
‫For train data, first we have to mention the train directory.
‫We have already saved the information of our train directory address in this train directory variable.
‫So first parameter here is our variable, which contains the directory name.
‫Then the second parameter here is the size of images that we want.
‫So since our image sizes are not centralized, we are a serializing it using the target size of 150
‫by 150 pixels.
‫So all of our images are going to convert in this size.
‫Now, we have to mention the batch size that we are going to use in our model.
‫So earlier, if you remember last time we were using a batch size of 64.
‫Now we are using a batch size of 20.
‫Now this flow from directory will feed images in the batches of twenty to our train generator
‫Then we have to provide information that is contained in the form of folders.
‫So since we are doing binary classification and we have two folders, one for cat one for dog
‫we have to provide class mode as binary.
‫The output of this train generator will be in the form of X variables and the y variables Y labels
‫will depend on the name of the folders.
‫So if we have cats and dogs as our folder.
‫So our y variable will will automatically become.
‫Cats and dogs for all the images that are inside those folders.
‫Now, let's revise what this train generator is doing.
‫First.
‫We are going into a train directory.
‫There are two folders there.
‫Cats and dogs.
‫We are resizing all the images in the shape of 150 by 150 y one foot pixels.
‫We are feeding images in the batches of 20.
‫So at the time, we are picking 20 images.
‫Then to create wine labels, we are using the folder names.
‫And since there are two folders and it's a binary classification problem, we have to grow it last more
‫as binary.
‫Then as a result of this function, we will have our images in the form of floating point ten sets
‫with RGB values between 0 and 255.
‫This is the kind of data we used in our last video.
‫Then we are reskilling those pixel values between zero and one.
‫And that's why we are dividing it by 255.
‫Now, why we have created this train data gen object?
‫This is because instead of directly feeding all the data into our fixed function, we are going
‫to feed 20 images at a time indo our fit function while training over model.
‫One of the advantage of using this image data generator is that in case we want augmented data, we
‫can directly apply many more functions like Rescale, Zoom, sheer etc, to generate some more data
‫from our existing data.
‫If you want some more detail about this image data generator.
‫You can click on this link to open the keras documentation.
‫So here you will get all the information you need for image preprocessing