Build a TensorFlow Image Classifier in 5 Min

October 9, 2019 posted by


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

100 Replies to “Build a TensorFlow Image Classifier in 5 Min”

  1. Rohan Verma says:

    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

  2. Hani YOUSFI says:

    Is it possible to do that without docker? If yes, how?!

  3. iLoL iLoLians says:

    can u help me with image processing to measure fish length?

  4. human being says:

    super idea!!!! thanks saray

  5. timothy lampung says:

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

  6. tenzin chokey says:

    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

  7. Taha Alam says:

    how to do it on windows

  8. Samrat Mukherjee says:

    how to do these things in Google CoLab?

  9. Ved Sarkar says:

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

  10. LyonOconner says:

    no working in 2018? xD

  11. Harddisk Kosong says:

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

  12. Jiang James says:

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

  13. nature1upclose says:

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

  14. FilmerXYZ 4309 says:

    paste what? where is the script?

  15. ruslan smirnov says:

    Sometimes you need to watch Siraj in 0.5 speed factor…

  16. Ali Murtaza says:

    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

  17. molefe sky Mleya says:

    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?

  18. Norman Sabin says:

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

  19. Mustafa Ayyıldız says:

    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?

  20. Norman Sabin says:

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

  21. Norman Sabin says:

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

  22. Bhupinder says:

    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

  23. Chun Leong Lee says:

    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?

  24. Aritz Otxotorena says:

    Too fast, nothing good to say.

  25. panagranit pana says:

    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!

  26. FilmerXYZ 4309 says:

    install what?

  27. anonymous says:

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

  28. Ahmed Alhisaie says:

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

  29. sgg17003 says:

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

  30. Pinku Ki Pathshala says:

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

  31. Kwoods says:

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

  32. ProNoob Gaming says:

    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?

  33. Mr. Schäeffer says:

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

  34. Edwin Garcia says:

    speak slowly please 😵

  35. A Guy from 90's says:

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

  36. Emi Moldovan says:

    Can you help me, I really need your help?

  37. Joey Wolfs says:

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

  38. ROSHAN ZAMEER says:

    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

  39. شهران بهران says:

    Send my wats app 00967716797422

  40. Raducki Frédéric says:

    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.

  41. samartha siddhartha says:

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

  42. As'hat Uzbekov says:

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

  43. Saman Dhindsa says:

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

  44. Alex Acosta says:

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

  45. Pp jp says:

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

  46. Nigel Higgs says:

    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?

  47. Marc Hasenkopf says:

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

  48. apoorv mittal says:

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

  49. Haris Hassan says:

    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

  50. yuvan bajjurla says:

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

  51. Rayan Fernandes says:

    dude your first funny video?

  52. Pratik Mehta says:

    Fatkun batch download does not work!

  53. ERROR 200 says:

    What the hell?

  54. Space Science says:

    0:05 Green screen error black at background

  55. Syed Mannawar Hussain says:

    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

  56. Tejas Joshi says:

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

  57. Toloy Keripin Munsang says:

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

  58. Vishal Salunkhe says:

    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?

  59. Milan Harkhani says:

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

  60. Umesh Verma says:

    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

  61. Michael Richardson says:

    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.

  62. akouaydi hanen says:

    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

  63. ahmad annazili says:

    Kenapa judulnya jadi bahasa indo 😬

  64. anaya angel anaya arya says:

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

  65. preeti patil says:

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

  66. RompotMechanikos says:

    but how do I get it to sort my porn?

  67. Skynse says:

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

  68. celasvictoria360 says:

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

  69. Aci says:

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

  70. Aci says:

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

  71. hrkhaff says:

    No Thanks! I will NEVER take Mac developers seriously!

  72. pjossy joshi says:

    Hello sir , is this python ?

  73. C S says:

    Oh yes, fat cunt bitch, a wonderful extension

  74. Karan Singh says:

    How to get , rectangle around object

  75. Hector Zayas says:

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

  76. Chaitra Nagabhushan says:

    I am unable to link tf image to the data file

  77. Outcasted Gaming says:

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

  78. Bhupinder says:

    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

  79. Aditya Rawat says:

    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

  80. Michael Corleone says:

    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.

  81. Steve Ryan says:

    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?

  82. AndroHub says:

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

  83. teimoor bahrami says:

    can you explain lstm bro

  84. Karan Singh says:

    What is its speed

  85. Aniruddha Sarkar says:

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

  86. Ana Rita Gomes says:

    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.

  87. Pure White Peace says:

    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

  88. Pure White Peace says:

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

  89. Xav Dest says:

    Gonna create one for paycheques

  90. Amogh Mulge says:

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

  91. Freddy B says:

    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?

  92. Alexander M says:

    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

  93. georgi5000 says:

    don't do fart jokes, just don't

  94. pradeep kumar says:

    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

  95. Yu Cao says:

    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/

  96. חי says:

    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

  97. danf10s says:

    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…

  98. Jarvis Minsk says:

    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?

  99. D4n13L M4th3w says:

    what line did he paste in?

  100. Jannatul Ferdos says:

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

Leave a Comment

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