Articles, Blog

Build a TensorFlow Image Classifier in 5 Min

November 8, 2019


Hello world. It’s Siraj. 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 wanted 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 Siraj 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 Siraj’s my hair is a lot. Seriously, it’s never the same. This is where convolutional 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 mates). That’s why we want to use a pre-trained CNN model called Inception. Inception was trained by Google on 100 K 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 is 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 will 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 Tensorflow built in. Just download a Docker toolbox, go through the installation process, and then you can launch your Docker container anytime easily by double-clicking the Docker Quickstart terminal. Cool! Now that we have Docker opened that brings us the step two: installing the Tensorflow Docker image by pasting in this line. It’ll take a few minutes. then once it’s installed we’ll move on to Step 3. Downloading our image dataset to our local machine will stop Docker with control D and create a directory called tf_files /star_wars in our home directory Locally we want to put a folder labeled Darth vader that contains a couple hundred vader pics in here There’s this dope Chrome extension I found called Fatkun 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 got them we’ll just drag that folder into our tf_files/star_wars folder that brings us to step four. Now that we have our images in tf_files directory if we want to link them to our Docker container with this command. Boom! All linked up! Step 5 is to download the training script via Git. Just cd into the Tensorflow directory then run “git pull”. This code will allow us to retrain the inception classifier with a newly linked Darth Vader image dataset. 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 graph 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 this script right from terminal. It’ll take about 30 minutes or so to train our classifier, so do something productive. The Script should output a training accuracy somewhere between 85 and 99 percent when it’s done. And this brings us to our final step. We want to write a script that will use our new retrained classifier to detect if a novel image contains Darth Vader. Where I disrupt ourselves first things first, we’ll import Tensorflow, then we 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 a prediction by feeding the image data into our retrained model to get our prediction output. In order to do this will 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 using 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 a 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 scientist to have. It can be any field of science you’d like. Upload your code to GitHub and then in readme write up a few sentences on how a scientist would use this. Post your repository in the comment section and I’ll judge them based on utility and accuracy. The winner gets a shout-out for me (two videos from now, so in two 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.

100 Comments

  • Reply Rohan Verma September 16, 2016 at 2:06 pm

    Amazing Video. I made a classifier that can be used by scientists to classify whether an image is of a Spiral or an Elliptical Galaxy. https://github.com/rhnvrm/galaxy-image-classifier-tensorflow

  • Reply timothy lampung October 12, 2018 at 1:55 am

    hi siraj, one question.. can i do this without docker?

  • Reply tenzin chokey October 13, 2018 at 4:46 am

    Can anyone help me to configure docker on terminal? I tried using avel$ docker run -it gcr.io/tensorflow/tensorflow:latest-devel but it says command not found

  • Reply Taha Alam October 15, 2018 at 9:48 pm

    how to do it on windows

  • Reply Samrat Mukherjee October 18, 2018 at 6:01 am

    how to do these things in Google CoLab?

  • Reply Ved Sarkar October 18, 2018 at 9:51 am

    any solution to "The name 'DecodeJpeg/contents:0' refers to a Tensor which does not exist" ?

  • Reply LyonOconner October 25, 2018 at 7:36 pm

    no working in 2018? xD

  • Reply Harddisk Kosong October 31, 2018 at 4:09 pm

    im using linux, is there any docker toolbox version for linux? or is there any alternatives? thaks

  • Reply Jiang James November 12, 2018 at 11:51 am

    Do you know a thing to tell pHash rate of an image ?

  • Reply nature1upclose November 13, 2018 at 5:25 am

    Will this technology be used to increse the resolution in Standard definition videos?

  • Reply Waranthorn 5309 November 19, 2018 at 11:57 pm

    paste what? where is the script?

  • Reply ruslan smirnov November 22, 2018 at 4:37 am

    Sometimes you need to watch Siraj in 0.5 speed factor…

  • Reply Ali Murtaza November 23, 2018 at 6:28 pm

    Hi, i am getting "python: can't open file 'tensorflow/examples/image_retraining/retrain.py': [Errno 2] No such file or directory" error in step 6, can somebody help

  • Reply molefe sky Mleya November 28, 2018 at 2:47 am

    hi Siraj, thank you for your great work! I am a beginner and your videos are unfathomably helpful! Keep on doing you and killing it! Love from Africa! Anyway, I am getting this error when I train… python: can't open file 'tensorflow/examples/image_retraining/retrain.py': [Errno 2] No such file or directory, I am using docker on my windows machine and I am using python 2. Where am I going wrong? Where exactly in the code you uploaded on github is the retrain.py file?

  • Reply Norman Sabin December 6, 2018 at 7:23 am

    Why does he not run the "git pull" command?

  • Reply Mustafa Ayyıldız December 9, 2018 at 11:30 am

    I have a problem in this step 4: Link TF image to dataset.

    Mustafa-MacBook-Pro:plasticbottle mustafa$ docker run -it -v $HOME/tf_files:/plasticbottle/gcr.io/tensorflow/tensorflow:latest-devel
    "docker run" requires at least 1 argument.
    See 'docker run –help'.

    Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

    Run a command in a new container

    how can I fix that?

  • Reply Norman Sabin December 9, 2018 at 5:56 pm

    Does he have multiple dockers open or is he just opening and closing the cli?

  • Reply Norman Sabin December 9, 2018 at 6:11 pm

    Also where does this"tensorflow directory" come from??

  • Reply Bhupinder December 11, 2018 at 6:57 pm

    Hello there,
    I consistently follow your channel, and i love it.
    i have a small question, that is recently udacity release about >200 GB of Lincoln MKZ autonomous car research data and my question is, what's the use of those data for open source community and how we can use those for our projects or research or whatever.
    thank you

  • Reply Chun Leong Lee December 17, 2018 at 9:23 am

    I have tried to install the docker toolkit but having issues trying to run up in my win 10. Is it possible to run the script straight in a Ubuntu VM environment?

  • Reply Aritz Otxotorena December 22, 2018 at 1:19 pm

    Too fast, nothing good to say.

  • Reply panagranit pana December 29, 2018 at 5:05 pm

    How can you use Collabs to do this?? as this requires Docker which if you do not have Windows pro you have to go into BIOs etc. Seems like Collabs would be now easier!

  • Reply Waranthorn 5309 January 2, 2019 at 1:17 pm

    install what?

  • Reply anonymous January 5, 2019 at 4:21 pm

    which algorithm will be suitable for feature extraction of the home window also
    from where I can get the dataset

  • Reply Ahmed Alhisaie January 8, 2019 at 3:57 am

    very fast, very compact and full of information, thanks a lot

  • Reply sgg17003 January 8, 2019 at 5:52 pm

    It's good to know how "productive" I can be.

  • Reply Pinku Ki Pathshala January 8, 2019 at 7:30 pm

    Top free resources to learn AI Machine learning and data science..
    https://youtu.be/D2SZO5inoiM

  • Reply Kwoods January 12, 2019 at 7:50 pm

    I still can’t even figure out if I installed python correctly yet 😭😭😭😭😭😭😭😭😭😭

  • Reply ProNoob Gaming January 15, 2019 at 5:29 pm

    I have a doubt! Once all the training part and stuff is done, can we link this classifier to any platform like a website or an app and use it?

  • Reply Mr. Schäeffer January 18, 2019 at 3:16 pm

    Why the title is in my language? The YouTube are translating titles and descriptions?🤔

  • Reply Edwin Garcia January 19, 2019 at 4:30 am

    speak slowly please 😵

  • Reply A Guy from 90's January 28, 2019 at 2:56 pm

    Don't forget to do something productive once in while…stay healthy guyzz..

  • Reply Emi Moldovan February 6, 2019 at 4:57 am

    Can you help me, I really need your help?

  • Reply Joey Wolfs February 7, 2019 at 10:28 am

    I am trying to follow this guid, but Dockers has changed their GUI and i am having some problems with finding or putting images in my TF_files…
    I have used
    C:UsersusernameDownloadshamers_google .
    UsersusernameDownloadshamers_google .
    /Users/username/Downloads/hamers_google .
    Also tried with other names but everytime i get the error:

    mv: cannot stat 'UsersusernameDownloadshamers_google' : No such file or directory

    But the images are in that folder idk what to do..

  • Reply ROSHAN ZAMEER February 9, 2019 at 4:44 pm

    hey I have got a project on handwritten image detection using deep learning do you have any useful links. I am new to machine learning

  • Reply شهران بهران February 15, 2019 at 4:30 pm

    Send my wats app 00967716797422

  • Reply Raducki Frédéric February 18, 2019 at 1:57 pm

    Hello. at 03:16 we must copy the files to the specific folder. how do you manage to access the image files on windows ? how to reach for example C:vaderimages and copy these to the docker "virtual" folders.

  • Reply samartha siddhartha February 20, 2019 at 10:22 am

    Watch this a step further to this
    https://youtu.be/-QIqsgsg7XQ

  • Reply As'hat Uzbekov February 21, 2019 at 8:12 pm

    Thanks, good job!! the retrain.py missing in the master branch.

  • Reply Saman Dhindsa February 22, 2019 at 11:26 pm

    Hey can u plz give ur gmail id??..I have a project to make Machine learning model. Regarding that i need ur help..

  • Reply Alex Acosta February 25, 2019 at 7:45 pm

    I left half way… had to back to work, back to reality😬

  • Reply Pp jp March 4, 2019 at 6:56 am

    This VDO is short but very unfriendly to beginners like me.
    Just flooding with codes that seem not working.

  • Reply Nigel Higgs March 4, 2019 at 10:54 pm

    When he is training the model, do you need TensorFlow gpu installed to make training fast or could this be done on a typical cpu laptop also?

  • Reply Marc Hasenkopf March 8, 2019 at 1:09 pm

    By pasting in THIS line. Siraj why didn't you put it in the description ????? ;(

  • Reply apoorv mittal March 8, 2019 at 6:15 pm

    you forgot to to tell to get the tf_files/inception file

  • Reply Haris Hassan March 9, 2019 at 12:26 pm

    bro i want to train classifier for vehical catogories like truck car etc. and I have no Idea in this domain how can I get started? please help

  • Reply yuvan bajjurla March 10, 2019 at 7:52 am

    what happens if we provide an image which is not present in the dataset and it it belongs to some other type

  • Reply Rayan Fernandes March 12, 2019 at 6:46 pm

    dude your first funny video?

  • Reply Pratik Mehta March 13, 2019 at 5:04 am

    Fatkun batch download does not work!

  • Reply Sainath Singineedi March 15, 2019 at 7:18 pm

    What the hell?

  • Reply Space Science March 24, 2019 at 10:16 pm

    0:05 Green screen error black at background

  • Reply Syed Mannawar Hussain March 26, 2019 at 8:40 am

    Dear Siraj,

    what is the difference between tensorflow from GPU perspective and normal cpu ?

    what difference will that lead to from learning or through working prspective?

    I have anaconda installed with python 3.7
    Regards,

    Mannawar

  • Reply Tejas Joshi March 27, 2019 at 12:21 pm

    How will I get 'tflite' file from .pb and labels ???????????????

  • Reply Toloy Keripin Munsang April 1, 2019 at 7:20 am

    anyone have a github link for freshwater fish species identification via photos?

  • Reply Vishal Salunkhe April 9, 2019 at 8:52 am

    Hello Guys, I am working on a project where I need to work on colour intensity measurement. Is there any way that one can use tensor flow for the same?

  • Reply Milan Harkhani April 19, 2019 at 6:00 am

    chocolate meme was awesome …can't stop laughing ..lol

  • Reply Umesh Verma April 19, 2019 at 2:39 pm

    bro waste video what is the fun of making a 5 min video if no one is able understand single step from hiding docker toolbox URL to pasting commands

  • Reply Michael Richardson April 25, 2019 at 8:47 pm

    For those of you expressing concerns that this was not beginner friendly, please understand that the video was 5 minutes. I have spent days on any one of these topics just to be comfortable enough to move around reasonably and this video assumes a fair bit of knowledge about docker at the least and computer vision, labeling and confidence factors are all used here as well. If you're not comfortable with the ladder and can't read python, it would be hard to understand what's going on here but here's my breakdown.

  • Reply akouaydi hanen April 26, 2019 at 4:15 pm

    Hello, I try to use this tutorial but I have (x,y,z) file not image I build .pb model but the predicted class is wrong??
    Can you help me ?? plz

  • Reply ahmad annazili April 28, 2019 at 12:39 pm

    Kenapa judulnya jadi bahasa indo 😬

  • Reply 1 Billion Hearts May 4, 2019 at 3:26 pm

    are you indian?? or what actually you looks like indian.

  • Reply preeti patil May 7, 2019 at 11:55 am

    Do a video on Google landmark recognition using python code in MATLAB

  • Reply RompotMechanikos May 14, 2019 at 11:43 pm

    but how do I get it to sort my porn?

  • Reply Skynse May 17, 2019 at 11:26 pm

    Im starting to feel like I should use this instead of opencv classifiers

  • Reply celasvictoria360 May 19, 2019 at 5:39 pm

    How to keep training the same model? Because for me it looks like it just scraps one for another.

  • Reply Aci May 22, 2019 at 4:15 pm

    Okay but how do I install Docker on a Raspberry pi…..?

  • Reply Aci May 22, 2019 at 4:17 pm

    Can you like put the link to the download pages in the descriptioon?

  • Reply hrkhaff May 29, 2019 at 4:03 pm

    No Thanks! I will NEVER take Mac developers seriously!

  • Reply pjossy joshi June 1, 2019 at 8:33 am

    Hello sir , is this python ?

  • Reply C S June 2, 2019 at 3:14 pm

    Oh yes, fat cunt bitch, a wonderful extension

  • Reply Karan Singh June 4, 2019 at 5:00 pm

    How to get , rectangle around object

  • Reply Hector Zayas June 10, 2019 at 12:20 pm

    Thanks , How would use this to classify all characters in star world ? Just create new file images for each character? And? Thanks

  • Reply Chaitra Nagabhushan June 17, 2019 at 7:45 am

    I am unable to link tf image to the data file

  • Reply Outcasted Gaming June 19, 2019 at 12:40 pm

    I am going to make it classify dinos from ark for my ai project

  • Reply Bhupinder June 28, 2019 at 6:05 pm

    hi, siraj
    is there a way to use or deploy object or image recognition on in a video clip or in a movie for images recognition or object recognition or characters recognition in a video etc, rather than using real-time object detection via webcam, etc. please reply

  • Reply Aditya Rawat July 1, 2019 at 8:39 pm

    I was creating my own CNN and unfortunately I don't have a GPU.
    I had about 1700 images of 2 types of pokemons (pikachu and balbasaur) and during the training of the model my computer froze.
    So i guess you just can't create your own CNN from scratch

  • Reply Michael Corleone July 6, 2019 at 10:17 pm

    People with no mathematical background, weak programming background starts to learn ML watching these types of videos and leaves after a few days. No wonder why. What is this bullshit? This is exactly why NO ONE can be SIR ANDREW NG. These faggots are disrespecting him by their utter existence.

  • Reply Steve Ryan July 14, 2019 at 1:16 am

    This would be much better without requiring Docker, since many of us have working tensorflow and python environments set up some other way. Running "git pull" with no arguments or other repository specification is just weird. Where is it?

  • Reply AndroHub July 17, 2019 at 8:26 pm

    Grt video. I wanna know how to classify the diseased leaf from dataset using tensorflow

  • Reply teimoor bahrami July 18, 2019 at 11:03 am

    can you explain lstm bro

  • Reply Karan Singh July 18, 2019 at 8:41 pm

    What is its speed

  • Reply Aniruddha Sarkar July 19, 2019 at 5:24 pm

    all that i well learned is how to fart hard(joking)

  • Reply Ana Rita Gomes July 23, 2019 at 7:19 pm

    Hello, Siraj. I am very enthusiastic about your work. I am a software engineer, and currently I am trying to learn Tensorflow. I honestly don't know where the retrain.py file is. In the directory tensorflow_src/tensorflow/examples/image_retraining I only have a Readme. Can anyone help, please? Thank you.

  • Reply BaiShaira Mangulamas July 24, 2019 at 8:41 pm

    Hello siraj I cant go to cd tensorflow it said that error no such file or directory plss help….even i already install the tensorflow

  • Reply BaiShaira Mangulamas July 24, 2019 at 8:42 pm

    Hello siraj I cant go to cd tensorflow it said that error no such file or directory plss help..even that i already install the tensorflow plss help..

  • Reply Xav Dest August 6, 2019 at 11:27 pm

    Gonna create one for paycheques

  • Reply Amogh Mulge August 10, 2019 at 10:06 am

    what was the link you pasted in step2 ????bro

  • Reply Freddy B August 10, 2019 at 1:43 pm

    Got a problem in step 4. I'm running Docker on windows. I'm writing my corresponding paths, but I still get Unable to find image xxxx locally. What to do?

  • Reply Alexander M August 19, 2019 at 2:17 am

    Get this working (as of August 2019):

    #yes, it's not clean, but this was done for school so it was crunch and move on

    #as per video, create ~/tf_files/
    #then create ~/tf_files/images & ~/tf_files/guess/
    #when you pull images from google (need at least two image classes), put each differentiating directory in the ~/tf_files/images/ directory
    #when you want to run the classifier, put a file named guess.jpg in the ~/tf_files/guess/ directory

    #in terminal, run:
    sudo docker run -it -v $HOME/tf_files:/tf_files/ tensorflow/tensorflow:latest-devel
    #then run:

    cd /tensorflow_src
    git pull

    #change to example images folder and download retrain script
    cd tensorflow/examples/image_retraining
    curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/r1.1/tensorflow/examples/image_retraining/retrain.py

    #upgrade to get tf_upgrade_v2 script
    pip install –upgrade –ignore-installed tensorflow
    tf_upgrade_v2
    –intree .
    –outtree ../image_retraining_v2
    –reportfile report.txt

    #change to updated retraining script and run
    cd /tensorflow_src/tensorflow/examples/image_retraining_v2
    python retrain.py
    –bottleneck_dir=/tf_files/bottlenecks
    –how_many_training_steps 500
    –model_dir=/tf_files/inception
    –output_graph=/tf_files/retrained_graph.pb
    –output_labels=/tf_files/retrained_labels.txt
    –image_dir /tf_files/images

    #clone classifier repo
    cd /
    git clone https://github.com/llSourcell/tensorflow_image_classifier.git

    #run classifier tf_files
    python /tensorflow_image_classifier/src/label_image.py /tf_files/guess/guess.jpg

  • Reply georgi5000 August 21, 2019 at 10:01 am

    don't do fart jokes, just don't

  • Reply pradeep kumar September 2, 2019 at 3:38 pm

    Hai siraj ,I have created a image classification with two class but I am having want to create a model with multiple class for eg 6 class

  • Reply Yu Cao September 15, 2019 at 4:34 am

    Very impressive!

    Our company is looking for AI Robotics Engineer working in Tokyo, Japan.
    If you’re interested, please email [email protected]

    https://connected-robotics.com/

  • Reply חי September 23, 2019 at 1:45 pm

    I can't move the folder where I downloaded the photos to the docker.
    He writes to me: mv: cannot stat 'C: fff': No such file or directory

  • Reply danf10s September 24, 2019 at 3:56 pm

    Can anyone help me create a classifier that can tell me if there is a puppy on a image? I need on because of errmmm… reasons…

  • Reply Jarvis Minsk October 2, 2019 at 11:12 pm

    I strongly tried to follow along. I keep seeing an import error: "No module named Tensorflow", even after i've downloaded tensorflow and tensorflow_hub. Anyone help?

  • Reply D4n13L M4th3w October 3, 2019 at 12:20 am

    what line did he paste in?

  • Reply Jannatul Ferdos October 5, 2019 at 6:21 pm

    how to open an account in docker? What is reCAPTCHA?

  • Reply George Odecuas October 13, 2019 at 8:15 pm

    I cant change directory into tensorflow 3:26

  • Reply Jean Cute October 20, 2019 at 2:36 am

    Great. Thank you. Can you also show a tutorial on how to identify an object from a live feed video and automatically save those object detected in the form? And is it also be integrated in powerapps? Thanks again

  • Reply André Barbosa November 1, 2019 at 3:19 pm

    What about a classifier that identifies plagiarized articles? Oh, sorry, someone has already done that, and actually found a near perfect match with your neural qubit bullshit….

  • Leave a Reply