A Practical Guide to Machine Learning with TensorFlow 2.0 & Keras Image Processing with Keras
Transcript from the "Image Processing with Keras" Lesson
>> We'll be talking about next is the computer vision. So let's create new notebook. Because I want just to show you a couple of datasets you can start using to play around with the images. For instance, once again, let's import TensorFlow. We also kinda import Keras directly. So let's say from tensorflow import keras.
[00:00:34] All right, just to make sure, we can print out a TensorFlow version. I think Keras should also have the same format, new version of the Keras, at least. So, keras__version__ should give you, yes, 2.2.4-tf. Meaning that Keras, this version of Keras is using TensorFlow, or actually part of TensorFlow, to be precise.
[00:01:02] And what is interesting in Keras, There is additional functionality available. And the one I'm looking for is keras.datasets. So keras.datasets, if you press Enter and just press top afterwards, it will show you the available datasets already embedded or available for you right now as part of the Keras framework itself.
[00:01:36] So boston_housing, I believe it's data from 1970 about prices of houses in Boston depending on multiple different input values. For instance, the data points use the age of the person living there, how many rooms, how much floor, how far away it is from the downtown. Criminal activities in the region, kinda a lot of different information which affects the prices, so you can play with it.
[00:02:14] For instance, if you want to try different regression algorithms. cifar10 and cifar100, it's images, but pretty small images. I don't remember the exact resolution. I think it's 20, maybe similar to mnist. That's the one I want to use. It's 28 by 28 small images with 10 different classes or 100 different classes.
[00:02:42] fashion_mnist or mnist, that's the one I want to use. mnist is your handwritten digits, and fashion_mnist is just ten different types of clothing and shoes. So to basically visualize what we have here, we can simply say load_data, right? And if you kinda curious what this function will return, that's the only function available in the mnist module.
[00:03:11] You can just run this command and it will tell you that it will load the mnist data. So it will return back x_train, y_train and x_test, y_test. Okay, so let's just copy those and say these values should be created, right? And after we call this function, ta-dah, okay.
[00:03:40] So now in my x_train, I will have something, as well as y_train. So let's go back and actually discuss what exactly we have here. Remember that the whole machine learning process can be split into two parts. First, we're doing the training of the model itself, right? And for training parts, we can use x_training, that's the input data, and y_training, that's the labels we should get.
[00:04:12] So remember we need to provide input and output for model to modify their weights, right? And x_test and y_test, it's another set of similar inputs and outputs, but the one our model haven't seen yet. And those can be used to just test your model, to see what's the accuracy of the predictions you're gonna get.
[00:04:36] So let's actually visualize it. So what's what is train? If I just simply print out x_train, you will see that it looks like arrays, tons of arrays. So what if I just grab train 0, kinda the first array? Okay, a lot of zeros, and some other numbers, all right.
[00:05:02] That's pretty awesome, although it doesn't really clear how it looked like. I think if I zoom out just for a second, it will be slightly better. No, we still cannot see it easily. I know one trick. What I can do, I can, yes, import numpy as np. And in numpy, there is very good command called set_printoptions, yes, printoptions.
[00:05:40] And to this command, you can provide the linewidth. So let's return quickly back to the zoom. So what we did, we just import numpy as np and setting up the printoptions. Because I want full line to be printed, so I can put it to something really large, right, I don't care.
[00:06:00] All right, so now if I zoom out, You can actually see that it looks like number 5, right? So it's just the values of the intensity. So 0 is our background, and those numbers between 0 and 255 is just the intensity of ink. So instead of just printing out those numbers, I can even visualize them, it's gonna be easier.
[00:06:39] All right, back to 200%. What I can do, I can use matplotlib inline, import matplotlib.pyplot as plt. I'm just using this library for visualization. And I can, for instance, plt.imshow. imshow function is just showing images. And I can say I want to visualize x_train
. Let's see if it works.
[00:07:17] Yeah, it does, so basically in x_train
, so x_train is the list of images. And for instance, in second image, if we execute it, is our 0, right? And at the same time, if we print our ys, so y_train, for instance, that was number 5. So it actually shows us what the number we should recognize by looking at this image, right?
[00:07:51] And in index number 1, we should see 0, which is true. So that’s gonna be the data we will be using to train our model, right? But the thing is when we print it out, the numbers were between 0 and 255, right, integers. Our machine learning models, they usually work not with integers, but with floating point numbers, right?
[00:08:20] So what we will need to do, we need to, first of all, convert all of those inputs to floating points. And probably, we want to rescale it. It is recommended in a stabilization of your model for data and signal to be normalized and ideally kinda shrink to, well, as close to 0.
[00:08:42] So what we can do here, we can simply convert. So just all our x_trains, we can say we want all those to be divided by 255.0. So what this operation will do, it will take all individual pixels as numbers, divide them by 255. Which will automatically cast them into floating point numbers and write them back to the original location.
[00:09:21] So for instance, if I just do that and do the same thing with, not ys, x_test. And execute this. Now, for instance, if I just print x_train
, We'll see that all my numbers, which were distributed between 0 and 255, now shrink down to the values between 0 and 1.
[00:09:54] That's what I want, all right.