Hello everyone, this is Junaid here from Edureka and I welcome you all to this session where we're going to see how to detect brain tumor using deep learning. So without any further ado, let me quickly walk you through today's agenda. We'll start this session by understanding what exactly is deep learning and why do we need it. Moving ahead, we'll take a look into how to process images using deep learning. And then finally we shall build a deep learning model using which we can detect brain tumor.
Finally we'll end this session by taking a look into pre-trained model and see how we can use transfer learning or pre-trained model to improve our model accuracy. Before we begin do consider subscribing to our YouTube channel and hit the bell icon to stay updated on trending technologies. And also if you're looking for online training certification using deep learning please check out the link given in the description box below.
So let us now get started with understanding what exactly is deep learning. So the first question that we should have in our minds is why do we need deep learning when we already had machine learning algorithms? So the first question that we should have in our minds is why do we need deep learning when we already had machine learning algorithms like random Forest isolation Forest various types of machine learning algorithm. Why do we need deep learning? Why do we need neural networks?
Well, you see with deep learning we can look into various features or dimensions of being a image or even for a textual data and apart from that. deep learning works better when you provide more and more amount of data. Whereas contrary to that what happens with machine learning algorithms is that you know after a certain point of time it reaches a threshold no matter how much data you provide to a machine learning model.
There won't be any increase in the performance or accuracy of a model but for deep learning more the data you provide it prevents overfitting and it also acquires more dimensions and therefore giving us more accuracy. So this is why we need deep learning. So now that we have discussed why we need deep learning.
Now you might be wondering what exactly is deep learning? Well, you see deep learning is basically a subset of machine learning algorithms, which are inspired from human brains. And as I mentioned earlier deep learning works well when you have provide a huge amount of data. So how does this work in contrast to machine learning algorithm? Well, you see machine learning algorithms everything is flattened and everything is in the terms of a single dimension array.
But whereas in deep learning we use something called as tensors and tensors are basically small matrices inside a big Matrix. So you can consider them as a matrix nested inside a matrix and what happens here how deep learning performs better when we give a different images is because looks into different features of that particular image. For example, if I have to take process an image of a cat. So when I provide multiple images of a cat with a different different dimensions, so it takes in a different features and learn that features rather than learning a pixel value of that region. So this is what exactly is deep learning.
So now that we know what exactly is deep learning and why we need deep learning. Let's have a general intuition about you know, the fundamentals of deep learning algorithm the simplest or the most basic deep learning algorithm is basically a perceptron. Well, you see perceptron is a deep learning algorithm that allows us to build a binary classification and a fun fact about perceptron is that it was inspired by actual brain in our neuron just like a neuron in our body perceptron accepts multiple inputs in the form of a matrix and then based on the probability distribution value.
We can conclude which class do they belong? I'm pretty sure you might be confused. What is this Matrix and all of that?
So as you can see here in this slide, these are basically the inputs of our neuron and these are in the form of a Matrix and let me tell you this all of these Matrix x1 x2 x3 and x4 are of the same size and each of these are assigned of weights and weights if you don't know what it means weights basically tries to say what percentage or what share of this value is contributing for the output in general we say it as weights. So are the same. What is the say of x1 for Y and then as is a supervised learning algorithm will also have Y over here. This is the actual value.
Now what happens is when I provide the combination of weights with the inputs and then I also add a bias. This would be a linear function. Now, I want to add some non-linearity for adding a non-linearity.
I have something called as activation function and now when I combine this activation function, I can either have multiple outputs or a single output. And then the way these weights are updated is by taking the loss between the Y and Y hat. Y hat over here refers to a predicted value.
And now once I know that the certain amount of loss that has been made, I would basically back propagate and update the weights. And if I have to do the back propagation, I would basically be taking out the derivatives. This is where the math is pretty important when it comes to talking about math for machine learning and deep learning.
So far, what we saw is a single layer neural network. I would not even say it as layer. I would just say a single neuron and as I mentioned earlier single neuron is called as a perceptron.
But when I combine this multiple perceptrons, we have something called as dense layer. And when I combine this perceptron like many number of perceptrons and I have multiple layers then I can call it as multi-layer perceptron. So what is the advantage of multi-layer perceptron with respect to a single layer perceptron? Well, if I have to give you a brief example a single layer perceptron or a single perceptron is basically a logistic regression problem. I can just perform a single class and I cannot even take a look into multiple dimensions, but will multiple layer perceptron I'll have multiple densely up.
and with every layer I can extract multiple features from whatever input I have been provided with and therefore, you know, I can get a better accuracy on whatever results I'm getting. So this is what is a multi-layer perceptron. So now that we know what is deep learning. What are the basic fundamentals of deep learning?
Let's now quickly move ahead and see how we can use deep learning for processing our images first off as I mentioned earlier that in order to process our image we use two algorithm that we have discussed so far. That is our single layer perceptron and multi-layer perceptron. But do you think we can use these for our images? Well, you see to understand this you have to understand how this works. So let me quickly show you here.
So if this is my neural network and if I have to pass a data and then have some activation, but I would get an output. So I would get whether that belongs to a cat or a dog or I can also say it as I have a single output. So let me quickly show you here.
So let's say I have a neuron here single neuron. We can also take multi-layer perceptron. But as of now, let's take this as an example.
So we have multiple inputs coming in this has the activation function and everything whatever goes in and then finally I have an output whether the probability of this features belonging to cat or a dog, but what happens in this perceptron or multi-layer perceptron is that whatever image is there. So we all know images basically group of numbers or Matrix outside. They are flattened into whatever the shape times one. So it's going to be 1 times n is basically the shape of our or I would say the number of features like if I have to take over here if I have an image of 28 cross 28, I would basically flatten this to 1 comma 28 cross 28 and then I would just pass the values here.
But what is happening with this approach? Well, what happens with this approach is that it ends to overfitting. It leads to overfitting of a data as we have a lot of dimensions.
It will also lead to curse of dimensionality and the worst part over here is that just looking at the pixel value. So we all know this Matrix represents a pixel value now with this what is the issue is it leads to variance in object position. So what this means is like for example, now this is my original image and have image of a cat which is present at this location.
Now, if I flatten this only this location would have that specific mug and next time when I pass a new data and it has an image of a cat which is present in the image but in a different location, but not present at the exact location the algorithm would predict that there is no image of a cat in our particular object. So this is why it's important that we come up with something else. This is exactly why we had CNN algorithm. So what is the CNN stands for CNN basically stands for convolution neural network.
So what exactly is convolution neural network? Well, you see convolution neural network is a class of deep learning algorithms, which are majorly applied for computer vision applications. So what exactly is happening over here?
Well, you see it. The image instead of being flattened directly we extract multiple features and every time I extract this feature I would pass this through another filter which is responsible for seeing which part of the feature actually accurately represented if I say this image has a image of a boat. There are a couple of things that I would see it. I would see the wave because waves are formed only when the boats are moving and then I obviously need to have a boat.
It should have an arch. It should have a couple of grills and all of that and then I don't need these blue one over here. This is something which is irrelevant.
So I would basically extract this feature reduce the dimension and then even more details reduce the dimension and then finally flatten it up and then pass that to my particular algorithm the end. We are actually passing this through a multi-layer perceptron or you can say dense layer. So let's now see each of these components of this convolution neural network in detail. So before we move ahead to see the basic components of convolution neural network, let's take some time out and see how exactly CNN works. Well, you see the The concept behind CNN is something called as image templating or you can say template matching.
So what exactly is happening in template matching? So in template matching we basically will have a filter. So this is basically a filter and this filter can be of any size and what this filter would do is this would go across every part of an image and it will see if there is any pattern that is matching this particular part here. So if you want me to give a comparison of how exactly this thing is working if I used to take a normal in an approach. This entire image this would be flattened into an array and then based on the pixel value.
I'll pass it to a deep neural network or you can say dense layer and then finally have an outcome but here this is not what's happening over here. Oh here, I would have an image. I will generate a filter and then I would basically compare whatever filter I have created to every pixel value over here.
So basically it's moving across rather than being static. We have a filter that is moving across the entire grid of an image and once it finds a particular pattern, it will generate a probability distribution saying that it is this probability that this algorithm says that it belongs to the image of a messy or it belongs to image of a Ronaldo or whatever the football player is there. So this is exactly what is happening in template matching and this is the core concept for detection of images using CNN algorithm.
So let us now move ahead and see what are the basic components that go in in convolution neural network. So let us start off by understanding the most basic concept in CNN that is convolution filter. So what exactly is this convolution filter? Well, I would say convolution filter is the most important stuff because it is responsible for extracting features from our image.
So what do I mean by this feature? So let me quickly show you that now let's say that I have an image of a cat. So I have an image of a cat here and then these are its ear and then you can say. face and nose over here some whiskers whiskers are pretty important features of a cat and then let's say we have an ice and then a small mouth over here. So now what's going to happen here is each filter is responsible for taking out every character of a cat.
So let's say we have 10 filters. So one filter is responsible for taking out the ears one filter is responsible for just taking out the borders and then one is responsible for taking out the whiskers nose and whatever the different features are that And every time I create a feature like let's say we'll have multiple convolution layers. So in the initial stage of my convolution layer or when I would say in the initial stage when I'm applying convolution filters on my images, I'll be using less number of filters because oh here are we getting a broader vision and now as I zoom in as I progress through my pipeline, my image becomes smaller and I would be getting more and more minute detail.
This is basically like let's say you have a TSLR camera. and you are just looking into the hole or that you're just trying to capture an image. Obviously, it would be pretty huge.
No more and more you zoom in more and more you focus into the stuff that you want to take a picture of this exactly how convolution filters are working. You're trying to eliminate the other unnecessary filters or unnecessary characters and then you're going to focus only on the ones which is really important and let's say if we have like our trees over here and other noise that would be ignored. Obviously that won't be ignored at once. We'll have multiple images. So this is the main reason why we have multiple images.
So this would actually take up the cat image and the cat object from different images and then basically come to a conclusion that this is a cat and the cat has so-and-so features. I would just say it has embedded features of the cat. So this is all there is about convolution filters to something sub convolution filter is basically a filter. We usually take it as 3 cross 3 or 5 cross 5 filter and then I pass that through an image and this would extract the features of my image. So moving on to the next component that is pooling layer.
So now you have passed our image through a multiple convolution filter. This would obviously increase the dimension of our image. And now what you're going to do is in order to reduce the dimensions like in order to reduce the complexity. I would just take the maximum value or an average value. This totally depends upon the type of pooling layer.
We have multiple types of pooling layer. You can say global average pool. You can say Max pool and you can also say average pool layer.
So as the name says, what is this pool? There are two parts over here. First off we have Max and then we have pool. So let me just divide this pool is basically this part in this pool of 4 cross 4 matrix. What is the highest number?
That's why I'm using Max. So this is the pool and is the highest number and highest number goes here. Similarly in this pool over here.
What is the maximum number that is 6 that's why it comes down here moving down over here maximum value in this particular pool is 9 and similarly it goes down for this part over here. So here basically we are trying to use about 2 cross 2 Matrix. Why am I taking 2 cross 2 Matrix basically this is an industry standard and you can change the Matrix size whatever is up to your case, but this is what we follow in the industry and what this pool does there is no features over here are learning basically are just trying to extract the maximum value. Now you might be wondering why maximum value. So the reason why we take the maximum value is because they are the probability of existence of that feature is the highest.
So this is why I'm taking the maximum value. So out of these four pixels the probability is highest over here. That's why it's going to be a so this is exactly why we are going to use pooling layer.
And now I am sure you might have understood what exactly is Max pool layer and why you are using it. So moving on to the next type we have padding layer. So as you might have observed earlier whenever I try to pass my image or convolution filter the image size decreases sometimes what happens is we might have a particular feature which is present at the corner. In order to prevent our image or convolution filter from literally ignoring that I would add a padding layer padding layer is basically a zeros which I'm adding across or around my image. With this what happens is if the convolution filter passes through it the image size would remain the same and all the features no matter where they are present in my image would be captured.
So this is what is padding layer and as I mentioned earlier padding layer and even the pooling layer. There is no learning of features. So moving on to the next component we have flattening operation. So if you remember basically at the end of the day, we are going to pass our convolution neural network to a dense layer.
So these are basically the features of a cat. Like if I have to take an example of an image this features cannot just be directly processed. I obviously have to convert them into a single array of numbers and this array is fed to the inputs of our dense layer or I would say multi-layer perceptron and then whatever is the output of that I would get it and the probability distribution manner.
So let's now move ahead and see the complete end-to-end implementation of CNN algorithm. So as you can see here, we have a fully connected layer. So first off we pass our image through this convolution filters and then out of this convolution filter. We have extracted couple of key characteristics of our object in our image and now I want to reduce the dimensionality of it.
Therefore I'm passing them through a max pull layer and again after this want to extract few more details minor details of my image or an object in an image. I'm going to again pass this through a convolution layer and then finally I'm going to flatten it up flattening basically over here refers to basically converting into a single array. And once I flatten my images or I would say once I flatten my features, I'm going to pass this through a dense layer or I would say multi-layer perceptron and finally based on number of outputs I have I'm going to have that many number of neurons in my output layer.
So this is what is the concept of convolution neural network and how I can use convolution neural network to process my images using deep learning. So now that we know what exactly CNN what is deep learning how CNN works. I'm sure now you might be pretty curious on how can I implement CNN on a real-world situation.
So how can I implement CNN to a problem which can be a solution for many things. So now what we are going to do is we are going to jump to the code editor and see how we can implement convolution neural network to detect if a person is suffering from tumor or not and the data set that we are going to use here would be of a brain and it would be an MRI images. So let me know quickly jump to my code editor and the code editor that I'm going to use for today is going to be Google Colab.
So here we are at Google collab. Let me zoom in a bit. So first off what I'm going to do here is let me see if my the run type is a GPU or not as of now, I'll keep it for none because you know, it would just be an unnecessary only during their training time. I would just change this back to GPU.
If you want to change it to GPU can just click on over here. So the first thing that I'm going to do is import warning and before that let me connect this. And for the data set I have uploaded that to my Dropbox and in order for me to get the data, all I'm going to do here is just copy the code and paste it here. This Linux command will just import the data and let me execute this.
So as you can see here that it downloads this particular data and now all I need to do is unzip this up and to unzip. Let me zoom out here a bit and to unzip all I would do is unzip followed by the path. and let me execute this as well.
So as you can see here now, these are the images that are contained over here. And now if I try to refresh this you will see we are going to have a new folder which says brain tumor data within the data again will have brain tumor and the one of a healthy image. This looks great. So now what I'm going to do is before I execute further, let me have this as you can see right.
So these are the cancer images will go through the data set once but before that, let me import some libraries. before then let me close this as well. So now what I'm going to do, let me import warning import warning and then let me just ignore the warnings warnings dot filter warnings and we are going to pass an argument called as ignore looks great. Fine. So now first off we are going to work this on a three stages first off we'll collect the data which we have already done.
Now we'll pre-process it and then we are going to train it to a model. And finally the fourth step would be to test our data like to see how well our model is working and here we are going to train our model on our own and you'll get to know what I'm speaking in a while. So first thing first, I will just import some of the libraries that are commonly used everywhere.
So it would be import numpy as NP then I have import matplotlib as PLT and then as I'm dealing here with files, I'll couple of libraries that are related to file. Let me get them as well. So OS, this is definitely helpful for the files to get the directories and all that.
And then let me also have something like math module. And apart from that, let's also take something like import shuttle. So this math, as we all know, this is basically used for any mathematical related computations. OS would help me with the file directories and if I have to delete any file or something in a folder, this would definitely help me with that.
And then we have shuttle basically with the help of shuttle, I can move my files from one folder to the other. So let me execute this up. So one more library that I would like to import before we move ahead, it will be glob.
So based on the patent of an image, don't have to write the entire path. So let me execute this up. So now what I'm going to do here is count how many images I have in each set count the number of images in the respective classes. What do I mean here by classes the classes is going to be we can say 0 here represents brain tumor and the one here represents a healthy in general brain tumor is the one class and healthy is the other class. So let's now quickly read our data.
So we have this is the folder right? So first thing I'm going to create this directory here root dir and now this would be something which is common for both the files or folders over here. So I'll just take this copy the path and add it between this Collins here and now I'm just going to create a dictionary number of images and what this dictionary would have is the key would be the name of the folder over here.
It's going to be brain tumor and the number of images in the brain tumor and similarly we are going to do for the health. So for this I'm going to have a for loop for dir that is directory in OS dot list dir list dir basically would give me what is present in that particular folder and if I just give root directory so root dir so what this thing would give me is a list of the values that are present here or the folders that would be brain tumor and healthy and now this number of images I have right so let me just copy this as the dictionary and I'm here going to pass dir value here and this would be equal to the length. So if I want to find number of images I have present in this all I'm going to do is list dir again, but the length so if you want me to show you what this list dir does, let me give you an example here. So before that, let me just write this code as well. Let me finish it so that I can execute this up.
So first off OS dot list dir and now I need to provide the path. So for this it will be OS dot path dot join. So we are going to join the root directory. root directory is the one over here. It would be root directory and then we are going to also have dir so dir as we all know this is the name of folder.
It can be either healthy or the brain tumor one and now if I execute this you will see number of images and if I just hit this dot items and now you will see what is present here so you can see brain tumor and these are the images I have but now you can see I'm getting the images that is that I don't want the images. So this is what list dir returns. It returns me the list.
So now I want to know how many images I have for that. I just use a simple function called as length and as that is an array I can iterate it over here and let me execute this up and now once again if I do items and you can see the number of images I have in brain tumor is 2513 and whereas for healthy is going to be 2087 images and now if you want to see what does this list dir does so let me give you an illustration here. OS dot list dir and let me just give any folder over here.
So let me hardcore it rather than going here. If I take the path of this brain tumor and if I add it here, what am I supposed to expect? I'm supposed to expect two folders.
One is a brain tumor and healthy. So let me execute this. Now you see this would give me a list and now if I want the length all I would do is length and then provide length over here as well and it would give me two.
So this is exactly what is happening with the length dir. And now I hope you understand how we are getting this number over here. Let me just add this here and give a space.
So now that we know how many images I have I'm going to perform a split here. So the split is going to be in such a manner that 70% of my data would go for the training data set and then 15% of each would go for validation and testing. So let me quickly show you what I'm doing here.
Let me write it as a comment. So let me minimize this up and expand a bit. So we will split the data. such that so we have 70% of the data for training or I can also set as train data and then what I'm going to do is 15% for validation and then finally, I'm going to take another 15% or let's say rest of it is for testing but so approximately it's going to be 15% for testing looks great fine.
So now what I'm going to do is for this as I'm going to use this data generator which will understand what it is in the future or as we move ahead. So I'm going to put all of the images what I need into a folder. So I'm going to create a folder.
So that is we will create a training folder. So obviously, you know, we don't want this folder to be created again and again. So for this, I'm going to write a if condition that is if that is not that is if the folder is not present that is OS dot path dot exist and the folder name is going to be like dot dot over here means a root directory and then I would pass your train if this is not the case then please create a folder and it will be OS dot MK dir and here I'm going to pass whichever folder I wanted to be created so train and now what I'm going to do is within the same thing we'll have a for loop for dir in OS dot list dir and here we are going to pass the root directory this we know that this is going to give us two things that is the healthy and the unhealthy MRI images.
So within this train, I obviously want to create two different folders one for healthy one for the MRI. So for this I'm going to do OS dot mkdir. So as you can see here, we have two things here.
If I use mkdir I can create only one folder. Whereas if I use mkdir I can create multiple folders. So I'll just use mkdir. and then I already have train if the folder is present it will not create a new folder and for this I will just add something like dir so how this train folder would look like would be something similar to this over here. So inside the train folder will have brain tumor and healthy folders and now what I want within this folder is for image that is the image that is present in I obviously have to take the image randomly.
will not take the image something in from starting few and then somewhere in the middle because with this what happens is I won't have randomness and I cannot generalize my model. So for this I have this numpy function that is np dot random dot random. Sorry, it's not going to be random.
It's going to be choice because we have to choose so choice and now first thing that we're going to pass is the array. So that would be a is equal to os.listdir and here we are going to give os.path.join This would be the root directory and then it'll have dir. Dir basically is the folder and now within this folder what I want over here is the size.
This is where I'll be defining how much value goes here like 70 percent 15 and this. So for that let me just give a comma here and we have this parameter called a size and for the size parameter what we'll do is we'll just use this math function or math module. So that would be math dot floor math dot flow what this would do is Which was the nearest less value if it is 5.4, then it will be regularized or it will be generalized to 5. The reason why I'm doing this is because I obviously don't want my data or the index to be, you know, a decimal number or a whole number.
So it would be 70% so it would be number of images. So what I'm going to do is just simply use this 70 divided by 100. So this obviously can write 0.7 times number of images. This is you know, and then we'll give dir. So why am I doing this number of images number of images is my dictionary over here.
So now dir would refer whether it is for the brain tumor or healthy image and then I'll just give minus 5 here just in case to be on a safer side. I don't want to lose a lot of images or I don't want an unnecessary error. So I'll give you instead of minus 5 you can give minus 2 and then we'll also have this replace is equal to false. So let me explain what I have done here.
Once again, let me just give this a proper indent. So what basically I'm trying to do here is for this image, which is present in this location. I will obviously take image in a random pattern.
So within this 2000 images or whatever the images are present in the respective class, I'll randomly pick the images just so that I have more randomness and my model can be generalized. I obviously have to distribute my data equally between training data set testing data set and validation and when I mean equally I mean to say between 70 15 and 15%. So this is what I'm doing. Whatever is the total size. I just take the 70% of it and minus it by 2 just in case if the value is missing and now what I'm going to do is just transfer this from our major folder that is this folder which is present here brain tumor folder to the test folder.
So let me execute this up. So we have O is equal to OS dot path dot join and this would be obviously root dir and then the dictionary that we are present in or the directory that we are present in and the image make sure most of the time people get an error here. Make sure you give the path and not the image the path the complete path is for the original and this is the destination region and destination. This would be the same. It would be OS dot path dot join and the destination.
We all know it is going to be train and then we will have a folder dir and then finally I'm going to remove it. So OS dot remove. Oh, the reason oh is because I want to remove the image which is present in this particular path and now this happens when the path does not exist.
And now what if path exists, I obviously have to give output away. So I'll give here as else and we'll print it out the folder exists and let's now try to zoom out and see what happens. Let me reset this and now as you can see we don't have any folder by the name test and let me execute this up and now if I try to run this you will see we have this folder called a strain and within this train will have two more folders which would be the tumor and this so I have done a small type of error here and the error that is like we are supposed to pass a slash over here and if I don't pass a slash that would take it as you can see a tumor so let me quickly fix this up let me delete this folder. and let me delete this folder as well.
And now I just did this small fix and let us now execute this up. What this means is that we are exceeding whatever the number we are supposed to give. So for this the small fixes, let me reduce this over here.
I'll make it five and let's try to execute this up. So as I'm done with my execution here, you will see we will have a train folder and this train folder would have brain tumor and healthy and each of them will have the images. So it would be like 70% of the images which is present here. So similarly what we are going to do is we are going to create the same thing for the validation and test and all we are going to do is we are going to copy this and paste it here. So instead of copying right what I could have done here is create a function.
So let's do that. Let's create a function where in every time I pass this up beat for a train or for validation or even for the testing data set all it has to do is it will take up the parameter by what number you want to split or by what number you want to keep up and then add rest of it to the new folder. So let me just do that the EF. data folder creator or I can just say data folder and this would take in two parameters that would be path and then we'll give split and now what we'll do is we just copy this up copy everything here and add it to our function and we'll see F and let's kind of fix the indentation here.
So as you can see, we just create a folder and I have fixed the indentation and now we'll just add the split. We'll replace wherever have trained with split. So let me fix this up and then we'll just add this to split and then similarly we'll do it here.
I had a split. No, this is going to be not split. So PA path or we can just give it as P and this basically refers to whether you want to create it for the train test or validation.
Let me copy this and let me add this wherever we can. Let me add it here in the beginning. We won't do the same mistake again over here.
We'll be adding one more slash just so that makes a difference there and similarly we'll do it over here. So this is the original this thing and we'll have to do some changes present over here. If you're using OS dot path dot join, then you definitely don't need to add the plus or even this because that's what path dot join does and now I think we are good to go.
Let me just cross verify if everything is fine. So the shuttle over here basically transfers the file from one this folder to the other and next the important stuff is we're going to have to add split and now let me execute this up. So now what we'll do is we'll just call this data folder.
This is one function and now in place of P we'll give here as train will just see now it should give me the message that the train folder exists instead of train folder. We would say here. Let's give whatever is the folder name.
So let's give it as P and next the split also would be somewhere like we wanted 70% so 70 means it would be 0.7 and if I just send this it says train is not defined. So we're supposed to pass a string here. So let me pass a string and execute this up so you can say train folder already exists. Now what I want to do is I want to create similarly for the validation data and for validation it will be 15% and let's say we'll give the name here as Val.
and execute this up and if I try to execute this once again, but before that, let me show you what happens here. We have a validation folder and similarly, this is kind of similar to this pattern. And if I try to execute this once again, it says train folder maybe because I haven't executed this again.
So let me try executing this and now you will see here that it says validation folder exists and similarly let's create it for test folder and for the test folder. We will add all the data. So I basically don't want to split.
See how the split works is like whatever is the total number of image. It takes 15% of that. I obviously don't want that.
So instead of taking a 15% I could have just taken 50% so that the next term I could have just added one here. So let me add test over here. Yes. And next time we are going to add 15% can just give it a try over here with 50% and see how well you get the result and this is going to be 100% so as you can see here we are getting an error.
So we'll just change the value like. Let's see what happens if I give 0.5% or 1.5. So as you can see here, it says test folder already exists. So let me see. So this has worked totally fine for the brain tumor, but it has not worked for the healthy one.
So for this I'll just rerun my cell again. So let me just do this quickly. So now we have successfully run this once again and let's see how our folders look like.
So we have test train and validation. So if I see test I have two folders same thing with validation and similarly with the brain tumor. Let's see.
What are the remaining number of images I have so as I'm deleting the old images, that means my number of images over here should decrease. So let me copy this again or just for our reference. Let me take this up and let's see how many images we have.
So as you can see here, we have just 17 and 16 images. That means we have successfully transferred our images from one folder to the other. So now that we have successfully divided our data between train and test, let's now kind of build a model.
So this is the interesting part that we all have been waiting for and let me just zoom in a bit over here. So we'll add a comment saying that model building. So for this model, we have to import couple of libraries that would be like from Keras Keras is a framework which is like the underlying framework for TensorFlow and we have this function here called as layers and here we'll have import now we can import whatever we want.
If you remember we had first thing convolution layer. So we have con 2D because images are 2D vector. Then if you remember we had max pool and even this is going to be 2D. and then finally we have to obviously drop out or in order to prevent overfitting. We have to drop out some data.
So this is it we drop out and finally once we have a perfect matrix that I believe it's trained. I would just flatten it and this flattened layer has to be passed through my dense layer, which is basically multi-layer perceptron or you can say dense neural network and apart from this we can also take few of the normalization technique. I can take batch normalization. I can then take max pull 2D both of these are same. Max pooling 2d Max pool 2d.
They both are the same so I wouldn't have to import it instead of this. I would just use global average pool and now what we'll do is we'll import couple more libraries here the model that I'm going to use here is sequential. So Keras dot model import sequential.
So what's happening with sequential model sequential model. Basically, we have to provide the inputs one after the other you'll understand the difference between sequential and the other type which I'm going to discuss a bit later. And now as we are dealing with images we have to pre-process our image.
So it will be from Keras dot pre-processing dot images. Then I obviously have to load the image. So import load image and then I have to convert this load image to my vector. So we have image to array find this looks great and we'll just import Keras just in case if you need any libraries on the go looks good. So let me execute this cell and let's now start off by building a model.
So let me give a comment here CNN model if you remember again CNN basically stands for convolution neural network. This is one of the most popular algorithm that is being used for computer vision. So first off I'll give a variable to my model should be model is equal to sequential.
So this is something which is mandatory and within the sequential model. I have to add layers. There are two ways either you can create an array inside this and add layer.
But what I would prefer to do is just give us indent and have model dot add. and inside this add function. I can add whichever layer I want.
So first off I'm going to add convolution layer. So it will be con 2D and this would take couple of parameters that would be number of filters. Let's say it's 16 then obviously kernel size if you remember kernel size kernel size is basically the size of our convolution filter.
So I'll take my kernel size to be like 3 cross 3 is the parameter that we follow in the industry 3 cross 3 then obviously the activation function just so that I can add up. some non-linearity. So it will be ReLU. And finally, as this is my first layer, what I'm going to add here is input shape. It's pretty important to add input shape.
So input shape and the size of the image that I'll use here is 2 to 4, 204, 3 channels. So this is my first layer. Now to this first layer, I'm going to add a couple more layer, but this layer is going to be added to the sequential. So let me add something like model.add Let's say convolution layer. So it's going to be the same thing.
Now the input layer is only for the first one. So let me just copy this up and paste it here and we'll manually change the data. I'll tell you what it is supposed to be first off.
We'll remove this input shape something that we don't need kernel size remains the same activation remains the same. I would just increase the number of filters that is 6 and now in order to prevent overfitting I'll remove model. Add pulling layer. So what pooling layer I'm going to use Max pool as I mentioned earlier before we used to use something called as average pooling, but now no people in the community most of them use the max pool layer and now we'll have pool size and the pool size. We're going to take is 2 comma 2 and now similarly we are going to add couple more layers.
So for that, I'll just copy this. and add it here so far how many convolution layer have I have one two and three three convolution layer to Max pool layer. I think this should be sufficient as a data set is not too huge.
We'll just add a dropout model dot dropout. It should be within add and then we'll have dropout. So what dropout will do is we are supposed to give a rate as you can see by that amount of rate it will decrease the data. So I'll just give I want to drop out 25% of my data, which means I want to retain 75% of my data. So now that I have my convolution layer ready if you remember from what I've explained earlier convolution layer is just the features of my image and now I don't want to add more features because it would lead me to overfitting so I would just flatten this up.
So that would be model.add flatten this flatten layer does not take any parameter and now we'll just add couple of dense layers model.addDense and number of units I'm going to give here as 64 and similarly activation and I'll add one more dropout layer. I'm just using dropout just so that my model does not overfit. So it will be a dropout and we'll give the rate here as 25% again.
Finally, we are coming down to our last layer. That is model dot add dense layer and number of units here will be one because the way this thing is going to work is going to give me a probability of being one or probability of being zero. So the value in this output neuron will range from 0 to 1 and whichever value is closer to either 0 1. it belongs to that value. So let's say this would be one and then we'll have activation instead of really I'm going to use sigmoid if you see close the sigmoid is something similar to what we use in our logistic regression and there are a couple more things that I would like to change is see initially we have taken 16 filters then we have increased to 36 and now I'm going to increase this to 64 and let's add one more layer like this as 128 128. So as you can see here the number over here is increasing but why am I using less number of filters over here compared to over here.
Well as I have mentioned earlier when you take a look into an image through a camera, obviously you'll have a very wider angle and then if you want to focus on let's say on a bird or something you'll zoom in zoom in and more in zoom in more the features you see. So this is what is happening. So when I put on a first filter it would take a look into the entire big picture and here the features are not specific and then as I extract features from the features or the feature map is what it said. I can get more information from it and therefore I'm increasing the values of this to the power of 2 16 36 64 and 128 and let me know execute this but before that, let me also compile this thing up. So it will be model dot compile but before compile, let's just execute this and let's see the summary and let's see how this would look like.
So this is the summary of our model first of the input that is going to take is going to be 2 to 4 obviously once I add up the convolution layer, it's going to decrease by 2. And if I don't want to decrease this by 2 I can say padding and then we have this parameter called as padding. So let's say padding and then I can just give the value here to be same and once I do this my padding layer would be added and size of my image will not change but so far this is fine as you can see here total number of parameters that is going to be trained is 56 lakhs and now finally we'll try to compile this model dot compile here. We're going to say what kind of optimizer I'm going to use.
So by default we have RMS prop that's totally fine, but I would prefer to go with Adam. Obviously if you have to specify the loss and the type of laws that I'm going to use here it will be binary cross entropy because we are doing binary classification. What is binary classification as the name states binary which means to so to class classification whether it's the image is a cancerous or not that forwards binary classification.
So for this either you can write binary cross entropy or the easier way that I would say is Keras dot losses. This would prevent you from doing any typos. Then in the losses, we have couple of them.
We can say binary cross entropy binary category cross entropy. You have to choose the one with this one and not the function and now we have done the optimizer. We have done the loss and now we need Matrix.
So it's going to be a Matrix and will provide I want to do it on accuracy and this is done. So now our model is ready and now for our model. Now we have to input the data.
so that we can train it and then finally test our data for inputting the data. I'm not going to use anything that I'm going to develop. So we have this built-in module which is called as image data generator. So let me quickly get that up and this thing is present inside our image module. So let me also import this up.
Actually, we won't be needing load image and array as we are going to use image data generator. So image data generator. So now that we have imported data generator, let's now get our images.
So for this we have to just call the image. So let me just give this comment over here data generator. And now what we'll do here is try to have this. We'll have train data gen and what this would return me is image.
I don't have to write image. I just have to write image data generator. I think I have not executed this up.
Let me execute this. Looks great. And let me just execute this one last time here.
Maze data generator. And then instead of writing this just for one, I'll write a function. I think that would be great.
So let's say oh here we have the year and what this basically the sort of egg is let's say pre-processing image. Let's give the name here as free images and what this would take is a path and let me give you a comment input. There should be a path and then output would be data generators or I say generated data or I would also say encoded data. This can't be encoded data encoded data comes once I pass it through the model. I can say it as generated data or pre-processed data images.
So now what I'll do here is image data. This is a normal variable and for this I'll have image data generator and this will have this function called as couple of features. So this is basically used for data augmentation. Let's say I'm going to take zoom range and this would be 0.2 then it will be share size or it will be share range that is I would give you a rest 20% 0.2 Similarly, we'll give rescale rescale is something pretty important. I will give it as 1 by 255. We are using rescale to normalize my data and horizontal flip.
This would be true. Why am I doing this zoom range and all of this basically I'm trying to do this in order to increase the dimension. So what do I mean by increasing the dimension?
Now, let's say you are given an object for the first time. Let's say you have taken a parker pen for the first time and now it is not something like any other pen. It is something different.
The way it is built is different way. It looks is different way. It has the tip is different. Whatever the various features are there. How can you make a differentiation between one pen and the other pen obviously by making in-depth analysis of it.
So this is what I'm doing here. So although all the images are of the CD scan, but I'm just going to change couple of parameters here and then finally we have to pass this through an image. So return image or I would say image and this image data will have a function.
And this function is going to be something like image data dot flow from directory. So let me first execute this so that I can get some recommendations. And now I have image this should be image data.
And now this will have flow from directory F O W from directory. And basically over here, I'm going to just provide whatever the parameters is that that is one is a path or I would say directory. And here we'll have obviously like directory as a parameter. and this would take a parameter of path and finally we have a target size make sure the target size is same as the size in the model.
It's going to be 2 to 4 comma 2 to 4. You don't have to provide the model size or I would say dimensions. The reason is because over here we don't have to add the channel size by default. It takes three that is RGB and finally now we have batch size.
So this would be 32 that is 32 images per batch and finally we are going to have class mode and this would be binary. looks great and now we'll just return the image. So now let me just copy this up.
Let me execute this before that and paste it here. Now for this I'm going to have like train data and now I'm just have to provide the path. So path is going to be the path of our train folder. That is this one and let me execute this up.
So as you can see it has found 3,002 not nine images that belongs to two different classes. Similarly, I'm going to do it for the other data that is for validation and for testing. But for validation, I won't be adding zoom range share size and horizontal flip. So let me copy this again and let me paste it here.
I'll give here as two. So let me give it as two and this would be for one because when I try to do the validation, I obviously want the data to be as natural as possible. This is just so that I don't have to deal with model which is works only on our training data set because when I launch this in a production, I don't know what type of data I'll be having.
So to have a more generalized model out remove all of this and this is pretty important because based on this validation know it calculates the loss. So this kind of important step. So now everything remains the same. We are just going to remove few of the parameters and now we'll just give train data. We are done with train.
We have to just add test test data would be equal to just this and the path and the path as I mentioned earlier is going to be test instead of train. It will be test and let's execute this. So it found 679 images and finally will do it for validation.
Everything remains the same. We'll just have to change the folder. So it's going to be Val because this is my validation folder. So with this data generator, I don't have to explicitly label my data.
Everything is done by the function. So on top of that, you know, everything is in one place. So I don't have to worry about regularization or if I've missed on something and finally, let's get our validation data as well.
So even this has the same number of images as the path or as the test path. So now that we are done with our data pre-processing and we have also created a model. The next stage over here is to train a model.
But before that, what we'll do here is we will I'll add some stuff that is called as early stopping and the reason why I'm doing this early stopping is just so that when I'm training my model obviously sometimes my results can come up early and I don't want my model to waste the resource by training all the other parameters. So for this we have something called as early stopping and then model checkpoint. So for this I'll do from Keras dot callbacks import model checkpoint and then I can also just add early stopping and now for early stopping.
Let's say yes is early stopping and now I'll create an object of this for that. Let me just run this block. So early stopping and the parameter here would be monitor the parameter that I want to monitor is validation accuracy. The validation accuracy has to increase then we have to define your minimum Delta minimum Delta means by what value so I'll give it as 0.01 then patients a patient's means how many number of times you should give a chance. So I'll give you a rest 3. You can play around with the different hyper parameters.
These are called hyper parameters and if you get a better result, you can definitely mention them in the comment box below and we will definitely have some offers for you guys with a course and verbose one and then more this would be auto. So this is a stopping and similarly, let me do it for model checkpoint. Everything remains the same. You have just we have to do a couple of changes. So we'll give MC as model checkpoint and then we'll create an object of it.
Obviously it has to monitor validation accuracy minimum Delta is totally fine. We'll have to just remove couple of things that is patience verbose and minimum Delta also would be removing and apart from this I'll just provide couple of stuff that is file path. This is where my best model is going to be saved.
So I'll say best model dot h5. It's important that you give the extension because if you don't save it as an extension it would just be a random file and then we have verbose. Okay, so let me also include verbose.
This would be one And there's one more stuff over here called as save the best model. So save best only and we'll get us true. So this is great. I would just add this one array because when we are trying to add this to a model dot fit we have to pass as an array.
So it will be CB that's callbacks and pass here as ES which stands for early stopping and MC which is for model checkpoint. So finally we are going to train our model. So to train a model, it's pretty simple. All we are going to do is model dot fit transform.
So we have model dot this would return as a history history basically tells me, you know, how our model has been working. So that is like the validation data and the validation accuracy with respect to the training and validation. So it will be model dot fit transform instead of fit. I would just use fit generator.
So fit generator because we have defined a generator give a generator name. So this will be generator is going to be trained data. And apart from that we have couple more steps. We can have steps per Epoch. This is usually given as a then we have number of Epochs.
I'm mentioning 30 here, but usually we give it as 300 400, but provided that we have huge amount of data as well, but no matter what data you give we have early stopping if the model things that you know, it has trained enough or it has the highest accuracy within that time frame. It will just stop the execution. Then we have verbose. We'll give it as one.
Then we have validation data. So for validation data, if you remember, we had created this function. So which is over here, we have this validation data over here. So I think I have done a mistake.
So instead of test, it's going to be Val and let me execute this once again, looks great. So now we'll just pass here as Val data. And finally we have validation steps. So validation steps are going to be like 16 and finally the callbacks. callbacks is something which I have mentioned we have to pass this as an array.
So that's the reason why I have put here as CD. So let me explain you what we're doing here. Let me scroll this over here.
So model dot fit generator basically, you know, it reduces our effort of manually adding up and all of these data. So first off we will take the training data from this generator. It takes this steps per epoch. Then this is the number of epochs that is number of times and the verbose over here. Basically, I'm using verbose to show like whatever the execution is happening.
I want that to be displayed. So that's what is worse validation data. Basically as I mentioned earlier, we are not trying to perform any pre-processing in our data.
So we have let me quickly show you what I mean by that. So as you can see we have created reprocess image one where in I'm trying to do some pre-processing something like data augmentation. So this is called as data augmentation. But now when it comes to test and validation, we are not trying to perform any data augmentation. It is just a rescaling.
So this is what is basically happening here. The another advantage of doing this kind of function is that I'm trying to import new image. So let's say I have downloaded this image from Google and I want to see how my model is working. I just have to pass my image through this function.
I would get whatever pre-processed images that and then I can directly pass it to my model. So this is what is happening and this is what is validation steps and finally callbacks as I mentioned earlier. So now that we are model is ready and only thing that is left is to train when I'm training. I'll just change my runtime type to GPU.
This is because if I have it in my CPU, it would take a pretty long time to execute it and let me give your run all let me factory reset. and then run all so that whatever was there earlier would get cleared up. So as you can see here, our model has started training and then it has also ended and you know, as the values didn't change your much, the model just terminated the execution.
But we are having this like the maximum accuracy that I'm getting is 0.65. Let's do something that's just some hyper parameter tuning. So instead of having this patients as three, let me just give it as five, let me execute this up.
And now this model won't take much time to execute the maximum amount of time that this thing takes is for the first Epoch and now if I try to execute this again, you'll see here now we have straight away jump to zero point that is 67% and now let's see what accuracy our model gives so straight away. We are getting here as 67 and then we have 73% let's see what happens to our model accuracy. So as you can see here, our model has been successfully trained and it trained for like 20 points.
And now the accuracy over here finally does come down to 80% but you know, the best accuracy was in this equal and therefore our model has saved that like early stopping and model checkpoint has saved that 82% and this would be saved as a dot h5 file if I show you over here and now as you can see now our accuracy is kind of increasing and earlier we were getting our accuracy of somewhere around 67 and now finally we have 82%. Now you might be wondering how can I say if my model fits overfitting or not? Well, you can look into couple of parameters when you look into this loss this loss difference between them is not too high. It is one thing to look at and the second thing that you can look at is the difference between accuracy and validation accuracy.
So as long as the difference between validation accuracy and accuracy is not more than 10 I would generally consider that the model is not overfitting and it's really working fine and now funnily let's load our best model. If I don't load our best model, then our model would basically give me this. But before that, let's do some interesting stuff.
We can actually see how our model was performing. So for that, I will do plotting our errors. So let's see our model graphical interpretation. So as I mentioned earlier, we have this history over here.
So if you can see we have this history variable, let me copy this and let me show you what this thing returns. So first off, this will have Let's say H and this would be HS dot history. And now let me just have this edge dot key. It's going to be keys and let me execute this up so you can see we have loss accuracy validation loss and validation accuracy.
Let us know plot this and see how well our model has performed for this. I have to import matplotlib. I guess I have already done that. So for this I have plt dot plot looks like we haven't imported matplotlib.
So let me import over here itself. import matplotlib as plt. Let me execute this up and now we'll just plot our values plt.plot. I want to plot this is my edge.
I want to plot accuracy here and similarly plt.plot validation accuracy. So now you'll understand what I meant to say whether my model is overfitting or not and now finally I'll give a title here plt.title. Let's say accuracy versus validation and finally PLT dot show and let us know execute this up.
So as you can see here, let me give a color as well just so that we know which is what so we'll say C is equal to red, you can give any color you want. So we're just giving it as red, our ED. So as you can see here, validation data or validation accuracy is performing way better.
And I would say accuracy is just fine. Our model is not overfitting. And similarly, we can do this for our loss. Here would be loss.
and here to be validation loss. We also change this over here. And let's see how this thing would look like.
So as you can see here, our loss is decreasing. So initially the loss was at somewhere around 60 65% and now loss has come down to somewhere around 40% so this is what is how to see or graphical interpretation of your model training. So what we'll do now is we'll calculate our model accuracy.
So model accuracy and to do this will obviously load our best model. This is the best model and let me copy the path and in order to do this we have a library that is from Keras dot models. import will have load model and now we have a function here in the load model.
Let's say model is equal to load model. So this is basically a function and here we pass file path and let me do that. Now we have loaded our model and this model.
Let's see the accuracy of our model. So for this we give a title and let's execute this. So we'll say accuracy. This would be equal to model.evaluate generator and now So if you remember we had divided data between three things one was the training which we have used one was the validation which have also used the third one is test something which I have not used.
So this is what we are going to do here. We'll pass your test data test data and let me also see what are the parameters I can give this is great and now in this I'm going to take the value in the first index because this will return me loss and the accuracy and just for our sake will also print this. Let's give a formatting here.
the accuracy of our model is accuracy and let's execute this up. So as you can see our model accuracy is 83% and if you want to see this in the form of person all I need to do is multiply this by 100 times 100 and then also give 1% over here. So as you can see our model accuracy is 83% and now what we'll do here is we'll try to see if our model actually predicts whether a given image is a COVID one or not.
So we'll define a variable here. We'll say path and here we'll take any random image from here. But before that will also try to do pre process the image that is image is equal to image dot load. This is where we would need load image. But instead of load image what we'll do here will just pass as a generator but for you to understand this better.
I'll just use load image function as I have not used that. So for this we have keras. It will be from Keras not pre-processing not images because we are pre-processing the image import.
We have a couple of libraries that is load image and then we'll also have to convert this. So it will be image to an array. So now we are going to use load image and this takes two parameter one is a path which you're supposed to provide and then the target size the target size that we are going to give here is 2 to 4 comma 2 to 4. Let's try to execute this up. Let's see if there are any errors. So we have this error.
Let's see where we are going wrong. Maybe we haven't executed this well. So let me check this out.
Yeah, the reason why we are getting an error is because we obviously have to load our image. So we'll do that in a while. And now finally once we have loaded an image, let's do it right away.
What's that in that? So we have done some pre-processing on test train and Val one data that we haven't touched is from this brain tumor to our original data set. Let's now do one thing.
Let's now see how well our model predicts. I know that this image is of a cancer. So I'll just copy this and paste it here. Thing is, I know that this image belongs to cancer image and not the model.
So let's load. We have loaded the image. Now we'll convert this in array.
It'll be input array. So for this, we have this function, as I mentioned, it will be image to array. And all we are going to do here is image and then divide this by two five five. Let me quite zoom in a bit for you guys.
The reason why I'm doing this by 255 is because I want to normalize my image. So now that I have done this, let's also see the array shape input array dot shape. So it should be somewhere like two to four comma two to four comma three. So let's see that.
Yeah, we are having this now in order for me to feed this to my model, it has to be in a batch size manner. So as you can see here, if I scroll up, so it's going to be two to four comma two to four comma 16. But we also have this none. This basically takes the bath size.
Now if I'm providing one image, it's going to be one. So for this we can do in one of two ways you can either convert this to an array or you can use numpy function. So the number function would be pretty simple. That is input array. This would be equal to NP dot expand dimension.
And now all we're going to do is input array and then we have access. I'll keep it as zero because I want one to be in the zeroth index and now finally we'll do the prediction. So it will be pred this will be equal to model dot predict class and now here we will just pass an input array and I don't want rest of it.
So it will be just 0 and 0 before that. Let's see what this would return. So it will be Fred.
So let me execute this up. So I'm getting the 0 and I'm getting couple of this. I just want 0 here for this. It will be 0 and then again 0 here. Now you might ask me how can I tell what does this 0 belongs to?
Well, if you want to know what this 0 belongs to let's take our train data and now we can just have this function called as classes or class indexes. So it means if we have zero, it means it's a brain tumor. If it has one, it means it is a healthy one.
So what I'll do is that it's right at condition here. If bread is equal, equal to zero, then we'll have print image. Okay. Let's give this function here, having a cancer or tumor. And finally, norm the else print.
The image is not having a tumor instead of image. I can just put us CTR or MRI but before that, let's also see how this would look like. So let me also plot this so for this plt dot lot or will be I am sure I am sure I will just part here input array and then we'll do plt dot show so this should work perfectly fine. Zero is not defined instead of oh it is going to be zero right I've put here as oh so as you can see yes our image indeed has a brain tumor.
Let's take a couple more image. Let's try to play around with this. Let me zoom out here and let me come down here for a change.
Let me just go for healthy and let me take any image. Let's take any image because this is every time I run this the image gets refreshed. So there's no way that I can predetermine whether this image belongs to which class. So I have this image.
Let's run this. So as you can see this image has no cancer. The model is also predicted that it does not have a tumor or let me put this as caps. not having a tumor which means it's working perfectly fine. Just one more last example.
So we know that this belongs to an image which does not have cancer. Let's add this up. Yeah. So as you can see again our model has predicted that MRI image of this brain does not have a tumor, but you know what one thing that I'm kind of worried about is if I have to deploy this on a real-time scenario or when I'm trying to deploy this let's say for an hospital application.
My model accuracy is just 83% So that means that there are 20% chances that my model can predict something wrong. So how do I fix this up? Well, let's see how we can do that. There are a couple of ways that we can do.
So let's quickly move back to our PPT and see what happens. So as we saw when we were trying to perform this practical session, our model accuracy is coming down to 82, which is good, but it's not great. The reason that makes me say it's not great is because as I mentioned earlier, when I try to deploy this in a real-time environment, this being a health field, right?
If let's say if a model has predicted that image or an MRI image of a patient is not cancerous but the reality was that there was a cancer then this is a catastrophe because we are talking about people's life here. So over here will try to increase the accuracy here. So in order to do this we have couple of ways one is by transfer learning the other way is by hyperparameter tuning hyperparameter tuning is something like we can use grid search CV or random search CV. wherein we can change or tune the different parameters that we have and then finally we can just change the values and the best way to do hyper parameter tuning for deep learning models is by using Keras, you know the other or more efficient way is by using transfer learning. So let's see what exactly is transfer learning.
So transfer learning as the name states, we are basically trying to transfer the knowledge that was learned from one model to the other and now you might be wondering like on what basis was this knowledge learned? You see few years back. We had something called as image net competition and the agenda of this image net competition was to develop a model such that it can give out the best accuracy of whatever the object was to be recognized. So over the years, we try to develop more and more complicated models and every year the model the density of the layers increased and model complexity also increase finally, we came up with something called as transfer learning and some of the state-of-the-art models. You can say something like ResNet or Google in it.
These are trained on images like 1 billion images and then they have up to thousand classes and for us to develop such thing to take huge amount of resources and very large amount of time. This is something which we as a normal people can't afford to so this is where transfer learning comes into picture. So what happens over here is let's say a Institute tries to develop a research project and they try to learn on it and once they learn they try to put this on a public open source network.
So let's see what happens here. So this is what we were doing so far. We train a model.
We try to finish the agenda and it's over. So again, what we are going to do here, you know, we are going to try to create another model rather than we could have saved a lot of time in training the model with this. We can save the resources and all.
But as we don't know transfer learning yet, what we would do traditionally is train a model pre-process the data train a model and then once the model is trained, which would usually take quite some time and then we try to deploy it. But this is not what happens in transfer learning as you can see here in transfer learning. There's this Institute they try to learn and they try to train that model on a huge data set and like say image net data set the one which is trained by ResNet or Alex net. They're all trained on 1 billion images and they have around thousand classes.
and this knowledge whatever has been learned or I would say image embeddings, whichever were there have been stored and now what happens is let's say I have another model over here and I train this model and now all instead of training this from scratch. I would refer to this model instead of taking the outputs. I'll give my custom inputs to this so that that would become the ultimate output over here fine.
So basically what's happening here is I have a pre-trained model and then I have my actual data set here. Now, I'll create a model in such a manner that I combine my pre-trained model and my actual model over here and basically pre-trained model has the convolution neural networks, which we usually don't train and then the custom model that we build is usually a dense layer and we usually tend to add the number of classes over here. So this is what is transfer learn. So let us now move ahead and see some of the most popular CNN pre-trained architectures. So starting off with lean it mobile architecture.
You see lean it is one of the first CNN model which was developed to give out the best accuracy outside. So this model is pretty simple compared to any other ones here. We have an input whose size of 32 cross 32 and this has a single channel single channel means the image is usually black and white and then I pass this through a convolution layer as this was developed in 1980s or so you can see we are using 5 cross 5 Matrix, but in the current generation would just be using 3 cross 3 Matrix and this creates a feature map. And then when I perform sampling you can say something like max pooling or average pooling then we reduce the size and so on and so forth and finally we create a flatten layer and then we pass this and the number of outputs or number of classes that this can detect is 10. So this was the basics of lean it architecture now moving down to something more advanced.
We have something like mobile architecture. You see mobile net architecture. The reason why it's important despite of having better models like VGG 16 or ResNet is that this is mobile friendly.
What I mean by mobile friendly is that in order to train or in order to use this deep learning models, it requires huge amount of compute power like GPUs and CPUs and we all know mobile cannot afford that. So in order to keep it light and simple and so that we don't have face any latency when we are running a program they came up with mobile net architecture and now moving ahead we are going to implement mobile net architecture into our application and see how we can enhance the accuracy of a model. So let me know quickly move to my code editor and show you how we can work through this.
So coming back to our Google collab, we don't have to do much. So here if you remember we are trying to do the same thing. I'm going to ignore the warnings and then just get the data using this command and then finally, let me just unzip it up.
And then as I mentioned earlier, you want to see how many images with that are there and then we would split the data. So OS is not defined. It's because I have to run this up and now we'll see how many images are present is the same data set.
So I just try to run this in a faster pace. You try to create the folders train validation and test and then we'll just pre-process the input. So we are using data generator.
So here it says image data generator is not defined as because we have to import it. So let me quickly get that done for you guys. So from Keras not pre-processing not images import image data generator. All right looks great. Let's go ahead.
So one more thing that I would like to show you is the class indices. So for this let me go for train data dot class indices. So what this basically tells me is if I get an output of 0 it means it is a brain tumor image and then if it's one then it's a healthy image now similarly, we'll get our test and validation data and now finally coming down to model training now in order to train a model.
We won't be training our model from scratch. We are going to use a pre-trained model. and let's see how well we get our accuracy over here.
So first thing that you're going to do is let's import some libraries. So import numpy I guess we have already done this but it's okay then import mat.lib and then finally, let's go down to from Keras dot layers. Now we'll import flatten and then we'll also import dense.
So as you can see here unlike before we are not going to import convolution or max pool. Because all of that is taken care by the pre-trained model. And now once we are done with the layers, let's also take model from keras.models.import.
Last time we had used something called a sequential. So this time you won't be using sequential you'll be using functional API. So I'll give you the difference between functional API and the sequential model apart from that will also take load more and now we also have to pre-process the image. So what is this pre-processing basically means is now as you can see here, we have to pre-process the image over here. So what does this pre-processing basically means you see here some images you can say some model do the regularization by actually dividing it by 1 by 255. but few images take the mean and average and divided.
So we are going to do that changes. But before that, let's kind of import all the libraries. So from Keras dot models is going to be applications. So from application Keras or application package, I can import whichever model I want.
So as you can see I have dense net I have efficient net. I have inception V2 and then I have mobile net and various other architectures. I have yet exception net which is 16. and one that I'm going to use over here is mobile net.
So for this particular one, I'm going to import mobile net and then pre-process input. So what this pre-process input will do I have this function. I'll come back to that, but let's get started with the model.
So now as you can see here, we have this pre-process input. So now what we'll do here will come back to function where we are trying to change this input. So we have to bring this to similar format as my as the mobile net was trained on. So for this I'll just have to add this pre process function.
So for that we would just come down to this part here and then we have let's see what are the various parameters that we have here. So as you can see if you have pre processing function and to this pre processing function, all I'm going to do is add pre process input. I'm not going to scale my image.
Everything would be done by pre processing. So let me copy this and do the same thing for my test data set. Oh, yeah, everything remains the same zoom range. Whatever data augmentation we are doing.
Just that we won't be rescaling it scaling would be done automatically by the pre-process input. So I'm pretty sure this pre-process input would be an issue when we move ahead. So I would just import this earlier.
So let me just import it over here. So let's execute this functions once again, nothing changes is there that the way the data representation is there only that's the thing that's going to change. So let's now import our mobile net model here.
And finally, let's now get started by creating our model. So to create a model first off we are going to create this functional model. I would say base underscore model.
So this would be equal to mobile net and now we have to obviously provide the input shape here. So as you can see input shape, this would be 2 to 4 comma 2 to 4 comma 3. Make sure you give the number of channels and now you have to include top. This is a very important concept.
So include top make sure it's false false. So once I do this, you can see, you know, this thing gets downloaded from the internet. So make sure you are connected to internet when you are trying to do this pretend model.
So this has downloaded and now we are going to make sure that we don't want to retrain this again. So for layers in base model dot layers. So if I don't want to train it, all I need to do is layer dot trainable is equal to false and let's execute this now.
So now finally we have imported our base model. Now what we are going to do is We are going to club our base model with our custom model. So before that see how our base model would look like. So base model dot summary.
So these are number of layers that is there and for us to write all of these layers. Obviously, it's going to be pretty tough. So as you can see here, we have a lot of layers and then what we're going to do is as we come down here this particular layer would be clubbed with our custom layer.
So this would be the input for it. So let us quickly get that done. So let's say X is available. Then we have a flattened layer and now we have to provide the input to the flattening layer for the input to the flatten layer. All I'm going to do is right here base model dot output.
So the output of the base model, which is the convolution layer would be flattened up. And now what I'm going to do X would be equal to dense layer. So dense number of units would be one activation would be sigmoid and then if you want you can give a name to this layer, but we won't do that.
And now the input for this would be X. So let me change this as well. This would be uppercase and now finally we are going to create our model. So it will be model is equal to a functional API. This is the functional API.
So here the input to be basically base model dot inputs dot inputs and then we have our output would be X and this is our final model and now to see a model summary you will see that our dense layer would be added at the bottom. So this is something which we have added up and even the flattened layer. So now what we'll do is we'll just feed our data to the model. But before that, let's compile it up.
So model dot compile. We'll have an optimizer here. So last time we took Adam.
So let's now take this default one. Let's say yeah, we'll take the optimizer the default optimizer. So this is the default optimizer and for loss, it's going to be binary cross entropy. So let me import keras here just so that I can call this function import keras. So this would be keras dot losses dot binary crossing and finally we are going to give a matrix here and here we are going to pass accuracy.
So this compiles a model and now finally we are going to train our data. So to train our data, we have model dot fit generator. And now all we're going to do is pass the values.
But before that, let's also have early stopping and model checkpoints. So I just come down to code. Let's say callbacks. So from Keras dot callbacks import model checkpoint and also I need early stopping. And now for model check model be MC me execute this other I get those recommendations.
So it will be MC is equal to model checkpoint. And now we are going to pass various parameters like file path. This basically once tells me where I should store my best model.
So it will be best model dot h5. Make sure you give this extension which is pretty important and then you have monitor. So I want to monitor validation accuracy and then we also have verbose will keep that as one because I want to see what is happening in my model and then also save the best mode save best only so similarly, let's do it for early stopping.
So I'll give it as yes, this would be equal to early stopping fine. And now we'll have to monitor validation accuracy and then we have verbose before that minimum Delta out get a 0.01 and then patients as this is a pretend model. So I would just give it as three and verbose.
This would be just one and yeah that pretty much it. So now we'll just create a list of it callback CB is equal to MC comma ES and will execute this up. So now that we have successfully executed our callbacks.
Let's finally get down to training our generator or to train our generator. It is pretty similar to what we have done earlier. So let me quickly show you that we have before that.
Let me have his tier H I It's basically stand for history. And now we are going to add a couple of parameters. We'll have trained data and then we'll have steps per epoch. We'll give it as 10. Last time we had given it as eight.
So now we'll get us 10. We can change the different different values and see how well you get the accuracy. Then we have epochs. I'll give 30 although I'm pretty sure it would get over soon because we are using the pre-trained model. Then we have validation data. So I have validator for this and then we have validation steps.
So I'll give it as 16 and over here. I just give it as a so that we have a multiples of 8 and then finally callbacks. I'll just pass an array that is see me. So let me just bring everything in order over here.
We have epochs validation validation steps and then finally callbacks and let's now execute our code. So as you can see our model has been successfully trained. And now you can observe here compared to last time where our model started with 0.5% accuracy or I should say 56% accuracy here.
It has already started off with 73% accuracy. And as we go down, it has come down to 87 then 82 then we have 91 92 and yeah, so this is what happens when we use pre-trained model and let me now load the best model. So for that we have this function.
So let's load the best fit model. So as I mentioned earlier for this We have already imported the data. So it will be model is equal to this library model load model.
Let me quickly see if I have imported this or not. So let me execute this once again. And now we'll just provide the path of our model over here. So this is the best model. You copy the path and paste it over here and let's now try to execute this.
So we have loaded the best fit model. And now let's see the accuracy of our model. So we are going to use a similar logic. So accuracy this would be equal to model dot evaluate generator here.
We are going to pass the test data. I have the test data ready here and then yeah, we can want you can also pass a step size, but I think I would just ignore it for now and I'll just give one because this returns me loss as well as accuracy. So let's print this up print our model accuracy is let's say a CC. times hundred percent.
So as you can see our model accuracy has increased by 10% that is 93% accuracy and let's also see how well are the thing has performed. Let's take the history. H I S T or just say H is equal to history not history and now we'll take H dot keys.
We're just trying to see what is that. So it's just like before we have loss accuracy validation accuracy and validation loss and now finally we'll plot it up PLT dot plot. I'll take H and now pass accuracy and similarly we'll say PLT dot plot. We have H which is a dictionary and then pass validation accuracy. We'll give a color here.
This would be red and now finally PLT dot show if you want we can also add some title PLT dot title. Let's say it's accuracy versus validation accuracy and let's execute this. So as you can see our model accuracy, it's somewhere around 95% the max.
Similarly, let's do it for our loss. So we'll have loss here and then instead of validation accuracy, it will be loss and finally this would be loss. So in the earlier model when we try to create our own model the loss was somewhere around 40% but now you can see the loss has come closely to the 0th line here. So if you're thinking why am I choosing this point over here?
The reason is because if you remember we have taken the best model. So our best model had said that the loss over here is somewhere near 0. This is why I'm considering only this point. So now what we'll do is we'll take some random image and then we'll see what our model predict.
So this is something which I'm copying from the what we have done earlier and instead of your argmax, I'll just give a smaller dot predict classes. So we are going to get this and let me just do here. I will just pass out our input array and the reason why we are using argmax is because unlike before here we are going to get two neurons and basically we'll be telling that which neuron has the highest probability.
So how do I know the highest probability? I would know the highest probability by telling which value is higher. So let's now see how this thing works.
So for this as we all know we are getting an error here. Once I start to execute this it says image is not defined. So for this I have to import the libraries that is from Keras dot pre-processing import image. This is kind of similar to Keras or pre-processing dot image. then import load or to array.
So this is one thing that we are going to do fine. We have imported this now apart from that. We also have to pre-process our image so that it is kind of similar to what we have done.
So for this all I will do is take this up and then I is equal to pre-process input and then pass I and let's now execute this. Let's me know update this as well path. So let me randomly choose any values here.
So if I go for brain tumor, it's something which you have not touched and let's say I have taken brain tumor and let's randomly choose any value here and pass it down here. We know that this is the image of a cancer. It says this image is of a healthy brain.
So let's take one more over here. Let's add this over here. I think the major reason why we are getting it this way is because we haven't normalized our data. So let me now get that done as well. So I'll divide it by 255. So let me fix this up for you guys.
So let's drop this to be this way and we also have to change this over here. So what happened over here is I tried to rerun the entire code from the start. So now you can see my model accuracy has increased.
I also tried doing some hyper parameter tuning and so let us now take some random image here. So let me take this image of a cancer and let me pass it down over here. and let's execute this and let's see how our output would look like.
So we have this image so you can see the MRI image is of a brain tumor and which is actually true and now you can see that our model accuracy has now increased to 97% which is a great thing which means there is only 2% chance that my model would predict something wrong. So let's do some more trial here. Let me add this to couple more values one more image and let's see whether this predicts it to be cancer or not. We all know this of a brain tumor and yes, it did predict correct. Let's take one more image from a healthy side and let me copy the path and then let me add it over here.
So as you can see MRI images of a healthy brain and this thing would hold true even if I take out input from an external Google source. So this is it guys about using pre-trained model and you can see there's a huge amount of difference between creating our own model and having a custom pre-trained model. So you can see here our training. accuracy has increased to 97% all right guys with this we come to the end of a session. I hope you enjoyed and learn something new.
If you have any further queries, please do mention them in a comment box below. Until next time, goodbye and take care. I hope you have enjoyed listening to this video.
Please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest. Do look out for more videos in our playlist and subscribe to Edureka channel to learn more. Happy learning!