[MUSIC PLAYING]
JASON MAYES: So the next question you might be wondering
is, how on Earth do you train such systems?
And that's a really great question.
Now, I know you all work in many different industries,
so feel free to adapt the following
to your own area in your minds.
But let's do a thought experiment
and pretend we are trying to make a web-connected system
for farmers who are trying to classify apples
and oranges to speed up the delivery of the picked fruits
that are currently mixed together
and need to be sent to the right destinations.
Now, the first thing you need to do
is identify the features or attributes of the fruits
that you could measure.
Let's take color and weight as an example just like before.
Both are easy to measure and can be represented numerically.
You can use digital weighing scales and color values
from a webcam that would allow you to do this.
Now, going back to our high school maths,
if you were to sample some apples and oranges
and plot these values on a scatter chart as shown,
you can see here that the red and green apples
fall into the red and green spectrums on the x-axis
of this graph and tend to cluster together
with a similar weight variance in the y-axis.
The oranges, as they're super juicy, tend to be heavier
and are higher up on the chart.
Now, if you can draw a line that separates the oranges
from the apples, you can now have some degree of certainty,
decide what fruit something is simply by plotting its feature
values on the chart.
If it's above the line, it's most likely an orange.
If it's below, it's probably an apple.
You have essentially learned how to classify the fruits.
So if you can get a piece of software
to define the equation of this line by itself,
you can get a computer to then learn to classify fruits too.
And this is the essence of what machine learning is
doing behind the scenes.
Not so magic, right?
Essentially, you're just trying to figure out the best
possible way to separate out the example data, such
that for a new unseen example, you
have the best chance of classifying it correctly.
But what if you had chosen bad features?
Let's take ripeness and number of seeds.
Here, the plot is less useful to us.
There is no straight or even curved
line that would allow us to separate these data points.
You can't really learn from this data alone.
And you might be thinking, but, Jason,
why would you choose such obviously bad features
like this?
And sure, with this trivial example,
it's quite clear that this would be unwise.
But what about those medical scans
we spoke about earlier in the course that
are just pixels in an image.
How do you define features for that?
It's not always so obvious.
And what if you had more than two features?
Previously, you had two features,
so you used a two-dimensional chart to separate the data.
If you had three features, you would need a 3D chart
to do so, as shown here.
Here, a weight dimension is added to our previously
unspecible features.
And now, you can use a plane or a rectangle in 3D space,
if you will, to separate the oranges from the apples.
Hopefully, in this image, you can
see that the oranges are now further back in the weight
axis, making them separable from the apples.
But it turns out that even three dimensions is typically not
enough for most machine learning problems.
It's not unusual to have tens, hundreds, thousands,
even millions in the case of images,
where each pixel is a feature.
As humans, you'll struggle to visualize anything higher
than 3D.
I tried myself and failed.
However, for a computer, the mathematics
works out just the same and is capable of doing
such calculations.
Instead of using a plane, you actually
use something called a hyperplane, which simply means
one dimension less than the number of dimensions
that you have, allowing us to split the data,
just like you did here, but by using more features
and attributes, which can sometimes lead us
to better data separation.
Now, when you're working with supervised learning,
there are typically two types of problems
you're trying to predict.
First is a classification problem.
What thing is represented by the inputs provided?
Is it a cat or a dog, for example?
You're trying to predict a specific class from a number
of possible known classes.
Second is a regression problem.
This is where you're trying to predict some number
from some other input numbers.
For example, what is the price of a house if it's
1,000 square feet in size?
You can see visually here how, in these trivial examples,
a single straight line is used to solve both types of problem.
But in the first instance, it separates
out the data, and the second, the line
itself is used to predict the output value.
So let's say you've got a dog, and you've got a mop.
It should be pretty easy to find the differences
between these two images, right?
Well, not so fast.
It turns out, in the real world, there
are edge cases that can overlap.
Even I as a human need to double-check
some of these images.
And the reason I bring this up is
to raise awareness about bias in training your data.
One of the biggest challenges you will face
is collecting enough training data
that's truly representative of all the different situations
you might encounter in the real world,
like the edge cases you see here.
And if you don't do this, then there's
a good chance that the machine learning system
will fail in these cases too.
Imagine you wanted to recognize cats.
You might need 10,000 images of different breeds of cats
at different stages of their life, kitten versus adults,
different fur patterns, different colors, all of which
are taken at different angles and lighting conditions,
even indoors versus outdoors, in order
to be able to train a system that
understands the essence of what cat pixels really are.
Now, the other thing I'd like to point out here
is that data is not always in the form of imagery.
I used that in my slides as it's easy to view,
but remember, input data to train an ML system could
be tabular data, text, sensor recordings, sound samples,
or pretty much anything you want to classify,
so long as it can be represented numerically to be input
into the model for training.
So now you have a high-level appreciation for what's
going on behind the scenes.
Let's focus some more on gathering data and trying it
for yourself.
For this, you'll learn how to use a website known
as Teachable Machine.
This site was made by some friends at Google
and is powered by a machine learning library
for JavaScript, which is known as TensorFlow.js,
which we'll learn more about later on in the course.
Now, this site is great for prototyping, and in our case,
to illustrate the importance of good quality input data.
So go ahead, pause the video, and open
teachablemachine.withgoogle.com in a new window
and place that window side by side,
so you can follow along as I show you how to use it.
I'll wait for you to do that.
And remember to hit Play when you're ready to continue.
All right, so to kick things off,
let's hear from the creators of Teachable Machine
to give us the overview before we use it together.
[MUSIC PLAYING]
SPEAKER 1: People are training computers and creating machine
learning models to explore all kinds of new ideas,
new ways to interact--
SPEAKER 2: Yes.
SPEAKER 1: --to understand the world, to play--
[XYLOPHONE PLAYING]
--and experiment.
But machine learning is pretty intimidating to get
into, so we've been wondering, what if it wasn't?
With Teachable Machine, we set out
to make it easier for anyone to create machine learning
models without needing to write any machine learning code.
When it first launched in 2017, it
allowed everyone to get a feeling for what
machine learning is all about.
But now, Teachable Machine puts the power
of machine learning in your hands,
allowing you to save your models and use them
in your own projects.
SPEAKER 3: So let's say you want to build a model to recognize
you versus your dog.
You just open up the site, record samples of you
and samples of your dog, click Train,
and you instantly have your own machine
learning model, which you can use in your sites, apps,
and more.
You can upload your model to host it online or download
it to work entirely on device.
SPEAKER 1: With Teachable Machine,
you can create custom models for all sorts of things--
using images, audio--
[CLICK]
[FINGER SNAP]
[CLAP]
--or even poses, personalized machine learning models
for the things that matter to you.
And folks have already been trying it out,
using Teachable Machine in their own experiments,
solving problems in their communities--
SPEAKER 4: [HUMS]
[CLICK]
SPEAKER 1: --or even just at home.
[CLINK]
Start creating and see where your ideas take you
at g.co/teachablemachine.
JASON MAYES: Great.
So now you know what this system can do.
Follow along with me to make your very first machine
learning model to recognize any object in your room.
OK, so when you head over to Teachable Machine,
you should see a website that looks something like this.
As you can see, it supports many different types of projects.
First is image recognition, the second is audio recognition,
and the third is pose recognition.
All of these are models that are powered
by TensorFlow.js, what you'll learn
more about in the next lesson.
Now, today, we're going to focus on image recognition,
just to teach you the basics of gathering data.
So click on that and then click Standard image model, as shown.
You should then see a screen that looks something like this.
On the left-hand side, we've got the objects you want to detect.
At the moment, they're called Class 1, Class 2, and so on.
Let's give it a more meaningful name.
So the first thing, I'm going to call
"Jason," and the second thing, I'm
going to call "Bottle," as shown.
OK, so we now can click on Webcam,
enable access to our webcam if it's our first time using
this site, and if you allow access,
you should then see your webcam come into view.
Now, if you've got multiple webcams,
you can click on the dropdown to select the right one.
And now, we're going to hold to record some images
of the thing of interest.
Now, this one is Jason, so that's me.
And, of course, you're going to record your own things over
there, so feel free to change your names as needed.
And now I'm going to try and get some sample images of myself
right here.
Note, I move my head around to get some variation as well.
I've got about 43 images, and that's great.
That should be enough.
We now go to Bottle, click on Webcam,
and here, I'm going to try and recognize this bottle instead.
So I'm going to try and get the same number of images
as before.
And that's important because if you
have too many of one class, statistically speaking,
the machine learning will think that that's
more likely to appear just naturally
So it might learn just to predict
that object all the time.
And, of course, when it's training, it would be right 90%
of the time.
So it's important to have roughly the same number
of examples for your training data.
So let's try and do the same thing
here with the bottle nice and close to the webcam
and get roughly the same number, 40-something images.
44-- that's close enough.
Now, let's go ahead and train our model.
And what's happening now is that, live in the browser,
TensorFlow.js is going to retrain the model so it
can distinguish between these two different object types.
And you can see, it's already finished.
It happened pretty fast.
And you now got a preview on the right-hand side of it
in action.
Now, you can see right now that it
says "Jason" with 100% confidence, which
is technically correct.
And if I bring the bottle into view,
it switches to "Bottle" with 100% confidence, which
is also now correct.
Jason, Bottle, Jason, Bottle.
And look how fast that is.
It works in real time.
And this is very usable for a prototype, for example.
Now, if this is good enough for your needs,
you can actually click on Export Model at the top
here, click on Download, and then click on
this button here to download the model files.
This will contain things like a model.json
and some binary files you can host on your website to then
use in any way you wish.
And, of course, we've given you some example code down below
here that you can use to get started.
But don't worry about that for now.
We'll be teaching you all about how to code later on.
So for now, let's go back and focus on training data.
Now, I just trained a system that
recognizes the difference between me and a bottle,
and that's great.
But let's try other bottles to see if it
manages to recognize those too.
Let's try this one.
Pretty good.
Not too bad.
It seems to be recognizing that shape is important here.
And let's just try one with a different color as well.
And again, it gets that that is actually a similar thing.
Now, this is great.
However, if I bring into view this kind of jar-like object,
it's not quite as sure, but it gets to the 90s,
which is pretty confident.
So we might want to train a system
to be better at recognizing jars, for example.
So in order to do that, we can add another class on the left
here, maybe call it "Jar," like so.
And now we can add training data for that.
So let's go ahead and get roughly 45 images of this jar
here.
Let's open that up like so and get roughly the same number
of images.
44 is good enough.
Hit on retrain to train the model once again, between three
objects this time, not two.
And now you can see we've got Jason, Bottle, or Jar showing.
And if I show it the jar, it's now 100%
confident that that is a jar, which is correct.
And let's go ahead and test the bottles again.
It's always good to retest things.
The bottle is 100% confident there.
And our Coca-Cola bottle--
100% confidence.
And let's bring that green one again back into view.
And, oh, what happened here?
It actually think this is a jar.
And why might that be?
Well, looking at these two objects,
you can see that they're both green, or a lot of green
contained within them, right?
So what's happened is that it thinks that a jar is just
some green object, whereas a bottle is just kind
of bottle-shaped object, right?
So in order to get around this, you're
going to have to add more training data to your examples
to improve the accuracy and so it
knows that this is, in fact, a jar versus a bottle.
So what are we going to do here?
Well, for this bottle over here, we're
going to go back to the bottle training data, click on Webcam,
and we're going to add some examples of this bottle
to the scene so that we know that this
is a green bottle, OK?
So let's go ahead, put that in view, and click Hold to Record,
and there we go.
We've got about 84 images now.
Now, because I've got 84 images of Bottle,
I need to go and get some more training data
for my other classes as well.
So let's go back to Jason.
Oh, let's move the bottle out of view
because that would be contaminating my data.
I'm going to click Train Model and get some more of me.
Oh, sorry, not Train Model.
Click Hold to Record.
So now I'm going to click Hold to Record to get
some more training data of me.
So roughly 77 there.
Let's get a bit more.
80, OK.
90, OK.
That's close enough to 84.
That should be fine.
And now let's do the same thing for Jar as well.
So open up the webcam for Jar and put this back into view
here.
And let's get some variation of the jar and get roughly--
oh, 93.
OK, that should be close enough.
So now, if I click on Train Model here,
we're going to have a lot more training data.
It might take a bit more time, but we should now
get a more reliable output model that
can distinguish these things.
So here we go--
Jason, 100%, correct.
Bottle number 1, close enough to 100%.
That's good.
Bottle number 2, straight in there, 100%.
Very good.
Bottle number 3, which was the one that was
causing us problems before--
100%, beautiful.
And now, if we try the jar, hopefully it still
remembers that this thing over here is a jar.
And as you can see, we've now trained a much more robust
model.
So go ahead and explore yourself with objects in your room
to see what you can recognize and what causes problems
and what doesn't.
As you experienced, the machine learning model you created
was as good as the data that you presented to it.
The more data you use, the better
it can be at generalizing, just like the image
shows on this slide.
All of these objects are bottles,
but if you only use one type in training,
the model could struggle to detect
the other types in the future.
Instead of learning that color was the distinguishing feature,
it might learn that shape was more important, leading
to a more robust system.
As you saw, in this use case, where images were the input,
you do not have control over what
features it decides are important,
as it will decide by itself, based on the data presented,
what distinguishing features provide the best data
separation.
And by providing varied inputs, it
has less chance of choosing wrong features.
Be careful, though.
In all the images I just showed you,
I used my hand to hold the object.
In this case, it could learn that the hand is the thing
to recognize and not the object itself.
Technically speaking, it would be correct,
as my hand existed in every single image that we showed it.
But as a human, you know that this was not
the intended outcome.
Continue to explore using Teachable Machine to see what
edge cases you can find that lead to issues for the object
you're trying to recognize.
Maybe try for other objects too if they're similar
and see how that goes.
[MUSIC PLAYING]