Convolutional Neural Network (CNN) Image Classification in Matlab

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
  • Original Title: Convolutional Neural Network (CNN) Image Classification in Matlab
  • Author: Nuruzzaman Faruqui
  • Description: In this lesson, we will learn how to perform image classification using Convolutional Neural Network (CNN) in MATLAB. Convolutional Neural Network (CNN) is ...
  • Youtube URL: https://www.youtube.com/watch?v=j8taOvhHcoU
👍︎︎ 1 👤︎︎ u/aivideos 📅︎︎ Apr 25 2019 🗫︎ replies
Captions
today we will learn how to use a pre trained convolutional neural network for image classification using image data set called Celtic 101 first we need to download the data set to do it go to your web browser and search for Caltech 101 here it is now click on download this is the data set click here the data set is being downloaded you the download is completed now copy it you and paste it in a folder it is a dipped file so we need to extract it we don't need this deep file anymore we can remove it if we want I prefer removing it let's rename the folder as Caltech 101 our dataset is ready and we are ready to use three trained convolutional neural network to classify the images of this dataset convolutional neural network is a powerful machine learning technique a large collection of images is required to train convolutional neural networks the larger the collection the richer the features that CNM learns these features often outperform features such as hog LBP or serve training our CNN with large collection of diverged images is not an easy task however there is an easy way we can use free trained convolutional neural network it saves a huge amount of time and effort and this is what we are going to do in this lesson to keep things organized create a folder let's name it image classification using CNN now move the dataset in this folder launch your MATLAB and locate the folder we have just created now the folder is in MATLAB current folder window we can see the data set here create a new script and save it as classification you can use any other name there is no restriction on it I'm going to load the data set there is a function in MATLAB called full file this function is used to create file paths let's see how this function works we have the Caltech 101 folder inside this folder there is another folder called 1:1 object categories inside one one of the categories folder there are multiple folders let's take airplane folder now if we run the program the function will return the full path to the last folder you can see here the function has returned the path of the folder aeroplanes I think you have understood how this function works now let's take a variable named output folder and using full file function load the path of the dataset in this variable next task is to get the path of the one on one object categories folder let's take a variable and name it root folder and use the full file function again instead of directly accessing the folder we can use the output folder variable because the path is already there we want to get the path of 101 underscore object categories folder let's run the program yes we got the path of 101 object categories folder in the root folder variable let's terminate it using semicolon so that it doesn't get executed every time we run the program this folder has hundred and two different categories I really want to use all of these categories however it is not convenient to use all of them because if we do so this lesson will be too long to follow properly my goal is to show you how to use pre trained convolutional neural network to classify images and it can be shown using three or four categories let's take three categories taking our variable named categories and I like to show airplanes very laptop category now we will create our image data store in short I MDS I MDS helps managing data because it operates on the image file location it does not level the images into memory until they are rare and that's make it efficient to use type with me IMD s equals image data tour full file root folder categories level source folder names let's run this program and see what happens the image data store has loaded these images and the levels are aeroplanes and more and 9:45 more so the images located in these three categories are now in IMD s with the appropriate level let's terminate this line so it does not get executed every time we run the program here the image data store function has two arguments the first argument is full file root folder common categories it is the location of the images the second argument is a name and value pair here the name is level source and the value is for the names that means the images will be leveled as per their folder names now the IMD s variable contains the images and their corresponding category levels let's count the number of images in each category I'm taking a variable named TBL short form of table and I'm going to use a function named count each level and we will count the level of IMD as variable if we run the program we can see that airplane has 800 images ferry has 67 images and laptop has 81 images the number of images per category is not saying it will create a problem to avoid problems let's make the number of images per category the same I'm going to find out which category has least number of images to do it I'm going to use mean function type with me min set count it's just a variable name you can use other name if you want name and the argument of this function will be this TBL variable this is how the TBL variable is storing data the first column is the name of the categories and the second column contains the number of image per category we want the number of images that is why we have to focus on second column only and using this code we are accessing these numbers that means these second columns let's terminate this line and run this code to see what this line does it's 67 have you noticed that the list number of image was 67 and this line has produced 67 so we will reduce the number of images per category to 6070 terminate this line now we want to update our image data stored in the updated data store each category will have 67 images to do it I'm going to use a function called split each level this function will split each level located in my MDS variable and we'll tip main set count number images in each category you remember mean set count gave us 67 right and I want to select 67 images randomly that is why use randomize argument terminate the line and finally I'm assigning these modified data so to imd is variable now each of the categories of our image data store has same number of images you don't have to believe me see for yourself let's use count each level again and run this program each category has 67 images terminate this line taking our variable names airplanes now using find function to find the images located in I MDS data store which level is airplanes and sequence is one doing the same thing for ferry and laptop category now plotted these images the first image will show the first subplot will show arrow points image the second subplot will show very image and the third subplot will show laptop image running the program and you can see the first image is the image of an aeroplane second one is a ferry and third one is a laptop now we have equal number of images in each category we also visualized the first images of each category it's time to load the pre-trained convolutional neural network there are several pre-trained convolutional neural network that has gained popularity and ResNet 50 is one of them it has been trained on image net data set image net data set has 1,000 object categories and 1.2 million training images we can load ResNet 50 by writing ResNet 50 and we need to assign a variable to it to store the network let's take a variable named net if the rest net 50 module is not installed in MATLAB this command will not work however don't be alert it is very easy to install rest net 15 go to your whole new tab click on add-ons the add-on Explorer will appear in this search box located on top-right corner type ResNet 15 and click on search icon here it is this add-on is already installed in my computer that is why it is showing it is told if it is not installed in your MATLAB you will see our install button here simply click on the button to install it and that's it let's get back to the coding so in our coding the arrest net 50 has been loaded let's plug the network to visualize its architecture figure now plot the net and the title will be architecture of rest net PT running this code and we will see the architecture of the network it seems pretty complex architecture and difficult to understand we need to resize the figure to understand the architecture properly to do it I'm using set function to resize the figure of the network the first argument of this function is GCA it loads the current exists the second argument is why live why Lima stands for why limit the third argument is the region within which we want to limit the figure I'm limiting the X&Y exist within 150 and 170 region let's run the program now we can understand the architecture easily this is the input this is convolution layer and this is the activation layer this is really function and you understand you got the idea how to see the network architecture we need to inspect the first layer to find out what type of input it accepts different pre-trained CNN requires different image size and type by inspecting the first layer we can find out what type of images a particular pre-trained convolutional neural network requires to do that type with me net dot layers one if we run this code we will see the input layer property you can see the input layer properties here the name is input one it takes 224 into two 24 into three size image that means the image dimension has to be 224 into 224 and the tree represents a three channel image for example RGB image let's inspect the last layer to do that type net dot layers and run the program these are the properties of the last layer the name of this layer is classification layer FC 1000 there are 1000 classes that means this CNN model has been trained to solve a 1000 wear classification problem there is another way of getting the number of classes of a neural network you can get the number of classes by typing and um num L break it start net dot layers and dot class names if we run this code we got the number of classes there are 1000 classes in this network one thing I'd like to mention that we are not going to use this CNM for the original classification task it is going to be repurposed to solve a different classification task on Caltech 101 dataset to do it we have to prepare the training and test images I am dividing the images of Caltech 101 data said into training set and test set using a function called split each level and this function will operate on images stored in IMD s that means image data stored I want to use 30% of the image for training and rest of the 70% image for validation and I want to select the image randomly that is why using randomized at the end terminate the line I mentioned earlier that different CNM accepts different image dimension that is why we need to perform a little image processing before sending them to CNN the rest net 50 takes 224 by 224 3 channel images as input let's get the required size of input image net dot layers 1 dot input size gives us the required size of input images let's store the size value in a variable named image size I'm removing the semicolon and running the code to see the size the required size is to 24 - 24 3 now bringing back the semicolon to terminate the line many engineers mistakenly override the source image while processing to avoid such mistake let's use augmented image datastore to resize and convert any grayscale image to RGB image on the fly there is another benefit of augmented image datastore it can be used for additional data augmentation when used for network training the first argument is the size of the image the second argument is setup image we want to perform the image processing on the training data set first training set we need the color pre-processing argument to convert gray image to RGB image the line is pretty long let's break it down now our training data set will be resized according to image size which is the required input image size of ResNet 50 and the grayscale images will be converted to RGB color image let's do the same thing for our test image to do that simply change the variable name then the image set name here this test set is coming from this test set and here this training set is coming from this training set each layer of a convolutional neural network produces a response however not all of the layers are suitable for image extraction the layers at the beginning captures basic image features such as edges and blobs we can see these features by visualizing the network filter weights from first convolutional layer this can help you to understand why the features extracted from convolutional neural networks work so well for image recognition let's get the wait for second convolution layer W 1 equals to net dot layers 2 dot weights this line is getting the weights of second convolutional layer and storing it in W 1 here W 1 is a matrix to visualize it we need to convert it to image using matlab's mat to gray function we can easily convert the matrix to image just put W 1 as the argument of Matsu gray function and it will convert doubly 1 from matrix to a grayscale image storing this grayscale image into w1 flooring W 1 title is first convolutional layer wait running the program and you can see the weights of the first convolutional layer these features are processed by different network layers which combine the early features to form the higher-level image features these higher-level features are better suited for recognition tasks because they combine all the primitive features into a richer image representation using activation method we can easily extract features from one of the deeper layers we can choose any layer however I prefer extracting features from the layer right before classification layer in ResNet 50 this layer is named FC 1000 let's extract the features the name of the feature layer is FC 1000 let's take a variable named training features and now using the activation method it will be applied on resonate 50 neural network and the image data set is augmented training set and we are interested in feature layer that means FC 1000 the mini back size is 32 and we want the output as Collins is a pretty long line let's break it down the activation function although medical use GPU however if GPU is not available the CPU is used here the mini batch size is set to 32 to ensure that the CNN and the image data fit into GPU memory if you de you run out of memory you may need to lower the mini batch size the activations output is arranged as columns this type of columnar output speeds up the multi class linear SVM training now we need the levels of the training set and I'm doing it using this line to train the SVM I'll use a function called feet class error correcting output callback in short fit CEO C it's a strange name but a very powerful function it returns a full trained multi class error correcting output codec model this function uses K into K minus 1 / 2 binary support vector machine models using 1 vs. 1 coding design here K is the number of unique passed levels the first argument of this function will be training features we extracted the features earlier you remember right the second argument will be the levels of training images we stored the levels in a variable named training levels training level the learner of this SVM will be linear classification instead of default 1vs1 coding we will use one versus all coding and our observations will be in power it has become a pretty long line let's break it down I mentioned earlier this function returns a trained model so we need a variable to stall the model let's call this very evil classifier you can use other names you are totally free to do it now we will evaluate the classifier to do that we have to extract the features from test set and we know how to do it we have done it earlier in this line why don't we copy this line and paste it however we have to change the variable name here let's rename it as test features and we have to change the name of test set we're going to extract the feature of this test set so copy the name and paste it here this line will give us the features of test image using these test features we can measure the accuracy of the trained classifier if we pass the test features to the classifier and compare it with obtained features we can calculate the accuracy of the classifier to do it we have to use predict function the predict function returns a vector of predicted class levels based on trained classifier we have already trained our classifier remember this is our classifier so write it down classifier the features the predict function will use our stoled in test features variable write down and our observe they will be in Hollands we need to assign a variable to this function to store the levels returned by this function let's create a variable named predict levels we will have the predicted levels in this variable to compare the result let's get the original levels for that right test set dot levels and assign our variable to it let's name it test levels so we have the predicted levels in this variable and the actual levels in this variable now we can use a confusion matrix to evaluate the performance of the classifier in mat lab we use confusion math function to generate the conclusion matrix the first argument of this function is the known value in our case the test levels are known so copy the name and passed it here as the first argument separated with a comma the second argument is the predicted value in our example the predicted values are predicted levels copy it and test it here as second argument I am removing this semicolon to see the confusion matrix running the program you see the system is working it will take a while here is the confusion matrix let's assign a variable to this function to store the confusion matrix we can use any name I like to call it count that and terminate this line when we are evaluating a classifier we expect a percentage value and there is an easy way to generate the percentage value from this confusion matrix we can do it by using a function called B sx5 the first argument of the BS x function represents the operation we are doing division here so right at our divide we want to divide the confusion matrix so write count math here and we will divide the confusion matrix by this it is a single column matrix instead of explaining let's see what this matrix is really doing copy this section and paste it here now run the program MATLAB is running this is the output from this line what it actually did is it calculated the sum of entire row of this matrix and put the value as the first element here the first element is 47 and this line did the same thing for each rows I'm going to remove this line because we don't need it anymore now let's see what this BS X 1 function doing for that trim of this semicolon and run the program the program is working and it will take some time the vs x 1 function has generated this matrix what it actually did it converted the value of confusion matrix into percentage this is 100% it represents 100% this is also 100% now if we calculate the mean value of this matrix then we will get our percentage accuracy and notice carefully that this matrix has value diagonally so to calculate the mean value of this matrix we have to use the MATLAB diag function so let's see how to do it first store this value in comp math variable and terminate this line then we will use the min function of MATLAB inside it will use diag function and it will be applied on cont mat matrix let's run this program here is the accuracy the accuracy is 0.99 to 9 that means the classifier for this test set is ninety nine point two nine percent accurate now it's time to classify images before that let me terminate few lines now I'm going to comment out the subplots because I want to show the classes only I don't want to show any figures I'm done remember we use three categories airplanes theory and laptop that means we can categorize images of airplanes theory and laptop let's download an image of an aeroplane go to your web browser search for airplanes go to image pick up any image let's take this one save it to your working directory let's rename it test one one you can see the image in current folder window now we need to load this image in MATLAB we know how to do it take a variable know image use eye and read function then use the full file function to get the part of the image named test 101 dot PNG now we need to process this image so that we can send it to the rest net neural network and we know how to do it we already have done it let's just copy and paste it here we need to change the variable names let's call this D s and we need to change the name of the test set here the image is new image this image is representing this image now this image has been processed to use in our system we need to get the image features now and we know how to do it we already have done it I'm simply copying it and pasting it we need to change the variable name let's call it image features and we need to change the name of image set the name of our image set is des this des is representing this D s now using credit function we can compare the features and predict the class we already have used the predict function why don't we copy and paste it here instead of typing we need to change this name because our features are now stored in this variable let's copy the name and paste it here and we need to change this variable name let's rename it to level finally using the s printf function to find out to which class the loaded image belongs to let's run the program you can see the loaded image belongs to aeroplanes class great let's download an image of laptop I'm taking this one saving it in the working directory as test 102 you can see the image in map labs current folder window changing the name of the image and the extension it's a jpg in night running the program again you can see the loaded image belongs to laptop class now the test one or two is an image of laptop so our system has successfully classified the image now let's download an image of a ferry take any other image you want I'm going with this one saving it as test 1 or 3 you can see the image in map labs current folder window change the name from here and run the program again and here it is the loaded image belongs to ferry class that means the image has been classified as ferry and this is the end of this lesson if you find this lesson helpful please subscribe and I will be very happy if you share this video and leave a comment thank you for watching
Info
Channel: Nuruzzaman Faruqui
Views: 72,041
Rating: 4.9496064 out of 5
Keywords: Convolutional neural network image classification MATLAB, Image classification convolutional neural network MATLAB, Convolutional neural network in MATLAB, CNN image classification in MATLAB, Image Classification CNN in MATLAB, CNN in MATLAB, Convolutional Neural Network, SVM CNN MATLAB, Object recognition MATLAB code, Image recognition MATLAB code, CNN MATLAB example, CNN MATLAB tutorial, CNN MATLAB code, Image classification MATLAB code, Deep Learning MATLAB tutorial
Id: j8taOvhHcoU
Channel Id: undefined
Length: 51min 12sec (3072 seconds)
Published: Wed Apr 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.