In this lecture we are going to look at a code lab notebook that does transfer learning without data
augmentation.
This lecture is going to walk you through a prepared code lab notebook.
Although a very good exercise which I always recommend is once you know how this is done to try and
recreate it yourself with as few references as possible as usual you can look at the title of the notebook
to determine what notebook we are currently looking at.
And this notebook is called Pi torch transfer learning fast so our main goal in this script is to see
if we pre compute the features a Will training B faster and B Well we get an unacceptable reduction
in accuracy I'll assume that in this lecture you've already seen the code that does transfer learning
with data augmentation.
So I'm not going to explain any of the lines that I've already explained so at the top everything is
the same as before.
Same imports same data same process to move the data into the folders which are structured the right
way for the image folder data set object
OK.
So when we get to the transforms you'll notice that we only have one.
Why do we only need one.
Well since we're not going to do any data augmentation we can apply the same transformations to both
the train and test that and these are all non data augmentation and transformations
so next we create our image folder objects.
Next we create our data load our objects.
Next I grab the pre train model with prearranged equals true
so the next part is also new
since the V-J operation is separate from the rest of the network.
We can define a separate model for it so I'll define a class called Veggie features in the constructor.
The only thing I need to do is save the VB model in the forward function.
I take X and I pass it through the different parts of the e.g. one at a time until right before the
final classifier.
At that point I just flatten and then return
next I instantiate this class and I call it VEGF next.
As a sanity check just to make sure this works we pass in a random input output a shape one by twenty
five thousand eighty eight.
So it works.
Next we're going to transform our data using the feature transformer that we just instantiated.
So first we need to find the length of each dataset.
Luckily we can use the lend function on the data set objects
so we have entrained three thousand and test one as.
Next we're gonna grab D the feature dimensionality.
This is the size of the output of the VEGF transformer which is equal to twenty five thousand eighty
eight.
Next we're going to work on creating our tabular data set consisting of transform the features from
the original data.
Once we have this we no longer have to work with the original dataset.
So we're going to start by instantiating empty arrays to store X train y train x test and Y test.
Next we're going to set the device to the GP you and move the feeder transformer to the GP you
next we're going to populate X train and Y training.
We can do this by looping over the train at dataset.
So on each iteration we start by moving the inputs to the GP you it's not necessary to move the targets
that the GP you because we only want to make a prediction with the model.
Next we get the output by passing the inputs through VEGF.
Next we get the size of the output.
Note that this is not equal to the back size since the number of samples may not divide evenly into
the batch size.
So the final batch is less than or equal to the batch size next we assign the features and the targets
to X train and Y train at the current index ie.
Then we increment I by the current that size and go to the next iteration of the loop so since we print
ie on each iteration we can confirm that the number of samples in our new dataset is equal to the original
number of images.
All right so we can see that the final value of AI is three thousand which is the correct number of
samples.
Next we do the same thing for X test and Y test.
So one thing that's interesting to check is the men in the max of the feature values.
As you can see the max in X train is about sixty two which should seem weird to you because you should
expect it values in a neuron that work to be somewhat normalized.
Since we're using a real you activation at the minimum value with zero
so anyway since the data is at this weird scale we're going to use a standard scalar object to transform
it so we'll call the scale data X train to an X test to
now because the next step is to do logistic regression.
There's no actual reason we need to use pi torch.
In fact we can just use the logistic regression that's built into socket learn so let's do that.
As usual it's just fit and predict which I've taught you for free in the past so everybody knows this.
And as you can see we do very well just as well as when we did use data augmentation in fact.
So maybe data augmentation wasn't so useful in this instance.
Next we're going to complete this script by doing transfer learning the usual way which is to use the
same library for the classifier.
So let's start by creating a linear model and the rest of these steps you've seen before so we move
the model to the GP you create the loss and optimizer make some tensor data set objects create the data
loaders define the training function call the training function blood loss preparation and calculate
the accuracy
and so we end up with pretty much the same result
also important.
Notice how much faster this is with data augmentation.
Each epoch takes about 40 seconds without data augmentation each epoch is nearly instant about zero
point two seconds.
So that's a two hundred x speed up by not using data augmentation and not having to go through the pre
train network on each pass.