Python, numpy and PyTorch will be used for the MPV labs. In case you are not familiar with them, study the following parts of the "Intro to numpy", "A Beginner-Friendly Guide to PyTorch and How it Works from Scratch", Pytorch for numpy users, |numpy for matlab users
Introduction into PyTorch Image Processing.
To fulfil this assignment, you need to submit these files (all packed in one .zip
file) into the upload system:
imagefiltering.ipynb
- a notebook for data initialisation, calling of the implemented functions and plotting of their results (for your convenience, will not be checked).
imagefiltering.py
- file with the following methods implemented:
gaussian1d
, gaussian_deriv1d
- functions for computing Gaussian function and its first derivative.
filter2d
, - function for applying 2d filter kernel to image tensor
gaussian_filter2d
, spatial_gradient_first_order
, spatial_gradient_second_order
- functions for Gaussian blur and 1st and 2nd image spatial gradients computation
affine
, - function for transforming 3 points in the image into affine transformation matrix
extract_affine_patches
, - function extraction of the patches, defined by affine transform A.
Use template of the assignment. When preparing a zip file for the upload system, do not include any directories, the files have to be in the zip file root.
import torch.nn.functional as F with torch.no_grad(): out = F.conv2d(in, weight)
gaussian1d(x,sigma)
that computes values of a (1D) Gaussian with zero mean and variance $\sigma^2$:x
.
gaussian_deriv1d(x,sigma)
that returns the first derivative of a Gaussianx
.
torch.nn.functional.conv2d
. Use padding mode “replicate” (see F.pad.)
from lab0_reference.imagefiltering import gaussian_filter2d inp = torch.zeros((1,1,32,32)) inp[...,15,15] = 1. imshow_torch(inp) sigma = 3.0 out = gaussian_filter2d(inp, sigma) imshow_torch(out)try to find out impulse responses of other combinations of the Gaussian and its derivatives.
filter2d(in,kernel)
that implements per-channel convolution of input tensor with kernel.
gaussian_filter2d(in,sigma)
of an input image tensor in with a Gaussian filter of width 2*ceil(sigma*3.0)+1
and variance $\sigma^2$ and returns the smoothed image tensor out (e.g. Lena). Exploit the separability property of Gaussian filter and implement the smoothing as two convolutions with one dimensional Gaussian filter (see function torch.nn.functional.conv2d
).
spatial_gradient_first_order(in,sigma)
that returns the estimate of the gradient (gx, gy) in each point of the input image in (BxChxHxW tensor) after smoothing with Gaussian with variance $\sigma^2$. Use either first derivative of Gaussian or the convolution and symmetric difference to estimate the gradient BxChx2xHxW tensor.
spatial_gradient_second_order(in,sigma)
that returns all second derivatives of input image in (BxChxHxW tensor) after smoothing with Gaussian of variance $\sigma^2$ BxChx3xHxW tensor. Make sure that it outputs zeros for constant inputs
extract_affine_patches(in,A,ps,ext)
that warps a patch from image in (BxChxMxN tensor) into canonical coordinate system. Affine transformation matrix A (3×3 elements) is a transformation matrix from the canonical coordinate system into image from previous task. The parameter ps defines the dimensions of the output image (the length of each side) and ext is a real number that defines the extent of the patch in coordinates of the canonical coordinate system. E.g. extract_affine_patches(in,A,41,3.0)
, returns the patch of size 1xChx41x41 pixels that corresponds to the rectangle (-3.0,-3.0)x(3.0,3.0) in the canonical coordinate system. Top left corner of the image has coordinates (0,0). Use bilinear interpolation for image warping. Check the functionality on this image.
Geometric transformations - review of course Digital image processing
Geometric transformations - hierarchy of transformations, homogeneous coordinates
You can check results of the functions required in this lab using the Jupyter notebook imagefiltering.ipynb.