roboflow / notebooks

Examples and tutorials on using SOTA computer vision models and techniques. Learn everything from old-school ResNet, through YOLO and object-detection transformers like DETR, to the latest models like Grounding DINO and SAM.
https://roboflow.com/models
4.77k stars 744 forks source link

Vision Transformer #149

Open SalmanFarsiM opened 1 year ago

SalmanFarsiM commented 1 year ago

Search before asking

Notebook name

GOOGLE COLLAB

Bug

I am getting the 'Key Error'='annotations' And Type of Error: Detections._Getitem not supported for index of type <class 'numpy.ndarray'>.... Please Do guide me with the following error how can i come up with it. Will really appreciate it.

Environment

Google Colab

Minimal Reproducible Example

import random import cv2 import numpy as np import pandas as pd

utils

categories = TEST_DATASET.coco.cats id2label = {k: v['name'] for k,v in categories.items()} box_annotator = sv.BoxAnnotator()

select random image

image_ids = TEST_DATASET.coco.getImgIds() image_id = random.choice(image_ids) print('Image #{}'.format(image_id))

load image and annotatons

image = TEST_DATASET.coco.loadImgs(image_id)[0] annotations = TEST_DATASET.coco.imgToAnns[image_id] image_path = os.path.join(TEST_DATASET.root, image['file_name']) image = cv2.imread(image_path)

annotate

detections = sv.Detections.from_coco_annotations(coco_annotation=annotations) labels = [f"{id2label[classid]}" for , _, classid, in detections] frame = box_annotator.annotate(scene=image.copy(), detections=detections, labels=labels)

print('ground truth') %matplotlib inline
sv.show_frame_in_notebook(frame, (16, 16))

inference

with torch.no_grad():

# load image and predict
inputs = image_processor(images=image, return_tensors='pt').to(DEVICE)
outputs = model(**inputs)

# post-process
target_sizes = torch.tensor([image.shape[:2]]).to(DEVICE)
results = image_processor.post_process_object_detection(
    outputs=outputs, 
    threshold=CONFIDENCE_TRESHOLD, 
    target_sizes=target_sizes
)[0]

annotate

detections_obj = sv.Detections.from_transformers(transformers_results=results).with_nms(threshold=0.5) detections = detections_obj.get_data() labels = [f"{id2label[classid]} {confidence:.2f}" for , confidence, classid, in detections] frame = box_annotator.annotate(scene=image.copy(), detections=detections_obj, labels=labels)

print(type(detections)) %matplotlib inline

sv.show_frame_in_notebook(frame, (16, 16))

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 1 year ago

👋 Hello @SalmanFarsiM, thank you for leaving an issue on Roboflow Notebooks.

🐞 Bug reports

If you are filing a bug report, please be as detailed as possible. This will help us more easily diagnose and resolve the problem you are facing. To learn more about contributing, check out our Contributing Guidelines.

If you require support with custom code that is not part of Roboflow Notebooks, please reach out on the Roboflow Forum or on the GitHub Discussions page associated with this repository.

💬 Get in touch

Do you have more questions about Roboflow that we haven't responded to yet? Feel free to ask them on the Roboflow Discuss forum. Our developer advocates and community team actively respond to questions there.

To ask questions about Notebooks, head over to the GitHub Discussions section of this repository.

hieuthanhpro commented 10 months ago

i have bug that "AttributeError: type object 'Detections' has no attribute 'from_coco_annotations'" thank you

LuizAkazawa commented 10 months ago

i have bug that "AttributeError: type object 'Detections' has no attribute 'from_coco_annotations'" thank you

I'm having the same error on Jupyter Notebooks