Skip to content

Add different skimage.hog blocks normalization methods - #2040

Merged
sciunto merged 1 commit into
scikit-image:masterfrom
soupault:fix_hog_norm
Jan 26, 2017
Merged

sciunto merged 1 commit into
scikit-image:masterfrom
soupault:fix_hog_norm

Conversation

@soupault

@soupault soupault commented Apr 7, 2016 •

Copy link
Copy Markdown
Member

References

This PR is catching up #1345 .
UPD: Fixes #2335, #2350.

@soupault soupault changed the title Fix skimage.hog normalization [WIP] Fix skimage.hog normalization Apr 7, 2016
@soupault soupault changed the title [WIP] Fix skimage.hog normalization Fix skimage.hog normalization Apr 7, 2016
@stefanv

stefanv commented Apr 7, 2016

Copy link
Copy Markdown
Member

@soupault Is the plan to add a test to this PR then?

@soupault

soupault commented Apr 8, 2016

Copy link
Copy Markdown
Member Author

@stefanv there is one already, which had been added after original PR: https://github.com/soupault/scikit-image/blob/fix_hog_norm/skimage/feature/tests/test_hog.py#L23-L30 . It actually hits the normalization method, that's why I've updated also the reference data skimage/data/astronaut_GRAY_hog.npy.

In the coming days I'd like to check if there's a divergence of the results with MATLAB/OpenCV ones and will update the PR if any.

@stefanv

stefanv commented Apr 8, 2016

Copy link
Copy Markdown
Member

Excellent. Then +1 on this.

@soupault

soupault commented Apr 9, 2016

Copy link
Copy Markdown
Member Author

As for MATLAB, unfortunately, I don't have CV toolbox.

As for OpenCV, it supports only uint8 images (both color and grayscale), and only L2-Hys for cell normalization. I've implemented L2-Hys inline and used the results on uint8_grayscale for comparison.
Here they are:

import numpy as np

import skimage
from skimage import data, color
import _hog as feature

import cv2


if __name__ == "__main__":
    img_rgb_float = data.astronaut()
    img_gray_float = color.rgb2gray(img_rgb_float)

    img_gray_uint = skimage.img_as_uint(img_gray_float).astype(np.uint8)
    img = img_gray_uint

    # Parameters
    win_size = img.shape
    win_stride = win_size
    block_size_pixels = (16, 16)
    block_size_cells = (2, 2)
    block_stride = (8, 8)
    cell_size = (8, 8)
    n_bins = 9
    deriv_aperture = False
    win_sigma = -1
    histogram_norm_type = 0
    l2_hys_thr = 0.2
    gamma_correction = False
    n_levels = 64
    padding = (0, 0)
    locations = ()

    # OpenCV
    hog = cv2.HOGDescriptor(win_size,
                            block_size_pixels,
                            block_stride,
                            cell_size,
                            n_bins,
                            deriv_aperture,
                            win_sigma,
                            histogram_norm_type,
                            l2_hys_thr,
                            gamma_correction,
                            n_levels)
    res_opencv = hog.compute(img,
                             win_stride,
                             padding,
                             locations)

    # skimage
    res_skimage = feature.hog(img,
                              orientations=n_bins,
                              pixels_per_cell=cell_size,
                              cells_per_block=block_size_cells,
                              transform_sqrt=gamma_correction)
    res_skimage = res_skimage.reshape(-1, 1)

    # Comparison
    print(res_opencv.shape)  
    print(res_skimage.shape)

    neq = res_opencv != res_skimage
    print(np.sum(neq))

    print(np.sum(np.isclose(res_opencv, res_skimage)))

    print(res_opencv[neq][:10])
    print(res_skimage[neq][:10])

Output:

(142884, 1)
(142884, 1)
137607
5281
[ 0.19842896  0.1645349   0.18767893  0.14802523  0.18860251  0.08718761
  0.13788545  0.12342861  0.12345016  0.20405114]
[ 0.22380796  0.08814926  0.16543179  0.04125849  0.22380796  0.05233872
  0.22380796  0.1140374   0.08486468  0.22380796]

Either I'm doing something wrong, or there is a major flaw in the implementation.

@soupault

soupault commented Apr 9, 2016

Copy link
Copy Markdown
Member Author

Here are the histograms(density=True) of output vectors (OpenCV results are for L2-Hys everythere, as anticipated earlier):

skimage L1 skimage L2 skimage L2-Hys
l1 l2 l2hys

@soupault

soupault commented Apr 9, 2016

Copy link
Copy Markdown
Member Author

@sdsingh I bet you would like to have a look at this.

@soupault

soupault commented Apr 9, 2016

Copy link
Copy Markdown
Member Author

xref #2011

@soupault

soupault commented Apr 9, 2016

Copy link
Copy Markdown
Member Author

OpenCV is a pain 🔪 .
It appears that they use gaussian weighting scheme within cells: 1, 2.

Authors of the article are unclear about this: they mention Gaussian spatial window with sigma = 8pixel among the parameters of their "default" implementation (section 6, beginning of page 4), but do not directly recommend to use any kind of smoothing. Our current "magnitude/area" is amongst recommended approaches: see section 6.3, end of page 4.


If noone objects, I'm pushing L2-Hys blocks' normalization to this PR, updating the test and that's it.

@soupault

soupault commented Apr 10, 2016 •

Copy link
Copy Markdown
Member Author

For those who are wondering (after GIMP +50 contrast, +30 brightness; the difference is not so obvious, though):

L1 L2-hys
l1 l2hys

UPD 08.06.2016, code to reproduce comparison:

import numpy as np

import skimage
from skimage import data, color
import _hog as feature

import cv2


# Reference links:
# 1. http://scikit-image.org/docs/dev/api/skimage.feature.html#skimage.feature.hog
# 2. https://github.com/scikit-image/scikit-image/blob/master/skimage/feature/_hog.py
# 3. https://github.com/scikit-image/scikit-image/blob/master/skimage/feature/_hoghistogram.pyx
# 4. http://docs.opencv.org/3.1.0/d5/d33/structcv_1_1HOGDescriptor.html#gsc.tab=0
# 5. http://docs.opencv.org/2.4/modules/gpu/doc/object_detection.html#gpu-hogdescriptor-hogdescriptor


if __name__ == "__main__":
    img_rgb_float = data.astronaut()
    img_gray_float = color.rgb2gray(img_rgb_float)

    img_gray_uint = skimage.img_as_uint(img_gray_float).astype(np.uint8)
    # img_gray_uint = skimage.img_as_ubyte(img_gray_float)

    img = img_gray_uint

    # Parameters
    win_size = img.shape
    win_stride = win_size
    block_size_pixels = (16, 16)
    block_size_cells = (2, 2)
    block_stride = (8, 8)
    cell_size = (8, 8)
    n_bins = 9
    deriv_aperture = False
    win_sigma = 4
    histogram_norm_type = cv2.HOGDescriptor_L2Hys
    l2_hys_thr = 0.2
    gamma_correction = False
    n_levels = 64
    padding = (0, 0)
    locations = ()

    # OpenCV
    hog = cv2.HOGDescriptor(win_size,
                            block_size_pixels,
                            block_stride,
                            cell_size,
                            n_bins,
                            deriv_aperture,
                            win_sigma,
                            histogram_norm_type,
                            l2_hys_thr,
                            gamma_correction,
                            n_levels)
    res_opencv = hog.compute(img,
                             win_stride,
                             padding,
                             locations)

    # skimage
    res_skimage = feature.hog(img,
                              orientations=n_bins,
                              pixels_per_cell=cell_size,
                              cells_per_block=block_size_cells,
                              transform_sqrt=gamma_correction)
    res_skimage = res_skimage.reshape(-1, 1)

    # Comparison
    print(res_opencv.shape)  # (142884, 1)
    print(res_skimage.shape)  # (142884, 1)

    neq = res_opencv != res_skimage
    print(np.sum(neq))

    print(np.sum(np.isclose(res_opencv, res_skimage)))

    print(res_opencv[neq][:10])
    print(res_skimage[neq][:10])


    from bokeh.plotting import figure, show, output_file, vplot, save

    p1 = figure()
    hist_opencv, _ = np.histogram(res_opencv, density=True,
                                  bins=100, range=(0, 0.25))
    hist_skimage, _ = np.histogram(res_skimage, density=True,
                                  bins=100, range=(0, 0.25))

    x = np.linspace(0, 0.25, 100)
    p1.line(x, hist_opencv, line_color="red", alpha=0.7, legend="opencv")
    p1.line(x, hist_skimage, line_color="green", alpha=0.7, legend="skimage")

    p1.legend.location = "top_right"
    output_file('zzz.html')
    save(p1)

@soupault

Copy link
Copy Markdown
Member Author

@scikit-image/core could anyone, please, give some feedback on this?

@soupault

Copy link
Copy Markdown
Member Author

@scikit-image/core still noone? 😉

@soupault soupault closed this Jun 8, 2016
@soupault soupault reopened this Jun 15, 2016
@soupault soupault added ⏩ type: Enhancement Improve existing features and removed ⚖️ Needs decision labels Jun 15, 2016
@soupault soupault changed the title Fix skimage.hog normalization Add different skimage.hog blocks normalization methods Jun 15, 2016
@soupault

soupault commented Jun 15, 2016 •

Copy link
Copy Markdown
Member Author

@scikit-image/core OK, I've made this backward-compatible while still addressing the original issue. Will be really glad to see some reviews. Thanks!

@stefanv

stefanv commented Jun 17, 2016

Copy link
Copy Markdown
Member

@soupault I like this PR--it's an improvement, it should be merged. But it also feels like an ideal opportunity to expand the Notes section of the docstring to explain what's happening underneath the hood (you mention a lot of useful things in your comments above that I don't see encapsulated in the code itself).

@soupault
soupault force-pushed the fix_hog_norm branch 2 times, most recently from 6866ae6 to ff063ca Compare June 17, 2016 09:38
@soupault

Copy link
Copy Markdown
Member Author

@stefanv Updated. Would you have a time to look through this once again?

I also had to change the reference .npy due to a new (better) way of handling boundary conditions for gradients.

Comment thread skimage/feature/_hog.py Outdated
Number of cells in each block.
block_norm : string, optional
Block normalization method.
Has to be one of ('L1', 'L1-sqrt', 'L2', 'L2-Hys').

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No explanation of L2-Hys

@soupault

Copy link
Copy Markdown
Member Author

@sciunto indeed, thanks! Fixed.

@soupault

Copy link
Copy Markdown
Member Author

I really hope this goes in one day.

@sciunto

sciunto commented Dec 25, 2016

Copy link
Copy Markdown
Member

It should! @stefanv We are waiting for your approval :P

@jni

jni commented Dec 30, 2016

Copy link
Copy Markdown
Member

@soupault it looks like this needs another rebase!

@soupault

soupault commented Dec 30, 2016 •

Copy link
Copy Markdown
Member Author

@jni Done. Hope you like the PR, and this is the last rebase, as I've already mastered my rebasing skills. :)

@sciunto

sciunto commented Dec 30, 2016

Copy link
Copy Markdown
Member

🥇 for rebasing skills :)

@sciunto

sciunto commented Jan 5, 2017

Copy link
Copy Markdown
Member

@jni @stefanv Could you have a look at this PR please?

@soupault
soupault dismissed stefanv’s stale review January 13, 2017 19:41

All addressed

@soupault
soupault force-pushed the fix_hog_norm branch 2 times, most recently from b1f34b2 to b09e35d Compare January 18, 2017 07:58
@codecov-io

codecov-io commented Jan 18, 2017 •

Copy link
Copy Markdown

Current coverage is 90.66% (diff: 84.61%)

Merging #2040 into master will decrease coverage by 0.04%

@@             master      #2040   diff @@
==========================================
  Files           304        304          
  Lines         21511      21468    -43   
  Methods           0          0          
  Messages          0          0          
  Branches       1853       1848     -5   
==========================================
- Hits          19513      19464    -49   
- Misses         1658       1661     +3   
- Partials        340        343     +3   

Powered by Codecov. Last update 2b4e24d...b09e35d

@soupault

Copy link
Copy Markdown
Member Author

JFYI: it required one more rebase. 💪

@sciunto
sciunto requested review from jni and stefanv January 18, 2017 08:17
Comment thread skimage/feature/_hog.py
Normalization using L1-norm, followed by square root.
``L2``
Normalization using L2-norm.
``L2-Hys``

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@sciunto

sciunto commented Jan 25, 2017

Copy link
Copy Markdown
Member

@soupault please rebase once more and then, I merge it.

@soupault

Copy link
Copy Markdown
Member Author

@sciunto Done.

updated reference npys, renewed deprecation message
@sciunto
sciunto merged commit 5c0a810 into scikit-image:master Jan 26, 2017
@sciunto

sciunto commented Jan 26, 2017

Copy link
Copy Markdown
Member

Champagne!

@stefanv

stefanv commented Sep 26, 2017

Copy link
Copy Markdown
Member

Yikes, it's quite painful reading this PR history---I'm sorry @soupault that you struggled so much to get feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⏩ type: Enhancement Improve existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants