This video is sponsored by PCBWay.
Be one of the first 100 people to use the code in the video description below to get
$10 off your order at PCBway.com.
Hi I'm Alex and welcome to Super Make Something Basics!
In today's Machine Learning tutorial, we take a look at probabilistic image classification
with help from the NVIDIA Jetson Xavier NX.
Image classification is one of the most common applications of machine learning today.
From facial recognition and tracking in your camera when you take a photo to identifying
lane markers and roads in your self driving car, applications that require a computer
to understand what is in an image are all around us.
But how exactly does a computer go from raw pixel values to determining whether an object
in an image is a dog, a cat, or a banana?
One way to do it is to use an algorithm called "Gaussian Naive Bayes," a classifier that
uses probability to determine the most likely object in an image based on a set of pre-computed
features.
To explore this topic further, NVIDIA graciously sent me a Jetson Xavier NX, a single-board
development platform that aims to bring artificial intelligence and machine learning to embedded
edge devices.
Like it's predecessor the Jetson Nano, the Xavier NX's compute module is about the size
of a credit card, but has between 6 to 28 times faster performance, thanks to its 6-core,
64-Bit 1.4 GHz ARM processor, 8 GB of LPDDR4 RAM, and an NVIDIA VOLTA GPU with 384 CUDA
cores, 48 Tensor cores, and 2 deep learning accelerator engines.
All together, this means that the Xavier NX is capable of performing 21 trillion operations
per second, making it easily capable of running multiple machine learning models simultaneously,
all while only consuming between 10 to 15 Watts of power.
Retailing for $399, this system-on-a-module packs an insane amount of computational power
into a tiny package and allows developers to deploy demanding machine learning applications
virtually anywhere.
Regardless of the type, every machine learning algorithm needs a computer to run on, so let's
begin by setting up the Jetson Xavier NX.
I began by heading to NVIDIA's Jetson Download Center, and grabbing the latest Xavier NX
JetPack SD Card image.
After this, I flashed the image to an SD card, identical to how I did it in my previous Jetson
Nano video that explored Neural Networks.
For more information about this process, be sure to check out that video by following
the link in the video description below.
I next unboxed the Xavier NX developer kit, which contained the Xavier NX compute module,
a carrier board with a pre-installed wifi and bluetooth module, an integrated heatsink
and fan, and a 19.5V power supply.
The carrier board is handy for development, and breaks out multiple ports and interfaces,
including a DC barrel power jack, a Display Port and an HDMI Port, 4 USB 3.1 Ports, a
Gigabit Ethernet connector, a micro USB connector, and a micro SD card slot.
The carrier board also contains a slot to install an M.2 NVMe solid state drive, 2 CSI
camera connectors, and a 40 pin GPIO header that is compatible with the layout of both
the Jetson Nano and Raspberry Pi, making it perfect for maker projects that require an
extra dose of computation.
If you intend to use the Xavier NX for your next project and find yourself needing some
circuit boards, consider checking out PCBWay.
PCBWay offers various types of circuit board fabrication services, including both standard,
advanced, and flexible PCBs, as well as board assembly and SMD-stencil services.
Getting your board manufactured is incredibly easy thanks to their online system that populates
board parameters automatically from uploaded Gerber files, after which you can continue
to customize options to your liking.
PCBs that I've ordered for my projects have typically always arrived within less than
a week, and the quality of the boards has always been top notch.
If you're one of the first 100 people to use the code in the video description below, you'll
also get $10 off your order of $10 or more, meaning that you essentially get free circuit
boards just for being a Super Make Something viewer.
Be sure to check them out at PCBWay.com and thank you PCBWay for sponsoring this video!
After connecting a monitor, mouse, and keyboard to the carrier board, I inserted the micro
SD card containing the JetPack image into the onboard micro SD card slot.
I next plugged in the power connector into the barrel jack, after which the Xavier NX
booted.
Like the Nano, the Xavier NX runs a full Linux operating system, which makes the computer
incredibly easy to use.
After connecting to WiFi and verifying that everything worked, I next headed to the "Jetson
Zoo" website using the shortcut on the desktop, and followed their instructions to set up
TensorFlow and various other packages that I may need for developing future machine learning
applications.
Like in my Jetson Nano video, I also installed various Python numerical support libraries
and Jupyter Notebook, a browser based, interactive computing environment to develop and test
Python code.
Installing all of these libraries and applicatons is straightforward, but can take a bit of
time, so be sure to check out my "Machine Learning 101: Intro To Neural Networks" video,
which walks you through this setup process using a Jetson Nano.
Once everything was installed, I headed to Github and downloaded the Fruits-360 image
dataset, a collection of over 90,000 100x100 pixel images of different types of fruits,
vegetables, and nuts, which we will use to develop a probabilistic image classifier using
a "Gaussian Naive Bayes" classification scheme.
I next created a folder on my Desktop, unzipped the "Fruits 360" dataset into it, and finally
downloaded the "gnbImageClassifier" Jupyter Notebook from the Super Make Something Github
page.
A link to all of these files can be found in the video description below.
Let's now take a look at our image classifier.
Like its name suggests, Naive Bayes is a probabilistic classifer based on Bayes theorem, which describes
the likelihood that an event will occur based on information that you have collected about
conditions that might be related to that event.
In terms of image classification, this can be thought of as follows: Let's say that your
job is to program a robot to automatically sort a warehouse of apples and bananas into
separate bins or classes.
Based on the two fruits you are trying to sort, a good way to differentiate between
them might be to look at their color.
The color of the fruits is known as a feature.
Let us also assume that you have some prior information about the number of apples and
bananas you are trying to sort that day, and know that 75% of all of the fruits in the
warehouse are bananas.
Using this information, it is possible to calculate the probability that the robot is
seeing an apple or banana based on a fruit's color using Bayes rule as follows:
Using its camera, the robot sees that the color of the fruit it is trying to identify
is yellow.
The probability that the fruit is an apple given that its color is yellow, is equal to
the probability that the color of a fruit is yellow given that it is an apple (also
known as the conditional probability) times the probability of randomly picking an apple
out of the warehouse (also known as the prior probability).
To calculate the real-world probability that the fruit the robot is currently seeing is
an apple or a banana, this number would then need to be divided by a number known as the
marginal probability, but this number is a constant when trying to classify objects into
a pre-determined number of classes, so we can ignore this for now and instead write
that the probability that the fruit is an apple given that it is yellow is :proportional:
to the other two quantities.
Plugging in some numbers, we know that the probability that a fruit is yellow given that
it is an apple is 0, because we know that our warehouse doesn't contain any yellow apples
and that the probability of randomly picking an apple from the warehouse is 25%.
Multiplying these numbers together, we can then conclude that the probability that the
fruit the robot is seeing is an apple given that the fruit's color is yellow is 0%.
Running the same calculation for a banana, we know that the probability that the color
of a fruit is yellow given that it is a banana is 100% and that the probability of randomly
picking a banana out of the warehouse is 75%.
Multiplying these together, we get a result of 0.75, which, while not the real-world probability
because we didn't divide it by the marginal probability, is definitely bigger than 0,
so we can confidently say that the fruit the robot is looking at is a banana.
Unfortunately there is a problem... in the last delivery to the warehouse, 66% of the
fruits were bananas and 33% were apples, which changes the prior probability a bit, but that's
okay.
However this delivery included both red and yellow apples, which causes the robot to make
the following decision:
The probability of a fruit being an apple given that it is yellow is still equal to
the probability that the color of a fruit is yellow given that it is an apple times
the probability of randomly picking out an apple from the warehouse by chance, which
is now equal to 0.5 (since 1 out of 2 apples are yellow) times 0.33 (since 33% of all of
the fruits in the warehouse are apples).
Multiplying these numbers together gives 0.17.
Unfortunately, repeating the same math for a banana gives 0.66, meaning that the robot
will always classify something yellow as being a banana and that it will never sort yellow
apples correctly.
So how do we fix this problem?
One way is to add more features to boost our chances of uniquely describing each type of
fruit.
While yellow apples certainly exist in the real world, we know with 100% certainty that
round bananas do not.
Therefore, adding a second feature that describes a fruit's shape could help to save the day.
In the real world, the probability that a fruit is yellow and round is correlated, but
this is where the "naive" part of a Naive Bayes classifier comes into play.
If we assume that the probability of a fruit being yellow and round is independent, we
can calculate the probability that a fruit is an apple given that it is yellow and round
as the probability that a fruit is yellow given that it's an apple (a 50% chance) times
the probability that a fruit is round given that it is an apple (a 100% chance) times
the probability of randomly encountering an apple (a 33% chance).
Multiplying these numbers together gives 0.17.
But repeating this calculation for a banana gives 0%, since there is a 0% chance that
a banana is round.
By adding another features to describe each class, the image classification algorithm
is now able to correctly identify that the fruit it is seeing is an apple and sort everything
with 100% accuracy.
This is one of the central motivations to use a Gaussian Naive Bayes classifier -- for
a dataset with lots of classes, it is unlikely that you will be able to find a single feature
that is able to perfectly classify everything correctly.
However, by using multiple features, where some features are very good at describing
a few of the classes, you boost the probability that the classifier will make the correct
decision.
The final note is that unlike the apple/banana example, a computer needs to describe each
feature using a numerical value.
One way to do this is to assume that the conditional probability for each feature follows a Gaussian
distribution, where the likelihood can be described by a mean and standard deviation.
While this equation looks complicated, all that is needed to calculate the probability
that a new sample belongs to a certain class is the values of that new sample's features,
and the mean and standard deviations of those features for all of the classes that you are
trying to differentiate between, which is calculated from your training data.
This makes Gaussian Naive Bayes a fast and easy image classification scheme that is useful
for many different applications.
To run the code, I simply highlighted the first cell of the Jupyter notebook, and then
pressed Shift + Enter together.
This ran the highlighted cell and advanced to the next one, allowing me to continuously
press SHIFT + ENTER until I had made it all the way through the notebook.
This particular example tries to differentiate between five fruit in the Fruits-360 dataset
using four features similar to the previous example: the fruit's color, broken out into
its red, green, and blue pixel value components, and the number of white pixels in the image,
which roughly correlates to the fruit's shapes and sizes.
Overall, I was incredibly impressed with the speed of the Jetson Xavier NX.
The classifier was trained on approximately 475 100x100 pixel images from each of the
five classes, and was able to complete all of the training, including feature extraction
in 29.99 seconds.
For comparison my desktop, which contains an Intel i7 9700KF, a GeForce RTX 2070 Super,
and 32 GB of DDR4 RAM completed the training in 30.00 seconds, essentially the same speed.
Classifying 10 new images from each of the categories was also fast, taking 0.24 seconds
on the Xavier NX and 0.07 seconds on my desktop.
Let's now take a look at the results.
A confusion matrix lists the number of correct and incorrect classifications made by the
classifier in table form.
Reading across the rows tells you what the classifier predicted when the image actually
contained a certain type of fruit.
Reading down the columns tells you what the image actually contained when the classifier
predicted a certain type of fruit.
For an accurate classifier, we want the actual fruit to match the prediction, meaning that
the diagonal of the table should contain as many tens as possible, since we had it predict
the labels for ten different images of each fruit.
Overall, we see that the classifier performed extremely well, predicting the correct label
for all images except one, where it incorrectly said that an image which actually contained
a lime contained an apple.
Looking at this image individually allows us to see what happened.
Both its average red value and number of white pixels were closer to the feature values that
the classifier learned for apples than for limes.
This led to a likelihood value that was greatest for the apple class when all of the feature
values were multiplied together, which was then made worse by the prior probability of
encountering an apple from the dataset at random, because there were slightly more apple
than lime examples in the training dataset.
Interestingly, the classifiers' second choice was a lemon, this time because of the image's
average green value and its size.
One way to potentially address this is to add other features that both better describe
the overall shape of the fruit, since lemons tend to be the most elliptical looking fruit
among these classes, and the fruit's texture, since the skin of an apple is relatively smooth
compared to the other two.
However, adding these features may negatively affect the classification accuracy of other
fruits, so new features should always be tested carefully before they are deployed in the
real world.
Overall, I'm incredibly impressed with the NVIDIA Jetson Xavier NX.
The platform is great both for industrial applications and for makers looking to make
their mobile machine learning projects smaller and faster.
Gaussian Naive Bayes classifiers are an intuitive, simple, and computationally inexpensive way
to classify images.
This example focused on classifying whole images that only contained a single subject,
but this isn't the only way this classifier can be used.
This classifier could just as easily be used on a subset of pixels in to identify multiple
objects or to perform image segmentation, or on an entirely different data format like
sound files or text.
A big thank you to NVIDIA for sending the Jetson Xavier NX to me in order to make this
video.
In case you are interested in picking one up for yourself, links to where this system
can be purchased online can be found in the video description below.
If you liked this tutorial and learned something new, please consider giving the video a like,
sharing it with your friends, and subscribing to my channel.
Your support helps me to make more videos.
Well, that's all there is for this episode of Super Make Something Basics!
Thanks for watching -- now go Super Make Something!