Skip to content
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

HerdNet requires input images of 512x512 at minimum #553

Closed
1 of 2 tasks
calebrob6 opened this issue Jan 14, 2025 · 1 comment
Closed
1 of 2 tasks

HerdNet requires input images of 512x512 at minimum #553

calebrob6 opened this issue Jan 14, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@calebrob6
Copy link
Member

Search before asking

  • I have searched the Pytorch-Wildlife issues and found no similar bug report.

Bug

Following https://github.com/microsoft/CameraTraps/blob/main/demo/image_detection_demo_herdnet.ipynb with a path to an image that is 256x256 results in the following error:

RuntimeError: Expected size of input's dimension 1 to be divisible by the product of kernel_size, but got input.size(1)=81920 and kernel_size=(256, 256).

Resizing the image to 512x512 makes this error go away. It seems to be caused by the default parameters of the stitcher.

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@calebrob6 calebrob6 added the bug Something isn't working label Jan 14, 2025
idchacon28 added a commit that referenced this issue Jan 25, 2025
… with dimension less than 512 are scaled to use default herdnet values
zhmiao added a commit that referenced this issue Jan 25, 2025
@idchacon28
Copy link
Collaborator

idchacon28 commented Jan 27, 2025

Hello @calebrob6,

Thank you for reporting this issue. You are correct that HerdNet requires a minimum image size of 512x512 due to the default parameters of the stitcher, and inputting an image with smaller dimensions causes a runtime error.

To resolve this, I have implemented a ResizeIfSmaller class that resizes an image to the minimum required size if it is smaller. This ensures compatibility with the default stitcher parameters without manual resizing. Here's the code for the class:

class ResizeIfSmaller:  
    def __init__(self, min_size, interpolation=Image.BILINEAR):  
        self.min_size = min_size  
        self.interpolation = interpolation  
  
    def __call__(self, img):  
        if isinstance(img, np.ndarray):  
            img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))  
        assert isinstance(img, Image.Image), "Image should be a PIL Image"  
  
        width, height = img.size  
        if height < self.min_size or width < self.min_size:  
            ratio = max(self.min_size / height, self.min_size / width)  
            new_height = int(height * ratio)  
            new_width = int(width * ratio)  
            img = img.resize((new_width, new_height), self.interpolation)  
        return img

You can use this class to preprocess your images before feeding them into HerdNet. This should prevent the error you were experiencing. To incorporate this fix into your workflow, please pull the latest changes from the repository. I am going to close this issue now, but please feel free to reopen it if the problem persists or if you have any more questions.

Best regards,
@idchacon28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants