The torch_hog library provides implementation for calculating the Histogram Of Oriented Gradients feature descriptors.
- [KNN] "Histograms of Oriented Gradients for Human Detection" Navneet Dalal and Bill Triggs - https://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf
%%bash
if !python -c "import torch_hog" 2>/dev/null; then
pip install https://github.com/Simon-Bertrand/HOG-PyTorch/archive/main.zip
fi
import torch_hog
!pip install -q scikit-image
�[1m[�[0m�[34;49mnotice�[0m�[1;39;49m]�[0m�[39;49m A new release of pip available: �[0m�[31;49m22.2.1�[0m�[39;49m -> �[0m�[32;49m24.0�[0m
�[1m[�[0m�[34;49mnotice�[0m�[1;39;49m]�[0m�[39;49m To update, run: �[0m�[32;49mpip install --upgrade pip�[0m
from skimage import feature
from skimage import data
import numpy as np
import torch
import torch_hog
image = data.astronaut().astype(np.float32)
im = torch.Tensor(image).moveaxis(-1, 0).unsqueeze(0)
featuresGt, hogImFt = feature.hog(
image,
orientations=9,
pixels_per_cell=(16, 16),
cells_per_block=(2, 2),
block_norm="L2",
visualize=True,
channel_axis=-1,
feature_vector=False,
)
hog = torch_hog.HOG(
cellSize=16,
blockSize=2,
kernel="finite",
normalization="L2",
accumulate="simple",
channelWise=False,
)
hogIm = hog.visualize(im, orthogonal=True)
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 2, figsize=(16, 16))
axes[0].imshow(image / 255)
axes[0].set_title("SkImage")
axes[0].imshow(
hogImFt, cmap="gray", alpha=0.75
)
axes[1].imshow(image / 255)
axes[1].set_title("torch_hog")
axes[1].imshow(
hogIm[0,0], cmap="gray", alpha=0.75
)
<matplotlib.image.AxesImage at 0x7fc1884eaa40>
features = hog(im)
assert (features[0,0]-torch.Tensor(featuresGt)).abs().max()<1e-6
(features[0,0]-torch.Tensor(featuresGt)).mean()
tensor(3.1590e-10)
hog = torch_hog.HOG(
cellSize=16,
blockSize=2,
kernel="finite",
normalization="L2",
accumulate="simple",
channelWise=False,
)
hogIm = hog.plotVisualize(im)
hog = torch_hog.HOG(
cellSize=16,
blockSize=2,
kernel="finite",
normalization="L2",
accumulate="simple",
channelWise=True,
)
hogIm = hog.plotVisualize(im)
hog = torch_hog.HOG(
cellSize=16,
blockSize=2,
kernel="finite",
normalization="L2",
accumulate="bilinear",
channelWise=False,
)
hogIm = hog.plotVisualize(im)