Learn practical skills, build real-world projects, and advance your career

PyTorch for Image Processing

Image processing is one of the widely used operations in a wide number of real-world applications such as advanced driver assistance systems, autonomous driving, automation, etc. Operations in image processing could be computationally intensive and tedious to write. Machine learning is one of the upcoming streams to implement image processing. PyTorch is a promising machine learning library that could be used for this purpose. Let's explore some basic image processing matrix operations using functions from the PyTorch library.

  • Matrix Multiplication : tensor.matmul(tensor tensorOne, tensor tensorTwo, out=None)
  • Dot Product : tensor.dot(tensor tensorOne,tensorTwo)
  • Cross Product : tensor.cross(tensor tensorOne,tensor tensorTwo, dim=-1, out=None)
  • Historgram : torch.histc(tensor inputTensor, bins=100, min=0, max=0, out=None)
  • Eigen Values & Vectors : torch.eig(tensor inputTensor, eigenvectors=False, out=None)
# Import torch and other required modules
import torch

Matrix Multiplication : tensor.matmul(tensor tensorOne, tensor tensorTwo, out=None)

Matrix multiplication is one of the most basic image processing operations. Applications include edge detection, object detection, feature extraction and many more.

# Example 1 - simple matrix multiplication
tensorOne = torch.tensor([[1, 2], [3, 4.]])
tensorTwo = torch.tensor([[5., 6.], [7., 8.]])
print(tensorOne)
print(tensorTwo)
tensorOutput = torch.matmul(tensorOne, tensorTwo)
tensorOutput
tensor([[1., 2.], [3., 4.]]) tensor([[5., 6.], [7., 8.]])
tensor([[19., 22.],
        [43., 50.]])

Here we could see a simple matrix multiplication of matrices with rank 2