A package for bark and ambrosia beetle identification.
Project description
Intelligent Bark Beetle Identifier (IBBI)
IBBI is a Python package that provides a simple and unified interface for detecting and classifying bark and ambrosia beetles from images using state-of-the-art computer vision models.
This package is designed to support entomological research by automating the laborious task of beetle identification, enabling high-throughput data analysis for ecological studies, pest management, and biodiversity monitoring.
Motivation
The ability to accurately identify bark and ambrosia beetles is critical for forest health and pest management. However, traditional methods face significant challenges:
- They are slow and time-consuming.
- They require highly specialized expertise.
- They create a bottleneck for large-scale research.
The IBBI package provides a powerful, modern solution to overcome these obstacles by using pre-trained, open-source models to automate detection and classification from images, lowering the barrier to entry for researchers.
Key Features
- Simple API: Access powerful detection and classification models with a single function call:
ibbi.create_model(). - Multiple Model Types:
- Single-Class Detection: Detect the presence of any beetle in an image.
- Multi-Class Species Detection: Identify the species of a beetle from an image.
- Zero-Shot Detection: Detect objects using a text prompt (e.g., "insect"), without prior training on that specific class.
- Pre-trained Models: Leverages pre-trained models hosted on the Hugging Face Hub for immediate use.
- Model Explainability: Understand model predictions using SHAP (SHapley Additive exPlanations) to visualize which parts of an image contribute to the identification.
- Extensible: Designed to easily incorporate new model architectures in the future.
Table of Contents
Workflow: How the Models Were Built
The models in ibbi are the result of a comprehensive data collection, annotation, and training pipeline.
- Data Collection and Curation: The process begins with data collection from various sources. A zero-shot detection model performs initial beetle localization, followed by human-in-the-loop verification to ensure accurate bounding box annotations.
- Model-Specific Training Data: The annotated dataset is curated for different model types:
- Single-Class Detection: Models are trained on all images with verified beetle localizations.
- Multi-Class Species Detection: Models are trained only on images with both verified localizations and species-level labels. To ensure robustness, species with fewer than 50 images are excluded.
- Evaluation and Deployment: A held-out test set is used to evaluate all models. Performance metrics can be viewed with
ibbi.list_models(). Final models are deployed to the Hugging Face Hub for easy access.
Package API and Usage
The ibbi package is designed to be simple and intuitive.
The main components are:
- Inputs: Images (file paths, URLs, PIL/numpy objects) and a model name string.
model.predict(): The main prediction function. Returns bounding boxes, scores, and class labels.model.extract_features(): Extracts deep feature embeddings from images for downstream tasks.- Helper Functions:
get_dataset()to load the training/test data,list_models()to see available models, andexplain_with_shap()to visualize model predictions using SHAP.
Usage Examples
Here are visual examples of what you can do with ibbi.
| Input Image | Single-Class Detection ( yolov10x_bb_detect_model) |
Multi-Class Species Detection ( yolov10x_bb_multi_class_detect_model) |
Zero-Shot Detection ( grounding_dino_detect_model) |
|---|---|---|---|
Installation
This package requires PyTorch. For compatibility with your specific hardware (e.g., CUDA-enabled GPU), please install PyTorch before installing ibbi.
1. Install PyTorch
Follow the official instructions at pytorch.org to install the correct version for your system.
2. Install IBBI
Once PyTorch is installed, install the package from PyPI:
pip install ibbi
Quick Start
Using ibbi is straightforward. Load a pre-trained model and immediately use it for inference.
import ibbi
from PIL import Image
# Use a URL or local path
image_source = "IBBI/docs/assets/images/beetles.png"
# --- 1. Load All Model Types ---
species_detector = ibbi.create_model("yolov10x_bb_multi_class_detect_model", pretrained=True)
detector = ibbi.create_model("yolov10x_bb_detect_model", pretrained=True)
zs_detector = ibbi.create_model("grounding_dino_detect_model", pretrained=True)
# --- 2. Run Prediction for Each Model ---
species_results = species_detector.predict(image_source)
detection_results = detector.predict(image_source)
zs_results = zs_detector.predict(image_source, text_prompt="insect")
# --- 3. Print Results with Name and Confidence ---
# Example for Multi-Class Species Detection
print("--- Multi-Class Species Detection ---")
for box in species_results[0].boxes:
class_name = species_results[0].names[int(box.cls)]
confidence = float(box.conf)
print(f" - Detected '{class_name}' with confidence {confidence:.2f}")
# Example for Single-Class Detection
print("\n--- Single-Class Detection ---")
for box in detection_results[0].boxes:
class_name = detection_results[0].names[int(box.cls)]
confidence = float(box.conf)
print(f" - Detected '{class_name}' with confidence {confidence:.2f}")
# Example for Zero-Shot Detection
print("\n--- Zero-Shot Detection (prompt: 'insect') ---")
for box in zs_results[0].boxes:
class_name = zs_results[0].names[int(box.cls)]
confidence = float(box.conf)
print(f" - Detected '{class_name}' with confidence {confidence:.2f}")
# To visualize the bounding boxes, you can uncomment the following lines:
# species_results[0].show()
# detection_results[0].show()
# zs_results[0].show()
For more detailed, hands-on demonstrations, please see the example notebooks located in the notebooks/ folder of the repository.
Advanced Usage
Feature Extraction
All models can extract deep feature embeddings from an image. These vectors are useful for downstream tasks like clustering or similarity analysis.
# Assuming 'species_detector' is a loaded model
features = species_detector.extract_features(image_source)
print(f"Extracted feature vector shape: {features.shape}")
Model Explainability with SHAP
Understand why a model made a certain prediction using SHAP. This is crucial for building trust and interpreting the model's decisions by highlighting which pixels were most influential.
import ibbi
# Load a model
model = ibbi.create_model("yolov10x_bb_multi_class_detect_model", pretrained=True)
# Get images to explain and a background dataset
# This can be any dataset the model is applied to
explain_data = ibbi.get_dataset(split="train", streaming=True).take(5)
background_data = ibbi.get_dataset(split="train", streaming=True).skip(5).take(10)
# Generate explanations (this is computationally intensive)
shap_explanation = ibbi.explain_with_shap(
model=model,
explain_dataset=list(explain_data),
background_dataset=list(background_data),
num_explain_samples=1,
num_background_samples=5
)
# Plot the explanation for the first image
ibbi.plot_shap_explanation(shap_explanation[0], model)
Available Models
To see a list of available models and their performance metrics directly from Python, run:
import ibbi
# Returns a pandas DataFrame
models_df = ibbi.list_models(as_df=True)
print(models_df)
A detailed list can also be found in the src/ibbi/data/ibbi_model_summary.csv file.
How to Contribute
Contributions are welcome! If you would like to improve IBBI, please see the Contributing Guide.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ibbi-0.1.1b2.tar.gz.
File metadata
- Download URL: ibbi-0.1.1b2.tar.gz
- Upload date:
- Size: 148.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5567cc23f917d9c6b3148bec96fe07cf7a01e8cd1f3ca6585790e2fc86faa7ee
|
|
| MD5 |
4104ae5523925dadadd82bbc0c8ec905
|
|
| BLAKE2b-256 |
aa34df25074bb071e05b4d7b0a157bdc70807edb50c0e4fa82754679fb061b85
|
Provenance
The following attestation bundles were made for ibbi-0.1.1b2.tar.gz:
Publisher:
publish-to-pypi.yml on ChristopherMarais/IBBI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ibbi-0.1.1b2.tar.gz -
Subject digest:
5567cc23f917d9c6b3148bec96fe07cf7a01e8cd1f3ca6585790e2fc86faa7ee - Sigstore transparency entry: 420057742
- Sigstore integration time:
-
Permalink:
ChristopherMarais/IBBI@167ecc37b9a202d1d6c6081a997ba96065bfad53 -
Branch / Tag:
refs/tags/v0.1.1.b2 - Owner: https://github.com/ChristopherMarais
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@167ecc37b9a202d1d6c6081a997ba96065bfad53 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ibbi-0.1.1b2-py3-none-any.whl.
File metadata
- Download URL: ibbi-0.1.1b2-py3-none-any.whl
- Upload date:
- Size: 151.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4755f2f49e59e25b7d15382d0d9e88a70bcdee24e31d1d17bdaa157a98fc1bba
|
|
| MD5 |
dd672db5948e17fb217a821f6cdb35f8
|
|
| BLAKE2b-256 |
0cbf52b3971a17c17679732ea74bae3d5988f615ab3ba1e71286f413cc4f77f2
|
Provenance
The following attestation bundles were made for ibbi-0.1.1b2-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on ChristopherMarais/IBBI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ibbi-0.1.1b2-py3-none-any.whl -
Subject digest:
4755f2f49e59e25b7d15382d0d9e88a70bcdee24e31d1d17bdaa157a98fc1bba - Sigstore transparency entry: 420057766
- Sigstore integration time:
-
Permalink:
ChristopherMarais/IBBI@167ecc37b9a202d1d6c6081a997ba96065bfad53 -
Branch / Tag:
refs/tags/v0.1.1.b2 - Owner: https://github.com/ChristopherMarais
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@167ecc37b9a202d1d6c6081a997ba96065bfad53 -
Trigger Event:
release
-
Statement type: