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 · 1 comment
Open

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

apan666555 opened this issue Jul 10, 2024 · 1 comment
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)}")

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

2 participants