How to display subset of classes during inference?
See original GitHub issue❓ Questions and Help
I want to output masks of a particular class in instance segmentation say person (id: 0 according to COCO). In order to do so (on the same image in colab notebook), I first find the indexes of non-zero classes then remove the corresponding tensors from pred_classses, scores, etc. as follows:
cls = outputs['instances'].pred_classes
scores = outputs["instances"].scores
masks = outputs['instances'].pred_masks
print(cls)
# tensor([17, 0, 0, 0, 0, 0, 0, 0, 25, 0, 25, 25, 0, 0, 24], device='cuda:0')
# non-zero elements (non-person class categories)
indx_to_remove = cls.nonzero().flatten().tolist()
print(indx_to_remove)
# [0, 8, 10, 11, 14]
# delete corresponding arrays
cls = np.delete(cls.cpu().numpy(), indx_to_remove)
scores = np.delete(scores.cpu().numpy(), indx_to_remove)
masks = np.delete(masks.cpu().numpy(), indx_to_remove, axis=0)
# convert back to tensor and move to cuda
cls = torch.tensor(cls).to('cuda:0')
scores = torch.tensor(scores).to('cuda:0')
masks = torch.tensor(masks).to('cuda:0')
print(cls) # similar to as it was before except unwanted classes removed
# tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], device='cuda:0')
# not interested in boxes
outputs['instances'].remove('pred_boxes')
outputs['instances'].pred_classes = cls
outputs["instances"].scores = scores
outputs['instances'].pred_masks = masks
It gives me error:
AssertionError Traceback (most recent call last)
<ipython-input-47-695b068ba737> in <module>()
15 masks = torch.tensor(masks).to('cuda:0')
16
---> 17 outputs['instances'].pred_classes = cls
18 outputs["instances"].pred_boxes = None
19 outputs["instances"].scores = scores
1 frames
/content/detectron2_repo/detectron2/structures/instances.py in set(self, name, value)
69 assert (
70 len(self) == data_len
---> 71 ), "Adding a field of length {} to a Instances of length {}".format(data_len, len(self))
72 self._fields[name] = value
73
AssertionError: Adding a field of length 10 to a Instances of length 15
Is it because the instances
structure has a fixed label 15 in it {'instances': Instances(num_instances=15, image_height=480, image_width=640, fields=[...
?
How do I solve this problem, and is there any better method to do so?
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Subsetting model selection table in MuMIn - Rdrr.io
In MuMIn: Multi-Model Inference ... S3 method for class 'model.selection' subset(x, subset, select, recalc.weights = TRUE, recalc.delta ... See subset .
Read more >How do I pull a subset of an ARVIZ array within the inference ...
That said, I only want to pull 10 of those groups to show the subset. Currently, running the following code yields the following...
Read more >Should I train different models for detecting subsets of objects?
During training, the model runs through a sequence of binary classifiers, training each to answer a separate classification question.
Read more >CAP'NN: Class-Aware Personalized Neural Network Inference
In this work, we propose CAP'NN as a framework that enables personalized inference by pruning the network for a specific subset of classes...
Read more >TF object detection: return subset of inference payload
I was able to find a hacky workaround. In the export process (here), some of the components of the prediction dict are deleted....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, solved it by creating new
Instances
object as follows:I get the following error. It seems that the visualization needs the bboxes.