PyTorch Tutorial 10 - Dataset Transforms
Skills:
ML Pipelines80%
Key Takeaways
This video tutorial demonstrates how to use dataset transforms in PyTorch, including built-in transforms and custom transforms, to manipulate data in a dataset.
Full Transcript
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 were 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
Original Description
New Tutorial series about Deep Learning with PyTorch!
⭐ Check out Tabnine, the FREE AI-powered code completion tool I use to help me code faster: https://www.tabnine.com/?utm_source=youtube.com&utm_campaign=PythonEngineer *
In this part we learn how we can use dataset transforms together with the built-in Dataset class.
Apply built-in transforms to images, arrays, and tensors. Or write your own custom Transform classes.
- Dataset Transforms
- Use built-in Transforms
- Implement custom Transforms
Part 10: Dataset Transforms
📚 Get my FREE NumPy Handbook:
https://www.python-engineer.com/numpybook
📓 Notebooks available on Patreon:
https://www.patreon.com/patrickloeber
⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN
If you enjoyed this video, please subscribe to the channel!
Official website:
https://pytorch.org/
Part 01:
https://youtu.be/EMXfZB8FVUA
Logistic Regression from scratch:
https://youtu.be/JDU3AzH3WKg
Code for this tutorial series:
https://github.com/patrickloeber/pytorchTutorial
You can find me here:
Website: https://www.python-engineer.com
Twitter: https://twitter.com/patloeber
GitHub: https://github.com/patrickloeber
#Python #DeepLearning #Pytorch
----------------------------------------------------------------------------------------------------------
* This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Patrick Loeber · Patrick Loeber · 44 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
▶
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
PyTorch Tutorial 01 - Installation
Patrick Loeber
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber
More on: ML Pipelines
View skill →Related Reads
📰
📰
📰
📰
I Found the Neural Network I Built in Class 9 — Here’s What Happened When I Tried to Run It Again
Medium · Deep Learning
Introduction to Deep Learning and Neural Networks: From Human Brain to Artificial Intelligence
Medium · Deep Learning
Want to get started with deep learning
Reddit r/deeplearning
Building a Deepfake Detector From Scratch — What Nobody Tells You
Medium · Deep Learning
🎓
Tutor Explanation
DeepCamp AI