Shahid Akhtar Khan

Shahid Akhtar Khan

169 Articles Published

Articles by Shahid Akhtar Khan

Page 15 of 17

PyTorch – How to compute element-wise entropy of an input tensor?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 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 – torchvision.transforms – RandomErasing()

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 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

PyTorch – How to normalize an image with mean and standard deviation?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 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
Shahid Akhtar Khan
Updated on 06-Jan-2022 8K+ 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

PyTorch – How to resize an image to a given size?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 36K+ 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

PyTorch – torchvision.transforms – RandomResizedCrop()

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 3K+ Views

RandomResizedCrop() transform crops a random area of the original input image. This crop size is randomly selected and finally the cropped image is resized to the given size. RandomResizedCrop() transform is one of the transforms provided by the torchvision.transforms module. This module contains many important transforms that can be used to perform different types of manipulations on the image data.RandomResizedCrop() accepts both PIL and tensor images. A tensor image is a PyTorch tensor with shape [..., H, W], where ... means a number of dimensions, H is the image height, and W is the image width. If the image is ...

Read More

PyTorch – torchvision.transforms – RandomHorizontalFlip()

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 3K+ Views

To flip an image horizontally in a random fashion with a given probability, we apply RandomHorizontalFlip() transform. 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.RandomHorizontalFlip() accepts both PIL and tensor images. A tensor image is a PyTorch Tensor with shape [C, H, W], where C is the number channels, H is the image height, and W is the image width.Syntaxtorchvision.transforms.RandomHorizontalFlip(p)(img)If p = 1, it returns a horizontally flipped image.If p = 0, It returns the original image.If p ...

Read More

PyTorch – torchvision.transforms – RandomGrayscale()

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 672 Views

To randomly convert an image to grayscale with a probability, we apply RandomGrayscale() transformation. 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 manipulations on the image data.RandomGrayscale() 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]. B is the number of images in the batch.Syntaxtorchvision.transforms.RandomGrayscale(p)(img)If ...

Read More

PyTorch – How to crop an image at a random location?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 3K+ Views

To crop an image at a random location, we apply RandomCrop() transformation. It's one of the many important transforms provided by the torchvision.transforms module.The RandomCrop() transformation 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 tensor image, then we first convert it to a tensor image and then apply RandomCrop().Syntaxtorchvision.transforms.RandomCrop(size)(img)where size is the desired crop size. size is a sequence like (h, w), where h ...

Read More

PyTorch – How to convert an image to grayscale?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 8K+ Views

To convert an image to grayscale, we apply Grayscale() transformation. 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 manipulations on the image data.Grayscale() 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]. B is the number of images in the batch.Syntaxtorchvision.transforms.Grayscale()(img)It ...

Read More
Showing 141–150 of 169 articles
Advertisements