Check out a free preview of the full Machine Learning in JavaScript with TensorFlow.js course

The "Training the Model with Image Data" Lesson is part of the full, Machine Learning in JavaScript with TensorFlow.js course featured in this preview video. Here's what you'd learn in this lesson:

Charlie creates the module to execute the loading of the images and the training and testing of the model. Loss and accuracy metrics are logged to the console to highlight how well the model performs based on the inputs.

Preview
Close

Transcript from the "Training the Model with Image Data" Lesson

[00:00:00]
>> So we're going to move to our train drawings file, and there again should be nothing in there yet, and we're going to write everything. So if we do it the same way as before, const tf, and we're going to require tensorflow, the node gpu, that's gonna be needed to actually run the training, and we're going to be able to import some of the functions that we wrote in our get data file.

[00:00:28]
So we're going to load or to import loadData, so okay, loadData, okay, getTrainData. And then we have geTestData as well, and we are going to oops, oops, require that from our get data file. So that should be our three main functions that we're going to need. And then we also need to import the model itself.

[00:01:02]
So const model, and we're gonna require create model here. So at this point, we should have our kind of functions that are gonna help us load the data and only retrieve the test set and we also have access to the model itself. So then we can start creating our function.

[00:01:27]
Okay, so I called it run, but maybe let's call it train, makes more sense. So we have const train and it's going be an async function. So like this. And first of all, we're going to load our data. So we load the data, and if we remember what this does here is that it's calling the loadImages here.

[00:01:58]
Okay, and what we're going to do, is then we're going to be able to actually get our images, so trainImages here, and then we're going to get the labels as well. It's gonna be trainLabels and that will be from getTrainingData. Oops, yeah, getTrainData, sorry. We're loading it and then we're only requiring the training image in the training label.

[00:02:37]
And then what we can do before we, actually no we can do that now. So then, if you remember from yesterday, the method to call to train the model is called .fit. So if we do a wait, and then model.fit, and the first two arguments are gonna be training data and training label, so here will trainImages and then trainLabels.

[00:03:05]
And then there's a few extra parameters. So if you remember from yesterday as well, what epochs are so epoch is just the number of times or the number of steps that the training model is going to go through. So here for epochs, let's say we're going to do, how many do I have here?

[00:03:28]
10, okay, we're gonna do 10 steps to start with, and then again, you can change that. The more epochs you have, the longer it's going to train. But also you might have better accuracy. And if you use a lower number, you might have lower accuracy but it go faster.

[00:03:42]
So again, it will depend. There's so many different parameters because it also depends on number of images that you have. Okay, so we have epochs and we also need a batchSize. So we have a batchSize. That's the number of images that are going to be used for each of the training step.

[00:03:58]
I picked five we can change that later as well. And then the third argument that you can pass it as well is a validationSplit. And what that means, I'm gonna declare it, we're gonna use 0.2. ValidationSplit, yeah, and that is different from the testing part. So the validationSplit means that here we're gonna use 20% of our training images to actually validate that it predicts the right label before we even go into the testing set as well.

[00:04:36]
So testing, when you run into the testing of a model it's more about feeding it images never seen before and see if it actually has the right label. And the validation is using the training data already. So you're going through the process, you're using the training data, you're learning from it, and through each of the step, you're using kind of 20% or here, yes, here 20% of your training data, and you're just checking, okay, I just gave you that training data do you recognize the data you've already seen?

[00:05:04]
And if already that's good, then the testing set should do a even better evaluation because it's given a data it hasn't seen. So, this is not required, but usually I guess it's, I guess it would be good to pass it anyway. To kind of train our model with data it's already seen and then we can do the testing part of it.

[00:05:26]
So here again, this fit method is going to do the training. And now if we move on to the testing, you can copy the line that you had above where we were requiring the training images and training labels. So instead of calling it from trainImages, we have testImages and then testLabels and we're calling a function getTestData.

[00:05:50]
So this is going to get our objects of tensors, images and labels from the test set from under our test folder in our data folder. And the way to do the testing here is going to be to call the evaluate method. So first we can call a function or a variable called evalOutput and we're going to call model.evaluate and we're going to pass it these testImages and testLabels.

[00:06:23]
So testImages and then testLabels. And at this point, what is useful is to actually then log this into the terminal so that when we run the training process, we'll have an output every time that we tests things and we'll be able to see how the accuracy changes over the courses of the course of the training process.

[00:06:45]
So the way that we can do this is maybe we're gonna create a variable called loss, and it's going to have the evalOutput, the first element of the evalOutput. And then there's a method you can do to extract the data that's called dataSync. And we're going to get the first value of that as well, and then toFixed3.

[00:07:13]
And I mean, we can see even if we don't use that, what's coming back of it, but that's the way that I had it. And then the accuracy is the second element of that evaluation output. So here if we change to accuracy, we have evalOutput the first element, and we can do the rest just to format the numbers in the same way.

[00:07:33]
And then we can have a console.log and we can have, maybe we can have two just to format it properly. So we can do have loss, and then we can pass that as loss here, and then below we can have accuracy, accuracy, okay. So I'm tempted at this point, there's another step, but maybe let's first check that it even runs, okay.

[00:08:00]
So here, if you call your train function, let's see if at least we're not getting any error, hopefully. Okay, so if you're at this point and we're calling train, you can go to your terminal, and the way to run this is to do node and then you should have train-drawings.js.

[00:08:23]
If we run this, no, okay, so I know why. So here it's complaining because when we declared our one hot tensor, it's telling you it must be an int 32, but actually we passed it for 32. So it's something where I would try to improvise and see what it would change.

[00:08:43]
But what I like sometimes is that even if you do trial and error, tensor flow will tell you what's wrong. So if we go back to our, it probably would be in our getData file. So here where we're creating one hot tensor for the labels, we just need to change it to int 32, instead of 32 for both, for both training data and then test data as well.

[00:09:12]
So once this is done, it seemed to, hopefully that will work. So if we run node train-drawings.js again, okay, so it is going through sorry. So another thing that I miss coded here, I can fix that error coz I wanna talk about what's up there. But here I just need to, actually I'm gonna do it now.

[00:09:34]
So instead of I wrote model.evaluation, but this is model.evaluate. I think the autocomplete was probably, didn't help me here. Okay, so what's happening here is that we have trained our model. We haven't saved it yet, that will be what's happening after, but here, yeah, so I was right here I didn't call any await or anything, so it console logged everything at the same time anyway.

[00:09:58]
So here we can see that, if you remember, in terms of number of epochs I believe we had 10 and we can see here, so you have the output of, okay, the first step of the training process. So one out of 10 we are getting here an accuracy of 0.49, which is not even half, so it is we're not doing very well, and then a very high loss.

[00:10:17]
So I'm actually not exactly sure what these are compared to acccuracy and loss. But usually I look at these ones here. And the issue as well is that as you see, when we go through the subsequent steps of the training process, so epoch 2, 3, the value actually doesn't change, it stays the same.

[00:10:34]
So there is definitely here something that we need to change and we can experiment a little bit with that. And at the end of the last training process, we have only an accuracy of 0.5. So at this point, we're not gonna tweak our model yet. We're probably going to keep going with the exercise to have our full implementation ready.

[00:10:56]
And then we are going to tweak some parameters and then retrain it and see how it impacts also the experience in the UI.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now