Hey, welcome back.
Remember, we had a section on COVAX a while back callbacks were methods we can run during a trading
process so we can print different things, we can load checkpoints, we can implement early stopping
and a few other things.
So we'll take a look and see how we can use that, how we can implement this in PyTorch lightning.
So this is just an illustration of really stopping this graph that I've used before.
So let's take a look at Model Checkpoint 10 in pretty much lightning.
So a model checkpoint as you call back is used to save a model, which at some interval, which we can
define so that the model waits can be little loaded for continuing training or just to just run to use
those, we had to run some inferences and evaluate the model at a time.
So all you have to do is just import early, stopping here from the early stop from PyTorch lightning
COVAX ili stopping and it and really stopping function.
Here we just specify what we want to monitor the patients, which is how many epochs we want to wait
before stopping strict and verbose other strategies that we can use.
The little boys adjust the output strict as a strategy.
We can use whatever we want it to strictly do this or not.
I'm not entirely sure how it's configured, but you can dig into the documentation and take a look.
Mode is equal to minimum, which allows yes, which means that we're looking at a minimum loss or another
maximum loss.
So you can just specify like if you're looking at accuracy, this would need to be set to MAX.
So let's run this block.
There we go.
And now let's look at a model check point one.
So again, from the PyTorch torch callbacks, we import model checkpoint.
We again specify we want to monitor validation loss.
We said the part we want to save on models, do we set a file name and notice that we can actually put
variables here as well?
These curly brackets indicate we're putting the epoch here as two decimal places validation laws as
well.
So you can actually see the file name, pretty descriptive file name as well for when you you're saving
the sequence.
And we can save the top three models if we want to save that.
And again, we set them more to minimum because we're looking at a loss, we want a minimum loss, not
a maximum.
Again, if this was the validation accuracy, you would use max here.
So let's run this and create our checkpoint callback and our early stop callback.
No.
What do we need to do here?
Note what we're doing actually right now is just creating a printing callback, which just allows us
to print outputs of different things during the training so we can just simply do it through this here.
So this inherits from this class here and this basically on the first start.
We're just printing this here on the first, on the end of it.
We are printing this now and then on the training and as well, we're printing this here.
So these are just different functions that we can use to print different things at different times.
And all we do is just print here, however you can do.
You can have any kind of code you want here to run basically on startup, on the end or at the training
end.
So let's create this printing callback.
I know this this one, there's a class here and we'll be passing, and now let's see how we train with
callbacks.
So again, we specify a batch size and learning rate.
Even though we won't be using those values.
Those are just values that we need to send in.
And because we're using the automatic selected that size and automatically selected learning, right?
We do.
We created a trainer and we just do p one trainer, APL sorry, trainer.
Specify We're using one GPU if you want is all we have available.
Set our maximum number of epochs set of progress, bar refresh rate and then specify all the callbacks
that we want to use right here in this array, just like in Keros how we did it.
And then once this training object has been configured for those parameters we can do trainer thought
fit model and that's about it.
And then it starts training.
It prints out the model summary here and you can see it's training right now, so we can just wait for
this as well.
Can we observe anything?
All right, so are executing the values from day and then we move on to the next epoch just like we
did before.
So this is pretty cool.
And you can see here we just printed out the 3D print printing things that we wanted to start here.
It's starting to initialize train and the train is now initialized.
And you can see those results here so that it's pretty cool.
So that's it.
For now, you can actually take a look at this in the intensive intensive board after it's finished.
And unfortunately, we can't run it live like you would on the local machine, where you can run tens
of boards separately and observe logs while it trains.
It doesn't work in CoLab, although there's a way to make it work, but it doesn't actually work naturally.
Like you'd expect, it's here.
So now let's take a look at this.
This is how we can restore from checkpoint to simply do a checkpoint callback, which is the object.
We created the best model part, and it returns to part of the best Mod. It's quite convenient, so
now we can load that model here, but just using the best model parts in this function when we want
to run some inferences.
So this is how we do it on model so we can do a lightning model.
Dart lowered from checkpoint specified that size, learning rate and a part which is given by this line
here.
And then we go the model so we can set the model to if you could change it to evaluation mode and then
freeze it and then we can run it as well.
So now let's take a look at number nine here.
This is how we save our model for production deployments so we can export our models here to a total
script model, which can be used in different inference engines.
We can use it probably on mobile as well.
You can use it on different servers.
You can convert it to an end video deep stream model as well.
The number of things you can do adopt models with the two and an X model.
All of those are model, the model type formats that can be run on different systems.
There isn't a full standard all wheel, and an X is supposed to be the standard for deep learning models.
But these things change quite quickly, and the way of it is what it is.
It's not a very easy field to be in because of how fast things are changing.
But for now, P2 models of what script models, which are very versatile and can be used in a number
of inference engines.
So to convert it, all you have to do is load a model here, then do Model two toward script script
and gives you the script violence variable called script.
And then we just see that using torched object that save.
And we just specify the file name here and the path.
And that's basically it.
So now let's take a look at how we can use that model.
We saved there to inference now.
So we just import too much vision support live.
And no, even though this may have been an imported previously, we get OGI, we set our device to GPU
if it's available GPU, meaning CUDA in this case.
And then what do we do?
We just take the validate.
Take 32 images from the validation data set by using converting it to a table and then getting the sample
something out of it.
We take the images here, not liberals, because we don't want to see the labels right now, and we
put those images onto CUDA and onto the GPU.
Then we just to create or plot that we're going to bring tight lipped because we want it to be nice
and compact.
Then we want to show you the results just yet.
Then what we do, we take the pre-trained model that we've loaded that was loaded up here.
And then what we do, we just do the regular stuff where we get the max class probability outputs and
we prayed.
Then we just loop through it here and get to have a lookup dictionary for its here.
So when we're actually putting it here, we can actually see the class name itself.
It's all quite simple and we can just visualize our predictions right there.
So that's pretty cool.
So I'll stop there for now.
And in the next section, we'll take a look at how we can use Page to watch lightning to do multi-GPU
training.
So that's a pretty cool feature that it offers.
So I'll see you there.
Thank you.