Build a TensorFlow Image Classifier in 5 Min

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. Can anyone help me to configure docker on terminal? I tried using avel$ docker run -it but it says command not found

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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