Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Vision Transformer #149

Open
2 tasks done
SalmanFarsiM opened this issue Jun 17, 2023 · 3 comments
Open
2 tasks done

Vision Transformer #149

SalmanFarsiM opened this issue Jun 17, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@SalmanFarsiM
Copy link

Search before asking

  • I have searched the Roboflow Notebooks issues and found no similar bug report.

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[class_id]}" for _, _, class_id, _ 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[class_id]} {confidence:.2f}" for _, confidence, class_id, _ 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?

  • Yes I'd like to help by submitting a PR!
@SalmanFarsiM SalmanFarsiM added the bug Something isn't working label Jun 17, 2023
@github-actions
Copy link

👋 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
Copy link

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

@LuizAkazawa
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants