-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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 change the num_instances when i detect the image? #5324
Comments
To change the number of instances detected in the image, modify the detection threshold or apply post-processing to filter out unwanted instances based on your criteria.
|
In your case, you're working with Detectron2 and performing object detection on images or videos, where the number of instances detected is printed as However, if you're looking to control or limit the number of detected instances based on certain criteria, such as a confidence threshold, here are a few strategies you can consider: 1. Modify Confidence ThresholdBy default, Detectron2 might detect a lot of instances, but you can filter detections based on the confidence score. For example, if you want to only keep instances that have a high confidence score, you can adjust the Here's how you can adjust the confidence threshold: cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")
cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Adjust the confidence threshold here
cfg.TEST.DETECTIONS_PER_IMAGE = 100 # Control how many instances per image to output (optional)
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # Set a score threshold for detections
predictor = DefaultPredictor(cfg)
# Perform prediction as usual
outputs = predictor(frame) By increasing the threshold, you can reduce the number of detections, as only instances with a confidence above the threshold will be returned. 2. Post-Processing to Limit InstancesYou can also manually limit the number of instances that are considered after the predictions are made. For instance, you can take only the top N instances (with the highest scores). Here's how you could modify the detection results to keep only a specified number of instances: outputs = predictor(frame)
instances = outputs["instances"]
# Let's say you only want the top 50 instances based on the highest scores
num_instances = 50
scores = instances.scores.cpu().numpy()
indices = np.argsort(scores)[::-1][:num_instances] # Sort scores and get the indices of the top N
# Keep only the top N instances
instances = instances[indices]
# Use the filtered instances for visualization
boxes = instances.pred_boxes if instances.has("pred_boxes") else None 3. Limiting Instances Based on ClassIf you have a custom dataset with only 2 classes, you can modify the class indices to make sure you're only detecting objects from your desired classes. You can filter based on class labels, like this: # Assume you want to filter only instances belonging to class 0 or class 1
classes_to_keep = [0, 1]
# Filter instances based on class
filtered_instances = instances[torch.isin(instances.pred_classes, torch.tensor(classes_to_keep))] 4. Modify the Model's Config (for Custom Models)If you're training a custom model, you can also change the number of classes the model detects. To do this, you would modify the number of output classes in the model configuration during training. For instance: cfg.MODEL.ROI_HEADS.NUM_CLASSES = 2 # Set the number of classes your model detects This change affects the training phase, and it ensures that only your specified classes are used during detection. |
📚 Documentation Issue
I predict the image show:detected 100 instances in 0.69s,and i print the predictor:Instances(num_instances=100, image_height=1080,
my weight only have 2 class, i want to change the num_instances ,please tell me how to change
The text was updated successfully, but these errors were encountered: