Compute Eigenvalues and Eigenvectors of a Square Matrix in PyTorch

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:08:11

2K+ Views

torch.linalg.eig() computes the Eigen value decomposition of a square matrix or a batch of square matrices. It accepts matrix and batch of matrices of float, double, cfloat and cdouble data types. It returns a named tuple (eigenvalues, eigenvectors). The eigenvalues and eigenvectors are always complex valued. The eigenvectors are given by columns of eigenvectors.Syntax(eigenvalues, eigenvectors) = torch.linalg.eig(A)Where A is a square matrix or a batch of square matrices. It returns a named tuple (eigenvalues, eigenvectors).StepsImport the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torchCreate a square matrix ... Read More

Invert Image Colors Randomly with Probability in PyTorch

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:47:32

731 Views

The RandomInvert() transform inverts the colors of an image randomly with a given probability. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data.RandomInvert() accepts both PIL and tensor images or batch of tensor images. A tensor image is a PyTorch Tensor with shape [3, H, W], where H is the image height and W is the image width. A batch of tensor images is also a torch tensor with [B, 3, H, W] where B is the number of images in the batch.Syntaxtorchvision.transforms.RandomInvert(p)(img)It returns a randomly color inverted ... Read More

Rotate an Image by an Angle in PyTorch

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:34:31

6K+ Views

RandomRotation() rotates an image by a random angle. The chosen random angle is from a given range of angles in degree. RandomRotation() is one of the many important transforms provided by the torchvision.transforms module. RandomRotation() transform accepts both PIL and tensor images.A tensor image is a Torch tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width. If the image is neither a PIL image nor a tensor image, then we first convert it to a tensor image and then apply the transform.Syntaxtorchvision.transforms.RandomRotation(degree)(img)Where degree is the ... Read More

Pad an Image on All Sides in PyTorch

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:32:59

3K+ Views

To pad an image on all sides, we can apply Pad() transform provided by the torchvision.transforms module. This module contains many important transformations that can be used to perform different types of manipulations on the image data.Pad() transformation accepts both PIL and tensor images or a batch of tensor images. A tensor image is a torch Tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.A batch of tensor images is also a torch tensor with shape [B, C, H, W]. B is the number of ... Read More

Compute Element-Wise Entropy of an Input Tensor in PyTorch

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:29:57

2K+ Views

To compute the element-wise entropy of an input tensor, we use torch.special.entr() method. It returns a new tensor with entropy computed element-wise.If the element of tensor is negative, the entropy is negative infinity.If the element of the tensor is a zero, the entropy is zero.The entropy for a positive number element is computed as the negative value of the element multiplied by its natural logarithm. It accepts torch tensor of any dimension.StepsWe could use the following steps to compute the entropy on a tensor element-wise −Import the required library. In all the following examples, the required Python library is torch. Make ... Read More

PyTorch Random Erasing Transform

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:04:01

2K+ Views

The RandomErasing() transform randomly selects a rectangular region in an input image and erases its pixels. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data. RandomErasing() transformation accepts only tensor images of any size. A tensor image is a torch tensor.As this transform supports only tensor image, the PIL images should be first converted to a torch tensor. And after applying the RandomErasing() transform, we convert torch tensor image to PIL image.StepsWe could use the following steps to randomly select a rectangular region in an input image and ... Read More

Normalize an Image with Mean and Standard Deviation in PyTorch

Shahid Akhtar Khan
Updated on 06-Jan-2022 12:28:30

7K+ Views

The Normalize() transform normalizes an image with mean and standard deviation. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data.Normalize() accepts only tensor images of any size. A tensor image is a torch tensor. A tensor image may have n number of channels. The Normalize() transform normalizes the tensor image for each channel.As this transform supports only tensor image, the PIL images should be first converted to a torch tensor. And after applying Normalize() transform, we convert the normalized torch tensor to a PIL image.StepsWe could use the ... Read More

PyTorch TorchVision Transforms GaussianBlur

Shahid Akhtar Khan
Updated on 06-Jan-2022 11:42:24

7K+ Views

The torchvision.transforms module provides many important transformations that can be used to perform different types of manipulations on the image data. GaussianBlur() transformation is used to blur an image with randomly chosen Gaussian blur.The GaussianBlur() transformation accepts both PIL and tensor images or a batch of tensor images. A tensor image is a PyTorch Tensor with shape [3, H, W], where H is the image height and W is the image width. A batch of tensor images is also a torch tensor with [B, 3, H, W] where B is the number of images in the batch.Syntaxtorchvision.transforms.GaussianBlur(kernel_size, sigma=(0.1, .2))(img)kernel_size – ... Read More

Resize an Image to a Given Size in PyTorch

Shahid Akhtar Khan
Updated on 06-Jan-2022 11:38:02

35K+ Views

The Resize() transform resizes the input image to a given size. It's one of the transforms provided by the torchvision.transforms module. Resize() accepts both PIL and tensor images. A tensor image is a torch tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.This transform also accepts a batch of tensor images, which is a tensor with [B, C, H, W] where B is the number of images in the batch. If the image is neither a PIL image nor a tensor image, then we first convert ... Read More

Random Vertical Flip Transformation in PyTorch

Shahid Akhtar Khan
Updated on 06-Jan-2022 11:34:18

1K+ Views

We apply RandomVerticalFlip() transform to flip an image vertically at a random angle with a given probability. It's one of the transforms provided by the torchvision.transforms module. This module contains many important transformations that can be used to perform different types of manipulations on the image data.RandomVerticalFlip() accepts both PIL and tensor images. A tensor image is a torch Tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.Syntaxtorchvision.transforms.RandomVerticalFlip(p)(img)If p = 1, it returns the vertically flipped image.If p = 0, It returns the original image.If ... Read More

Advertisements