SoFunction
Updated on 2024-11-21

Pytorch's implementation of the sobel operator's convolution operation in detail

Convolution has two implementations in pytorch, one is .Conv2d() and the other is .conv2d(), both of which essentially perform convolution operations and have the same input requirements, the first thing that needs to be input is a () of type size (batch, channel, H, W) where batch denotes the number of batch of data input. channel denotes the number of channels in the input.

Generally a color image is 3, grayscale image is 1, and the number of channels in the convolutional network process is relatively large, there will be dozens to hundreds of channels.H and W denote the height and width of the input image, for example, a batch is 32 images, each image is a 3-channel, the height and width of the image is 50 and 100, then the size of the input is (32, 3, 50, 100).

The following code is an implementation of the convolutional execution soble edge detection operator:

import torch
import numpy as np
from torch import nn
from PIL import Image
from  import Variable
import  as F
 
 
def nn_conv2d(im):
  # Define convolution operations with nn.Conv2d
  conv_op = nn.Conv2d(1, 1, 3, bias=False)
  # Define the parameters of the sobel operator
  sobel_kernel = ([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype='float32')
  # Convert sobel operators into convolution kernels adapted to convolution operations
  sobel_kernel = sobel_kernel.reshape((1, 1, 3, 3))
  # Assign a value to the convolution kernel of a convolution operation
  conv_op. = torch.from_numpy(sobel_kernel)
  # Convolutional operations on images
  edge_detect = conv_op(Variable(im))
  # Convert output to image format
  edge_detect = edge_detect.squeeze().detach().numpy()
  return edge_detect
 
def functional_conv2d(im):
  sobel_kernel = ([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype='float32') #
  sobel_kernel = sobel_kernel.reshape((1, 1, 3, 3))
  weight = Variable(torch.from_numpy(sobel_kernel))
  edge_detect = F.conv2d(Variable(im), weight)
  edge_detect = edge_detect.squeeze().detach().numpy()
  return edge_detect
 
def main():
  # Read in an image and convert to grayscale
  im = ('./').convert('L')
  # Convert image data to matrices
  im = (im, dtype='float32')
  # Convert image matrices to pytorch tensor and adapt to convolutional input requirements
  im = torch.from_numpy(((1, 1, [0], [1])))
  # Edge detection operations
  # edge_detect = nn_conv2d(im)
  edge_detect = functional_conv2d(im)
  # Convert array data to image
  im = (edge_detect)
  # image data converted to grayscale mode
  im = ('L')
  # Save the picture
  ('', quality=95)
 
if __name__ == "__main__":
  main()

Original picture:

Result in photo:

Above this Pytorch implementation of sobel operator convolution operation details is all that I share with you, I hope to give you a reference, and I hope you support me more.