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

A short presentation of chosen PyTorch functions

Related to working with images

In this notebook I'll present a few PyTorch functions which I found useful when operating on images. Many of these functions could also be used when working with any tensor (since images are represented as tensors as well), but I will show how they work (and fail) in a context of working with images.

Below there is the list of functions:

  • reshape
  • clamp
  • comparisons
  • histc
  • unfold
# Import torch and other required modules
!pip install torch
!pip install torchvision
!pip install matplotlib
!pip install pillow

import torch
import matplotlib.pyplot as plt
from PIL import Image
from torchvision.transforms import transforms
Requirement already satisfied: torch in /srv/conda/envs/notebook/lib/python3.7/site-packages (1.5.0) Collecting future Downloading future-0.18.2.tar.gz (829 kB) |████████████████████████████████| 829 kB 3.5 MB/s eta 0:00:01 Requirement already satisfied: numpy in /srv/conda/envs/notebook/lib/python3.7/site-packages (from torch) (1.18.1) Building wheels for collected packages: future Building wheel for future (setup.py) ... done Created wheel for future: filename=future-0.18.2-py3-none-any.whl size=491056 sha256=24d5987b5ca2f19ff3e996df7025ec65e6d9aec8d3ab5dd0aee2c455cbeac0b1 Stored in directory: /home/jovyan/.cache/pip/wheels/56/b0/fe/4410d17b32f1f0c3cf54cdfb2bc04d7b4b8f4ae377e2229ba0 Successfully built future Installing collected packages: future Successfully installed future-0.18.2 Collecting torchvision Downloading torchvision-0.6.0-cp37-cp37m-manylinux1_x86_64.whl (6.6 MB) |████████████████████████████████| 6.6 MB 3.5 MB/s eta 0:00:01 Requirement already satisfied: numpy in /srv/conda/envs/notebook/lib/python3.7/site-packages (from torchvision) (1.18.1) Requirement already satisfied: torch==1.5.0 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from torchvision) (1.5.0) Collecting pillow>=4.1.1 Downloading Pillow-7.1.2-cp37-cp37m-manylinux1_x86_64.whl (2.1 MB) |████████████████████████████████| 2.1 MB 33.5 MB/s eta 0:00:01 Requirement already satisfied: future in /srv/conda/envs/notebook/lib/python3.7/site-packages (from torch==1.5.0->torchvision) (0.18.2) Installing collected packages: pillow, torchvision Successfully installed pillow-7.1.2 torchvision-0.6.0 Collecting matplotlib Downloading matplotlib-3.2.1-cp37-cp37m-manylinux1_x86_64.whl (12.4 MB) |████████████████████████████████| 12.4 MB 3.2 MB/s eta 0:00:01 |█ | 409 kB 3.2 MB/s eta 0:00:04 Collecting kiwisolver>=1.0.1 Downloading kiwisolver-1.2.0-cp37-cp37m-manylinux1_x86_64.whl (88 kB) |████████████████████████████████| 88 kB 11.2 MB/s eta 0:00:01 Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB) |████████████████████████████████| 67 kB 7.2 MB/s eta 0:00:01 Collecting cycler>=0.10 Downloading cycler-0.10.0-py2.py3-none-any.whl (6.5 kB) Requirement already satisfied: numpy>=1.11 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib) (1.18.1) Requirement already satisfied: python-dateutil>=2.1 in /srv/conda/envs/notebook/lib/python3.7/site-packages (from matplotlib) (2.8.1) Requirement already satisfied: six in /srv/conda/envs/notebook/lib/python3.7/site-packages (from cycler>=0.10->matplotlib) (1.14.0) Installing collected packages: kiwisolver, pyparsing, cycler, matplotlib Successfully installed cycler-0.10.0 kiwisolver-1.2.0 matplotlib-3.2.1 pyparsing-2.4.7 Requirement already satisfied: pillow in /srv/conda/envs/notebook/lib/python3.7/site-packages (7.1.2)

Displaying examples

To view the images (created or modified) or any other plot I'm gonna use a matplotlib library (you can find more information on this at the link at the end of notebook). Below I define functions that will be used through all the notebook:

%%html
<style>
.output_png {
    display: flex;
    justify-content: center;
}
</style>
def show_img(img, figsize=(3, 3), cmap="gray", title=None):
    plt.figure(dpi=150, figsize=figsize)
    plt.axis("off")
    plt.imshow(img, cmap=cmap, vmin=0, vmax=1)
    if title is not None:
        plt.title(title)
    plt.tight_layout()

def show_hist(bin_counts, vmin, vmax, figsize=(3, 3), title=None):
    bins = len(bin_counts)
    plt.figure(dpi=150, figsize=figsize)
    if title is not None:
        plt.title(title)
    plt.hist(torch.linspace(vmin, vmax, bins), bins=bins, weights=bin_counts)