Pytorch Learning -3 [TRANSFORMS]

Pytorch Learning -3 [TRANSFORMS]

TRANSFORMS

Data does not always come in its final processed form that is required for training machine learning algorithms. We use transforms to perform some manipulation of the data and make it suitable for training.

All TorchVision datasets have two parameters -

1)transform to modify the features 

2)target_transform to modify the labels 

that accept callables containing the transformation logic.The torchvision.transforms module offers several commonly-used transforms out of the box.


Consider the FahionMNIST features are in PIL image format,and the labels are integers .Just for sake of training,the features of normalized tensors are required and the labels ae one-hot encoded tensors.In order to make this transformations, ToTensor and Lambda is used.


import torch
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda


ds = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=ToTensor(),
    target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)        

ToTensor()

ToTensor converts a PIL image or NumPy ndarray into a FloatTensor. and scales the image’s pixel intensity values in the range [0., 1.]


Lambda Transforms

Lambda transforms apply any user-defined lambda function. Here, we define a function to turn the integer into a one-hot encoded tensor. It first creates a zero tensor of size 10 (the number of labels in our dataset) and calls scatter which assigns a value=1 on the index as given by the label y.


target_transform = Lambda(lambda y: torch.zeros(
    10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))        

Functional Transforms

Functional transforms give you fine-grained control of the transformation pipeline. As opposed to the transformations above, functional transforms don’t contain a random number generator for their parameters. That means you have to specify/generate all parameters, but the functional transform will give you reproducible results across calls.

you can apply a functional transform with the same parameters to multiple images


import torchvision.transforms.functional as TF
import random


def my_segmentation_transforms(image, segmentation):
    if random.random() > 0.5:
        angle = random.randint(-30, 30)
        image = TF.rotate(image, angle)
        segmentation = TF.rotate(segmentation, angle)
    # more transforms ...
    return image, segmentation        

 you can use a functional transform to build transform classes with custom behavior


import torchvision.transforms.functional as TF
import random


class MyRotationTransform:
    """Rotate by one of the given angles."""


    def __init__(self, angles):
        self.angles = angles


    def __call__(self, x):
        angle = random.choice(self.angles)
        return TF.rotate(x, angle)


rotation_transform = MyRotationTransform(angles=[-30, -15, 0, 15, 30])        

Transforms are common image transformations available in the torchvision.transforms module. They can be chained together using Compose. Most transform classes have a function equivalent: functional transforms give fine-grained control over the transformations. This is useful if you have to build a more complex transformation pipeline (e.g. in the case of segmentation tasks).


To view or add a comment, sign in

More articles by Dhanushkumar R

  • MLOPS -Getting Started .....

    What is MLOps? MLOps (Machine Learning Operations) is a set of practices and principles that aim to streamline and…

    1 Comment
  • Pydub

    Audio files are a widespread means of transferring information. So let’s see how to work with audio files using Python.

    1 Comment
  • Introduction to Python libraries for image processing(Opencv):

    Image processing is a crucial field in computer science and technology that involves manipulating, analyzing, and…

    1 Comment
  • @tf.function

    Learning Content:@tf.function @tf.

  • TEXT-TO-SPEECH Using Pyttsx3

    Pyttsx3 : It is a text to speech conversion library in Python which is worked in offline and is compatible with both…

    2 Comments
  • Web Scraping

    Web scraping is the process of collecting structured web data in an automated manner. It's also widely known as web…

  • TORCHAUDIO

    Torchaudio is a library for audio and signal processing with PyTorch. It provides I/O, signal and data processing…

  • Getting Started With Hugging Face-Installation and setUp

    Hub Client Library : The huggingface_hub library allows us to interact with Hugging FaceHub wich is a ML platform…

  • Audio Features of ML

    Why audio? Description of sound Different features capture different aspects of sound Build intelligent audio system…

  • Learning Path: "Voice and Sound Recognition"

    Chapter 1: SOUND AND WAVEFORMS The concept that I learnt from this content is fundamental concepts related to sound and…

Insights from the community

Others also viewed

Explore topics