Cancer Detection Using Deep Learning | Deep Learning Projects | Deep Learning Training | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone this is janeed here from edureka and i welcome you all to this session where we're gonna 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 training technologies and also if you're looking for online training certification using deep learning please check out the link here 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 an 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 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 it looks into different features of that particular image like 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 learns that features rather than learning a pixel value at 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 or the say what is the say of x one for y and then as is a supervised learning algorithm we 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 is being 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 match 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 perceptrons i'll have multiple dense layer 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 you 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 feature is 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 i would say they are flattened into whatever the shape times one so it's going to be one times n and 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 too overfitting it leads to overfitting of our data as we have lot of dimension 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's 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 i have image of a cat which is present at this location now if i flatten this only this location would have that specific mark 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 have cnn algorithm so what does a cnn stands for cnn basically stands for convolutional 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 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 now if i say this image has a image of a board there are a couple of things that i would see i would see the wave because waves are formed only when the ports are moving and then i obviously need to have a port it should have an arch it should have couple of grills and all of that and then i don't need this blue one over here this is something which is irrelevant so i would basically extract this feature reduce the dimension and then extract 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 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 will 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 a n approach this entire image this should 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 over 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 a 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 now 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 are 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 gonna 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 over here i'll be 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 my new detail this is basically like let's say you have a dslr camera and you are just looking into the hole or that you're just trying to capture an image obviously it will be pretty huge now more and more you zoom in more and more you focus into the stuff that you want to take a picture of this is 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 will 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 as embedded features of the cat so this is all there is about convolution filters to sum things up convolution filter is basically a filter we usually take it as three cross three or five cross five 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 pulling layer so now we have pass our image through a multiple convolution filter this would obviously increase the dimension of our image and now what you are 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 pulling layer we have multiple types of pulling layer you can say global average pool you can say max pool and you can also say average pulley 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 this 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 the maximum value in this particular pool is nine and similarly it goes down for this part over here so here basically we are trying to use about two cross two matrix why am i taking two cross two matrix basically this is an industry standard and you can change the metric 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 you're 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 there 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 eight 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 so 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 these 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 in 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 full layer and again after this i 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 out 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 gonna pass this through a dense layer or i would say multi-layer perceptron and finally based on number of outputs i have i'm gonna 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 now quickly jump to my code editor and the code editor that i'm going to use for today is going to be google collab 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 that training time i would just change this back to gpu if you want to change it to gpu you 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 we'll have brain tumor and the one of a healthy image ah 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 we'll go through the data set once but before that let me import some libraries um then let me close this as well so now what i'm gonna do let me import warning import warning and then let me just ignore the warnings warnings start 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 will collect the data which we have already done now we will pre-process it and then we're gonna train it to our model and finally the fourth step would be to test our data like to see how well our model is working and here we're gonna 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 i would commonly use 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 a 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 maps 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 you 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 a 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 columns 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're 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 d i r 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 gonna 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 gonna do is this dir again but the length so if you want me to show you what this list dr 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 is the name of the 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'll 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 it's gonna be 2087 images and now if you want to see what does this dr 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 hard code 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 2. so this is exactly what is happening with the length tir 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 yeah 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 percent for validation and then finally i'm gonna take another 15 percent or let's say rest of it is for testing but stop rocks 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 you'll 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 mkdir and here i'm gonna pass whichever folder i want it to be created so train and now what i'm gonna do is within the same thing we'll have a for loop for dir in os dot list dir and here we are gonna pass the root directory this we know that this is gonna give us two things that is the healthy and the unhealthy mri images so within this train i obviously wanna create two different folders one for healthy one for the mri so for this i'm gonna 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 mkdirs i can create multiple folders so i'll just use mkdirs 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 we'll 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 a randomness and i cannot generalize my model so for this i have this numpy function that is np.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 dot list dir and here we are going to give os dot path dot join this would be the root directory and then it will have dir they are basically 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 15 and this so for that let me just give a comma here and we have this parameter called as 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 mat that flow what this would do is whichever is 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 you 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 here 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 percent so this is what i'm doing whatever is the total size i'll just take the 70 of it and minus it by two 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 this is for the original and this is the destination origin 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 strain and within this train we'll have two more folders which would be the tumor and this so i have done a small typo 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 your 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 the 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're 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 every time i pass this update 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'll just copy this up copy everything here and add it to our function control cf 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 will replace wherever i have train with split so let me fix this up and then we'll just add this to split and then similarly we will do it here add a split no this is going to be not split so p8 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.path.join then you definitely don't need to add the plus or even this because that's what path.join does and now i think we are good to go or 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 gonna 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 strain we'll 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 strain 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 exist 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 strain 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 exist 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 a total number of image it takes 15 of that i obviously don't want that so instead of taking a 15 percent i could have just taken 50 percent so that the next term i could have just added one here so let me add test over here test and next time we are going to add 15 percent you 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 percent 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 our 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 would be dropout and finally once we have a perfect matrix that i believe it's strain 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 pool 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 boot 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 fine this looks great and we'll just import keras just in case if we 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 this would 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 gonna 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 three cross three is the parameter that we follow in the industry three cross three then obviously the activation function just so that i can add up some non-linearity so it will be a 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 two to four comma 204 comma three channels so this is my first layer now to this first layer i'm going to add couple more layer but this layer is going to be added to the sequential so let me add something like model dot add uh 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 six and now in order to prevent overfitting i'll remove model dot add pulling layer so what pulling layer i'm going to use max pool as i mentioned earlier before we used to use something called as average pooling but now you know 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're gonna add couple more layers so for that i'll just copy this and add it here so far how many convolution layer i have i have one two and three three convolution layer two max pool layer i think this should be sufficient as our data set is not too huge we'll just add a dropout model dot drop out 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 percent 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 dot add flatten this flatten layer does not take any parameter and now we'll just add couple of dense layers model dot add dense and number of units i'm going to give here are 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 drop out 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 zero to one and whichever value is closer to either zero one it belongs to that value so let's say this would be one and then we'll have activation instead of railway i'm gonna 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 of 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 now 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 off the input that it's gonna take is gonna be two to four obviously once i add up the convolution layer it's gonna decrease by two 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 over 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 gonna be trained is 56 lakhs and now finally we'll try to compile this model.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 atom obviously if you have to specify the loss and the type of loss 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 two so two class classification whether it's the image is a cancerous or not therefore it's 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 should prevent you from doing any typos then in the losses we have couple of them we can say binary cross entropy binary categorical 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 then we need matrix so it's going to be a matrix and we'll 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 means 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 over here we have def and what this basically this would take 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 here a comment input there should be a path and then output would be data generators or i would 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 see it as generated data or pre-processed data images so now what i'll do here is image data this is just 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 would be share size or it would be share range that is i would give here as 20 0.2 similarly we'll give rescale rescale is something pretty important and we'll give it as 1 by 255 we are using rescale to normalize my data and horizontal flip uh 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're 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 different way it has the tip is different whatever the various features are that 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 animate so written 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 l o w from directory and basically over here i'm gonna just provide whatever the parameters is that that is one is the 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 our target size make sure the target size is same as the size in the model it's going to be two to four comma two to four you don't have to provide the model size or outside 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 gonna 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 three thousand two not nine images that belongs to two different classes similarly i'm gonna 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 2 and this would be for 1 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 i would remove all of this and this is pretty important because based on this validation you know it calculates the loss so this is kind of important step so now everything remains the same we are just going to remove few of the parameters and now we will 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 we'll 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 and 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 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 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 have 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 here minimum delta minimum delta i means by what value so i'll give it as 0.01 then patience our patience means how many number of times you should give a chance so i'll give here as three 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'll definitely have some offers for you guys with the course and verbose one and then more this should be auto so this is stopping and similarly let me do it for model checkpoint everything remains the same here just we have to do a couple of changes so we'll give m c 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 a couple of things that is patience verbose and minimum delta also we'll be removing and apart from this i'll just provide couple of stuff that is file path this is where my best model is gonna 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 will 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 to an array because when we are trying to add this to a model.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 our model it's pretty simple all we are going to do is model.fit transform so we have model dot this would written 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 this is going to be train 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 thinks 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 will go does 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 well 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.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 volvo's 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 pre-process image one wherein 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 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 image is there 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 vr 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 very long time to execute it and let me give here 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 here 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 let's do some hyper parameter tuning so instead of having this patience 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 will see here now we have straight away jump to zero point that is 67 percent and now let's see what accuracy our model gives so straight away we are getting here as 67 and then we have 73 percent 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 well epochs and now the accuracy over here finally it has come down to 80 but you know the best accuracy was in this epoch and therefore our model has saved that like early stopping and model checkpoint has saved that 82 percent 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 percent now you might be wondering how can i say if my model fits over fitting or not well you can look into couple of parameters when you look into this loss this loss of difference between them is not too high there 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 finally 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 now 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 map.lib 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.lord i want to plot this is my edge i want to plot accuracy here and similarly plt dot 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 dot title let's say accuracy versus validation accuracy and finite will be plt plt.show and let us now 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 red so as you can see here validation data or validation accuracy is performing way better and i would say the accuracy is just fine our model is not over fitting and similarly we can do this for our loss here would be loss and here would be a validation loss let me 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 percent and now loss has come down to somewhere around 40 percent 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 we'll obviously load our best model this is the best modeling 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 mode 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 let me give a title and let's execute this so we'll say accuracy this would be equal to model dot evaluate generator and now if you remember we had divider data between three things one was the training which we have used one was the validation which you 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 will 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 we'll 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 percent and if you want to see this in the form of person all i need to do is multiply this behind times 100 and then also give one percent 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 a model actually predicts whether a given image is a coded 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 we'll also try to 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 dot pre-processing dot images because we are pre-processing the image import we have 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 the path which you are 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 value one data that we haven't touched is from this brain tumor so 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 into array it will be input array so for this we have this function as i mentioned will be image to array and all we are going to do here is image and then divide this by 255 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 2 to 4 comma 2 to 4 comma 3 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 2 to 4 comma 2 to 4 comma 16 but we also have this none this basically takes the path 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 numpy function would be pretty simple that is input array this would be equal to np dot expand dimension and now all we are going to do is input array and then we have axis i'll keep it as 0 because i want one to be in the zeroth index and now finally we'll do the prediction so it will be spread this will be equal to model dot predict class and now here we'll just pass an input array and i don't want rest of it so it'll be just 0 and 0. before that let's see what this should return so it will be spread so let me execute this up so i'm getting the 0 and i'm getting couple of this i just want 0 over here for this it will be zero and then again zero over here now you might ask me how can i tell what does this zero belongs to well if you want to know what this zero 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 let's write a condition here if red 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 no else print the image is not having a tumor instead of image i can just put as 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 it will be i am sure i am sure i will just part here input array and then we will do plt dot show so this should work perfectly fine zero is not defined instead of o it is going to be zero right i have put here as o so as you can see yes our image indeed has a brain tumor let's take 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 an 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 has also predicted that it does not have a tumor or let me put this as caps not having a tumor so 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 here 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 we'll 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 hyper parameter tuning hyper parameter tuning is something like we can use grid search cv or random search cv wherein we can change the 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 tuner 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 a few years back we had something called as imagenet competition and the agenda of this imagenet 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 tried to develop more and more complicated models and every year the model the intensity of the layers increased and model complexity also increased 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 lee net these are trained on images like one billion images and then they have up to thousand classes and for us to develop such things it would 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 our model we try to finish the agenda and it's over so again what we're gonna do here you know we are gonna 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 a model is trained which should 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 imagenet dataset the one which is trained by resnet or alexnet they're all trained on one billion images and they have around thousand classes and this knowledge whatever has been learned or i would say image embeddings whichever was there i 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 learning so let us now move ahead and see some of the most popular cnn pre-trained architectures so starting off with linear mobile architecture you see leonard 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 we 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 flattened 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 lenet architecture now moving down to something more advanced we have something like mobile architecture you see mobile. architecture is the reason why it's important despite of having better models like vg16 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 require 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 mobilenet architecture into our application and see how we can enhance the accuracy of a model so let me now 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 gonna 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 we want to see how many images 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 it's 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 different that's because we have to import it so let me quickly get that done for you guys so from keras dot pre-processing dot 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 zero it means it is a brain tumor image and then if it's one then it's a healthy image now similarly we will get our test and validation data and now finally coming down to model training now in order to train our model we won't be training our model from scratch we're going to use a pre-trained model and let's see how well we get our accuracy over here so first thing that we'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 matplotlib and then finally let's go down to from keras dot layers now we'll import flatten and then we'll also import dents 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 dot models import last time we had used something called as sequential so this time you won't be using sequential we'll be using functional api so i'll give you the difference between functional api and the sequential model apart from that we'll also take load mode 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 divide it so we are going to do that changes but before that let's kind of import all the libraries so from keras dot models it's going to be applications so from application keras dot application package i can import whichever model i want so as you can see i have densenet i have efficient net i have inception v2 and then i have mobile net and various other architectures i have here exception net vg16 and the one that i'm going to use over here is mobilenet 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 we'll 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 we have preprocessing function and to this brief 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 over here 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 that the way the data representation is there only that's the thing that cannot 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're 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 lets 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 a variable then we have a flattened layer and now we have to provide the input to the flattened layer for the input to the flattened layer all i'm going to do is write 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 a model so it will be model is equal to a functional api this is the functional api so here the input would 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 a 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 our 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 are 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 checkpoint it will be mc let me execute this so that 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.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 we'll 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 es this should be equal to early stopping fine and now we will have to monitor validation accuracy and then we have verbose before that minimum delta or get a 0.01 and then patience as this is a pretend model so i would just give it as 3 and verbose this would be just one and yeah that's pretty much it so now we'll just create a list of it callback cb is equal to mc comma yes and we'll execute this up so now that we have successfully executed our callbacks let's finally get down to training our generator now to train our generator it is pretty similar to what we have done earlier so let me quickly show you that but before that let me have hist here hi it's basically stand for history and now we are going to add a couple of parameters we'll have train data and then we'll have steps for epoch we'll give it as 10. last time we had given it as 8 so now we'll get as 10. you 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 valid data for this and then we have validation steps so we'll keep it as 16 and over here i'll just give it as 8 so that we have a multiples of 8 and then finally callbacks i'll just pass an array that is c 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 over here compared to last time where our model started with 0.5 percent accuracy or i should say 56 percent 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 mode 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 copy the path and paste it option 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 if you want you can also pass a step size but i think i'll 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 acc times 100 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 we'll just say h is equal to history dot 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 here our model accuracy it's somewhere around 95 percent 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 tried to create our own model the loss was somewhere around 40 but now you can see the loss has come closely to the zeroth 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 has said that the loss over here is somewhere near zero 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 predicts so this is something which i'm copying from the what we have done earlier and instead of your arg max i'll just give as small.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 our max is because unlike before here we're gonna 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 so that is from keras dot pre-processing import image this is kind of similar to keras or preprocessing 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 now 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 instead of this it would 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 two percent 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 image is 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 percent all right guys with this we come to the end of our session i hope you enjoyed and learned something new if you have any further queries please do mention them in the 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
Info
Channel: edureka!
Views: 24,549
Rating: undefined out of 5
Keywords: yt:cc=on, cancer detection using deep learning, cancer detection using machine learning, cancer detection using ct scan, cancer detection using cnn, cnn, covid detection using convolution neural networks, brain tumor detection using deep learning, cancer and machine learning, cancer detection, deep learning tumor prediction, deep learning in health care, deep learning projects, deep learning training, edureka deep learning, edureka machine learning, edureka
Id: 7MceDfpnP8k
Channel Id: undefined
Length: 90min 32sec (5432 seconds)
Published: Sun Jun 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.