You wanna build your portfolio.
And data science and machine learning.
Today we're going to be building a convolutional neural network from scratch,
the thing that actually powers image recognition.
Last video we built a typical neural network from scratch.
So today we're going a bit further
because somebody asked for me to explain saving weights to an actual file.
And the pooling layer. And we're only going to be using JSON library
and numpy
for a quick map of what we're doing.
A cnn looks at an image in pieces
instead of all at once.
So first a convolution layer scans
the image looking for features,
and then a pooling layer shrinks
things down to keep only what matters.
You stack up those and the network
learns to see.
We're building each of those as its
own class,
just like last time. Start with the star
of the show,
which is a convolution. And this is
the idea that makes the whole thing
a CNN.
intuition is that instead of looking at
the whole image at once,
we take a tiny window called a filter
and slide it across the image.
And at every position, it checks if
the pattern were looking for shows
up here.
One filter might hunt for vertical edges,
and another will hunt for corners.
And you slide it everywhere to get a
map of where that feature appears.
Heres that in code. When we create
the layers,
we make a set of filters full of
random numbers.
And those are the patterns it'll
eventually learn.
The filters are 4D, so we have
number of filters, channels,
filter height, and filter width.
And you're gonna have to hang on
to that because it matters when we
save them.
And then the forward method is
sliding in the math.
Now, there are two tools that are
doing a lot of heavy lifting.
The first is the sliding window view
from numpy,
and it hands us every single patch
the filter would land on all at once,
so we don't have to loop to slide.
Second is einsum, which multiplies
every patch by every filter
and sums it all up in one clean line.
that random string of letters.
Is just naming the axis, which is,
like, the dimensions to multiply and,
like,
which to add together. This,
I would say, is, like,
much closer to, like,
how real frameworks actually do
under the hood.
And once you get comfortable with
these two tools,
a lot of the scary array code gets
really clean.
Kay, our next problem is after
convolution,
we've got a lot of data right when
we don't need the exact,
like, pixel perfect position of every
feature.
So we just need to know roughly
where the strong signals are.
And that's what pooling does
max pooling is beautifully simple,
so we chop the image into little squares,
like maybe 2 by 2. And from each
square,
we keep only the biggest value,
where the strongest signal survives
and everything else gets dropped.
And instead of looping over every
square,
we reshape the image so each little
block becomes its own dimension.
And then take the max across those
blocks in a single call.
The result is a smaller image that
keeps the important stuff.
So it's less data, less computation.
And it actually makes the network tougher,
because tiny shifts in the image
doesn't change the answer.
So now we've got our pieces.
convolution to find the features,
relu to keep the strong ones.
Same activation from the last video,
and pooling to shrink it down.
Now we're gonna wire them together.
And the big one: save what the
network learns
the network class is the same
simple idea as before.
It's just a list of layers,
and we run forward through all of
them.
But now we can save and load.
So we're gonna watch the save
method and go through each layer.
And if it has filters, we store two
things,
the weight and the shape. Now,
for our main method, we're going to
make a tiny 10 by 10 image
and build our network with a
convolution layer,
relu and pooling.
And then push the image through.
And you're going to see that the
image goes in by a 10 by 10
and comes out by a (i meant 2 by 4
by 4)
by 3.
Two feature maps were shrunk
down,
and the image got scanned and
squeezed exactly like we designed.
Furthermore, we saved the weights
and opened the file
and we actually loaded it back in
and remade the network with the
layer type,
the shape and the natural learned
numbers.
And they were sitting right there in
plain text.
And you could send this file to
somebody
and they can load the exact
network with the exact weights.
And that's the beauty of saving
weights.
And there you go, we made a CNN
from scratch with convolution,
pooling and saving your weights.
However, I do wanna add that I did
leave one thing out on purpose,
and that was
training the CNN with back
propagation is the hardest math in
this whole series.
So I might make it its own video.