Pytorch Data Augmentation using Torchvision

Aladdin Persson · Beginner ·🧬 Deep Learning ·6y ago
Skills: CV Basics70%

Key Takeaways

PyTorch data augmentation using Torchvision, including transforms such as ToPILImage, RandomHorizontalFlip, ColorJitter, Resize, RandomCrop, and Normalize

Full Transcript

[Music] in this video we want to learn how to use PI torch inbuilt transforms on images so first if you're unfamiliar with data augmentation or wonder why you should use it essentially more data is always better when we train Internet and if we can get more for free by doing some transformations to our images it's almost always a good thing in this case we're loading our data using a custom data set which I've chose showed how to do in a previous video but that's really not really the focus in this video anyway you managed to load the data is fine in this case the data that we're working with is two pictures of cats and we want to apply some transformations to those and we want to see how they look like after we've transformed the transformations and perhaps most importantly we want to see how do we actually apply the transformations so let's see what we do first is we can actually just for image common label in dataset we can do print image dot shape yeah so we get two images which are colored so three channels RGB and 224 by 224 size the first thing I want to show you how to do is we're gonna use transforms dot compose alright let's go back so we're you can see we do one single transformation which is we convert the in this case numpy array to a tensor but let's say we want to do more transformations than this happen here okay so transforms that compose is what we're going to use to combine several different transformations I'll talk a little bit more about that later first of all we want to do is we're just going to use transforms that compose and we're gonna do transforms that to pill image that's the first thing we're gonna do because all the transformations work on this I guess format when we do it to pill image so that's usually what we do first then there are a bunch of this different transformations you can apply to two images we're gonna go through some of them but really there are many configurations of them you might want to read the documentation for those specifics but let's say we want to do transforms that random horizontal flip and we can input a probability here with which is default 0.5 and then in the end we can do transform start to tensor so for example the name is pretty self-explanatory but what it does is it flips the image horizontally so what we can do is we can do we'd be able to visualize this we can do save we can use the torch vision that utils save image that I've imported here so save image image with the name image plus string of image num which we're going to define here then we're gonna do plus PNG format and then we're just gonna do image num plus equals one and let's say we do that for we do that ten times so in total we will have 20 images and we just run that now if we go back to our folder here we have 20 images we can see that yeah so this one is flipped horizontally I believe that one yeah so some of them are flipped horizontally and and and that's kind of like a simple transformation to do one thing we could do as well is we could do transforms that color jitter with brightness 0.5 so what this does is apply some random brightness like change to the color to the image there are more things you can input here as well but yeah really I just want to go through some of the most common ones and you can choose for your specific case which one you think is best another one we can do is transform that resize so we first resize the image to let's say 256 by 256 and then we do some random crop of that image we do the crop to 24 - 24 then let's say we want to apply we could also apply rotation to the image right a rotated image of a cat still a cat so we can do transforms that random rotation and we input degrees with 45 for example um one more thing we could do transforms that random vertical flip but perhaps you know it's more common to see it's quite uncommon to see vertically flipped images of cats so maybe this is a low probability of 0.05 and yeah so those are some examples what we can do actually we can add some more we can do transforms that random gray scale with a probability of let's say twenty percent so this will convert the image to grayscale with a twenty percent probability another thing that is quite important to do which improves the training and quite a bit is a transform start normalized what you do after you have two tensor and for this you input a mean and a standard deviation and essentially what you want to do here is so for each channel in this case we have three channels you want to find the mean for that specific channel across all training examples and for all of the over all of the pixel values you would find that mean value and you would also find the standard deviation for all training examples across all pixel values and you would define those for each channel so there will be three values in this case then you would input them like this and like this now of course I don't know the mean value in the standard deviation for those two two images that I have in this case but you find those values first and then you do that and what it does it takes each value for that channel and then it subtracts it with the mean that you input it and then it divides by the standard deviation so in this case this would actually not do anything right since this would just subtract zero and divide by one perhaps we can write note this does nothing yeah but in practice you would find those values first and then you would use them I just want to add a comment here about transforms that compose that what it does is it applies all the transformations that we wrote inside to the image that we send in and it also does it in the order that we wrote it so it performs to peel image before resize and random crop after resize etc and since we have a lot of random transformations in the transform compose each time we send in an image we will get another image as output so ya know we've done a lot of transforms on our images let's run this I'll see yes we need a comma here anything and yeah another comma here it should be as small as yeah okay so now we've run it and let's see so here we got a rotation and it's also a grayscale grayscale again yeah so essentially you can see that the images still all look like cats except there are variants of that image and this is a bit weird but yeah still a cat so yeah that's an example of how you would use it now this is just we just do the images but really what you would do when training is you would just do a training loader here using the data loader and then you would train a network yeah so if you have any questions leave them in the comment below hopefully this was useful and thank you so much for watching the video

Original Description

In this video we look at an example of how to performs tranformations on images in Pytorch. This idea of expanding your dataset with transformed images is called data augmentation and is a very popular and effective method for achieving better performance. ❤️ Support the channel ❤️ https://www.youtube.com/channel/UCkzW5JSFwvKRjXABI-UTAkQ/join Paid Courses I recommend for learning (affiliate links, no extra cost for you): ⭐ Machine Learning Specialization https://bit.ly/3hjTBBt ⭐ Deep Learning Specialization https://bit.ly/3YcUkoI 📘 MLOps Specialization http://bit.ly/3wibaWy 📘 GAN Specialization https://bit.ly/3FmnZDl 📘 NLP Specialization http://bit.ly/3GXoQuP ✨ Free Resources that are great: NLP: https://web.stanford.edu/class/cs224n/ CV: http://cs231n.stanford.edu/ Deployment: https://fullstackdeeplearning.com/ FastAI: https://www.fast.ai/ 💻 My Deep Learning Setup and Recording Setup: https://www.amazon.com/shop/aladdinpersson GitHub Repository: https://github.com/aladdinpersson/Machine-Learning-Collection ✅ One-Time Donations: Paypal: https://bit.ly/3buoRYH ▶️ You Can Connect with me on: Twitter - https://twitter.com/aladdinpersson LinkedIn - https://www.linkedin.com/in/aladdin-persson-a95384153/ Github - https://github.com/aladdinpersson
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Aladdin Persson · Aladdin Persson · 38 of 60

1 computeCost.m Linear Regression Cost Function - Machine Learning
computeCost.m Linear Regression Cost Function - Machine Learning
Aladdin Persson
2 gradientDescent.m Gradient Descent Implementation -  Machine Learning
gradientDescent.m Gradient Descent Implementation - Machine Learning
Aladdin Persson
3 Neural Network from scratch - Part 1 (Standard Notation)
Neural Network from scratch - Part 1 (Standard Notation)
Aladdin Persson
4 Neural Network from scratch - Part 2 (Forward Propagation)
Neural Network from scratch - Part 2 (Forward Propagation)
Aladdin Persson
5 Neural Network from scratch - Part 3 (Backward Propagation)
Neural Network from scratch - Part 3 (Backward Propagation)
Aladdin Persson
6 Neural Network from scratch - Part 4 (With Python)
Neural Network from scratch - Part 4 (With Python)
Aladdin Persson
7 sigmoid.m - Programming Assignment 2 Machine Learning
sigmoid.m - Programming Assignment 2 Machine Learning
Aladdin Persson
8 costFunction.m - Programming Assignment 2 Machine Learning
costFunction.m - Programming Assignment 2 Machine Learning
Aladdin Persson
9 predict.m - Programming Assignment 2 Machine Learning
predict.m - Programming Assignment 2 Machine Learning
Aladdin Persson
10 costFunctionReg.m - Programming Assignment 2 Machine Learning
costFunctionReg.m - Programming Assignment 2 Machine Learning
Aladdin Persson
11 lrCostFunction.m - Programming Assignment 3 Machine Learning
lrCostFunction.m - Programming Assignment 3 Machine Learning
Aladdin Persson
12 oneVsAll.m - Programming Assignment 3 Machine Learning
oneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
13 predictOneVsAll.m - Programming Assignment 3 Machine Learning
predictOneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
14 predict.m - Programming Assignment 3 Machine Learning
predict.m - Programming Assignment 3 Machine Learning
Aladdin Persson
15 Caesar Cipher Encryption and Decryption with example
Caesar Cipher Encryption and Decryption with example
Aladdin Persson
16 Cryptography: Caesar Cipher Python
Cryptography: Caesar Cipher Python
Aladdin Persson
17 Vigenere Cipher Explained (with Example)
Vigenere Cipher Explained (with Example)
Aladdin Persson
18 Cryptography: Vigenere Cipher Python
Cryptography: Vigenere Cipher Python
Aladdin Persson
19 Hill Cipher Explained (with Example)
Hill Cipher Explained (with Example)
Aladdin Persson
20 Cryptography: Hill Cipher Python
Cryptography: Hill Cipher Python
Aladdin Persson
21 Interval Scheduling Greedy Algorithm: Python
Interval Scheduling Greedy Algorithm: Python
Aladdin Persson
22 Weighted Interval Scheduling Algorithm Explained
Weighted Interval Scheduling Algorithm Explained
Aladdin Persson
23 Weighted Interval Scheduling Python Code
Weighted Interval Scheduling Python Code
Aladdin Persson
24 Sequence Alignment | Needleman Wunsch Algorithm
Sequence Alignment | Needleman Wunsch Algorithm
Aladdin Persson
25 Sequence Alignment | Needleman Wunsch in Python
Sequence Alignment | Needleman Wunsch in Python
Aladdin Persson
26 Codility BinaryGap Python
Codility BinaryGap Python
Aladdin Persson
27 Codility CyclicRotation Python
Codility CyclicRotation Python
Aladdin Persson
28 Derivation Linear Regression with Gradient Descent
Derivation Linear Regression with Gradient Descent
Aladdin Persson
29 Linear Regression Gradient Descent From Scratch in Python
Linear Regression Gradient Descent From Scratch in Python
Aladdin Persson
30 Pytorch Neural Network example
Pytorch Neural Network example
Aladdin Persson
31 Pytorch CNN example (Convolutional Neural Network)
Pytorch CNN example (Convolutional Neural Network)
Aladdin Persson
32 Pytorch LeNet implementation from scratch
Pytorch LeNet implementation from scratch
Aladdin Persson
33 Pytorch VGG implementation from scratch
Pytorch VGG implementation from scratch
Aladdin Persson
34 Pytorch GoogLeNet / InceptionNet implementation from scratch
Pytorch GoogLeNet / InceptionNet implementation from scratch
Aladdin Persson
35 How to save and load models in Pytorch
How to save and load models in Pytorch
Aladdin Persson
36 How to build custom Datasets for Images in Pytorch
How to build custom Datasets for Images in Pytorch
Aladdin Persson
37 Pytorch Transfer Learning and Fine Tuning Tutorial
Pytorch Transfer Learning and Fine Tuning Tutorial
Aladdin Persson
Pytorch Data Augmentation using Torchvision
Pytorch Data Augmentation using Torchvision
Aladdin Persson
39 Pytorch Quick Tip: Weight Initialization
Pytorch Quick Tip: Weight Initialization
Aladdin Persson
40 Pytorch Quick Tip: Using a Learning Rate Scheduler
Pytorch Quick Tip: Using a Learning Rate Scheduler
Aladdin Persson
41 Pytorch ResNet implementation from Scratch
Pytorch ResNet implementation from Scratch
Aladdin Persson
42 Pytorch TensorBoard Tutorial
Pytorch TensorBoard Tutorial
Aladdin Persson
43 Pytorch DCGAN Tutorial (See description for updated video)
Pytorch DCGAN Tutorial (See description for updated video)
Aladdin Persson
44 Naive Bayes from Scratch - Machine Learning Python
Naive Bayes from Scratch - Machine Learning Python
Aladdin Persson
45 Spam Classifier using Naive Bayes in Python
Spam Classifier using Naive Bayes in Python
Aladdin Persson
46 K-Nearest Neighbor from scratch - Machine Learning Python
K-Nearest Neighbor from scratch - Machine Learning Python
Aladdin Persson
47 Linear Regression Normal Equation Python
Linear Regression Normal Equation Python
Aladdin Persson
48 SVM from Scratch - Machine Learning Python (Support Vector Machine)
SVM from Scratch - Machine Learning Python (Support Vector Machine)
Aladdin Persson
49 Neural Network from Scratch - Machine Learning Python
Neural Network from Scratch - Machine Learning Python
Aladdin Persson
50 Pytorch RNN example (Recurrent Neural Network)
Pytorch RNN example (Recurrent Neural Network)
Aladdin Persson
51 Pytorch Bidirectional LSTM example
Pytorch Bidirectional LSTM example
Aladdin Persson
52 Pytorch Text Generator with character level LSTM
Pytorch Text Generator with character level LSTM
Aladdin Persson
53 Logistic Regression from Scratch - Machine Learning Python
Logistic Regression from Scratch - Machine Learning Python
Aladdin Persson
54 K-Means Clustering from Scratch - Machine Learning Python
K-Means Clustering from Scratch - Machine Learning Python
Aladdin Persson
55 Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Aladdin Persson
56 Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Aladdin Persson
57 Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Aladdin Persson
58 Paper Review: Sequence to Sequence Learning with Neural Networks
Paper Review: Sequence to Sequence Learning with Neural Networks
Aladdin Persson
59 Pytorch Seq2Seq Tutorial for Machine Translation
Pytorch Seq2Seq Tutorial for Machine Translation
Aladdin Persson
60 Pytorch Seq2Seq with Attention for Machine Translation
Pytorch Seq2Seq with Attention for Machine Translation
Aladdin Persson

This video teaches how to use PyTorch and Torchvision to perform data augmentation on images, including various transformations such as flipping, jittering, resizing, and normalizing. The goal is to increase the size of the dataset and improve model performance.

Key Takeaways
  1. Import necessary libraries
  2. Load image data
  3. Apply transformations using Torchvision
  4. Visualize transformed images
  5. Use data loader to train a network
💡 Data augmentation is a technique to increase the size of the dataset by applying transformations to the existing images, which can improve model performance.

Related Reads

📰
Why Qwen3.8 27B Looked Brilliant in Testing but Failed to Ship My AI Newspaper
Learn why a high-performing AI model like Qwen3.8 27B failed to deliver in real-world application and how to avoid similar pitfalls
Medium · Deep Learning
📰
The 91% Model: What August’s Benchmarks Reveal About the Real Cost of Staying at the Frontier
The 91% model reveals the real cost of staying at the frontier of deep learning performance, questioning whether the last 9% is worth the investment
Medium · Deep Learning
📰
Nemotron 3.5 vs Qwen 3.6 vs Muse: I Thought Qwen Was Done. My Experiments Said Something Else.
Compare performance of Nemotron 3.5, Qwen 3.6, and Muse on a DGX Spark with 13 trick questions and a pressure test
Medium · Data Science
📰
Nemotron 3.5 vs Qwen 3.6 vs Muse: I Thought Qwen Was Done. My Experiments Said Something Else.
Compare performance of Nemotron 3.5, Qwen 3.6, and Muse on a DGX Spark with 13 trick questions and a pressure test
Medium · Deep Learning
Up next
how to run ablation in pretraining?
Deep Learning with Yacine
Watch →