Skip to main content

This package provides means of explainable classification of images with LLM-generated text descriptions, that are then analyzed using the Self Model Entities Related (SMER) explanation technique.

Project description

SMERVisual

SMERVisual is a Python package designed for explainable machine learning using the Self Model Entities Related (SMER) method. It provides tools for explainable classification of images with LLM-generated text descriptions, which are then analyzed using the SMER explanation technique.

The package supports both OpenAI API models and local language models, offering flexibility in model selection.


Installation

Install SMERVisual using pip:

pip install smer-visual

Features

Image Description

The image_description function generates text descriptions for images using either OpenAI models or local language models. Key features include:

  • Support for OpenAI models like gpt-4o-mini and local models.
  • Customizable prompts for generating concise and informative descriptions.

Description Embeddings

The get_description_embeddings function computes embeddings for image descriptions using OpenAI or local models.

Explainable Classification

The classify_with_logreg function performs logistic regression-based classification on image datasets while computing SMER values for explainability. Key features include:

  • Aggregation of embeddings for classification.
  • Computation of feature importance for each word in the description.
  • Support for AOPC (Area Over the Perturbation Curve) analysis.

Visualization

  • Important Words: The plot_important_words function visualizes the most important words across images.
  • AOPC Curves: The plot_aopc function compares the explainability of SMER and LIME methods by plotting AOPC curves.
  • Bounding Boxes: The BoundingBoxGenerator class overlays bounding boxes on images, highlighting critical words identified in classification.

Usage Example

Image Description and Embeddings

from smer_visual.smer import image_description, get_description_embeddings

# Generate image descriptions
descriptions = image_description(
    model="gpt-4o-mini",
    data_folder="path/to/images",
    api_key="your_openai_api_key"
)

# Generate embeddings for descriptions
embeddings_df = get_description_embeddings(
    descriptions=descriptions,
    embedding_model="text-embedding-ada-002",
    api_key="your_openai_api_key"
)

Explainable Classification

from smer_visual.smer import classify_with_logreg, aggregate_embeddings
from sklearn.linear_model import LogisticRegression
import numpy as np

# Prepare data
embeddings_df["aggregated_embedding"] = embeddings_df["embedding"].apply(aggregate_embeddings)
X_train = np.stack(embeddings_df["aggregated_embedding"].values)
y_train = embeddings_df["label"]

# Train a logistic regression model
logreg_model = LogisticRegression()
logreg_model.fit(X_train, y_train)

# Perform classification and compute feature importance
aopc_df, updated_dataset = classify_with_logreg(embeddings_df, X_train, logreg_model)

Visualization

from smer_visual.smer import plot_important_words, plot_aopc

# Plot important words
plot_important_words(updated_dataset)

# Plot AOPC curves
plot_aopc(aopc_df, logreg_model, max_k=5)

Bounding Box Generation

from smer_visual.smer import save_bounding_box_images
results = save_bounding_box_images(
    input_path="data/",
    output_folder="output",
    df_top_words=top_words_df,
    model_id="IDEA-Research/grounding-dino-base",
    box_threshold = 0.5,
    text_threshold = 0.4
)

Why Use SMERVisual?

  • Explainable AI – Provides insight into model decision-making.
  • Model-Agnostic – Compatible with OpenAI APIs and open-source models.
  • Zero-Shot Detection – No additional training data required.
  • Easy Integration – Simple API for seamless use with existing machine learning workflows.

Contributing

Contributions are welcome. If you’d like to contribute, follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit changes (git commit -m "Add new feature").
  4. Push to the branch (git push origin feature-branch).
  5. Open a pull request.

License

SMERVisual is released under the MIT License.


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

smer_visual-1.1.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

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

smer_visual-1.1.0-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file smer_visual-1.1.0.tar.gz.

File metadata

  • Download URL: smer_visual-1.1.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for smer_visual-1.1.0.tar.gz
Algorithm Hash digest
SHA256 259d0baae966e7b568e14b6aba6d14894ceedb02b9565e9d3af572d46d647c5f
MD5 b0c74436860b7f60e1271020db02b48b
BLAKE2b-256 4590d3597c7be8b7db1514d81c647a7fdd1d1c210a1dd358874ff04ab32cc64b

See more details on using hashes here.

File details

Details for the file smer_visual-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: smer_visual-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for smer_visual-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e75645279b55c70a433897982711658d3c64abc989275dc89d7366fcdd1ac080
MD5 3901a17e1840068fdbfe9d2a49249367
BLAKE2b-256 775c48af57b3b197dc2ffa6fa62bc11717daf02934d84b8d08a93459b6f3bc0b

See more details on using hashes here.

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