-
Notifications
You must be signed in to change notification settings - Fork 584
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
[Docs] Tutorial on Small Object Detection with SAHI #4263
Conversation
WalkthroughThe recent update brings a new tutorial card for "Small Object Detection with SAHI" to the tutorials index page. This addition includes a detailed description, a direct link, an illustrative image, and relevant tags. Furthermore, a link to the "Detecting small objects" tutorial has been included in the list. Changes
Recent Review DetailsConfiguration used: .coderabbit.yaml Files selected for processing (2)
Files not summarized due to errors (1)
Files skipped from review as they are similar to previous changes (1)
Additional comments not posted (7)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
Actionable comments posted: 7
"pip install -U fiftyone sahi ultralytics huggingface_hub --quiet" | ||
] |
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.
Consider pinning the versions of the libraries to ensure reproducibility.
- pip install -U fiftyone sahi ultralytics huggingface_hub --quiet
+ pip install fiftyone==0.24.0 sahi==0.8.5 ultralytics==1.0.0 huggingface_hub==0.2.0 --quiet
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
"pip install -U fiftyone sahi ultralytics huggingface_hub --quiet" | |
] | |
"pip install fiftyone==0.24.0 sahi==0.8.5 ultralytics==1.0.0 huggingface_hub==0.2.0 --quiet" | |
] |
} | ||
], | ||
"source": [ | ||
"dataset = fouh.load_from_hub(\"jamarks/VisDrone2019-DET\", name=\"sahi-test\", max_samples=100, overwrite=True)" |
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.
Ensure that the dataset name and the maximum number of samples are configurable to enhance flexibility.
- dataset = fouh.load_from_hub("jamarks/VisDrone2019-DET", name="sahi-test", max_samples=100, overwrite=True)
+ dataset_name = "jamarks/VisDrone2019-DET"
+ max_samples = 100 # Configurable number of samples
+ dataset = fouh.load_from_hub(dataset_name, name="sahi-test", max_samples=max_samples, overwrite=True)
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
"dataset = fouh.load_from_hub(\"jamarks/VisDrone2019-DET\", name=\"sahi-test\", max_samples=100, overwrite=True)" | |
dataset_name = "jamarks/VisDrone2019-DET" | |
max_samples = 100 # Configurable number of samples | |
dataset = fouh.load_from_hub(dataset_name, name="sahi-test", max_samples=max_samples, overwrite=True) |
} | ||
], | ||
"source": [ | ||
"session = fo.launch_app(dataset)" |
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.
Consider handling potential exceptions when launching the FiftyOne app.
- session = fo.launch_app(dataset)
+ try:
+ session = fo.launch_app(dataset)
+ except Exception as e:
+ print(f"Failed to launch FiftyOne app: {e}")
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
"session = fo.launch_app(dataset)" | |
try: | |
session = fo.launch_app(dataset) | |
except Exception as e: | |
print(f"Failed to launch FiftyOne app: {e}") |
"source": [ | ||
"from ultralytics import YOLO\n", | ||
"\n", | ||
"ckpt_path = \"yolov8l.pt\"\n", | ||
"model = YOLO(ckpt_path)\n", | ||
"## fiftyone will work directly with the Ultralytics.YOLO model\n", | ||
"\n", | ||
"dataset.apply_model(model, label_field=\"base_model\")" |
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.
Ensure proper error handling when applying the model to the dataset.
- dataset.apply_model(model, label_field="base_model")
+ try:
+ dataset.apply_model(model, label_field="base_model")
+ except Exception as e:
+ print(f"Error applying model: {e}")
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
"source": [ | |
"from ultralytics import YOLO\n", | |
"\n", | |
"ckpt_path = \"yolov8l.pt\"\n", | |
"model = YOLO(ckpt_path)\n", | |
"## fiftyone will work directly with the Ultralytics.YOLO model\n", | |
"\n", | |
"dataset.apply_model(model, label_field=\"base_model\")" | |
"source": [ | |
"from ultralytics import YOLO\n", | |
"\n", | |
"ckpt_path = \"yolov8l.pt\"\n", | |
"model = YOLO(ckpt_path)\n", | |
"## fiftyone will work directly with the Ultralytics.YOLO model\n", | |
"\n", | |
"try:\n", | |
" dataset.apply_model(model, label_field=\"base_model\")\n", | |
"except Exception as e:\n", | |
" print(f\"Error applying model: {e}\")" |
"def get_label_fields(sample_collection):\n", | ||
" \"\"\"Get the (detection) label fields of a Dataset or DatasetView.\"\"\"\n", | ||
" label_fields = list(\n", | ||
" sample_collection.get_field_schema(embedded_doc_type=fo.Detections).keys()\n", | ||
" )\n", | ||
" return label_fields\n", | ||
"\n", | ||
"def filter_all_labels(sample_collection):\n", | ||
" label_fields = get_label_fields(sample_collection)\n", | ||
"\n", | ||
" filtered_view = sample_collection\n", | ||
"\n", | ||
" for lf in label_fields:\n", | ||
" filtered_view = filtered_view.filter_labels(\n", | ||
" lf, F(\"label\").is_in([\"person\", \"car\", \"truck\"]), only_matches=False\n", | ||
" )\n", | ||
" return filtered_view" |
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.
Refactor the filtering function to improve readability and maintainability.
- def filter_all_labels(sample_collection):
- label_fields = get_label_fields(sample_collection)
- filtered_view = sample_collection
- for lf in label_fields:
- filtered_view = filtered_view.filter_labels(
- lf, F("label").is_in(["person", "car", "truck"]), only_matches=False
- )
- return filtered_view
+ def filter_labels_by_classes(sample_collection, classes):
+ """Filters labels in the sample collection to include only specified classes."""
+ label_fields = get_label_fields(sample_collection)
+ for lf in label_fields:
+ sample_collection = sample_collection.filter_labels(
+ lf, F("label").is_in(classes), only_matches=False
+ )
+ return sample_collection
+ # Usage
+ filtered_view = filter_labels_by_classes(dataset, ["person", "car", "truck"])
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
"def get_label_fields(sample_collection):\n", | |
" \"\"\"Get the (detection) label fields of a Dataset or DatasetView.\"\"\"\n", | |
" label_fields = list(\n", | |
" sample_collection.get_field_schema(embedded_doc_type=fo.Detections).keys()\n", | |
" )\n", | |
" return label_fields\n", | |
"\n", | |
"def filter_all_labels(sample_collection):\n", | |
" label_fields = get_label_fields(sample_collection)\n", | |
"\n", | |
" filtered_view = sample_collection\n", | |
"\n", | |
" for lf in label_fields:\n", | |
" filtered_view = filtered_view.filter_labels(\n", | |
" lf, F(\"label\").is_in([\"person\", \"car\", \"truck\"]), only_matches=False\n", | |
" )\n", | |
" return filtered_view" | |
"def get_label_fields(sample_collection):\n", | |
" \"\"\"Get the (detection) label fields of a Dataset or DatasetView.\"\"\"\n", | |
" label_fields = list(\n", | |
" sample_collection.get_field_schema(embedded_doc_type=fo.Detections).keys()\n", | |
" )\n", | |
" return label_fields\n", | |
"\n", | |
"def filter_labels_by_classes(sample_collection, classes):\n", | |
" \"\"\"Filters labels in the sample collection to include only specified classes.\"\"\"\n", | |
" label_fields = get_label_fields(sample_collection)\n", | |
" for lf in label_fields:\n", | |
" sample_collection = sample_collection.filter_labels(\n", | |
" lf, F(\"label\").is_in(classes), only_matches=False\n", | |
" )\n", | |
" return sample_collection\n", | |
"\n", | |
"# Usage\n", | |
"filtered_view = filter_labels_by_classes(dataset, [\"person\", \"car\", \"truck\"])" |
"detection_model = AutoDetectionModel.from_pretrained(\n", | ||
" model_type='yolov8',\n", | ||
" model_path=ckpt_path,\n", | ||
" confidence_threshold=0.25, ## same as the default value for our base model\n", | ||
" image_size=640,\n", | ||
" device=\"cpu\", # or 'cuda'\n", | ||
")" |
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.
Consider adding error handling for model loading and setting a default device based on availability.
- detection_model = AutoDetectionModel.from_pretrained(
- model_type='yolov8',
- model_path=ckpt_path,
- confidence_threshold=0.25, ## same as the default value for our base model
- image_size=640,
- device="cpu", # or 'cuda'
- )
+ try:
+ device = "cuda" if torch.cuda.is_available() else "cpu"
+ detection_model = AutoDetectionModel.from_pretrained(
+ model_type='yolov8',
+ model_path=ckpt_path,
+ confidence_threshold=0.25,
+ image_size=640,
+ device=device
+ )
+ except Exception as e:
+ print(f"Failed to load model: {e}")
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
"detection_model = AutoDetectionModel.from_pretrained(\n", | |
" model_type='yolov8',\n", | |
" model_path=ckpt_path,\n", | |
" confidence_threshold=0.25, ## same as the default value for our base model\n", | |
" image_size=640,\n", | |
" device=\"cpu\", # or 'cuda'\n", | |
")" | |
try: | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
detection_model = AutoDetectionModel.from_pretrained( | |
model_type='yolov8', | |
model_path=ckpt_path, | |
confidence_threshold=0.25, | |
image_size=640, | |
device=device | |
) | |
except Exception as e: | |
print(f"Failed to load model: {e}") |
"def predict_with_slicing(sample, label_field, **kwargs):\n", | ||
" result = get_sliced_prediction(\n", | ||
" sample.filepath, detection_model, verbose=0, **kwargs\n", | ||
" )\n", | ||
" sample[label_field] = fo.Detections(detections=result.to_fiftyone_detections())" |
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.
Add error handling for the predict_with_slicing
function to manage exceptions during prediction.
- def predict_with_slicing(sample, label_field, **kwargs):
- result = get_sliced_prediction(
- sample.filepath, detection_model, verbose=0, **kwargs
- )
- sample[label_field] = fo.Detections(detections=result.to_fiftyone_detections())
+ def predict_with_slicing(sample, label_field, **kwargs):
+ try:
+ result = get_sliced_prediction(
+ sample.filepath, detection_model, verbose=0, **kwargs
+ )
+ sample[label_field] = fo.Detections(detections=result.to_fiftyone_detections())
+ except Exception as e:
+ print(f"Error during slicing prediction: {e}")
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
"def predict_with_slicing(sample, label_field, **kwargs):\n", | |
" result = get_sliced_prediction(\n", | |
" sample.filepath, detection_model, verbose=0, **kwargs\n", | |
" )\n", | |
" sample[label_field] = fo.Detections(detections=result.to_fiftyone_detections())" | |
def predict_with_slicing(sample, label_field, **kwargs): | |
try: | |
result = get_sliced_prediction( | |
sample.filepath, detection_model, verbose=0, **kwargs | |
) | |
sample[label_field] = fo.Detections(detections=result.to_fiftyone_detections()) | |
except Exception as e: | |
print(f"Error during slicing prediction: {e}") |
Hey @jacobmarks, thanks for the great tutorial on our work SAHI! It would be better if you could add a link to the original SAHI paper published in ICIP2022 in the first page of the blog. |
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.
LGTM and will include in 0.23.8 docs release 🤗
Slicing Aided Hyper Inference (SAHI) works in conjunction with object detection models to improve detection quality for small objects/large images.
This tutorial covers generating predictions with SAHI, and evaluating these predictions.
SAHI already integrated with FiftyOne, which made this very straightforward. The tutorial also leverages our Hugging Face Hub and Ultralytics integrations.
Static PDF Here:
Detecting Small Objects with SAHI — FiftyOne 0.24.0 documentation.pdf
What changes are proposed in this pull request?
(Please fill in changes proposed in this fix)
How is this patch tested? If it is not, please explain why.
(Details)
Release Notes
Is this a user-facing change that should be mentioned in the release notes?
notes for FiftyOne users.
(Details in 1-2 sentences. You can just refer to another PR with a description
if this PR is part of a larger change.)
What areas of FiftyOne does this PR affect?
fiftyone
Python library changesSummary by CodeRabbit