Make predictions with a Keras CNN Image Classifier

deeplizard · Intermediate ·🧬 Deep Learning ·8y ago

Key Takeaways

This video demonstrates how to use a Keras CNN for image classification, specifically predicting whether an image is a cat or a dog, using TensorFlow's Keras API and tools like Jupyter Notebook and scikit-learn.

Full Transcript

[Music] in this video we'll be demonstrating how to predict whether an image is a cat or a dog with a convolutional neural network or CNN and caris so in previous videos I explained some basic principles for working with carrots and then we moved on to focusing on some basics of working with CNN's it's recommended that you watch the previous set of Karass videos before moving forward here because we'll be using some of the items we learned in those videos and building on them here it's especially important that you watch the video that comes directly before this one on building and training a CNN on images and caris because we'll be picking up directly where we left off in that video so our goal has been to build a CNN that can identify whether a given photo is an image of a cat or an image of a dog and in the last video we built a very basic CNN with only one convolutional layer and one dense layer and we saw that it performed pretty poorly in this video we'll see how our CNN holds up to predicting on 10 images of cats and dogs from our test set and these images have not been seen by the model during training so given the poor results that we saw whenever we trained our model in the last video we're expecting it to not do so well at predicting so we're here in our Jupiter notebook and this is the same Jupiter notebook that we've used in the last couple of videos and we left off with this model that we built with one convolutional layer and then we compiled and fit the model and these are the metrics that we solve from it so now I'm going to scroll down to the predict section of our notebook and this is where we're going to start today so the first thing that we're going to do is we are going to create a couple of variables one called test images one called test labels and we're going to set this equal to next test batches and just quickly to refresh your memory we define test batches up top where we defined all of our batches we used an image data generator to define our test batches and this is grabbing batches of images from our test path additionally my test path only has ten total images of cats and dogs and I set my batch size equal to ten so it's only going to take one batch from test batches to make up the entire test dataset alright so now let's scroll back down to our predict so we define the test images and labels equal to next test batches so this is since it's grabbing the next batch of tin and there's only ten in our entire set of test batches it's actually going to be grabbing all of the test images and then I'm just going to plot those along with their labels to show you what the testament just look like so again just some images of cats and dogs here and the dogs are labeled with one zero and cats are labeled with zero one so I'm going to set the labels only equal to the 0th index here so instead of a dog being labeled as a one zero it's only going to be labeled as a one and instead of a cat being labeled as zero one it's only going to be labeled as a zero okay so now let's print out those test labels and see what they look like all right so these are the labels corresponding to these images here so before we saw a label for a dog was one zero now it's a one one zero one one zero one and then a cat zero one zero zero one matches to zero right so that's all I did there okay so now we're actually going to have the model predict on these images and classify these images of dogs and cats so I'm creating a variable called predictions and I'm setting it equal to model dot predicted generator so in previous videos we used model dot predict and predict generator just generates predictions for the input samples from our data generator our data generator in this case is going to be our test batches that we created with image data generator so just like before we would use model dot fit on previous models whenever we trained this model we use to fit generator and now when we are predicting with this model we're going to use predict generator because we're going to be passing it in this test batches variable which is an image data generator so that is the first parameter it's expecting is it generator and then the second parameter we're setting steps equal to one and this is just like we saw if I scroll up the parameter that we saw in modeled fit generator the steps for a pork it's the same type of idea for predictions so basically it's the total number of steps or batches of samples to yield from a generator before stopping so in our case we have 10 sample images in test batches and our batch size for test matches is 10 so it's only going to take one step within our test batches to get all 10 images so if I had 20 images and test matches for example and my batch size was 10 then our steps would be 2 because it would take 2 steps to go through all the data here with our given batch size and lastly I'm just specifying for verbose equals 0 and this is again just depending on how much or how little output that you want to see printed to the console during predicting so I'm going to run this all right it's all finished and now let's just take a look at what the predictions look like okay so here we are these are the labels that it's predicting for our images so looks like actually all of them are 0 1 so what is 0 1 that is our cat so it's predicting for all 10 images which are printed out here in our notebook it's saying that these are all cats which is not good right so let's just look and see what that looks like in a confusion matrix so I'm creating a confusion matrix in exactly the same way as I did in our previous video but there is a video specifically dedicated to how to make a confusion matrix so I'm creating this variable CM I'm setting it equal to a confusion matrix which is something that we imported from scikit-learn at the top of our notebook and I'm passing in our test labels and then our predictions now recall our test labels I arranged those to be these one-dimensional labels and I'm doing the same thing with our prediction so instead of having our predictions equals 0 or 1 like this I'm only grabbing the first column here because we said that a 0 1 was a cat and if we cut it off and only looked at the first column that 0 was going to be our cat and a 1 would be adults we'll run that again this is just the function here that I got from scikit-learn that implements how to plot the confusion matrix so I'm going to run that this is all covered in the previous video on how to create a confusion matrix and now I'm going to actually plot arc and in matrix and see what it looks like so again this is what we expected given the predictions that we looked at above we've got our predicted label so we predicted that an image was a cat five times when it was actually an image of a dog and we predicted that an image it was a cat five times when the image was actually a cat we didn't predict that it was a dog any times so basically it's predicting at a 50 percent accuracy rate which is no good at this point the models not doing any better than chance and with the model that we created if we scroll up and just take a look at the model again this is a very simple model one convolutional layer one dense layer not many filters at all in this convolutional layer and it's pretty naive to think that a model this simple would be able to do something as sophisticated as predict on images that are complex images of dogs and cats like shown here so it would actually need a way more complex model to be able to do this sophisticated task and that's what we're going to be moving towards in our next video in our next video we'll be building a model by importing an existing model that's already been trained on images from a thousand different categories and then we're going to fine-tune that model to be able to classify only on images of cats and dogs so not the 1000 categories that it was previously trained on but instead on cats and dogs so we'll we'll talk more in the next video about what fine-tuning actually is and we'll see how we can use it but the first thing we'll do in that video is import an existing model and show how to build a model with that technique then we'll see that the fine-tuning approach works surprisingly well and then we'll go on to discuss the importance and convenience of fine tuning so stay tuned for that so I hope you found this video helpful if you did please like the video subscribe suggest and comment thanks for watching [Music]

Original Description

In this episode, we demonstrate how to use a convolutional neural network (CNN) for inference to predict on images of cats and dogs using TensorFlow's Keras API. 🕒🦎 VIDEO SECTIONS 🦎🕒 00:00 Welcome to DEEPLIZARD - Go to deeplizard.com for learning resources 00:30 Help deeplizard add video timestamps - See example in the description 08:05 Collective Intelligence and the DEEPLIZARD HIVEMIND 💥🦎 DEEPLIZARD COMMUNITY RESOURCES 🦎💥 👋 Hey, we're Chris and Mandy, the creators of deeplizard! 👉 Check out the website for more learning material: 🔗 https://deeplizard.com 💻 ENROLL TO GET DOWNLOAD ACCESS TO CODE FILES 🔗 https://deeplizard.com/resources 🧠 Support collective intelligence, join the deeplizard hivemind: 🔗 https://deeplizard.com/hivemind 🧠 Use code DEEPLIZARD at checkout to receive 15% off your first Neurohacker order 👉 Use your receipt from Neurohacker to get a discount on deeplizard courses 🔗 https://neurohacker.com/shop?rfsn=6488344.d171c6 👀 CHECK OUT OUR VLOG: 🔗 https://youtube.com/deeplizardvlog ❤️🦎 Special thanks to the following polymaths of the deeplizard hivemind: Tammy Mano Prime Ling Li 🚀 Boost collective intelligence by sharing this video on social media! 👀 Follow deeplizard: Our vlog: https://youtube.com/deeplizardvlog Facebook: https://facebook.com/deeplizard Instagram: https://instagram.com/deeplizard Twitter: https://twitter.com/deeplizard Patreon: https://patreon.com/deeplizard YouTube: https://youtube.com/deeplizard 🎓 Deep Learning with deeplizard: Deep Learning Dictionary - https://deeplizard.com/course/ddcpailzrd Deep Learning Fundamentals - https://deeplizard.com/course/dlcpailzrd Learn TensorFlow - https://deeplizard.com/course/tfcpailzrd Learn PyTorch - https://deeplizard.com/course/ptcpailzrd Natural Language Processing - https://deeplizard.com/course/txtcpailzrd Reinforcement Learning - https://deeplizard.com/course/rlcpailzrd Generative Adversarial Networks - https://deeplizard.com/course/gacpailzrd 🎓 Othe
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from deeplizard · deeplizard · 0 of 60

← Previous Next →
1 Install Jaxx cryptocurrency wallet on Windows 10 and verify file hash
Install Jaxx cryptocurrency wallet on Windows 10 and verify file hash
deeplizard
2 Jaxx cryptocurrency wallet overview - A Blockchain Interface
Jaxx cryptocurrency wallet overview - A Blockchain Interface
deeplizard
3 Remove Jaxx cryptocurrency wallet from Windows 10
Remove Jaxx cryptocurrency wallet from Windows 10
deeplizard
4 Install Jaxx cryptocurrency wallet Chrome extension
Install Jaxx cryptocurrency wallet Chrome extension
deeplizard
5 Send Litecoin from GDAX to Jaxx wallet
Send Litecoin from GDAX to Jaxx wallet
deeplizard
6 Send Litecoin from Jaxx wallet to GDAX
Send Litecoin from Jaxx wallet to GDAX
deeplizard
7 Backup and restore Jaxx wallet with passphrase
Backup and restore Jaxx wallet with passphrase
deeplizard
8 Send Litecoin to Bittrex using Jaxx and monitor confirmations with BlockCypher
Send Litecoin to Bittrex using Jaxx and monitor confirmations with BlockCypher
deeplizard
9 Join a mining pool on Waves platform and lease Waves
Join a mining pool on Waves platform and lease Waves
deeplizard
10 ZCASH Explained | An introduction to a privacy based cryptocurrency
ZCASH Explained | An introduction to a privacy based cryptocurrency
deeplizard
11 ZCash t address creation with Jaxx wallet and private key blockchain discussion
ZCash t address creation with Jaxx wallet and private key blockchain discussion
deeplizard
12 Buy ZCash with Litecoin using the Shifty button in the Jaxx wallet
Buy ZCash with Litecoin using the Shifty button in the Jaxx wallet
deeplizard
13 Buy ZCash with Litecoin using ShapeShift - FAILURE
Buy ZCash with Litecoin using ShapeShift - FAILURE
deeplizard
14 Litecoin | Jaxx | Shapeshift | zcash | failed
Litecoin | Jaxx | Shapeshift | zcash | failed
deeplizard
15 Buy ZCash with Litecoin using ShapeShift - SUCCESS even with Jaxx issues
Buy ZCash with Litecoin using ShapeShift - SUCCESS even with Jaxx issues
deeplizard
16 Explore ZCash blockchain with Zchain block explorer
Explore ZCash blockchain with Zchain block explorer
deeplizard
17 Zchain ZCash block explorer API - PowerShell Code
Zchain ZCash block explorer API - PowerShell Code
deeplizard
18 Zchain ZCash block explorer API - Introduction
Zchain ZCash block explorer API - Introduction
deeplizard
19 Zchain ZCash block explorer API - Application
Zchain ZCash block explorer API - Application
deeplizard
20 Coinbase's Trading Platform | Previously known as GDAX
Coinbase's Trading Platform | Previously known as GDAX
deeplizard
21 Coinbase Social Security Number (SSN) Requirement Explained
Coinbase Social Security Number (SSN) Requirement Explained
deeplizard
22 Who owns Coinbase? Here are some KEY people
Who owns Coinbase? Here are some KEY people
deeplizard
23 How does Coinbase/GDAX secure Bitcoin, Litecoin, Ether?
How does Coinbase/GDAX secure Bitcoin, Litecoin, Ether?
deeplizard
24 Coinbase | HackerOne bug bounty program
Coinbase | HackerOne bug bounty program
deeplizard
25 Is Bitcoin safe at Coinbase/GDAX?
Is Bitcoin safe at Coinbase/GDAX?
deeplizard
26 Coinbase Login Demo Using Google Authenticator (2FA)
Coinbase Login Demo Using Google Authenticator (2FA)
deeplizard
27 Coinbase Pro - GDAX | Trading Interface Overview
Coinbase Pro - GDAX | Trading Interface Overview
deeplizard
28 Coinbase gives $10 in Bitcoin | Watch this before signing up
Coinbase gives $10 in Bitcoin | Watch this before signing up
deeplizard
29 Coinbase around the globe | What countries are supported?
Coinbase around the globe | What countries are supported?
deeplizard
30 Order book explained | Trading concept to know
Order book explained | Trading concept to know
deeplizard
31 Bid/Ask spread explained | Trading concept to know
Bid/Ask spread explained | Trading concept to know
deeplizard
32 Maker vs Taker | Trading concept to know
Maker vs Taker | Trading concept to know
deeplizard
33 Market Orders are Always TAKERS (HIGHER FEES)!
Market Orders are Always TAKERS (HIGHER FEES)!
deeplizard
34 Buy as a MAKER (LOWER FEE) on Coinbase Pro - GDAX | Limit Order - Part 1
Buy as a MAKER (LOWER FEE) on Coinbase Pro - GDAX | Limit Order - Part 1
deeplizard
35 Buy as a MAKER (LOWER FEE) on Coinbase Pro - GDAX | Limit Order - Part 2
Buy as a MAKER (LOWER FEE) on Coinbase Pro - GDAX | Limit Order - Part 2
deeplizard
36 Time-in-force explained | Trading concept to know
Time-in-force explained | Trading concept to know
deeplizard
37 Stop order explained | How to stop a loss | Coinbase Pro - GDAX
Stop order explained | How to stop a loss | Coinbase Pro - GDAX
deeplizard
38 Stop Order on Coinbase Pro - GDAX | What the WARNINGS Mean
Stop Order on Coinbase Pro - GDAX | What the WARNINGS Mean
deeplizard
39 Market price vs Last price | Trading concept to know
Market price vs Last price | Trading concept to know
deeplizard
40 Stop Order on Coinbase Pro - GDAX | How it is ACTIVATED
Stop Order on Coinbase Pro - GDAX | How it is ACTIVATED
deeplizard
41 Stop-limit order | How to set the limit | Coinbase Pro - GDAX
Stop-limit order | How to set the limit | Coinbase Pro - GDAX
deeplizard
42 Flash CRASH Part 1 | ETH/USD currency pair traded at $0.10
Flash CRASH Part 1 | ETH/USD currency pair traded at $0.10
deeplizard
43 Slippage explained | Trading concept to know
Slippage explained | Trading concept to know
deeplizard
44 Flash CRASH Part 2 | How did Coinbase Respond?
Flash CRASH Part 2 | How did Coinbase Respond?
deeplizard
45 Buy side stop-limit order | Crypto trading strategy for buying a breakout
Buy side stop-limit order | Crypto trading strategy for buying a breakout
deeplizard
46 Buy side stop-limit order | Triggering under the market price
Buy side stop-limit order | Triggering under the market price
deeplizard
47 What is an order book?
What is an order book?
deeplizard
48 What is a market?
What is a market?
deeplizard
49 What is an exchange?
What is an exchange?
deeplizard
50 What is a broker-dealer?
What is a broker-dealer?
deeplizard
51 Keras prerequisites
Keras prerequisites
deeplizard
52 Change Keras backend to Theano
Change Keras backend to Theano
deeplizard
53 #1 Order types and parameters | Trading on Coinbase Pro - GDAX
#1 Order types and parameters | Trading on Coinbase Pro - GDAX
deeplizard
54 Trading strategy for stopping a loss | Don't trade all at once!
Trading strategy for stopping a loss | Don't trade all at once!
deeplizard
55 #2 Order matching engine | Trading on Coinbase Pro - GDAX
#2 Order matching engine | Trading on Coinbase Pro - GDAX
deeplizard
56 Batch Size in a Neural Network explained
Batch Size in a Neural Network explained
deeplizard
57 Deep Learning playlist overview & Machine Learning intro
Deep Learning playlist overview & Machine Learning intro
deeplizard
58 Artificial Neural Networks explained
Artificial Neural Networks explained
deeplizard
59 Regularization in a Neural Network explained
Regularization in a Neural Network explained
deeplizard
60 Create confusion matrix for predictions from Keras model
Create confusion matrix for predictions from Keras model
deeplizard

This video teaches how to use a Keras CNN for image classification and prediction, covering the compilation and fitting of the model, creation of test images and labels, and prediction using the model. It also discusses the use of a confusion matrix to evaluate the model's performance.

Key Takeaways
  1. Compile and fit the model with metrics
  2. Create test images and labels
  3. Predict on test images and labels using predict_generator
  4. Create a confusion matrix with scikit-learn's confusion_matrix function
  5. Train the model with fit_generator
💡 The Keras CNN Image Classifier can be used for simple image classification tasks, but may not be suitable for complex tasks without fine-tuning.

Related Reads

Chapters (3)

Welcome to DEEPLIZARD - Go to deeplizard.com for learning resources
0:30 Help deeplizard add video timestamps - See example in the description
8:05 Collective Intelligence and the DEEPLIZARD HIVEMIND
Up next
RNNs Explained in 60 Seconds #ai #coding #machinelearning
Ascent
Watch →