Image Classification using CNN | Machine Learning Projects | GeeksforGeeks
Key Takeaways
This video demonstrates image classification using Convolutional Neural Networks (CNN) with a practical example using the Dogs vs Cats dataset from Kaggle, covering the basics of CNN and its application in machine learning projects.
Full Transcript
Hello everyone. My name is Chitranjan Upadhyay. And I'm back with yet another interesting project. Though this time it is a deep learning project. In this project, I'll try to create a CNN model which would be able to classify images. So let's jump to my screen and see how it's going to work. So here I'm starting with Google Colab. And one more thing, you just check your resources. It should just make it as GPU. Okay. And then what's the data set I'm going to use? Here I'm going to take uh you know, from Kaggle, I'm going to take this dogs versus cat. You can download this first. Then do one thing. Copy this API command. Right? And paste it here with the exclamation mark. But do not just run it. First, I need to import something. For that you just go to your uh Kaggle profile. And here you just go to settings. As I've already done, create new token. Just click over this create new token. Let me show you again. If you go to settings create new token. Okay? If you just click over it then a JSON file would get open. Now the thing is go to your Colab. Upload this JSON file. And of course just rename it as you know to make things simpler, easier, just rename it. Okay, great. Now, let me just copy one more code to just upload this data set. So, let me run this. Then this. Okay. Now, it's being uploaded, but the point is uh this is like a zip file. If you can see dogs versus cat dogs zip. So, I need to extract the zip file. For that, I'm using another library, zip file, and this code. Okay? Okay. Now, you can see it's being uploaded. All my data sets which were there in my system or there was in Kaggle, train test data is being uploaded like this. You can see I've downloaded into my system as well, cats and dogs. A lot of you know, there are so so many images in it. Sort of a very big data. Okay? But, thereby I have just directly took it from my from Kaggle. Right? Just using my Kaggle profile API, I just take it from the Kaggle, not my from my system. I've downloaded it on my system, but the other way can be just I can directly import from my system, but it take more time, of course. So, I went directly from the Kaggle. Now, the data has been uploaded. Now, let me import the necessary libraries. So, I'm starting with TensorFlow. Then, Keras. Then, let me import uh sequential class. And um there are some li- uh layers I need to import, very important, to create CNN model. Now, the thing is I've tried with uh this these many layers only. Uh just a minute. It's not And the thing is the point was uh you know, I checked the accuracy already. Thereby, I just try to uh thereby to improve the accuracy, I've also imported this batch normalization and uh dropout. But, my point is you should also try other methods how to increase the accuracy. So, that is not that is subjective, right? It's not absolute. So, you need to try it again and again and look for other ways, alternatives to increase the accuracy and Right. So, I believe I've imported all the less- necessary libraries. Great. Now, the next part is I need to dump these images to my model. But, the point here is this is There is This is a lot of data. This is lot of It's going to take a lot of RAM. So, it's not going to be very fast or efficient way uh if I just go like this. A lot of, you know, my RAM get utilized. It will take a lot of RAM. So, how I'm going to deal with this situation? I can do this with using OS module and just, you know, go go to each images one by one and like this. But, in this case, uh what I'm going to do is I'm going to create generators. And how I'm going to create that? Let me show you. I'll go here. Keras. This one. And the one I would create for train data and the same I would create for the test data uh taking the necessary um parameters. Okay. So, I would create the data set likewise. So, one Let me just create the one for training data. So, what I'm going to do is here is I'm just going to creating generators. So, generator would, you know, divide my data into batches. So, it would be efficient for the model to deal with this data. It would come in batches and, you know, would take less RAM and, would be efficient. So, with generator, I'm creating the batches uh for my training data and test data. Okay. So, let me just go for that. So, let me name this as my training data. Train data And here directory for directory, just go there. Copy the path. Copy path. And uh Right. Uh this I don't need. These parameters I do not need. So, let me take it off. Right. Same I would go to create the test for test. Everything I'm just going to copy from there. I believe you got it. Okay? Just need to change the path here. Go to the test. Copy path. Perfect. Now, I can run this. The generator is being created. Okay. So, you can see that. Uh I got two files for three my training data test data. One is like uh you know uh 2 lakh and 5,000 20,000 and uh belonging to two classes, cats and dogs, right? And the other one is also the same, 5,000 belonging to two classes, cats and dogs. Okay, so this much files has been created. Now, second point if if you can see the image size is 256 256. Now, I need to scale scale this size, so it would be easy for computation and all. And we have we know this technique, normalization. So, now see how I how I'm going to do do this. How I'm going to, you know, normalization my data. So, for that I'm just going to create a function. And uh Right? Okay. I'm going to divide that by 255 cuz if you go to the size of the data and all, you'll you can see that, you know, that is the maximum pixel is getting, right? So, we have seen it earlier as well. I'll just go to the train.shape and all, and just go to the train data, you'll get it everything. Okay. So, this is the function I've created. Now, for my train data set, I'm going to use the map function and apply this normalization to every images. You can find it as it's suggesting as well. Same I'll do for this test data. test data set and map my function to it. Great. So, this much is done. Now, the next part is I'm going to create a CNN model. Okay, so let's create a CNN model. And here I'm going to create a different different layers, okay? So, I would go with a you know I would create some layers of convolution 2D with you know 32 filters, 64 128 filters thereby dense layers and plus you know I'm going to add batch normalization and drop out. So, see what are the parameters I'm adding and all in all. So, first of all let me create a model for sequential class. So, 32 filters, okay? Kernel size I'm giving three time three, okay? I've The other parameter is padding for this I'm going just to take it as valid. Activation function I'm going to take value. Okay. Just going with the fundamental parameters and all. And input shape. Like for the images 256 by 256 by 3. Great. So earlier I went with you know without this batch normalization and drop out and what I noticed then you know the over fitting it was you know my model was or was showing over fitting. Right? It was getting over fitting. So you you can try it yourself as well but just to make it more efficient I'm going with this batch normalization. Okay. Pool size just let me Right. So, first layer is added. Now, I'm I'm going to add the same layers likewise. Convolutional 64 filters and 128 filters. Right. So, let me just copy this. Change it to 64. 128. Right. Now, let me add other layers to it. Flatten. Let me add dense to it and drop out. With 128 filters and activation as ReLU. Activation function is Relu. Also, let me add dropout with 0.1. Then again dense with 64 filters. This I've already, you know, tried with different different what sort what sort of different, uh, you know, layers I need to add this neural network which sort of neural how what sort of or how neural I need to create the neural network. So, you can try it on your as well just to try with the different things to just, uh, get the best model. So, it will not be like getting over fitted and you can also check the accuracy though I will show you as well. Right. I believe this okay. Now, let me run this. Great. So, my model is created. Okay. Let me see the summary of my model. How different layers are looking model. Great. Great. Great. Fine. Um Yes. So, there are total almost 1 crore parameters. Okay? These are trainable. These are not variables. Perfect, perfect, perfect. So, this is my model is getting created. Now, I'm going to, you know, fit my model with the data. With the training data. Then, let's see. Okay, before fitting, before fitting, I just want uh to compile it. It's better to compile it. Okay? With the optimizer as Adam. Optimizer and losses binary cross-entropy. These parameters I would choose binary cross-entropy for that. Right. And metrics accuracy. Great. Now, the thing is my model is compiled and it's been created as well. The point here is now to fit it. I I'm going to take 10 epochs. And um for validation data, this parameter I'm going to take my test test DS. So, it will take some time. Have patience. Um Okay, guys. So, now it has been fitted and you can see it went through all the 10 epoch and the accuracy I'm getting is like 79. Eventually, it started uh from 71 and I'm able to reach 80%. Now, the thing is I've tried to uh plot this with Matplotlib and uh show try to show you uh you know, this. So, sort of it's also over fitting but not that much. Okay? So, one is train, one is test. You can see that. The difference is not much though it is. And I can even go for the loss as well. Let me show you that as well. Okay. Here is if I just want to show you the loss corresponding to them. Just a minute. Right. Now, the thing is our model is sort of created and it is being fitted with test data as well. And uh it's a decent accuracy to go for the for testing our data with the you know, real world data or any uh unknown data. So, now the next step is I'm going to take any unknown data. Actually, I'm going to download some picture from the Google of some cat and dogs and upload it to this environment and then let my model predict it what is it, okay? So, I'm loading the images which I've already downloaded from Google. Mhm. Okay, so I have uploaded cat and dog image from the Google, which is sort of a strange to my model. And now, let's see if my model is able to predict them, okay, or classify them. So, first of all, let me import the library, necessary library for that, which is CV2. And uh say for example, just image I'll take it as and uh the path for this one. Now, let me just uh run this. with Okay, so this is how my image is looking. And It is a big image. So let me check the size of that. Okay. dot shape So of course I need to resize it and as well as reshape it. So let me do that. Okay, before shaping it I need to resize it. Correct. Fine. Now it is fine. So let me predict it. Let my model predict it, okay? I believe now everything is fine. Shape size. So it's saying it's at the zeroth index. Uh Now the thing is it is able to predict the model is able to predict the image of this one. Of course, this is by the order of which I have uploaded it. You just try with the other image as well. I'll try to upload it, try to upload many images of others as well, and try to predict with this model. And I believe you got it, the basic idea. I tried to make it simple. I I can understand there are so many jargons involved in this, but I tried to make it very generic and with easy stuff. So just revise it again and again, and then it would settle better. So that's all from my side. Bye-bye. Take care. And let me know in the comment box if you want more project like this. We can go more depth into it as per your response. Thank you.
Original Description
Data Set : https://www.kaggle.com/datasets/salader/dogs-vs-cats?resource=download
In this machine learning project tutorial, we delve into the fascinating world of image classification using Convolutional Neural Networks (CNN). Whether you're a beginner or an experienced ML enthusiast, this tutorial will guide you through the process of building an image classifier using CNN, a powerful technique in deep learning.
Explore Premium LIVE and Online Courses :
https://practice.geeksforgeeks.org/courses/
Follow us for more fun, knowledge and resources:
📱 Download GeeksforGeeks' Official App: https://geeksforgeeksapp.page.link/gfg-app
💬 Twitter- https://twitter.com/geeksforgeeks
🧑💼 LinkedIn- https://www.linkedin.com/company/geeksforgeeks
📷 Instagram- https://www.instagram.com/geeks_for_geeks/?hl=en
💌 Telegram- https://t.me/s/geeksforgeeks_official
Also, Subscribe if you haven't already! :)
Keywords
Image Classification
CNN (Convolutional Neural Network)
Machine Learning Projects
Deep Learning
TensorFlow
PyTorch
Data Preprocessing
Model Training
Model Evaluation
Fine-tuning
Real-world Applications
GeeksforGeeks
Tutorial
Beginners
Experienced Enthusiasts
Intelligent Systems
ML Journey
#GeeksforGeeks #Learntocode #GFG #machinelearning #imageclassification #cnn
Playlist
Uploads from GeeksforGeeks · GeeksforGeeks · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How I got into Walmart | Shailesh Sharma
GeeksforGeeks
Upgrade yourself In 29 Days | GeeksforGeeks
GeeksforGeeks
Learn AWS Fundamentals For Free
GeeksforGeeks
Conversation With Young Achievers | Meet the winners of Bi-Wizard Coding Contest | GeeksforGeeks
GeeksforGeeks
Meet The Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
Interview Prep Strategies | PayPal
GeeksforGeeks
OLX Interview Preparation Strategies | Hukam Singh
GeeksforGeeks
Meet Some More Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Microsoft Azure For Absolute Beginners
GeeksforGeeks
Python for Data Science | Data Science Master Bootcamp | Arpit Jain
GeeksforGeeks
Getting Started with Data Analysis | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
How to prepare theory subjects for SDE interviews | Geeks Summer Carnival 2022
GeeksforGeeks
Get Your Tickets To The Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
TED Talk Data Analysis Project | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
How I Secured AIR 9 in GATE'22 | Tushar
GeeksforGeeks
Learn Java Backend Development | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
How to Recognize which Data Structure to use in a question | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
Learn Data Structures and Algorithms | GeeksforGeeks
GeeksforGeeks
Interview experience at Flipkart | GeeksforGeeks
GeeksforGeeks
Lets Prepare for GATE'23 the Right Way | Sakshi Singhal | GeekSummerCarnival
GeeksforGeeks
Highest Paying Jobs in 2022 | Ishan Sharma | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Geeks Summer Carnival 2022 | 5th April- 11th April | GeeksforGeeks
GeeksforGeeks
Preparing for SDE interviews | Soham Mukherjee | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Full Stack Development with React & Node | Utkarsh Malik | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
Introduction to Open Source and Roadmap to GSOC 2022 | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Web Scraping in Action | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Getting Hired at BITCS via GfG Job Portal | Get Hired With GeeksforGeeks
GeeksforGeeks
How to build a faster landing Page | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Geeks Summer Carnival | 5th To 11th April, 2022 | GeeksforGeeks
GeeksforGeeks
How to get ideas for Startup | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Journey from Tier 3 to JusPay | GeeksforGeeks
GeeksforGeeks
Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
Dispelling Myths and Pre conceptions of Programming Languages
GeeksforGeeks
Must Do System Design Questions
GeeksforGeeks
Understanding Sorting Techniques in an hour | Keerti Purswani | Geeks Summer Carnival
GeeksforGeeks
Get Hired at NEC | Job-A-Thon 8
GeeksforGeeks
Journey from Tier 3 college to Microsoft | GeeksforGeeks
GeeksforGeeks
Get Hired with GeeksforGeeks at SuperK | Job A Thon 8
GeeksforGeeks
GeeksforGeeks: Redesigned
GeeksforGeeks
From Tier 3 to cracking multiple interviews | GeeksforGeeks
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Youtube Data Analysis | Ashish Jangra | GeeksforGeeks
GeeksforGeeks
DSA Self-Paced Course Preview | Sandeep Jain | GeeksforGeeks
GeeksforGeeks
GATE Live Classes | Prepare for GATE CS 2023 | GeeksforGeeks
GeeksforGeeks
Journey from JIIT to Adobe
GeeksforGeeks
Life Is Unfair Ft. Shonty badmash | LIVE Discord Session | A GeeksforGeeks Exclusive
GeeksforGeeks
Interview Experience at Google | Tech Dose
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Interview Experience @ Amazon | GeeksforGeeks
GeeksforGeeks
My journey through the tech world from India to US | Vidushi | GeeksforGeeks
GeeksforGeeks
Complete Interview Preparation Course | GeeksforGeeks
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
Getting Hired at FiftyFive Technologies | Job-a-thon 9.0
GeeksforGeeks
GFG Karlo, Ho Jayega | GeeksforGeeks ft. Khaleel Ahmed
GeeksforGeeks
How I got job offers from 2 big companies : Arcesium & Microsoft | GeeksforGeeks
GeeksforGeeks
LINUX for Beginners | GFG x Itversity
GeeksforGeeks
My interview experience at Walmart | GeeksforGeeks
GeeksforGeeks
Get Hired at Speckyfox
GeeksforGeeks
Live Mock DSA
GeeksforGeeks
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
I Went Back to 2012 to Build AlexNet From Scratch
Medium · Deep Learning
Detecting Rice Leaf Diseases with Deep Learning
Medium · Deep Learning
Trained a neural net to reconstruct Bad Apple in real-time.
Reddit r/deeplearning
AI/ML Under the Hood — Part 29: CNN Breaking News: Proximity Matters
Medium · Deep Learning
🎓
Tutor Explanation
DeepCamp AI