Skip to content

Add timm data loader for sv.Classifications - #572

Merged
SkalskiP merged 4 commits into
developfrom
add-timm-classification-loader
Nov 27, 2023
Merged

SkalskiP merged 4 commits into
developfrom
add-timm-classification-loader

Conversation

@capjamesg

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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 requested a review from SkalskiP November 27, 2023 10:51
@capjamesg
capjamesg marked this pull request as ready for review November 27, 2023 10:51
@SkalskiP

Copy link
Copy Markdown
Collaborator

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

@capjamesg

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

Awesome! Thanks a lot, @capjamesg! 馃檹馃徎 Merging!

@SkalskiP
SkalskiP merged commit 1589d8f into develop Nov 27, 2023
@SkalskiP
SkalskiP deleted the add-timm-classification-loader branch January 2, 2024 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants