Train an Image Classifier with TensorFlow for Poets – Machine Learning Recipes #6


[MUSIC PLAYING] JOSH GORDON: Hey, everyone. Welcome back. In this episode,
I’ll show you how to train your own image
classifier starting from just a directory of images. For example, say you want
to build a classifier that can tell the difference
between a picture of a T. rex and a triceratops. Or maybe you want to
classify a painting as being a Monet or Picasso. To do that, we’ll
work with a code lab called TensorFlow for Poets. And this is a great
way to get started learning about and working
with image classification. Now two things off the bat. First, this code
lab is high level. To train our classifier,
we’ll basically just need to run a couple of scripts. But what’s impressive is what
the classifier will create is better than what
I could have written myself just a few years ago. As we go, I’ll show you how
the code lab looks in action, and I’ll add context
and background on why it works so well. OK, let’s get started. To train an image classifier
with TensorFlow for Poets, we’ll only need to provide
one thing– training data. Here that’s just
directories full of images. My plan is to
create a classifier to tell the difference between
five types of flowers– roses, sunflowers, and so on. And here’s what my
training data looks like. Notice that I have
five directories, one for each type of flower. Inside each directory
are lots of pictures. And the reason I’m
working with flowers is we provided this
data set in the code lab so you can get
started right away. If you want to use your own
images, say, for dinosaurs or paintings, all you need
to do is create a directory and fill it with
images from the web. We’ll need about 100 images
in each directory to start. Once we have our training data,
the next thing we’ll need to do is train our classifier. And for that, we’ll
use TensorFlow. TensorFlow as an open source
machine learning library. And it’s especially
useful for working with a branch of machine
learning called deep learning. Deep learning has led to great
results in the last couple years, especially in domains
like image classification, which we’ll be
working with today. And here’s one reason why. Recall that way
back in episode one, we talked about telling the
difference between apples and oranges. We saw it’s
impossible to do this by hand because there’s too
much variation in the world. But now we also know that
classifiers take features as input. And with images,
it’s incredibly hard to write code to extract
useful features by hand. For example, you wouldn’t
want to write code to detect the texture
of a piece of fruit. To get around this,
we use deep learning because it has a major advantage
when working with images. And it’s this. You don’t need to extract
features manually. Instead, you can use the raw
pixels of the image’s features, and the classifier
will do the rest. To see the difference
in our training data looks, let’s compare
the Iris data set with our directories of images. In Iris, each
column is a feature that describes the flower. And you can imagine we
came up with these features manually, say, by measuring
the flower with a ruler. Now by contrast, here’s
our training data in TensorFlow for Poets. It’s just a list
of labeled images. And again, a classifier
is just a function. f of x equals y. Here x is a 2D array of
pixels from the image. And y is a label like rose. Now when we’re talking
about deep learning, the classifier we’ll be using
is called a neural network. At a high level, that’s just
another type of classifier, like the nearest neighbor
one wrote last time. The difference is
a neural network can learn more
complex functions. In this code lab,
TensorFlow for Poets takes care of setting up and
training the neural network for you behind the scenes. That doesn’t mean that
TensorFlow code is any harder to write than what
we’ve seen so far. In fact, my favorite way of
writing TensorFlow programs is by using TF Learn. And TF Learn is a high level
machine learning library on top of TensorFlow. And the syntax is
similar to scikit-learn like we’ve seen so far. For example, here’s
a code snippet that shows you how to
import a neural network, train it, and use it
to classify new data. And you can do this using the
skills you’ve already learned. If you want to learn
more, no pun intended, about this stuff
right now, I put links in the description
you can check out. OK, now let’s return
to TensorFlow for Poets and train our classifier. To do that, we’ll kick
it off with this script. It’s covered in detail
in the code lab, so I won’t say too
much about it here. But I will give you
context on two more things you might want to know about. First, the script
takes about 20 minutes to train the classifier. Now ask yourself,
is that a long time? The answer turns out to be no. Under the hood,
TensorFlow for Poets isn’t actually training a
classifier from scratch. Instead, it’s starting with
an existing classifier called Inception. And Inception is one of
Google’s best image classifiers. And it’s open source. Whereas we have just a couple
thousand images in our training data, Inception was trained
on 1.2 million images from 1,000 different categories. Training Inception took about
two weeks on a fast desktop with eight GPUs. In TensorFlow for Poets,
we’ll begin with Inception and then use a technique
called retraining to adjust it to work
with our images. This lets us re-use some
of the parameters Inception has previously learned so we
can create a new high accuracy classifier with far
less training data. I’ll fast forward til
our training finishes. And once we have a trained
classifier, we can try it out. To do that, I’ll download
this image of a rose from Wikimedia Commons
and use our classifier to predict what type
of flower it is. As we can see, it gets it right. And we can see the
confidence distribution for the other types
of flowers as well. Now keep in mind
our classifier only knows about the training
data we’ve shown it. So if we ask it to
classify an image, say, of the Roman
Colosseum, it must predict that it’s a type of flower. Hopefully, though, the
confidence will be low. Now let me give you one or
two more closing thoughts. To train a good
image classifier, the name of the game is
diversity and quantity. By diversity, I
mean the more images of different types of roses we
have, the better off we’ll be. For example, our
training data includes pictures of red, white,
and yellow roses. We also have pictures
taken at different angles, say, from above or to the side. And we’ve included pictures
of roses in the foreground as well as the background. Now by quantity, I mean the
more training data we have, the better a classifier
we’re likely to create. There are several hundred
images inside the roses folder. That’s enough to
retrain Inception. And you can probably get
away with even fewer images, though your accuracy
might decrease. OK, that’s it for now. As a next step,
you’ll probably want to dig deeper and try writing
your own TensorFlow code. Here’s a link to a tutorial that
will show you how to do that. And you can use exactly the
same technology we saw here. As always, thanks very
much for watching. And I’ll see you guys next time. [MUSIC PLAYING]

Leave a Reply

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