Skip to content

Add multichannel support to feature.hog - #2870

Merged
jni merged 11 commits into
scikit-image:masterfrom
soupault:hog_multichannel
Nov 24, 2017
Merged

jni merged 11 commits into
scikit-image:masterfrom
soupault:hog_multichannel

Conversation

@soupault

@soupault soupault commented Nov 4, 2017 •

Copy link
Copy Markdown
Member

Description

This PR implements the support for multichannel images for HOG descriptor.
Gallery example has been modified to use an RGB image.

Before:
figure_1

With this PR:
figure_2

Checklist

[It's fine to submit PRs which are a work in progress! But before they are merged, all PRs should provide:]

[For detailed information on these and other aspects see scikit-image contribution guidelines]

References

Closes #1879.
Based on #1963.

For reviewers

(Don't remove the checklist below.)

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.

@codecov-io

codecov-io commented Nov 4, 2017 •

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@0fa3bcb). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #2870   +/-   ##
=========================================
  Coverage          ?   86.25%           
=========================================
  Files             ?      335           
  Lines             ?    26895           
  Branches          ?        0           
=========================================
  Hits              ?    23198           
  Misses            ?     3697           
  Partials          ?        0
Impacted Files Coverage Δ
skimage/feature/tests/test_hog.py 98.83% <100%> (ø)
skimage/feature/_hog.py 94.44% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0fa3bcb...e02eba7. Read the comment docs.

@soupault

Copy link
Copy Markdown
Member Author

This one is ready for review.

@soupault
soupault requested review from jni and sciunto November 19, 2017 10:27

@jni jni left a comment

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.

@soupault very nice! I've left some comments, to hopefully improve gradient computation and coordinate notations. =)

Comment thread skimage/feature/_hog.py Outdated
gy[-1, :] = 0
gy[1:-1, :] = channel[2:, :] - channel[:-2, :]

return gx, gy

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.

I feel like this should be using np.gradient or something? Also, gr, gc. ;)

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.

Or np.convolve

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jni

I feel like this should be using np.gradient or something

That's how it is in the paper (see #2809 (comment)).

Or np.convolve

Specially for you :) :

from datetime import datetime                                                                                                                                                                 
import numpy as np                                                                                                                                                                            
from scipy.signal import convolve2d                                                                                                                                                           
from skimage import data                                                                                                                                                                      
                                                                                                                                                                                              
                                                                                                                                                                                              
if __name__ == '__main__':                                                                                                                                                                    
    img = data.coins().astype('float')                                                                                                                                                        
                                                                                                                                                                                              
    N = 1000                                                                                                                                                                                  
                                                                                                                                                                                              
    def grad_diff(image):                                                                                                                                                                     
        gx = np.empty(image.shape, dtype=np.double)                                                                                                                                           
        gx[:, 0] = 0                                                                                                                                                                          
        gx[:, -1] = 0                                                                                                                                                                         
        gx[:, 1:-1] = image[:, 2:] - image[:, :-2]                                                                                                                                            
        gy = np.empty(image.shape, dtype=np.double)                                                                                                                                           
        gy[0, :] = 0                                                                                                                                                                          
        gy[-1, :] = 0                                                                                                                                                                         
        gy[1:-1, :] = image[2:, :] - image[:-2, :]                                                                                                                                            
        return gx, gy                                                                                                                                                                         
                                                                                                                                                                                              
    def grad_conv(image):                                                                                                                                                                     
        gx = convolve2d(image, np.array([[-1, 0, 1]]),                                                                                                                                        
                        mode='same', boundary='fill', fillvalue=0)                                                                                                                            
        gy = convolve2d(image, np.array([[-1], [0], [1]]),                                                                                                                                    
                        mode='same', boundary='fill', fillvalue=0)                                                                                                                            
        return gx, gy                                                                                                                                                                         
                                                                                                                                                                                              
    time_start = datetime.now()                                                                                                                                                               
    for i in range(N):                                                                                                                                                                        
        out1 = grad_diff(img)                                                                                                                                                                 
    time_elapsed = datetime.now() - time_start                                                                                                                                                
    print('Duration grad_diff: {}'.format(time_elapsed))                                                                                                                                      
                                                                                                                                                                                              
    time_start = datetime.now()                                                                                                                                                               
    for i in range(N):                                                                                                                                                                        
        out2 = grad_conv(img)                                                                                                                                                                 
    time_elapsed = datetime.now() - time_start                                                                                                                                                
    print('Duration grad_conv: {}'.format(time_elapsed)) 
Duration grad_diff: 0:00:01.874263
Duration grad_conv: 0:00:08.140994

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.

Wow! Well, that sucks. FWIW, ndimage.correlate is significantly faster, but not as much as your ugly manual diff method. (Sorry =P)

 $ python timegrad.py
Duration grad_diff: 0:00:00.692133
Duration grad_conv: 0:00:05.982160
Duration grad_corr: 0:00:02.190535

Comment thread skimage/feature/_hog.py
just before returning.
multichannel : boolean, optional
If True, the last `image` dimension is considered as a color channel,
otherwise as spatial.

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.

Since HOG doesn't work for 3D images, perhaps this could have a default value of "None"? Then if multichannel is None: multichannel = (image.ndim == 3). I know we have a "don't guess" policy, but my feeling is that this applies when there is ambiguity.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jni Make sure you add this comment to #2613 . 😉
I'd be fine with the proposed change.

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.

How you keep all of these issue cross-references in your head, I'll never know. =D Done.

Comment thread skimage/feature/_hog.py Outdated
Returns
-------
newarr : ndarray
out : ndarray

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.

What is the shape of out?

Comment thread skimage/feature/_hog.py Outdated
gx_by_ch[:, :, idx_ch], gy_by_ch[:, :, idx_ch] = \
_hog_channel_gradient(image[:, :, idx_ch])
g_magn[:, :, idx_ch] = np.hypot(gx_by_ch[:, :, idx_ch],
gy_by_ch[:, :, idx_ch])

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.

gr, gc, or maybe better: row_gradient, col_gradient

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jni I'm going to change x, y to c, r, but notice that the underlying Cython function still use x, y notation, and the API is being parsed as x, y.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, done for Cython func, but the API issue holds.
Also, there are too many changes in this PR now, nobody will review it 😞 .

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm breaking x, y notation for pixels_per_cell and cells_per_block parameters. I think the current implementation is very confusing as the notation is not specified anywhere in the docstring, andm in addition, the returned HOG descriptor uses r, c notation.

@soupault

Copy link
Copy Markdown
Member Author

@jni All addressed.

@jni jni left a comment

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.

@soupault correction: one person will review. =P Usually that mrg+1 label helps bring someone else in. =)

@jni

jni commented Nov 23, 2017

Copy link
Copy Markdown
Member

@soupault if you like, add this to release_dev! =)

@grlee77 grlee77 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good to me. see my minor suggestion for using a sparse meshgrid. Using row/col has made the code easier to read.

Comment thread skimage/feature/_hog.py Outdated
idcs_max = g_magn.argmax(axis=2)
rr, cc = np.meshgrid(np.arange(image.shape[0]),
np.arange(image.shape[1]),
indexing='ij')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can also set sparse=True in meshgrid to save memory. The indexing below should still work by broadcasting.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, done!

Comment thread skimage/feature/_hog.py
g_magn[:, :, idx_ch] = np.hypot(g_row_by_ch[:, :, idx_ch],
g_col_by_ch[:, :, idx_ch])

# For each pixel select the channel with the highest gradient magnitude

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I haven't worked with HOG previously. Is this the standard approach to dealing with color images or is it something that should be mentioned specifically in the Notes of the docstring?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is the recommended method from the original paper.

Comment thread skimage/feature/_hog.py
image = np.atleast_2d(image)

if multichannel is None:
multichannel = (image.ndim == 3)

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.

What happens when alpha images land here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AFAIK, most of our functions don't work well with RGBA (only some filters are implemented in an appropriate way).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In any case, this line was proposed by @jni, so, please, consider blaming him 😄 . There was no guessing code in the previous version of this PR.

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.

This would still work totally fine with an RGBA image.

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.

And the result will be identical to using an RGBA image with multichannel=True, as you would. Namely, the gradients in the alpha layer will "count". Dunno if that's what you want, but this line isn't the problem.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, of course, you are right. I'll try to find some info on HOG for RGBA. In the original paper there weren't any hints on this case.

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.

I misread; thought this checked the final dimension, but see it only checks the number of dimensions.

@jni

jni commented Nov 24, 2017

Copy link
Copy Markdown
Member

Hey, lots of +1s! I think I'll merge now. =) Thanks @soupault!

@jni
jni merged commit a5b4808 into scikit-image:master Nov 24, 2017
@soupault
soupault deleted the hog_multichannel branch November 24, 2017 07:01
@soupault

soupault commented Nov 24, 2017 •

Copy link
Copy Markdown
Member Author

Thanks to everyone! 🍾

@stefanv

stefanv commented Nov 27, 2017

Copy link
Copy Markdown
Member

Another one down!! Thanks, @soupault

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.

6 participants