Found 26504 Articles for Server Side Programming

PyTorch – torchvision.transforms – RandomGrayscale()

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

555 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
Updated on 06-Jan-2022 11:01:13

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

How to 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

PyTorch – How to convert an image to grayscale?

Shahid Akhtar Khan
Updated on 06-Jan-2022 10:21:15

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

PyTorch – FiveCrop Transformation

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

918 Views

To crop a given image into four corners and the central crop, we apply FiveCrop() transformation. It's one of the transformations 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.FiveCrop() 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 a tensor image, then we first convert it ... Read More

PyTorch – Randomly change the brightness, contrast, saturation and hue of an image

Shahid Akhtar Khan
Updated on 06-Jan-2022 10:03:27

7K+ Views

To randomly change the brightness, contrast, saturation and hue of an image, we apply ColorJitter(). It's one of the transforms provided by the torchvision.transforms module. This module contains many important transformations that can be used to manipulate the image data.ColorJitter() transformation accepts both PIL and tensor images. A tensor image is a PyTorch 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. A batch of tensor images is a tensor with [B, C, H, W]. B is ... Read More

How to crop an image at center in PyTorch?

Shahid Akhtar Khan
Updated on 06-Jan-2022 09:43:59

5K+ Views

To crop an image at its center, we apply CenterCrop(). It's one of the transforms provided by the torchvision.transforms module. This module contains many important transformations that can be used to perform manipulation on the image data.CenterCrop() transformation accepts both PIL and tensor images. A tensor image is a PyTorch 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. A batch of tensor images is a tensor with [B, C, H, W]. B is the number of ... Read More

How to convert a PyTorch tensor with gradient to a numpy array?

Shahid Akhtar Khan
Updated on 06-Jan-2022 09:34:56

7K+ Views

To convert a Torch tensor with gradient to a Numpy array, first we have to detach the tensor from the current computing graph. To do it, we use the Tensor.detach() operation. This operation detaches the tensor from the current computational graph. Now we cannot compute the gradient with respect to this tensor. After the detach() operation, we use the .numpy() method to convert it to a Numpy array.If a tensor with requires_grad=True is defined on GPU, then to convert this tensor to a Numpy array, we have to perform one more step. First we have to move the tensor to ... Read More

Python – scipy.interpolate.interp1d

Syed Abeed
Updated on 24-Dec-2021 10:25:39

5K+ Views

The interp1d() function of scipy.interpolate package is used to interpolate a 1-D function. It takes arrays of values such as x and y to approximate some function y = f(x) and then uses interpolation to find the value of new points.Syntaxscipy.interpolate.interp1d(x, y)where x is a 1-D array of real values and y is an N-D array of real values. The length of y along the interpolation axis must be equal to the length of x.Example 1Let us consider the following example −# Import the required libraries import matplotlib.pyplot as plt import numpy as np from scipy import interpolate # ... Read More

Python – scipy.linalg.tanm()

Syed Abeed
Updated on 24-Dec-2021 10:12:31

228 Views

The tanm() function of scipy.linalg package is used to compute the tangent of an input matrix. This routine uses expm to compute the matrix exponentials.Syntaxscipy.linalg.tanm(x)where x is the input array or a square matrix. It returns the matrix tangent of x.Example 1Let us consider the following example −# Import the required libraries from scipy import linalg import numpy as np # Define the input array x = np.array([[69 , 12] , [94 , 28]]) print("Input array: ", x) # Calculate the Tangent a = linalg.tanm(x) # Display the Tangent of matrix print("Tangent of X: ", a)OutputIt will ... Read More

Advertisements