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.

Build a TensorFlow Image Classifier in 5 Min

100 thoughts on “Build a TensorFlow Image Classifier in 5 Min

  • September 16, 2016 at 2:06 pm
    Permalink

    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
  • October 12, 2018 at 1:55 am
    Permalink

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

    Reply
  • October 13, 2018 at 4:46 am
    Permalink

    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
  • October 15, 2018 at 9:48 pm
    Permalink

    how to do it on windows

    Reply
  • October 18, 2018 at 6:01 am
    Permalink

    how to do these things in Google CoLab?

    Reply
  • October 18, 2018 at 9:51 am
    Permalink

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

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

    no working in 2018? xD

    Reply
  • October 31, 2018 at 4:09 pm
    Permalink

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

    Reply
  • November 12, 2018 at 11:51 am
    Permalink

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

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

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

    Reply
  • November 19, 2018 at 11:57 pm
    Permalink

    paste what? where is the script?

    Reply
  • November 22, 2018 at 4:37 am
    Permalink

    Sometimes you need to watch Siraj in 0.5 speed factor…

    Reply
  • November 23, 2018 at 6:28 pm
    Permalink

    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
  • November 28, 2018 at 2:47 am
    Permalink

    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
  • December 6, 2018 at 7:23 am
    Permalink

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

    Reply
  • December 9, 2018 at 11:30 am
    Permalink

    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
  • December 9, 2018 at 5:56 pm
    Permalink

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

    Reply
  • December 9, 2018 at 6:11 pm
    Permalink

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

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

    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
  • December 17, 2018 at 9:23 am
    Permalink

    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
  • December 22, 2018 at 1:19 pm
    Permalink

    Too fast, nothing good to say.

    Reply
  • December 29, 2018 at 5:05 pm
    Permalink

    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
  • January 2, 2019 at 1:17 pm
    Permalink

    install what?

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

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

    Reply
  • January 8, 2019 at 3:57 am
    Permalink

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

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

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

    Reply
  • January 8, 2019 at 7:30 pm
    Permalink

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

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

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

    Reply
  • January 15, 2019 at 5:29 pm
    Permalink

    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
  • January 18, 2019 at 3:16 pm
    Permalink

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

    Reply
  • January 19, 2019 at 4:30 am
    Permalink

    speak slowly please 😵

    Reply
  • January 28, 2019 at 2:56 pm
    Permalink

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

    Reply
  • February 6, 2019 at 4:57 am
    Permalink

    Can you help me, I really need your help?

    Reply
  • February 7, 2019 at 10:28 am
    Permalink

    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
  • February 9, 2019 at 4:44 pm
    Permalink

    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
    Permalink

    Send my wats app 00967716797422

    Reply
  • February 18, 2019 at 1:57 pm
    Permalink

    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
  • February 20, 2019 at 10:22 am
    Permalink

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

    Reply
  • February 21, 2019 at 8:12 pm
    Permalink

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

    Reply
  • February 22, 2019 at 11:26 pm
    Permalink

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

    Reply
  • February 25, 2019 at 7:45 pm
    Permalink

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

    Reply
  • March 4, 2019 at 6:56 am
    Permalink

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

    Reply
  • March 4, 2019 at 10:54 pm
    Permalink

    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
  • March 8, 2019 at 1:09 pm
    Permalink

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

    Reply
  • March 8, 2019 at 6:15 pm
    Permalink

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

    Reply
  • March 9, 2019 at 12:26 pm
    Permalink

    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
  • March 10, 2019 at 7:52 am
    Permalink

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

    Reply
  • March 12, 2019 at 6:46 pm
    Permalink

    dude your first funny video?

    Reply
  • March 13, 2019 at 5:04 am
    Permalink

    Fatkun batch download does not work!

    Reply
  • March 15, 2019 at 7:18 pm
    Permalink

    What the hell?

    Reply
  • March 24, 2019 at 10:16 pm
    Permalink

    0:05 Green screen error black at background

    Reply
  • March 26, 2019 at 8:40 am
    Permalink

    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
  • March 27, 2019 at 12:21 pm
    Permalink

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

    Reply
  • April 1, 2019 at 7:20 am
    Permalink

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

    Reply
  • April 9, 2019 at 8:52 am
    Permalink

    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
  • April 19, 2019 at 6:00 am
    Permalink

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

    Reply
  • April 19, 2019 at 2:39 pm
    Permalink

    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
  • April 25, 2019 at 8:47 pm
    Permalink

    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
  • April 26, 2019 at 4:15 pm
    Permalink

    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
  • April 28, 2019 at 12:39 pm
    Permalink

    Kenapa judulnya jadi bahasa indo 😬

    Reply
  • May 4, 2019 at 3:26 pm
    Permalink

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

    Reply
  • May 7, 2019 at 11:55 am
    Permalink

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

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

    but how do I get it to sort my porn?

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

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

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

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

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

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

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

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

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

    No Thanks! I will NEVER take Mac developers seriously!

    Reply
  • June 1, 2019 at 8:33 am
    Permalink

    Hello sir , is this python ?

    Reply
  • June 2, 2019 at 3:14 pm
    Permalink

    Oh yes, fat cunt bitch, a wonderful extension

    Reply
  • June 4, 2019 at 5:00 pm
    Permalink

    How to get , rectangle around object

    Reply
  • June 10, 2019 at 12:20 pm
    Permalink

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

    Reply
  • June 17, 2019 at 7:45 am
    Permalink

    I am unable to link tf image to the data file

    Reply
  • June 19, 2019 at 12:40 pm
    Permalink

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

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

    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
  • July 1, 2019 at 8:39 pm
    Permalink

    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
  • July 6, 2019 at 10:17 pm
    Permalink

    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
  • July 14, 2019 at 1:16 am
    Permalink

    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
  • July 17, 2019 at 8:26 pm
    Permalink

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

    Reply
  • July 18, 2019 at 11:03 am
    Permalink

    can you explain lstm bro

    Reply
  • July 18, 2019 at 8:41 pm
    Permalink

    What is its speed

    Reply
  • July 19, 2019 at 5:24 pm
    Permalink

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

    Reply
  • July 23, 2019 at 7:19 pm
    Permalink

    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
  • July 24, 2019 at 8:41 pm
    Permalink

    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
  • July 24, 2019 at 8:42 pm
    Permalink

    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
  • August 6, 2019 at 11:27 pm
    Permalink

    Gonna create one for paycheques

    Reply
  • August 10, 2019 at 10:06 am
    Permalink

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

    Reply
  • August 10, 2019 at 1:43 pm
    Permalink

    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
  • August 19, 2019 at 2:17 am
    Permalink

    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
  • August 21, 2019 at 10:01 am
    Permalink

    don't do fart jokes, just don't

    Reply
  • September 2, 2019 at 3:38 pm
    Permalink

    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
  • September 15, 2019 at 4:34 am
    Permalink

    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
    Permalink

    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
  • September 24, 2019 at 3:56 pm
    Permalink

    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
  • October 2, 2019 at 11:12 pm
    Permalink

    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
  • October 3, 2019 at 12:20 am
    Permalink

    what line did he paste in?

    Reply
  • October 5, 2019 at 6:21 pm
    Permalink

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

    Reply
  • October 13, 2019 at 8:15 pm
    Permalink

    I cant change directory into tensorflow 3:26

    Reply
  • October 20, 2019 at 2:36 am
    Permalink

    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
  • November 1, 2019 at 3:19 pm
    Permalink

    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….

    Reply

Leave a Reply

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