-
Notifications
You must be signed in to change notification settings - Fork 130
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
Added RTDETR model to inference #558
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from inference.models.rtdetr.rtdetr import RTDETR |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
|
||
import torch | ||
import requests | ||
|
||
from typing import Any, Tuple | ||
|
||
from PIL import Image | ||
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor | ||
|
||
from inference.core.models.base import PreprocessReturnMetadata | ||
from inference.models.transformers.transformers import TransformerModel | ||
from inference.core.utils.image_utils import load_image_rgb | ||
|
||
DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu" | ||
|
||
class RTDETR(TransformerModel): | ||
def __init__(self, *args, model_id=f"", **kwargs): | ||
super().__init__(*args, model_id=model_id, **kwargs) | ||
self.model_id = model_id | ||
self.endpoint = model_id | ||
self.api_key = API_KEY | ||
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these 3 lines are needed, this is set on RoboflowInferenceModel |
||
self.dataset_id, self.version_id = model_id.split("/") | ||
self.cache_dir = os.path.join(MODEL_CACHE_DIR, self.endpoint + "/") # "PekingU/rtdetr_r50vd" | ||
dtype = torch.bfloat16 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bfloat16 shouldn't be hardcoded here, bfloat16 is only supported on gpus with compute capability >= 8.0 |
||
self.model = RTDetrForObjectDetection.from_pretrained( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RTDetrForObjectDetection should be a class property, see https://github.com/Bhavay-2001/roboflow-inference/blob/d3c88f74fdcaac5c29822a7444698b11b78067c8/inference/models/paligemma/paligemma.py#L10 |
||
self.cache_dir, | ||
torch_dtype=dtype, | ||
device_map=DEVICE, | ||
revision="bfloat16", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can upload float16 weights to a Roboflow project and load from there |
||
).eval() | ||
|
||
self.processor = RTDetrImageProcessor.from_pretrained( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment for class property |
||
self.cache_dir, | ||
) | ||
# self.task_type = "lmm" | ||
|
||
def preprocess( | ||
self, image: Any, **kwargs | ||
) -> Tuple[Image.Image, PreprocessReturnMetadata]: | ||
pil_image = Image.fromarray(load_image_rgb(image)) | ||
|
||
return pil_image, PreprocessReturnMetadata({}) | ||
|
||
def postprocess( | ||
self, | ||
predictions: Tuple[str], | ||
preprocess_return_metadata: PreprocessReturnMetadata, | ||
**kwargs, | ||
) -> Any: | ||
return predictions[0] | ||
|
||
def predict(self, image_in: Image.Image, **kwargs): | ||
model_inputs = self.processor( | ||
images=image_in, return_tensors="pt" | ||
).to(self.model.device) | ||
|
||
with torch.inference_mode(): | ||
outputs = self.model(**model_inputs) | ||
|
||
results = self.image_processor.post_process_object_detection(outputs, target_sizes=torch.tensor([image_in.size[::-1]]), threshold=0.3) | ||
|
||
return results | ||
|
||
if __name__ == "__main__": | ||
m = RTDETR() | ||
print(m.infer()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed, self.device should be set on the TransformerModel