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

How to generate instance mask, only one channel? #27

Closed
rockywind opened this issue Mar 7, 2023 · 6 comments
Closed

How to generate instance mask, only one channel? #27

rockywind opened this issue Mar 7, 2023 · 6 comments
Labels
question Further information is requested

Comments

@rockywind
Copy link

No description provided.

@praeclarumjj3
Copy link
Member

Hi @rockywind, thanks for your interest in our work.

If I understand your question correctly, you want to generate a single-channel segmentation mask for the instance segmentation predictions. Please provide more description about your issue if this is not the case.

You can loop through the pred_masks stored in the instance predictions, assign an ID to each mask, and aggregate those into a single channel mask.

result.pred_masks = (mask_pred > 0).float()

@praeclarumjj3 praeclarumjj3 added the question Further information is requested label Mar 7, 2023
@rockywind
Copy link
Author

Hi,
thank you for your help.
Each pixel value represents an instance category, the value is 1,2,3, and so on. The 0 is the representation's background.
But, I found that the value of result.pred_masks is between 0 and 1, the shape of result.pred_masks is [7, 1114, 2191], the image's size is [1114, 2191] .

@praeclarumjj3
Copy link
Member

I believe you are talking about the semantic segmentation result, where each pixel corresponds to the corresponding object's category.

You need to do an argmax operation on the semantic predictions to obtain those.

predictions["sem_seg"].argmax(dim=0).to(self.cpu_device), alpha=0.7

@rockywind
Copy link
Author

Hi,
Sorry for not being clear before. The following is sample data。
There are 3 cars in the picture, the first car's pixel value is 1, the second car's pixel value is 2, and the third car's pixel is 3.
0151

@praeclarumjj3
Copy link
Member

Each pixel value represents an instance category, the value is 1,2,3, and so on. The 0 is the representation's background.
But, I found that the value of result.pred_masks is between 0 and 1, the shape of result.pred_masks is [7, 1114, 2191], the image's size is [1114, 2191] .

Right, that's what I thought you wanted to do. You can loop through the result.pred_masks, assign an ID (starting from 1) to each mask, and aggregate them on an all-zeros mask. Please find the pseudo-code below:

# create an all-zeros mask
single_channel_mask = torch.zeros_like(image) # or torch.zeros((1114, 2191))
count = 0

# loop through all instance masks
for mask in result.pred_masks:
    count += 1
    mask *= count
    single_channel_mask = torch.max(single_channel_mask, mask)

Let me know if you have any more issues.

@rockywind
Copy link
Author

Thank you very much.
I have a try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants