Skip to main content

A package for bark and ambrosia beetle identification.

Project description

Intelligent Bark Beetle Identifier (IBBI)

PyPI version License: MIT Documentation

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.

IBBI Data Flow

  1. 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.
  2. 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.
  3. 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.

IBBI Inputs and Outputs

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, and explain_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)
Beetles Object Detection Object Classification Zero-Shot Classification

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ibbi-0.1.2.tar.gz (149.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ibbi-0.1.2-py3-none-any.whl (153.8 kB view details)

Uploaded Python 3

File details

Details for the file ibbi-0.1.2.tar.gz.

File metadata

  • Download URL: ibbi-0.1.2.tar.gz
  • Upload date:
  • Size: 149.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ibbi-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ac95c5187c2f0e98a76eb8b2510410a75f6cac623507d9ba1749e95e83005895
MD5 987ecd1f3e0cee6cfd323ba6630ff7c2
BLAKE2b-256 205dd03b6223335ee69c14581c7c6e385886c79cf8f920d3fbf0b9658a478222

See more details on using hashes here.

Provenance

The following attestation bundles were made for ibbi-0.1.2.tar.gz:

Publisher: publish-to-pypi.yml on ChristopherMarais/IBBI

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ibbi-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ibbi-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 153.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for ibbi-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dda936363e5b08b0253bb7b8c8d1ffb18db7a8f491929adea3bbb255751bd5c3
MD5 950b44a024bc7270e983898e1fa5246a
BLAKE2b-256 8d8975fb30212a7190cd414139ab3c618d39fa2d56830e91440ad7684bd9d41c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ibbi-0.1.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on ChristopherMarais/IBBI

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page