New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image.extract_patches_2d fails if max_patches > all_patches #10100

Closed
varunagrawal opened this Issue Nov 9, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@varunagrawal
Contributor

varunagrawal commented Nov 9, 2017

Description

When calling image.extract_patches_2d on an image of say 512 to get a patch size of 512, the _compute_n_patches function raises an exception since the case where max_patches >= all_patches is not handled.

This happens when we have a pool of images of varying sizes and it would be tedious to handle this simple corner case.

Steps/Code to Reproduce

import numpy as np
from sklearn.feature_extraction import image
from PIL import Image

def sample_patches(img, patch_size=512, max_patches=50):

    if isinstance(patch_size, int):
        patch_size = (patch_size, patch_size)
    
    patches = image.extract_patches_2d(img, patch_size, max_patches)
    return patches

patches = sample_patches(np.array(Image.open("sample_512_image.jpg")))

Expected Results

The code should return the max number of patches allowable, i.e. all_patches.
So if it encounters an image of size 512 from a pool of images of varying sizes and we request a patch size of 512, this would simply return a single patch of size 512 instead of erroring out.

Actual Results

Traceback (most recent call last):
  File "sample_patches.py", line 49, in <module>
    main()
  File "sample_patches.py", line 37, in main
    patches = sample_patches(np.array(img), args.patch_size)
  File "sample_patches.py", line 12, in sample_patches
    patches = image.extract_patches_2d(img, patch_size, max_patches)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/feature_extraction/image.py", line 378, in extract_patches_2d
    n_patches = _compute_n_patches(i_h, i_w, p_h, p_w, max_patches)
  File "/usr/local/lib/python3.5/dist-packages/sklearn/feature_extraction/image.py", line 236, in _compute_n_patches
    raise ValueError("Invalid value for max_patches: %r" % max_patches)
ValueError: Invalid value for max_patches: 50

Versions

Linux-4.4.0-97-generic-x86_64-with-Ubuntu-16.04-xenial
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609]
NumPy 1.13.3
SciPy 0.18.1
Scikit-Learn 0.19.1

@agramfort

This comment has been minimized.

Show comment
Hide comment
@agramfort

agramfort Nov 9, 2017

Member
Member

agramfort commented Nov 9, 2017

@varunagrawal varunagrawal changed the title from `image.extract_patches_2d` fails if max_patches > all_patches to image.extract_patches_2d fails if max_patches > all_patches Nov 9, 2017

@varunagrawal

This comment has been minimized.

Show comment
Hide comment
@varunagrawal

varunagrawal Nov 9, 2017

Contributor

@agramfort it breaks because the check for max_patches is only a "less than" instead of "less than or equal to". I have implemented fix for this which takes care of that.

About your first point, I was under the impression that max_patches represents max possible patches, i.e. the number of patches returned is upper bounded by max_patches but returning less is a possibility if the image is not large enough.
Perhaps this can be easily fixed by updating the docs?

Contributor

varunagrawal commented Nov 9, 2017

@agramfort it breaks because the check for max_patches is only a "less than" instead of "less than or equal to". I have implemented fix for this which takes care of that.

About your first point, I was under the impression that max_patches represents max possible patches, i.e. the number of patches returned is upper bounded by max_patches but returning less is a possibility if the image is not large enough.
Perhaps this can be easily fixed by updating the docs?

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