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

Added RTDETR model to inference #558

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inference/models/rtdetr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from inference.models.rtdetr.rtdetr import RTDETR
68 changes: 68 additions & 0 deletions inference/models/rtdetr/rtdetr.py
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"
Copy link
Collaborator

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


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

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.cache_dir,
torch_dtype=dtype,
device_map=DEVICE,
revision="bfloat16",
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(
Copy link
Collaborator

Choose a reason for hiding this comment

The 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())