Build a TensorFlow Image Classifier in 5 Min

Siraj Raval · Intermediate ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

This video demonstrates how to build a TensorFlow image classifier in 5 minutes to detect Darth Vader images using transfer learning and a pre-trained Inception model. The process involves installing Docker, downloading the TensorFlow Docker image, preparing the image dataset, linking the images to the Docker container, downloading the training script, retraining the Inception classifier, and writing a script to use the retrained classifier for image detection.

Full Transcript

hello world it's SJ in this episode we're going to build an image classifier using tensorflow in 30 lines of python and I don't mean a classifier that can detect handwritten digits or Iris Flowers I'm talking literally anything you want you'll be able to train this thing to classify chocolate if you want it [Music] h the possibilities are endless there's so many industries that can be disrupted by just this simple solution a Japanese cucumber farmer built a machine to detect whether each of his cucumbers was one of nine different types using this thing let's say we want to build a sage classifier if we think about this problem in the traditional programming Paradigm we want to handcraft a bunch of features maybe we could do some Edge detection to save the shape of my hair or use a color histogram to save the color of my teeth the problem with that is there's so much variance in sjas my hair is alive seriously it's never the same this is where a comp evolutional neural networks come into play they're essentially a black box that constructs features that we would otherwise have to handcraft and these abstract features they create from training are so generalized that they account for variance if we wanted to train a CNN ourselves we need a lot of computing power and a lot of time both of which we don't have I don't even have time to do my dishes sorry remits that's why we want to use a pre-trained CNN model called Inception Inception was trained by Google on 100K images with a thousand categories our use case in this video will be classifying Darth Vader pictures but Inception wasn't trained on Vader so we're going to perform a process called transfer learning that means applying the learnings from a previous training session to a new training session if we look at the Inception model we can see that when we feed in an image as an input at each layer it will perform a series of operations on that data until it outputs a label and classification percentage each layer is a different set of abstractions in the first layers it's basically taught itself Edge detection then shape detection in the middle layers and they get increasingly more abstract up until the end if we look at the last few layers these are the highest level detectors for whole objects for transfer learning we basically just want to retrain that last layer on features of Darth Vader so it can add a representation of him to its repository of knowledge so this is going to be a seven-step process and we're going to go through each step in order sound good step one is to install Docker which is a tool for creating a virtual container on your machine for running apps the benefit of Docker is you don't have to install any dependencies on your machine so we'll eventually download a Docker image that has all the necessary dependencies for tensor flow built in just download the docker toolbox go through the installation process and then you can launch your Docker container anytime easily by double clicking the docker quickart terminal cool now that we have Docker open that brings us to step two installing the tensorflow docker image by pasing in this line it'll take a few minutes then once it's installed we'll move on to step three downloading our image data set to our local machine we'll stop Docker with contr d then create a directory called TF file Star Wars in our home directory locally we want to put a folder labeled Darth Vader that contains a couple hundred Vader p Pi in here there's this dope Chrome extension I found called faton batch download image that bulk downloads all the images from your Google image search results just go to Google image search type in Darth Vader and start downloading all of those images once we've got them we'll just drag that folder into our TF file Star Wars folder that brings us to step four now that we have our images in our TF files directory we want to link them to our Docker container with this command boom all Linked UP step five is to download the training script via git just CD into the tensorflow directory then run git hole this code will allow us to retrain the Inception classifier with a newly linked Darth Vader image data set step six is the actual retraining part the bottleneck directory will be used to Cache the outputs of the lower layers on disk so they don't have to repeatedly be recalculated we'll run this example for 500 iterations the next flag asks where to store our trained model our output craft which we can later view in tensor board our output labels which will be the same as our training data folder name and the image directory where we stored our Vader images let's go ahead and run the script right from terminal it'll take about 30 minutes so to train our classifier so do something productive the script should output a training accuracy somewhere between 85 and 99% when it's done and this brings us to our final step we want to write a script that will use our new retrain classifier to detect if a novel image contains Darth Vader we'll write the script ourselves first things first we'll import tensorflow then we'll want to create a variable to store the user input image path we'll create another variable to store the data from that image and one more to load the label of that image from the label file next we'll want to grab our model from the saved retrained graph file store it in the graph def variable and parse it now that we have our image and model ready it's time to make our prediction by feeding the image data into our retrained model to get our prediction output in order to do this we'll create a tensorflow session this will give us an environment to perform operations on our tensor data in the first thing we'll do in a session is get our softmax function tensor from the last layer of our model the softmax function is used in the final layer to map input data into probabilities of an expected output we will execute our softmax tensor function on our input image data via the session run function it will output our predictions as an array we'll next want to sort our prediction labels in order of confidence and lastly for every prediction we have we can get the predicted label and the score and print it out to terminal let's take the script and run it on one of our Vader pictures the result is pretty good tensorflow makes it much easier to classify an image and I've got a challenge for you guys on this episode The Challenge is to create a classifier that you think would be a useful tool for scientists to have it can be any field of science you'd like upload your code to GitHub and in the read me write up a few sentences on how a scientist would use this post a repository in the comment section and I'll judge them based on utility and accuracy the winner gets a shout out from me two videos from now so in 2 weeks and I'll also send you a free signed copy of my book decentralized applications for now I've got to go not buy the iPhone 7 so thanks for watching

Original Description

In this episode we're going to train our own image classifier to detect Darth Vader images. The code for this repository is here: https://github.com/llSourcell/tensorflow_image_classifier I created a Slack channel for us, sign up here: https://wizards.herokuapp.com/ The Challenge: The challenge for this episode is to create your own Image Classifier that would be a useful tool for scientists. Just post a clone of this repo that includes your retrained Inception Model (label it output_graph.pb). If it's too big for GitHub, just upload it to DropBox and post the link in your GitHub README. I'm going to judge all of them and the winner gets a shoutout from me in a future video, as well as a signed copy of my book 'Decentralized Applications'. This CodeLab by Google is super useful in learning this stuff: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/?utm_campaign=chrome_series_machinelearning_063016&utm_source=gdev&utm_medium=yt-desc#0 This Tutorial by Google is also very useful: https://www.tensorflow.org/versions/r0.9/how_tos/image_retraining/index.html This is a good informational video: https://www.youtube.com/watch?v=VpDonQAKtE4 Really deep dive video on CNNs: https://www.youtube.com/watch?v=FmpDIaiMIeA I love you guys! Thanks for watching my videos and if you've found any of them useful I'd love your support on Patreon: https://www.patreon.com/user?u=3191693 Much more to come so please SUBSCRIBE, LIKE, and COMMENT! :) edit: Credit to Clarifai for the first conv net diagram in the video Follow me: Twitter: https://twitter.com/sirajraval Facebook: https://www.facebook.com/sirajology Instagram: https://www.instagram.com/sirajraval/ Instagram: https://www.instagram.com/sirajraval/ Signup for my newsletter for exciting updates in the field of AI: https://goo.gl/FZzJ5w Hit the Join button above to sign up to become a member of my channel for access to exclusive content! Join my AI community: http://chatgptschool.io/ Sign up for
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Siraj Raval · Siraj Raval · 34 of 60

1 What is Bitcoin?
What is Bitcoin?
Siraj Raval
2 5 Ways to Use Bitcoin
5 Ways to Use Bitcoin
Siraj Raval
3 BTC Fever - Siraj [Music Video]
BTC Fever - Siraj [Music Video]
Siraj Raval
4 5 Reasons to Build Decentralized Apps
5 Reasons to Build Decentralized Apps
Siraj Raval
5 The Interplanetary File System
The Interplanetary File System
Siraj Raval
6 How to Build a Dapp in 3 min
How to Build a Dapp in 3 min
Siraj Raval
7 Life Before Smartphones
Life Before Smartphones
Siraj Raval
8 4 Ways to Use Smart Contracts
4 Ways to Use Smart Contracts
Siraj Raval
9 3 Dapps You HAVE to See
3 Dapps You HAVE to See
Siraj Raval
10 Char's Life as a BitTorrent Engineer
Char's Life as a BitTorrent Engineer
Siraj Raval
11 4 Reasons AlphaGo is a Huge Deal
4 Reasons AlphaGo is a Huge Deal
Siraj Raval
12 Build a Neural Net in 4 Minutes
Build a Neural Net in 4 Minutes
Siraj Raval
13 Sentiment Analysis in 4 Minutes
Sentiment Analysis in 4 Minutes
Siraj Raval
14 The Hackathon Life
The Hackathon Life
Siraj Raval
15 Your First ML App - Machine Learning for Hackers #1
Your First ML App - Machine Learning for Hackers #1
Siraj Raval
16 Build an AI Composer - Machine Learning for Hackers #2
Build an AI Composer - Machine Learning for Hackers #2
Siraj Raval
17 Build a Game AI - Machine Learning for Hackers #3
Build a Game AI - Machine Learning for Hackers #3
Siraj Raval
18 Build a Movie Recommender - Machine Learning for Hackers #4
Build a Movie Recommender - Machine Learning for Hackers #4
Siraj Raval
19 Build an AI Artist - Machine Learning for Hackers #5
Build an AI Artist - Machine Learning for Hackers #5
Siraj Raval
20 Build a Chatbot - ML for Hackers #6
Build a Chatbot - ML for Hackers #6
Siraj Raval
21 Build an AI Reader - Machine Learning for Hackers #7
Build an AI Reader - Machine Learning for Hackers #7
Siraj Raval
22 Build an AI Writer - Machine Learning for Hackers #8
Build an AI Writer - Machine Learning for Hackers #8
Siraj Raval
23 Build a Chatbot w/ an API - ML for Hackers #9
Build a Chatbot w/ an API - ML for Hackers #9
Siraj Raval
24 One-Shot Learning - Fresh Machine Learning #1
One-Shot Learning - Fresh Machine Learning #1
Siraj Raval
25 Generative Adversarial Nets - Fresh Machine Learning #2
Generative Adversarial Nets - Fresh Machine Learning #2
Siraj Raval
26 Tone Analysis - Fresh Machine Learning #3
Tone Analysis - Fresh Machine Learning #3
Siraj Raval
27 Generate Rap Lyrics - Fresh Machine Learning #4
Generate Rap Lyrics - Fresh Machine Learning #4
Siraj Raval
28 Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Build an Autoencoder in 5 Min - Fresh Machine Learning #5
Siraj Raval
29 Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Build a Self Driving Car in 5 Min - Fresh Machine Learning #6
Siraj Raval
30 Build an Antivirus in 5 Min - Fresh Machine Learning #7
Build an Antivirus in 5 Min - Fresh Machine Learning #7
Siraj Raval
31 TensorFlow in 5 Minutes (tutorial)
TensorFlow in 5 Minutes (tutorial)
Siraj Raval
32 Build a Recurrent Neural Net in 5 Min
Build a Recurrent Neural Net in 5 Min
Siraj Raval
33 Build a Simulation in 5 Min
Build a Simulation in 5 Min
Siraj Raval
Build a TensorFlow Image Classifier in 5 Min
Build a TensorFlow Image Classifier in 5 Min
Siraj Raval
35 Tensorboard Explained in 5 Min
Tensorboard Explained in 5 Min
Siraj Raval
36 Generate Music in TensorFlow
Generate Music in TensorFlow
Siraj Raval
37 Build a Game Bot (LIVE)
Build a Game Bot (LIVE)
Siraj Raval
38 Deep Learning Frameworks Compared
Deep Learning Frameworks Compared
Siraj Raval
39 Introduction - Learn Python for Data Science #1
Introduction - Learn Python for Data Science #1
Siraj Raval
40 Build a Neural Network (LIVE)
Build a Neural Network (LIVE)
Siraj Raval
41 Twitter Sentiment Analysis - Learn Python for Data Science #2
Twitter Sentiment Analysis - Learn Python for Data Science #2
Siraj Raval
42 Recommendation Systems - Learn Python for Data Science #3
Recommendation Systems - Learn Python for Data Science #3
Siraj Raval
43 Predicting Stock Prices - Learn Python for Data Science #4
Predicting Stock Prices - Learn Python for Data Science #4
Siraj Raval
44 Pong Neural Network (LIVE)
Pong Neural Network (LIVE)
Siraj Raval
45 Deep Dream in TensorFlow - Learn Python for Data Science #5
Deep Dream in TensorFlow - Learn Python for Data Science #5
Siraj Raval
46 Visualizing Data with D3.js (LIVE)
Visualizing Data with D3.js (LIVE)
Siraj Raval
47 Genetic Algorithms - Learn Python for Data Science #6
Genetic Algorithms - Learn Python for Data Science #6
Siraj Raval
48 Enter Siraj [Music Video]
Enter Siraj [Music Video]
Siraj Raval
49 Build a Web Scraper (LIVE)
Build a Web Scraper (LIVE)
Siraj Raval
50 Why is P vs NP Important?
Why is P vs NP Important?
Siraj Raval
51 How to Make a Neural Network (LIVE)
How to Make a Neural Network (LIVE)
Siraj Raval
52 How to Make an Amazing Tensorflow Chatbot Easily
How to Make an Amazing Tensorflow Chatbot Easily
Siraj Raval
53 How to Make an Amazing Video Game Bot Easily
How to Make an Amazing Video Game Bot Easily
Siraj Raval
54 How to Make a Tensorflow Neural Network (LIVE)
How to Make a Tensorflow Neural Network (LIVE)
Siraj Raval
55 How to Make a Simple Tensorflow Speech Recognizer
How to Make a Simple Tensorflow Speech Recognizer
Siraj Raval
56 Joel Shor - Really Quick Questions with an Awesome Google Engineer
Joel Shor - Really Quick Questions with an Awesome Google Engineer
Siraj Raval
57 How to Make a Path Planning Algorithm Easily (LIVE)
How to Make a Path Planning Algorithm Easily (LIVE)
Siraj Raval
58 The Best Way to Prepare a Dataset Easily
The Best Way to Prepare a Dataset Easily
Siraj Raval
59 Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Catherine Olsson - Really Quick Questions with an OpenAI Engineer
Siraj Raval
60 How to Make a Tic Tac Toe Neural Network Easily (LIVE)
How to Make a Tic Tac Toe Neural Network Easily (LIVE)
Siraj Raval

This video teaches how to build a TensorFlow image classifier using transfer learning and a pre-trained Inception model. The process involves several steps, including installing Docker, preparing the image dataset, and retraining the classifier. The resulting model can be used to detect objects in images.

Key Takeaways
  1. Install Docker
  2. Download the TensorFlow Docker image
  3. Prepare the image dataset
  4. Link the images to the Docker container
  5. Download the training script
  6. Retrain the Inception classifier
  7. Write a script to use the retrained classifier for image detection
💡 Transfer learning can be used to adapt a pre-trained model to a new task, reducing the need for large amounts of training data and computational resources.

Related Reads

📰
AI Tools that Actually Pay You Back: A Developer's Guide to Monetizing AI
Learn how to monetize AI tools as a developer and earn revenue through profitable applications
Dev.to AI
📰
Creativity AI #82: Anthropic maps how people really use AI, designers shift from making to mending…
Explore how people interact with AI and the shift in design from making to mending, and learn to apply these concepts in your own work
Medium · AI
📰
The End of YouTube Search? Why AI Creator Discovery Is Becoming the Smarter Way to Learn in 2026
AI creator discovery is becoming a smarter way to learn, shifting focus from video content to creator expertise
Medium · AI
📰
Why AI Tools Are Becoming Essential for Modern Professionals
Learn how AI tools are revolutionizing everyday work for modern professionals, increasing productivity and efficiency
Medium · AI
Up next
I Built a Live Dashboard With Claude - Zero Coding, Zero IT Skills
Nicolas Boucher
Watch →