Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between transform & target_transform in pytorch?

Tags:

pytorch

Difference between transform & target_transform in pytorch?
Context:
Every TorchVision Dataset includes two arguments: transform and target_transform to modify the samples and labels respectively.

like image 841
smsubham Avatar asked Oct 16 '25 19:10

smsubham


1 Answers

If you look at the source code, particularly the __getitem__ method for any of the torchvision Dataset classes, e.g., torchvision.datasets.DatasetFolder, you can see that transform and target_transform are used to modify / augment / transform the image and the target respectively.

Examples where this might be useful include object detection and semantic segmentation, where if you apply a translation / rotation / shear / scale / crop on the source image, you would also want corresponding transforms on the bounding boxes / segmentation masks.

As an additional example, you can take a look at this official tutorial where target_transform is used to convert integer class labels to one-hot format for image classification.

like image 113
Riddhiman Dasgupta Avatar answered Oct 19 '25 13:10

Riddhiman Dasgupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!