Convolutional Neural Networks (CNN) in Keras – Python

Convolutional Neural Networks (CNN) in Keras – Python

hello everyone welcome to the semicolon
this is the first video of the series deep learning and I would like to start
off with deep neural networks so deep neural networks are neural networks
which we have seen already so if you just increase the number of layers they
become deep neural networks so that is it nothing much in it so now we move on
to another variant of neural network deep neural network which is
convolutional neural networks so convolutional neural networks are
specifically made for image recognition and classification purposes so they are
inspired by the animal visual cortex so this is how they look when they see an
image like this there are convolutional sub samplings and these again and again so
let’s take an example and build it out in Kera’s as well so we’ll be building
this sorry I have done it in paint and that is why it looks horrible but you
will understand what it means so say we take a 50×50 image and applied 32
filters each of three cross three resize so these filters are to be learned by our
convolutional neural network but then we apply we initialize the weights randomly
and then we apply to 50×50 images and there are 32 filters so we get 30 to 50×50
images and there is a relu activation of course now again to these 32 images
we apply more 32 filters or we apply those filters again so we get a 48×48
images now what we do is in the first filter when we apply we keep the
boundary same and here we remove the boundary so we get 48×48 images 32
images each of size 48×48 now what max pooling is you remove you
have a 2×2 grid and you just maximum value of that grid so say this
48×48 image has grids and in that 2×2 grid whichever is the
highest you take one out of it so in that way the size of the image is
reduced. so now we get 24×24 images 32 in number and now we drop out now
drop out what it does it remove some weights and drains it so that there is a
concept called overfitting and that leads to very good results in training
but very poor results in real life problems so we have to avoid it because
we want our model to work in real life as well. so when we drop out this
prevents it from the problem of overfitting so after that we flatten so
what we do is we dissolve this grid and we put it in a linear structure and
now it’s the same as our normal neural network, a deep neural network you can
say so we have 32x24x24 features here and we’ll be passing it
through 128 and then we have 200 outputs so these outputs can vary based upon the
number of classifications or based on the number of factors so as for now this
is what convolutional neural network is convolutional neural network
is so we’ll be learning how to implement it in Keras soon. so these are libraries
which we have to input, sequential dense dropout, flatten, activation, convolution,
max pooling and the other libraries to do the pre-processing part of the data
then we import numpy as well then we import OS and change the directory so
because my files the data is present in deep learning folder and then we import
a test train split as well. so I’ve I’ve set my optimizer to none because I
have problems with blass installation but you do not have to do it then the
image sizes 50×50 and my images are present in input and data folder. so I
initialize two lists and then I do the following, I convert the image I read
each image in the folder convert each image to RGB then divide by 255 and I
am dividing by 255 because the highest pixel value is 255
and I want my image pixel values to be between 0 to 1 and then I’m come converting them into
00:05:10,979 –>00:05:16,650
numpy array these are the parameters the batch size should be 32 the number
of classes is the length of number of folders in the training data the number
of epoch is the number of iterations we are going to run and the other
filters and stuff so what this does is uniques, Id_train what this does is
since we have our classes as strings it converts them into an array so say you
have five classes ABCDE what it does is creates 5 columns and makes 10000 so it
converts your classes into numbers now this is the model which we need to
create in keras the image which we have seen before so the model is sequential
the convolution 2d and all the parameters are mentioned above then relu
activation then again convolution again relu activation max pooling dropout
flatten dense drop out and the softmax classifier for probability and then we
are fitting the model and the training will take a little bit of time here and
we are predicting with one image which we have so as you can see these are the
five classes on execution these are listed and these are the libraries so
reloaded modules so that has nothing to do with it and the
1 to 5 are the classes which are converted into numbers by this NP underscore
utils so as you can see it is taking a little bit of time and it is
expected to take a lot of time so let’s wait for it. as you can see the training
has been started and we are just working with one iteration that is epoch is
one number of epochs is one and that is why I’m expecting the accuracy
to be pretty low let’s however check out after waiting this is going to take
estimated of 160 seconds so I will I better skip to that part and as you can
see the accuracy keeps changing. so I’m however going to skip to that part. so
as you can see our first model of convolution neural network is about to
get trained in seven seconds and hopefully there won’t be any errors.
so let’s wait for it and as you can see the accuracy is pretty low point
three around thirty percent it’s pretty low you can’t expect it to give good
results but if you increase the number of iterations that is the number of
epochs then it is sure and certain that your accuracy is going to improve.
so there was an error I think with the string part. so however the accuracy
is point two eight which is pretty low and our model is now trained. so this is
small error which okay I found the errors this I had to replace path 2 to
with path 1 and I will be doing it and then I will be executing again so our image is this and now this is the
previous image and our image hasn’t been the image which you put in the test folder
has it been executed. so we have to change the path folder and this is what solves our error
and that error had nothing to do with deep learning stuff. it was just a
variable problem. so now that the prediction is done the model dot predict is
executed. so this is the probability functions and the fourth class is the
highest probable so let’s check if it’s right or wrong. so fourth and then fifth
but the answer I think is wrong because our answer is supposed to be fifth and
it gives four but we can’t blame the model because we’ve just run it once so it
is crested Auklet but our answer was three. our answer was four I’m sorry so,
it is groove, it predicts it to be groove billed. so the probability for it is higher
now let’s change the number of iterations to five and let’s check if our
model improves. so the model has started training again and I’ll again skip to
the part where the training is completed. let’s wait for it so as you can see the
training for this is almost complete and we’ll be done with our model in 14 to 15
seconds. so as you can see the accuracy is increasing from the first iteration
to the last it continuously keeps increasing. so if you go on say up to 2o
iterations the accuracy will be pretty good and that is what we need but just
for the sake of illustration, we can use this number of iterations as well and
this should be done anytime soon. so we are training on 225 samples
testing on 57 samples which is pretty low in itself but that is being done
because we want it fast. so let’s check the predictions again before that let’s
check the image and this is the image this is the right one we are predicting
which what label which object which I am sorry I am extremely sorry which
type of bird is this we are predicting that and the actual answer should be in the string
IMG and it’s crested Auklet and it says. so as you can see the fourth and fifth
classes are almost same in probability. they have the same probability, so we can
say our model has improved now it predicts fourth and fifth together before
that it said it is just fourth which was wrong and the probability of fifth had
improved and I think if we train it for some more iterations it will improve for
sure so this is it guys this is our convolution neural network we have
learned about this and keep watching, hit the like button if this helped you and
share it with your friends. also subscribe if you want to keep watching
thank you

36 thoughts on “Convolutional Neural Networks (CNN) in Keras – Python

  1. nice. Could you elaborate on the image preprocessing? especially the transpose? I worked through some CNN Tutorials in keras. The problem is that data is already kind of preprocessed. Therefore it's hard for a beginner to dive deeper into that. A video with the exact steps to preprocess your own saved pictures and put them into the CNN would be great. Could you do that? Anyway thanks for the video (I subscribed)

  2. Why don't you use Jupyter Notebooks for writing these CNNs, I feel fairly comfy while using it instead of your current IDE.

  3. I saw you video x_train has shape (225,3,50,50) , and cnn has input 3,50,50. What happen if you change x_train shape to (255,50,50,3) dimension put it last, and change cnn input to , 50,50,3 ?

  4. thanks bro for this video. please i would like to know how you structured your data set and what the Deep learning file comprises of? Are input and data part of your training set folders?

  5. Hey Everyone ! Just uploaded a new video on CNN improving the quality of content ! Thanks for the feedback. this video will be removed shortly.

    New Video CNN Explanation :
    CNN Implementation :

  6. What really happens ?are the images converted in to matrices (numbers) and then those numbers are multipled with the weights so every layer would get some numbers different then the images then how do different layers are able to detect feature if the image is changing what am I missing here?

  7. in this video, how did you set the datasets? i download the dataset but all i got only 200 folder with images and no "input" and "data" folder

  8. I divided it into training and testing part. While the training part gets executed but accuracy is very poor as it gets trained on only 1 epoch. As you have showed on 10 epochs I don't get any such thing at all.

  9. Also can you please make video on the cnn and how regularisation is being done on mnist data set using Dropout for image classification

  10. Non very satisfied because:
    – you use absolute path instead of relative
    – dataset are not automatically downloaded in the script
    – requires a lot of editing before it can run

  11. hello I have one error
    The system cannot find the path specified: 'data'
    how can ı solved this problem ?
    thank you…

Leave a Reply

Your email address will not be published. Required fields are marked *