All right.
So in this lesson we're gonna start working with images we're going to start a brand new Google collab
notebook.
And in that notebook we're gonna be using something called tensor flow tensor flow is an open source
machine learning framework.
It was developed originally by Google but was subsequently open sourced in 2015 and ever since then
it's become incredibly popular and widely used tensor flow is the technology that's going to be running
in the background powering our neural network.
But on top of tense afloat we're gonna be using something called Charisse.
You can think of carrots as a bolt on to tensor flow.
It's a neural network library and it's probably one of the easiest most user friendly ways to get started
working with neural networks.
Charisse is essentially a module that will use tensor flow in the background to do the calculations.
So let's jump right in and fire up your chrome browser and follow along with me.
How.
In this case I really do recommend the Google Chrome browser because I've seen some strange bugs with
other browsers like Firefox when it comes to things like file upload.
So go to drive dot google dot com.
You'll need a Google account to do this of course and then go to new more collaboratively.
So just click on that and Google Drive will create a new file namely their version of a Jupiter notebook.
The interface is actually very very similar to Jupiter which is not really a surprise considering it's
all based on Jupiter notebook.
There's only a few minor differences which you'll see shortly.
Once your page is loaded click up here and let's rename this notebook.
Let's call it 0 9 neural nets hyphen pre trained image classification.
Now let's add a markdown cell so you just click on text and him will create a market on cell that reads
inputs.
So one difference you'll see is you'll get a little preview of what your markdown will actually look
like here on the right shift enter.
And so it's a cell below.
And here we can add our input statements.
So I'll say import num pi adds and P.
We always need num Pi.
And then I'll import tens of low as T F and then I'll import Charisse.
Now one other thing I might actually do is click on this little arrow sign here and toggle the header
visibility that way we get a bit more screen real estate.
Next I want to import the I Python display.
But before I do that I'm going to hit connect up here which connects a runtime to this notebook.
Meaning our code could actually get executed.
And in addition it means that we can use the autocomplete in the collab notebook which you can't otherwise.
So I'll see from I python and I'll hit tab on my keyboard and you might notice a slight delay until
the suggestion pops up I'll hit enter their dot see tab on my keyboard core dot D tab on my keyboard
display import display.
There we go.
Now let me hit shift enter here and the cell will run.
And down here you'll get a message using tense of low back end.
This is a message from carers because carers can be bolted on to a number of other technologies as well
doesn't have to be tens of flow.
But here is just confirming it's kind of using tensor flow as a back end.
Great.
Now what I'd like you to do is go to the course resources page and download our sample images that we're
gonna be using for this project.
On that page under this modules resources you should find a zip file that you can download and this
file will contain our sample images.
And once I unzip this file and open the folder I can see that I've got eleven images here I've actually
pulled these images from on Splash dot com.
This is a fantastic resource for high resolution photos from professional photographers but what I've
done is I've downloaded these images and I've resized them I've made him a lot smaller.
This will help us when it comes to feeding data to our neural nets and how after you've unzipped this
file click on this little arrow here and upload the files that you funds it upload these jpeg images
click on upload and then in your Downloads folder open the folder that you've unzipped.
Select the images therein and click upload will instantly get a reminder from Google telling us that
these files will only exist while we're working with this notebook.
They won't actually stay there permanently.
The way they phrase this is files will get deleted when this runtime is recycled.
What this means is that eventually this runtime that we've got will be shut down and the computing resource
on Google servers will be given to someone else.
So that's just one of the trade offs of using a free resource.
There's some limitations in place but after your files have been uploaded they should all come up here.
Next I want to take this cell.
I'll move it down here to be a bit more organized and I'll add another markdown cell that reads constants
and there I will add a few file names so I'll see file underscore one is equal to single quotes 0 1
reload and JPEG and then I'll see a file on the score two is equal to single quotes.
02 coupled up JPEG I'm just picking out a couple of the file names that I can use then later on in my
code file underscore three is equal to single quotes zero three ocean dot J Peg shift into.
There we go.
Now we can add another markdown cell and we'll call this one pre processing images.
This is where we're gonna write our code that will take these images and format the data in such a way
that we can feed it into our neural networks.
And I do say neural networks because we're going to be trying out more than one of them in this module
so how we're going to process our data how we're gonna process these images for the code that will follow.
Well the thing with working with images is that we need to convert these images to an array of numbers.
Why.
Well our neural network will need to work with columns and rows of values.
Right.
But what kind of values make up an image.
We can actually express every single pixel in an image as a number or a set of numbers with a color
image like this.
We'll need three numbers to describe a pixel.
And that's because we can express every single color in terms of the amount of red the amount of green
and the amount of blue.
This is the very famous RG B format.
You can actually see this.
When I bring up my color selector in my keynote program so if I click on this thing there not only do
I have like a little color wheel but I've also got this option of using these RG B sliders to select
a color.
And what you see here is that there are three sliders.
This one governs the amount of red.
So this is a number between zero and two hundred and fifty five.
So 2055 has lots of red.
Zero is very little red.
I've got a slider for the amount of blue and I've got the slider for the amount of green.
And as I play around with these sliders I get different colors because every single color out there
is actually just a mix between red green and blue.
Now if I had a black and white image instead then I actually wouldn't need three sliders.
I wouldn't need three different values to express the value of a pixel.
I would actually just use this grayscale slider and this would give me a value between zero and two
hundred and fifty five and accomplish exactly the same thing.
So once we know the values for each individual color we can turn our image into an array.
But the question is what are the dimensions going to be of this array.
Well it's gonna be the height and pixels times the width and pixels times the number of channels.
So that's three dimensions really.
What I mean by number of channels.
Well for our G.B. we have three channels red green and blue.
The array that you'd actually get after doing this conversion from a picture to a set of numbers will
actually look something a bit like a set of spreadsheets each channel in this case will have its own
spreadsheet.
And if you stack all of these spreadsheets up then each individual pixel will correspond to the three
values of these colors to the top left pixel will have the value 0 for red the value 1 2 7 for green
and the value 0 for blue that's the information that we're gonna be piping over into our neural net
back in our core lab notebook.
Let's go to the very top and under our import statements let's import a few pre processing functions
from Caris carries will not only help us work with neural networks cares will also help us pretty process
some of our data I think this is very very handy.
So let's write from Chris dot pre processing dot image import.
I m g underscore to underscore array come and then load on a score.
I m g load image.
Let me hit shift and sure on this.
This is a helper function that we can try out right here.
And the way we're going to do this is we're going to pull up our first image in our collab notebook.
Let's try it out.
So I'll say pick is equal to load on a school image ethanol take file at a school one.
One thing you'll find notice is that in contrast to running all the stuff locally on your machine there's
that slight delay.
When you hit tab on your keyboard to use autocomplete it drives me absolutely crazy.
And this is why I really really prefer working locally rather than with the Google collab notebook.
But the big advantage working with the core lab notebook is that we don't have to do any setup right.
We can jump straight in into Charisse and tensor flow without having to do any installation.
So it's a give and take.
You win some you lose some.
So now that I've loaded my image I can actually display it right.
Using the I Python display so I'll say display parentheses pick and headings shift enter and I'll actually
show the image right here as an output under my cell.
So this is the image for us humans but our neural network will want that array of numbers.
So how do we get that pick on a score array shall be equal to the output of the image.
Underscore to underscore array function from Caris.
Here we can feed in the picture that we loaded earlier.
And then we can see.
Pick the score array don't shape to take a look at what the output for this function actually is.
It's indeed an array and you can see that I've downscaled these images from unsurpassed quite dramatically.
This one here is 2 5 6 by 2 5 6 and it's got 3 channels.
This is how to interpret this information.
So with very few lines of code we've just loaded our images and we've converted them into an array in
the next lesson.
We're going to start looking at our neural network models that are available in Caris.
I'll see you there.
Take her.