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

scale_masks fucntion #13015

Closed
1 task done
polinamalova0 opened this issue May 15, 2024 · 2 comments
Closed
1 task done

scale_masks fucntion #13015

polinamalova0 opened this issue May 15, 2024 · 2 comments
Labels
question Further information is requested Stale

Comments

@polinamalova0
Copy link

Search before asking

Question

Dear Ultralytics!
I faced the problem when i wsa trying to resize the masks I got after the inference. In the documentation you have the function that does it:

def scale_masks(masks, shape, padding=True):
    """
    Rescale segment masks to shape.

    Args:
        masks (torch.Tensor): (N, C, H, W).
        shape (tuple): Height and width.
        padding (bool): If True, assuming the boxes is based on image augmented by yolo style. If False then do regular
            rescaling.
    """
    # print('masks.shape[0:2]: ',masks.shape[0:2])
    mh, mw = masks.shape[2:]
    gain = min(mh / shape[0], mw / shape[1])  # gain  = old / new
    pad = [mw - shape[1] * gain, mh - shape[0] * gain]  # wh padding
    if padding:
        pad[0] /= 2
        pad[1] /= 2
    top, left = (int(pad[1]), int(pad[0])) if padding else (0, 0)  # y, x
    bottom, right = (int(mh - pad[1]), int(mw - pad[0]))
    masks = masks[..., top:bottom, left:right]

    masks = F.interpolate(masks, shape, mode="bilinear", align_corners=False)  # NCHW
    return masks

after submitting masks = scale_masks(results[0].masks, (w, h)) command, i received this error

Traceback (most recent call last):
  File "proj.py", line 169, in <module>
    masks = scale_masks(results[0].masks, (w, h))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "proj.py", line 60, in scale_masks
    mh, mw = masks.shape[2:]
    ^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)

and i do not understand what is wrong since there's not much i could have possibly disrupt in the process (the results variable is calculated as results = model(img))

I would be really grateful for the help!

Additional

No response

@polinamalova0 polinamalova0 added the question Further information is requested label May 15, 2024
@glenn-jocher
Copy link
Member

Hello! It looks like the error you're encountering is likely due to masks.shape not having the expected dimensions. The scale_masks function expects a four-dimensional tensor (N, C, H, W), but the masks tensor you are passing might be missing some dimensions.

Please ensure that the results[0].masks tensor has the correct shape. You can debug this by adding a print statement just before you call scale_masks to check the shape of your masks tensor:

print(results[0].masks.shape)
masks = scale_masks(results[0].masks, (w, h))

This will let you see if the dimensions are indeed (N, C, H, W). If the dimensions are different, you'll need to adjust them appropriately before passing them to scale_masks.

Hope this helps! 👍

Copy link
Contributor

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

@github-actions github-actions bot added the Stale label Jun 15, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

2 participants