DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

transform vs target_transform vs transforms in PyTorch (1)

Buy Me a Coffee

*Memos:

There are the differences between transform, target_transform and transforms as shown below. *It's about origin and transform:
*Memos:

  • transform is for the function which must have one parameter for transform. *Resize(), RandomRotation(), RandomHorizontalFlip(), etc also can be used.
  • target_transform is for the function which must have one parameter for target(label).
  • transforms is for the function which must have two parameters for both transform and target(label). *Resize(), RandomRotation(), RandomHorizontalFlip(), etc also can be used.
  • Both transform and target_transform can be used at the same time.
  • transforms cannot be used with transform and/or target_transform at the same time.
  • According to my experiments, target_transform and transforms are useless.

<origin>

from torchvision.datasets import OxfordIIITPet

origin_data = OxfordIIITPet(
    root="data"
)

origin_data[0]
# (<PIL.Image.Image image mode=RGB size=394x500>, 0)

origin_data[50]
# (<PIL.Image.Image image mode=RGB size=500x333>, 1)

origin_data[100]
# (<PIL.Image.Image image mode=RGB size=333x500>, 2)
Enter fullscreen mode Exit fullscreen mode

<transform>

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import Resize

tfresize100_50_data = OxfordIIITPet(
    root="data",
    transform=Resize(size=[100, 50])
)

tfresize100_50_data[0]
# (<PIL.Image.Image image mode=RGB size=50x100>, 0)

tfresize100_50_data[50]
# (<PIL.Image.Image image mode=RGB size=50x100>, 1)

tfresize100_50_data[100]
# (<PIL.Image.Image image mode=RGB size=50x100>, 2)
Enter fullscreen mode Exit fullscreen mode
from torchvision.datasets import OxfordIIITPet

def tf_func(transform):
    return transform

tf_func_data = OxfordIIITPet(
    root="data",
    transform=tf_func
    # transform=lambda transform: transform    
)

tf_func_data[0]
# (<PIL.Image.Image image mode=RGB size=394x500>, 0)

tf_func_data[50]
# (<PIL.Image.Image image mode=RGB size=500x333>, 1)

tf_func_data[100]
# (<PIL.Image.Image image mode=RGB size=333x500>, 2)
Enter fullscreen mode Exit fullscreen mode
from torchvision.datasets import OxfordIIITPet

def tf_func(transform):
    return [0, 1, 2]

tf_func_data = OxfordIIITPet(
    root="data",
    transform=tf_func
    # transform=lambda transform: [0, 1, 2]
)

tf_func_data[0]
# ([0, 1, 2], 0)

tf_func_data[50]
# ([0, 1, 2], 1)

tf_func_data[100]
# ([0, 1, 2], 2)
Enter fullscreen mode Exit fullscreen mode
from torchvision.datasets import OxfordIIITPet

def tf_func():
    return [0, 1, 2]

tf_func_data = OxfordIIITPet(
    root="data",
    transform=tf_func
    # transform=lambda: [0, 1, 2]
)

tf_func_data[0]
# TypeError: tf_func() takes 0 positional arguments but 1 was given
Enter fullscreen mode Exit fullscreen mode
from torchvision.datasets import OxfordIIITPet

def tf_func(transform, target):
    return [0, 1, 2]

tf_func_data = OxfordIIITPet(
    root="data",
    transform=tf_func
    # transform=lambda transform, target: [0, 1, 2]
)

tf_func_data[0]
# TypeError: tf_func() missing 1 required positional argument: 'target'
Enter fullscreen mode Exit fullscreen mode

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

Join the MiniMax AI Agent Challenge — Build your first AI Agent 🤖

Developers, innovators, and AI tinkerers, build your AI Agent and win $150,000 in cash. 💰

Read more →

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

Your Python stack deserves better infra

Your Python stack deserves better infra

Stop duct-taping user flows together. Manage auth, access, and billing in one simple SDK with Kinde.

Get a free account

👋 Kindness is contagious

Take a moment to explore this thoughtful article, beloved by the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A heartfelt "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay