Build a TensorFlow Image Classifier in 5 Min

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 thoughts on “Build a TensorFlow Image Classifier in 5 Min

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

  2. Hi
    Is it normal to get this?
    INFO:tensorflow:Saver not created because there are no variables in the graph to restore

  3. Can this process be replicated in Ubuntu? Or do i have to use different commands, because i am having some troubles installing the

  4. How can I get own images for retraining in Google Colab except the flowers dataset.

  5. i hope you make videos showing the folders/directory being populated with whatever files it is or dependencies needed. docker downloaded alot of things from (docker run -it tensorflow/tensorflow:latest-devel bash), and i idon't know what that is for, nothing is added to the docker toolbox folder, furthermore, i ran that line few more times, and it keeps on downloading/pulling. Is it that the files are being deleted after docker exits ? This could be not so big of an issue but i live in a country where internet access in scarce 🙁 .
    -my teacher asked us to do one of your tutorial video as a quiz, and its hour left before the submission, now i'll sure have a 0 on it, dang. pretty sure im just trying to point the blame out of me, sorry about that.

  6. Ok. Assisti o vídeo em Inglês, of course! Mas vi q o título estava em português… depois de uma taça de vinho, tudo pode acontecer. Ok, vamos conferir… Uéééé…. a descrição tbém está em português?!?! mother fucker…. holy wine!!!!

  7. Seems like many people need to work on image lists. Could be have a play store of image identification apps, that could plug into a bigger artificial intelligence app. Like we could download the app for recognizing Darth that you made, link recognizer app to a bool function called IsthisDarth () , Just have an app that does one small part of intelligence well and share it on a play store when done, We need more shared data projects but also share point like a play store of solved image identification apps, same for sounds, same for other types of sensors, moving images, you could even have security apps that look for specific things, if there was a larger intelligence app that sat under the mini intelligence apps that would be good, I want an intelligence that just recognizes Darth Vader. Or even topic based a.I like an a.I that just knows how to do maths. Or an a.I that just knows how to convert my English into French. Or an a.I. that has knowledge to talk about ancient Rome. I also want an app that is able to combine all my knowledge apps together to speak about all together at once and keep improving,

  8. Can anyone help me to configure docker on terminal? I tried using avel$ docker run -it but it says command not found

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

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

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

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

  13. 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/': [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 file?

  14. 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/
    "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?

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

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

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

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

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

  20. Top free resources to learn AI Machine learning and data science..

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  51. 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 file is. In the directory tensorflow_src/tensorflow/examples/image_retraining I only have a Readme. Can anyone help, please? Thank you.

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

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

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

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

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

    #change to updated retraining script and run
    cd /tensorflow_src/tensorflow/examples/image_retraining_v2
    –how_many_training_steps 500
    –image_dir /tf_files/images

    #clone classifier repo
    cd /
    git clone

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

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

Leave a Reply

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