Shahid Akhtar Khan has Published 216 Articles

How to move a Torch Tensor from CPU to GPU and vice versa?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 06-Nov-2023 03:42:20

22K+ Views

A torch tensor defined on CPU can be moved to GPU and vice versa. For high-dimensional tensor computation, the GPU utilizes the power of parallel computing to reduce the compute time.High-dimensional tensors such as images are highly computation-intensive and takes too much time if run over the CPU. So, we ... Read More

How to normalize a tensor in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 31-Oct-2023 03:57:21

22K+ Views

A tensor in PyTorch can be normalized using the normalize() function provided in the torch.nn.functional module. This is a non-linear activation function.It performs Lp normalization of a given tensor over a specified dimension.It returns a tensor of normalized value of the elements of original tensor.A 1D tensor can be normalized ... Read More

How to join tensors in PyTorch?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 14-Sep-2023 13:58:38

27K+ Views

We can join two or more tensors using torch.cat(), and torch.stack(). torch.cat() is used to concatenate two or more tensors, whereas torch.stack() is used to stack the tensors. We can join the tensors in different dimensions such as 0 dimension, -1 dimension.Both torch.cat() and torch.stack() are used to join the ... Read More

How to convert a NumPy ndarray to a PyTorch Tensor and vice versa?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 12-Sep-2023 03:08:40

29K+ Views

A PyTorch tensor is like numpy.ndarray. The difference between these two is that a tensor utilizes the GPUs to accelerate numeric computation. We convert a numpy.ndarray to a PyTorch tensor using the function torch.from_numpy(). And a tensor is converted to numpy.ndarray using the .numpy() method.StepsImport the required libraries. Here, the ... Read More

How to convert a Torch Tensor to PIL image?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 10-Sep-2023 08:19:41

34K+ Views

The ToPILImage() transform converts a torch tensor to PIL image. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data. ToPILImage() accepts torch tensors of shape [C, H, W] where C, H, and W are the number of channels, ... Read More

How to find the bounding rectangle of an image contour in OpenCV Python?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 28-Aug-2023 13:57:14

22K+ Views

The bounding rectangle of an object is a rectangle drawn around an object in the image. There are two methods to find the bounding rectangle in OpenCV − Straight Bounding Rectangle It is a straight rectangle as it does not consider the rotation of an object. It can be computed ... Read More

How to rotate an image in OpenCV Python?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 28-Aug-2023 13:20:58

25K+ Views

OpenCV provides us the function cv.rotate() to rotate an image (NumPy array) in multiples of 90 degrees. This function rotates an image in three possible ways: 90, 180, and 270 degrees clockwise. We use the following syntax − Syntax cv2.rotate(img, rotateCode) rotateCode is a rotate flag specifying how to ... Read More

How to detect a rectangle and square in an image using OpenCV Python?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 26-Aug-2023 08:26:18

31K+ Views

To detect a rectangle and square in an image, we first detect all the contours in the image. Then Loop over all contours. Find the approximate contour for each of the contours. If the number of vertex points in the approximate contour is 4 then we compute the aspect ratio ... Read More

How to change the contrast and brightness of an image using OpenCV in Python?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 26-Aug-2023 08:24:50

34K+ Views

In OpenCV, to change the contrast and brightness of an image we could use cv2.convertScaleAbs(). The syntax we use for this method is as follows − cv2.convertScaleAbs(image, alpha, beta) Where image is the original input image. alpha is the contrast value. To lower the contrast, use 0 < ... Read More

How to normalize an image in OpenCV Python?

Shahid Akhtar Khan

Shahid Akhtar Khan

Updated on 26-Aug-2023 03:42:32

36K+ Views

We use the function cv2.normalize() to normalize an image in OpenCV. This function accepts the parameters- src, dst, alpha, beta, norm_type, dtype and mask. src and dst are input image and output of the same size as input, alpha is lower norm value for range normalization, beta is upper norm ... Read More

1 2 3 4 5 ... 22 Next
Advertisements