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 change the num_instances when i detect the image? #5324

Open
apan666555 opened this issue Jul 10, 2024 · 2 comments
Open

how to change the num_instances when i detect the image? #5324

apan666555 opened this issue Jul 10, 2024 · 2 comments
Labels
documentation Problems about existing documentation or comments

Comments

@apan666555
Copy link

📚 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

@apan666555 apan666555 added the documentation Problems about existing documentation or comments label Jul 10, 2024
@Programmer-RD-AI
Copy link
Contributor

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.

from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2 import model_zoo

# Load the model configuration and set the desired threshold
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7  # Set your desired threshold here
cfg.MODEL.WEIGHTS = "path/to/your/model_weights.pth"  # Update with your model weights

predictor = DefaultPredictor(cfg)

# Run the prediction
image = cv2.imread("path/to/your/image.jpg")  # Replace with the path to your image
outputs = predictor(image)

# Post-processing to filter the instances if needed
instances = outputs["instances"]
filtered_instances = instances[instances.scores > 0.7]  # Filtering based on score threshold

print(f"Number of detected instances: {len(filtered_instances)}")

@micedevai
Copy link

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 Instances(num_instances=100, image_height=1080, ...). To modify the number of instances (num_instances), you would need to control the model's behavior related to object detection, including adjusting the thresholds or the classes.

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 Threshold

By 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 SCORE_THRESH parameter.

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 Instances

You 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 Class

If 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Problems about existing documentation or comments
Projects
None yet
Development

No branches or pull requests

3 participants