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

Add timm data loader for sv.Classifications #572

Merged
merged 4 commits into from
Nov 27, 2023

Conversation

capjamesg
Copy link
Collaborator

Description

This PR adds a data loader for classifications from the timm Python package.

Type of change

  • New feature (non-breaking change which adds functionality)

How has this change been tested, please provide a testcase or example of how you tested the change?

You can test the API using the following code:

import timm
import torch
from PIL import Image
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
import supervision as sv

# Load from Hub 🔥
model = timm.create_model(
    'hf-hub:nateraw/resnet50-oxford-iiit-pet',
    pretrained=True
)

# Set model to eval mode for inference
model.eval()

# Create Transform
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))

# Get the labels from the model config
labels = model.pretrained_cfg['label_names']
top_k = min(len(labels), 5)

# Use your own image file here...
image = Image.open('./annotated_image.jpg').convert('RGB')

# Process PIL image with transforms and add a batch dimension
x = transform(image).unsqueeze(0)

# Pass inputs to model forward function to get outputs
out = model(x)

results = sv.Classifications.from_timm(out)

print(results)

Any specific deployment considerations

N/A

Docs

This PR does not yet contain a docs update, but I will add documentation for the method as part of this PR.

@capjamesg capjamesg self-assigned this Nov 3, 2023
@SkalskiP
Copy link
Collaborator

@capjamesg, is that PR ready for review? I see it is still marked as Draft.

btw, just want to remind you about: Please share a Google Colab with minimal code to test new feature or reproduce PR whenever it is possible. Please ensure that Google Colab can be accessed without any issue.

@capjamesg capjamesg marked this pull request as ready for review November 27, 2023 10:51
@SkalskiP
Copy link
Collaborator

@capjamesg pre-commit is failing can you fix this?

@capjamesg
Copy link
Collaborator Author

The variable names and usage within the 'ultralytics' and 'timm' functions were updated for improved code readability. The docstrings were also refined to better explain the function parameters. Instances where YOLO was redundantly called were also eliminated.
Adjustments have been made to the import statements in the 'core.py' file of the 'classification' module.
@SkalskiP
Copy link
Collaborator

Awesome! Thanks a lot, @capjamesg! 🙏🏻 Merging!

@SkalskiP SkalskiP merged commit 1589d8f into develop Nov 27, 2023
6 checks passed
@SkalskiP SkalskiP deleted the add-timm-classification-loader branch January 2, 2024 18:51
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants