Torchvision.transforms | Pytorch Tutorial 10 – Dataset Transforms

Python Engineer

Subscribe Here





Pytorch Tutorial 10 - Dataset Transforms


Hi, everybody! Welcome back to a new. Pi Torch Tutorial. This time. We want to talk about transforms for our data set. In the last tutorial, we use the Built-in data set and data loader classes, And if we use a built-in data set like we see here and we see that we can pass the transform argument to this data set and then apply some transforms. Soon, in this example, we use the built in M. This data set, and then we apply the two tensor transform, which will convert imagers or numpy arrays to tensors and pi torch already has a lot of transforms implemented for us. So please have a look at the official documentation, which you can find at this link and there you can see all the available transforms, and for example, there are some forms that can be applied to images, for example, sent a crop or grayscale or petting, and then there are transforms that can be applied to tens of us, like the linear transformation or normalize. Then there are conversion transforms, for example, that took pillow image and the two Tenza transform. Then there are also generic transforms, so we can use lambdas or we can write our very own custom class, and then we can also compose multiple transforms, so we can use transforms compose and then pass in a list which will apply multiple transforms after each other and yeah, so in the last tutorial we implemented a custom wine data set Now let’s extend this class to support our transforms and write our own transform classes. So let’s start and here. I copied the code from the last tutorial where we have our own custom wine data set, which will load the data and then we implement to get item and the Lang method, which will allow indexing and the length function. So let’s extend this data set so now this should also support the transform argument, so we put this in our in it and say transform. Oh, sorry, transform equals. So this is optional so by default. This is none and then in the init. We store this, so we say self dot transform equals transform, and now we also have to make some changes to our get item function so here we want to apply a transform if it’s available, so let’s say here sample equals this, and then we say if self dot transform, so if this is not none, then we apply this, so we say sample equals self dot, transform our sample, and then we simply return our sample, so let’s return sample, and this is all the change that we need for our data set, and now let’s continue and let’s create some custom transform classes. For example, we can write our own two tens or class. So, in the last tutorial, we already converted it to a tensor right here in this step, but we don’t need to do this so we can leave this as a numpy array and then let’s implement a two tensor class, which will then be passed to our data set and which we’re then later. Convert this to a ten. Zoa, so the class to tens or and the only thing that we need is that we need to implement. Is the double underscore all method, which will get self and a sample. So now this is a callable object and what we do here is first We unpack our samples, so we say inputs and labels or targets equal sample, and then we say return torch dot from numpy and here inputs and then also torch dot from Numpy targets. So here, also we return. We still want to return a tuple like we did here, and this is all that we need for our to tends our transform, and now we can pass this in here. So now we can say our wine Data set gets the transform transform equals to Tenza, which is a function and now let’s have a look at this, so let’s get the first item, so let’s say first. Data equals data set of index 0 and then let’s unpack our data so first data, so let’s say features and labels equals first data and now let’s print the type of the features and also the type of the labels. So now if we run this, then we should see this is now class. Torche Tenza. And if we don’t pass this in here, so if you say this is none know, transform, then we see that it’s still a numpy and dra. So this is how we write our own tens or our own transform and then apply it for our own data set and now let’s write, for example. I another custom transform, so let’s call this mul transform so a multiplication and here we imp implement the init method, so this has self and this has a factor argument. So here we store this self dot factor equals factor and then again we must implement the double underscore call function or call method, which gets self and the sample and here again. Let’s unpack our sample, so let’s say inputs and inputs and target equal sample and then it’s only applied factor to our features, so let’s say inputs. Ty is multiplied by our self dot factor and now let’s return our inputs or modified inputs in our target, like still as a tuple and so this is the multiplication transform, and now let’s apply this. Let’s apply a let’s say a compose transform in this case to see how we can use this, so let’s say composed equals, and here we need torch vision dot transforms dot compose, and here we put in a list of our transforms so here first we want to have two tens, or and then we want to have mall transform and let’s say so this needs a factor, so let’s say multiply it by two, and now let’s create a new data set equals swine data set, which gets the transform equals our compose transform. Come post and now again, let’s get this so. I’ll get or let’s just copy this from here and run this to see if this is working. So now here, we have a ten. Zoa, and let’s also have a look at, so let’s print the features, and you’re also print the features to see if the multiplication got applied so here now we should see that each value got doubled, and now let’s use another factor, so let’s multiply it by four and run this, and now you should see that each of the value should now be multiplied by four and, yeah, so this is how we can use to transform for our data sets and it’s very useful. Yeah, most of the time you see the conversion transform to tens or but also a lot of times when we work with images, you might see some of them. So yeah, please check that out on the documentation website. And I hope you like this tutorial. Please subscribe to the channel and see you next time bye and.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more